Slavik9510

Untitled

Feb 21st, 2024
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. //Star.h
  2. #pragma once
  3. #include "../../Structures/Point2D.h";
  4. #include <GL/glut.h>
  5. #include <iostream>
  6. # define M_PI           3.14159265358979323846
  7.  
  8. class Star
  9. {
  10. private:
  11.     Point2D center;
  12.     Point2D mainVertices[5];
  13.     Point2D controlVertices[5];
  14.     float angle = 0;
  15. public:
  16.     float scale = 1;
  17.     const int VERTICES_COUNT = 5;
  18.     Star(Point2D center, float radius)
  19.     {
  20.         mainVertices[0] = { 0, radius * 3 };
  21.         mainVertices[1] = { -radius * 2.5f, radius };
  22.         mainVertices[2] = { -radius * 1.5f, -2 * radius };
  23.         mainVertices[3] = { radius * 1.5f, -2 * radius };
  24.         mainVertices[4] = { radius * 2.5f, radius };
  25.  
  26.         controlVertices[0] = { -radius / 1.5f,radius };
  27.         controlVertices[1] = { -radius,0 };
  28.         controlVertices[2] = { 0,-radius };
  29.         controlVertices[3] = { radius,0 };
  30.         controlVertices[4] = { radius / 1.5f,radius };
  31.  
  32.         for (int i = 0; i < VERTICES_COUNT; i++)
  33.         {
  34.             mainVertices[i] += center;
  35.             controlVertices[i] += center;
  36.         }
  37.  
  38.         this->center = center;
  39.     }
  40.     void draw()
  41.     {
  42.         //outer triangles
  43.         glBegin(GL_TRIANGLES);
  44.         for (int i = 0; i < VERTICES_COUNT; i++)
  45.         {
  46.             glVertex2f(mainVertices[i].x, mainVertices[i].y);
  47.             glVertex2f(controlVertices[i].x, controlVertices[i].y);
  48.             if (i == 0)
  49.             {
  50.                 glVertex2f(controlVertices[VERTICES_COUNT - 1].x, controlVertices[VERTICES_COUNT - 1].y);
  51.             }
  52.             else
  53.             {
  54.                 glVertex2f(controlVertices[i - 1].x, controlVertices[i - 1].y);
  55.             }
  56.         }
  57.         glEnd();
  58.  
  59.         //inner polygon
  60.         glBegin(GL_POLYGON);
  61.         for (int i = 0; i < VERTICES_COUNT; i++)
  62.         {
  63.             glVertex2f(controlVertices[i].x, controlVertices[i].y);
  64.         }
  65.         glEnd();
  66.     }
  67. }
  68. //main.cpp
  69. #include <GL/glut.h>
  70. #include "Models/Star/Star.h";
  71.  
  72. float scale = 0.01f;
  73.  
  74. Star* star = NULL;
  75. void reshape(int, int);
  76. void display();
  77. void init();
  78. void deallocate();
  79. void timer(int value);
  80.  
  81. int main(int argc, char* argv[])
  82. {
  83.     glutInit(&argc, argv);
  84.  
  85.     glutInitWindowSize(600, 600);
  86.     glutCreateWindow("Lab02");
  87.  
  88.     init();
  89.     glutDisplayFunc(display);
  90.     glutReshapeFunc(reshape);
  91.     glutMainLoop();
  92.  
  93.     deallocate();
  94.     return 0;
  95. }
  96.  
  97. void display()
  98. {
  99.     glClear(GL_COLOR_BUFFER_BIT);
  100.  
  101.     if (star)
  102.     {
  103.         star->draw();
  104.     }
  105.  
  106.     glutSwapBuffers();
  107. }
  108.  
  109. void reshape(int w, int h)
  110. {
  111.     glViewport(0, 0, w, h);
  112.  
  113.     glMatrixMode(GL_PROJECTION);
  114.     glLoadIdentity();
  115.     gluOrtho2D(-10, 10, -10, 10);
  116.  
  117.     glMatrixMode(GL_MODELVIEW);
  118. }
  119.  
  120. void init()
  121. {
  122.     star = new Star(Point2D { 0,0 }, 1.0f);
  123. }
  124.  
  125. void deallocate()
  126. {
  127.     delete star;
  128.     star = NULL;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment