2024年4月1日发(作者:)
1,在OPENGL中,视图变换是指保持模型坐标不变情况下,从不同的方位观察模型,常
用的函数为 glLookAt。
2,在OPENGL中,如果想要移动、缩放、旋转模型,则使用模型变换。模型变换使用的
函数有:
glTranslatef 平移
glRotatef 旋转
glScalef 缩放
3,glLoadIdentity是一个特殊,它既将模型世界坐标恢复,又将视野也恢复到(0,0,0),朝
向是-z,方向向上
下面的一段摘自百度百科:
视点转换
函数原型
void gluLookAt(GLdouble eyex,GLdouble eyey,GLdouble eyez,GLdouble
centerx,GLdouble centery,GLdouble centerz,GLdouble upx,GLdouble upy,GLdouble
upz);
该函数定义一个视图矩阵,并与当前矩阵相乘。
第一组eyex, eyey,eyez 相机在世界坐标的位置
第二组centerx,centery,centerz 相机镜头对准的物体在世界坐标的位置
第三组upx,upy,upz 相机向上的方向在世界坐标中的方向
你把相机想象成为你自己的脑袋:
第一组数据就是脑袋的位置
第二组数据就是眼睛看的物体的位置
第三组就是头顶朝向的方向(因为你可以歪着头看同一个物体)。
[cpp] view plaincopy
1. #include "stdafx.h"
2. #include
3. #include
4.
5. void init(void)
6. {
7. glClearColor (0.0, 0.0, 0.0, 0.0); //背景黑色
8. }
9.
10. void display(void)
11. {
12. glClear (GL_COLOR_BUFFER_BIT);
13. glColor3f (1.0, 1.0, 1.0); //画笔白色
14.
15. glLoadIdentity(); //加载单位矩阵
16.
17. gluLookAt(0.0,0.0,5.0, 0.0,0.0,0.0, 0.0,1.0,0.0);
18. glutWireTeapot(2);
19. glutSwapBuffers();
20. }
21.
22. void reshape (int w, int h)
23. {
24. glViewport (0, 0, (GLsizei) w, (GLsizei) h);
25. glMatrixMode (GL_PROJECTION);
26. glLoadIdentity ();
27. gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
28. glMatrixMode(GL_MODELVIEW);
29. glLoadIdentity();
30. gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
31. }
32. int main(int argc, char** argv)
33. {
34. glutInit(&argc, argv);
35. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
36. glutInitWindowSize (500, 500);
37. glutInitWindowPosition (100, 100);
38. glutCreateWindow (argv[0]);
39. init ();
40. glutDisplayFunc(display);
41. glutReshapeFunc(reshape);
42. glutMainLoop();
43. return 0;
44. }
一、上面的display()函数中:gluLookAt(0.0,0.0,5.0, 0.0,0.0,0.0, 0.0,1.0,0.0); 相当于我们
的脑袋位置在(0.0,0.0,5.0)处,眼睛望向(0.0,0.0,0.0),即原点。后面的三个参数(0.0,1.0,0.0),y


发布评论