在斜坡上绘制模型
我的模型是轴对齐的。
我知道我想在其上绘制它们的表面的法线;如何计算用于绘制垂直于表面的模型的旋转或旋转矩阵?
My models are axis-aligned.
I know the normal of the surface I want to draw them on; how do I compute the rotations or rotation matrix that I should use to draw the model perpendicular to the surface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,如果 u 是单位向上向量,而 n 是单位曲面法线,那么您需要将 u 转换为 的旋转>n。 (此旋转不是唯一的:您可以在其之前编写关于 u 的任何旋转,并在其之后编写关于 n 的任何旋转。)
如何计算此旋转?嗯,这取决于您正在使用的框架或 API,以及旋转的表示方式。一个不错的框架对此有一个 API 调用,例如在 Unity 3D 中,有
四元数.FromToRotation
。如果您没有相应的 API 调用,那么您可以自己解决。 任何旋转都可以用轴-角形式表示,例如(a ,θ)。这里 θ 是向量 u 和 n 之间的角度(您可以使用 余弦规则:u · n = |u| |n| cos θ),a 是垂直于两个向量的轴:a = u ;× n. (有两种特殊情况:当 u = n 时,将进行零旋转;如果 u = −n > 关于垂直于 u 的任何向量的任何半旋转都可以。)
如果您使用 OpenGL,您现在可以调用
glRotate
。如果你连这个都没有……那么,你就没有资格问这种基本问题!但是请参阅维基百科了解如何将轴角度表示转换为矩阵表示。
Well, if u is the unit up vector, and n is the unit surface normal, then you want a rotation that turns u into n. (This rotation isn't unique: you can compose any rotation about u before it, and any rotation about n after it.)
How to compute this rotation? Well, it depends on what framework or API you're working with, and how rotations are represented. A decent framework has an API call for this, for example in Unity 3D there's
Quaternion.FromToRotation
.If you don't have an API call for that, then you can work it out for yourself. Any rotation can be represented in axis–angle form, say (a, θ). Here θ is the angle between the vectors u and n (which you can compute using the cosine rule: u · n = |u| |n| cos θ), and a is an axis perpendicular to both vectors: a = u × n. (There are two special cases: when u = n the null rotation will do, and if u = −n any half-rotation about any vector perpendicular to u will do.)
If you're using OpenGL you can now call
glRotate
.And if you don't even have that ... well, you've no business asking this kind of basic question! But see Wikipedia for how to turn axis–angle representation into matrix representation.