1

I tried to follow Copying depth render buffer to the depth buffer but it does not work for me. Here is my complete example:

#include <GL/glew.h>
#include <GL/glfw.h>
#include <GL/glu.h>
#include <iostream>

typedef unsigned int uint;

int main()
{
    if (glfwInit() != 1)
    {
        return 1;
    }

    const uint width = 256;
    const uint height = 256;

    if (glfwOpenWindow(width, height, 8, 8, 8, 0, 24, 0, GLFW_WINDOW) != 1)
    {
        return 1;
    }

    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        std::cerr << glewGetErrorString(err) << std::endl;
        return 1;
    }

    GLuint r;
    glGenRenderbuffers(1, &r);
    glBindRenderbuffer(GL_RENDERBUFFER, r);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);

    GLuint r2;
    glGenRenderbuffers(1, &r2);
    glBindRenderbuffer(GL_RENDERBUFFER, r2);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA4, width, height);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);

    GLuint f;
    glGenFramebuffers(1, &f);
    glBindFramebuffer(GL_FRAMEBUFFER, f);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, r);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, r2);
    std::cout << (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) << std::endl;
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    std::cout << gluErrorString(glGetError()) << std::endl;

    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glBindFramebuffer(GL_READ_FRAMEBUFFER, f);
    glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_DEPTH_BUFFER_BIT, GL_NEAREST);

    std::cout << gluErrorString(glGetError()) << std::endl;

    return 0;
}

Output I get:

1
no error
invalid operation

The 24 in glfwOpenWindow is the number of depth bits. What am I doing wrong?

4
  • Why are you attaching a color renderbuffer when you are only using a depth buffer? Commented Jun 6, 2013 at 10:48
  • I wanted the framebuffer to be complete as I am not sure if it has any influence. Commented Jun 6, 2013 at 11:05
  • 1
    The code does not give an invalid operation for me. You could try with an 8-bit stencil (e.g. GL_DEPTH24_STENCIL8). Commented Jun 6, 2013 at 19:48
  • Yes GL_DEPTH24_STENCIL8 works! :) Commented Jun 7, 2013 at 10:40

2 Answers 2

2

The parameters to glfwOpenWindow are specifying the desired values. However, it's entirely possible that your driver doesn't support the exact format you're asking for (24 bit color, 24 bit depth, no alpha, no stencil). GLFW will attempt to find the best match while meeting your minimums.

In this case it's probably giving you the most standard format of RGBA color (32 bit) and a 24/8 bit depth/stencil buffer, or it could be creating a 32 bit depth buffer. You'd actually need to call glfwGetWindowParam to query the depth and stencil bits for the default framebuffer, and then build your renderbuffers to match that.

Sign up to request clarification or add additional context in comments.

Comments

0

As ThomasD suggested in the comments GL_DEPTH24_STENCIL8 worked.

1 Comment

That's all well and good, but since the original code worked fine on his machine, it's more important people coming to look at this question in the future to understand why it failed on your machine and succeeded on his. Especially since someone else running different drivers / hardware might not be able to find success just by changing the stencil format in the same way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.