0
\$\begingroup\$

I have a very simple OpenGL view rendering 2 triangles with a single texture applied. The minimum setup for rendering a 2d game.

What i do is redraw the texture for every frame and easily get 60fps.

I would like to add an effect simulating LCD slow response by outputting pixels that are the average between the current and the previous frame.

The pseudo code of fragment shader would look like this

varying vec2 v_TexCoordinate;
uniform sampler2D u_Texture;

void main() {
    gl_FragColor   = (texture2D(u_Texture, v_TexCoordinate) + PIXEL_PREVIOUS_FRAME) / 2;
    PIXEL_PREVIOUS_FRAME = gl_FragColor;
};

Well, i think shaders can't keep a persistent variable like PIXEL_PREVIOUS_FRAME, then i really don't know how to tackle this problem.

Any suggestion?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Look up "rendering to textures" in OpenGL. This will let you render your scenes to alternating render targets backed by textures named, say, A and B. By alternating them, one texture always contains “this frame” and the other the “last frame.”

So every frame, you'd:

  1. Swap A and B.
  2. Bind A as a render target; render the game as normal to A.
  3. Bind A and B as textures to read from.
  4. Render a single triangle (or quad) that covers the full screen using a fragment shader that samples from both A and B and blends them however you want.
\$\endgroup\$
2
  • \$\begingroup\$ Could you please explain it more extensively? Should i create 2 textures, apply both on the polygons and pass them both as uniform to fragment shader? \$\endgroup\$ Apr 10, 2019 at 15:13
  • \$\begingroup\$ Sure; edited. Basically you make two textures and use them render targets. If you're not familiar with rendering to textures (instead of the framebuffer) in OpenGL, you should research that, as there are a variety of ways to do. Once you have those two textures, you bind them as inputs for the last phase. This is a fairly basic application of a post-processing technique, so you can do some more research on that general subject as well to get more specific examples related to precisely how you are currently using OpenGL. \$\endgroup\$
    – user1430
    Apr 10, 2019 at 16:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .