转换为NDC,计算并转换回世界空间 [英] Transform to NDC, calculate and transform back to worldspace

查看:426
本文介绍了转换为NDC,计算并转换回世界空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将世界坐标移动到ndc坐标时遇到问题,而不是用它计算某些东西并将其移回到着色器中时遇到问题.

I have a problem moving world coordinates to ndc coordinates than calculate something with it and move it back inside the shader.

代码如下:

vec3 testFunc(vec3 pos, vec3 dir){
    //pos and dir are in worldspace, convert to NDC
    vec4 NDC_dir = MVP * vec4(dir,0);
    vec4 NDC_pos = MVP * vec4(pos,1);
    NDC_dir /= NDC_dir.w;
    NDC_pos /= NDC_pos.w;

    //... do some caclulations => get newPos in NDC 

    //Transform newPos back to worldspace
    vec4 WS_newPos = inverse(MVP) * vec4(newPos,1);
    return WS_newPos.xyz / Ws_newPos.w;
}

我在测试时发现的是,尽管NDC_dir.x和NDC_dir.y似乎是合理的,但NDC_dir.w和NDC_dir.z始终几乎相等.因此,除以"w"时,z值始终约为1.我不认为这应该是正确的吗? (与NDC_pos相同).

What I found when testing is, that while the NDC_dir.x and NDC_dir.y seem to be reasonable, the NDC_dir.w and NDC_dir.z are always almost equal. Therefor when dividing by "w" the z-Value is always about 1. I dont think this is how it should be right? (Same for NDC_pos).

另一方面,当我将"pos"转换为NDC而不是将其转换回Worldspace(不进行任何计算)时,似乎获得了原始Point,这实际上意味着转换是正确的.

On the other hand, when I transform "pos" to NDC and than transform it back to Worldspace (without any calculations) it seems to get the original Point which actually would mean the transformations are correct.

有人可以告诉我我在这里做错了吗,否则,为什么z值始终为1?

Can anybody tell me if I am doing something wrong here, and if not, why the z-Value is always 1?

更新: 这有点尴尬.我有两个问题:1. @Arne指出了方向问题.另一个只是我这方面的一个扩展问题.我有一个很小的近端剪辑和一个很大的远段剪辑.而且,由于该值是对数的,所以我仅作了两步就意识到z值实际上从-1变为1.

Update: This is a little embarrasing. I had two problems: 1. one was the direction problem @Arne pointed out. The other one was just a scaling problem on my side. I had a really small near clip and a big far clip. And as the value are logarithmic, I just moved in two big steps to realize the z-Value actually really goes from -1 to 1.

推荐答案

我在测试时发现,虽然NDC_dir.x和NDC_dir.y似乎是合理的,但NDC_dir.w和NDC_dir.z总是几乎相等.因此,除以"w"时,z值始终约为1.我不认为这应该是正确的吗? (与NDC_pos相同).

What I found when testing is, that whilte the NDC_dir.x and NDC_dir.y seem to be reasonable, the NDC_dir.w and NDC_dir.z are always almost equal. Therefor when dividing by "w" the z-Value is always about 1. I dont think this is how it should be right? (Same for NDC_pos).

实际上是的,应该是这样.如果您的输入格式为{x,y,z,0},则它将被解释为在{x,y,z}方向上无限远的点.因此,深度分量应始终保持最远的距离(如果完全可见).顺便说一句,无限远的地方仍然是平移不变

Actually yes, that is how it should be. If your input is formed as {x,y,z,0} then it will be interpreted as a point infinitely far in the direction of {x,y,z}. Therefore the depth component should always be most distant, if visible at all. By the way a point infinitely far away is still translation invariant

但是您应该知道,无限远的一点可能不是您在NDC中想要的.

But you should know that a point infinitely far away is probably not what you want within NDC.

3D空间中的向量不变,只是保持相同的向量,因为向量仅具有方向,而没有位置.在失真的NDC中这实际上是不可能的.

A vector in 3D space is invariant to translation, it just stays the same vector, because a vector has only a direction, not a position. This is not really possible in the distorted NDC.

如果要变换向量,则最好变换两个点,然后再次求和NDC.但是您应该知道结果取决于位置.

If you want to transform a vector, you should better transform two points, and then take the difference in NDC again. But you should know that your result then depends on the position.

这篇关于转换为NDC,计算并转换回世界空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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