Login  Register

Re: display() called repeatedly, yet screen not updated?

Posted by jerron on May 25, 2011; 4:33pm
URL: https://forum.jogamp.org/display-called-repeatedly-yet-screen-not-updated-tp2979558p2984970.html

Another case using explicit swapBuffer() calls, still the same issue. The first frame is
rendered, no subsequent frames are (the OpenGL calls take place but nothing on the
screen changes, suggesting some sort of buffer issue).

--

package com.io7m.javagl1;

import javax.media.opengl.GL;
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.fixedfunc.GLMatrixFunc;

import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.opengl.GLWindow;

public final class JavaGL2 implements GLEventListener
{
  static {
    GLProfile.initSingleton(true);
  }

  private static void initGL()
  {
    final GLProfile profile = GLProfile.getDefault();
    final GLCapabilities capabilities = new GLCapabilities(profile);
    final GLWindow window = GLWindow.create(capabilities);

    window.setSize(300, 300);
    window.setVisible(true);
    window.setTitle("NEWT Window Test");
    window.addWindowListener(new WindowAdapter() {
      @Override public void windowDestroyNotify(final WindowEvent arg0)
      {
        System.exit(0);
      }
    });

    final JavaGL2 test0 = new JavaGL2();
    window.addGLEventListener(test0);

    while (true) {
      System.out.println("info: sleeping");
      try {
        Thread.sleep(1000);
        System.out.println("info: awoke, rendering");
        window.display();
        window.swapBuffers();
      } catch (final InterruptedException e) {
        // Don't care.
      }
    }
  }

  public static void main(final String[] args)
  {
    JavaGL2.initGL();
  }

  @Override public void dispose(final GLAutoDrawable drawable)
  {
    System.out.println("info: dispose - autoswap(" + drawable.getAutoSwapBufferMode() + ")");
  }

  @Override public void init(final GLAutoDrawable drawable)
  {
    drawable.setAutoSwapBufferMode(false);
    System.out.println("info: init - autoswap(" + drawable.getAutoSwapBufferMode() + ")");
  }

  private void render(final GL2 gl)
  {
    System.out.println("info: render " + System.nanoTime());

    gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrtho(0.0, 1.0, 0.0, 1.0, 0.1, 100.0);

    gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
    gl.glLoadIdentity();

    gl.glClearColor((float) Math.random(), (float) Math.random(), (float) Math.random(), 1.0f);
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
  }

  @Override public void display(final GLAutoDrawable drawable)
  {
    System.out.println("info: display " + System.nanoTime());
    this.render(drawable.getGL().getGL2());
  }

  @Override public void reshape(final GLAutoDrawable drawable,
                                final int x,
                                final int y,
                                final int width,
                                final int height)
  {
    System.out.println("info: resize " + x + "," + y + "+" + width + "x" + height);
    final GL2 gl = drawable.getGL().getGL2();
    gl.glViewport(0, 0, width, height);
  }
}