Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: openGL: JOGL: What happens behind the scenes when I call the display() method?

Tags:

java

opengl

jogl

I have this line of code:

renderableObject.renderObject(gl, glu);

This leads to a large list of objects being rendered by openGL, however it only works when used as follows:

@Override
public void display(GLAutoDrawable drawable)
    {               
        renderableObject.renderObject(gl, glu);
    }

If I call the line outside the overridden display method I get an Exception saying there is no glContext on the current thread, actually if I call any gl draw commands outside this method I get the same exception

now ideally I want to create a lot of display lists once, then render them every frame with the odd display list to be recreated periodically. However I have to go through this single display() method which means I would have to test every frame if the display list has been created, or is in need of change etc... 60 times a second! what a waste of processing power when I could handle them separately once when needed.

So whatever calling the display() method does, I would like to be able to replicate it allowing me to create a plethora of my own custom display methods, without going through this one method for everything!

So is there a simple gl call I can make myself?

like image 835
Troyseph Avatar asked Nov 04 '22 14:11

Troyseph


1 Answers

Odd as it may seem, this is the way it is supposed to work.

Behind the scenes what is going on is that when the GLCanvas you have created comes to be drawn, behind the scenes JOGL is doing a whole pile of work. It is creating a GLContext, and making it current for the GLCanvas for the current thread. Only when that is done can you make rendering calls. A GLContext that has not been made current, or a GL object that derives from it, is no use to you. Additionally the GLContext is made current only for that thread, and is made non-current as soon as the display call is finished, so hanging on to a reference to it or the GL for later use won't work.

Almost all JOGL applications work that way. You create a GLEventListener, and implement display() in it, extract a GL from the GLAutoDrawable and use it to make rendering calls. You don't want to make rendering calls in any other place, any more than you want to make Graphics2D calls outside of the paint() method. Most beginner Java programmers try to paint from outside the paint method; this is similar. If you need to trigger a repaint then you do it the same way you would with Java2D: use invalidate(). (You can of course write submethods that are called from the display() method, and which take the GL or GLAutoDrawable as an argument).

There are ways for you to specifically create a GLContext and make it current yourself, but they are rarely necessary. It is almost always better to use the approach here.

like image 179
DJClayworth Avatar answered Nov 11 '22 05:11

DJClayworth