I’ve struggled with FBO’s for a while, but they simply aren’t working. I’m just trying to render a 3D scene into an FBO, then render the FBO’s color vaues into the back buffer. The screen just shows a black screen. I’ve successfully rendered the scene straight to the back buffer, so I know that the problem is with FBO’s and not the scene itself. Here is the code I’m using:
//-------------Initialization code----------------
//Set up the frame buffer.
//Create the color texture.
glGenTextures(1, &colorTex);
glBindTexture(GL_TEXTURE_2D, colorTex);
//Set some parameters for the texture.
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
//Attach the texture to the frame buffer.
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTex, 0);
//Set up the depth renderbuffer.
GLuint rendBuff;
glGenRenderbuffers(1, &rendBuff);
glBindRenderbuffer(GL_RENDERBUFFER, rendBuff);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rendBuff);
//Set up shaders for drawing FBO results to the back buffer.
const char * pVS = "#version 330
\
\
layout (location = 0) in vec3 position;
\
layout (location = 1) in vec2 texCoord;
\
\
out vec2 texCoordOut;
\
\
void main()
\
{
\
texCoordOut = texCoord;
\
gl_Position = vec4(position, 1.0);
\
}";
const char * pFS = "#version 330
\
\
in vec2 texCoordOut;
\
out vec4 outColor;
\
\
uniform sampler2D frameBufferTex;
\
\
void main()
\
{
\
outColor = vec4(texture(frameBufferTex, texCoordOut).xyz, 1.0);
\
}";
//[code to create, compile, and link shaders here]
//[code to create vertex buffer object for a quad that covers the screen here]
//-----------------------Rendering code-----------------------
//Render the 3d scene.
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glViewport(0, 0, 500, 500);
GLenum drawBuffs = GL_COLOR_ATTACHMENT0;
glDrawBuffers(1, &drawBuffs);
glClearColor(0, 0, 0, 0);
glClearDepth(1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
//[code to render the 3D scene here]
//Render to screen.
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, 500, 500);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glDisable(GL_DEPTH_TEST);
glUseProgram(shaderProg);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colorTex);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexPosTex1), 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(VertexPosTex1), (GLvoid*)12);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
//Display the back buffer.
wind.display();
//--------For reference, here is the shader code for the 3d scene I'm rendering---------
const char * pVS3D = "#version 330
\
\
layout (location = 0) in vec3 Position;
\
layout (location = 1) in vec3 Color;
\
\
uniform mat4 gWVP;
\
\
out vec3 Color0;
\
\
void main()
\
{
\
vec4 pos = gWVP * vec4(Position.x, Position.y, Position.z, 1.0);
\
gl_Position = pos;
\
Color0 = Color;
\
}";
const char * pFS3D = "#version 330
\
in vec3 Color0;
\
out vec4 fG;
\
\
void main()
\
{
\
fG = vec4(Color0, 1.0);
\
}";
I’ve omitted the extensive error-checking I did as I created and used the frame buffer; no OpenGL errors are occurring anywhere inside this code.