openGL的简单应用。
1.opengl的菜单功能(实现直线、折线和矩形的橡皮筋绘制技术)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include"gl/glut.h" 2 static GLsizei iMode=1; 3 int ipointnum=0;//点的数目 4 int x1=0,x2=0,y1=0,y2=0;//点的坐标 5 int winWidth=400,winHeight=300;//窗口的宽度和高度 6 int num=0,a[100],b[100],w1,h1,w2,h2; 7 void Initial(void) 8 { 9 glClearColor(1.0f,1.0f,1.0f,1.0f);//设置窗口的背景颜色 10 } 11 12 void ChangeSize(int w,int h) 13 { 14 winWidth=w;winHeight=h;//保存当前窗口的大小 15 glViewport(0,0,w,h);//指定窗口显示区域 16 glMatrixMode(GL_PROJECTION);//指定设置投影参数 17 glLoadIdentity();//调用单位矩阵,去掉以前的投影参数设置 18 gluOrtho2D(0.0,winWidth,0.0,winHeight);//设置投影参数 19 } 20 void Display(void) 21 { 22 GLint i; 23 glClear(GL_COLOR_BUFFER_BIT);//用当前背景色填充窗口 24 glColor3f(1.0f,0.0f,0.0f);//指定当前的绘图颜色 25 if(iMode==1) //绘制直线段 26 { 27 glBegin(GL_LINES); 28 glVertex2i(x1,y1); 29 glVertex2i(x2,y2); 30 glEnd(); 31 } 32 else if(iMode==2)//绘制折线 33 { 34 glBegin(GL_LINE_STRIP); 35 for(i=0;i<num;i++) 36 { 37 glVertex2i(a[i],b[i]); 38 } 39 glEnd(); 40 glBegin(GL_LINES); 41 glVertex2i(w1,h1); 42 glVertex2i(w2,h2); 43 glEnd(); 44 } 45 else if(iMode==3)//绘制矩形 46 { 47 glBegin(GL_LINES);//通过给定两点坐标绘制矩形的四条边 48 glVertex2i(x1,y1); 49 glVertex2i(x2,y1); 50 glEnd(); 51 glBegin(GL_LINES); 52 glVertex2i(x1,y1); 53 glVertex2i(x1,y2); 54 glEnd(); 55 glBegin(GL_LINES); 56 glVertex2i(x2,y1); 57 glVertex2i(x2,y2); 58 glEnd(); 59 glBegin(GL_LINES); 60 glVertex2i(x1,y2); 61 glVertex2i(x2,y2); 62 glEnd(); 63 } 64 glutSwapBuffers();//交换缓冲区 65 } 66 void MousePlot(GLint button,GLint action,GLint xMouse,GLint yMouse) 67 { 68 if(iMode==2){ 69 if(button==GLUT_LEFT_BUTTON&&action==GLUT_DOWN) 70 { 71 if(num==0) 72 { 73 w1=xMouse;h1=winHeight-yMouse;//确定直线段的第一个端点 74 a[num]=w1;b[num]=h1;num++; 75 } 76 else 77 { 78 79 w2=xMouse;h2=winHeight-yMouse;//确定直线段的第二个端点 80 a[num]=w2;b[num]=h2;num++; 81 w1=w2;h1=h2; 82 glutPostRedisplay();//指定窗口重新绘制 83 } 84 } 85 if(button==GLUT_RIGHT_BUTTON&&action==GLUT_DOWN) 86 { 87 num=0; 88 glutPostRedisplay(); 89 } 90 } 91 else if(iMode==3||iMode==1) 92 { 93 if(button==GLUT_LEFT_BUTTON&&action==GLUT_DOWN) 94 { 95 if(ipointnum==0||ipointnum==2) 96 { 97 ipointnum=1; 98 x1=xMouse;y1=winHeight-yMouse;//确定直线段的第一个端点 99 } 100 else 101 { 102 ipointnum=2; 103 x2=xMouse;y2=winHeight-yMouse;//确定直线段的第二个端点 104 glutPostRedisplay();//指定窗口重新绘制 105 } 106 } 107 if(button==GLUT_RIGHT_BUTTON&&action==GLUT_DOWN) 108 { 109 ipointnum=0; 110 glutPostRedisplay(); 111 } 112 } 113 } 114 void PassiveMouseMove(GLint xMouse,GLint yMouse) 115 { 116 if(iMode==2) 117 { 118 if(num){ 119 w2=xMouse; 120 h2=winHeight-yMouse;//将当前鼠标位置指定为直线的未固定端点 121 glutPostRedisplay(); 122 } 123 } 124 else if(iMode==3||iMode==1) 125 { 126 if(ipointnum==1) 127 { 128 x2=xMouse; 129 y2=winHeight-yMouse; 130 glutPostRedisplay(); 131 } 132 } 133 } 134 void ProcessMenu(int value) 135 { 136 iMode=value; 137 glutPostRedisplay(); 138 } 139 int main(int argc,char *argv[]) 140 { 141 glutInit(&argc,argv); 142 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);//使用双缓存及RGB模型 143 glutInitWindowSize(400,300);//指定窗口的大小 144 glutInitWindowPosition(100,100);//指定窗口在屏幕上的位置 145 glutCreateWindow("橡皮筋技术"); 146 147 glutCreateMenu(ProcessMenu); 148 glutAddMenuEntry("直线的橡皮筋绘制",1); 149 glutAddMenuEntry("折线的橡皮筋绘制",2); 150 glutAddMenuEntry("矩形的橡皮筋绘制",3); 151 glutAttachMenu(GLUT_RIGHT_BUTTON); 152 153 glutDisplayFunc(Display); 154 glutReshapeFunc(ChangeSize);//指定窗口再整形回调函数 155 glutMouseFunc(MousePlot);//指定鼠标响应函数 156 glutPassiveMotionFunc(PassiveMouseMove);//指定鼠标移动响应函数 157 158 Initial(); 159 glutMainLoop();//启动主GLUT时间处理循环 160 return 0; 161 }
2.黑白棋盘填充
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include"gl/glut.h" 2 static GLsizei iMode =0; 3 void Initial(void) 4 { 5 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 6 glMatrixMode(GL_PROJECTION); 7 gluOrtho2D(0.0, 200.0, 0.0, 150.0); 8 } 9 void Display(void) 10 { 11 glClear(GL_COLOR_BUFFER_BIT); 12 glColor3f(0.0f, 0.0f, 0.0f); 13 if (iMode == 1) 14 { 15 glRectf(50.0f, 100.0f, 150.0f, 50.0f); 16 } 17 else if(iMode==2){ 18 GLubyte fly[] = 19 { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 20 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 21 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 22 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 23 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 24 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 25 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 26 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 27 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 28 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 29 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 30 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 31 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 32 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 33 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 34 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 35 }; 36 glEnable(GL_POLYGON_STIPPLE); 37 glPolygonStipple(fly); 38 glRectf(50.0f, 100.0f, 150.0f, 50.0f); 39 } 40 glutSwapBuffers(); 41 } 42 void ProcessMenu(int value) 43 { 44 iMode = value; 45 glutPostRedisplay(); 46 } 47 int main(int argc, char *argv[]) 48 { 49 glutInit(&argc, argv); 50 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 51 glutInitWindowSize(400, 300); 52 glutInitWindowPosition(100, 100); 53 glutCreateWindow("用棋盘图形填充矩形"); 54 55 glutCreateMenu(ProcessMenu); 56 glutAddMenuEntry("不填充矩形 ", 1); 57 glutAddMenuEntry("填充矩形", 2); 58 glutAttachMenu(GLUT_RIGHT_BUTTON); 59 60 glutDisplayFunc(Display); 61 Initial(); 62 glutMainLoop(); 63 return 0; 64 }
3.分别在四个投影视区内显示空间四面体的三视图、透视投影图
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include "stdafx.h" 2 #include"stdio.h" 3 #include"vector" 4 #include"iostream" 5 #include<gl/glut.h> 6 using namespace std; 7 const int maxnum = 200; 8 vector<int>face[10]; 9 int winwidth = 1000, winheight = 600; 10 int pointnum = 4, facenum = 4; 11 double matrix[4][4]= 12 { 13 { 1, 0, 0, 0 }, 14 { 0, 1, 0, 0 }, 15 { 0, 0, 1, 0 }, 16 { 500, 300, 300, 1 } 17 }; 18 double xoz[4][4]= 19 { 20 {1,0,0,0}, 21 {0,0,0,0}, 22 {0,0,1,0}, 23 {0,0,0,1} 24 }; 25 double xoy[4][4] 26 { 27 {1,0,0,0}, 28 {0,0,-1,0}, 29 {0,0,0,0}, 30 {0,0,-50,1} 31 }; 32 double yoz[4][4] 33 { 34 {0,0,0,0}, 35 {-1,0,0,0}, 36 {0,0,1,0}, 37 {-150,0,0,1} 38 }; 39 double dd = -400, nn = -200, mm = -360, ll = -500; 40 double yoy[4][4]= 41 { 42 {1,0,0,0}, 43 {0,1,0,0}, 44 {0,0,0,1/dd}, 45 {ll,mm,0,1+nn/dd} 46 }; 47 struct defpoint 48 { 49 double x, y, z, tag; 50 }point[maxnum], tpoint[maxnum], xozpoint[maxnum], 51 xoypoint[maxnum], yozpoint[maxnum], yoypoint[maxnum]; 52 53 void thpmidinit() 54 { 55 pointnum = 4; 56 point[0].x = 400, point[0].y = 0, point[0].z = 0, point[0].tag = 1; 57 point[1].x = 400, point[1].y = 200, point[1].z = 0, point[1].tag = 1; 58 point[2].x = 0, point[2].y = 200, point[2].z = 0, point[2].tag = 1; 59 point[3].x =200, point[3].y = 200, point[3].z = 200, point[3].tag = 1; 60 61 facenum = 4; 62 face[0].push_back(0); face[0].push_back(1); face[0].push_back(2); 63 face[1].push_back(0); face[1].push_back(1); face[1].push_back(3); 64 face[2].push_back(0); face[2].push_back(2); face[2].push_back(3); 65 face[3].push_back(1); face[3].push_back(2); face[3].push_back(3); 66 } 67 68 void transform(defpoint newpoint[], defpoint oldpoint[], double tran[4][4]) 69 { 70 for (int i = 0; i < pointnum; i++) 71 { 72 double tx = oldpoint[i].x, ty = oldpoint[i].y, tz = oldpoint[i].z, ttag = oldpoint[i].tag; 73 newpoint[i].x = tx*tran[0][0] + ty*tran[1][0] + tz*tran[2][0] + ttag*tran[3][0]; 74 newpoint[i].y = tx*tran[0][1] + ty*tran[1][1] + tz*tran[2][1] + ttag*tran[3][1]; 75 newpoint[i].z = tx*tran[0][2] + ty*tran[1][2] + tz*tran[2][2] + ttag*tran[3][2]; 76 newpoint[i].tag = tx*tran[0][3] + ty*tran[1][3] + tz*tran[2][3] + ttag*tran[3][3]; 77 if (newpoint[i].tag != 0 && newpoint[i].tag != 1) 78 { 79 newpoint[i].x /= newpoint[i].tag; 80 newpoint[i].y /= newpoint[i].tag; 81 newpoint[i].z /= newpoint[i].tag; 82 newpoint[i].tag = 1; 83 } 84 } 85 } 86 87 void reshape(int w, int h) 88 { 89 winwidth = w; winheight = h; 90 glViewport(0, 0, w, h); 91 glMatrixMode(GL_PROJECTION); 92 glLoadIdentity(); 93 gluOrtho2D(0.0, winwidth, 0.0, winheight); 94 } 95 void ondraw(defpoint temppoint[]) 96 { 97 glBegin(GL_LINES); 98 for (int i = 0; i < facenum; i++) 99 { 100 int size = face[i].size(); 101 for (int j = 0; j < size; j++) 102 { 103 glVertex2d(temppoint[face[i][j]].x, temppoint[face[i][j]].z); 104 glVertex2d(temppoint[face[i][(j + 1) % size]].x, temppoint[face[i][(j + 1) % size]].z); 105 } 106 } 107 glEnd(); 108 } 109 110 void ondraw_0(defpoint temppoint[]) 111 { 112 glColor3f(1.0f, 0.0f, 0.0f); 113 glBegin(GL_LINES); 114 glVertex2d(temppoint[0].x, temppoint[0].y); 115 glVertex2d(temppoint[1].x, temppoint[1].y); 116 glVertex2d(temppoint[0].x, temppoint[0].y); 117 glVertex2d(temppoint[2].x, temppoint[2].y); 118 glVertex2d(temppoint[0].x, temppoint[0].y); 119 glVertex2d(temppoint[3].x, temppoint[3].y); 120 glVertex2d(temppoint[1].x, temppoint[1].y); 121 glVertex2d(temppoint[2].x, temppoint[2].y); 122 glVertex2d(temppoint[1].x, temppoint[1].y); 123 glVertex2d(temppoint[3].x, temppoint[3].y); 124 glVertex2d(temppoint[2].x, temppoint[2].y); 125 glVertex2d(temppoint[3].x, temppoint[3].y); 126 127 glEnd(); 128 glColor3f(0.0f, 1.0f, 0.0f); 129 glBegin(GL_LINES); 130 glVertex2d(temppoint[0].x, temppoint[0].y); 131 glVertex2d(0, 0); 132 glVertex2d(temppoint[1].x, temppoint[1].y); 133 glVertex2d(0, 0); 134 glVertex2d(temppoint[2].x, temppoint[2].y); 135 glVertex2d(0, 0); 136 glVertex2d(temppoint[3].x, temppoint[3].y); 137 glVertex2d(0, 0); 138 glEnd(); 139 } 140 141 void oncoordinate() 142 { 143 glColor3f(0.0f, 0.0f, 0.0f); 144 glBegin(GL_LINES); 145 146 glVertex2d(winwidth / 2, 0); 147 glVertex2d(winwidth / 2, winheight); 148 glVertex2d(0, winheight/2); 149 glVertex2d(winwidth, winheight/2); 150 151 glVertex2d(winwidth/2+5, winheight-15); 152 glVertex2d(winwidth/2+15, winheight-15); 153 glVertex2d(winwidth/2+5, winheight-25); 154 155 glVertex2d(winwidth/2+15, winheight-15); 156 glVertex2d(winwidth/2+5, winheight-25); 157 glVertex2d(winwidth/2+15, winheight-25); 158 159 glVertex2d(winwidth/2-5, winheight-5); 160 glVertex2d(winwidth/2,winheight); 161 glVertex2d(winwidth/2+5, winheight-5); 162 glVertex2d(winwidth/2, winheight); 163 164 glVertex2d(winwidth/2+25,0+15); 165 glVertex2d(winwidth/2+20,0+10); 166 glVertex2d(winwidth/2+15,0+15); 167 glVertex2d(winwidth/2+20,0+10); 168 glVertex2d(winwidth/2+20,0+10); 169 glVertex2d(winwidth/2+20,0+5); 170 171 glVertex2d(winwidth/2-5,0+5); 172 glVertex2d(winwidth/2,0); 173 glVertex2d(winwidth/2 + 5,0+5); 174 glVertex2d(winwidth/2, 0); 175 176 glVertex2d(0+25,winheight/2+15); 177 glVertex2d(0+20, winheight/2+10); 178 glVertex2d(0+15, winheight/2+15); 179 glVertex2d(0+20, winheight/2+10); 180 glVertex2d(0+20, winheight/2+10); 181 glVertex2d(0+20, winheight/2+5); 182 183 glVertex2d(0+5, winheight/2+5); 184 glVertex2d(0, winheight/2); 185 glVertex2d(0+ 5, winheight/2-5); 186 glVertex2d(0, winheight/2 ); 187 188 glVertex2d(winwidth-25, winheight/2 + 15); 189 glVertex2d(winwidth-15, winheight/2 + 5); 190 glVertex2d(winwidth-25, winheight/2 + 5); 191 glVertex2d(winwidth-15, winheight/2 + 15); 192 193 glVertex2d(winwidth-5, winheight/2 -5); 194 glVertex2d(winwidth , winheight/2 ); 195 glVertex2d(winwidth - 5, winheight/2 + 5); 196 glVertex2d(winwidth, winheight/2); 197 198 glEnd(); 199 } 200 201 void display() 202 { 203 glClear(GL_COLOR_BUFFER_BIT); 204 oncoordinate(); 205 glColor3f(0.0f, 0.0f, 0.0f); 206 glBegin(GL_LINES); 207 glVertex2d(winwidth/2,0); 208 glVertex2d(winwidth/2, winheight); 209 glVertex2d(0, winheight/2); 210 glVertex2d(winwidth, winheight / 2); 211 glEnd(); 212 213 glColor3f(1.0f, 0.0f, 0.0f); 214 ondraw(xozpoint); 215 glColor3f(0.0f, 1.0f, 0.0f); 216 ondraw(xoypoint); 217 glColor3f(0.0f, 0.0f, 1.0f); 218 ondraw(yozpoint); 219 glColor3f(1.0f, 0.0f, 0.0f); 220 ondraw_0(yoypoint); 221 glutSwapBuffers(); 222 } 223 224 void getthpmidview() 225 { 226 transform(xozpoint, point, xoz); 227 transform(xoypoint, point, xoy); 228 transform(yozpoint, point, yoz); 229 transform(yoypoint, point, yoy); 230 transform(xozpoint, xozpoint, matrix); 231 transform(xoypoint, xoypoint, matrix); 232 transform(yozpoint, yozpoint, matrix); 233 transform(yoypoint, yoypoint, matrix); 234 } 235 void initial() 236 { 237 for (int i = 0; i < 10; i++) 238 face[i].clear(); 239 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 240 thpmidinit(); 241 getthpmidview(); 242 } 243 int main(int argc, char *argv[]) 244 { 245 glutInit(&argc, argv); 246 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 247 glutInitWindowSize(1000, 600); 248 glutInitWindowPosition(150, 100); 249 glutCreateWindow("三维图形 透视投影图&三视图 演示程序"); 250 glutDisplayFunc(display); 251 glutReshapeFunc(reshape); 252 initial(); 253 glutMainLoop(); 254 return 0; 255 }
4. 综合应用。实现菜单功能、四面体的旋转和相应三视图的输出
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <gl/glut.h> 2 static GLsizei iMode = 1; 3 static GLfloat xRot = 0.0f; 4 static GLfloat yRot = 0.0f; 5 int winWidth = 400, winHeight = 200; 6 int iPointNum = 0; 7 int x1=0,x2=0,y1=0,y2=0; 8 GLuint Cube; 9 void initial(void) 10 { 11 glClearColor(1.0, 1.0, 1.0, 1.0); 12 glMatrixMode (GL_PROJECTION); 13 glLoadIdentity(); 14 gluOrtho2D(-3.0, 3.0, -3.0,3.0); 15 Cube=glGenLists(1); 16 glNewList(Cube,GL_COMPILE); 17 glBegin(GL_TRIANGLE_FAN); 18 glShadeModel(GL_FLAT); 19 glColor3f(1.0,0.0,0.0); 20 glVertex3f(1,1,1); 21 glVertex3f(2,0,0); 22 glVertex3f(2,1,0); 23 glEnd(); 24 glBegin(GL_TRIANGLE_FAN); 25 glShadeModel(GL_FLAT); 26 glColor3f(1.0,1.0,0.0); 27 glVertex3f(1,1,1); 28 glVertex3f(0,1,0); 29 glVertex3f(2,1,0); 30 glEnd(); 31 glBegin(GL_TRIANGLE_FAN); 32 glShadeModel(GL_FLAT); 33 glColor3f(0.0,0.0,1.0); 34 glVertex3f(1,1,1); 35 glVertex3f(0,1,0); 36 glVertex3f(2,0,0); 37 glEnd(); 38 glBegin(GL_TRIANGLE_FAN); 39 glShadeModel(GL_FLAT); 40 glColor3f(0.0,1.0,0.0); 41 glVertex3f(0,1,0); 42 glVertex3f(2,0,0); 43 glVertex3f(2,1,0); 44 glEnd(); 45 glEndList(); 46 } 47 void triangle (GLsizei mode) 48 { 49 if(mode == 1) 50 { 51 glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); 52 glMatrixMode(GL_PROJECTION); 53 glLoadIdentity(); 54 gluOrtho2D(-5.0,5.0,-5.0,5.0); 55 glMatrixMode(GL_MODELVIEW); 56 glPushMatrix(); 57 glLoadIdentity(); 58 switch(iMode){ 59 case 1:glCallList(Cube);break; 60 case 2:glRotatef(-90.0,1.0,0.0,0.0);glCallList(Cube);break; 61 case 3:glRotatef(90.0,0.0,1.0,0.0);glCallList(Cube);break; 62 } 63 glPopMatrix(); 64 } 65 else 66 { 67 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); 68 glMatrixMode(GL_PROJECTION); 69 glLoadIdentity(); 70 gluPerspective(45.0f,1,1.0f,100.0f); 71 gluLookAt(0,0,-10,0,0,0,0,1,0); 72 glMatrixMode(GL_MODELVIEW); 73 glPushMatrix(); 74 glRotatef(xRot,1.0f,0.0f,0.0f); 75 glRotatef(yRot,0.0f,1.0f,0.0f); 76 glCallList(Cube); 77 glPopMatrix(); 78 glRotatef(xRot, 1.0f, 0.0f, 0.0f); 79 glRotatef(yRot, 0.0f, 1.0f, 0.0f); 80 } 81 } 82 void Display(void) 83 { 84 glClear(GL_COLOR_BUFFER_BIT); 85 glViewport(0, 0, 200, 200); 86 triangle(1); 87 glViewport(200, 0, 200, 200); 88 triangle(2); 89 glFlush(); 90 } 91 void PassiveMouseMove (GLint xMouse, GLint yMouse) 92 { 93 if(iPointNum == 1) { 94 x2 = xMouse; 95 y2 = winHeight - yMouse; 96 if(y2<=y1) xRot -= 0.05f; 97 if(y2>=y1) xRot += 0.05f; 98 if(x2>=x1) yRot -= 0.05f; 99 if(x2<=x1) yRot += 0.05f; 100 if(xRot > 356.0f) xRot = 0.0f; 101 if(xRot < -1.0f) xRot = 355.0f; 102 if(yRot > 356.0f) yRot = 0.0f; 103 if(yRot < -1.0f) yRot = 355.0f; 104 glutPostRedisplay(); 105 } 106 } 107 void ProcessMenu(int value) 108 { 109 iMode = value; 110 glutPostRedisplay(); 111 } 112 void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse) 113 { 114 if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) { 115 if(iPointNum == 0 || iPointNum == 2){ 116 iPointNum = 1; 117 x1 = xMouse; y1 = winHeight - yMouse; 118 } 119 else{ 120 iPointNum = 2; 121 x2 = xMouse; y2 = winHeight - yMouse; 122 glutPostRedisplay(); 123 } 124 } 125 if(button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN){ 126 iPointNum = 0; 127 glutPostRedisplay(); 128 } 129 } 130 void main() 131 { 132 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 133 glutInitWindowPosition(100, 100); 134 glutInitWindowSize(400,200); 135 glutCreateWindow("三视图与旋转"); 136 int nMainMenu = glutCreateMenu(ProcessMenu); 137 glutAddMenuEntry("正视图",1); 138 glutAddMenuEntry("俯视图", 2); 139 glutAddMenuEntry("侧视图", 3); 140 glutAttachMenu(GLUT_RIGHT_BUTTON); 141 glutMouseFunc(MousePlot); 142 glutPassiveMotionFunc(PassiveMouseMove); 143 initial(); 144 glutDisplayFunc(Display); 145 glutMainLoop(); 146 147 }