给定旋转后的法向矢量,找到平面的OpenGL旋转矩阵 [英] Find OpenGL rotation matrix for a plane, given the normal vector after the rotation
问题描述
有没有一种方法可以获取将平面旋转到新方向的矩阵,给定其新的法向矢量
Is there a way to obtain the matrix which rotates a plane to a new orientation, given its new normal vector
下图描述了所描述的内容
The following image depicts what is described
推荐答案
鉴于旧的法线N
和新的法线N'
,您可以通过以下方式获得旋转值:
Given the old normal N
and the new normal N'
you can obtain the rotation by:
RotationAxis = cross(N, N')
RotationAngle = arccos(dot(N, N') / (|N| * |N'|))
哪里
-
cross(x, y)
是向量x
和y
的叉积
-
dot(x, y)
是向量x
和y
的点积
-
|x|
是向量x
的长度
cross(x, y)
is the cross product of the vectorsx
andy
dot(x, y)
is the dot product of the vectorsx
andy
|x|
is the length of the vectorx
这将以尽可能短的方式将旧法线旋转到新法线上.
This will rotate the old normal onto the new one by the shortest way possible.
注释
-
RotationAngle
将采用弧度(如果arccos像大多数实现一样返回弧度) -
arccos
是余弦函数的反函数.这是必要的,因为dot(N, N') = |N| * |N'| * cos(RotationAngle)
其中RotationAngle
是矢量之间的角度. -
RotationAxis
未规范化 - 如果两个法线均被归一化,则不必用
(|N| * |N'|)
除(实际上,如果N
被归一化,则可以忽略产品的|N|
,如果N'
被归一化,则排除|N'|
) - 如果
N' = -N
,此方法将失败(因为有许多最短的方法)
RotationAngle
will be in radians (if arccos returns radians as it does in most implementations)arccos
is the inverse of the cosine function. It is necessary becausedot(N, N') = |N| * |N'| * cos(RotationAngle)
whereRotationAngle
is the angle between the vectors.RotationAxis
is not normalized- If both normals are normalized the division by
(|N| * |N'|)
becomes unnecessary (in fact ifN
is normalized you can leave out|N|
of the product and ifN'
is normalized then leave out|N'|
) - This method will fail if
N' = -N
(as there are infinite many shortest ways)
它如何工作?
第一个观察结果是,两个法线将始终(至少)定义两个均位于其中的平面.分开它们的最小角度也将在此平面内测量.
The first observation is that the two normals will always define (at least) one plane in which both are lying. The smallest angle that parts them will be measured inside this plane too.
所以RotationAxis
向量将是同时包含N
和N'
的平面的法线,而RotationAngle
是前面提到的两者之间的最小角度.
So the RotationAxis
vector will be the normal of the plane that encloses both N
and N'
and the RotationAngle
is the smallest angle between the two mentioned earlier.
因此,通过RotationAngle
围绕RotationAxis
旋转,旧法线N
在平面内旋转,朝着通往N'
的最短路径.
So by rotating around RotationAxis
by the RotationAngle
the old normal N
is rotated inside the plane, on the shortest path towards N'
.
这篇关于给定旋转后的法向矢量,找到平面的OpenGL旋转矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!