OpenGL 旋转和缩放

发布于 2024-12-07 19:12:59 字数 74 浏览 0 评论 0原文

旋转总是绕原点 (0,0,0) 发生吗?

翻译总是相对于之前的翻译发生吗?

缩放会增加坐标轴的大小吗?

Does rotation always occur about the origin (0,0,0)?

Does translation always occur relative to previous translation?

Does scaling increase the coordinates axes size?

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

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

发布评论

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

评论(3

清风不识月 2024-12-14 19:12:59

我建议初学者的一个好方法是从考虑点而不是 3D 对象开始。那么所有的变换都可以被认为是把一个点位置改变到一个新位置的函数。

首先想象一个 XYZ 笛卡尔坐标空间,然后想象空间中原点为 (0, 0, 0) 的点 (X,Y,Z)。 OpenGL 在这个阶段只知道点 X、Y、Z。现在您已准备好开始:

旋转需要角度和旋转中心。 glRotate 允许您仅指定角度。根据数学原理,从概念上讲,旋转中心位于位置 (XX,YY,ZZ) 或 (0,0,0)。

平移只是相对于当前位置的偏移量。由于 OpenGL 知道您的点 (X,Y,Z),因此它只需将偏移量添加到位置向量即可。因此,说它是相对于当前位置而不是之前的翻译更为正确。

缩放是点向量 (Xm,Ym,Zm) 的乘法,因此它只是将该点平移 m 倍。因此,从概念上讲,我们可以说它不会改变坐标轴的大小。

然而,当您开始以 3D 方式思考时,事情会变得有点棘手,因为您会意识到,如果您不小心,单个 3D 对象中的所有点并不总是按照您希望的相对于每个点的方式改变位置其他。例如,您将了解到,如果您想绕对象的中心旋转,则必须“将其移动到原点,旋转,然后再次将其移回原点”。这个来回移动的过程可以被认为是指定旋转中心。这些实际上是您应用的数学“技巧”。

I suggest that a good way for a beginner is to start by thinking about points rather than 3D objects. Then all the transformation can be thought of as functions to change a point position to a new position.

First imagine an XYZ cartesian coordinate space, then imagine a point (X,Y,Z) in space with origin (0, 0, 0). All OpenGL knows at this stage is the point X,Y,Z. Now you are ready to begin:

Rotation requires an angle and a center of rotation. glRotate allows you to only specify the angles. By virtue of mathematics, conceptually, the center of rotation is at the location (X-X,Y-Y,Z-Z) or (0,0,0).

Translation is just an offset from the current position. Since OpenGL knows your point (X,Y,Z) it simply adds the offest to the position vector. It is therefore more correct to say it is relative to the current position rather than previous translation.

Scaling is a multiplication of the point vector (X.m,Y.m,Z.m) hence it simply just translating that point by a factor of m. Hence conceptually one can say it doesn't change the coordinate axes size.

However, when you start to think in 3D things get abit tricky because you will realise that if you are not careful, the all the points in a single 3D object doesn't always change position in the way you desire relative to each other. You will learn for example that if you want to rotate about the object's center, you will have to "move it to the origin, rotate, and then move it back again". This process of moving it back an forth can be thought as specifying the center of rotation. These are actually mathematical "tricks" that you apply.

习惯成性 2024-12-14 19:12:59

旋转总是围绕原点 (0,0,0) 发生吗?

确实是这样。

翻译总是相对于之前的翻译发生吗?

缩放是否会增加坐标轴的大小?

这需要一些解释:OpenGL 和许多其他使用几何数据操作的软件不会构建链式变换列表。他们维护的是一个同质变换矩阵。

“附加”变换是通过将当前变换矩阵与描述“下一个”变换的变换矩阵相乘来完成的,替换旧的变换。这也意味着复合变换矩阵(就像您最终在 OpenGL 模型视图中得到的矩阵一样)也可以用作变换。

总而言之,这完全取决于所应用的转换。旧的 OpenGL 为您提供了一些基本的矩阵操作。在 OpenGL-3 中它们已被删除,因为 OpenGL 不是数学库,而是绘制东西。

那么这样的变换矩阵是什么样的呢?像这样:

Xx Yx Zx Tx
Xy Yy Zy Ty
Xz Yz Zz Tz
_x _y _z  w

也许您注意到有 3 个主要列,用大写的 X、Y、Z 表示。这些列形成向量。在 3D 变换的情况下,这些是坐标系的向量,相对于变换所应用的坐标系。然而向量只给出“方向”和长度。因此还需要新坐标系的相对原点,这就是 T 向量所包含的内容。

大多数时候 _x = _y = _z = 0 且 w = 1

通过将点向量与矩阵相乘来变换几何点。设这样一个矩阵为 M,点 p,然后

p' = M * p

现在假设我们链接变换:

p'' = M' * p' = M' * M * p

我们可以替换 M_ = M' * M,所以

p'' = M_ * p

很容易看出,我们可以链接任意长的

答案最后两个问题:转换(不仅仅是翻译)做链。是的,应用缩放变换将“缩放”轴。

并澄清一些常见的误解:OpenGL 不是场景图,它不处理“对象”,而只是处理几何图形列表。 glScale、glTranslate、glRotate 不变换对象,而是“链接”变换操作。

Does rotation always occur about the origin (0,0,0)?

Indeed this is the case.

Does translation always occur relative to previous translation?

Does scaling increase the coordinates axes size?

This requires some explanation: OpenGL, and so many other software operating with geometry data don't build a list of chained transformations. What they maintain is one single homogenous transformation matrix.

"Appending" a transformation is done by multiplying the current transformation matrix with the transformation matrix describing the "next" transformation, replacing the old transformation. This also means that a compound transformation matrix, like what you end up having in the OpenGL modelview, may be applied as transformation as well.

To make a long story short, it depends all on the transformation applied. Old OpenGL gives you some basic matrix manipulations. In OpenGL-3 they have been removed, because OpenGL is not a math library, but draws stuff.

So how does such a transformation matrix look like? Like this:

Xx Yx Zx Tx
Xy Yy Zy Ty
Xz Yz Zz Tz
_x _y _z  w

Maybe you noticed that there are 3 major columns designated by capital X, Y, Z. Those columns form vectors. And in the case of 3D transformations those are the base vectors of a coordinate system, relative the one the transformation is applied upon. However vectors only give "directions" and a length. So what's needed as well is the relative point of origin of the new coordinate system, and that's what the T vector contains.

Most of the time _x = _y = _z = 0 and w = 1

Transforming a point of geometry happens by multiplying the points vector with the matrix. Let such a matrix be M, the point p, then

p' = M * p

Now assume we chain transformations:

p'' = M' * p' = M' * M * p

We can substitute M_ = M' * M, so

p'' = M_ * p

It's easy to see, that we can chain this arbitrarily long

To answer your two last questions: Transformations (not just translations) do chain. And yes, applying a scaling transform will "scale" the axes.

And to clear up some commong misunderstanding: OpenGL is not a scene graph, it does not deal with "objects", but just lists of geometry. glScale, glTranslate, glRotate don't transform objects, but "chain up" transformation operations.

夏日落 2024-12-14 19:12:59

有更多经验的人肯定会为您指出一个很好的教程,但您的问题反映出您不了解 3D 图形管道,更准确地说,不了解投影矩阵的概念(我可能在这里有错误的名称,因为我很久以前用法语学习过这个哈哈)。

基本上,每当您应用旋转/平移/缩放时,

当您每次操作修改现有状态时,您都会修改相同的矩阵。
例如,先旋转然后平移会给您带来与平移然后旋转不同的结果(尝试做太阳系太阳地球月亮它会帮助您理解)

关于您的问题:

  • 不,基本旋转不会总是发生在 0, 0,0。例如,如果您首先转换为 2,3,4,则旋转将发生在 2,3,4 中。

  • 简单的答案是肯定的,您正在将矩阵从最后一个位置移动。(请阅读最后我的评论以获得不简单的答案^^)

  • 缩放将影响之后完成的所有转换。例如,缩放 1,2,2 后跟平移 2,3,4 可以被视为全局平移 2,6,8

现在对于不那么简单的部分:
正如所解释的,每个更改都会受到先前更改的影响(比例示例)
还有很多方法可以做同样的事情或改变行为,例如:
实现绝对翻译可以这样完成
-翻译
- 创建一个对象
-indentity(将矩阵重置为0)
-翻译2
-create object2

我的建议是阅读教程,但也阅读全球 3D 编程博客或书籍(当你开始时,红书很好,哈哈)

someone with more experience will surely point you to a good tutorial but your question reflect that you don't understand the 3D graphical pipeline and more precisely the concept of projection matrix (I might have the wrong name here since I studied this ages ago in French lol).

Basically whenever you apply a rotation/translation/scaling you are modifying the same matix

therefor when you each operation modifies the existing state.
For example doing rotation then a translation will give you a different result that translation then rotaiton (try doing the solar system sun earth moon it will help you understand)

regarding your questions:

  • No the basic rotation will not always occur in 0,0,0. for example if you first translate to 2,3,4 then the rotation will happen in 2,3,4.

  • the simple answer is yes, you are moving your matrice form its last position.(read my comment at the end for the not the simple answer ^^)

  • scaling will affect all the transformations done after. example scale 1,2,2 followed by a translation 2,3,4 could be seen as a a global translation 2,6,8

now for the not so simple part:
as explained each change will be affected by the previous changes (example of the scale)
also there is a lot of ways to do the same thing or to alter the behavior, for example:
achieving absolute translation can be done like this
-translate
-create an object
-indentity (reset the matrix to 0)
-translate2
-create object2

My advice is read tutorials but also global 3D programing blogs or a book (red book is good when you start lol)

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