C++ OpenGL 旋转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenGL Rotation
提问by Matt Mitchell
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.
我正在尝试在 OpenGL 中做一个简单的旋转,但一定没有抓住重点。我不是在寻找一个特定的修复,而是一个更一般地解释 OpenGL 旋转的快速解释或链接。
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.
但结果不是旋转 90 度的三角形。
EditHmm 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.
编辑嗯,感谢 Mike Haboustak - 看来我的代码正在调用一个使用 glOrtho 的 SetCamera 函数。我对 OpenGL 太陌生了,不知道这意味着什么,但是禁用它并在 Z 轴上旋转会产生所需的结果。
采纳答案by Mike Haboustak
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.
你得到 1 个单位的直线吗?似乎90度腐烂。Y 周围会让你看到一个没有深度的三角形的边。
You should try rotating around the Z axis instead and see if you get something that makes more sense.
你应该试着绕 Z 轴旋转,看看你是否得到了更有意义的东西。
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.
OpenGL 有两个与几何图形显示相关的矩阵,ModelView 和 Projection。在数据在屏幕上可见之前,两者都应用于坐标。首先应用 ModelView 矩阵,将数据从模型空间转换到视图空间。然后应用投影矩阵转换来自视图空间的数据,以便在 2D 显示器上进行“投影”。
ModelView is used to position multiple objects to their locations in the "world", Projection is used to position the objects onto the screen.
ModelView 用于将多个对象定位到它们在“世界”中的位置,Projection 用于将对象定位到屏幕上。
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.
您的代码看起来不错,所以我假设通过阅读文档,您知道 glPushMatrix() 等函数的性质是什么。如果围绕 Z 旋转仍然没有意义,请通过调用 glMatrixMode 验证您是否正在编辑 ModelView 矩阵。
回答by spate
Ensure that you're modifying the modelview matrix by putting the following before the glRotatef call:
通过在 glRotatef 调用之前放置以下内容,确保您正在修改模型视图矩阵:
glMatrixMode(GL_MODELVIEW);
Otherwise, you may be modifying either the projection or a texture matrix instead.
否则,您可能正在修改投影或纹理矩阵。
回答by Perry
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.
“接受的答案”并不完全正确 - 围绕 Z 旋转不会帮助您看到这个三角形,除非您在此代码之前做了一些奇怪的事情。在这种情况下,删除 glOrtho(...) 调用可能已经纠正了问题,但您仍然有一些其他问题。
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.
您之前是否定位过相机?在 OpenGL 中,相机位于原点,向下看 Z 轴,正 Y 为向上。在这种情况下,三角形与您的眼睛在同一平面上绘制,但向上和向右。除非你有一个很奇怪的投影矩阵,否则你是看不到它的。gluLookat() 是最简单的命令,但任何移动当前矩阵(应该是 MODELVIEW)的命令都可以工作。
您正在以左手或顺时针方法绘制三角形,而 OpenGL 的默认方法是右手或逆时针坐标系。这意味着,如果您正在剔除背面(您可能不会,但随着您的进步可能会继续),您将不会像预期的那样看到三角形。要查看问题,请将您的右手放在您的脸前,假设它在 XY 平面上,按照绘制顶点 (1,1) 到 (3,2) 到 (3,1) 的顺序移动手指)。当你这样做时,你的拇指背对着你的脸,这意味着你正在看三角形的背面。您需要养成使用右手方法绘制人脸的习惯,因为这是 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.
我可以推荐的最好的方法是使用 NeHe 教程 - http://nehe.gamedev.net/。他们首先向您展示如何在多个系统中设置 OpenGL,然后继续绘制三角形,然后慢慢地继续学习更高级的主题。他们很容易遵循。
回答by Ant
When I had a first look at OpenGL, the NeHe tutorials(see the left menu) were invaluable.
当我第一次看到 OpenGL 时,NeHe 教程(见左侧菜单)是非常宝贵的。
回答by Adam Hollidge
I'd like to recommend a book:
我想推荐一本书:
3D Computer Graphics : A Mathematical Introduction with OpenGLby Samuel R. Buss
3D Computer Graphics : A Mathematical Introduction with OpenGLby Samuel R. Buss
It provides very clear explanations, and the mathematics are widely applicable to non-graphics domains. You'll also find a thorough description of orthographic projections vs. perspective transformations.
它提供了非常清晰的解释,并且数学广泛适用于非图形领域。您还将找到对正交投影与透视变换的全面描述。
回答by Adi
Regarding Projection matrix, you can find a good source to start with here:
关于投影矩阵,你可以在这里找到一个很好的来源:
http://msdn.microsoft.com/en-us/library/bb147302(VS.85).aspx
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).
它解释了一些关于如何构造一种类型的投影矩阵。正交投影是这种矩阵的基本/原始形式,基本上是取 3 个轴坐标中的 2 个并将它们投影到屏幕上(您仍然可以翻转轴并缩放它们,但没有扭曲或透视效果) .
transformation of matrices is most likely one of the most important things when rendering in 3D and basically involves 3 matrix stages:
矩阵变换很可能是 3D 渲染时最重要的事情之一,基本上涉及 3 个矩阵阶段:
- Transform1 = Object coordinates system to World (for example - object rotation and scale)
- Transform2 = World coordinates system to Camera (placing the object in the right place)
- Transform3 = Camera coordinates system to Screen space (projecting to screen)
- Transform1 = 对象坐标系统到世界(例如 - 对象旋转和缩放)
- Transform2 = 世界坐标系到相机(将对象放置在正确的位置)
- Transform3 = 相机坐标系到屏幕空间(投影到屏幕)
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.
通常将 3 矩阵乘法结果称为 WorldViewProjection 矩阵(如果您遇到过这个术语),因为它将坐标从模型空间转换为世界,然后转换为相机,最后转换为屏幕表示。
Have fun
玩得开心