OpenGL纹理绑定失败

时间:2013-01-23 00:42:11

标签: cocoa opengl

我试图让我的第一张阴影贴图启动并运行(来自here),但我遇到了一个非常奇怪的问题。这是导致错误的代码:

@interface HSOpenGLView() {
    GLuint shadowFramebuffer;
    GLuint shadowTexture;
}

@end

@implementation HSOpenGLView

- (void) drawRect:(NSRect)dirtyRect {

}

#pragma mark - Init

- (void) prepareOpenGL {
    [super prepareOpenGL];

    glEnable(GL_TEXTURE_2D);

    glGenFramebuffers(1, &shadowFramebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, shadowFramebuffer);

    // Depth texture. Slower than a depth buffer, but you can sample it later in your shader
    glGenTextures(1, &shadowTexture);
    glBindTexture(GL_TEXTURE_2D, shadowTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowTexture, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowTexture, 0);
    //glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, shadowTexture);

    glDrawBuffer(GL_NONE); // No color buffer is drawn to.

    // Always check that our framebuffer is ok
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
        NSLog(@"Failed to create the shadow framebuffer! %u", glGetError());
    }
}

该类是'NSOpenGLView'的子类。当它打印出错误编号时,结果为“0”。可能导致什么呢?

1 个答案:

答案 0 :(得分:2)

致电glReadBuffer(GL_NONE)后,致电glDrawBuffer(GL_NONE)。您需要指出您的FBO也没有用于读取操作的颜色缓冲源。