More Related Content
Similar to Ass day2 3_sun_moon_earth (6)
Ass day2 3_sun_moon_earth
- 1. #include "windows.h"
#include <glglut.h>
#define sunRaduis 0.4
#define earthRaduis 0.08
#define moonRaduis 0.018
GLfloat rotationSpeed = 0.07;
GLfloat daysInYear = 365;
GLfloat year = 0.0; //in degrees
GLfloat day = 0.0;
GLfloat moonAroundEarth = 0.0;
GLfloat moonItsSelf = 0.0;
GLfloat earthOrbitRadius = 1.2;
GLfloat moonOrbitRadius = 0.2;
GLfloat moonAroundEarthRate = 1 * rotationSpeed;
GLfloat moonRotationItselfRate = 1.0 * rotationSpeed;
GLfloat dayRate = 5.0 * rotationSpeed;
GLfloat yearRate = daysInYear / 360.0 * dayRate * rotationSpeed;
void drawSolarSystem()
{
glPushMatrix();
gluLookAt(0.0,0.0,-3.0,0.0,0.0,1.0,0.0,-3.0,0.0);
//Drawing the sun.
glColor3f(1,0,0);
glutSolidSphere(sunRaduis,40,40);
glPushMatrix();
glRotatef(year,0.0,0,1.0); //rotation for earth around
y-axis
glTranslatef(earthOrbitRadius,0.0,1.0); //
translation for earth around y-axis
glRotatef(-year,0.0,0,1.0); //rotation for earth around
y-axis
//EARTH
glPushMatrix();
//color earth
glColor3f(0.0,0.0,1);
//Drawing earth
glutSolidSphere(earthRaduis,10,10);
glPopMatrix();
// rotation for moon.
glRotatef(moonAroundEarth,0.0,0.0,1.0);
// translation for moon.
glTranslatef(moonOrbitRadius,0.2,0.2); //we took along x-axis and y-axis
// around earth rotation.
glRotatef(-moonAroundEarth,0.0,0.0,1.0);
glColor3f(1,1,1);
// drawing moon
glutSolidSphere(moonRaduis,20,20);
glPopMatrix();
glPopMatrix();
}
void Initialization()
{
glClearColor(0.0,0.0,0.0,0.0);
glClearDepth(10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
- 2. }
void displayFunc()
{
glClear(GL_COLOR_BUFFER_BIT);
//now going to drawSolarSystem() function
drawSolarSystem();
glFlush();
glutSwapBuffers();
}
void reshapeFunc(int x, int y)
{
if (y == 0 || x==0) return;
glLoadIdentity();
gluPerspective(45,(GLdouble)x/(GLdouble)y,0.5,200.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y);
//now going to displayFunc() function
displayFunc();
}
void idleFunc(void)
{
// idle event call back,here we increase the values and redisply .
day = day + dayRate;
year= year + yearRate;
moonItsSelf = moonItsSelf + moonRotationItselfRate;
moonAroundEarth = moonAroundEarth + moonAroundEarthRate;
//now going to displayFunc() function
displayFunc();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800,800);
glutCreateWindow("Sun,Earth,Moon = ROTATION ");
Initialization();
glutDisplayFunc(displayFunc);
glutReshapeFunc(reshapeFunc);
glutIdleFunc(idleFunc);
glutMainLoop();
return 0;
}