Sunday, June 24, 2018

Can I leverage the fact that my scene is often static to improve OpenGL (JOGL) performance?


My scene is drawn based on the location of several (often several million) vertices (kept in VBO's) and a camera. I can easily tell in my code when my scene has changed and when it hasn't. There are also some odd cases such as the window being resized, but I believe I can easily enumerate and handle those as well.


Can I (in user code or through some OpenGL property) leverage this to increase the performance when the scene is static? Clearly when the scene is changing, all of the math must be done to properly calculate what should be rendered. But when the scene is static, that picture isn't changing each frame.


I've tried implementing something in my code to do this, but the result is a flickering scene (and I'm not entirely sure why). Basically I check to see if anything has changed and if it hasn't I simply return from the display(GLAutoDrawable drawable) function that is invoked by the JOGL FPSAnimator.


I feel like this is probably a common problem that should have a standard solution. However, I haven't been able to find anything so far.



Answer




You can render the scene to a FBO when it changes, and render the FBO to the screen every frame. It's a little more effort than just enabling a property, but still quite straightforward to implement. This approach also allows you to render something on the background and foreground independent of the scene.


edit: Check this tutorial on using FBOs. You need to do the following initialization steps:



  1. Create and bind a Frame Buffer Object

  2. Create a Renderbuffer Object same size as the screen. Bind it to GL_DEPTH_COMPONENT. Renderbuffers are buffers that you are not using as a texture. In this case you need a Renderbuffer for depth buffer.

  3. Create a texture same size as the screen. Specify the format to include alpha channel, if you want to render something behind the scene also.

  4. Attach both the Renderbuffer object and the texture to the FBO (Attaching images to FBO)


When rendering the scene to the FBO:




  1. Bind the Frame Buffer Object

  2. Render the scene

  3. Bind 0 to restore normal screen rendering


When rendering the FBO to the screen:



  1. Bind the texture attached to the FBO

  2. Render a full-screen quad. Provide texture coordinates for each vertex spanning the whole texture range.


This should be enough for you. If you still need further help, I can try to provide working code sample when I have time.



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...