通过FBO渲染到纹理

时间:2013-05-17 20:53:19

标签: objective-c c macos opengl

我正在尝试将后期处理效果应用于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的代码时,它会绘制红色而没有问题。

0 个答案:

没有答案