Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.10 KB | None | 0 0
  1. /**********************************
  2.  
  3. The reader of this program should not limit
  4. himself/herself to the comments of this
  5. program.
  6.  
  7. If one wants to read more about the syntax and
  8. the semantics of OpenGL functions used in this
  9. program, one should read the beginning of the
  10. paragraph 2.6 "Begin/End Paradigm", the
  11. parag raph 2.6.1 "Begin and End Objects" from the
  12. file glspec15.pdf at page 25/333 and the index
  13. from the end of that file. One could also
  14. read the references to the GLUT functions from
  15. the file glut-3.spec.pdf.
  16.  
  17.  
  18.  
  19. H O W T O R E A D T H I S P R O G R A M ?
  20.  
  21. Start from the function "main" and follow the
  22. instruction flow, paying attention to the fact that
  23. this program belongs to the event-driven programming
  24. paradigm. So pay attention to what happens when
  25. the user presses a key, moves the mouse or presses a
  26. mouse button. There are also some special events: the
  27. redrawing of the application window, etc.
  28. Identify what happens in the program when one of these
  29. events occurs.
  30.  
  31. **********************************/
  32.  
  33.  
  34.  
  35.  
  36. /**********************************
  37.  
  38. If you want to use the GLUT utility toolkit
  39. you must include the header file <GL/glut.h> .
  40.  
  41. This header file will include automatically
  42. the header files needed in order to use
  43. OpenGL library : <GL/gl.h> and <GL/glu.h> .
  44. You don't need to include these two header files
  45. in your program.
  46.  
  47. The OpenGL library functions are prefixed by gl,
  48. those in GLU by glu and those in GLUT by glut
  49.  
  50. In the following it is desirable that the
  51. students will use this program like a framework
  52. (or like a "skeleton") for their future programs
  53. at Computer Graphics course.
  54.  
  55. **********************************/
  56.  
  57.  
  58. // Pay attention to the order of include-file directives !
  59. // That's important, in order to avoid some compile-time errors.
  60. #include <stdlib.h>
  61. #include <stdio.h>
  62. #include <math.h>
  63. /**********************************
  64. This include-file directive will be used only if
  65. you have installed OpenGL library as an Administrator
  66. i.e., you have the necessary rights to copy the files
  67. glut.h, glut32.lib, glut32.dll in the following locations:
  68. C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\gl\glut.h
  69. C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib\glut32.lib
  70. C:\Windows\System32\glut32.dll
  71. **********************************/
  72. #include <GL/glut.h>
  73. // Instead of previous include-file directive use this one.
  74. //#include <GL/freeglut.h>
  75.  
  76. // This variable stores the code of the last key
  77. // of the computer keyboard pressed. This code
  78. // will be used later in the program to select which
  79. // image to draw.
  80. unsigned char prevKey;
  81.  
  82. void Display1() {
  83. // The geometry object in this case is a line segment.
  84. // In order to completely specify a line segment one
  85. // should specify two different points.
  86.  
  87. // A point is specified completely by providing
  88. // two (or three in 3D case) coordinates: for x, y
  89. // (and z).
  90. glColor3f(0.2,0.15,0.88); // blue
  91. // A line segment is drawn between the points
  92. // specified by the vertices
  93. glBegin(GL_LINES); // line drawing
  94. glVertex2i(1,1); // vertice coordinates
  95. glVertex2i(-1,-1);
  96. glEnd();
  97.  
  98. glColor3f(1,0.1,0.1); // red
  99. // A line segment is drawn between the points
  100. // specified by the vertices
  101. glBegin(GL_LINES);
  102. glVertex2i(-1,1);
  103. glVertex2i(1,-1);
  104. glEnd();
  105.  
  106. // A line segment is drawn between the points
  107. // specified by the vertices
  108. glBegin(GL_LINES);
  109. glVertex2d(-0.5,0);
  110. glVertex2d(0.5,0);
  111. glEnd();
  112. }
  113.  
  114. void Display2() {
  115. glColor3f(1,0.1,0.1); // red
  116. // In this case, by using GL_LINES begin/end paradigm
  117. // several line segment are drawn, between the 1st and
  118. // the 2nd point, between the 3rd and the 4th, etc.
  119. glBegin(GL_LINES);
  120. glVertex2f(1.0,1.0);
  121. glVertex2f(0.9,0.9);
  122. glVertex2f(0.8,0.8);
  123. glVertex2f(0.7,0.7);
  124. glVertex2f(0.6,0.6);
  125. glVertex2f(0.5,0.5);
  126. glVertex2f(-0.5,-0.5);
  127. glVertex2f(-1.0,-1.0);
  128. glEnd();
  129. }
  130.  
  131. void Display3() {
  132. // The geometry object in this case is a point.
  133.  
  134. // By using GL_POINTS begin/end paradigm
  135. // it is possible to draw as many points as there
  136. // are specified between glBegin and glEnd.
  137.  
  138. glColor3f(1,0.1,0.1); // red
  139. glBegin(GL_POINTS);
  140. glVertex2f(-0.5, -0.5);
  141. glVertex2f(-0.5, 0.5);
  142. glVertex2f(0.5, -0.5);
  143. glVertex2f(0.5, 0.5);
  144. // add lines of code here
  145. glEnd();
  146. }
  147.  
  148. void Display4() {
  149. // The geometry object in this case is a polygonal line.
  150.  
  151. // By using GL_LINE_STRIP begin/end paradigm
  152. // it is possible to draw a polygonal line having as many
  153. // points as there are specified between glBegin and glEnd.
  154.  
  155. glColor3f(1,0.1,0.1); // red
  156. glBegin(GL_LINE_STRIP);
  157. // add lines of code here
  158. glVertex2f(1.0, 1.0);
  159. glVertex2f(1.0, 0.8);
  160. glVertex2f(0.5, 0.5);
  161. glVertex2f(0.5, 0.3);
  162. glEnd();
  163. }
  164.  
  165. void Display5() {
  166. // The geometry object in this case is a closed polygonal line.
  167.  
  168. // By using GL_LINE_LOOP begin/end paradigm
  169. // it is possible to draw a closed polygonal line having as many
  170. // points as there are specified between glBegin and glEnd.
  171.  
  172. glColor3f(1,0.1,0.1); // red
  173. glBegin(GL_LINE_LOOP);
  174. glVertex2f(1.0, 1.0);
  175. glVertex2f(1.0, 0.8);
  176. glVertex2f(0.5, 0.5);
  177. glVertex2f(0.5, 0.3);
  178. // add lines of code here
  179. glEnd();
  180. }
  181.  
  182. void Display6() {
  183. // The geometry object in this case is a triangle.
  184.  
  185. // By using GL_TRIANGLES begin/end paradigm
  186. // it is possible to draw triangles having the vertices
  187. // (v0,v1,v2), (v3,v4,v5), ...
  188. // There should be specified at least 3 vertices between
  189. // glBegin and glEnd.
  190.  
  191. glColor3f(1,0.1,0.1); // red
  192. glBegin(GL_TRIANGLES);
  193. // add lines of code here
  194. glVertex2f(1.0, 1.0);
  195. glVertex2f(1.0, 0.8);
  196. glVertex2f(0.8, 0.8);
  197.  
  198. glVertex2f(-1.0, -1.0);
  199. glVertex2f(-1.0, -0.8);
  200. glVertex2f(-0.8, -0.8);
  201. glEnd();
  202. }
  203.  
  204. void Display7() {
  205. // The geometry object in this case is a quadrilateral
  206. // (i.e., a polygon with 4 vertex).
  207.  
  208. // By using GL_QUADS begin/end paradigm
  209. // it is possible to draw quadrilaterals having the vertices
  210. // (v0,v1,v2,v3), (v4,v5,v6,v7), ...
  211. // There should be specified at least 4 vertices between
  212. // glBegin and glEnd.
  213.  
  214. glColor3f(1,0.1,0.1); // red
  215. glBegin(GL_QUADS);
  216. glVertex2f(1.0, 1.0);
  217. glVertex2f(1.0, 0.5);
  218. glVertex2f(0.5, 0.5);
  219. glVertex2f(0.3, 0.7);
  220. glVertex2f(0.3, 1.0);
  221. // add lines of code here
  222. glEnd();
  223. }
  224.  
  225. void Display8() {
  226. // The geometry object in this case is a polygon.
  227.  
  228. // By using GL_POLYGON begin/end paradigm
  229. // it is possible to draw a polygon having as many vertices
  230. // as there are specified between glBegin and glEnd.
  231. // OpenGL guarantees that only convex polygons are
  232. // drawn correctly.
  233.  
  234. //glColor3f(1,0.1,0.1); // red
  235. glColor3f(0.2, 0.15, 0.88); // blue
  236. glBegin(GL_POLYGON);
  237. glVertex2f(0.3, 0.3);
  238. glVertex2f(0.5, 0.0);
  239. glVertex2f(0.3, -0.3);
  240. glVertex2f(-0.3, -0.3);
  241. glVertex2f(-0.5, 0.0);
  242. glVertex2f(-0.3, 0.3);
  243.  
  244.  
  245. // add lines of code here
  246. glEnd();
  247. }
  248.  
  249. void Init(void) {
  250. // The function glClearColor specifies a clear value
  251. // for the color buffers in RGBA mode (i.e., its
  252. // color after its content was deleted by a
  253. // glClear function call). The first three parameters
  254. // specifiy the amounts of Red, Green and Blue colors
  255. // and the last parameter (alpha) specifies the opacity:
  256. // 1 = full opacity, 0 = no opacity
  257. // The part of the OpenGL state modified by this call
  258. // remains as it is until a new call may changes it.
  259. // That's true for all OpenGL functions.
  260. glClearColor(1.0,1.0,1.0,1.0);
  261.  
  262. // the name is self-explanatory
  263. glLineWidth(3);
  264.  
  265. // the name is self-explanatory
  266. glPointSize(4);
  267.  
  268. // The function glPolygonMode with the parameters
  269. // "GLenum face" and "GLenum mode" controls how
  270. // a polygon is drawn.
  271. // The parameter "mode" can have the values
  272. // GL_POINT (meaning that only the vertices of
  273. // an object are drawn), GL_LINE (meaning that only
  274. // the edges of an object are drawn) and GL_FILL
  275. // (meaning that the polygon is uniformly colored).
  276. // The parameter "face" specifies how a face of an
  277. // object is oriented. It can have the values
  278. // GL_FRONT (meaning that the face is oriented forward),
  279. // GL_BACK (meaning that the face is oriented backward)
  280. // GL_FRONT_AND_BACK (the face is not oriented)
  281. glPolygonMode(GL_FRONT, GL_LINE);
  282. }
  283.  
  284. void Display(void) {
  285. printf("Call Display\n");
  286.  
  287. // The buffer GL_COLOR_BUFFER_BIT is cleared
  288. glClear(GL_COLOR_BUFFER_BIT);
  289.  
  290. switch(prevKey) {
  291. case '1':
  292. Display1();
  293. break;
  294. case '2':
  295. Display2();
  296. break;
  297. case '3':
  298. Display3();
  299. break;
  300. case '4':
  301. Display4();
  302. break;
  303. case '5':
  304. Display5();
  305. break;
  306. case '6':
  307. Display6();
  308. break;
  309. case '7':
  310. Display7();
  311. break;
  312. case '8':
  313. Display8();
  314. break;
  315. default:
  316. break;
  317. }
  318.  
  319. // practically, this call forces the redrawing of
  320. // the application window
  321. glFlush();
  322. }
  323.  
  324. // The parameters w(idth) and h(eight) specify the new
  325. // dimensions of the application window
  326. void Reshape(int w, int h) {
  327. printf("Call Reshape : width = %d, height = %d\n", w, h);
  328.  
  329. // The function void glViewport (GLint x, GLint y,
  330. // GLsizei width, GLsizei height)
  331. // defines the viewport : a rectangular area inside the
  332. // application window used for displaying by OpenGL.
  333. // x, y are the coordinates of the left down corner and
  334. // width and height are expressed in pixels.
  335. // In this case the viewport has the same dimensions as
  336. // the application window.
  337. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  338. }
  339.  
  340. // The key parameter indicates the key code and x, y
  341. // the position of the mouse cursor
  342. void KeyboardFunc(unsigned char key, int x, int y) {
  343. printf("You have pressed <%c>. The mouse has the position %d, %d.\n",
  344. key, x, y);
  345. // The value of variable key is stored in the
  346. // variable prevKey and thus it will be used in the
  347. // Display function to select which images to display
  348. prevKey = key;
  349. if (key == 27) // escape
  350. exit(0);
  351. glutPostRedisplay();
  352. }
  353.  
  354. // The values of the parameter button could be
  355. // GLUT_LEFT_BUTTON or GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON.
  356. // The parameter state indicates the state of the mouse
  357. // button: GLUT_DOWN (the button is pressed) and GLUT_UP (the
  358. // button is released).
  359. // The parameters x and y specifies the coordinates of the
  360. // mouse cursor.
  361. void MouseFunc(int button, int state, int x, int y) {
  362. printf("Call MouseFunc : you have %s the %s button in the position %d %d\n",
  363. (state == GLUT_DOWN) ? "pressed" : "released",
  364. (button == GLUT_LEFT_BUTTON) ?
  365. "left" :
  366. ((button == GLUT_RIGHT_BUTTON) ? "right": "middle"),
  367. x, y);
  368. }
  369.  
  370. int main(int argc, char** argv) {
  371. // The function glutInit initializes the OpenGL library.
  372. // The parameters argc and argv are those of the command
  373. // line and should not be modified before this function
  374. // call.
  375. // All the calls of other OpenGL functions should be
  376. // made after this function call.
  377. glutInit(&argc, argv);
  378.  
  379. // The parameters of this function represent the
  380. // width and the height of the application window
  381. // expressed in pixels. The default values are
  382. // 300, 300.
  383. glutInitWindowSize(300, 300);
  384.  
  385. // The parameters of this function represent the
  386. // coordinates of the left up corner of the application
  387. // window, expressed in pixels. The default values
  388. // are -1, -1.
  389. glutInitWindowPosition(100, 100);
  390.  
  391. // This function specifies, by a bitwise OR, the set of buffers
  392. // which could be used in the program. The buffers are
  393. // GLUT_SINGLE (one image buffer), GLUT_DOUBLE (2 image buffers),
  394. // GLUT_RGB or GLUT_RGBA (a RGB color buffer), etc. (see the
  395. // OpenGL specification for more information).
  396. // In this case it will be available one colored buffer.
  397. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  398.  
  399. // This function creates a window having the name given
  400. // by the parameter and returns a window handle.
  401. glutCreateWindow (argv[0]);
  402.  
  403. // This is a call to a user-defined function.
  404. // See above.
  405. Init();
  406.  
  407. // In the following there are used some callback
  408. // functions. These functions are defined in the
  409. // program (see above) and registered in OpenGL
  410. // by using some GLUT functions. These callback
  411. // functions are called by the operating system
  412. // when a certain event appears (e.g., a key is
  413. // pressed when the application window has the
  414. // focus).
  415.  
  416. // The function
  417. // void glutReshapeFunc (void (*Reshape)(int width, int height))
  418. // registers the callback function Reshape which is called
  419. // whenever the application window changes its shape.
  420. glutReshapeFunc(Reshape);
  421.  
  422. // The function
  423. // void glutKeyboardFunc (void (*KeyboardFunc)(unsigned char,int,int))
  424. // registers the callback function KeyboardFunc which is called
  425. // whenever a key is pressed.
  426. glutKeyboardFunc(KeyboardFunc);
  427.  
  428. // The function
  429. // void glutMouseFunc (void (*MouseFunc)(int,int,int,int))
  430. // registers the callback function MouseFunc which is called
  431. // whenever a mouse button is pressed or released.
  432. glutMouseFunc(MouseFunc);
  433.  
  434. // The function
  435. // void glutDisplayFunc (void (*Display)(void))
  436. // registers the callback function Display which is called
  437. // whenever the application window needs to be redrawn:
  438. // at its initialization, when its dimensions change or
  439. // the function glutPostRedisplay is called.
  440. glutDisplayFunc(Display);
  441.  
  442. // The function glutMainLoop launches the GLUT events processing
  443. // loop. This loop ends only when the application window is
  444. // closed.
  445. // This function should be called only once in a program.
  446. // The callback functions must be registered prior to this call.
  447. // When the event loop is empty, the callback function IdleFunc
  448. // ( registered by calling the function
  449. // void glutIdleFunc (void (*IdleFunc) (void))
  450. // ) is executed.
  451. glutMainLoop();
  452.  
  453. return 0;
  454. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement