Advertisement
kanciastopantalones

Untitled

Mar 16th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. #include <iostream>
  2. #include "zpr.h"
  3. #include "GL/glut.h"
  4. #include <osg/Matrix>
  5. #include <osg/Vec3>
  6. #include <osg/Quat>
  7.  
  8. //! Macierz opisująca czajnik
  9. osg::Matrix teapotMatrix;
  10. //! Macierz opisująca torus
  11. osg::Matrix torusMatrix;
  12. //! Macierz opisująca sześcian
  13. osg::Matrix cubeMatrix;
  14.  
  15. //! TODO
  16. //! Tutaj inicjalizowane są pierwsze pozycje obiektów
  17. void initObjects()
  18. {
  19. teapotMatrix = osg::Matrix::translate(1,1,1);
  20. torusMatrix = osg::Matrix::translate(1,0,0);
  21. cubeMatrix = osg::Matrix::translate(1,0,1);
  22. }
  23.  
  24. //! TODO
  25. //! Tutaj aktualizaowana jest pozycja czajnika
  26. void updateTeapot()
  27. {
  28. auto pos=cubeMatrix.getTrans();
  29. cubeMatrix*=osg::Matrix::translate(pos);
  30. cubeMatrix*=osg::Matrix::scale(2,2,2);
  31.  
  32. }
  33.  
  34. //! TODO
  35. //! Tutaj aktualizowana jest pozycja torusa
  36. void updateTorus()
  37. {
  38.  
  39. }
  40.  
  41. //! TODO
  42. //! Tutaj aktualizowana jest pozycja sześcianu
  43. void updateCube()
  44. {
  45.  
  46. }
  47.  
  48. //! \param objectMatrix Macierz pisująca pozycję 3D obiektu na scenie
  49. void refreshObject(const osg::Matrix & objectMatrix)
  50. {
  51. auto t = objectMatrix.getTrans();
  52. glTranslated(t.x(), t.y(), t.z());
  53.  
  54. auto s = objectMatrix.getScale();
  55. glScaled(s.x(), s.y(), s.z());
  56.  
  57. double angle, x, y, z;
  58. objectMatrix.getRotate().getRotate(angle, x, y, z);
  59. glRotated(osg::RadiansToDegrees(angle), x, y, z);
  60. }
  61.  
  62. //! Metoda odrysowywuje osie układu
  63. void drawAxes()
  64. {
  65. /* Name-stack manipulation for the purpose of
  66. selection hit processing when mouse button
  67. is pressed. Names are ignored in normal
  68. OpenGL rendering mode. */
  69.  
  70. glPushMatrix();
  71. /* No name for grey sphere */
  72.  
  73. glColor3f(0.3,0.3,0.3);
  74. glutSolidSphere(0.07, 20, 20);
  75.  
  76. glPushMatrix();
  77. glPushName(1); /* Red cone is 1 */
  78. glColor3f(1,0,0);
  79. glRotatef(90,0,1,0);
  80. glutSolidCone(0.06, 0.4, 20, 20);
  81. glPopName();
  82. glPopMatrix();
  83.  
  84. glPushMatrix ();
  85. glPushName(2); /* Green cone is 2 */
  86. glColor3f(0,1,0);
  87. glRotatef(-90,1,0,0);
  88. glutSolidCone(0.06, 0.4, 20, 20);
  89. glPopName();
  90. glPopMatrix();
  91.  
  92. glColor3f(0,0,1); /* Blue cone is 3 */
  93. glPushName(3);
  94. glutSolidCone(0.06, 0.4, 20, 20);
  95. glPopName();
  96.  
  97. glPopMatrix();
  98. }
  99.  
  100. // Drawing (display) routine.
  101. void drawScene()
  102. {
  103. // Clear screen to background color.
  104. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  105.  
  106. // Pudełko
  107. glPushMatrix();
  108. glColor4f(1,0,0, 0.9);
  109. refreshObject(cubeMatrix);
  110. glutSolidCube(0.4);
  111. glPopMatrix();
  112.  
  113. // Torus
  114. glPushMatrix();
  115. glColor4f(1,0,0, 0.9);
  116. refreshObject(torusMatrix);
  117. glutSolidTorus(0.05, 0.4, 10, 10);
  118. glPopMatrix();
  119.  
  120. // Czajnik
  121. glPushMatrix();
  122. glColor4f(0,1,0, 0.9);
  123. refreshObject(teapotMatrix);
  124. glutSolidTeapot(0.2);
  125. glPopMatrix();
  126.  
  127. // Draw orientation axis
  128. drawAxes();
  129.  
  130. // Swap buffers for double buffering
  131. glutSwapBuffers();
  132. }
  133.  
  134. //! Metoda realizująca obliczenia na potrzeby kolejnych klatek, generuje animację
  135. void animate() {
  136.  
  137. updateTeapot();
  138. updateTorus();
  139. updateCube();
  140.  
  141. glutPostRedisplay();
  142. }
  143. //! Zmienne opisujące materiał i światło OpenGL
  144. const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  145. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  146. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  147. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  148.  
  149. const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  150. const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  151. const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  152. const GLfloat high_shininess[] = { 100.0f };
  153.  
  154. // Initialization routine.
  155. void setup()
  156. {
  157. glEnable (GL_BLEND);
  158. glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  159. glClearColor(0.5, 0.5, 0.5, 0.5);
  160. glEnable(GL_CULL_FACE);
  161. glCullFace(GL_BACK);
  162.  
  163. glEnable(GL_DEPTH_TEST);
  164. glDepthFunc(GL_LESS);
  165.  
  166. glEnable(GL_LIGHT0);
  167. glEnable(GL_NORMALIZE);
  168. glEnable(GL_COLOR_MATERIAL);
  169. glEnable(GL_LIGHTING);
  170.  
  171. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  172. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  173. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  174. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  175.  
  176. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  177. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  178. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  179. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  180.  
  181. // Register display routine.
  182. glutDisplayFunc(drawScene);
  183. // Register idle routine
  184. glutIdleFunc(animate);
  185. // Initialize camera manipulator
  186. zprInit();
  187. // Initialize first object positions
  188. initObjects();
  189. }
  190.  
  191. // Main routine: defines window properties, creates window,
  192. // registers callback routines and begins processing.
  193. int main(int argc, char **argv)
  194. {
  195. // Initialize GLUT.
  196. glutInit(&argc, argv);
  197.  
  198. // Set display mode as double-buffered, RGB color and depth.
  199. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  200.  
  201. // Set OpenGL window size.
  202. glutInitWindowSize(800, 800);
  203.  
  204. // Set position of OpenGL window upper-left corner.
  205. glutInitWindowPosition(50, 50);
  206.  
  207. // Create OpenGL window with title.
  208. glutCreateWindow("Laboratorium GK: AFI");
  209.  
  210. // Initialize.
  211. setup();
  212.  
  213. // Begin processing.
  214. glutMainLoop();
  215.  
  216. return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement