我弄清楚如何放置"相机"在我创建的立方体内部,我可以像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
}
答案 0 :(得分:1)
您希望使用glTranslatef(float x,float y,float z)来移动相机。
请注意,由于OpenGL的工作原理,转换实际上适用于世界其他地方,而不是相机。因此,上述功能实际上将按照指定的量移动之后绘制的所有内容(而不是相机)。
要让相机移动到某个位置,您需要在将位置传递给函数之前否定该位置的所有组件。这将把世界推向相反的方向,将相机放在你想要的地方。