Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Star.h
- #pragma once
- #include "../../Structures/Point2D.h";
- #include <GL/glut.h>
- #include <iostream>
- # define M_PI 3.14159265358979323846
- class Star
- {
- private:
- Point2D center;
- Point2D mainVertices[5];
- Point2D controlVertices[5];
- float angle = 0;
- public:
- float scale = 1;
- const int VERTICES_COUNT = 5;
- Star(Point2D center, float radius)
- {
- mainVertices[0] = { 0, radius * 3 };
- mainVertices[1] = { -radius * 2.5f, radius };
- mainVertices[2] = { -radius * 1.5f, -2 * radius };
- mainVertices[3] = { radius * 1.5f, -2 * radius };
- mainVertices[4] = { radius * 2.5f, radius };
- controlVertices[0] = { -radius / 1.5f,radius };
- controlVertices[1] = { -radius,0 };
- controlVertices[2] = { 0,-radius };
- controlVertices[3] = { radius,0 };
- controlVertices[4] = { radius / 1.5f,radius };
- for (int i = 0; i < VERTICES_COUNT; i++)
- {
- mainVertices[i] += center;
- controlVertices[i] += center;
- }
- this->center = center;
- }
- void draw()
- {
- //outer triangles
- glBegin(GL_TRIANGLES);
- for (int i = 0; i < VERTICES_COUNT; i++)
- {
- glVertex2f(mainVertices[i].x, mainVertices[i].y);
- glVertex2f(controlVertices[i].x, controlVertices[i].y);
- if (i == 0)
- {
- glVertex2f(controlVertices[VERTICES_COUNT - 1].x, controlVertices[VERTICES_COUNT - 1].y);
- }
- else
- {
- glVertex2f(controlVertices[i - 1].x, controlVertices[i - 1].y);
- }
- }
- glEnd();
- //inner polygon
- glBegin(GL_POLYGON);
- for (int i = 0; i < VERTICES_COUNT; i++)
- {
- glVertex2f(controlVertices[i].x, controlVertices[i].y);
- }
- glEnd();
- }
- }
- //main.cpp
- #include <GL/glut.h>
- #include "Models/Star/Star.h";
- float scale = 0.01f;
- Star* star = NULL;
- void reshape(int, int);
- void display();
- void init();
- void deallocate();
- void timer(int value);
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitWindowSize(600, 600);
- glutCreateWindow("Lab02");
- init();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMainLoop();
- deallocate();
- return 0;
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- if (star)
- {
- star->draw();
- }
- glutSwapBuffers();
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(-10, 10, -10, 10);
- glMatrixMode(GL_MODELVIEW);
- }
- void init()
- {
- star = new Star(Point2D { 0,0 }, 1.0f);
- }
- void deallocate()
- {
- delete star;
- star = NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment