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变量被声明为全局双变量。我尝试过用两种方式初始化它们:在声明它们时,以及在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()方法调用,现在相机似乎在移动。解释是什么?
似乎您溢出了矩阵堆栈,您检查了glGetError结果吗?
此外,对glPushMatrix的调用看起来像这样毫无用处,你希望它能做什么?
相关文章:
- OpenGL 相机移动程序顶点着色器问题
- 如何将相机移动并将其粘贴在玩家身上
- OpenGL - 让相机移动
- OpenCV中移动相机的深度/视差图
- 将网格与VTK相机对齐(相机的视觉表示,随之移动和旋转的网格)
- 检测OpenCV C (移动相机)上的运动
- 如何在 3D 空间中正确移动相机
- OpenGL3SFML2.0rc FPS风格的相机-抖动的鼠标移动
- 如何在 directx11 中将相机移动到窗口的 1 个像素单位,而不是世界空间中的 1 个单位
- 当相机移动DirectX时,模型会消失
- 如何在OpenGL中使用鼠标在相机周围移动
- 如何将鼠标移动转换为相机平移
- OpenGL程序/w着色器,相机移动不正确
- 需要帮助在OpenGL中移动相机
- OpenGL-向前移动相机
- glulookat()-使用键盘移动相机(OpenGL)
- 移动相机在OpenGL
- OpenGL如何实现自由移动相机
- 移动相机
- DirectX9左右移动相机