为什么这个图像不是水平渐变?

拉福

在此处输入图片说明

这个问题不是我的问题,而是更多为什么我们会得到这个结果。该图像是使用以下着色器渲染两个立方体而生成的:

片段


uniform mat4 u_ModelMatrix;
uniform mat4 u_ViewMatrix;
uniform mat4 u_ProjectionMatrix;

in FS {
    vec4 pos;
} fs;

out vec4 out_Color;

void main()
{
    out_Color.rgb = vec3(u_ProjectionMatrix * u_ViewMatrix * fs.pos).xxx;
}

顶点

layout (location = 0) in vec4 in_Pos;

uniform mat4 u_ModelMatrix;
uniform mat4 u_ViewMatrix;
uniform mat4 u_ProjectionMatrix;

out FS {
    vec4 pos;
} fs;

void main()
{
    fs.pos = u_ModelMatrix * in_Pos;
    gl_Position = u_ProjectionMatrix * u_ViewMatrix * worldPos;
}

My understand is that as we get the projected fragment coordinates by multiplying by MVP, we should have vec3(u_ProjectionMatrix * u_ViewMatrix * fs.pos) equals to the window screen in X and Y and equals to the depth in Z. So, using xxx as rgb should show an horizontal gradient where models are drawn, with full black at center of the window (as right are negative X) and full white to the right. This is globally the result, but the color does not only depend on X, as we can see that there is vertical lines in the images that have pixels of different colors. Why does it happen ?

BDL

投影矩阵将输入转换为剪辑空间。剪辑空间是一个齐次空间(实际上是投影 P^3 空间),并且不保证轴上有任何边界。对于裁剪空间中可见的任何点,x、y、z 轴必须小于 w 分量。

如果您需要所有坐标都在 [-1,1] 范围内的空间中的点,则必须通过将剪辑空间坐标除以 w 坐标来均匀化剪辑空间坐标。

...我们应该vec3(u_ProjectionMatrix * u_ViewMatrix * fs.pos)在 X 和 Y 中等于窗口屏幕,在 Z 中等于深度:

NDC = clipSpace.xyz / clipSpace.w;

这既不适用于剪辑空间也不适用于 NDC 空间,要获得屏幕坐标,必须执行视口转换,该转换基本上将 [-1, 1] 空间转换为 [0, width] (或 [0, height]) ) 空间。为此,您必须执行以下计算:

windowSpace = (NDC.xy + 1.0) * ([width, height] / 2.0)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章