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