Jump to content
  • Advertisement
Sign in to follow this  
db123

[FXIED]can't bind a texture depth buffer to a frame buffer[with demo codes]

This topic is 2063 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i want to use frame buffer object, so i download a demo from network.
it works well, but i want to change the depth buffer from render buffer to a texture, so i change the code, and then, glCheckFramebufferStatusEXT return GL_FRAMEBUFFER_COMPLETE_EXT, but glGetRenderbufferParameterivEXT can't get valid width and height for this frame buffer object, they are all return 0.
this is the code:
 
old :
glGenRenderbuffersEXT(1, &g_fboDepthBuffer);
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, g_fboDepthBuffer);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, g_fboWidth, g_fboHeight);
    //glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8, g_fboWidth, g_fboHeight);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, g_fboDepthBuffer);
    // glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER_EXT, g_fboDepthBuffer);
 
i have changed to:
glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &g_fboDepthBuffer);
    glBindTexture(GL_TEXTURE_2D, g_fboDepthBuffer);
//     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
//     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
//     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);                    
//     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//     #define GL_CLAMP_TO_EDGE 0x812F
//     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
//     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
#define GL_TEXTURE_DEPTH_SIZE 0x884A
#define GL_DEPTH_TEXTURE_MODE 0x884B
#define GL_TEXTURE_COMPARE_MODE 0x884C
#define GL_TEXTURE_COMPARE_FUNC 0x884D
#define GL_COMPARE_R_TO_TEXTURE 0x884E
 
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
 
    #define GL_DEPTH_COMPONENT24 0x81A6
 
    // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_SizeX, m_SizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, g_fboWidth, g_fboHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
 
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, g_fboDepthBuffer, 0);
    
    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);
 
check code:
// Verify that the FBO is 'complete'.
    // Once the FBO is complete it is ready for us to render to.
 
    CheckFramebufferStatus();
    
    // Verify that the FBO dimensions are of the expected size.
 
    int actualWidth = 0;
    int actualHeight = 0;
 
    glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_WIDTH_EXT, &actualWidth);
    glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_HEIGHT_EXT, &actualHeight);
 
    assert(actualWidth == g_fboWidth);
    assert(actualHeight == g_fboHeight);
 
void CheckFramebufferStatus()
{
    switch (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT))
    {
    case GL_FRAMEBUFFER_COMPLETE_EXT:
        break;
 
    case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
        throw std::runtime_error("Setup FBO failed. Unsupported framebuffer format.");
 
    case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
        throw std::runtime_error("Setup FBO failed. Missing attachment.");
 
    case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
        throw std::runtime_error("Setup FBO failed. Duplicate attachment.");
 
    case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
        throw std::runtime_error("Setup FBO failed. Attached images must have the same dimensions.");
 
    case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
        throw std::runtime_error("Setup FBO failed. Attached images must have the same format.");
 
    case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
        throw std::runtime_error("Setup FBO failed. Missing draw buffer.");
 
    case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
        throw std::runtime_error("Setup FBO failed. Missing read buffer.");
 
    case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT:
        throw std::runtime_error("Setup FBO failed. Attached images must have the same number of samples.");
 
    default:
        throw std::runtime_error("Setup FBO failed. Fatal error.");
    }
}
 
this is the project with visual studio 2010, you can open it and compile it directly.
 
[attachment=20086:rt.rar]
 
in rt.rar, 
see the function InitFBO();
 
thank you very much...;);)
Edited by db123

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!