Login  Register

GLJPanel and translate

Posted by gsxruk on Dec 13, 2010; 9:28pm
URL: https://forum.jogamp.org/GLJPanel-and-translate-tp2081621.html

Hi,

I have a simple test case (below), which I believe should draw a square in the centre of the window.  This is not the case with the GLJPanel as in the code.  Also, when the window is resized by dragging, the square is redrawn all over the place.  If the GLJPanel is changed to a GLCanvas, the behaviour seems to be as expected.  The square is drawn in the centre to start with and resizing of the window redraws the square as expected.

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLJPanel;
import javax.swing.JFrame;

public class Main
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        GLProfile.initSingleton(true);
        new JOGLTest();
    }

    private static class JOGLTest extends JFrame implements GLEventListener
    {
        public JOGLTest()
        {
            super("JOGL Test");
            this.setLayout(new BorderLayout());
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            initialiseJOGL();
            this.setVisible(true);
        }

        private void initialiseJOGL()
        {
            GLProfile profile = GLProfile.getDefault();
            GLCapabilities capabilities = new GLCapabilities(profile);
            capabilities.setHardwareAccelerated(true);
            capabilities.setDoubleBuffered(true);
            GLJPanel canvas = new GLJPanel(capabilities);
            canvas.setPreferredSize(new Dimension(400, 400));
            canvas.addGLEventListener(this);
            this.add(canvas, BorderLayout.CENTER);
            this.pack();
        }

        @Override
        public void init(GLAutoDrawable drawable)
        {
            GL2 gl = drawable.getGL().getGL2();
            gl.glClearColor(0, 0, 0, 0);
            gl.glLoadIdentity();
            gl.glOrtho(0, drawable.getWidth(), 0, drawable.getHeight(), -1, 1);
            gl.glMatrixMode(GL2.GL_MODELVIEW);
        }

        @Override
        public void display(GLAutoDrawable drawable)
        {
            GL2 gl = drawable.getGL().getGL2();
            gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
            //Translate to the centre
            gl.glTranslatef(200.0f, 200.0f, 0.0f);
            //Draw a blue square
            gl.glBegin(GL2.GL_POLYGON);
                gl.glColor4f(0, 0, 1.0f, 1.0f);
                gl.glVertex2f(-50, -50);
                gl.glVertex2f(-50, 50);
                gl.glVertex2f(50, 50);
                gl.glVertex2f(50, -50);
            gl.glEnd();
            gl.glFlush();
        }

        @Override
        public void reshape(GLAutoDrawable drawable,
                int left, int top, int width, int height)
        {
            GL2 gl = drawable.getGL().getGL2();
            gl.glViewport(0, 0, width, height);
            gl.glLoadIdentity();
            gl.glOrtho(0, width, 0, height, -1, 1);
        }

        @Override
        public void dispose(GLAutoDrawable drawable)
        {
            //Do nothing
        }
    }
}

Is this another GLJPanel bug?

Thanks.