使用 C++ 在 OpenGL 中移动自动旋转的 3d 多边形
Posted
技术标签:
【中文标题】使用 C++ 在 OpenGL 中移动自动旋转的 3d 多边形【英文标题】:Moving an auto rotating 3d polygons in OpenGL with C++ 【发布时间】:2020-06-12 14:43:13 【问题描述】:我正在尝试使用这些键将这些自动旋转多边形移动到 Right-Left 、 Up-Down 和 Front-Back : 对于金字塔,它的键('a','w','s','d','+','-') 对于立方体,它的键('←','↑','→','↓','PAGE UP','PAGE DOWN') 多边形确实在移动,但它们以一种奇怪的方式移动,而不是我希望它们移动,尤其是当我尝试上下和前后移动它们时 我想知道它们为什么会这样移动,我应该改变什么才能使它们正常移动。
这是代码:
#include <GL\glew.h>
#include <SOIL.h>
#include <GL/glut.h>
float pyramid_x = 0, pyramid_y = 0, pyramid_z = 0;
float cube_x = 0, cube_y = 0, cube_z = 0;
float pyramid_angle = 0.0;
float cube_angle = 0.0;
int refresh = 10;//ms
float degree = 0;
void cube_right(void)
glLoadIdentity();
cube_x += 0.1;
void cube_left(void)
glLoadIdentity();
cube_x -= 0.1;
void cube_up(void)
glLoadIdentity();
cube_y += 0.1;
void cube_down(void)
glLoadIdentity();
cube_y -= 0.1;
void cube_front(void)
glLoadIdentity();
cube_z -= 0.1;
void cube_back(void)
glLoadIdentity();
cube_z += 0.1;
void pyramid_right(void)
glLoadIdentity();
pyramid_x += 0.1;
void pyramid_left(void)
glLoadIdentity();
pyramid_x -= 0.1;
void pyramid_up(void)
glLoadIdentity();
pyramid_y += 0.1;
void pyramid_down(void)
glLoadIdentity();
pyramid_y -= 0.1;
void pyramid_front(void)
glLoadIdentity();
pyramid_z -= 0.1;
void pyramid_back(void)
glLoadIdentity();
pyramid_z += 0.1;
void keyboard(int buttons, int x, int y)
switch (buttons)
case GLUT_KEY_LEFT:cube_left(); break;
case GLUT_KEY_RIGHT:cube_right(); break;
case GLUT_KEY_UP:cube_up(); break;
case GLUT_KEY_DOWN:cube_down(); break;
case GLUT_KEY_PAGE_UP:cube_front(); break;
case GLUT_KEY_PAGE_DOWN:cube_back(); break;
glutPostRedisplay();
void keyboard(unsigned char buttons, int x, int y)
switch (buttons)
case 'w':pyramid_front(); break;
case'a':pyramid_left(); break;
case's':pyramid_down(); break;
case'd':pyramid_right(); break;
case '+':pyramid_front(); break;
case'-':pyramid_back(); break;
glutPostRedisplay();
void settings()
glClearColor(1, 1, 1, 0);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
void drawing_function()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); //reset
glTranslatef(2.0f, 0.0f, -7.0f);
glRotatef(pyramid_angle, 1.0f, 0.0f, 0.0f);
glColor3f(1.0, 0.0, 0.0);
glTranslatef(pyramid_x, pyramid_y, pyramid_z);
glBegin(GL_TRIANGLES);
// front
glTexCoord3f(1, 0, 0);
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(1.0f, -1.0f, 1.0f);
// right
glTexCoord3f(0, 0, 0);
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(1.0f, -1.0f, -1.0f);
// back
glTexCoord3f(0, 1, 0);
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(-1.0f, -1.0f, -1.0f);
// left
glTexCoord3f(1, 1, 0);
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
glLoadIdentity();//(Reset model-view matrix)
glTranslatef(-2.0f, 0.0f, -7.0f);
glRotatef(cube_angle, 1.0f, 0.0f, 0.0f);
glTranslatef(cube_x, cube_y, cube_z);
glBegin(GL_QUADS); //cube
glColor3f(1.0f, 0.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);
glColor3f(1.0f, 0.5f, 0.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);
glColor3f(0.0f, 0.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);
glColor3f(1.0f, 1.0f, 0.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);
glColor3f(1.0f, 0.0f, 0.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);
glColor3f(0.0f, 1.0f, 0.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();
glutSwapBuffers();
pyramid_angle += 1.0f;
cube_angle -= 0.2f;
void timer(int deger)
glutPostRedisplay();
glutTimerFunc(refresh, timer, 0);
void view_setting(GLsizei x, GLsizei y)
if (y == 0) y = 1;
GLfloat aspect = (GLfloat)x / (GLfloat)y;
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0f, aspect, 1.0f, 20.0f);
int main(int argc, char** argv)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(50, 50);
glutInitWindowSize(800, 600);
glutCreateWindow("3d");
glutDisplayFunc(drawing_function);
glutReshapeFunc(view_setting);
glutSpecialFunc(keyboard);
glutKeyboardFunc(keyboard);
settings();
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
【问题讨论】:
您能详细说明一下吗? “以奇怪的方式移动”是什么意思? 我为我糟糕的英语道歉,但我会尽力而为:当我将它们向上、向下、前后移动时,它们开始看起来像是在围绕我不想要的坐标中心移动它们像那样移动,我想做的是能够在它们围绕它们旋转时移动它们,而不是围绕坐标编辑的中心:如果你可以尝试运行代码,我会更清楚认为 如果您希望对象围绕自己的中心旋转,请在任何glTranslate
之前调用glRotatef
。如果您希望物体绕另一点运行,请在glRotate
之前调用glTranslate
。想象对象从原点 (0,0,0) 开始,然后可视化每个转换 - 旋转、平移、缩放 - 按照您在代码中调用它们的顺序发生。请记住,所有的旋转始终是相对于原点的。
【参考方案1】:
更改转换的顺序。先旋转再平移:
glTranslatef(2.0f, 0.0f, -7.0f);
glTranslatef(pyramid_x, pyramid_y, pyramid_z);
glRotatef(pyramid_angle, 1.0f, 0.0f, 0.0f);
glTranslatef(-2.0f, 0.0f, -7.0f);
glTranslatef(cube_x, cube_y, cube_z);
glRotatef(cube_angle, 1.0f, 0.0f, 0.0f);
矩阵乘法不是Commutative。 glRotate
和 glTranslate
之类的操作设置一个新矩阵并将当前矩阵乘以新矩阵。因此,首先需要完成的转换必须是代码中的最后一个。
此外keyboard
中还有一个错误。当 w 被按下时,金字塔必须向上移动而不是移动到前面:
case 'w':pyramid_front(); break;
case 'w':pyramid_up(); break;
【讨论】:
以上是关于使用 C++ 在 OpenGL 中移动自动旋转的 3d 多边形的主要内容,如果未能解决你的问题,请参考以下文章
如何在 C++ 中使用 sdl、opengl 移动相机时修复奇怪的相机旋转