是否在顶点着色器中分割? [英] Perspecitve divide in vertex shader?

查看:90
本文介绍了是否在顶点着色器中分割?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在顶点着色器中使用透视矩阵时,我应该编写代码以除以w还是在以后的阶段自动完成?

When using a perspective matrix in a vertex shader am I supposed to write code to divide by w or is it done automatically in a later stage?

我提出这个问题的原因是,我已经看到许多使用以下方法的顶点着色器:

The reason for my question is that I have seen lots of vertex shaders using:

gl_Position = matrix * pos;

如果后面有一个阶段可以将向量及其w分量相除,那么这是很有意义的.

which makes sense if there is a later stage that divides the vector with its w component.

但是,直到在顶点着色器中使用以下命令,我才使它起作用:

However I never got it to work until I used the following in my vertex shader:

gl_Position = matrix * pos;
gl_Position = gl_Position / gl_Position.w;

第二个示例是正确的示例还是缺少某些其他设置?

Is the second example the correct one or could some other setting be missing?

采用另一种方式: OpenGL顶点转换(第一张图片)我必须在顶点着色器中编写吗?

Put it another way: which of the steps shown in the OpenGL Vertex transformation(first image) do I have to write in the vertex shader?

我肯定知道ModelView和Projection矩阵属于该矩阵(或两者的合并).视口变换不是顶点着色器的一部分,但是divide by w怎么办?

I know for sure that ModelView and Projection matrix belongs there(or a merge of the two). The viewport transform is not a part of the vertex shader, but what about the divide by w?

我的设置是一些简单的三角形,其x/y/z坐标在[-1 1]之内.

My setup is some simple triangles having coordinates within [-1 1] for x/y/z.

Perspective矩阵应该将z = -1到-10的坐标投影到z = -1,x = [-1,1],y = [-1,1]上.

The Perspective matrix is supposed to project coordinates from z=-1 to -10 onto z=-1, x=[-1,1], y=[-1,1].

-1.0   0.0   0.0   0.0
 0.0  -1.0   0.0   0.0
 0.0   0.0  -1.2  -2.2
 0.0   0.0   1.0   0.0

它是由以下人员生成的:

It was generated by:

x = 2.0f * zNear / (xMax - xMin);
y = 2.0f * zNear / (yMax - yMin);
a = -(xMax + xMin) / (xMax - xMin);
b = -(yMax + yMin) / (yMax - yMin);
c = (zFar + zNear) / (zNear - zFar);
d = -(2.0f * zFar * zNear) / (zNear - zFar);

要制作矩阵P:

x, 0, a, 0
0, y, b, 0
0, 0, c, d
0, 0, 1, 0;

最后,我通过矩阵= P * T生成最终矩阵,其中T是一个平移(0,0,-2)

Finally I generate the final matrix by matrix = P * T where T is a translation (0,0,-2)

我尝试在CPU上进行数学运算,它似乎可以产生预期的结果,但是我也手动执行了除以w的操作.

I have tried to do the math on the CPU and it appears to work generating expected results, however there I also do the divide by w manually.

更新:已解决,但需要了解

Update: Solved but need understanding

我否定了矩阵中的所有成分(乘以-1),现在它可以工作了. 上面的示例还存在一个问题,即将正Z坐标和负Z坐标都投影到投影平面上,这一问题也得到解决.

I negated all components in the matrix (multiply by -1) and now it works. The example above also had an issue with projecting both positive and negative z-coordinates onto the projection plane which also got solved by this change.

欢迎任何引用或解释为什么此更改可以解决此问题.

Any references or explanation why it got solved by this change is welcome.

推荐答案

您不应该在顶点着色器中进行透视划分,而是在稍后的管道中自动完成.

You should not do the perspective divide yourself in the vertex shader, it will be done automatically later in the pipeline.

如果这不起作用,您可以显示一些代码或进一步描述问题吗?我很惊讶它对您有所作为.

If that's not working, can you show some code or describe the problem more? I'm surprised that it's making a difference for you.

这篇关于是否在顶点着色器中分割?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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