OpenGL 中的矩阵

发布于 2024-10-14 16:21:12 字数 218 浏览 2 评论 0原文

所以我正在以“红皮书”作为主要资源来学习OpenGL。我正在阅读有关矩阵代数、旋转/缩放/变换矩阵的内容,一切都很棒,但我只是没有得到一件简单的事情。假设函数 glLoadIdentity()。它设置默认矩阵 4x4。所以它设置了3个顶点和1个点:(1,0,0)(0,1,0)(0,0,1)顶点,(0,0,0)点。但我的问题是,这些对应什么?一般来说,OpenGL中的矩阵对应什么?我知道这些是轴的方向。但轴是什么?相机?

So I am learning OpenGL with as the main resource having the "Red book". I am reading about matrix algebra, rotation/scaling/transform matrices and everything is great, but I just don't get one simple thing. Let's say the function glLoadIdentity(). It sets the default matrix of 4x4. So it sets 3 vertices and 1 point: (1,0,0) (0,1,0) (0,0,1) vertices, (0,0,0) point. But my question is, what do those correspond? Generally speaking, what does a matrix correspond in OpenGL? I got an idea that these are the directions of axies. But the axies of what? The camera?

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

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

发布评论

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

评论(2

骷髅 2024-10-21 16:21:12

OpenGL矩阵仅对应于一种变换,将一个坐标空间中定义的对象、向量和点移动到另一个坐标空间。如果在一个坐标空间中有一个矩阵 M(m11 - m44,如下所示)和一个向量 V (v1 - v4),那么乘以 M 将转换你的 V 向量(它可以描述运动向量、对象位置或对象顶点) ) 到不同坐标空间中的 W (w1-w4):

| m11 m12 m13 m14 |   | v1 |     | w1 |
| m21 m22 m23 m24 |   | v2 |     | w2 |
| m31 m32 m33 m34 | X | v3 |  =  | w3 |
| m41 m42 m43 m44 |   | v4 |     | w4 |

其中:

w1 = m11 * v1 + m12 * v2 + m13 * v3 + m14 * v4
w2 = m21 * v1 + m22 * v2 + m23 * v3 + m24 * v4
w3 = m31 * v1 + m32 * v2 + m33 * v3 + m34 * v4
w4 = m41 * v1 + m42 * v2 + m43 * v3 + m44 * v4

因此,如果我们将 v1 - v3 视为旧的 x、y 和 z 坐标并将 v4 设置为 1,那么我们可以将 w1 - w3 视为新坐标x、y 和 z 坐标我们可以看到一些东西:
m11 是从旧 x 坐标到新 x 坐标的乘数,因此它用于比例变换(m22 和 m33 也类似)
m14 乘以 1 并添加到新的 x 坐标,以便用于平移(对于 m24 和 m34 也类似)。

旋转有点难以概念化,但它们是通过将其他矩阵值设置为适当的值来完成的。您可以在这里阅读更多内容:http://gpwiki.org/index.php/Matrix_math

OpenGL matrices only correspond to a transformation, moving objects, vectors and points defined in one coordinate space to another. If you have a matrix M (m11 - m44 as shown below) and a vector V (v1 - v4) in one coordinate space then multiplying by M will convert your V vector (which could describe a movement vector, object location or an object vertex) to W (w1-w4) in a different coordinate space:

| m11 m12 m13 m14 |   | v1 |     | w1 |
| m21 m22 m23 m24 |   | v2 |     | w2 |
| m31 m32 m33 m34 | X | v3 |  =  | w3 |
| m41 m42 m43 m44 |   | v4 |     | w4 |

Where:

w1 = m11 * v1 + m12 * v2 + m13 * v3 + m14 * v4
w2 = m21 * v1 + m22 * v2 + m23 * v3 + m24 * v4
w3 = m31 * v1 + m32 * v2 + m33 * v3 + m34 * v4
w4 = m41 * v1 + m42 * v2 + m43 * v3 + m44 * v4

So if we think of v1 - v3 as the old x, y and z coordinates and set v4 to 1, then we can think of w1 - w3 as the new x, y and z coordinates there are a few things we can see:
m11 is a multiplier from the old x coordinate to the new one so it's used in scale transformations (and similarly for m22 and m33)
m14 multiplied by 1 and added to the new x coordinate so it is used for translations (and similarly for m24 and m34)

Rotations are a little harder to conceptualise but they are done by setting the other matrix values to appropriate values. You can read more here: http://gpwiki.org/index.php/Matrix_math

栀子花开つ 2024-10-21 16:21:12

默认矩阵只是单位矩阵:

/1 0 0 0\
|0 1 0 0|
|0 0 1 0|
\0 0 0 1/

在更一般的情况下(忽略透视和可能的其他奇异变换)...

/a d g j\
|b e h k|
|c f i l|
\0 0 0 1/

...变换后的坐标系的组成部分如下:

         /a\
X-axis = |b|
         \c/

         /d\
Y-axis = |e|
         \f/


         /g\
Z-axis = |h|
         \i/

         /j\
Origin = |k|
         \l/

如果将这些与单位矩阵相关联,则可以看到你的“3个顶点和1个点”来自哪里。

除了单位矩阵之外,这还适用于任何将底行保持在 (0 0 0 1) 的变换(旋转、平移等),并提供了一种简单的方法来可视化此类变换。简单地认为上面的四个分量代表轴 (1 0 0)、(0 1 0)、(0 0 1) 和原点 (0 0 0) 在经过矩阵变换后最终的位置(请记住,轴不是绝对的,而是相对于原点的)。

The default matrix is simply the identity matrix:

/1 0 0 0\
|0 1 0 0|
|0 0 1 0|
\0 0 0 1/

In the more general case (ignoring perspective and possibly other exotic transforms)...

/a d g j\
|b e h k|
|c f i l|
\0 0 0 1/

...the components of the transformed coordinate system are as follows:

         /a\
X-axis = |b|
         \c/

         /d\
Y-axis = |e|
         \f/


         /g\
Z-axis = |h|
         \i/

         /j\
Origin = |k|
         \l/

If you correlate these to the identity matrix, you can see where your "3 vertices and 1 point" come from.

Beyond the identity matrix, this applies to any transform — rotation, translation, etc. — that keeps the bottom row at (0 0 0 1), and provides a simple way to visualise such transforms. Simply think of the four components above as representing where the axes (1 0 0), (0 1 0), (0 0 1) and the origin (0 0 0) end up after being transformed by the matrix (keeping in mind that the axes are not absolute, but relative to the origin).

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