Skip to content

Instantly share code, notes, and snippets.

@thuandt
Created March 12, 2013 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thuandt/5140777 to your computer and use it in GitHub Desktop.
Save thuandt/5140777 to your computer and use it in GitHub Desktop.
Star Animation
#include <GL/gl.h>
#include <GL/glut.h>
#include <cmath>
static GLfloat spin = 0.0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void star(void)
{
GLfloat angle, r, r_big_circle, r_small_circle, step = 3.14 / 5.0;
r_big_circle = 25;
r_small_circle = 10;
// draw star
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 10; i++)
{
r = (i % 2 == 0 ? r_small_circle : r_big_circle);
angle = step * i;
glVertex3f(r * cos(angle), r * sin(angle), 0);
}
glEnd();
// draw big circle
int circle_points = 100;
glBegin(GL_LINE_LOOP);
for (int i = 0; i < circle_points; i++)
{
angle = 2 * 3.14 * i / circle_points;
glVertex2f(r_big_circle * cos(angle), r_big_circle * sin(angle));
}
glEnd();
// draw small circle
glBegin(GL_LINE_LOOP);
for (int i = 0; i < circle_points; i++)
{
angle = 2 * 3.14 * i / circle_points;
glVertex2f(r_small_circle * cos(angle), r_small_circle * sin(angle));
}
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
star();
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void spinDisplay(void)
{
spin = spin + 2.0;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
/* Press M to spin */
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'm':
glutIdleFunc(spinDisplay);
break;
case 'M':
glutIdleFunc(spinDisplay);
break;
default:
glutIdleFunc(NULL);
break;
}
}
void mouse(int button, int state, int x, int y)
{
}
/*
* Request double buffer display mode.
* Register mouse input callback functions
*/
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment