icatalin

tema 4 ex 2 [GRAFICA]

Mar 25th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <windows.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <GL/freeglut.h>
  5.  
  6. const double TWO_PI = 6.2831853;
  7.  
  8. GLsizei winWidth = 700, winHeight = 700;
  9. GLuint regHex;
  10. static GLfloat rotTheta = 0.0;
  11. double i = 0.0;
  12. double j = 0.0;
  13. double alpha = 0.5;
  14. class scrPt
  15. {
  16. public:
  17.     GLint x, y;
  18. };
  19.  
  20. static void init(void)
  21. {
  22.    
  23.  
  24.     glClearColor(1.0, 1.0, 1.0, 0.0);
  25.     glMatrixMode(GL_PROJECTION);
  26.     glOrtho(-20.0, 780.0, 0.0, 600.0, -1.0, 1.0);
  27.  
  28. }
  29.  
  30.  
  31. void deseneazaScena(void)
  32. {
  33.     glClear(GL_COLOR_BUFFER_BIT);
  34.     // axa de rostogolire in afara transformarii
  35.  
  36.  
  37.  
  38.     scrPt hexVertex;
  39.     GLdouble hexTheta;
  40.     GLint k;
  41.  
  42.     glClear(GL_COLOR_BUFFER_BIT);
  43.  
  44.     glColor3f(0, 0, 0);
  45.     glBegin(GL_LINES);
  46.     glVertex2i(-30, 100);
  47.     glVertex2i(780, 100);
  48.     glEnd();
  49.  
  50.     glPushMatrix();
  51.     glTranslated(i, 100.0, 0.0);
  52.     glPushMatrix();
  53.     glRotated(i, 0, 0, 1);
  54.     glTranslated(-i, -100.0, 0.0);
  55.     glColor3f(1.0, 0.0, 0.0);
  56.     glBegin(GL_POLYGON);
  57.  
  58.     for (k = 0; k < 6; k++)
  59.     {
  60.         hexTheta = TWO_PI * k / 6;
  61.         hexVertex.x = i + 50 * cos(hexTheta);
  62.         hexVertex.y = 100 + 50 * sin(hexTheta);
  63.         glVertex2i(hexVertex.x, hexVertex.y);
  64.     }
  65.  
  66.     glEnd();
  67.     glPopMatrix();
  68.  
  69.  
  70.    
  71.     glPopMatrix();
  72.  
  73.     glutSwapBuffers();
  74.     glFlush();
  75. }
  76.  
  77. void reshape(int w, int h)
  78. {
  79.     glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  80.     glMatrixMode(GL_PROJECTION);
  81.     glLoadIdentity();
  82.     glOrtho(-20.0, 780.0, 0.0, 600.0, -1.0, 1.0);
  83.     glMatrixMode(GL_MODELVIEW);
  84.     glLoadIdentity();
  85. }
  86.  
  87. void miscad(void)
  88. {
  89.     i = i + alpha;
  90.     if (i > 750.0)
  91.         alpha = -0.5;
  92.     else if (i < 0.0)
  93.         alpha = 0.5;
  94.     j = j + 1.0;
  95.  
  96.     glutPostRedisplay();
  97. }
  98.  
  99. void miscas(void)
  100. {
  101.     i = i + alpha;
  102.     if (i < 0.0)
  103.         alpha = 0.5;
  104.     else if (i > 750.0)
  105.         alpha = -0.5;
  106.     j = j + 1.0;
  107.  
  108.     glutPostRedisplay();
  109. }
  110. void mouse(int button, int state, int x, int y)
  111. {
  112.     switch (button) {
  113.     case GLUT_LEFT_BUTTON:
  114.         if (state == GLUT_DOWN)
  115.             alpha = -1.0; glutIdleFunc(miscas);
  116.         break;
  117.     case GLUT_RIGHT_BUTTON:
  118.         if (state == GLUT_DOWN)
  119.             alpha = 1.0; glutIdleFunc(miscad);
  120.         break;
  121.     default:
  122.         break;
  123.     }
  124. }
  125.  
  126.  
  127. void main(int argc, char** argv)
  128. {
  129.     glutInit(&argc, argv);
  130.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  131.     glutInitWindowSize(800, 600);
  132.     glutInitWindowPosition(100, 100);
  133.     glutCreateWindow("Patrat care se rostogoleste");
  134.     init();
  135.     glutDisplayFunc(deseneazaScena);
  136.     glutReshapeFunc(reshape);
  137.     glutMouseFunc(mouse);
  138.     glutMainLoop();
  139. }
Advertisement
Add Comment
Please, Sign In to add comment