Advertisement
Guest User

Untitled

a guest
May 26th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <GL/glut.h>
  5.  
  6. void drawScene(void),
  7. setMatrix(void),
  8. initLightAndMaterial(void),
  9. animation(void),
  10. resize(int w, int h),
  11. menu(int choice),
  12. keyboard(unsigned char c, int x, int y);
  13.  
  14. bool isLighting = true;
  15.  
  16. float ax, ay, az;
  17. GLUquadricObj *quadObj;
  18. static float lmodel_twoside[] = {GL_TRUE};
  19. static float lmodel_oneside[] = {GL_FALSE};
  20.  
  21. int main (int argc, char **argv)
  22. {
  23. glutInit(&argc, argv);
  24. quadObj = gluNewQuadric();
  25. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  26. glutCreateWindow("Oswietlenie dwustronne");
  27. ax = 10.0;
  28. ay = -10.0;
  29. az = 0.0;
  30.  
  31. initLightAndMaterial();
  32.  
  33. glutDisplayFunc(drawScene);
  34. glutReshapeFunc(resize);
  35. glutCreateMenu(menu);
  36. glutAddMenuEntry("Motion", 3);
  37. glutAddMenuEntry("Two-sided lighting", 1);
  38. glutAddMenuEntry("One-sided lighting", 2);
  39. glutAttachMenu(GLUT_RIGHT_BUTTON);
  40. glutKeyboardFunc(keyboard);
  41. glutMainLoop();
  42. return 0;
  43. }
  44.  
  45. void drawScene(void)
  46. {
  47. glClearColor(0.0, 0.0, 0.0, 0.0);
  48. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  49.  
  50. glPushMatrix();
  51. gluQuadricDrawStyle(quadObj, GLU_FILL);
  52. glColor3f(1.0, 1.0, 1.0);
  53. glRotatef(ax, 1.0, 0.0, 0.0);
  54. glRotatef(-ay, 0.0, 1.0, 0.0);
  55. gluCylinder(quadObj, 10.0, 7.0, 15.0, 45, 9); //
  56. glPopMatrix();
  57. glutSwapBuffers();
  58. }
  59.  
  60. void setMatrix(void)
  61. {
  62. glMatrixMode(GL_PROJECTION);
  63. glLoadIdentity();
  64. glOrtho(-15.0, 15.0, -15.0, 15.0, -10.0, 10.0);
  65. glMatrixMode(GL_MODELVIEW);
  66. glLoadIdentity();
  67. }
  68.  
  69. int count = 0;
  70.  
  71. void animation(void)
  72. {
  73. ax += 5.0;
  74. ay -= 2.0;
  75. az += 5.0;
  76.  
  77. if (ax >= 360)
  78. ax = 0.0;
  79. if (ay <= -360)
  80. ay = 0.0;
  81. if (az >= 360)
  82. az = 0.0;
  83. drawScene();
  84. count++;
  85.  
  86. if (count >= 60)
  87. glutIdleFunc(NULL);
  88. }
  89.  
  90. void keyboard(unsigned char c, int x, int y)
  91. {
  92. switch (c)
  93. {
  94. case 27:
  95. exit(0);
  96. break;
  97. case 108:
  98. if (isLighting)
  99. {
  100. glDisable(GL_LIGHTING);
  101. glDisable(GL_LIGHTING);
  102. }
  103. else
  104. {
  105. glEnable(GL_LIGHTING);
  106. glEnable(GL_LIGHTING);
  107. }
  108.  
  109. isLighting = !isLighting;
  110.  
  111. glEnable(GL_LIGHT0);
  112. default:
  113. break;
  114. }
  115. }
  116.  
  117. void menu(int choice)
  118. {
  119. switch (choice)
  120. {
  121. case 3:
  122. count = 0;
  123. glutIdleFunc(animation);
  124. break;
  125. case 2:
  126. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_oneside);
  127. glutSetWindowTitle("Jednostronne oswietlenie");
  128. glutPostRedisplay();
  129. break;
  130. case 1:
  131. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  132. glutSetWindowTitle("Dwustronne oswietlenie");
  133. glutPostRedisplay();
  134. break;
  135. }
  136. }
  137.  
  138. void resize(int w, int h)
  139. {
  140. glViewport(0, 0, w, h);
  141. setMatrix();
  142. }
  143.  
  144. void initLightAndMaterial(void)
  145. {
  146. static float ambient[] = {0.25, 0.25, 0.25, 1.0};
  147. static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
  148. static float position[] = {90.0, 90.0, 150.0, 0.0};
  149. static float front_mat_shininess[] = {76.8};
  150. static float front_mat_specular[] {0.77, 0.77, 0.77, 1.0};
  151. static float front_mat_diffuse[] {0.4, 0.4, 0.4, 1.0};
  152. static float back_mat_shininess[] = {90.0};
  153. static float back_mat_specular[] = {0.5, 0.5, 0.5, 1.0};
  154. static float back_mat_diffuse[] = {1.0, 0.9, 0.9, 1.0};
  155. static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  156.  
  157. setMatrix();
  158. glEnable(GL_DEPTH_TEST);
  159. glDepthFunc(GL_LEQUAL);
  160. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  161. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  162. glLightfv(GL_LIGHT0, GL_POSITION, position);
  163.  
  164. glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
  165. glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
  166. glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  167. glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
  168. glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
  169. glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  170.  
  171. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  172. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  173.  
  174. glEnable(GL_LIGHTING);
  175. glEnable(GL_LIGHT0);
  176.  
  177. glShadeModel(GL_SMOOTH);
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement