围绕世界 0,0,0 而不是对象中心旋转
Posted
技术标签:
【中文标题】围绕世界 0,0,0 而不是对象中心旋转【英文标题】:Rotating around world 0,0,0 instead of object center 【发布时间】:2013-01-04 15:54:27 【问题描述】:我的对象围绕 0,0,0 旋转,但我希望它在 Y 轴上围绕自身旋转。 我知道还有其他情况
Rotating object around itself in OpenGL
glRotatef not rotating as expected
但我在旋转之前没有任何翻译,但它仍然不起作用。
我的已编辑代码:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(_angle,0.0f,1.0f,0.0f);
glPushMatrix();
glTranslatef(0.0f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
//Front
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
//Back
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
//Right
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
//Left
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
//Top
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
//Bottom
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
我的原始代码:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(angle,0.0f,1.0f,0.0f); //The angle is a float that slowly increases until it reaches 360 Degrees, and then resets to 0.0
/* This is a Cube */
glBegin(GL_TRIANGLES);
//Front
glVertex3f(-1.0f, -1.0f, -5.0f);
glVertex3f(-1.0f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.0f, -5.0f);
glVertex3f(-1.0f, -1.0f, -5.0f);
glVertex3f(1.0f, -1.0f, -5.0f);
glVertex3f(1.0f, 1.0f, -5.0f);
//Back
glVertex3f(-1.0f, -1.0f, -7.0f);
glVertex3f(-1.0f, 1.0f, -7.0f);
glVertex3f(1.0f, 1.0f, -7.0f);
glVertex3f(-1.0f, -1.0f, -7.0f);
glVertex3f(1.0f, -1.0f, -7.0f);
glVertex3f(1.0f, 1.0f, -7.0f);
//Right
glVertex3f(1.0f, 1.0f, -5.0f);
glVertex3f(1.0f, -1.0f, -5.0f);
glVertex3f(1.0f, -1.0f, -7.0f);
glVertex3f(1.0f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.0f, -7.0f);
glVertex3f(1.0f, -1.0f, -5.0f);
//Left
glVertex3f(-1.0f, 1.0f, -5.0f);
glVertex3f(-1.0f, -1.0f, -5.0f);
glVertex3f(-1.0f, -1.0f, -7.0f);
glVertex3f(-1.0f, 1.0f, -5.0f);
glVertex3f(-1.0f, 1.0f, -7.0f);
glVertex3f(-1.0f, -1.0f, -7.0f);
//Top
glVertex3f(-1.0f, 1.0f, -7.0f);
glVertex3f(-1.0f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.0f, -5.0f);
glVertex3f(-1.0f, 1.0f, -7.0f);
glVertex3f(1.0f, 1.0f, -7.0f);
glVertex3f(1.0f, 1.0f, -5.0f);
//Bottom
glVertex3f(-1.0f, -1.0f, -7.0f);
glVertex3f(-1.0f, -1.0f, -5.0f);
glVertex3f(1.0f, -1.0f, -5.0f);
glVertex3f(-1.0f, -1.0f, -7.0f);
glVertex3f(1.0f, -1.0f, -7.0f);
glVertex3f(1.0f, -1.0f, -5.0f);
glEnd();
glutSwapBuffers();
【问题讨论】:
你应该避免使用固定功能管道。 【参考方案1】:确保对象的中心位于其局部坐标系的原点。
对于您的立方体,您的 X/Y 似乎居中,但您的 Z 中心看起来在 -6。
【讨论】:
好的,我更改了代码,但它仍然在做同样的事情 如果在旋转之前翻译会怎样? 请注意,矩阵乘法表示的变换是“向后”的,即最后乘以堆栈的变换是第一个应用的变换。以上是关于围绕世界 0,0,0 而不是对象中心旋转的主要内容,如果未能解决你的问题,请参考以下文章
Unity3D视图中心 ( 视图中心概念 | 围绕游戏物体旋转 | 添加游戏物体到游戏场景的位置 )