HELP: Rendering to texture

39 views
Skip to first unread message

AL

unread,
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");
 shader->Set(renderer);

 fplbase::RenderTarget bufferFrame1;
 bufferFrame1.Initialize(renderer.window_size());

 fplbase::RenderTarget bufferFrame2;
 bufferFrame2.Initialize(renderer.window_size());

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

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

  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.
  bufferFrame1.BindAsTexture(0);

//Render texture (bufferFrame1) to bufferFrame2
  bufferFrame2.SetAsRenderTarget();

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

//Swap
  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
  fplbase::RenderTarget::ScreenRenderTarget(renderer).SetAsRenderTarget();

  fplbase::Mesh::RenderAAQuadAlongX(
   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
Repeat

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).


Untitled.jpg

AL

unread,
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

unread,
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 <emailfo...@gmail.com> 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 fplbaselib+unsubscribe@googlegroups.com.
To post to this group, send email to fplba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fplbaselib/8c58e09a-df0b-4ca1-9ebc-51d424814735%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages