jogl
  1. jogl
  2. JOGL-14

Incorrect rendering when calling glOrtho() outside of GLDrawable.reshape()

    Details

    • Issuezilla Id:
      14

      Description

      Please see the attached reproduction case. When this program is compiled with
      setCameraInRepaint=true, the app renders correctly (diagonal and vertical lines,
      plus a small box at bottom left corner). When setCameraInRepaint is false, nothing
      renders.

      I am unsure as to why the app does not render correctly in the second case,
      since glOrtho() is being called in display() and hence should be called after the call
      to glViewport() is made in GLCanvas.reshape().

      I suspect some strange behavior with respect to the AWT integration and
      threading – perhaps because GLCanvas.reshape() is called in a different thread
      from that used to call GLDrawable.reshape()?

      --------

      import java.awt.Frame;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;

      import net.java.games.jogl.*;

      /**

      • Vectrix
      • @author John
      • @author Ckline modified to demostrate bug
        */
        public class Vectrix implements GLEventListener
        {
        // BUG REPRO: app does not render correctly when setCameraInReshape is false
        private boolean setCameraInReshape = true;

      public static Vectrix app = new Vectrix();

      private Frame frame;

      // private MouseHandler mouseHandler;
      private Animator animator;

      private GLCanvas canvas;

      /**

      • */
        private Vectrix()
        {
        System.out.println("Vectrix instance created");

      // Canvas setup
      GLCapabilities caps = new GLCapabilities();
      caps.setDoubleBuffered(true);
      caps.setAlphaBits(8);
      caps.setStencilBits(8);
      canvas = GLDrawableFactory.getFactory().createGLCanvas(caps);

      //canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities
      ());
      canvas.addGLEventListener(this);
      canvas.setGL(new DebugGL(canvas.getGL()));
      // Frame setup
      frame = new Frame("Vectrix");
      frame.add(canvas);
      frame.setSize(800, 600);

      frame.addWindowListener(
      new WindowAdapter()
      {
      public void windowClosing(WindowEvent e)

      { animator.stop(); System.exit(0); }

      }
      );

      // Event handlers
      animator = new Animator(canvas);
      }

      public void initialiseDisplay()

      { frame.show(); }

      public void run()

      { animator.start(); }

      public static void main(String[] args)

      { System.out.println("Vectrix starting..."); app = new Vectrix(); app.initialiseDisplay(); // Other loading/initialisation // .. // Start main game loop app.run(); }

      // GL Event Listener methods

      // Called when GL has been initialised for the first time.
      public void init(GLDrawable canvas)

      { System.out.println("Vectrix.init(): GL init event"); GL gl = canvas.getGL(); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); gl.glMatrixMode(GL.GL_TEXTURE); gl.glLoadIdentity(); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); }

      public void display(GLDrawable canvas)
      {
      //System.err.println("DISPLAY THREAD: " + Thread.currentThread());
      GL gl = canvas.getGL();
      GLU glu = canvas.getGLU();

      gl.glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT |
      GL.GL_STENCIL_BUFFER_BIT);

      if (!setCameraInReshape)

      { // l/r b/t near/far gl.glMatrixMode(GL.GL_PROJECTION); gl.glOrtho(0, 4, 0, 3, -8, 8); gl.glMatrixMode(GL.GL_MODELVIEW); }

      gl.glPushMatrix();
      {
      // draw a polygon
      gl.glBegin(GL.GL_POLYGON);
      gl.glColor3f(1f, 0f, 0f);
      gl.glVertex3f(.25f, .25f, 0f);
      gl.glVertex3f(.75f, .25f, 0f);
      gl.glVertex3f(.75f, .75f, 0f);
      gl.glVertex3f(.25f, .75f, 0f);
      gl.glEnd();

      // draw some lines
      gl.glBegin(GL.GL_LINES);
      {
      int xMin = 0;
      int xMax = 10;
      gl.glColor3f(1f, 0f, 0f);

      for (int x=xMin; x<=xMax; x++)

      { gl.glVertex3f(x, -1, 0f); gl.glVertex3f(x, +1, 0f); }

      gl.glVertex3f(0f, 0f, 0f);
      gl.glVertex3f(4f, 4f, 4f);

      }
      gl.glEnd();

      }
      gl.glPopMatrix();

      }

      public void reshape(GLDrawable arg0, int arg1, int arg2, int arg3, int arg4)
      {
      //System.err.println("RESHAPE THREAD: " + Thread.currentThread());
      GL gl = arg0.getGL();

      System.out.println("Vectrix.init(): GL reshape event " + arg1 + " " + arg2
      + " " + arg3 + " " + arg4);

      if (setCameraInReshape)

      { // l/r b/t near/far gl.glMatrixMode(GL.GL_PROJECTION); gl.glOrtho(0, 4, 0, 3, -8, 8); }

      }

      public void displayChanged(GLDrawable arg0, boolean arg1, boolean arg2)
      {

      }
      }

        Activity

        Hide
        ckline added a comment -

        This bug was initiated from the discussion at

        http://www.javagaming.org/cgi-bin/JGNetForums/YaBB.cgi?
        board=jogl;action=display;num=1056274375

        Show
        ckline added a comment - This bug was initiated from the discussion at http://www.javagaming.org/cgi-bin/JGNetForums/YaBB.cgi? board=jogl;action=display;num=1056274375
        Hide
        ckline added a comment -

        After some experimentation, I now believe this is not a bug. If you change all the
        calls around glOrtho() to look like this:

        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glLoadIdentity()
        gl.glOrtho(...)
        gl.glMatrixMode(gl.GL_MODELVIEW)

        the app works correctly regardless of the value of setCameraInReshape. Unless
        further issues crop up, I would like to declare this not-a-bug.

        Show
        ckline added a comment - After some experimentation, I now believe this is not a bug. If you change all the calls around glOrtho() to look like this: gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(...) gl.glMatrixMode(gl.GL_MODELVIEW) the app works correctly regardless of the value of setCameraInReshape. Unless further issues crop up, I would like to declare this not-a-bug.

          People

          • Assignee:
            kbr
            Reporter:
            ckline
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: