Entering the correct value in the depth buffer when using beam casting

I do beam casting in a 3d texture until I find the correct value. I am doing beam casting in a cube, and the corners of the cube are already in world coordinates, so I do not need to multiply the vertices using 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 want now is to select the correct value in the depth buffer. The value that is now written to the depth buffer is the coordinate of the cube. But I want the pos value in a three-dimensional texture to be written.

So, let's say the cube is placed 10 away from the start at -z , and the size is 10*10*10 . My solution, which does not work correctly, is as follows:

 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; 
+4
source share
2 answers

Decision:

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

One solution you can try is to draw a cube that is directly above the cube that you are trying to execute. Send the cube position in the same space as you, using the ray tracing algorithm, and perform the same transformations as for calculating "depth_vec", only in the vertex shader.

This way you can see where your problems are coming from. After you get this part of the transform to work, you can return this sequence of transforms to your raytracer. If this does not fix everything, then it will only be because your ray tracing algorithm does not display the position in the space in which you are thinking.

+1
source

All Articles