main.cpp
Upload User: guot24
Upload Date: 2022-06-16
Package Size: 20k
Code Size: 7k
Category:

OpenGL program

Development Platform:

C/C++

  1. #include <iostream>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <GLglut.h>
  5. using namespace std;
  6. int Width=800;
  7. int Height=600;
  8. const float Pi=3.1415;
  9. int Mystate=0; 
  10. int oldx=0;
  11. int oldy=0;
  12. float rate=1;
  13. float x1=1,x2=1,x3=1,x4=1;
  14. int color1=0,color2=0,color3=1;
  15. void MyInit()
  16. {
  17. glClear(GL_COLOR_BUFFER_BIT);
  18. glMatrixMode(GL_PROJECTION);
  19. glLoadIdentity();
  20. }
  21. void graph1()
  22. {
  23. /***************** area graph1 ****************************/
  24. //draw the blue line
  25. glLineWidth(1);
  26. glColor3f(0.0,0.0,1.0);
  27. glBegin(GL_LINES);
  28. glVertex2f(-1.0,1.0);
  29. glVertex2f(1.0,-1.0);
  30. glEnd();
  31. //draw the red broken line
  32. glLineWidth(1);
  33. glColor3f(1.0,0.0,0.0);
  34. glEnable(GL_LINE_STIPPLE);
  35. glLineStipple(1, 0x00FF);
  36. glBegin(GL_LINES);
  37. glVertex2f(-1.0,-1.0);
  38. glVertex2f(1.0,1.0);
  39. glEnd();
  40. //draw the yellow strip line
  41. glColor3f(1.0,1.0,0.0);
  42. glLineStipple(1, 0x67E6);
  43. glBegin(GL_LINE_STRIP);
  44. glVertex2f(-1.0,-1.0);
  45. glVertex2f(-0.8,-0.7);
  46. glVertex2f(0.7,0.8);
  47. glVertex2f(1.0,1.0);
  48. glEnd();
  49. }
  50. void graph2()
  51. {
  52. /************ area graph2 ************************/
  53. glDisable(GL_LINE_STIPPLE);
  54. glColor3f(0.0,1.0,0.0);
  55. glBegin(GL_TRIANGLES);
  56. glVertex2f(-0.8,-0.8);
  57. glVertex2f(0.5,0.5);
  58. glVertex2f(-0.2,0.3);
  59. glEnd();
  60. }
  61. void graph3()
  62. {
  63. /*********** area graph3 **********************/
  64. glShadeModel(GL_SMOOTH); 
  65. glBegin(GL_QUADS);
  66. glColor3f(0.9,0.0,0.0);
  67. glVertex2f(-1.0,0);
  68. glColor3f(0.0,0.8,0.0);
  69. glVertex2f(1.0,0.5);
  70. glColor3f(0.9,0.9,0.9);
  71. glVertex2f(0.6,-0.7);
  72. glColor3f(0.0,0.0,0.8);
  73. glVertex2f(-1.0,-0.5);
  74. glEnd();
  75. }
  76. void graph4()
  77. {
  78. /********** area graph4 ***********************/
  79. int blocknum=300;
  80. glColor3f(1,0,0);
  81. glBegin(GL_POLYGON);
  82. for(int i=0;i<blocknum;i++)
  83. {
  84. GLfloat Loc[2];
  85. Loc[0]=0.4*cos(2*Pi*i/blocknum);
  86. Loc[1]=0.5*sin(2*Pi*i/blocknum);
  87. glVertex2fv(Loc);
  88. }
  89. glEnd();
  90. }
  91. void MyDisplay() //绘制要显示的场景
  92. {
  93. if (Mystate==0)
  94. {
  95. glClear(GL_COLOR_BUFFER_BIT);
  96. glViewport( 0, Height/2, Width/2, Height/2 );
  97. glScalef(x1,x1,x1);
  98. graph1();
  99. glViewport( Width/2, Height/2, Width/2, Height/2 );
  100. glScalef(x2/x1,x2/x1,x2/x1);
  101. graph2();
  102. glViewport( 0, 0, Width/2, Height/2 );
  103. glScalef(x3/x2,x3/x2,x3/x2);
  104. graph3();
  105. glViewport( Width/2,0, Width/2, Height/2 );
  106. glScalef(x4/x3,x4/x3,x4/x3);
  107. graph4();
  108. glViewport(0,0,Width,Height);
  109. glFlush();
  110. }
  111. else if (Mystate==1)
  112. {
  113. glViewport(0,0,Width,Height);
  114. glClear(GL_COLOR_BUFFER_BIT);
  115. glScalef(x1,x1,x1);
  116. graph1();
  117. glFlush();
  118. }
  119. else if (Mystate==2)
  120. {
  121. glViewport(0,0,Width,Height);
  122. glClear(GL_COLOR_BUFFER_BIT);
  123. glScalef(x2,x2,x2);
  124. graph2();
  125. glFlush();
  126. }
  127. else if (Mystate==3)
  128. {
  129. glViewport(0,0,Width,Height);
  130. glClear(GL_COLOR_BUFFER_BIT);
  131. glScalef(x3,x3,x3);
  132. graph3();
  133. glFlush();
  134. }
  135. else 
  136. {
  137. glViewport(0,0,Width,Height);
  138. glClear(GL_COLOR_BUFFER_BIT);
  139. glScalef(x4,x4,x4);
  140. graph4();
  141. glFlush();
  142. }
  143. }
  144. void MyReshape(GLint cx,GLint cy) //窗口大小发生变化时的操作,cx为窗口宽,cy为窗口高
  145. {
  146. // 防止除数即高度为0 
  147. // (你可以设置窗口宽度为0). 
  148. if(cy == 0) 
  149. cy = 1; 
  150. Width=cx;
  151. Height=cy;
  152. }
  153. void MyMouse(GLint button,GLint state,GLint cx,GLint cy) //鼠标点击操作
  154. {
  155. //button为鼠标键,有左键GLUT_LEFT_BUTTON,右键GLUT_RIGHT_BUTTON,滚轮向上GLUT_WHEEL_UP,滚轮向下GLUT_WHEEL_DOWN
  156. //state为鼠标按键状态,有按下GLUT_DOWN,弹起GLUT_UP
  157. //cx,cy为鼠标当前坐标
  158. //对于滚轮的操作,需要较新的GLUT版本才能支持,可以使用主页上的glut2004
  159. //对滚轮的判定,使用state == GLUT_UP && button == GLUT_WHEEL_UP来判断
  160. if (button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
  161. {
  162. glLoadIdentity(); 
  163. rate=1;
  164. if (Mystate==0)
  165. {
  166. if (cx<=Width/2&&cy<=Height/2)
  167. {
  168. Mystate=1;
  169. glClear(GL_COLOR_BUFFER_BIT);
  170. glScalef(x1,x1,x1);
  171. graph1();
  172. glFlush();
  173. }
  174. else if (cx>Width/2&&cy<=Height/2)
  175. {
  176. Mystate=2;
  177. glClear(GL_COLOR_BUFFER_BIT);
  178. glScalef(x2,x2,x2);
  179. graph2();
  180. glFlush();
  181. }
  182. else if (cx<=Width/2&&cy>Height/2)
  183. {
  184. Mystate=3;
  185. glClear(GL_COLOR_BUFFER_BIT);
  186. glScalef(x3,x3,x3);
  187. graph3();
  188. glFlush();
  189. }
  190. else 
  191. {
  192. Mystate=4;
  193. glClear(GL_COLOR_BUFFER_BIT);
  194. glScalef(x4,x4,x4);
  195. graph4();
  196. glFlush();
  197. }
  198. }
  199. else
  200. {
  201. Mystate=0;
  202. glClear(GL_COLOR_BUFFER_BIT);
  203. MyDisplay();
  204. }
  205. }
  206. else if (button==GLUT_WHEEL_UP&&state==GLUT_UP&&Mystate>=1)
  207. {
  208. if (Mystate==1)
  209. {
  210. rate*=1.1;
  211. glScalef(1.1,1.1,1.1);
  212. x1*=1.1;
  213. glClear(GL_COLOR_BUFFER_BIT);
  214. graph1();
  215. glFlush();
  216. }
  217. else if (Mystate==2)
  218. {
  219. rate*=1.1;
  220. glScalef(1.1,1.1,1.1);
  221. x2*=1.1;
  222. glClear(GL_COLOR_BUFFER_BIT);
  223. graph2();
  224. glFlush();
  225. }
  226. else if (Mystate==3)
  227. {
  228. rate*=1.1;
  229. glScalef(1.1,1.1,1.1);
  230. x3*=1.1;
  231. glClear(GL_COLOR_BUFFER_BIT);
  232. graph3();
  233. glFlush();
  234. }
  235. else 
  236. {
  237. rate*=1.1;
  238. x4*=1.1;
  239. glScalef(1.1,1.1,1.1);
  240. glClear(GL_COLOR_BUFFER_BIT);
  241. graph4();
  242. glFlush();
  243. }
  244. }
  245. else if (button==GLUT_WHEEL_DOWN&&state==GLUT_UP&&Mystate>=1)
  246. {
  247. if (Mystate==1)
  248. {
  249. glScalef(0.9,0.9,0.1);
  250. rate*=0.9;
  251. x1*=0.9;
  252. glClear(GL_COLOR_BUFFER_BIT);
  253. graph1();
  254. glFlush();
  255. }
  256. else if (Mystate==2)
  257. {
  258. glScalef(0.9,0.9,0.1);
  259. rate*=0.9;
  260. x2*=0.9;
  261. glClear(GL_COLOR_BUFFER_BIT);
  262. graph2();
  263. glFlush();
  264. }
  265. else if (Mystate==3)
  266. {
  267. glScalef(0.9,0.9,0.1);
  268. rate*=0.9;
  269. x3*=0.9;
  270. glClear(GL_COLOR_BUFFER_BIT);
  271. graph3();
  272. glFlush();
  273. }
  274. else 
  275. {
  276. glScalef(0.9,0.9,0.1);
  277. rate*=0.9;
  278. x4*=0.9;
  279. glClear(GL_COLOR_BUFFER_BIT);
  280. graph4();
  281. glFlush();
  282. }
  283. }
  284. else if (button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
  285. {
  286. oldx=cx;
  287. oldy=cy;
  288. }
  289. }
  290. void MyMotion(GLint cx,GLint cy) //处理鼠标移动时的操作,cx,cy为鼠标当前位置
  291. {
  292. cout << cx <<" "<<cy<<endl;
  293. if (Mystate>=1)
  294. {
  295. if (Mystate==1)
  296. {
  297. glTranslatef(2.0*(cx-oldx)/Width/rate,2.0*(oldy-cy)/Height/rate,0);
  298. oldx=cx;
  299. oldy=cy;
  300. glClear(GL_COLOR_BUFFER_BIT);
  301. graph1();
  302. glFlush();
  303. }
  304. else if (Mystate==2)
  305. {
  306. glTranslatef(2.0*(cx-oldx)/Width/rate,2.0*(oldy-cy)/Height/rate,0);
  307. oldx=cx;
  308. oldy=cy;
  309. glClear(GL_COLOR_BUFFER_BIT);
  310. graph2();
  311. glFlush();
  312. }
  313. else if (Mystate==3)
  314. {
  315. glTranslatef(2.0*(cx-oldx)/Width/rate,2.0*(oldy-cy)/Height/rate,0);
  316. oldx=cx;
  317. oldy=cy;
  318. glClear(GL_COLOR_BUFFER_BIT);
  319. graph3();
  320. glFlush();
  321. }
  322. else 
  323. {
  324. glTranslatef(2.0*(cx-oldx)/Width/rate,2.0*(oldy-cy)/Height/rate,0);
  325. oldx=cx;
  326. oldy=cy;
  327. glClear(GL_COLOR_BUFFER_BIT);
  328. graph4();
  329. glFlush();
  330. }
  331. }
  332. }
  333. int main(int argc,char **argv)
  334. {
  335. glutInit(&argc,argv);
  336. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  337. glutInitWindowSize(Width,Height);
  338. glutCreateWindow("rjy_Exp1");
  339. MyInit(); //自定义OpenGL初始化函数
  340. glutDisplayFunc(MyDisplay); //显示
  341. glutReshapeFunc(MyReshape); //窗口改变
  342. glutMouseFunc(MyMouse); //鼠标点击
  343. glutMotionFunc(MyMotion); //鼠标移动
  344. glutMainLoop();
  345. return 0;
  346. }