从FBO到屏幕的OpenGL渲染

我们的想法是在不同颜色附件的FrameBuffer对象中渲染场景的特定部分,然后使用深度缓冲区为最终图像组合.在第一步中,我只想渲染到一个附件,然后再渲染到屏幕.由于我的旧显卡,我正在使用过剩和EXT_framebuffer_object规范.在我的主要功能:

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(600, 512);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    int mainHandle = glutCreateWindow("Demo");
    glutSetWindow(mainHandle);
    glutDisplayFunc(RenderCallback);
    glutReshapeFunc(ReshapeCallback);
    glutIdleFunc(IdleCallback);
    glutKeyboardFunc(KeyboardCallback);
    glutSpecialFunc(ArrowKeyCallback);
    glutMouseFunc(MouseCallback);
    glutMotionFunc(MotionCallback);

    MotionCallback(0,0);
    atexit(ExitNx);

    // Setup default render states
    glClearColor(0.3f, 0.4f, 0.5f, 1.0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);

    // Setup lighting
    glEnable(GL_LIGHTING);
    float ambientColor[]    = { 0.0f, 0.1f, 0.2f, 0.0f };
    float diffuseColor[]    = { 1.0f, 1.0f, 1.0f, 0.0f };       
    float specularColor[]   = { 0.0f, 0.0f, 0.0f, 0.0f };       
    float position[]        = { 100.0f, 100.0f, 400.0f, 1.0f };     
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambientColor);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specularColor);
    glLightfv(GL_LIGHT0, GL_POSITION, position);
    glEnable(GL_LIGHT0);
    GLenum status,st1,st2;                                            
    GLint colorBufferCount ;
    glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &colorBufferCount);   
    printf("Max number of attachments:%d \n " ,colorBufferCount);
    //initialising FBO
    glGenFramebuffersEXT(1,&fb);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fb);
    //attaching an image to render to
    glGenRenderbuffersEXT(1,&colorbf);
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,colorbf);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT,GL_RGBA8,600,512);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,   GL_RENDERBUFFER_EXT, colorbf);
    //creating depht buffer
    glGenRenderbuffersEXT(1, &depth_rb);
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth_rb);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, 600, 512);
    //attaching the depth buffer to the FBO
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depth_rb);
    st1=glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
    //checking for errors
    st2=glGetError();
    if(st1==GL_FRAMEBUFFER_COMPLETE_EXT)
        printf("so far so good\n ");
    else
        if(st1==GL_FRAMEBUFFER_UNSUPPORTED_EXT)
            printf("not good");
    const GLubyte *sir= new GLubyte [256];
    sir=gluErrorString(st1);
    printf("error received %s \n", sir);

    glutMainLoop();
}

在我的显示回调函数中

void RenderCallback()
{
    // Setup projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0f, (float)glutGet(GLUT_WINDOW_WIDTH)/(float)glutGet(GLUT_WINDOW_HEIGHT), 1.0f, 10000.0f);
    gluLookAt(gEye.x, gEye.y, gEye.z, gEye.x + gDir.x, gEye.y + gDir.y, gEye.z + gDir.z, 0.0f, 1.0f, 0.0f);

    // Setup modelview matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb); //setting our FBO to render to
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);  
    //render code 
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0); //unbiding

    //now I wish to see what I have written in the colorbf attachment , most likely my mistake is in the following to come
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity();
    //should I have done something else before?
    glutSwapBuffers();
}

输出是一个空场景.

最佳答案
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);

GL_COLOR_ATTACHMENT0_EXT不是glDrawBuffer()的有效参数.

如果要显示FBO,请添加纹理附件.完成渲染到FBO和draw a full-screen textured quad之后,只需绑定纹理附件.

转载注明原文:从FBO到屏幕的OpenGL渲染 - 代码日志