Thread: Frame buffer not working

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    41

    OpenGL: Rendering to off-screen texture

    I am trying to render to an off-screen texture using OpenGL = for post processing effects - but the frame buffer doesn't seem to work. If I just render straight to the back buffer like normal the scene is rendered fine. But when I try to do the simplest use of frame buffers - draw to a frame buffer and then just read out of that frame buffer - I just get a black screen. This has been bugging me for hours.

    So here's the setup stuff at the start of the program for the FBOs:
    Code:
    void init(){
    // Setup GL etc....
    
        //Buffer textures
        glGenTextures(2, ppTexture);
    	glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, ppTexture[0]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
        glBindTexture(GL_TEXTURE_2D, ppTexture[1]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    
        //Render buffer for depth buffer
        glGenRenderbuffersEXT(1, &renderbufferID);
        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbufferID);
        glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT32, textureWidth, textureHeight);
    
        //FBOs
        glGenFramebuffersEXT(6, framebufferID);
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID[0]);
        const GLenum bufs[2] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT};
        glDrawBuffers(2, bufs);
        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderbufferID);
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, ppTexture[0], 0);
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, ppTexture[2], 0);  
    }
    Yes I do generate and attach a second texture, but I don't use it.

    And here's the display function:
    Code:
    //GLUT displays the scene with this function
    void display(){
        glEnable(GL_DEPTH_TEST);
        
        glBindFramebuffer(GL_FRAMEBUFFER, framebufferID[0]);
        glViewport(0,0,textureWidth, textureHeight);
        glUseProgram(GM->getShaderProgram());
        
        //Set projection
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45, static_cast<float>(SCREEN_WIDTH) / SCREEN_HEIGHT, Z_NEAR_CLIP, Z_FAR_CLIP);
        
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        //.... draw the scene
        
        //Now read back from the frame buffer
        glUseProgram(0);
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glDisable(GL_LIGHTING);
        glDisable(GL_DEPTH_TEST);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, ppTexture[0]);
        
        glClear(GL_COLOR_BUFFER_BIT);
        
        glBegin(GL_QUADS);
            glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);
            glVertex2f(-1.0f, -1.0f);
            glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 0.0f);
            glVertex2f(1.0f, -1.0f);
            glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 1.0f);
            glVertex2f(1.0f, 1.0f);
            glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 1.0f);
            glVertex2f(-1.0f, 1.0f);            
        glEnd();
        
        //And flush
        glutSwapBuffers();
    }
    My shader does use glFragData[0] and glFragData[1] instead of glFragColor.

    There are no OpenGL errors and the frame buffer has no error status.

    Can anyone see a problem here?

    I am using Windows XP and Dev-C++, old I know but I've gotten used to it and VC++ doesn't want to work for some reason.
    Last edited by Noise; 02-15-2009 at 07:09 PM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What you are doing is rendering to an off-screen texture or a simple render to texture. Calling it this in your thread may lead more people to helping you since that is the standard terminology. I do not use OGL and so I cannot assist you. However changing this title to something like 'problems with render to texture in OGL' or something along those lines will definitely bring in more people to assist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-21-2008, 12:53 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 11:12 PM
  4. the holy grail for newbie programmers...
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2003, 07:19 PM
  5. the effects of textures on my frame rate
    By DavidP in forum Game Programming
    Replies: 37
    Last Post: 10-03-2003, 11:24 AM