Skip to main content
second stab at solving problem: depth texture; added 113 characters in body
Source Link
knight666
  • 5.6k
  • 1
  • 27
  • 31

EDIT: Second stab at solving this problem. Maybe the FBO isn't initialized properly?

My own version of the FBO init code:

RenderTarget::RenderTarget( unsigned int a_Width, unsigned int a_Height, int a_Colors/* = 1*/, bool a_Depth/* = true*/)
{
    m_Width = a_Width;
    m_Height = a_Height;
    m_Ratio = (float)m_Width / (float)m_Height;

    // this function returns the width and height if a system doesn't have
    // non-power of two texture support
    GetTextureSizes(&m_TexWidth, m_Width, &m_TexHeight, m_Height);

    m_TextureColors = a_Colors;
    m_TextureTotal = a_Colors;
    if (a_Depth) { m_TextureTotal++; }

    m_Texture = new GLuint[m_TextureTotal];
    glGenTextures(m_TextureTotal, m_Texture);
    m_TexDepth = NULL;

    glGenFramebuffersEXT(1, &m_Target);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_Target);

    glEnable(GL_TEXTURE_2D);

    int i = 0;
    for (; i < a_Colors; i++)
    {
        glBindTexture(GL_TEXTURE_2D, m_Texture[i]);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_TexWidth, m_TexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + i, GL_TEXTURE_2D, m_Texture[i], 0);
    }
    if (a_Depth)
    {
        m_TexDepth = m_Texture[i];
        
        glBindTexture(GL_TEXTURE_2D, m_TexDepth);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        
        glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_TexWidth, m_TexHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);

        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, m_TexDepth, 0);
    }
    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);

    GLenum status;
    
    status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
    if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
    {
        int i = 0;
    }
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

As you can see, I attach a depth texture, not a depth render target.

EDIT: Second stab at solving this problem. Maybe the FBO isn't initialized properly?

My own version of the FBO init code:

RenderTarget::RenderTarget( unsigned int a_Width, unsigned int a_Height, int a_Colors/* = 1*/, bool a_Depth/* = true*/)
{
    m_Width = a_Width;
    m_Height = a_Height;
    m_Ratio = (float)m_Width / (float)m_Height;

    // this function returns the width and height if a system doesn't have
    // non-power of two texture support
    GetTextureSizes(&m_TexWidth, m_Width, &m_TexHeight, m_Height);

    m_TextureColors = a_Colors;
    m_TextureTotal = a_Colors;
    if (a_Depth) { m_TextureTotal++; }

    m_Texture = new GLuint[m_TextureTotal];
    glGenTextures(m_TextureTotal, m_Texture);
    m_TexDepth = NULL;

    glGenFramebuffersEXT(1, &m_Target);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_Target);

    glEnable(GL_TEXTURE_2D);

    int i = 0;
    for (; i < a_Colors; i++)
    {
        glBindTexture(GL_TEXTURE_2D, m_Texture[i]);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_TexWidth, m_TexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + i, GL_TEXTURE_2D, m_Texture[i], 0);
    }
    if (a_Depth)
    {
        m_TexDepth = m_Texture[i];
        
        glBindTexture(GL_TEXTURE_2D, m_TexDepth);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        
        glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_TexWidth, m_TexHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);

        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, m_TexDepth, 0);
    }
    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);

    GLenum status;
    
    status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
    if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
    {
        int i = 0;
    }
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

As you can see, I attach a depth texture, not a depth render target.

Source Link
knight666
  • 5.6k
  • 1
  • 27
  • 31

There doesn't seem to anything inherently wrong to your code. It might be in your initialization, but you haven't posted that.

That you have a blue rectangle on the screen means your FBO is working right, your problem is in the rendering of the triangle.

Try turning backface culling into frontface culling before rendering:

glCullFace(GL_FRONT);

Alternatively, just copy a triangle from a NeHe tutorial and see if that works.

glBegin(GL_TRIANGLES);                      // Drawing Using Triangles
    glVertex3f( 0.0f, 1.0f, 0.0f);              // Top
    glVertex3f(-1.0f,-1.0f, 0.0f);              // Bottom Left
    glVertex3f( 1.0f,-1.0f, 0.0f);              // Bottom Right
glEnd();                            // Finished Drawing The Triangle