OpenGL 旋转
我正在尝试在 OpenGL 中进行简单的旋转,但一定没有抓住重点。 我并不是在寻找特定的修复程序,而是在寻找快速解释或更普遍地解释 OpenGL 旋转的链接。
目前我有这样的代码:
glPushMatrix();
glRotatef(90.0, 0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex3f( 1.0, 1.0, 0.0 );
glVertex3f( 3.0, 2.0, 0.0 );
glVertex3f( 3.0, 1.0, 0.0 );
glEnd();
glPopMatrix();
但结果不是旋转 90 度的三角形。
编辑 嗯,感谢 Mike Haboustak - 看来我的代码正在调用使用 glOrtho 的 SetCamera 函数。 我对 OpenGL 太陌生,不知道这意味着什么,但禁用它并在 Z 轴上旋转会产生所需的结果。
I'm trying to do a simple rotation in OpenGL but must be missing the point.
I'm not looking for a specific fix so much as a quick explanation or link that explains OpenGL rotation more generally.
At the moment I have code like this:
glPushMatrix();
glRotatef(90.0, 0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex3f( 1.0, 1.0, 0.0 );
glVertex3f( 3.0, 2.0, 0.0 );
glVertex3f( 3.0, 1.0, 0.0 );
glEnd();
glPopMatrix();
But the result is not a triangle rotated 90 degrees.
Edit
Hmm thanks to Mike Haboustak - it appeared my code was calling a SetCamera function that use glOrtho. I'm too new to OpenGL to have any idea of what this meant but disabling this and rotating in the Z-axis produced the desired result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
关于投影矩阵,您可以在这里找到一个很好的来源:
http://msdn.microsoft.com/en-us/library/bb147302(VS.85).aspx
它解释了一些有关如何构造一种类型的投影矩阵的信息。 正交投影是这种矩阵的非常基本/原始的形式,基本上它的作用是获取 3 个轴坐标中的 2 个并将它们投影到屏幕上(您仍然可以翻转轴并缩放它们,但没有扭曲或透视效果) 。
矩阵变换很可能是 3D 渲染时最重要的事情之一,基本上涉及 3 个矩阵阶段:
结果被称为 WorldViewProjection 矩阵(如果您遇到过这个术语),因为它将模型空间中的坐标转换为世界,然后到相机,最后到屏幕表示。
玩得开心
Regarding Projection matrix, you can find a good source to start with here:
http://msdn.microsoft.com/en-us/library/bb147302(VS.85).aspx
It explains a bit about how to construct one type of projection matrix. Orthographic projection is the very basic/primitive form of such a matrix and basically what is does is taking 2 of the 3 axes coordinates and project them to the screen (you can still flip axes and scale them but there is no warp or perspective effect).
transformation of matrices is most likely one of the most important things when rendering in 3D and basically involves 3 matrix stages:
Usually the 3 matrix multiplication result is referred to as the WorldViewProjection matrix (if you ever bump into this term), since it transforms the coordinates from Model space through World, then to Camera and finally to the screen representation.
Have fun
“接受的答案”并不完全正确 - 围绕 Z 旋转不会帮助您看到这个三角形,除非您在此代码之前做了一些奇怪的事情。 删除 glOrtho(...) 调用可能可以纠正这种情况下的问题,但仍然存在一些其他问题。
所编写的代码有两个主要问题:
您之前定位过相机吗? 在OpenGL中,相机位于原点,沿Z轴向下看,Y轴正向向上。 在本例中,三角形是在与您的眼睛相同的平面上绘制的,但位于上方和右侧。 除非你有一个非常奇怪的投影矩阵,否则你不会看到它。 gluLookat() 是执行此操作最简单的命令,但是任何移动当前矩阵(应该是 MODELVIEW)的命令都可以工作。
您正在以左手或顺时针方法绘制三角形,而 OpenGL 的默认坐标系是右手或逆时针坐标系。 这意味着,如果您正在剔除背面(您可能不会,但随着您变得更先进,可能会继续这样做),您将不会看到预期的三角形。 要查看该问题,请将右手放在脸前,想象它在 XY 平面中,按照绘制顶点 (1,1) 到 (3,2) 到 (3,1) 的顺序移动手指)。 当你这样做时,你的拇指背对着你的脸,这意味着你正在看三角形的背面。 您需要养成用右手方法绘制面的习惯,因为这是在 OpenGL 中完成的常见方法。
我可以推荐的最好的办法是使用 NeHe 教程 - http://nehe.gamedev.net/。 他们首先向您展示如何在多个系统中设置 OpenGL,然后转向绘制三角形,然后缓慢而坚定地继续讨论更高级的主题。 他们很容易跟随。
The "accepted answer" is not fully correct - rotating around the Z will not help you see this triangle unless you've done some strange things prior to this code. Removing a glOrtho(...) call might have corrected the problem in this case, but you still have a couple of other issues.
Two major problems with the code as written:
Have you positioned the camera previously? In OpenGL, the camera is located at the origin, looking down the Z axis, with positive Y as up. In this case, the triangle is being drawn in the same plane as your eye, but up and to the right. Unless you have a very strange projection matrix, you won't see it. gluLookat() is the easiest command to do this, but any command that moves the current matrix (which should be MODELVIEW) can be made to work.
You are drawing the triangle in a left handed, or clockwise method, whereas the default for OpenGL is a right handed, or counterclockwise coordinate system. This means that, if you are culling backfaces (which you are probably not, but will likely move onto as you get more advanced), you would not see the triangle as expected. To see the problem, put your right hand in front of your face and, imagining it is in the X-Y plane, move your fingers in the order you draw the vertices (1,1) to (3,2) to (3,1). When you do this, your thumb is facing away from your face, meaning you are looking at the back side of the triangle. You need to get into the habit of drawing faces in a right handed method, since that is the common way it is done in OpenGL.
The best thing I can recommend is to use the NeHe tutorials - http://nehe.gamedev.net/. They begin by showing you how to set up OpenGL in several systems, move onto drawing triangles, and continue slowly and surely to more advanced topics. They are very easy to follow.
你能得到一条 1 单位的直线吗? 看起来90度腐烂。 Y 周围会让你看到一个没有深度的三角形的边。
您应该尝试绕 Z 轴旋转,看看是否得到更有意义的结果。
OpenGL 有两个与几何显示相关的矩阵:模型视图和投影。 在数据在屏幕上可见之前,两者都会应用于坐标。 首先应用模型视图矩阵,将数据从模型空间转换到视图空间。 然后应用投影矩阵来转换视图空间中的数据,以便“投影”到 2D 显示器上。
ModelView 用于将多个对象定位到它们在“世界”中的位置,Projection 用于将对象定位到屏幕上。
您的代码看起来不错,所以我假设通过阅读文档您知道像 glPushMatrix() 这样的函数的本质是什么。 如果围绕 Z 旋转仍然没有意义,请验证您是否正在通过调用 glMatrixMode 编辑 ModelView 矩阵。
Do you get a 1 unit straight line? It seems that 90deg rot. around Y is going to have you looking at the side of a triangle with no depth.
You should try rotating around the Z axis instead and see if you get something that makes more sense.
OpenGL has two matrices related to the display of geometry, the ModelView and the Projection. Both are applied to coordinates before the data becomes visible on the screen. First the ModelView matrix is applied, transforming the data from model space into view space. Then the Projection matrix is applied with transforms the data from view space for "projection" on your 2D monitor.
ModelView is used to position multiple objects to their locations in the "world", Projection is used to position the objects onto the screen.
Your code seems fine, so I assume from reading the documentation you know what the nature of functions like glPushMatrix() is. If rotating around Z still doesn't make sense, verify that you're editing the ModelView matrix by calling glMatrixMode.
确保通过在 glRotatef 调用之前添加以下内容来修改模型视图矩阵:
否则,您可能会修改投影或纹理矩阵。
Ensure that you're modifying the modelview matrix by putting the following before the glRotatef call:
Otherwise, you may be modifying either the projection or a texture matrix instead.