Creating Low resolution output using glBlitFrameBuffer

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.

– 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

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();

do you have a valid framebuffer object with a color attachment ? how did you build “fbo[0]” ? the code looks OK so far

changed fbo[0] to fbo

GLuint fbo;
glGenFramebuffers(1, &fbo);
printf("FBO: %d
", 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.

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.

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
", 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

maybe you find this FBO-related list of code examples useful:
renderbuffer
viewport
scaling
MultiRenderTargets (renderbuffers)
depth / stencil
depth / stencil (textures)
multisample (textures)

there are also several books out there with useful examples:

(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

Cool thanks john!:slight_smile: