Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render to part of texture

Tags:

opengl

3d

Since layered rendering with a geometry shader in OpenGL seems a bit dodgy on some drivers/hardware, I would like to substitute the functionality with my own solution. This by using a big texture as rendertarget, with for example a resolution of 300². Then simulating layered rendering by rendering to 100² chunks in the texture. With that resolution it would result in 9 tiles/layers as shown in the mockup below:

Tiles

The question is; how to this with OpenGL? Supposedly with help of a geometry shader that renders the scene to the different tiles in one pass.?

like image 312
Morgan Bengtsson Avatar asked Nov 20 '10 18:11

Morgan Bengtsson


1 Answers

There is no simple way to do it. You can use geometry shader to hack it together but it will most probably be slow, as you have to perform a lot of geometry instancing and then use the geometry shader for vertex shading as well because vertex shader is out of the game.

Basic approach to the problem:

  • Leave vertex shader empty and just return the vertices unchanged
  • In geometry shader create 8 new instances of every triangle (together you will have 9 triangles)
  • On each of the triangles run a function that will project the triangle. According to your screenshot, you will need 9 different view matrices to be sent to the geometry shader.
  • Now arrange the triangles so that each renders on a different part of the image. Note that after the projection, the coordinates are in the range [-1; 1] so you need to divide the coordinates of each triangle by 3 and then shift the corresponding triangles by multiplies of 1/3.
  • If you calculate phong shading per pixel, you will need to send the camera position to the fragment shader with each vertex because the fragment shader has no clue about the screen being divided and you have 9 different camera positions.
  • like image 169
    Karel Petranek Avatar answered Sep 30 '22 22:09

    Karel Petranek