#define GLUT_DISABLE_ATEXIT_HACK
#include <glut.h>
#include <math.h>
#define PI 3.14159
int n=6,R=10;
float cx=0,cy=0;
float sx=1,sy=1;
float theta=0.0;
void Keyboard(unsigned char key,int x,int y);
void Display(void);
void Reshape(int w,int h);
void myidle();
int main()
{
glutInitWindowSize(700,700);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("ARotating Square");
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutIdleFunc(myidle);
glutMainLoop();
return 0;
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(cx,cy,0);
glRotatef(theta,0,0,1);
glScalef(sx,sy,1);
glTranslatef(-cx,-cy,0);
glColor3f(1.0,0,0);
glBegin(GL_POLYGON);
for (int i=0;i<n;i++)
glVertex2f(R*cosf(i*2*PI/n),R*sinf(i*2*PI/n));
glEnd();
glutSwapBuffers();
}
void myidle()
{
theta+=0.2;
if (theta>=360) theta-=360;
sx=sx*1.01;
sy=sy*1.01;
if(sx>3) sx=1;
if (sy>3) sy=1;
glutPostRedisplay();
}
void Reshape(GLsizei w,GLsizei h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.5*R*w/h,1.5*R*w/h,-1.5*R,1.5*R);
glViewport(0,0,w,h);
glMatrixMode(GL_MODELVIEW);
}