1

I'm following this answer link

I include glew.h , linked to glew32s.lib and compile and link successfully.

Now that what's the draw command? I used to use a display callback function , and use glutDisplayFunc(myDisplay);, glutMainLoop() to draw.

What drawing functions should I use to draw to the buffer now?

Thank you!

My code:

#define GLEW_STATIC
//...
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/glu.h>
//...
int main(int argc, char *argv[]) {
    //...
    //This initializes glut
    glutInit(&argc, argv);

    //This tells glut to use a double-buffered window with red, green, and blue channels 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    // Initalize theviewport size
    viewport.w = 400;
    viewport.h = 400;

    //The size and position of the window
    glutInitWindowSize(viewport.w, viewport.h);
    glutInitWindowPosition(0,0);

    glutCreateWindow(argv[0]);
    if(GLEW_OK!=glewInit()){return -1;}

    GLuint fbo, render_buf;
    glGenFramebuffers(1,&fbo);
    glGenRenderbuffers(1,&render_buf);
    glBindRenderbuffer(GL_RENDERBUFFER,render_buf);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB8, viewport.w, viewport.h);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER,fbo);
    glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, render_buf);



    //Before drawing
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER,fbo);


    myDisplay();
    savePNG(outputPNGName,0,0,viewport.w,viewport.h);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

    //At deinit:
    glDeleteFramebuffers(1,&fbo);
    glDeleteRenderbuffers(1,&render_buf);
}

// and whatever else

return 0;

}


savePNG:

glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *)image);
Community
  • 1
  • 1
shrekshao
  • 388
  • 4
  • 12
  • Your call to `glewInit` is commented out. You need to call it in order for GLEW to initialize. – Colonel Thirty Two Sep 21 '14 at 18:23
  • Thank you! It turns out that I should use GLEW_OK!=glewInit() instead of !glewInit(). Now the question turns out to be what drawing functions to use. Do you know something about this? – shrekshao Sep 21 '14 at 18:29

1 Answers1

0

You just use the usual OpenGL drawing commands. Only that instead to the main window they to some off-screen famebuffer. Eventually you'll have to draw the contents of that FBO somewhere else or read it into a file. Note that GLEW by itself does not provide you with an OpenGL context.

There's nothing in the OpenGL specification that says, that OpenGL contexts must be tied to a display system. But unfortunately right now all OpenGL GPU drivers are built on that assumption, so you have to run some kind of display system and create a window (which you can leave invisible / hidden /unmapped) to create an OpenGL context.

Note that GPUs are perfectly capable of doing OpenGL without a display system, it's just the APIs that are lacking. In Linux using some really obscure trickery using the OSS OpenGL drivers you can in fact use the GPU without running a display system.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Thanks, but I use usual OpenGL drawing commands as I drew to the screen, but can only get a black image output file (the output part works well when rendered in the screen). Can you tell me why? – shrekshao Sep 21 '14 at 20:28
  • @shrekshao: Where does `savePNG` come from. Most likely it tries to read from the window framebuffer and not your FBO. Somewhere in the process there must be a call to glReadBuffer and glReadPixels. – datenwolf Sep 21 '14 at 21:16
  • I've updated the related code of savePNG function. I think I'm doing the glReadBuffer and glReadPixels right – shrekshao Sep 21 '14 at 21:23