MateuszGrabarczyk

gk lab 1

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