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

微笑、不失礼 提交于 2019-12-05 10:37:58

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!