使用光线投射时,在深度缓冲区中写入正确的值 [英] Writing the correct value in the depth buffer when using ray-casting

查看:51
本文介绍了使用光线投射时,在深度缓冲区中写入正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以3d纹理进行射线投射,直到达到正确的值为止.我正在一个立方体中进行光线投射,并且立方体角已经在世界坐标中,因此我不必将顶点与modelviewmatrix相乘即可得到正确的位置.

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.

顶点着色器

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

一切正常,我现在想要的是将正确的值采样到深度缓冲区.现在写入深度缓冲区的值是立方体坐标.但是我想在3d纹理中写入pos的值.

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.

因此,可以说多维数据集放置在距-z的原点10的位置,并且大小为10*10*10.我无法正常工作的解决方案是这样的:

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;

推荐答案

解决方案是:

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;

这篇关于使用光线投射时,在深度缓冲区中写入正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆