gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:29 24 4
gpt4 key购买 nike

我正在尝试使用键盘移动相机。我在 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:

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

最佳答案

glPushMatrix 之后应该是 glPopMatrix。

看来你溢出了矩阵堆栈,你检查了 glGetError 结果了吗?

此外,像这样调用 glPushMatrix 似乎毫无用处,您希望它做什么?

关于c++ - glulookat() - 用键盘移动相机(OpenGL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8781887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com