3
\$\begingroup\$

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;
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

volumetric raycasting. I know something about that :).

But to you question: When you have sample in world space (your pos) you have to multiply it with View*Projection matrix to have correct output of depth. View matrix contains linear basis of the camera and converts the world position to the camera space and then you do projection.


Using this depth with regular rendering pipeline is easy (if you are not able to write the depth into the depth buffer and use HW acceleration). Store world position in Vertex shader (using varying in gl i think). Than in PS perform viewProjection multiplication on this worldPos. Normalize (pos /= pos.w). And compare pos.z with the value in a depth texture. Thats it ;)

\$\endgroup\$
9
  • \$\begingroup\$ How do I get the view matrix in opengl:)? is the modelview matrix just after the gluLookAt has been used? \$\endgroup\$
    – hidayat
    Jun 23, 2011 at 10:56
  • 1
    \$\begingroup\$ Well you have to split it. I don't know the exact correct function. But for just view matrix you can do. GlIdentity, GluLook at and thats the view matrix ;). Here is how to get the matrix: gamedev.stackexchange.com/questions/8880/… but i was pointed that it is deprecated and i dont know how to do it correctly (because i'm directx guy), but i what i wrote should work ;] \$\endgroup\$
    – Notabene
    Jun 23, 2011 at 11:09
  • \$\begingroup\$ nice, seems to work, no there is only one problem left, how to get the correct coordinates for the depth texture for the other objects \$\endgroup\$
    – hidayat
    Jun 23, 2011 at 11:52
  • \$\begingroup\$ Other objects are volumetric too? \$\endgroup\$
    – Notabene
    Jun 23, 2011 at 12:09
  • \$\begingroup\$ no they are normal object rendered in normal way \$\endgroup\$
    – hidayat
    Jun 23, 2011 at 12:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .