How to draw a 3d rendered Image (perspective proj) back to another viewport with orthogonal proj. simultaniously using multiple Viewports and OpenGL [closed]

StackOverflow https://stackoverflow.com/questions/20602982

  •  02-09-2022
  •  | 
  •  

Question

My problem is that i want to take a kind of snapshot of a 3d scene manipulate that snapshot and draw it back to another viewport of the scene,

I just read the image using the glReadPixel method. Now I want to draw back that image to a specified viewport but with the usage of modern OpenGL.

I read about FrameBufferObject (fbo) and PixelBufferObject (pbo) and the solution to write back the FrameBufferObject contents into a gl2DTexture and pass it to the FragementShader as simple texture.

Is this way correct or can anyone provide a simple example of how to render the image back to the scene using modern OpenGL and not the deprecated glDrawPixel method?

Was it helpful?

Solution

The overall process you want to do will look something like this

  1. Create an FBO with a color and depth attachment. Bind it.

  2. Render your scene

  3. Copy the contents out of its color attachment to client memory to do the operations you want on it.*

  4. Copy the image back into an OpenGL texture (may as well keep the same one).

  5. Bind the default framebuffer (0)

  6. Render a full screen quad using your image as a texture map. (Possibly using a different shader or switching shader functionality).

Possible questions you may have:

  • Do I have to render a full screen quad? Yup. You can't bypass the vertex shader. So somewhere just go make four vertices with texture coordinates in a VBO, yada yada.

  • My vertex shader deals with projecting things, how do I deal with that quad? You can create a subroutine that toggles how you deal with vertices in your vertex shader. One can be for regular 3D rendering (ie transforming from model space into world/view/screen space) and one can just be a pass through that sends along your vertices unmodified. You'll just want your vertices at the four corners of the square on (-1,-1) to (1,1). Send those along to your fragment shader and it'll do what you want. You can optionally just set all your matrices to identity if you don't feel like using subroutines.

*If you can find a way do your texture operations in a shader, I'd highly recommend it. GPUs are quite literally built for this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top