从线性深度重建世界位置 [英] Reconstructing world position from linear depth
本文介绍了从线性深度重建世界位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
VS (storing depth to 32F):
float linDepth(float z) {
return (-z-zNear)/(zFar-zNear);
}
void main() {
vec4 position = uViewMatrix * uModelMatrix * vec4(aPosition, 1);
depth = linDepth(position.z); //store linear view-depth
}
FS (reconstuction):
void main() {
vec3 vUV = vec2(0..1, 0..1); (from screen aligned quad)
vec3 ndc = vec3(vUV*2-1, linearViewDepth*2-1);
vec4 v0 = inverse(uProjectionMatrix)*vec4(ndc, 1);
vec3 reconViewPos = v0.xyz/v0.w;
vec3 reconWorldPos = inverse(uViewMatrix) * v0;
}
...结果完全不正确. 虽然我通过使用不变的线性视图深度作为ndc z感觉到一个问题.最后,我希望应用射线插值方法:
VS (reconstruction, aligned screenquad):
out vec3 vViewRay;
void main() {
gl_Position = aPosition;
vec4 v = vec4(aPosition.x, aPosition.y, 1, 1); //ndc (at the back of cube)
v = inverse(uProjectionMatrix) * v;
v /= v.w; //view coordinate
v /= v.z; //normalize by z for scaling
vViewRay = v.xyz;
}
FS(reconstruction):
in vec3 vViewRay;
float delinDepth(float z) {
return -(z*(zFar-zNear)+zNear);
}
void main() {
vec3 reconViewPos = vViewRay * delinDepth(linearViewDepth);
}
解决方案
最后,第二个重构代码确实起作用了,我猜想其他地方出现了问题……
i have issues reconstructing world positions from previously stored linear depth in glsl. I read lots of info online, but can't find my problem... So this is what I got:
VS (storing depth to 32F):
float linDepth(float z) {
return (-z-zNear)/(zFar-zNear);
}
void main() {
vec4 position = uViewMatrix * uModelMatrix * vec4(aPosition, 1);
depth = linDepth(position.z); //store linear view-depth
}
FS (reconstuction):
void main() {
vec3 vUV = vec2(0..1, 0..1); (from screen aligned quad)
vec3 ndc = vec3(vUV*2-1, linearViewDepth*2-1);
vec4 v0 = inverse(uProjectionMatrix)*vec4(ndc, 1);
vec3 reconViewPos = v0.xyz/v0.w;
vec3 reconWorldPos = inverse(uViewMatrix) * v0;
}
...and the results are completely off. Though I sense a problem by using unchanged linear view depth as ndc z. In the end I wish to apply the ray-interpolation approach:
VS (reconstruction, aligned screenquad):
out vec3 vViewRay;
void main() {
gl_Position = aPosition;
vec4 v = vec4(aPosition.x, aPosition.y, 1, 1); //ndc (at the back of cube)
v = inverse(uProjectionMatrix) * v;
v /= v.w; //view coordinate
v /= v.z; //normalize by z for scaling
vViewRay = v.xyz;
}
FS(reconstruction):
in vec3 vViewRay;
float delinDepth(float z) {
return -(z*(zFar-zNear)+zNear);
}
void main() {
vec3 reconViewPos = vViewRay * delinDepth(linearViewDepth);
}
解决方案
In the end the second reconstruction code did work, I guess something was off somewhere else...
这篇关于从线性深度重建世界位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文