#include <gl/glut.h>
#include <MATH.H>
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
#define DistanceEarthSun 8
GLint angleEarthSelf=0;
GLint angleEarthSun=0;
const float PI=3.1415;
void DrawOrbit(GLfloat radius)
{
GLfloat x,y,z;
glBegin(GL_LINE_LOOP);
for (int alpha=0;alpha<360;alpha++)
{
x=radius*cos(alpha*PI/180);
y=radius*sin(alpha*PI/180);
z=0;
glVertex3f(x,y,z);
}
glEnd();
}
void Display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glLoadIdentity();
glTranslatef(0,0,-25);
glutWireSphere(1,100,100);
glColor3f(0,1,0);
glLineWidth(5);
DrawOrbit(DistanceEarthSun);
glPushMatrix();
// glPushMatrix();
// glRotatef(angleEarthSelf,0,1,0);
// glutWireSphere(1,10,10);
// glPopMatrix();
glRotatef(angleEarthSun,0,0,1);
glTranslatef(DistanceEarthSun,0,0);
glRotatef(angleEarthSelf,0,1,0);
glutWireSphere(1,10,10);
glutSwapBuffers();
}
void RotateEarth(int value)
{
if(value==1)
{
angleEarthSelf+=5;
angleEarthSelf%=360;
glutPostRedisplay();
glutTimerFunc(100,RotateEarth,1);
}
else if(value==2)
{
angleEarthSun+=5;
angleEarthSun%=360;
glutPostRedisplay();
glutTimerFunc(1000,RotateEarth,2);
}
}
void reshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,GLdouble(w)/h,1,100);
glMatrixMode(GL_MODELVIEW);
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
glutInitWindowPosition(10,10);
glutInitWindowSize(1000,1000);
glutCreateWindow("Test");
glutTimerFunc(100,RotateEarth,1);
glutTimerFunc(1000,RotateEarth,2);
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutMainLoop();
}