JOGL display update problem

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

JOGL display update problem

polirol
Hi everybody,

I have this problem that is actually driving me crazy: looks like the display function does not update if the changes in the scene are triggered by a key pressed/relased/typed event.

I have something like that:

[imports]

public class TestClass extends GLCanvas implements GLEventListener, KeyListener
{

private float t;
[...]

 public TestClass()
 {
    some init variables;
    addKeyListener(this);
 }

[...]

public void display(GLAutoDrawable d )
{
      GL2 gl = d.getGL().getGL2();
      gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      gl.glMatrixMode(GL_MODELVIEW);
      gl.glLoadIdentity();  

      gl.glPushMatrix();
      gl.glTranslatef(0.0f, 0.0f, -3.0f);
      glut.glutSolidTeapot(0.5f *  Math.sin(t));
      gl.glPopMatrix();


     gl.glFlush();
}

 public void keyPressed(KeyEvent ke)
  {
            t+=0.05f;
            System.out.println("Key pressed : " + ke.getKeyChar());
        }

[...]

}

Well, it doesn't work even if the keyevent is correctly captured (the println output is displayed in the console), but the teapot does not changes its dimension according on how much I press some keys. Forcing a display() method call in keyPressed does not improve the situation.

Moreover, if I put the "t+=0.05f;" statement in the display(GLAutodrawable d) method, the teapot changes its size every frame update; so the question is why the key events are captured but ignored when redrawing the scene every frame update.

Reply | Threaded
Open this post in threaded view
|

Re: JOGL display update problem

gouessej
Administrator
Hi

Please give a full example, there is probably a problem in your code as calling glCanvas.display() should work.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: JOGL display update problem

polirol
Ok, here is the full code:

import javax.media.opengl.fixedfunc.*;
import javax.media.opengl.*;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.gl2.GLUT;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.*;
import com.jogamp.opengl.util.FPSAnimator;
import static javax.media.opengl.GL.*;
import static javax.media.opengl.GL2.*;
import com.jogamp.opengl.util.texture.*;

import java.io.InputStream;



public class TestClass extends GLCanvas implements GLEventListener, KeyListener {


    private GLU glu = new GLU();
    private GLUT glut = new GLUT();
    private float t;


    public TestClass()
    {
        t = 0.0f;
        addKeyListener(this);
    }

    public void init( GLAutoDrawable d ) {
      GL2 gl = d.getGL().getGL2();
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      gl.glClearDepth(1.0f);
      gl.glEnable(GL_DEPTH_TEST);
      gl.glDepthFunc(GL_LEQUAL);
      gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
      gl.glShadeModel(GL_SMOOTH);


   float ambientLight[]={1f,1f,1f,1.0f};                 // set ambient light parameters
   gl.glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight,0);

   float lightPos[]={0.0f,30.0f,0.0f,0.0f};                    // set light position
   gl.glLightfv(GL_LIGHT0,GL_POSITION,lightPos,0);

   gl.glEnable(GL_LIGHT0);                                      // activate light0
   gl.glEnable(GL_LIGHTING);                                    // enable lighting

   }



    public void reshape(GLAutoDrawable d, int x, int y, int width, int height ){
      GL2 gl = d.getGL().getGL2();  // get the OpenGL 2 graphics context
      if (height == 0) height = 1;   // prevent divide by zero
      float aspect = (float)width / height;

      // Set the view port (display area) to cover the entire window
      gl.glViewport(0, 0, width, height);
      gl.glMatrixMode(GL_PROJECTION);
      gl.glLoadIdentity();
      glu.gluPerspective(45.0, aspect, 0.1, 100.0); // fovy, aspect, zNear, zFar

      // Enable the model-view transform
      gl.glMatrixMode(GL_MODELVIEW);
      gl.glLoadIdentity(); // reset

    }


    public void display( GLAutoDrawable d ) {

      GL2 gl = d.getGL().getGL2();
      gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers
      gl.glMatrixMode(GL_MODELVIEW);
      gl.glLoadIdentity();  // reset the model-view matrix



      gl.glPushMatrix();
      gl.glTranslatef(0.0f,0.0f,-3f);
      glut.glutSolidTeapot(0.5f * Math.sin(t) );
      gl.glPopMatrix();

     // t+=0.05f;

      //d.swapBuffers();
      gl.glFlush();

    }



    public static void main( String[] args ) {

                JFrame frame = new JFrame("test class");
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


                GLCanvas canvas = new TestClass();
                canvas.setSize(800,800);

                final Animator animator = new Animator(canvas);

        TestClass listener = new TestClass();
                canvas.addGLEventListener( listener );

                frame.getContentPane().add( canvas, BorderLayout.CENTER );

                frame.pack();
                frame.setVisible( true );
                animator.start();

        }



  public void displayChanged( GLAutoDrawable d, boolean modeChanged,
            boolean deviceChanged ) { }

  public void dispose(GLAutoDrawable d)
  {
  System.out.println("Exiting");
  }



  ///KeyEvents///


  public void keyReleased(KeyEvent ke)
  { }

  public void keyTyped(KeyEvent ke){}


  public void keyPressed(KeyEvent ke)
  {
  System.out.println("Key pressed : " + ke.getKeyChar());

  switch(ke.getKeyChar())
  {

   //TODO

  }

  t+=0.5f;

  }



}

Thank you very much for your time

Reply | Threaded
Open this post in threaded view
|

Re: JOGL display update problem

gouessej
Administrator
static imports and the call to glFlush are useless. I see no call to display() in your methods implementing the key listener.

Edit.: I don't see why your source code doesn't work.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: JOGL display update problem

polirol
This post was updated on .
Thanks for the info about the imports and glFlush.

As for the display() call, I removed it because it does not work.
I tried to put it just after t+=0.05f; in the keyPressed method:

[...]

public void keyPressed(...)
{
[...]

t+=0.05f;
display();
}

Reply to your edit: I also don't see why it is not working... :(, I have seen lots of examples using key events in JOGL and I don't think mine is that different. However frames are just not updating. I found this while surfing the web : http://forum.jogamp.org/display-called-repeatedly-yet-screen-not-updated-td2979558.html and as far as I understand the problem is very similar. Looks like a bug for freeBSD, but I use windows 7 with jogl-2.0-b867-20121204-windows-amd64. Can it be a bug or a conflicting issue between this specific JOGL version with my JDK?
Reply | Threaded
Open this post in threaded view
|

Re: JOGL display update problem

Sven Gothel
Administrator
In reply to this post by polirol
On 12/29/2012 12:02 PM, polirol [via jogamp] wrote:

> Hi everybody,
>
> I have this problem that is actually driving me crazy: looks like the display
> function does not update if the changes in the scene are triggered by a key
> pressed/relased/typed event.
>
> I have something like that:
>
> [imports]
>
> public class TestClass extends GLCanvas implements GLEventListener, KeyListener
> {
>
> private float t;
-> private _volatile_ float t;

volatile is your magic word here,
which gives you build-in thread synchronization (updates).

be aware .. this is a multi threaded use case already,
i.e. renderer and EDT (key/mouse input, etc) thread.

~Sven


signature.asc (909 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: JOGL display update problem

polirol
I tried by modifying

private float t;
in
private volatile float t;

and I am still getting the same results :\.

I have no experience in volatile variables, so should I maybe also change something else in the code?
Reply | Threaded
Open this post in threaded view
|

Re: JOGL display update problem

gouessej
Administrator
You create 2 instances of TestClass, your main method was completely wrong. This source code works (without volatile):

import javax.media.opengl.fixedfunc.*;
import javax.media.opengl.*;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.gl2.GLUT;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.*;
import com.jogamp.opengl.util.FPSAnimator;
import static javax.media.opengl.GL.*;
import static javax.media.opengl.GL2.*;
import com.jogamp.opengl.util.texture.*;

import java.io.InputStream;



public class TestClass extends GLCanvas implements GLEventListener, KeyListener {


    private GLU glu = new GLU();
    private GLUT glut = new GLUT();
    private float t;


    public TestClass()
    {
        t = 0.0f;
        addKeyListener(this);
    }

    public void init( GLAutoDrawable d ) {
      GL2 gl = d.getGL().getGL2();
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      gl.glClearDepth(1.0f);
      gl.glEnable(GL_DEPTH_TEST);
      gl.glDepthFunc(GL_LEQUAL);
      gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
      gl.glShadeModel(GL_SMOOTH);


   float ambientLight[]={1f,1f,1f,1.0f};                 // set ambient light parameters
   gl.glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight,0);

   float lightPos[]={0.0f,30.0f,0.0f,0.0f};                    // set light position
   gl.glLightfv(GL_LIGHT0,GL_POSITION,lightPos,0);

   gl.glEnable(GL_LIGHT0);                                      // activate light0
   gl.glEnable(GL_LIGHTING);                                    // enable lighting

   }



    public void reshape(GLAutoDrawable d, int x, int y, int width, int height ){
      GL2 gl = d.getGL().getGL2();  // get the OpenGL 2 graphics context
      if (height == 0) height = 1;   // prevent divide by zero
      float aspect = (float)width / height;

      // Set the view port (display area) to cover the entire window
      gl.glViewport(0, 0, width, height);
      gl.glMatrixMode(GL_PROJECTION);
      gl.glLoadIdentity();
      glu.gluPerspective(45.0, aspect, 0.1, 100.0); // fovy, aspect, zNear, zFar

      // Enable the model-view transform
      gl.glMatrixMode(GL_MODELVIEW);
      gl.glLoadIdentity(); // reset

    }


    public void display( GLAutoDrawable d ) {

      GL2 gl = d.getGL().getGL2();
      gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers
      gl.glMatrixMode(GL_MODELVIEW);
      gl.glLoadIdentity();  // reset the model-view matrix



      gl.glPushMatrix();
      gl.glTranslatef(0.0f,0.0f,-3f);
      glut.glutSolidTeapot(0.5f * Math.sin(t) );
      System.out.println("Display : " + t);
      gl.glPopMatrix();

     // t+=0.05f;

      //d.swapBuffers();
      gl.glFlush();

    }



    public static void main( String[] args ) {

                JFrame frame = new JFrame("test class");
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


                TestClass canvas = new TestClass();
                canvas.setSize(800,800);

                final Animator animator = new Animator(canvas);
                canvas.addGLEventListener( canvas );

                frame.getContentPane().add( canvas, BorderLayout.CENTER );

                frame.pack();
                frame.setVisible( true );
                animator.start();

        }



  public void displayChanged( GLAutoDrawable d, boolean modeChanged,
            boolean deviceChanged ) { }

  public void dispose(GLAutoDrawable d)
  {
  System.out.println("Exiting");
  }



  ///KeyEvents///


  public void keyReleased(KeyEvent ke)
  { }

  public void keyTyped(KeyEvent ke){}


  public void keyPressed(KeyEvent ke)
  {
  System.out.println("Key pressed : " + ke.getKeyChar() + " " + t);

  switch(ke.getKeyChar())
  {

   //TODO

  }

  t+=0.5f;

  }



}
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: JOGL display update problem

polirol
Hi again,


yeah, now I see I had two instances running. I changed the main method code code as you suggested and now it is working.

To be more specific, in order to be able to compile without errors I also had to change


canvas.addGLEventListener( canvas );
in
canvas.addGLEventListener( (GLEventListener) canvas );

Thank you very much :)
Reply | Threaded
Open this post in threaded view
|

Re: JOGL display update problem

gouessej
Administrator
The source code I provided contains no compile error, I modified the type of the variable "canvas", TestClass already implements GLEventListener.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: JOGL display update problem

polirol
You're right, I missed it.

Thank you again.