正交光栅化中的近/远平面和 z [英] near/far planes and z in orthographic rasterization

查看:49
本文介绍了正交光栅化中的近/远平面和 z的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了大量着色器,但我偶然发现了一些我以前从未意识到的东西.

Ive coded tons of shaders but Ive stumbled into something I never realized before.

我需要一个带有简单正交投影的顶点 + 片段着色器,没有深度测试.

I needed a vertex + fragment shader with a simple orthographic projection, with no depth test.

相机与原点对齐.

我禁用了 GL_DEPTH_TEST,并屏蔽了深度写入.事实如此简单,以至于我决定我什至不需要投影矩阵.以我完全的无知和独创性,我认为对于任何三角形顶点,顶点着色器只会将 x,y(,z = ,w = 1) 传递给片段着色器.

I disabled GL_DEPTH_TEST, and masked depth writes. It was so simple infact, that I decided I didnt even need a projection matrix. In my complete ignorance and ingenuity, I thought that for any triangle vertex, the vertex shader would simply pass x,y(,z = <whatever>,w = 1) to the fragment shader.

我实际上认为片段着色器只需要 x,y 坐标,因为既然我们在谈论正交投影,w = 1(没有透视划分),并且由于深度缓冲区没有用,并且深度写入被禁用,z 可以字面上是任何,无所谓.

I actually thought the fragment shader would just need x,y coordinates, because since we're talking about orthographic projection, w = 1 (no perspective division), and since depth buffer is useless, and depth writes disabled, z could literally be anything, doesnt matter.

我当然错了.我很快发现 z 分量被光栅化器大量使用,事实上,我有大量的三角形根本没有被光栅化.

Of course I was wrong. I quickly found that the z component is quite used by the rasterizer, infact, I have tons of triangles that simply dont get rasterized.

我还发现调整 z 分量会显着改变渲染的三角形.所以我实现了一个规则的正交投影矩阵,这当然解决了这个问题,现在一切都被正确地光栅化了.

I also found that tweaking the z component would dramatically change the rendered triangles. So I implemented a regular orthographic projection matrix, and that of course solved the issue, now everything is properly rasterized.

现在,出于纯粹的好奇心,我开始玩正交投影矩阵,看看不同输入向量的真正结果,以及近和远剪裁平面的不同值,我真的无法理解.好的,x、y 和 w 都符合预期,但根据我的近/远剪裁平面,我得到了完全不同的 z 值.

Now, out of pure curiosity, I started playing with a orthographic projection matrix and see what really produced for different input vectors, with different values for near and far clipping planes, and I really cant understand. Ok, x,y and w are all as expected, but I get completely different z values based on my near/far clip planes.

问题是:片段着色器对裁剪平面和投影矩阵一无所知,它只是得到一堆 x,y,z,w...z 分量在光栅化过程中的作用是什么?它如何知道视锥内是否有东西?

The question is: the fragment shader doesnt know anything about clip planes and projection matrices, it just gets a bunch of x,y,z,w... what is the role of the z component in the rasterization process? how does it know if something is inside the viewing frustrum or not?

我的意思是,对于正交投影矩阵中的任何给定的近/远平面,我得到的 z 值对给定的投影矩阵有效,而对其他人无效.由于片段着色器无法知道矩阵中使用了哪些平面,它如何丢弃片段?同一个图元的每个顶点之间是否有关系?

I mean, for any given near/far plane in the orthographic projection matrix, I get z values that are valid for a given projection matrix, and invalid for others. Since the fragment shader has no way to know which planes were used in the matrix, how can it discards fragments? Is there a relationship between every vertex of the same primitive?

PS:我知道片段着色器甚至没有被调用,因为我设置了一个 SSBO + Atomic Counter 并检查了两者都没有改变.

PS: I know the fragment shader is not even invoked, as I setup a SSBO + Atomic Counter and checked both are unchanged.

推荐答案

顶点着色器将顶点从模型局部坐标系转换到裁剪空间.如果您的坐标在透视分割后超过单位立方体(每个维度具有 [-1;1] 大小的立方体,或在分割前 [-W;W]),那么这些片段将被剪裁.

Vertex shader transforms vertices from model-local coordinate system to clip space. If your coordinates exceeds unit cube after perspective division (cube with [-1;1] sizes for each dimension, or [-W;W] before division), then this fragments would be clipped.

如果您不关心 Z 并且真的不想设置正确的正交投影(例如,您不知道 Z 值范围,因此无法设置适当的剪裁平面),您可以通过钳位来校正顶点着色器中的 Z将其设置为 (-W; W) 范围(或仅将其设置为 0).

If you don't care about Z and really don't want to set correct ortho projection (e.g. you don't know Z values range and hence cannot set appropriate clip planes), you can correct Z in vertex shader by clamping it to (-W; W) range (or just setting it 0).

这篇关于正交光栅化中的近/远平面和 z的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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