Question

I want to use the depth buffer in a slightly unorthodox way and I got heavily confused by all the normalization, scaling and what ever is going on there.

My plan is to implement a spatial hashing algorithm by some guys from AMD (link to pdf).

tl;dr-version: Speed up nearest-neighbor-search by discretizing 3D-vertices into an array of (flat 2D) depth textures, setting the depth to the VertexID. The reason for using depth textures is that there is some smart depth testing going on to even get the results in a sorted order, but that's less important here.

My problem is that the VertexID is obviously an integer, ranging from 0 to the total amount of vertices ParticleCount, but that can't be directly used, as the output of the vertex shader has to be normalized to [-1..1) in OpenGL (or [0..1) in DirectX).

My vertex shader therefore does something like that:

float depth = 2.0 * gl_VertexID / ParticleCount - 1.0;
gl_Position = vec4(flatCoords, depth, 1.0);

That is kind of working, but what values are actually stored to the depth texture bound to the current framebuffer confuses me. I don't quite get the difference between the floating-point depth buffer and the integer version, if I can't even output real integers and when reading from the depth texture later everything seems to be normalized to [0..1] no matter what internal format I set (DepthComponent24, 32, 32f).

Can someone give me some advice how to get the VertexIDs out of these depth textures?

Thanks

Was it helpful?

Solution

Output from the vertex shader in OpenGL after perspective divide is clipped to [-1,1], that means gl_Position.z/gl_Position.w has to be in that range. However, the depth value that is actually stored in the depth buffer gets remapped to the 0..1 range using the current depth range (glDepthRange) values. By default the depth range is 0..1, which translates to

depth_buf_value = 0.5 + 0.5 * gl_Position.z / gl_Position.w;

So in your case the depth buffer ultimately contains values of float(gl_VertexID) / ParticleCount, and thus:

vertex_id = depth_buf_value * ParticleCount
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top