如何将"camera"放入OpenGL中的多维数据集中

How to put the "camera" inside a cube in OpenGL

本文关键字:数据 数据集 集中 OpenGL camera 放入      更新时间:2023-10-16

我想出了如何将"相机"放在我创建的立方体中,以便我可以以类似 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
}
您需要

使用 glTranslatef(float x,float y,float z) 来移动相机。

请注意,由于OpenGL的工作方式,转换实际上适用于世界其他地方,而不是相机。因此,上述函数实际上会将之后绘制的所有内容(而不是相机)移动指定的量。

要让相机移动到某个位置,您需要在将位置的所有组件传递给函数之前否定它们。这将使世界朝着相反的方向移动,将相机放在您想要的地方。