FBO not working

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.

Anybody? This is really a big problem for me! I need to get frame buffers working.

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.

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?
[i]glDisable/i

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);

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.

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.

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