Saturday, July 19, 2008

Simple Solar System in OpenGL




I have created a simple Solar System using OpenGL




#include <stdlib.h>
#include <GLUT/glut.h>
#include <math.h>
#include <stdio.h>

static float Xvalue = 0.0, Yvalue = 0.0, Angle = 0.0;

int MoveX = 0;
int MoveY = 0;

void myInit(void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
}


static float x1[360][2];
static float x2[360][2];
static float x3[720][2];


void generateCircle()
{
int i = 0;

for(i=0; i <= 360; i++)
{
x1[i][0] = sin(i*3.1416/180)*3;
x1[i][1] = cos(i*3.1416/180)*3;
}

for(i=0; i <= 360; i++)
{
x2[i][0] = sin(i*3.1416/180)*1;
x2[i][1] = cos(i*3.1416/180)*1;
}

for(i=0; i <= 720; i++)
{
x3[i][0] = sin(i*3.1416/180)*5;
x3[i][1] = cos(i*3.1416/180)*5;
}

}




void myDisplay(void) {

glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);

//sun
glPushMatrix();
gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
glTranslatef(Xvalue, 0.0, Yvalue);
glRotatef(Angle, 0.0, 0.0, 1.0);
glutWireSphere (0.5, 15, 15);
glPopMatrix();

glPushMatrix();
gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
if(MoveX==360)
MoveX = 0;
glTranslatef(x1[MoveX][1], x1[MoveX][0], 0.0);
glRotatef(Angle, 0.0, 0.0, 1.0);
glutWireSphere (0.4, 15, 15);
glTranslatef(x2[MoveX][0], x2[MoveX][1], 0.0);
glutWireSphere (0.2, 15, 15);
glPopMatrix();

glPushMatrix();
gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
if(MoveY==720)
MoveY = 0;
glTranslatef(x3[MoveY/2][1], x3[MoveY/2][0], 0.0);
glRotatef(Angle, 0.0, 0.0, 1.0);
glutWireSphere (0.4, 15, 15);
int i = 0;
//glBegin(GL_LINE_STRIP);
glBegin(GL_QUAD_STRIP);
for(i=0; i <= 360; i++)
{
glVertex3f(sin(i*3.1416/180)*0.5, cos(i*3.1416/180)*0.5, 0 );
glVertex3f(sin(i*3.1416/180)*0.7, cos(i*3.1416/180)*0.7, 0 );
}
glEnd();
glRotatef(Angle, 0.0, 0.0, 1.0);
glPopMatrix();

glFlush ();
}


void resize(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}


void animation()
{
Angle += 15.0;
glutPostRedisplay();
MoveX +=1;
MoveY +=1;
glutPostRedisplay();
glutTimerFunc(100, animation, 0);

}


int main(int argc, char ** argv){

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1024, 768);
glutInitWindowPosition(100, 150);
glutCreateWindow("OpenGL");
myInit();
glutDisplayFunc(myDisplay);
glutReshapeFunc(resize);
generateCircle();
glutTimerFunc(100, animation, 0);
glutMainLoop();
}


11 comments:

Unknown said...

it doesnt like gluttimerfunc, it cant convert the 2nd parameter from void to an int return type...any ideas?

salahuddin66 said...

yes my teacher had same problem compiling in windows gluttimerfunc.

One of my friend used custom "thread" to do same functionality like gluttimerfunc.

brownman said...

void animation(int value)


I just added a int parameter,
and now it is work without a problem.

thanks..

Anonymous said...

I wanted to map a texture onto the planets using bmp or raw files...i googled four days but couldn't find any codes... can you please write one on your blog.. or extend ur same program with texture mapping...and can u also explain how the functions using sin cos work in ur prog..atleast give a good site to study those pls..

Unknown said...

i have added nine planets but can u sugeest how to add orbits to these planets

Unknown said...

i have added nine planets but can u sugeest how to add orbits to these planets

salahuddin66 said...

void generateCircle() function is doing that...

Vadil said...

please, share the code, maybe that's useful to other people.

shravs said...

i'm unable to work with the timer function can any body help me??n also wanted to create textures also but not sure with the functions so i'm struck here

salahuddin66 said...

I guess you are using windows.

It wont work in windows. Use windows specific function for it.

Unknown said...

timerfunction works in visual studio 2008 n also in linux...
n i hav executed it