0

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
}
Olleggerr
  • 68
  • 1
  • 8
  • Why are you mixing old and new OpenGL? I'd get rid of *glBegin, glColor4f, glVertex2f* and rest of OGL < 3.2 commands. Come on, jump into *shaders* era. – Ripi2 Feb 19 '18 at 20:12
  • @Ripi2 Sorry but i am newbie in opengl and shaders stuff. So i choose the simplest way to do it. Also my task is super simple. Just draw bunch of 2d lines and save them as png. – Olleggerr Feb 19 '18 at 20:16

1 Answers1

3
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.

derhass
  • 43,833
  • 2
  • 57
  • 78
  • Oh my god. I tried switching `GL_FRAMEBUFFER`, `GL_DRAW_FRAMEBUFFER`, `GL_READ_FRAMEBUFFER` so many times. And now it's finally works. Thank you! Also thanks for page about OpenGL debugging. – Olleggerr Feb 19 '18 at 20:35