How can I create my own openGL context and bind it to a GLCanvas?

和自甴很熟 提交于 2019-12-06 05:39:43

问题


So when I got to grips with the java

paint(Graphics g){}

method I moved on to creating my own render methods but I had to learn about buffer strategies and how to 'get' Graphics

So now I am learning openGL, I have got to grips with the method:

@Override
public void display(GLAutoDrawable arg0){}

and now I would like to create my own render methods, so far I have constantly run up against one exception:

Exception in thread "main" javax.media.opengl.GLException: No OpenGL context current on this thread

EDIT: Question: If I want to make glDraw calls outside of the

@Override
public void display(GLAutoDrawable arg0){}

method, how do I ensure the glContext of my GLCanvas is "current"

Preferably as a code example, as I Have looked through the api's and used google extensivly, many links you guys have already been so kind as to put here I have already found before and I am still drawing a blank... literally! Closest I've got was when it didn't throw a "no current context" exception but the screen just went black permenantly!


回答1:


I found documentation discussing this issue here:

http://fivedots.coe.psu.ac.th/~ad/jg2/ch15/index.html

The GLContext.makeCurrent()/release() hack seems to work for me under Linux/OpenJDK 6/JOGL 2, but we will see... in particular, making the OpenGL context switch thread all the time like that will incur (too much?) overhead.

Then there is NEWT of JOGL 2 and LWJGL's toolkit, but they seem to be a whole new ball of wax to handle, bye bye Swing. :(




回答2:


Duplicating my answer to your other question:

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.

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. (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.

If you are using low-level buffers, like BufferStrategy, your best bet is to do your JOGL rendering to a GLPBuffer, which is an offscreen JOGL drawable. Create the GLPBuffer, render to it, and then copy the rendered bitmap into your buffer. Some implementations of GLDrawable allow for explicit creation of offscreen drawables with "createOffscreenDrawable(...). This article will give you some pointers.

You can also call GLDrawable.display() explicitly, provided you are on the rendering thread.

If you are looking to do initialization, such as creating a display list, you can use the GLDrawable.init(...) method, which is called before the first call to display(...), although it can be called more than once.




回答3:


Dont ask me why, but for me (after so many attempts) this is working like a charm

I declared as variable

private GLCanvas gLCanvas;

and then just a

gLCanvas.repaint();

Did the job ;)



来源:https://stackoverflow.com/questions/7420027/how-can-i-create-my-own-opengl-context-and-bind-it-to-a-glcanvas

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