GLJPanel and translate

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

GLJPanel and translate

gsxruk
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.
Reply | Threaded
Open this post in threaded view
|

Re: GLJPanel and translate

gsxruk
Hi,

I seem to have solved this problem by adding pushMatrix() and popMatrix() as shown in the amended display method below:

@Override
public void display(GLAutoDrawable drawable)
{
    GL2 gl = drawable.getGL().getGL2();
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
    gl.glPushMatrix();
    //Translate to the centre
    gl.glTranslated(200, 200, 0.0);
    //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.glPopMatrix();
    gl.glFlush();
}

However, I now seem to have discovered a further issue.  If the display() method is amended slightly (just reducing the translation in y and amending the polygon to all positive values), the resize behaviour is strange again.  If you drag the window and decrease the size in y, when it gets very small (just before the polygon is about to clip), the polygons right edge disappears to the right and appears to go on forever.  No matter how far the window is increased in size in x, the polygon is drawn past that point.  Here is the amended method.

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

Does anyone have any ideas?

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: GLJPanel and translate

Xfel
Try glLoadIdentity(); at the beginning of your display code. This will reset the translation matrix as you need