Olleggerr
Olleggerr

Reputation: 68

Can't draw on Framebuffer with Renderbuffer

Hello i am trying to make an offscreen render with opengl. I already read answers to question How to render offscreen on OpenGL?. I set up everything like in top answer, but opengl doesn't draw on framebuffer. At least i can get data from it. There's example. What am i doing wrong?

#define GLEW_STATIC
#define FREEGLUT_STATIC
#include "GL\glew.h"
#include "GL\freeglut.h"
#include <cstdio>

void RenderFunction(void);
GLuint fbo, render_buf;
unsigned char *data = new unsigned char[256 * 256 * 4]();

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutCreateWindow("Lel");

    glewInit();

    glGenFramebuffers(1, &fbo);                                                                        // 
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);                                                       // 
    glGenRenderbuffers(1, &render_buf);                                                                // (1) Comment this section
    glBindRenderbuffer(GL_RENDERBUFFER, render_buf);                                                   // 
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, 256, 256);                                         // 
    glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, render_buf); // 

    glutDisplayFunc(RenderFunction);
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

    glutMainLoop();
}

void RenderFunction(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_QUADS);
    glColor4f(1.0f, 1.0f, 0.0f, 0.5f);
    glVertex2f(1.0f, 1.0f);
    glVertex2f(-1.0f, 1.0f);
    glVertex2f(-1.0f, -1.0f);
    glVertex2f(1.0f, -1.0f);
    glEnd();
    glFlush();

    glReadBuffer(GL_COLOR_ATTACHMENT0);                  // (2) Comment first line and uncomment second
    //glReadBuffer(GL_BACK);                             // 
    glReadPixels(0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, data);
    printf("begin:\n");
    for (int i = 0; i < 8; i++) {
        printf("%d ", data[i]);
    }
    printf("\n");
    printf("end:\n");
    for (int i = 256 * 256 * 4 - 8; i < 256 * 256 * 4; i++) {
        printf("%d ", data[i]);
    }
    printf("\n\n");
    //glutSwapBuffers();                        // (3) uncomment buffer swap
}

Upvotes: 0

Views: 325

Answers (1)

derhass
derhass

Reputation: 45322

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); 

You bind fbo as the draw framebuffer, but glReadPixels will always read from GL_READ_FRAMEBUFFER. As a result, you do not get the contents of fbo, but of the default framebuffer, and glReadBuffer(GL_COLOR_ATTACHMENT0) should actually produce GL_INVALID_OPERATION error beause the default buffer has only the GL_FRONT and GL_BACK color buffers, and no generic color attachments.

Not that on many sources, you will see glBindFramebuffer(GL_FRAMEBUFFER, ...) which is just a shortcut to bind GL_READ_FRAMEBUFFER and GL_DRAW_FRAMEBUFFER at once.

In general, checking for GL errors will help diagnosing such issues. Modern GL has Debug Output functionality which greatly improves usability and information density, so you should use this whenever possible.

Your code is also lacking a proper glViewport call mathcing your FBO size. Initially, the viewport will be set to the size of the window when the GL context is first bound to it.

Upvotes: 4

Related Questions