PDA

View Full Version : Rendering to a sub-part of a framebuffer using Viewport



rivten
01-27-2017, 03:46 PM
Hello everyone,

So I have a Framebuffer which is very large (about (32*128) width and height) and that I want to fill it by making a certain render call.

For example, with one of my render call I can generate a 128x128 texture (let's call it texture A) and I want to render it to a certain part of my other big texture (let's call it texture B).

With pseudo-code, it would look like :



BindFramebuffer(GL_FRAMEBUFFER, BufferA);
RenderIntoA();
BindFramebuffer(GL_FRAMEBUFFER, BufferB);
RenderAIntoAPartOfB(Rectangle);


With Rectangle being the part of the B buffer I want to put A in. I already have a function that takes a texture and stretch it to display it on the whole framebuffer.

So I thought about using the glViewport so that I will write only texture from A into the specific part of framebuffer B I am interested in. But I was concerned about that fact that Viewport is related to the exact pixels in window-space. But my framebuffer B is not meant to be displayed, only to be given to another render call, soooo what's the role of the viewport for that kind of problem ? And how can I use it to my advantage so that I can render to a specific part of my texture ?

Thanks a lot !

Dark Photon
01-27-2017, 08:28 PM
I'm not really sure what your concern is. glViewport (https://www.khronos.org/opengl/wiki/GLAPI/glViewport) just modifies how your NDC cube XY maps to window space (pixel coordinates). If it's useful, you should use it.

Hellice
01-28-2017, 04:25 AM
I assume you want to have multiple view port some thing like this :
2330

-use one FBO GLuint FBO
-use multiple textutre 2D (let's 3 texture each one is going to render diffrent view ports
-change view port for example (glViewport(0,0,128,128) than an other window below it glViewport(128,128,256,256)... and so on)
-sweep betewen this texture (attach and disattach to Framebuffer when rendering)

arekkusu
01-28-2017, 12:59 PM
(let's call it texture A) and I want to render it to a certain part of my other big texture (let's call it texture B).

If you want to copy A to B, try glBlitFramebuffer.
If you want to render A's content directly to B in the first place (and never create A), use glViewport. The viewport doesn't clip (#10 (https://www.opengl.org/archives/resources/features/KilgardTechniques/oglpitfall/)), so you probably also need glScissor.

GClements
01-29-2017, 04:38 AM
So I thought about using the glViewport so that I will write only texture from A into the specific part of framebuffer B I am interested in. But I was concerned about that fact that Viewport is related to the exact pixels in window-space. But my framebuffer B is not meant to be displayed, only to be given to another render call, soooo what's the role of the viewport for that kind of problem ? And how can I use it to my advantage so that I can render to a specific part of my texture ?

Geometry is clipped to the NDC unit cube before rasterisation, and the bounds of the NDC unit cube are mapped to the viewport. Thus, if you take the vertices which form the clipped geometry and project them to window (i.e. framebuffer) coordinates, the resulting 2D points will always lie within the viewport.

However, that doesn't mean that nothing will be rendered outside of the viewport. In particular, points and lines have a width, meaning that rasterisation may affect pixels outside of the viewport. If you want to constrain rasterisation to a specific rectangle, you need to use scissoring (glEnable(GL_SCISSOR_TEST) and glScissor()).