PDA

View Full Version : Creating Low resolution output using glBlitFrameBuffer



BlendBand
04-07-2017, 08:19 AM
Totally noob on this but this is what I have:


glBindFramebuffer(GL_READ_FRAMEBUFFER , 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo[0]);
glBlitFramebuffer(0, 0, 640, 480, 0, 0, 320, 240, GL_COLOR_BUFFER_BIT , GL_NEAREST);

glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo[0]);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER , 0);
glBlitFramebuffer(0, 0, 320, 240, 0, 0, 640, 480, GL_COLOR_BUFFER_BIT, GL_NEAREST);

My thought are that I first set FBO:'0' to be read from and FBO:'fbo[0]' to be drawn onto.

Then I take a 640x480 frame from '0' and draw it on a small 320x240 frame on 'fbo[0]'.

Then I switch READ/DRAW and take the little 320x240 frame and draw it onto '0' as a 640x480 frame.

Maybe I completely don't understand how this works since the code does nothing to the output.

john_connor
04-07-2017, 08:52 AM
-- render into your low-res FBO, then copy the content onto the screen


/* render to low-res FBO */
glViewport(0, 0, mywidth, myheight);
glBindFramebuffer(GL_FRAMEBUFFER, myfbo);

... render here ...

/* copy to screen */
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(
0, 0, mywidth, myheight,
0, 0, screenwidth, screenheight,
GL_COLOR_BUFFER_BIT, GL_NEAREST);


check for GL errors + FBO completeness
https://www.khronos.org/opengl/wiki/OpenGL_Error
https://www.khronos.org/opengl/wiki/GLAPI/glCheckFramebufferStatus

BlendBand
04-07-2017, 10:10 AM
Nothing gets drawn:


glClear(GL_COLOR_BUFFER_BIT);

/* render to low-res FBO */
glViewport(0, 0, RES_WIDTH, RES_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, fbo[0]);

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(
0, 0, RES_WIDTH, RES_HEIGHT,
0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
GL_COLOR_BUFFER_BIT, GL_NEAREST);

glutSwapBuffers();

glutPostRedisplay();

john_connor
04-07-2017, 05:08 PM
do you have a valid framebuffer object with a color attachment ? how did you build "fbo[0]" ? the code looks OK so far

BlendBand
04-08-2017, 02:12 AM
changed fbo[0] to fbo


GLuint fbo;
glGenFramebuffers(1, &fbo);
printf("FBO: %d\n", fbo);
output: 1

I don't know about color attatchment. Googled a bunch but there's almost always seems to be a "texture" example which I dont know how to translate to this.

GClements
04-08-2017, 05:31 AM
A framebuffer object (created with glGenFramebuffers) is just a container; you have to create the drawing "surfaces" (textures or renderbuffers) separately, then attach them with glFramebufferTexture2D or glFramebufferRenderbuffer.

BlendBand
04-08-2017, 06:10 AM
That works! Thanks! here's the the whole fbo init if someone else needs
(texture stuff's copied from a tutorial, maybe I can delete some to make it cleaner?)
:


glGenFramebuffers(1, &fbo);
printf("FBO: %d\n", fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// The texture we're going to render to
GLuint renderedTexture;
glGenTextures(1, &renderedTexture);

// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, renderedTexture);

// Give an empty image to OpenGL ( the last "0" )
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, RES_WIDTH, RES_HEIGHT, 0,GL_RGBA, GL_UNSIGNED_BYTE, 0);

// Poor filtering. Needed !
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture, 0);

// Set the list of draw buffers.
GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers

EDIT: Yes glTexParameteri and draw buffers could be removed:p

john_connor
04-08-2017, 06:52 AM
maybe you find this FBO-related list of code examples useful:
-- renderbuffer (https://sites.google.com/site/john87connor/framebuffer/tutorial-10-1-framebuffer-renderbuffer)
-- viewport (https://sites.google.com/site/john87connor/framebuffer/tutorial-10-1-2-viewport)
-- scaling (https://sites.google.com/site/john87connor/framebuffer/tutorial-10-1-2-framebuffer-scaling)
-- MultiRenderTargets (renderbuffers) (https://sites.google.com/site/john87connor/framebuffer/tutorial-10-1-4-multiple-rendertargets-mrt)
-- depth / stencil (https://sites.google.com/site/john87connor/framebuffer/1-5-depth-stencil-buffer)
-- depth / stencil (textures) (https://sites.google.com/site/john87connor/framebuffer/tutorial-10-3-framebuffer-mrt)
-- multisample (textures) (https://sites.google.com/site/john87connor/framebuffer/tutorial-10-6-framebuffer-multisample)

there are also several books out there with useful examples:
https://www.khronos.org/developers/books
(hint: some of them can be found as .pdf online)

the wiki has also examples:
https://www.khronos.org/opengl/wiki/Framebuffer_Object_Extension_Examples

some tutorials are really good, others a bit difficult to understand:
https://sites.google.com/site/john87connor/home


@ texparameters:
if you want to sample from those textures later, you have to set proper texparameters

@ glDrawbuffer(s):
if you have more that 1 color attachment, you have to specify in which you want to render

@ glReadBuffer():
if you have more that 1 color attachment, you have to specify from which you want to read

both, glReadBuffer and glDrawBuffer(s) will be stored in the FBO
https://www.khronos.org/opengl/wiki/Framebuffer_Object#Framebuffer_Object_Structure

BlendBand
04-08-2017, 07:20 AM
Cool thanks john!:)