FBO和Render to renderbuffer

时间:2010-11-18 08:51:06

标签: opengl

您好我正在尝试使用Framebuffer对象扩展来进行屏幕外渲染,其中包含以下注释和示例: http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_offscreen/opengl_offscreen.html

我遇到了如何将我在FBO上绘制的内容显示到窗口的问题!我只得到一个全黑的屏幕!

纹理也更好吗?我发现渲染缓冲区的性能更好吗?

有人可以帮我这个吗?

#include <stdio.h>

#include <stdlib.h>

#include <GLUT/glut.h>   


void drawfunc();
void init(int argc, char** argv);
void initialise_FBO_toRenderBuf();
void onExit();

GLuint renderbufID;
GLuint bufferID;

int main(int argc, char** argv)
{
    init(argc,argv);
    initialise_FBO_toRenderBuf();
    glutDisplayFunc(drawfunc);
    glutMainLoop();
}

void init(int argc, char** argv)
{
    //initialising window settings
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GL_RGBA8);
    glutInitWindowSize(250,250);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Hello Leanne mine");
    /*GLubyte* versiongl;
     versiongl = glGetString(GL_VERSION);
     printf("version is: %s\n",versiongl);
     */
    //initial background colour of the window
    glClearColor(0.0, 0.0, 0.0, 0.0);

    //the coordinate system to be used
    glOrtho(0.0, 1.0, 0.0, 1.0, -10.0, 1.0);


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

}

void initialise_FBO_toRenderBuf(void)
{


    //generate FBO name (bufferID) and create a buffer objct with the texture name (bufferID)
    glGenFramebuffersEXT(1, &bufferID);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, bufferID);


    //generate renderbuffer name (renderbufID) and create a renderbuffer object with the renderbuffer name (renderbufID)
    glGenRenderbuffersEXT(1, &renderbufID);
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbufID);

    //Create data storage and establish the pixel format and dimensions of the renderbuffer image by calling the following function
    glRenderbufferStorageEXT (GL_RENDERBUFFER_EXT, GL_RGBA8,250, 250);

    //attach the texture to the FBO
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, renderbufID);

    switch(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT))
    {

        case GL_FRAMEBUFFER_COMPLETE_EXT:    printf("The fbo is complete\n"); break;
        case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:    printf("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT\n"); break;
        case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:    printf("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT\n"); break;   
        case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:    printf("GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT\n"); break;
        case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:    printf("GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT\n"); break;
    }

}


void drawfunc()
{
    //use the FBO with name bufferID
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, bufferID);

    //clears the FBO to this color when glClear is called i.e it is the texture background color
    glClearColor(0.0, 1.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);


    //drawing here
    glColor3f(0.0, 1.0, 1.0);
    glBegin(GL_QUADS);
    glVertex2f(0.25, 0.25);
    glVertex2f(0.25, 0.75);
    glVertex2f(0.75, 0.75);
    glVertex2f(0.75, 0.25);
    glEnd();


    glBegin(GL_QUADS);
    glColor3f(1.0, 0.0, 0.0);
    glVertex2f(0.5, 0.5);
    glVertex2f(0.5, 0.65);
    glVertex2f(0.65, 0.65);
    glVertex2f(0.65, 0.5);
    glEnd();

    GLvoid* pixels = malloc(250*250*4);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
    glReadPixels(0,0, 250,250, GL_RGBA8, GL_UNSIGNED_BYTE, pixels);


    glDrawBuffer(0);
    //glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0 );
    glRasterPos2d(0, 0);
    glDrawPixels(250, 250, GL_RGBA8,GL_UNSIGNED_BYTE,pixels);



    glFlush();

    onExit();
}

void onExit()
{
    //glDeleteTextures(1, &renderbufID);
    glDeleteFramebuffersEXT(1, &bufferID);
}

1 个答案:

答案 0 :(得分:1)

以下列出了至少部分错误:

  1. 在没有先将矩阵模式设置为glOrtho的情况下调用GL_PROJECTION,可能会“正常工作”,但投影矩阵最终会出现在模型视图矩阵中。
  2. 您应该将行glDrawBuffer(0);更改为glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0),以将渲染重定向到窗口。
  3. 在绘制功能结束时删除渲染目标,您认为下次调用drawfunc会发生什么?
  4. 解决了这些问题之后,在glDrawPixels之后设置断点并检查调试器中是否有正确的数据。