HELP: Rendering to texture

Skip to first unread message


Sep 24, 2016, 5:10:02 AM9/24/16
to fplbaselib
Newbie here. I'm trying to learn shaders using fplbase for about 3days now. I'm new in OpenGL, SDL, GLSL, Geometry (not new but I suck at it) and whatnots.

What I want to achieve is to save the changes made in a texture (displayed on screen) and use it for the next frame (next iteration).
I'm using fplbase::RenderTarget to do this. Two "frame buffers" (I think that's what it's called?) taking turns to be rendered on screen.

What I did:
auto shader = assetman.LoadShader("shaders/test_shader1");

 fplbase::RenderTarget bufferFrame1;

 fplbase::RenderTarget bufferFrame2;

 fplbase::RenderTarget* bufferFramePtr1 = NULL;
 fplbase::RenderTarget* bufferFramePtr2 = NULL;

while (!input.exit_requested()) {
  renderer.AdvanceFrame(input.minimized(), input.Time());

  const mathfu::vec2 res(renderer.window_size());
  const float aspect_ratio = res.x() / res.y();
  renderer.set_model_view_projection(mathfu::mat4::Ortho(-aspect_ratio, aspect_ratio, -1.0f, 1.0f, -1.0, 1.0));

  renderer.ClearFrameBuffer(mathfu::vec4(0.0f)); //Do I really need to do this every iteration?

  bufferFramePtr1 = &bufferFrame1;
  bufferFramePtr2 = &bufferFrame2;

//after this iteration, the other frame buffer will be set as the active texture.

//Render texture (bufferFrame1) to bufferFrame2

   mathfu::vec3(-aspect_ratio, 1.0f, 0.0f),
   mathfu::vec3(aspect_ratio, -1.0f, 0.0f));

  bufferFrame1 = *bufferFramePtr2;
  bufferFrame2 = *bufferFramePtr1;

  renderer.ClearFrameBuffer(mathfu::vec4(0.0f)); //Had to put his here for it work. I don't know why?

//Render texture to screen

   mathfu::vec3(-aspect_ratio, 1.0f, 0.0f),
   mathfu::vec3(aspect_ratio, -1.0f, 0.0f));

bufferFrame1 is texture1.
bufferFrame2 is texture2.

Render texture1 to texture2
Render texture1 to screen
Swap texture1 and texture2

Here are my shaders (vertex and fragment):
attribute vec4 aPosition;
attribute vec2 aTexCoord;
varying vec2 vTexCoord;
uniform mat4 model_view_projection;
void main(){
gl_Position = model_view_projection * aPosition;
vTexCoord = aTexCoord;

uniform sampler2D texture_unit_0;
varying vec2 vTexCoord;
void main() {
gl_FragColor = texture2D(texture_unit_0, vTexCoord);
gl_FragColor.r += 0.01;

The result should be the screen fading into red. It's happening but some weird jagged lines are showing (see attached jpg).



Sep 24, 2016, 6:15:11 AM9/24/16
to fplbaselib
Does anyone know what the problem is? Why these weird lines are showing when it's just supposed to be a solid red color (black fade to red)?

Wouter van Oortmerssen

Oct 3, 2016, 4:32:21 PM10/3/16
to AL, fplbaselib
From your code, there's nothing obviously causing the weird lines.

There's one major problem though, you are copying whole RenderTarget objects, and you're doing so without an intermediary, so one buffer will overwrite the other.

To fix that, remove the Ptr variables and their use entirely, and to swap, write std::swap(bufferFrame1, bufferFrame2); Or even cleaner, initialize the pointers before the loop, swap only the the pointers, and access the buffers inside the loop only over pointers.

As for having to clear the framebuffers, typically if you overwrite them entirely you don't need to clear, unless the framebuffer has a depth buffer attached and you have depth test on. If your program is meant to be 2D, you can probably turn the depth buffer off when you call Initialize.

On Sat, Sep 24, 2016 at 3:15 AM, AL <> wrote:
Does anyone know what the problem is? Why these weird lines are showing when it's just supposed to be a solid red color (black fade to red)?

You received this message because you are subscribed to the Google Groups "fplbaselib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
To view this discussion on the web visit

For more options, visit

Reply all
Reply to author
0 new messages