-1

I want to render a 2D image rendered from a 3D model with model, view and perspective projection transforms. However for each pixel/fragment in the output image, I want to store a representation of the index of the vertex in the original mesh which is physically closest to the point where the ray from the camera centre intersects the mesh.

I have a good understanding of the math involved in doing this and can build appropriate raytracing code 'longhand' to get this result but wanted to see if it was possible to achieve in OpenGL, via e.g. a fragment shader.

I'm not an OpenGL expert but my initial reading suggests a possible approach being to set a specific render target for the fragment shader that supports integral values (to store indices) and passing the entire mesh coordinates as a uniform to the fragment shader then performing a search for the nearest coordinate after back transforming gl_FragCoord to model space.

My concern is that this would perform hideously - my mesh has about 10,000 vertices.

My question is: Does this seem like a poor use case for OpenGL? If not, is my approach reasonable> If not, what would you suggest insetad.

Edit: While the indicated answer does contain the kernel of a solution to this question it is not in any way a duplicate question; it's a different question with a different answer that have common elements (raytracing). Someone searching for n answer to this question is highly unlikely to find the proposed duplicate.

Dave Durbin
  • 3,562
  • 23
  • 33
  • Inside each triangle, do you want to just consider the vertices which form the triangle as the candidates for the closest vertex, or all global vertices, potentially from a completely different triangle? – derhass Dec 19 '18 at 22:56
  • You pass your mesh encoded inside a texture... this is how I do it: [Reflection and refraction impossible without recursive ray tracing?](https://stackoverflow.com/a/45140313/2521214) no need for integers I use floats instead but you have to select the internal format of the texture that does not clamp to `<0,1>` range. Btw there are also compute shaders that might be even better for this – Spektre Dec 20 '18 at 05:25
  • @derhass Good question which I hadn't considered. I think for my use case it is reasonable to assume that the closest vertex is one of those of the triangle bounding this fragment. – Dave Durbin Dec 20 '18 at 07:09
  • @Spektre Thanks for the steer will read and learn... – Dave Durbin Dec 20 '18 at 07:12
  • @DaveDurbin in the fragment shader in the part responsible for triangle ray intersection computation you got barycentric coordinates of the triangle ... so just decide to which point it is closer `(0,0),(0,1)` or `(1,1)` that should be easy just by rounding the coordinates ... – Spektre Dec 20 '18 at 09:02

1 Answers1

0

First, 10000 vertices as a uniform is not a good practice in glsl. You may use ubo or create a data texture then upload this texture as an uniform texture, see the following post: GLSL: Replace large uniform int array with buffer or texture

I am not familiar with raytracing. However, I think in most of cases, uploading large number of data to gpu need to use texture uniform. https://www.opengl.org/discussion_boards/showthread.php/200487-Ray-intersection-with-GLSL?p=1292112&viewfull=1#post1292112

sirian_ye
  • 610
  • 6
  • 11