繁体   English   中英

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

[英]glulookat() - move camera with keyboard( OpenGL)

我正在尝试使用键盘移动相机。 我在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变量被声明为全局double变量。 我尝试用两种方式初始化它们:在声明它们时以及在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进行jour调用似乎毫无用处,您希望它做什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM