Writing the correct value in the depth buffer when using ray-casting

Posted by hidayat on Game Development See other posts from Game Development or by hidayat
Published on 2011-06-23T09:10:47Z Indexed on 2011/06/23 16:32 UTC
Read the original article Hit count: 457

Filed under:
|
|

I am doing a ray-casting in a 3d texture until I hit a correct value. I am doing the ray-casting in a cube and the cube corners are already in world coordinates so I don't have to multiply the vertices with the modelviewmatrix to get the correct position.

Vertex shader

world_coordinate_ = gl_Vertex;

Fragment shader

vec3 direction = (world_coordinate_.xyz - cameraPosition_);
direction = normalize(direction);

for (float k = 0.0; k < steps; k += 1.0) {
....
pos += direction*delta_step;
float thisLum = texture3D(texture3_, pos).r;
if(thisLum > surface_)
...
}

Everything works as expected, what I now want is to sample the correct value to the depth buffer. The value that is now written to the depth buffer is the cube coordinate. But I want the value of pos in the 3d texture to be written.

So lets say the cube is placed 10 away from origin in -z and the size is 10*10*10. My solution that does not work correctly is this:

pos *= 10;
pos.z += 10;
pos.z *= -1;

vec4 depth_vec = gl_ProjectionMatrix * vec4(pos.xyz, 1.0);
float depth = ((depth_vec.z / depth_vec.w) + 1.0) * 0.5; 
gl_FragDepth = depth;

© Game Development or respective owner

Related posts about opengl

Related posts about shaders