I am trying to create framebuffer with cubemap for environment-map, but it gives GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT error while attachment of the cubemap as color attachment.

void FrameBuffer::create(size_t width, size_t height) {
    this->width=width;
    this->height=height;

    glGenFramebuffersEXT(1, &id);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
void FrameBuffer::attachDepthTexture() {
    glGenTextures(1, &depthTexture);
    glBindTexture(GL_TEXTURE_2D, depthTexture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    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_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
    glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY); 

    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
    glBindTexture(GL_TEXTURE_2D, 0);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depthTexture, 0);

    checkStatus();
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
void FrameBuffer::attachDepthTextureCube() {
    glGenTextures(1, &depthTexture);

    glBindTexture(GL_TEXTURE_CUBE_MAP, depthTexture);

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

    for (int face=0; face<6; face++)
        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+face, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
    for(int face=0; face<6; face++)
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_CUBE_MAP_POSITIVE_X+face, depthTexture, 0);

    checkStatus();
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
void FrameBuffer::attachColorTextureCube(GLuint i=0) {
    i=clamp(i, 0, GL_MAX_COLOR_ATTACHMENTS_EXT-1);

    glGenTextures(1, &colorTexture[i]);

    glBindTexture(GL_TEXTURE_CUBE_MAP, colorTexture[i]);

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

    for(int face=0; face<6; face++)
        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+face, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
    for(int face=0; face<6; face++)
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_CUBE_MAP_POSITIVE_X+face, colorTexture[i], 0);

    checkStatus();
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

function Call is done here:

    reflectionFrameBuffer.create(Game::Settings::screenWidth, Game::Settings::screenHeight);
    reflectionFrameBuffer.attachDepthTexture();
    reflectionFrameBuffer.attachColorTextureCube();
有帮助吗?

解决方案 2

I got my error... I was using different texture width and height for cube map

其他提示

I suspect your issue has to do with the fact that you have a depth buffer attachment that is a 2D texture and a color buffer attachment that is a cube map. This violates the Framebuffer Object specification, as it pertains to attaching layered images.

Cube map textures should really be thought of as a specialized texture array - they are in essence an array of 6 2D textures, with a special texture lookup function. They are, therefore, layered images; in fact, you can address each cube map face in geometry shaders using layer semantics (explained here).

For a more concise statement, see bullet point 7 of the Framebuffer Object Completeness Rules.

Put simply, you either need to use a depth cube map or no depth attachment at all. Chances are the later of the two options is what you want, it sounds like you are really only interested in the color and not the depth.



DISCLAIMER: It is also worth mentioning that the Wiki I linked above pertains to the finalized FBO specification which is based on the ARB extension.

The EXT extension is very incomplete, lacking support for multisample texture attachments. Additionally, the EXT extension has much more draconian rules for determining completeness (each attachment must have the same dimensions, among other things). A good driver/hardware pairing will usually implement both extensions, and will enforce the stricter completeness nuances if you choose to use the EXT extension.

Therefore, unless you are targeting a platform where the ARB extension is not implemented you should avoid EXT at all costs. And if you are forced to use the EXT extension, be aware of the differences between it and the ARB extension.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top