这是如何计算深度图的?

发布于 2024-08-11 01:16:18 字数 1596 浏览 1 评论 0原文

从这个网站: http://www.catalinzima.com/?page_id=14

我一直很困惑深度图是如何计算的。

顶点着色器函数计算位置如下:

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)

{

    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);

    float4 viewPosition = mul(worldPosition, View);

    output.Position = mul(viewPosition, Projection);

    output.TexCoord = input.TexCoord;                            //pass the texture coordinates further

    output.Normal =mul(input.Normal,World);                   //get normal into world space

    output.Depth.x = output.Position.z;

    output.Depth.y = output.Position.w;

    return output;

}

什么是output.Position.z和output.Position.w?我不确定这背后的数学原理。

在像素着色器中有这一行:output.Depth = input.Depth.x / input.Depth.y;

那么output.Depth是output.Position.z/outputPOsition.w?我们为什么要这样做?

最后在点光源着色器中(http://www.catalinzima.com/?page_id=55) 将此输出转换为代码的位置:

 //read depth

    float depthVal = tex2D(depthSampler,texCoord).r;

    //compute screen-space position

    float4 position;

    position.xy = input.ScreenPosition.xy;

    position.z = depthVal;

    position.w = 1.0f;

    //transform to world space

    position = mul(position, InvertViewProjection);

    position /= position.w;

我再次不明白这一点。我有点明白为什么我们使用 InvertViewProjection 因为我们之前乘以视图投影,但是整个 z 和现在 w 被设置为等于 1,之后整个位置除以 w 让我很困惑。

From this site: http://www.catalinzima.com/?page_id=14

I've always been confused about how the depth map is calculated.

The vertex shader function calculates position as follows:

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)

{

    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);

    float4 viewPosition = mul(worldPosition, View);

    output.Position = mul(viewPosition, Projection);

    output.TexCoord = input.TexCoord;                            //pass the texture coordinates further

    output.Normal =mul(input.Normal,World);                   //get normal into world space

    output.Depth.x = output.Position.z;

    output.Depth.y = output.Position.w;

    return output;

}

What are output.Position.z and output.Position.w? I'm not sure as to the maths behind this.

And in the pixel shader there is this line: output.Depth = input.Depth.x / input.Depth.y;

So output.Depth is output.Position.z / outputPOsition.w? Why do we do this?

Finally in the point light shader (http://www.catalinzima.com/?page_id=55) to convert this output to be a position the code is:

 //read depth

    float depthVal = tex2D(depthSampler,texCoord).r;

    //compute screen-space position

    float4 position;

    position.xy = input.ScreenPosition.xy;

    position.z = depthVal;

    position.w = 1.0f;

    //transform to world space

    position = mul(position, InvertViewProjection);

    position /= position.w;

again I don't understand this. I sort of see why we use InvertViewProjection as we multiply by the view projection previously, but the whole z and now w being made to equal 1, after which the whole position is divided by w confuses me quite a bit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少心凉 2024-08-18 01:16:18

要完全理解这一点,您需要了解支撑 3D 变换的代数如何工作。 SO 并不能真正帮助(或者我不知道如何使用它)进行矩阵数学,所以它必须没有花哨的公式。不过,这里有一些高级解释:

如果仔细观察,您会注意到顶点位置发生的所有变换(从模型到世界到视图到剪辑坐标)恰好使用 4D 向量。这是正确的。 4D。为什么,当我们生活在 3D 世界时?因为在 4D 表示中,我们通常想要对顶点进行的所有变换都可以表示为矩阵乘法。如果我们停留在 3D 表示中,情况就不是这样了。而矩阵乘法正是 GPU 所擅长的。

3D 中的顶点对应于 4D 中的什么?这就是有趣的地方。 (x, y, z) 点对应于线 (ax, ay, az, a)。我们可以抓住这条线上的任何点来进行我们需要的数学运算,我们通常选择最简单的一个,a=1(这样,我们就不必进行任何乘法,只需设置 <代码>w=1)。

所以这几乎回答了你正在考虑的所有数学问题。为了在 4D 中投影 3D 点,我们设置 w=1,为了从 4D 向量中获取一个分量,我们想要将其与 3D 中的标准大小进行比较,我们必须将该分量除以 w。

如果您想更深入地了解,这个坐标系称为齐次坐标

To understand this completely, you'll need to understand how the algebra that underpins 3D transforms works. SO does not really help (or I don't know how to use it) to do matrix math, so it'll have to be without fancy formulaes. Here is some high level explanation though:

If you look closely, you'll notice that all transformations that happen to a vertex position (from model to world to view to clip coordinates) happens to be using 4D vectors. That's right. 4D. Why, when we live in a 3D world ? Because in that 4D representation, all the transformations we usually want to do to vertices are expressible as a matrix multiplication. This is not the case if we stay in 3D representation. And matrix multiplications are what a GPU is good at.

What does a vertex in 3D correspond to in 4D ? This is where it gets interesting. The (x, y, z) point corresponds to the line (a.x, a.y, a.z, a). We can grab any point on this line to do the math we need, and we usually pick the easiest one, a=1 (that way, we don't have to do any multiplication, just set w=1).

So that answers pretty much all the math you're looking at. To project a 3D point in 4D we set w=1, to get back a component from a 4D vector, that we want to compare against our standard sizes in 3D, we have to divide that component by w.

This coordinate system, if you want to dive deeper, is called homogeneous coordinates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文