10

I've got a fairly complicated scene with many GL_POINTS that I need to render. The scene will be largely static, so I'd like to render it to a Framebuffer Object and then only update that FBO when my scene actually changes. I'd then like to render the FBO to the screen each frame.

I've found examples that render an FBO into a texture. I've found examples that render an FBO into a RenderBuffer (still not quite sure what that is). I'm not sure what the steps are to achieve this. Do I need to render to a texture and the draw the texture to the screen?

Can you please enumerate the steps (ideally even in pseudocode or actual code) to render my scene to an FBO and then draw that FBO to the screen.

draw() is sufficient for a placeholder for my own drawing functions.

Luke
  • 3,602
  • 4
  • 29
  • 49

2 Answers2

20

I provide a minimal FBO example just for this

Basically the steps are: Create FBO with depth renderbuffer and color texture attachment. To render to FBO unbind the target texture, bind FBO, render to FBO. Unbind FBO, bind texture, render.


#include <GL/glew.h>
#include <GL/glut.h>

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

void init();
void display();

int const fbo_width = 512;
int const fbo_height = 512;

GLuint fb, color, depth;

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow("FBO test");
    glutDisplayFunc(display);
    glutIdleFunc(glutPostRedisplay);

    glewInit();

    init();
    glutMainLoop();

    return 0;
}

void CHECK_FRAMEBUFFER_STATUS()
{                                                         
    GLenum status;
    status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER); 
    switch(status) {
    case GL_FRAMEBUFFER_COMPLETE:
        break;

    case GL_FRAMEBUFFER_UNSUPPORTED:
    /* choose different formats */
        break;

    default:
        /* programming error; will fail on all hardware */
        fputs("Framebuffer Error\n", stderr);
        exit(-1);
    }
}

float const light_dir[]={1,1,1,0};
float const light_color[]={1,0.95,0.9,1};

void init()
{
    glGenFramebuffers(1, &fb);
    glGenTextures(1, &color);
    glGenRenderbuffers(1, &depth);

    glBindFramebuffer(GL_FRAMEBUFFER, fb);

    glBindTexture(GL_TEXTURE_2D, color);
    glTexImage2D(   GL_TEXTURE_2D, 
            0, 
            GL_RGBA, 
            fbo_width, fbo_height,
            0, 
            GL_RGBA, 
            GL_UNSIGNED_BYTE, 
            NULL);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);

    glBindRenderbuffer(GL_RENDERBUFFER, depth);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, fbo_width, fbo_height);
    glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);

    CHECK_FRAMEBUFFER_STATUS();
}

void prepare()
{
    static float a=0, b=0, c=0;

    glBindTexture(GL_TEXTURE_2D, 0);
    glEnable(GL_TEXTURE_2D);
    glBindFramebuffer(GL_FRAMEBUFFER, fb);

    glViewport(0,0, fbo_width, fbo_height);

    glClearColor(1,1,1,0);
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, 1, 1, 10);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);

    glEnable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

    glLightfv(GL_LIGHT0, GL_POSITION, light_dir);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color);

    glTranslatef(0,0,-5);

    glRotatef(a, 1, 0, 0);
    glRotatef(b, 0, 1, 0);
    glRotatef(c, 0, 0, 1);

    glutSolidTeapot(0.75);

    a=fmod(a+0.1, 360.);
    b=fmod(b+0.5, 360.);
    c=fmod(c+0.25, 360.);
}

void final()
{
    static float a=0, b=0, c=0;

    const int win_width  = glutGet(GLUT_WINDOW_WIDTH);
    const int win_height = glutGet(GLUT_WINDOW_HEIGHT);
    const float aspect = (float)win_width/(float)win_height;

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    glViewport(0,0, win_width, win_height);

    glClearColor(1.,1.,1.,0.);
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, aspect, 1, 10);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0,0,-5);

    glRotatef(b, 0, 1, 0);

    b=fmod(b+0.5, 360.);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, color);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glDisable(GL_LIGHTING);

    float cube[][5]=
    {
        {-1, -1, -1,  0,  0},
        { 1, -1, -1,  1,  0},
        { 1,  1, -1,  1,  1},
        {-1,  1, -1,  0,  1},

        {-1, -1,  1, -1,  0},
        { 1, -1,  1,  0,  0},
        { 1,  1,  1,  0,  1},
        {-1,  1,  1, -1,  1},
    };
    unsigned int faces[]=
    {
        0, 1, 2, 3,
        1, 5, 6, 2,
        5, 4, 7, 6,
        4, 0, 3, 7,
        3, 2, 6, 7,
        4, 5, 1, 0
    };

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(3, GL_FLOAT, 5*sizeof(float), &cube[0][0]);
    glTexCoordPointer(2, GL_FLOAT, 5*sizeof(float), &cube[0][3]);

    glCullFace(GL_BACK);
    glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, faces);

    glCullFace(GL_FRONT);
    glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, faces);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

}

void display()
{
    prepare();
    final();

    glutSwapBuffers();
}
onkar
  • 4,397
  • 10
  • 47
  • 85
datenwolf
  • 155,162
  • 12
  • 177
  • 284
  • Worked perfectly, thanks! One silly mistake I had made was not calling glEnable for GL_TEXTURE_2D...doh! – Luke Mar 16 '12 at 19:21
  • Could I recommend a copy/paste of that code to your answer, in case the link ever goes away or isn't available? – Luke Mar 19 '12 at 12:26
  • @Luke: It's quite improbable that the link may go away (unless github goes out of business). And I'm not going to delete that repository. Alas, I'm adding the code to the answer. – datenwolf Mar 19 '12 at 13:52
  • @SkybuckFlying oh god… now you've appeared here, too. I remember you just too well from comp.graphics.api.opengl. And no, on most hardware texturing is not *more* expensive than a blit, because it uses the *very exact same circuitry*. Heck on NVidia and AMD GPUs blitting is in fact translated in-situ into a texturing operation. What makes texturing expensive is the memory bandwidth requirements. Memory bandwidth is the bottleneck, and it's exactly the same for texturing and blitting. – datenwolf Feb 20 '20 at 08:33
  • @datenwolf I don't believe you, but maybe an fps comparison will some day shed some light on it =D – oOo Feb 20 '20 at 16:21
0

Here is alternative example which does not require textures:

// copy framebuffer
if(fboUsed)
{
    glBindFramebuffer(GL_READ_FRAMEBUFFER, fboId);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glBlitFramebuffer(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT,
                      0, 0, screenWidth, screenHeight,
                      GL_COLOR_BUFFER_BIT,
                      GL_LINEAR);
    glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
 }

Replace variables in blit with your own.

Apperently frame buffer 0 is front buffer. fboId is your frame buffer number.

E_net4 - Krabbe mit Hüten
  • 24,143
  • 12
  • 85
  • 121
oOo
  • 337
  • 2
  • 14