Search code examples
c++openglrender-to-texture

How to draw two layers with different fps rate?


I want to draw two layers in my scene (let us say main layer and debug info layer). Now I draw my layers in this way:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
layer1->draw();
layer2->draw();

I want do something like this:

static int frameCount = 0;

layer1->draw();

if (frameCount % 2) {
   layer2->draw();
}

frameCount++;

But I don't want draw my second layer each second frame. I want draw this layer each frame, but redraw each second. For example, I'm drawing my layer in this way:

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

glDrawArrays(GL_POINTS, 0, vertexNumber);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

How can I redraw this layer only each second frame?


Solution

  • you have to use backbuffering technique so:

    1. rewrite your rendering so it outputs to a texture instead of to Screen
    2. in your redraw event use the backbuffer textures

    There are two options

    1. each layer has its own target texture and in redraw you combine them to screen
    2. the layer which is updated every redraw has no texture and it is rendered directly to device and on top of that is rendered trasparent texture of the slower layer.

    Choose what is better for you. I usually have bool _redraw for each redraw and rerender the texture only if something changed in the layer. But visualize the content of texture each frame ...