Hello I am trying to orbit the earth around the sun in an orbit path.What I have done so far draw two cirlces one the sun and the earth.The earth is on the ellipse.Tried to use glutTimerFunc() to create an animation but no result, here is the code.How is it possible to make earth orbit around the sun in an ellipse path.
#include <gl/glut.h>
#include <math.h>
#define PI 3.14159
#define circlePoints 256
#define ellipsePoints 256
int i;
/*void myWireSphere(GLfloat radius, int slices, int stacks) {
glPushMatrix();
glRotatef(-90.0, 1.0, 0.0, 0.0);
glutWireSphere(radius, slices, stacks);
glPopMatrix();
}*/
void circle(){
glColor3f(1,1,0);
GLfloat angleStep=2*PI/(float)circlePoints;
GLuint pointsPerQuarter=circlePoints/4;
GLfloat x[circlePoints];
GLfloat y[circlePoints];
GLfloat radius=3;
for(i=0;i<pointsPerQuarter/2;i++)
{
//Define points in first quadrant
x[i]=radius*cos(i*angleStep);
y[i]=radius*sin(i*angleStep);
x[pointsPerQuarter-1-i]=y[i];
y[pointsPerQuarter-1-i]=x[i];
//Define points in second quadrant
x[pointsPerQuarter+i]=-y[i];
y[pointsPerQuarter+i]=x[i];
x[2*pointsPerQuarter-1-i]=-x[i];
y[2*pointsPerQuarter-1-i]=y[i];
//Define points in third quadrant
x[2*pointsPerQuarter+i]=-x[i];
y[2*pointsPerQuarter+i]=-y[i];
x[3*pointsPerQuarter-1-i]=-y[i];
y[3*pointsPerQuarter-1-i]=-x[i];
//Define points in fourth quadrant
x[3*pointsPerQuarter+i]=y[i];
y[3*pointsPerQuarter+i]=-x[i];
x[4*pointsPerQuarter-1-i]=x[i];
y[4*pointsPerQuarter-1-i]=-y[i];
}
glBegin(GL_LINE_LOOP);
for (i=0;i<circlePoints;i++)
{
glVertex2f(x[i],y[i]);
}
glEnd();
}
void circlearth(){
glColor3f(0,0,1);
GLfloat angleStep=2*PI/(float)circlePoints;
GLuint pointsPerQuarter=circlePoints/4;
GLfloat x[circlePoints];
GLfloat y[circlePoints];
GLfloat radius=1;
for(i=0;i<pointsPerQuarter/2;i++)
{
//Define points in first quadrant
x[i]=radius*cos(i*angleStep);
y[i]=radius*sin(i*angleStep);
x[pointsPerQuarter-1-i]=y[i];
y[pointsPerQuarter-1-i]=x[i];
//Define points in second quadrant
x[pointsPerQuarter+i]=-y[i];
y[pointsPerQuarter+i]=x[i];
x[2*pointsPerQuarter-1-i]=-x[i];
y[2*pointsPerQuarter-1-i]=y[i];
//Define points in third quadrant
x[2*pointsPerQuarter+i]=-x[i];
y[2*pointsPerQuarter+i]=-y[i];
x[3*pointsPerQuarter-1-i]=-y[i];
y[3*pointsPerQuarter-1-i]=-x[i];
//Define points in fourth quadrant
x[3*pointsPerQuarter+i]=y[i];
y[3*pointsPerQuarter+i]=-x[i];
x[4*pointsPerQuarter-1-i]=x[i];
y[4*pointsPerQuarter-1-i]=-y[i];
}
glBegin(GL_LINE_LOOP);
for (i=0;i<circlePoints;i++)
{
glVertex2f(x[i],y[i]);
}
glEnd();
}
void ellipse(){
glColor3f(0,0,0);
GLfloat angleStep=2*PI/(float)ellipsePoints;
GLuint pointsPerQuarter=ellipsePoints;///4;
GLfloat x[ellipsePoints];
GLfloat y[ellipsePoints];
GLfloat rx=15;
GLfloat ry=10;
glBegin(GL_LINE_LOOP);
for(i=0;i<pointsPerQuarter;i++)
{
x[i]=rx*cos(i*angleStep);
y[i]=ry*sin(i*angleStep);
}
for(i=0;i<ellipsePoints;i++)
{
glVertex2f(x[i],y[i]);
}
glEnd();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-32,32,-24,24);
glMatrixMode(GL_MODELVIEW);
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(500, timer, 0);
}
void init(void)
{
glClearColor(1,1,1,0);
glColor3f(0, 0, 0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(3);
ellipse();
glPushMatrix();
glTranslatef(-7.0, 0.0, 0.0);
circle();
glPopMatrix();
glPushMatrix();
glTranslatef(15.0, 0.0, 0.0);
circlearth();
glPopMatrix();
glFlush();
glutSwapBuffers(); // double buffer
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowPosition(50,50);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutCreateWindow("Earth-Sun");
glMatrixMode(GL_PROJECTION);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0000, timer, 0);
glutMainLoop();
return 0;
}