1

我正在尝试将后处理效果应用于 OS X 的小型测试程序。我正在尝试将图像渲染为纹理,然后将纹理渲染到屏幕(我什至还没有为最终编写第二个着色器图片)。我的代码仍然只是呈现黑屏。

这是创建 FBO 的代码。它在开始时执行一次。

-(void)setupFramebuffer{
    [self setOpenGLContext:self.openGLContext];

    glEnable(GL_TEXTURE_2D);
    CGSize textureSize = CGSizeMake(1024, 1024);

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

    glGenTextures(1, &texture);
    textureImage = MFGLImageCreateWithImageID(texture, textureSize);
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureSize.width, textureSize.height, 0,
                 GL_RGBA, GL_UNSIGNED_BYTE, NULL);

    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
                           GL_TEXTURE_2D, texture, 0);

    if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT)
        NSLog(@"Failed to create FBO: %i", glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));

    glBindTexture(GL_TEXTURE_2D, 0);
    g_CurrentlyBoundTexture = 0;
    glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
}

这是渲染代码。

- (void)drawRect:(NSRect)dirtyRect
{
    glUniform4f(g_OffsetUniform, dirtyRect.size.width/2.0, dirtyRect.size.height/2.0, 1, 1);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, textureFramebuffer);
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    [batchRenderer addFrame:CGRectMake(0, 0, textureImage.size.width, textureImage.size.height) withAlpha:1 forImage:textureImage renderAlpha:YES offset:0 color:[NSColor redColor] renderMode:GL_TRIANGLES];
    [batchRenderer finishAndDrawBatch];
    glFlush();
}

现在我只是清除 FBO 缓冲区并向其写入红色。然后我将图像传递给我的批处理渲染器进行渲染,但是除了黑屏之外我什么也得不到。(批处理渲染器不是问题,在其他程序中使用完全相同的代码)此外,当我注释掉在 draw 方法中绑定 FBO 的代码时,它会毫无问题地绘制红色。

4

0 回答 0