kanciastopantalones

to

Mar 16th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 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(0.5, 1, -1);
  21. cubeMatrix = osg::Matrix::translate(0.5, 1, 1);
  22. }
  23.  
  24. //! TODO
  25. //! Tutaj aktualizaowana jest pozycja czajnika
  26. void updateTeapot()
  27. {
  28. osg::Vec3d pos = teapotMatrix.getTrans();
  29. static osg::Vec3d p1 = cubeMatrix.getTrans(); //punkt, wzgledem ktorego bedzie tworzona pierwsza czesc osemki
  30. static osg::Vec3d p2(1.5, 1, 1); //punkt, wzgledem ktorego bedzie tworzona pierwsza czesc osemki
  31. static int step = 0;
  32.  
  33. if(step < 40)
  34. {
  35. teapotMatrix *= osg::Matrix::translate(-p1);
  36. teapotMatrix *= osg::Matrix::rotate(osg::DegreesToRadians(9.f), osg::Vec3d(0, 0, 1));
  37. teapotMatrix *= osg::Matrix::translate(p1);
  38. }
  39. else
  40. {
  41. teapotMatrix *= osg::Matrix::translate(-p2);
  42. teapotMatrix *= osg::Matrix::rotate(osg::DegreesToRadians(-9.f), osg::Vec3d(0, 0, 1));
  43. teapotMatrix *= osg::Matrix::translate(p2);
  44. }
  45.  
  46. step = (step + 1) % 80;
  47. }
  48.  
  49. //! TODO
  50. //! Tutaj aktualizowana jest pozycja torusa
  51. void updateTorus()
  52. {
  53. osg::Vec3d pos = torusMatrix.getTrans();
  54. osg::Vec3d point = cubeMatrix.getTrans(); // punkt odniesienia
  55. static double angle = 270; // w stopniach
  56. osg::Vec3d angleVec; //modyfikator zmieniajacy promien okregu (tworzac efekt elipsy)
  57. const double step = 1.8; //rozmiar kroku
  58.  
  59. angleVec = osg::Vec3d(cos(osg::DegreesToRadians(angle)), 0, 0);
  60.  
  61. //obrot wokol wlasnej osi
  62. torusMatrix *= osg::Matrix::translate(-pos);
  63. torusMatrix *= osg::Matrix::rotate(osg::DegreesToRadians(step), osg::Vec3d(0, 0, 1));
  64. torusMatrix *= osg::Matrix::translate(pos);
  65.  
  66.  
  67. //obrot wokol punktu
  68. torusMatrix *= osg::Matrix::translate(-point - angleVec); //-angleVec - usun modyfikacje na czas obracania
  69. torusMatrix *= osg::Matrix::rotate(osg::DegreesToRadians(step), osg::Vec3d(0, 1, 0));
  70. torusMatrix *= osg::Matrix::translate(point); //nie przywracaj angleVec. zaraz zostanie dodana zaktualizowana wartosc
  71.  
  72. //zmiana promienia
  73. angle = fmod(angle + step, 360);
  74. angleVec = osg::Vec3d(cos(osg::DegreesToRadians(angle)), 0, 0);
  75. torusMatrix *= osg::Matrix::translate(angleVec);
  76. //voila!
  77. }
  78.  
  79. //! TODO
  80. //! Tutaj aktualizowana jest pozycja sześcianu
  81. void updateCube()
  82. {
  83. const float scaleStep = 1.01;
  84. static int step = 50;
  85. osg::Vec3d pos = cubeMatrix.getTrans();
  86.  
  87. cubeMatrix *= osg::Matrix::translate(-pos);
  88.  
  89. if(step >= 50)
  90. cubeMatrix *= osg::Matrix::scale(scaleStep, scaleStep, scaleStep);
  91. else
  92. cubeMatrix *= osg::Matrix::scale(1.0/scaleStep, 1.0/scaleStep, 1.0/scaleStep);
  93.  
  94. step = (step + 1) % 100;
  95.  
  96. cubeMatrix *= osg::Matrix::translate(pos);
  97. }
  98.  
  99. //! \param objectMatrix Macierz pisująca pozycję 3D obiektu na scenie
  100. void refreshObject(const osg::Matrix & objectMatrix)
  101. {
  102. auto t = objectMatrix.getTrans();
  103. glTranslated(t.x(), t.y(), t.z());
  104.  
  105. auto s = objectMatrix.getScale();
  106. glScaled(s.x(), s.y(), s.z());
  107.  
  108. double angle, x, y, z;
  109. objectMatrix.getRotate().getRotate(angle, x, y, z);
  110. glRotated(osg::RadiansToDegrees(angle), x, y, z);
  111. }
  112.  
  113. //! Metoda odrysowywuje osie układu
  114. void drawAxes()
  115. {
  116. /* Name-stack manipulation for the purpose of
  117. selection hit processing when mouse button
  118. is pressed. Names are ignored in normal
  119. OpenGL rendering mode. */
  120.  
  121. glPushMatrix();
  122. /* No name for grey sphere */
  123.  
  124. glColor3f(0.3,0.3,0.3);
  125. glutSolidSphere(0.07, 20, 20);
  126.  
  127. glPushMatrix();
  128. glPushName(1); /* Red cone is 1 */
  129. glColor3f(1,0,0);
  130. glRotatef(90,0,1,0);
  131. glutSolidCone(0.06, 0.4, 20, 20);
  132. glPopName();
  133. glPopMatrix();
  134.  
  135. glPushMatrix ();
  136. glPushName(2); /* Green cone is 2 */
  137. glColor3f(0,1,0);
  138. glRotatef(-90,1,0,0);
  139. glutSolidCone(0.06, 0.4, 20, 20);
  140. glPopName();
  141. glPopMatrix();
  142.  
  143. glColor3f(0,0,1); /* Blue cone is 3 */
  144. glPushName(3);
  145. glutSolidCone(0.06, 0.4, 20, 20);
  146. glPopName();
  147.  
  148. glPopMatrix();
  149. }
  150.  
  151. // Drawing (display) routine.
  152. void drawScene()
  153. {
  154. // Clear screen to background color.
  155. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  156.  
  157. // Pudełko
  158. glPushMatrix();
  159. glColor4f(1,0,0, 0.9);
  160. refreshObject(cubeMatrix);
  161. glutSolidCube(0.4);
  162. glPopMatrix();
  163.  
  164. // Torus
  165. glPushMatrix();
  166. glColor4f(1,0,0, 0.9);
  167. refreshObject(torusMatrix);
  168. glutSolidTorus(0.05, 0.4, 10, 10);
  169. glPopMatrix();
  170.  
  171. // Czajnik
  172. glPushMatrix();
  173. glColor4f(0,1,0, 0.9);
  174. refreshObject(teapotMatrix);
  175. glutSolidTeapot(0.2);
  176. glPopMatrix();
  177.  
  178. // Draw orientation axis
  179. drawAxes();
  180.  
  181. // Swap buffers for double buffering
  182. glutSwapBuffers();
  183. }
  184.  
  185. //! Metoda realizująca obliczenia na potrzeby kolejnych klatek, generuje animację
  186. void animate() {
  187.  
  188. updateTeapot();
  189. updateTorus();
  190. updateCube();
  191.  
  192. glutPostRedisplay();
  193. }
  194. //! Zmienne opisujące materiał i światło OpenGL
  195. const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  196. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  197. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  198. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  199.  
  200. const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  201. const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  202. const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  203. const GLfloat high_shininess[] = { 100.0f };
  204.  
  205. // Initialization routine.
  206. void setup()
  207. {
  208. glEnable (GL_BLEND);
  209. glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  210. glClearColor(0.5, 0.5, 0.5, 0.5);
  211. glEnable(GL_CULL_FACE);
  212. glCullFace(GL_BACK);
  213.  
  214. glEnable(GL_DEPTH_TEST);
  215. glDepthFunc(GL_LESS);
  216.  
  217. glEnable(GL_LIGHT0);
  218. glEnable(GL_NORMALIZE);
  219. glEnable(GL_COLOR_MATERIAL);
  220. glEnable(GL_LIGHTING);
  221.  
  222. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  223. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  224. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  225. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  226.  
  227. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  228. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  229. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  230. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  231.  
  232. // Register display routine.
  233. glutDisplayFunc(drawScene);
  234. // Register idle routine
  235. glutIdleFunc(animate);
  236. // Initialize camera manipulator
  237. zprInit();
  238. // Initialize first object positions
  239. initObjects();
  240. }
  241.  
  242. // Main routine: defines window properties, creates window,
  243. // registers callback routines and begins processing.
  244. int main(int argc, char **argv)
  245. {
  246. // Initialize GLUT.
  247. glutInit(&argc, argv);
  248.  
  249. // Set display mode as double-buffered, RGB color and depth.
  250. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  251.  
  252. // Set OpenGL window size.
  253. glutInitWindowSize(800, 800);
  254.  
  255. // Set position of OpenGL window upper-left corner.
  256. glutInitWindowPosition(50, 50);
  257.  
  258. // Create OpenGL window with title.
  259. glutCreateWindow("Laboratorium GK: AFI");
  260.  
  261. // Initialize.
  262. setup();
  263.  
  264. // Begin processing.
  265. glutMainLoop();
  266.  
  267. return 0;
  268. }
Add Comment
Please, Sign In to add comment