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.]
- 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.TextureTarget.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.Renderbuffer, _rbo);
TKGL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent, 128, 128);
TKGL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0);
TKGL.BindFramebuffer(OpenTK.Graphics.OpenGL.FramebufferTarget.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.Framebuffer) != FramebufferErrorCode.FramebufferComplete) throw new Exception();
TKGL.Flush();
TKGL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
- 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);
[b]TKGL.Color4(0f, 1f, 0f, 1f)[/b]; TKGL.Vertex2(-0.25, 0.25);
TKGL.End();
- And using supposed created texture
TKGL.Enable(OpenTK.Graphics.OpenGL.EnableCap.Texture2D);
TKGL.BindFramebuffer(OpenTK.Graphics.OpenGL.FramebufferTarget.Framebuffer, 0);
TKGL.BindTexture(OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, _tbo);
TKGL.Viewport(0, 0, Width, Height);
TKGL.MatrixMode(OpenTK.Graphics.OpenGL.MatrixMode.Projection);
TKGL.LoadIdentity();
TKGL.TexEnv(OpenTK.Graphics.OpenGL.TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (float)OpenTK.Graphics.OpenGL.TextureEnvMode.Decal);
TKGL.Begin(OpenTK.Graphics.OpenGL.PrimitiveType.Quads);
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:
wiki.lwjgl.org/index.php?title=Render_to_Texture_with_Frame_Buffer_Objects_(FBO)