4

我在 3d 纹理中进行光线投射,直到达到正确的值。我正在一个立方体中进行光线投射,并且立方体角已经在世界坐标中,所以我不必将顶点与模型视图矩阵相乘以获得正确的位置。

顶点着色器

world_coordinate_ = gl_Vertex;

片段着色器

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_)
...
}

一切都按预期工作,我现在想要的是将正确的值采样到深度缓冲区。现在写入深度缓冲区的值是立方体坐标。但我希望pos写入 3d 纹理中的值。

因此,假设立方体10远离原点放置-z,大小为10*10*10. 我无法正常工作的解决方案是:

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

2 回答 2

2

解决方案是:

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;
于 2011-07-08T09:47:53.120 回答
1

One solution you might try is to draw a cube that is directly on top of the cube you're trying to raytrace. Send the cube's position in the same space as you get from your ray-tracing algorithm, and perform the same transforms to compute your "depth_vec", only do it in the vertex shader.

This way, you can see where your problems are coming from. Once you get this part of the transform to work, then you can back-port this transformation sequence into your raytracer. If that doesn't fix everything, then it would only be because your ray-tracing algorithm isn't outputting positions in the space that you think it is in.

于 2011-06-23T09:42:15.257 回答