为什么这个程序不能正确移动相机?
当按下向下键时,我预计茶壶会被拉得更远,但它的大小保持不变。为什么?
注意:这是一个家庭作业,我不允许使用 glTranslate
。
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
void display(void);
class Camera {
public: float eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ;
float aimX, aimY, aimZ;
Camera () {
eyeX = 0.0f ;
eyeY = 0.0f;
eyeZ = 0.5f ;
centerX = 0.0f;
centerY = 0.0f;
centerZ = 0.0f;
upX = 0.0f;
upY = 1.0f;
upZ = 0.0f;
}
void move_camera(double speed) {
aimX = centerX - eyeX;
aimY = centerY - eyeY;
aimZ = centerZ - eyeZ;
eyeX += aimX * speed;
eyeY += aimY * speed;
eyeZ += aimZ * speed;
centerX += aimX *speed;
centerY += aimY *speed;
centerZ += aimZ *speed;
}
};
Camera camera;
void init(void){
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void specialKeys(int key, int x, int y){
if (key==GLUT_KEY_UP){
camera.move_camera(0.03f);
display();
}
if (key==GLUT_KEY_DOWN){
camera.move_camera(-0.03f);
display();
}
}
void reshape(int w, int h){
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)w/(float)h, 0.0f, 200.0f); // fov, aspect ratio, ncp, fcp
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(camera.eyeX, camera.eyeY, camera.eyeZ, // eye
// camera.centerX, camera.centerY, camera.centerZ, // center
// camera.upX, camera.upY, camera.upZ // up
//
//);
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camera.eyeX, camera.eyeY, camera.eyeZ, // eye
camera.centerX, camera.centerY, camera.centerZ, // center
camera.upX, camera.upY, camera.upZ // up
);
//glTranslatef(0.0,0.0,1.0f);
glutWireTeapot(0.5f);
glutSwapBuffers();
glFlush();
}
int main (int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
//glutCreateWindow(argv[0]);
glutInitWindowPosition(500,200);
glutInitWindowSize(800,600);
glutCreateWindow("fgh");
init();
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glutIdleFunc(display);
glutMainLoop();
return 0;
}
While pressing the down key, I expect the teapot to be drawn away as farther, yet it remains the same size. Why?
Note: this is a homework thing, I'm not allowed to use glTranslate
.
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
void display(void);
class Camera {
public: float eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ;
float aimX, aimY, aimZ;
Camera () {
eyeX = 0.0f ;
eyeY = 0.0f;
eyeZ = 0.5f ;
centerX = 0.0f;
centerY = 0.0f;
centerZ = 0.0f;
upX = 0.0f;
upY = 1.0f;
upZ = 0.0f;
}
void move_camera(double speed) {
aimX = centerX - eyeX;
aimY = centerY - eyeY;
aimZ = centerZ - eyeZ;
eyeX += aimX * speed;
eyeY += aimY * speed;
eyeZ += aimZ * speed;
centerX += aimX *speed;
centerY += aimY *speed;
centerZ += aimZ *speed;
}
};
Camera camera;
void init(void){
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void specialKeys(int key, int x, int y){
if (key==GLUT_KEY_UP){
camera.move_camera(0.03f);
display();
}
if (key==GLUT_KEY_DOWN){
camera.move_camera(-0.03f);
display();
}
}
void reshape(int w, int h){
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)w/(float)h, 0.0f, 200.0f); // fov, aspect ratio, ncp, fcp
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(camera.eyeX, camera.eyeY, camera.eyeZ, // eye
// camera.centerX, camera.centerY, camera.centerZ, // center
// camera.upX, camera.upY, camera.upZ // up
//
//);
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camera.eyeX, camera.eyeY, camera.eyeZ, // eye
camera.centerX, camera.centerY, camera.centerZ, // center
camera.upX, camera.upY, camera.upZ // up
);
//glTranslatef(0.0,0.0,1.0f);
glutWireTeapot(0.5f);
glutSwapBuffers();
glFlush();
}
int main (int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
//glutCreateWindow(argv[0]);
glutInitWindowPosition(500,200);
glutInitWindowSize(800,600);
glutCreateWindow("fgh");
init();
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glutIdleFunc(display);
glutMainLoop();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的投影矩阵已损坏。
第三个参数是近剪裁平面的距离。它不能等于
0
,因为这意味着您需要无限精度的深度缓冲区。将其设为0.1
或0.01
。Your projection matrix is damaged.
The third argument is the distance of the near clipping plane. It cannot be equal to
0
as that would imply that you need an inifinite-precision depth buffer. Make it0.1
or0.01
.我错过了对 glutReshapeFunc(reshape); 的调用。就是这样。
I was missing the call to
glutReshapeFunc(reshape);
. That's it.