Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: FBO not working

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    4

    Angry 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:


    Code :
     
     
    //-------------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.

  2. #2
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    4
    Anybody? This is really a big problem for me! I need to get frame buffers working.

  3. #3
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Posts
    534
    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.

  4. #4
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    4
    That's not a suggestion; that's exactly what my code is supposed to do. My problem is that is isn't working.
    Last edited by heyheyhey27; 11-12-2013 at 07:44 PM.

  5. #5
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Posts
    534
    Quote Originally Posted by heyheyhey27 View Post
    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 )

  6. #6
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Posts
    534
    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);
    Last edited by Ehsan Kamrani; 11-13-2013 at 12:58 AM.

  7. #7
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    4
    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.

  8. #8
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    433
    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.

  9. #9
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Posts
    534
    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
    Last edited by Ehsan Kamrani; 11-14-2013 at 02:32 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •