PDA

View Full Version : FBO not working



heyheyhey27
11-09-2013, 12:50 PM
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 \n\
\n\
layout (location = 0) in vec3 position;\n\
layout (location = 1) in vec2 texCoord;\n\
\n\
out vec2 texCoordOut; \n\
\n\
void main() \n\
{ \n\
texCoordOut = texCoord; \n\
gl_Position = vec4(position, 1.0); \n\
}";
const char * pFS = "#version 330 \n\
\n\
in vec2 texCoordOut; \n\
out vec4 outColor; \n\
\n\
uniform sampler2D frameBufferTex; \n\
\n\
void main() \n\
{ \n\
outColor = vec4(texture(frameBufferTex, texCoordOut).xyz, 1.0);\n\
}";

//[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 \n\
\n\
layout (location = 0) in vec3 Position; \n\
layout (location = 1) in vec3 Color; \n\
\n\
uniform mat4 gWVP; \n\
\n\
out vec3 Color0; \n\
\n\
void main() \n\
{ \n\
vec4 pos = gWVP * vec4(Position.x, Position.y, Position.z, 1.0);\n\
gl_Position = pos;\n\
Color0 = Color; \n\
}";
const char * pFS3D = "#version 330 \n\
in vec3 Color0; \n\
out vec4 fG; \n\
\n\
void main() \n\
{ \n\
fG = vec4(Color0, 1.0); \n\
}";





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.

heyheyhey27
11-12-2013, 08:32 AM
Anybody? This is really a big problem for me! I need to get frame buffers working.

Ehsan Kamrani
11-12-2013, 11:25 AM
I did not look at your code(I can not see your code in my cell phone) here is my suggestion: render the scene to a texture using FBO and then bind this texture and draw a 2D quad that covers your entire screen.

heyheyhey27
11-12-2013, 04:20 PM
That's not a suggestion; that's exactly what my code is supposed to do. My problem is that is isn't working.

Ehsan Kamrani
11-12-2013, 11:41 PM
That's not a suggestion; that's exactly what my code is supposed to do. My problem is that is isn't working.

Could you please disable face culling as well while rendering the 2D quad?
glDisable( GL_CULL_FACE )

Ehsan Kamrani
11-12-2013, 11:48 PM
Moreveor, you haven't sent the texture unit to your shader. Use glUniform1i to send the texture unit to the shader:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colorTex);
glUniform1i(frameBufferTex, 0);

heyheyhey27
11-13-2013, 01:00 AM
SOrry, I forgot to put it in the code, but face culling was already disabled when the program initializes. Also, I added the glUniform1i code at the correct place, but it still shows a black screen.

Dan Bartlett
11-13-2013, 02:22 AM
You could try a debugging tool, such as AMD's CodeXL that is capable of stepping through OpenGL calls and seeing the state at each stage.

Ehsan Kamrani
11-14-2013, 01:26 AM
I'm not sure if your FBO status is complete. Please use glCheckFramebufferStatus() to see if FBO status is complete (GL_FRAMEBUFFER_COMPLETE).
Moreover, use gluOrtho2D with simple glBegin()/glEnd() commands without using shaders to see if this problem refers to your shaders:

//Initialize FBO
//Check FBO Status

//Enable FBO
//Render 3D scene
//Disable FBO and shader

//set viewport
//Use Projection matrix with gluOrtho2D
//Switch to model view matrix and set it to identity.
//Activate texture, Use glBegin()/glEnd() to draw simple 2D quad