Search code examples
c++opengl

OpenGL: How do i control the rendering to make it idle?


Say, my application has a 3D window rendering a tin model of millions of triangles using OpenGL.

Goal: For some operations of users, there is no need to update the 3D window. The 3D view can just stay idle with previously rendered content, without repeatly calulate the rotation/translation/scaling/texture things. I assume this will save lots of CPU and GPU time.

Current design: I have a rendering while loop running all the time. If I stop the while loop, then the content is rendered once and disappear.

Question: is there a way to achieve the goal? Can anyone give a direction?


Solution

  • Instead of rendering in your event loop, like many OpenGL tutorials teach you, you can use OpenGL to render in response to the system's draw event. You can also invalidate your own window if you know that the contents of it changed (e.g. due to a mouse click). In fact this is the proper way to draw in your window for anything other than latency sensitive applications.

    The specifics depend on the API you are using to create your window. For example,

    • With WinAPI you render on WM_PAINT and invalidate with InvalidateRect.
    • With Xlib you render on Expose and invalidate by sending an Expose event with XSendEvent.
    • With Qt you render in QOpenGLWindow::paintGL and invalidate with update().
    • With GLUT you render in glutDisplayFunc and invalidate with glutPostRedisplay.

    ... and so on. This is not OpenGL specific in any way.