glulookat()-使用键盘移动相机(OpenGL)

glulookat() - move camera with keyboard( OpenGL)

本文关键字:移动 相机 OpenGL 键盘 glulookat      更新时间:2023-10-16

我正试图用键盘移动相机。我在draw方法中调用glulookat函数,如下所示:

 gluLookAt(posx,posy,posz,lookx,looky,lookz,upx,upy,upz);

此外,例如,为了在X轴上移动相机的位置,我有以下代码:

 void keyboard(unsigned char key, int x, int y) {
switch(key) {
    case 'w' :
        break;
    case 'a' : 
        posx-=1.0;
        break;
    case 's' :
        break;
    case 'd' :
        posx+=1.0;
        break;
}
 }

posx、posy、posz、lookx、looky、lookz、upx、upy、upz变量被声明为全局双变量。我尝试过用两种方式初始化它们:在声明它们时,以及在init()方法中,但都没有成功。相机没有移动,尽管程序正确接收键盘输入,因为我已经单独测试了这一方面。你知道我做错了什么吗?

编辑:为了更好地理解,我提供了主要代码:

Outer_space* space;
Light* sceneLight;
Light* sceneLight2;
Light* sceneLight3;
Satelite* satelite1;
double posx=35.0,posy=35.0,posz=35.0,lookx=0,looky=1,lookz=0,upx=0,upy=0,upz=-1;
void init(void) {

//random number generator
   srand(time(NULL));

   space = new Outer_space(12, 12,12, new Vector3D(0.5f, 0.7f, 0.9f));
   sceneLight = new Light(0, 0);
   sceneLight->setTranslation(new Vector3D(0, 20, 0));
   sceneLight->setConstantAttenuation(0.09f);
   sceneLight->setLinearAttenuation(0.08f);
   sceneLight2 = new Light(1, 0);
   sceneLight2->setTranslation(new Vector3D(20, 0, 0));
   sceneLight2->setConstantAttenuation(0.09f);
   sceneLight2->setLinearAttenuation(0.08f);
   sceneLight3 = new Light(2, 0);
   sceneLight3->setTranslation(new Vector3D(0, 0, 20));
   sceneLight3->setConstantAttenuation(0.09f);
   sceneLight3->setLinearAttenuation(0.08f);

   satelite1 = new Satelite(2,new Vector3D(0.2f,0.3f,0.5f));
   satelite1->setTranslation(new Vector3D(10,10,10));
   satelite1->setRotation(new Vector3D(-90, 0, 0));
   satelite1->setScale(new Vector3D(10, 10, 10));


   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   glEnable(GL_DEPTH_TEST);
   glShadeModel(GL_SMOOTH);
   glEnable(GL_LIGHTING);
   glEnable(GL_NORMALIZE);
}

void draw(void) {
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

//gluLookAt(0,20,0, 0, 0, 0, 0, 1, 0);
   gluLookAt(posx,posy,posz,lookx,looky,lookz,upx,upy,upz);
   space->draw();
   sceneLight->draw();
   sceneLight2->draw();
   sceneLight3->draw();
   satelite1->draw();

   glPushMatrix();
   glRasterPos3f(-8.5, 4, -6);

   glutSwapBuffers();
}

void update(void){
   glutPostRedisplay();
}
void resize(int w, int h) {
   glViewport(0, 0, (GLsizei)w, (GLsizei)h);
   GLfloat aspect = (GLfloat)w / (GLfloat)h;
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(45, aspect, 1.0, 60);
}
void keyboard(unsigned char key, int x, int y) {
   switch (key)
   {
   case 'w' :
      break;
   case 'a' :
      posx-=1.0;
      break;
   case 's' :
      break;
   case 'd' :
      posx+=1.0;
      break;
   }
}
void specialKeyboard(int key, int x, int y) {
   switch (key)
   {
       case GLUT_KEY_RIGHT:
      posx+=1;
      break;
   case GLUT_KEY_LEFT:
      posx-=1;
      break;
   case GLUT_KEY_UP:
      break;
   case GLUT_KEY_DOWN:
      break;
   }
}
int main(int argc, char **argv) {
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
   glutInitWindowSize(800, 600);
   glutInitWindowPosition(100, 100);
   glutCreateWindow("Asteroid Escape");
   init();
   glutIdleFunc(update);
   glutDisplayFunc(draw);
   glutReshapeFunc(resize);
   glutKeyboardFunc(keyboard);
   glutSpecialFunc(specialKeyboard);
   glutMainLoop();
   return 0;
}

第2版:

我已经注释掉了glPushMatrix()从draw()方法调用,现在相机似乎在移动。解释是什么?

glPushMatrix后面应该跟有glPopMatrix。

似乎您溢出了矩阵堆栈,您检查了glGetError结果吗?

此外,对glPushMatrix的调用看起来像这样毫无用处,你希望它能做什么?