如何将“相机”放在 OpenGL 中的立方体内
Posted
技术标签:
【中文标题】如何将“相机”放在 OpenGL 中的立方体内【英文标题】:How to put the "camera" inside a cube in OpenGL 【发布时间】:2015-06-19 17:29:20 【问题描述】:我想出了如何将“相机”放入我创建的立方体中,以便我可以以类似 FPS 的方式移动。我尝试使用 gluLookAt 和 gluPerspective 但我显然错过了一些步骤。在 gluLookAt 之前我应该做什么?
这是目前编写的代码:
int rotate_Y; //used to rotate the cube about the Y-axis
int rotate_X; //used to rotate the cube about the X-axis
//the display function draws the scene and redraws it
void display()
//clear the screen and the z-buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //resets the transformations
glRotatef(rotate_X, 1.0, 0.0, 0.0);
glRotatef(rotate_Y, 0.0, 1.0, 0.0);
//Front Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();
//Back Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glEnd();
//Right Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();
//Left Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(0.7, 0.7, 0.0);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glEnd();
//Upper Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(0.7, 0.7, 0.3);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glEnd();
//Bottom Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(0.2, 0.2, 0.8);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();
glFlush();
glutSwapBuffers(); //send image to the screen
//the special keys function allows interaction via keys (also special ones)
void specialKeys(int key, int x, int y)
switch (key)
case GLUT_KEY_F1:
exit(0);
break;
case GLUT_KEY_LEFT:
rotate_Y -= 5;
break;
case GLUT_KEY_UP:
rotate_X -= 5;
break;
case GLUT_KEY_RIGHT:
rotate_Y += 5;
break;
case GLUT_KEY_DOWN:
rotate_X += 5;
break;
glutPostRedisplay(); //request a screen refresh to see changes
int main(int argc, char*argv[])
//initialize GLUT
glutInit(&argc, argv);
//request double buffering, RGB colors window and a z-buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
//create a window
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Space");
//enable depth
glEnable(GL_DEPTH_TEST);
//callback functions
glutDisplayFunc(display); //display - redraws the scene
glutSpecialFunc(specialKeys); //special - allows interaction with specialkeys
//pass control to GLUT for events
glutMainLoop();
return 0; //this line is never reached
【问题讨论】:
您的代码中没有对gluLookAt
和gluPerspective
的调用。
你可能想阅读OpenGL FAQ
【参考方案1】:
您需要使用glTranslatef(float x,float y,float z) 来移动相机。
请注意,由于 OpenGL 的工作方式,这些变换实际上适用于世界其他地方,而不是相机。所以上面的函数实际上会按照指定的量移动之后绘制的所有东西(而不是相机)。
要让相机移动到某个位置,您需要先否定该位置的所有组件,然后再将它们传递给函数。这将使世界向相反的方向移动,将相机放在您想要的位置。
【讨论】:
没有奇怪,因为没有相机。所有这些矩阵的存在都是为了一个目的:将您想要显示的任何内容粘贴到标准化设备坐标中,从 3D 切换到 2D 变得微不足道。 “相机”只是人类的一个概念,因此他们不会考虑潜在的转换。以上是关于如何将“相机”放在 OpenGL 中的立方体内的主要内容,如果未能解决你的问题,请参考以下文章