Hello. I have a problemm with my OpenGL application and i dont know how to fix this issue. The problemm is that sometimes my render result looks like the image on the left. But it should look like the image on the right side.
I render my scene to an framebuffer which gets rendered in the end to the screen.
This is how i create an framebuffer
public Framebuffer BuildFramebuffer(int width, int height)
{
int framebuffer = gl.GenFramebuffers(1);
gl.BindFramebuffer(OpenGL.FrameBuffer, framebuffer);
int texture = gl.GenTextures(1);
gl.BindTexture(OpenGL.Texture2D, texture);
gl.TexImage2D(OpenGL.Texture2D, 0, OpenGL.RGBA, width, height, 0, OpenGL.RGBA, OpenGL.UnsignedByte);
gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureMinFilter, NetGL.OpenGL.Linear);
gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureMagFilter, NetGL.OpenGL.Linear);
gl.FrameBufferTexture2D(OpenGL.FrameBuffer, OpenGL.ColorAttachment0, OpenGL.Texture2D, texture, 0);
int rbo = gl.GenRenderbuffers(1);
gl.BindRenderbuffer(OpenGL.RenderBuffer, rbo);
gl.RenderbufferStorage(OpenGL.RenderBuffer, OpenGL.DepthComponent24, width, height);
gl.BindRenderbuffer(OpenGL.RenderBuffer, 0);
gl.FramebufferRenderbuffer(OpenGL.FrameBuffer, OpenGL.DepthAttachment, OpenGL.RenderBuffer, rbo);
if (gl.CheckFramebufferStatus(OpenGL.FrameBuffer) == OpenGL.FrameBufferComplete)
{
Console.WriteLine("Framebuffer complete");
}
else
{
Console.WriteLine("Framebuffer Incomplete");
}
gl.BindFramebuffer(OpenGL.FrameBuffer, 0);
Framebuffer buffer = new Framebuffer();
buffer.Texture = texture;
buffer.RenderBuffer = rbo;
buffer.FramebufferID = framebuffer;
return buffer;
}
this is before i start to render the scene
public void PrepareSceneRendering(Scene scene)
{
gl.BindFramebuffer(OpenGL.FrameBuffer, sceneBuffer.FramebufferID);
gl.Enable(OpenGL.DepthTest);
gl.Enable(OpenGL.Blend);
gl.BlendFunc(OpenGL.One, OpenGL.OneMinusSrcAlpha);
gl.ClearColor(0f, 0f, 0f, 0f);
gl.Clear(NetGL.OpenGL.ColorBufferBit | NetGL.OpenGL.DepthBufferBit);
}
and this is what i do when the scene rendering is complete in order to render the frame buffer to the screen
public void FinishSceneRendering(Scene scene)
{
gl.BindFramebuffer(OpenGL.FrameBuffer, 0);
gl.Disable(OpenGL.DepthTest);
if(scene.BackgroundTexture != null)
{
DrawFramebuffer(scene.BackgroundTexture.RenderID);
}
DrawFramebuffer(sceneBuffer.Texture);
gl.Enable(OpenGL.DepthTest);
}
private void DrawFramebuffer(int textureID)
{
// load shader
gl.UseProgram(ShaderPrograms["ScreenShader"].ProgramID);
// send texture to the shader
gl.ActiveTexture(OpenGL.Texture0);
gl.BindTexture(OpenGL.Texture2D, textureID);
gl.Uniform1I(gl.GetUniformLocation(ShaderPrograms["ScreenShader"].ProgramID, "screenTexture"), 0);
// bind vertex buffer
gl.BindBuffer(OpenGL.ArrayBuffer, InstancedShapes["FrameShape"].vbo);
// define verticies
gl.EnableVertexAttribArray(0);
gl.VertexAttribPointer(0, 3, OpenGL.Float, false, 0, 0);
// define texture coords
gl.EnableVertexAttribArray(1);
gl.VertexAttribPointer(1, 2, OpenGL.Float, false, 0, 18 * sizeof(float));
// render vertex buffer
gl.DrawArrays(OpenGL.Triangles, 0, 6);
}
i have the feeling that something with my framebuffer could be wrong.
Thanks in advance