6.5编程实例-立方体透视投影
1 #include <GL/glut.h> 2 3 GLint winWidth = 600, winHeight = 600; //设置初始化窗口大小 4 5 /* 观察坐标系参数设置*/ 6 7 GLfloat x0 = 0.0, y0= 0.0, z0 =5.0; // 设置观察坐标系原点 8 9 GLfloat xref = 0.0, yref =0.0, zref = 0.0; //设置观察坐标系参考点(视点) 10 11 GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0; // 设置观察坐标系向上向量(y轴) 12 13 /*观察体(视见体)参数设置 */ 14 15 GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;//设置裁剪窗口坐标范围 16 17 GLfloat dnear = 1.5, dfar = 20.0;//设置远、近裁剪面深度范围 18 19 void init (void) 20 21 { 22 23 glClearColor(1.0,1.0,1.0,0.0); 24 25 //glShadeModel (GL_FLAT);//xz 26 27 /*①观察变换*/ 28 29 /*视点变换*/ 30 31 gluLookAt (x0, y0, z0, xref, yref, zref, Vx, Vy, Vz); //指定三维观察参数 32 33 /*模型变换*/ 34 35 glMatrixMode (GL_MODELVIEW); 36 37 glScalef (2.0, 2.0, 2.0); //比例放缩变换 38 39 glRotatef(45.0, 0.0, 1.0, 1.0);//旋转变换 40 41 /*②投影变换*/ 42 43 glMatrixMode (GL_PROJECTION); 44 45 glLoadIdentity (); 46 47 glFrustum (xwMin, xwMax, ywMin, ywMax, dnear, dfar);//透视投影,设置透视视景体 48 49 } 50 51 void displayFcn (void) 52 53 { 54 55 glClear (GL_COLOR_BUFFER_BIT); 56 57 glColor3f (0.0, 1.0, 0.0); // 设置前景色为绿色 58 59 glutSolidCube (1.0);//绘制单位立方体实体 60 61 glColor3f (0.0, 0.0, 0.0); // 设置前景色为黑色 62 63 glLineWidth (2.0); //设置线宽 64 65 glutWireCube (1.0);//绘制单位立方体线框 66 67 glFlush (); 68 69 } 70 71 void reshapeFcn (GLint newWidth, GLint newHeight) 72 73 { 74 75 /*③视口变换 */ 76 77 glViewport (0, 0, newWidth, newHeight);//定义视口大小 78 79 winWidth = newWidth; 80 81 winHeight = newHeight; 82 83 } 84 85 void main (int argc, char** argv) 86 87 { 88 89 glutInit (&argc, argv); 90 91 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 92 93 glutInitWindowPosition (100, 100); 94 95 glutInitWindowSize (winWidth, winHeight); 96 97 glutCreateWindow ("单位立方体的透视投影"); 98 99 init (); 100 101 glutDisplayFunc (displayFcn); 102 103 glutReshapeFunc (reshapeFcn); 104 105 glutMainLoop (); 106 107 }
附上本实验的VC++工程代码(VC++2008)
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步