#include #include #include #include using namespace std; using namespace glm; double Y_AXIS = 5; double aspectRatio = 1; int points_num = 5; vec2 center = vec2(2.5,2.4); double innerR = .3; double outerR = 1; double startAngle = 0; double direction = 1; double moveSpeed = .05; double w = 2.0; // angular velocity int FPS = 60; // frames per sec double alpha = w/FPS ; vector inner; vector outer; void draw(GLenum mode, vector points) { glColor3f(1.0, 1.0, 1.0); glBegin(mode); for(int i = 0; i < points.size(); i++) glVertex2d(points[i].x, points[i].y); glEnd(); } vector createCircle(int points_num, vec2 center, double startAngle, double r) { vector points; double dA = M_PI * 2 / points_num; double angle = startAngle; for (int i = 0; i < points_num; i++) { points.push_back(vec2(center.x + r*cos(angle), center.y + r*sin(angle))); angle += dA; } return points; } vector createStar(int points_num, vec2 center, double innerR, double outerR, double startAngle) { vector points; double dA = M_PI * 2 / points_num; double angle = startAngle; for (int i = 0; i < points_num; i++) { points.push_back(vec2(center.x + innerR*cos(angle), center.y + innerR*sin(angle))); angle += dA / 2; points.push_back(vec2(center.x + outerR*cos(angle), center.y + outerR*sin(angle))); angle += dA / 2; points.push_back(vec2(center.x + innerR*cos(angle), center.y + innerR*sin(angle))); } return points; } void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); draw(GL_POLYGON, inner); draw(GL_TRIANGLES, outer); glFlush(); glutPostRedisplay(); } void timer(int v) { startAngle += direction*alpha; inner = createCircle(points_num, center, startAngle, innerR); outer = createStar(points_num, center, innerR, outerR, startAngle); glutTimerFunc(1000/FPS, timer, v); glutPostRedisplay(); } void init() { glClearColor (0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, Y_AXIS*aspectRatio, 0.0, Y_AXIS, -1.0, 1.0); } void mouseClick(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) points_num++; else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN && points_num>2) points_num--; } void keyboardClick(unsigned char key, int x, int y) { switch (key) { case 'a': if (center.x - moveSpeed>outerR) center = vec2(center.x - moveSpeed, center.y); break; case 's': if (center.y - moveSpeed>outerR) center = vec2(center.x, center.y - moveSpeed); break; case 'd': if (center.x + moveSpeed<=Y_AXIS*aspectRatio-outerR) center = vec2(center.x + moveSpeed, center.y); break; case 'w': if (center.y + moveSpeed<=Y_AXIS - outerR) center = vec2(center.x, center.y + moveSpeed); break; case ' ': direction *= -1; break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow ("ZvezdaGLM"); init(); inner = createCircle(points_num, center, startAngle, innerR); outer = createStar(points_num, center, innerR, outerR, startAngle); glutDisplayFunc(display); glutTimerFunc(100, timer, 0); glutMouseFunc(mouseClick); glutKeyboardFunc(keyboardClick); glutMainLoop(); return 0; }