PDA

View Full Version : Problem with Render to Texture Using FBO



sinaxp19
05-25-2015, 02:02 AM
Hi, everyone. I'm trying to render my main screen to a texture (2D graph) and use it on a Quad to show the final results. I intend to use different sampling methods on textures and avoid the need to rebuild my screen data everytime. (just update the texture if needed) I don't know what, but there seems to be a problem with my code. Can anyone point out the mistakes I might've made? For now, I just want to make a simple dotted pattern and print it on a Quad.
[this is written in C#, using OpenTK. Aside from minor differences, everything is basically the same.]
1. Initialization


// BEGIN -> FBO
TKGL.GenFramebuffers(1, out _fbo);
TKGL.GenRenderbuffers(1, out _rbo); // Keeps Depth Buffer Data
TKGL.GenTextures(1, out _tbo); // Keeps Color Buffer Data

TKGL.BindTexture(OpenTK.Graphics.OpenGL.TextureTar get.Texture2D, _tbo);
TKGL.TexImage2D(
TextureTarget.Texture2D,
0, // int Level
PixelInternalFormat.Rgba, // Pixel Internal Format
128, // int Width
128, // int Height
0, // int Border
PixelFormat.Rgba, // Pixel Format
PixelType.UnsignedByte, // Pixel Type
IntPtr.Zero // Pixels(null for now)
);
TKGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0);
TKGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, 0);

TKGL.BindRenderbuffer(RenderbufferTarget.Renderbuf fer, _rbo);
TKGL.RenderbufferStorage(RenderbufferTarget.Render buffer, RenderbufferStorage.DepthComponent, 128, 128);
TKGL.BindRenderbuffer(RenderbufferTarget.Renderbuf fer, 0);

TKGL.BindFramebuffer(OpenTK.Graphics.OpenGL.Frameb ufferTarget.Framebuffer, _fbo);
TKGL.FramebufferTexture2D(
FramebufferTarget.Framebuffer,
FramebufferAttachment.ColorAttachment0,
TextureTarget.Texture2D,
_tbo,
0 // Level
);
TKGL.FramebufferRenderbuffer(
FramebufferTarget.Framebuffer,
FramebufferAttachment.DepthAttachment,
RenderbufferTarget.Renderbuffer,
_rbo
);

if (TKGL.CheckFramebufferStatus(FramebufferTarget.Fra mebuffer) != FramebufferErrorCode.FramebufferComplete) throw new Exception();
TKGL.Flush();
TKGL.BindFramebuffer(FramebufferTarget.Framebuffer , 0);


2. And Rendering to Texture:


TKGL.BindTexture(TextureTarget.Texture2D, 0); // Unlink
TKGL.BindFramebuffer(FramebufferTarget.Framebuffer , _fbo);

TKGL.ClearColor(1f, 0f, 0f, 1f);
TKGL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
TKGL.Begin(PrimitiveType.Quads);
TKGL.Color4(1f, 1f, 0f, 1f); TKGL.Vertex2(0, 0);
TKGL.Color4(1f, 0f, 1f, 1f); TKGL.Vertex2(128, 0);
TKGL.Color4(1f, 1f, 1f, 1f); TKGL.Vertex2(0, 128);
TKGL.Color4(0f, 1f, 0f, 1f); TKGL.Vertex2(-0.25, 0.25);
TKGL.End();


3. And using supposed created texture


TKGL.Enable(OpenTK.Graphics.OpenGL.EnableCap.Textu re2D);
TKGL.BindFramebuffer(OpenTK.Graphics.OpenGL.Frameb ufferTarget.Framebuffer, 0);
TKGL.BindTexture(OpenTK.Graphics.OpenGL.TextureTar get.Texture2D, _tbo);
TKGL.Viewport(0, 0, Width, Height);

TKGL.MatrixMode(OpenTK.Graphics.OpenGL.MatrixMode. Projection);
TKGL.LoadIdentity();
TKGL.TexEnv(OpenTK.Graphics.OpenGL.TextureEnvTarge t.TextureEnv, TextureEnvParameter.TextureEnvMode, (float)OpenTK.Graphics.OpenGL.TextureEnvMode.Decal );
TKGL.Begin(OpenTK.Graphics.OpenGL.PrimitiveType.Qu ads);
TKGL.TexCoord2(0, 0); TKGL.Vertex2(-0.5f, -0.5f);
TKGL.TexCoord2(1, 0); TKGL.Vertex2(-0.5f, 0.5f);
TKGL.TexCoord2(1, 1); TKGL.Vertex2(0.5f, 0.5f);
TKGL.TexCoord2(0, 1); TKGL.Vertex2(0.5f, -0.5f);
TKGL.End();


But all I get is a quad in the center of the screen with the color equal to the last color used in render to texture part (as seen in section 2 above, it's green here). There doesn't seem to be any problem with projection matrices since I've tried every possible combination of them.

I used these tutorials:
http://www.songho.ca/opengl/gl_fbo.html
wiki.lwjgl.org/index.php?title=Render_to_Texture_with_Frame_Buffe r_Objects_(FBO)
http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/