20

I'm starting to learn about 3D rendering and I've been making good progress. I've picked up a lot regarding matrices and the general operations that can be performed on them.

One thing I'm still not quite following is OpenGL's use of matrices. I see this (and things like it) quite a lot:

x y z n
-------
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

So my best understanding, is that it is a normalized (no magnitude) 4 dimensional, column-major matrix. Also that this matrix in particular is called the "identity matrix".

Some questions:

  • What is the "nth" dimension?
  • How and when are these applied?

My biggest confusion arises from how OpenGL makes use of this kind of data.

4

2 回答 2

65

在大多数 3D 图形中,点由 4 分量向量 (x, y, z, w) 表示,其中 w = 1。应用于点的通常操作包括平移、缩放、旋转、反射、倾斜和这些的组合。

这些变换可以用一个叫做“矩阵”的数学对象来表示。矩阵适用于这样的向量:

[ a b c tx ] [ x ]   [ a*x + b*y + c*z + tx*w ]
| d e f ty | | y | = | d*x + e*y + f*z + ty*w |
| g h i tz | | z |   | g*x + h*y + i*z + tz*w |
[ p q r s  ] [ w ]   [ p*x + q*y + r*z +  s*w ]

例如,缩放表示为

[ 2 . . . ] [ x ]   [ 2x ]
| . 2 . . | | y | = | 2y |
| . . 2 . | | z |   | 2z |
[ . . . 1 ] [ 1 ]   [ 1  ]

和翻译为

[ 1 . . dx ] [ x ]   [ x + dx ]
| . 1 . dy | | y | = | y + dy |
| . . 1 dz | | z |   | z + dz |
[ . . . 1  ] [ 1 ]   [   1    ]

第 4 个组件的原因之一是使翻译可以用矩阵表示。

使用矩阵的优点是可以通过矩阵乘法将多个变换合并为一个。

现在,如果目的只是将翻译放在桌面上,那么我会说 (x, y, z, 1) 而不是 (x, y, z, w) 并使矩阵的最后一行 always [0 0 0 1],如通常用于 2D 图形。实际上,4 分量向量将通过以下公式映射回正常的 3 向量向量:

[ x(3D) ]   [ x / w ]
| y(3D) ] = | y / w |
[ z(3D) ]   [ z / w ]

这称为齐次坐标允许这一点使得透视投影也可以用矩阵表示,它可以再次与所有其他变换结合。

例如,由于更远的物体在屏幕上应该更小,我们使用公式将 3D 坐标转换为 2D

x(2D) = x(3D) / (10 * z(3D))
y(2D) = y(3D) / (10 * z(3D))

现在如果我们应用投影矩阵

[ 1 . .  . ] [ x ]   [  x   ]
| . 1 .  . | | y | = |  y   |
| . . 1  . | | z |   |  z   |
[ . . 10 . ] [ 1 ]   [ 10*z ]

那么真正的 3D 坐标将变为

x(3D) := x/w = x/10z
y(3D) := y/w = y/10z
z(3D) := z/w = 0.1

所以我们只需要将 z 坐标切掉以投影到 2D。

于 2010-03-17T19:37:41.390 回答
3

可能会帮助您入门的简短答案是,您所说的“第 n”维度并不代表任何可视化的数量。它被添加为一种实用工具,以启用导致平移和透视投影的矩阵乘法。一个直观的 3x3 矩阵不能做这些事情。

代表空间中一个点的 3d 值总是附加 1 作为第四个值,以使这个技巧起作用。表示方向的 3d 值(即法线,如果您熟悉该术语)在第四个位置附加 0。

于 2010-03-18T15:48:14.070 回答