Draw offscreen with JOGL

2019-02-24 10:13发布

问题:

As part of a larger project I'm trying to implement a facility using JOGL that will export 3D renderings to bitmap formats. We do this by creating a GLJPanel and drawing the scene we want to it, then extracting the bitmap. This all works fine as long as the system has at least one visible window on the screen - not necessarily the window containing the panel we are drawing to.

But if we try to do this without making any window visible the GLJPanel won't draw. Stepping through the JOGL source I find that it won't draw unless it has a valid peer - essentially unless addNotify() has been called on it. The documentation says that addNotify() is only called when the panel is made part of a visible window heirarchy. Changing to a GLCanvas doesn't make much difference - the failure mode is different. WindowsOnscreenGLDrawable.realized is not set, and this means lockSurface returns LOCK_SURFACE_NOT_READY, causing makeCurrent() to fail.

Any help would be welcome on how to create a Java app that can create and export 3D scenes without having to make it's window visible.

回答1:

Not sure if I should be answering my own question, but here goes with what I found seems to work now.

The key is GLPbuffer, which is an offscreen GLAutoDrawable and can be created without a visible component heirarchy.

This article was helpful in getting it to work. I'll leave off accepting this answer until I've confirmed it's fully functional.

I should also say that the answer came from this forum, not my own meagre brain.



回答2:

You should look into method: glReadPixels() more info here. Basically it works more or less like this:

Init(); //doing some initializations in your JOGL app

glDrawBuffer(GL_BACK);

DrawGLScene(); //doing drawing here

glReadBuffer(GL_BACK);

//Copy the image to the array imageData
glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, imageData);