OpenGL ES 2.0:矩阵乘法问题

发布于 2024-11-19 18:01:01 字数 1381 浏览 0 评论 0原文

我正在尝试使用 OpenGL ES 2.0 绘制一个四边形。 我的顶点着色器看起来像这样:

uniform mat4 mvp;
attribute vec4 position;
attribute vec4 color;
varying vec4 colorVarying;

void main()
{
    gl_Position = mvp * position;
    colorVarying = color;
}

如果我使用这些顶点:

-1.0f, -1.0f, 0.f, 1.f,
 1.0f, -1.0f, 0.f, 1.f,
-1.0f,  1.0f, 0.f, 1.f,
 1.0f,  1.0f, 0.f, 1.f

和这个矩阵(按列主顺序,与所有以下矩阵一样):

0.5,0,0,0,
0,  1,0,0,
0,  0,1,0,
0.5,0,0,1

四边形覆盖视口的右半部分。事实上,如果我将这两个矩阵相乘,我会得到顶点

0,    -1,     0,     1,
1,    -1,     0,     1,
0,     1,     0,     1,
1 ,    1,     0,     1,

,如果我使用这些顶点和单位矩阵,我会得到相同的结果。所以看起来乘法是正确完成的。

现在,如果我将这些顶点(A):

-100.0f, -100.0f, 301.0f, 1.0f,
 100.0f, -100.0f, 301.0f, 1.0f,
-100.0f,  100.0f, 301.0f, 1.0f,
 100.0f,  100.0f, 301.0f, 1.0f,

乘以这个矩阵(M):

0.4170         0         0         0
     0    0.5560         0         0
     0         0   -1.2220   -1.0000
     0         0  444.4440         0

我得到这些顶点(B):

 0.2075,    0.2766,   -0.9892,    1.0000
-0.2075,    0.2766,   -0.9892,    1.0000,
 0.2075,   -0.2766,   -0.9892,    1.0000,
-0.2075,   -0.2766,   -0.9892,    1.0000,

如果我将B顶点和单位矩阵传递给我的着色器,它会在矩阵的中间渲染一个小矩形视口。但如果我向它传递 A 顶点和 M 矩阵,它不会渲染任何内容。

这是怎么回事?

I'm trying to draw a quadrilateral using OpenGL ES 2.0.
My vertex shader looks like this:

uniform mat4 mvp;
attribute vec4 position;
attribute vec4 color;
varying vec4 colorVarying;

void main()
{
    gl_Position = mvp * position;
    colorVarying = color;
}

If I use these vertices:

-1.0f, -1.0f, 0.f, 1.f,
 1.0f, -1.0f, 0.f, 1.f,
-1.0f,  1.0f, 0.f, 1.f,
 1.0f,  1.0f, 0.f, 1.f

and this matrix (in column-major order, as are all following matrices):

0.5,0,0,0,
0,  1,0,0,
0,  0,1,0,
0.5,0,0,1

the quad covers the right half of the viewport. Indeed, if I multiply those two matrices I get the vertices

0,    -1,     0,     1,
1,    -1,     0,     1,
0,     1,     0,     1,
1 ,    1,     0,     1,

and if I use those vertices and an identity matrix, I get the same result. So it seems that the multiplication is done correctly.

Now, if I multiply these vertices (A):

-100.0f, -100.0f, 301.0f, 1.0f,
 100.0f, -100.0f, 301.0f, 1.0f,
-100.0f,  100.0f, 301.0f, 1.0f,
 100.0f,  100.0f, 301.0f, 1.0f,

by this matrix (M):

0.4170         0         0         0
     0    0.5560         0         0
     0         0   -1.2220   -1.0000
     0         0  444.4440         0

I get these vertices (B):

 0.2075,    0.2766,   -0.9892,    1.0000
-0.2075,    0.2766,   -0.9892,    1.0000,
 0.2075,   -0.2766,   -0.9892,    1.0000,
-0.2075,   -0.2766,   -0.9892,    1.0000,

and if I pass my shader the B vertices and an identity matrix, it renders a small rectangle in the middle of the viewport. But if I pass it the A vertices and the M matrix, it renders nothing.

What's going on?

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

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

发布评论

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

评论(3

帅气尐潴 2024-11-26 18:01:01

问题是,顶点着色器完成后,gl_Position 会经过裁剪,与范围 [-WW] 进行比较,如果 gl_Position 超出该范围,则进行裁剪(不绘制)。

如果您乘以 A * M 并将结果设置为 gl_Position,则给定您使用的矩阵,W 坐标将为 -301,一个负数,这将反转裁剪比较。

在进行乘法之前尝试将矩阵 M 乘以 -1,这应该产生与矩阵 B 乘以单位矩阵相同的结果。

来自“红皮书”:

避免使用负 w 顶点坐标和负 q 纹理坐标。 OpenGL 可能无法正确裁剪此类坐标,并且在对此类坐标定义的着色图元进行着色时可能会出现插值错误。

The problem is that gl_Position after the vertex shader completes goes through clipping where it is compared against the range [-W W] and clipped ( not drawn ) if it gl_Position falls outside of that range.

IF you multiply A * M and set the result to gl_Position, then given the matrices you are using the W coordinate will be -301, a negative number, which will invert the clipping comparison.

Try multiplying the matrix M by -1 before doing the multiplication, that should yield the same results as multiplying matrix B by the identity matrix.

From the "Red Book":

Avoid using negative w vertex coordinates and negative q texture coordinates. OpenGL might not clip such coordinates correctly and might make interpolation errors when shading primitives defined by such coordinates.

我的痛♀有谁懂 2024-11-26 18:01:01

您使用的数学库可能不使用与 GLSL 相同的矩阵顺序。在将矩阵 M 传递给着色器之前尝试对其进行转置。

The math library you are using might not use the same matrix order as GLSL. Try transposing your matrix M before passing it to the shader.

倒带 2024-11-26 18:01:01

问题是,当 wz 单元格中的值为 -1 时,三角形最终其法线方向相反,并且由于它们是单面的,因此未绘制它们。我通过将 M 更改为这样解决了问题:

0.4170         0         0         0
     0    0.5560         0         0
     0         0    1.2220    1.0000
     0         0 -444.4440         0

我还更改了 z 列的符号(实际上是矩阵转置后的行),以便在 z 轴上保持 -1 尽可能近,+1 尽可能远。

The problem was that with that -1 in the wz cell, the triangles ended up with their normal in the opposite directions, and since they are one-sided they were not drawn. I solved the problem by changing M to this:

0.4170         0         0         0
     0    0.5560         0         0
     0         0    1.2220    1.0000
     0         0 -444.4440         0

I also changed the sign of the z column (actually row since the matrix is transposed) in order to keep -1 as near and +1 as far on the z axis.

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