# FBOs and Viewports

• 07-13-2014, 05:01 PM
FrankBoltzmann
FBOs and Viewports
Imagine I have a window of 1280x720 and I have an FBO of the same size. When drawing my scene, I draw it to my FBO using glViewport(640,360,1280,720) which is the top right corner of the window. Now I want to draw the top right corner of the FBO in the bottom left corner of the back buffer. I set glViewport(0,0,640,360). What else should I do?

I've been battling over this the past couple of days and I think it may be something really simple but I can't picture it right now.

Here's my ideas:

1. Create a rectangle from (0,0,0) to (1280,720,0) and UV coordinates from (0,0) to (1,1). Set projection matrix as
Code :

glm::ortho(640,360,1280,720,-1,1);

and no view matrix (identity), which will effectively render the top right corner of the rectangle with UV coordinates from (0.5,0.5) to (1,1) if I use texture(), which I can use to access the FBO's texture for each pixel to get the correct colour. gl_FragCoord would only give me coordinates from (0,0) to (640,360) so it's rather useless here. Is this correct?

2. Create a rectangle from (0,0,0) to (1280,720,0) and UV coordinates from (0,0) to (1280,720). Set same matrices as (1) but this time getting UV coordinates from (640,360) to (1280,720) and thus using texelFetch() to get the correct colour for my pixel. Is this EQUALLY correct? What's the difference between these two approaches, really?

3. I think now it gets overly complicated for no reason: Create a rectangle from (0,0,0) to (1,1,0) and UV coordinates from (0,0) to (1,1). Set projection as
Code :

glm::ortho(0.5,1,0.5,1,-1,1);

which will effectively render the top right corner of the rectangle with UV coordinates from (0.5,0.5) to (1,1). Then using texture() to get the colour values of the top right corner of the texture attached to the FBO. Is this ALSO correct?

That sure seems like a lot of ways to do the same thing. If so, wouldn't I benefit from using (3) due to the fact that I don't need to know the size of my FBO and all I really need to do is provide a viewport in terms of ratios?

Let me know what you think.
• 07-17-2014, 03:40 AM
Ed Daenar
Quote:

Originally Posted by FrankBoltzmann
Imagine I have a window of 1280x720 and I have an FBO of the same size. When drawing my scene, I draw it to my FBO using glViewport(640,360,1280,720) which is the top right corner of the window. Now I want to draw the top right corner of the FBO in the bottom left corner of the back buffer. I set glViewport(0,0,640,360). What else should I do?

So all you want to do is copy a section of the FBO into another section of the same (or another) FBO? If so, there's no need for convoluted solutions. Take a look at the documentation for glBlitFramebuffer: http://www.opengl.org/wiki/GlBlitFramebuffer
• 07-25-2014, 05:40 PM
FrankBoltzmann
Quote:

Originally Posted by Ed Daenar
So all you want to do is copy a section of the FBO into another section of the same (or another) FBO? If so, there's no need for convoluted solutions. Take a look at the documentation for glBlitFramebuffer: http://www.opengl.org/wiki/GlBlitFramebuffer

Hey, thanks for the reply. I was actually simplifying my question, because I didn't know about glBlitFramebuffer. What I really wanted to do was use one single full screen Framebuffer with multiple texture attachments and use for multiple viewports for Deferred Rendering. Imagine: You have a splitcreen game, which means you have to render the scene onto two viewports. However, you use Deferred Rendering for rendering the scene. Instead of creating two Framebuffers, one for each half of the screen, I wanted to know if it was possible to create only one and simply use the Geometry Pass of DR to render onto half of it, and then in the Lights Pass render a fullscreen poly with the Framebuffer's texture, but using a projection matrix to only sample from the correspondent half of the Render Target, do you understand?

I tested this using my 3rd option: setting a square from (0,0,0) to (1,1,0) with UVs (0,0) to (1,1) and using glm::ortho(x1,x2,y1,y2,0,1) as my projection matrix to specify which part of the Render Target I want to sample from, where (x1,y1) is the bottom left corner of my input viewport and (x2,y2) is the top right. I have an abstraction for viewports that has values (x1,x2,y1,y2) between 0 and 1 (as a ratio of the total window size) and sets glViewport(x1*width,y1*height,x2*width,y2*height); when it is passed as an output viewport.

I hope I explained everything clearly :)

In case people who read this don't understand, I no longer have a question. Nothing better than just trying for myself and seeing if it works like I think it does.