使用 4x4 矩阵变换 3D 平面
我有一个由几个三角形组成的形状,它位于世界空间中的某个位置,具有缩放、旋转、平移功能。我还有一个平面,我想在其上投影(正交)形状。
我可以将形状中每个三角形的每个顶点与对象变换矩阵相乘,以找出它在世界坐标中的位置,然后将该点投影到平面上。
但我不需要绘制投影,而是想用形状的逆变换矩阵来变换平面,然后将所有顶点投影到(逆变换)平面上。因为它只需要我变换平面一次而不是每个顶点。
我的平面有法线 (xyz) 和距离 (d)。如何将它与 4x4 变换矩阵相乘才能得到正确的结果?
你能创建一个 vec4 作为 xyzd 并将其相乘吗?或者创建一个向量 xyz1 然后如何处理 d?
I have a shape made out of several triangles which is positioned somewhere in world space with scale, rotate, translate. I also have a plane on which I would like to project (orthogonal) the shape.
I could multiply every vertex of every triangle in the shape with the objects transformation matrix to find out where it is located in world coordinates, and then project this point onto the plane.
But I don't need to draw the projection, and instead I would like to transform the plane with the inverse transformation matrix of the shape, and then project all the vertices onto the (inverse transformed) plane. Since it only requires me to transform the plane once and not every vertex.
My plane has a normal (xyz) and a distance (d). How do I multiply it with a 4x4 transformation matrix so that it turns out ok?
Can you create a vec4 as xyzd and multiply that? Or maybe create a vector xyz1 and then what to do with d?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将您的平面转换为不同的表示形式。其中 N 是法线,O 是平面上的任意点。你已经知道的常态是你的 (xyz)。平面上的点也很简单,它是你的正常N乘以你的距离d。
以正常方式将 O 变换为 4x4 矩阵,这将成为您的新 O。您将需要一个 Vector4 来与 4x4 矩阵相乘,将 W 分量设置为 1 (x, y, z, 1)。
同样通过 4x4 矩阵变换 N,但将 W 分量设置为 0 (x, y, z, 0)。将 W 分量设置为 0 意味着法线不会被转换。如果您的矩阵不仅仅由平移和旋转组成,那么这一步就不是那么简单了。您不必乘以变换矩阵,而是必须乘以矩阵的逆的转置,即
Matrix4.Transpose(Matrix4.Invert(Transform))
,这里有一个很好的解释。您现在有了一个新的法线向量N 和一个新的位置向量O。不过我想你又想要 xyzd 形式吗?没问题。和以前一样,xyz 是正常的 N ,剩下的就是计算 d 了。 d 是平面沿法向量距原点的距离。因此,它只是O 和N 的点积。
给你了!如果你告诉我你用什么语言来做这件事,我也很乐意用代码输入它。
编辑,伪代码:
平面是
vector3 xyz
和数字d
,矩阵是matrix4x4 M
xyz
和d
代表新平面You need to convert your plane to a different representation. One where N is the normal, and O is any point on the plane. The normal you already know, it's your (xyz). A point on the plane is also easy, it's your normal N times your distance d.
Transform O by the 4x4 matrix in the normal way, this becomes your new O. You will need a Vector4 to multiply with a 4x4 matrix, set the W component to 1 (x, y, z, 1).
Also transform N by the 4x4 matrix, but set the W component to 0 (x, y, z, 0). Setting the W component to 0 means that your normals won't get translated. If your matrix is composed of more that just translating and rotating, then this step isn't so simple. Instead of multiplying by your transformation matrix, you have to multiply by the transpose of the inverse of the matrix i.e.
Matrix4.Transpose(Matrix4.Invert(Transform))
, there's a good explanation on why here.You now have a new normal vector N and a new position vector O. However I suppose you want it in xyzd form again? No problem. As before, xyz is your normal N all that's left is to calculate d. d is the distance of the plane from the origin, along the normal vector. Hence, it is simply the dot product of O and N.
There you have it! If you tell me what language you're doing this in, I'd happily type it up in code as well.
EDIT, In pseudocode:
The plane is
vector3 xyz
andnumber d
, the matrix is amatrix4x4 M
xyz
andd
represent the new plane这个问题有点老了,但我想纠正已接受的答案。
您不需要转换您的平面表示。
任意点 位于平面上 如果
它可以写成点积:
您正在寻找由 4x4 矩阵转换的平面 。
出于同样的原因,您必须拥有
所以 并进行一些安排
TLDR :如果
p=(a,b,c,d)
,p' = 转置(逆(M))*p
This question is a bit old but I would like to correct the accepted answer.
You do not need to convert your plane representation.
Any point lies on the plane if
It can be written as dot product :
You are looking for the plane transformed by your 4x4 matrix .
For the same reason, you must have
So and with some arrangements
TLDR : if
p=(a,b,c,d)
,p' = transpose(inverse(M))*p
符号:
n
是表示为 (1x3) 行向量的法线n'
是根据变换矩阵n 的变换法线>T
(n|d)
是表示为 (1x4) 行向量的平面(其中n
为平面法线,d
代码> 平面到原点的距离)(n'|d')
是(n|d)
根据变换矩阵T
T
变换后的平面> 是一个(4x4)(仿射)列主变换矩阵(即变换列向量t 定义为t' = T t
)。变换法线n:
变换平面(n|d):
这里,adj是矩阵的伴随,其定义如下:矩阵的逆矩阵和行列式项:
注意:
佐数通常不等于变换矩阵 T 的逆矩阵。如果 T 包含反射,则 det(T) = -1 ,反转绕组顺序!
在数学上不需要重新归一化 n'(但可能在数值上取决于实现),因为缩放由行列式负责。感谢 Adrian Leonhard。Notation:
n
is a normal represented as a (1x3) row-vectorn'
is the transformed normal of n according to transform matrixT
(n|d)
is a plane represented as a (1x4) row-vector (withn
the plane's normal andd
the plane's distance to the origin)(n'|d')
is the transformed plane of(n|d)
according to transform matrixT
T
is a (4x4) (affine) column-major transformation matrix (i.e. transforming a column-vector t is defined ast' = T t
).Transforming a normal n:
Transforming a plane (n|d):
Here, adj is the adjugate of a matrix which is defined as follows in terms of the inverse and determinant of a matrix:
Note:
The adjugate is generally not equal to the inverse of a transformation matrix T. If T includes a reflection, det(T) = -1, reversing the winding order!
Re-normalizing n' is mathematically not required (but maybe numerically depending on the implementation) since scaling is taken care off by the determinant.Thanks to Adrian Leonhard.