Accessing GL from keyPressed() method

Hi All,

I am trying to access the GL object as follows from a GLCanvas object i hold as member variable:


    public void keyPressed(KeyEvent e)
	{
		System.out.println(angle);
		if (e.getKeyCode() == KeyEvent.VK_UP)
			angle += 1f;
		if (e.getKeyCode() == KeyEvent.VK_DOWN)
			angle -= 1f;
		
		examineModelViewMatrix(canvas.getGL());
	}

However, I read the following within GLCanvas javadoc:

And this is clearly the case as my modelview matrix is all zeros when i try to access it in this way, whereas if i access it in display(), for instance, i get a matrix in the form one would expect after rotation is applied.

So, who can tell me how to access the GL from input callback methods?

Well my JOGL knowledge is a whole 4 days old, but I’ll try to answer anyway.

One idea
Set up GL gl; as a class variable and then in each init, display, and reshape function assign gl=canvas.getGL();. That way you will have access to a valid GL. Make sure to test gl in the keyPressed function to see if is null as a keyPressed event could be fired before GL is init’d.

I should say though that I doubt you need to do it this way. If you need to adjust your model view matrix based on “angle” I think you should do it in the display routine. And at the end of the keyPressed() call canvas.display()

But like I said, 4 days of JOGL knowledge. Not even worth 2 cents.

Sorry but you really can’t legally perform OpenGL work from within your event handler callbacks. What I would recommend doing is setting up some state in your GLEventListener class from your event handler callback and then call GLCanvas.display() or repaint() to cause it to redraw based on that input. Take a look at the source code for some of the JOGL demos to see how they handle mouse and keyboard input.

I’m trying to implement frustum culling, that’s why I want to get the model-view matrix only when input is received, rather than in display()

…so I’m guessing that those who have gone before me and done frustum culling with JOGL have chosen the ‘geometric’ approach where you get the 8 points from your camera class instead? Sounds like it would be more efficient…

You can easily set a flag “frustumNeedsUpdate” in your keylistener and only update your culling in display() if this flag is set. A more OO approach would be to provide a queue of jobs to be done next time display() is called, but this may be oversized.

Anyway here’s a potential implementation

Thankyou gentlemen.