Professional Documents
Culture Documents
Written by Administrator
Tuesday, 07 July 2009 03:11 - Last Updated Tuesday, 07 July 2009 03:29
In this article I will provide the source code written in C to allow a user fly through the solar
system, moving towards and away from planets, be able to stop and start the rotation of any
planet, convert each planet from solid spherical shape to a wire-frame model, it also provides
the user with help on using the system. Also provided is the information on each planet. This
source code is for educational purposes only and demonstrates the use of C programming with
OpenGL for rendering. Source code is provided as a single file in worker.c, and a makefile is
provided.
/*
* Author: Kaleem Baig
* Purpose: 3-D flight through the solar system
*/
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define N 9
void right_menu(int) ;
void help(void) ;
GLuint startList ;
int animation = 0, rotate = 0, revolute = 0;
void copyright(void) ;
/* HERE I USE ARRAYS TO STORE THE VALUES FOR COLOR,ROTATION AND SPEED OF
PLANETS */
GLfloat color_array[N][3] ;
GLfloat rot_axis[N] ;
GLfloat rot_sun[N] ;
GLfloat speed_axis[N] ;
GLfloat speed_sun[N] ;
GLfloat rot_moon = 5 ;
float radius[N] ;
float translation[N][2] ;
float first = 0.01 ;
float second = -0.01 ;
int picked = 0 ;
1 / 11
C/OpenGL Code for 3D Flight through Solar System
Written by Administrator
Tuesday, 07 July 2009 03:11 - Last Updated Tuesday, 07 July 2009 03:29
char global_key ;
startList = glGenLists(66);
qobj = gluNewQuadric() ;
gluQuadricCallback(qobj,GLU_ERROR,errorCallback) ;
gluQuadricDrawStyle(qobj , GLU_POINT);
gluQuadricNormals(qobj,GLU_NONE);
glNewList(startList,GL_COMPILE);
gluDisk(qobj,7.0,7.3,55,1);
glEndList() ;
2 / 11
C/OpenGL Code for 3D Flight through Solar System
Written by Administrator
Tuesday, 07 July 2009 03:11 - Last Updated Tuesday, 07 July 2009 03:29
color_array[0][1] = 1.0;
color_array[0][2] = 0.0;
rot_axis[0] = 0;
rot_sun[0] = 0; /* MERCURY */
speed_axis[0] = 2 ;
speed_sun[0] = 1 ;
radius[0] = 0.12 ;
translation[0][0] = 1.4 ;
translation[0][1] = 0.0;
color_array[1][0] = 0.0;
color_array[1][1] = 1.0;
color_array[1][2] = 1.0; /* VENUS */
rot_axis[1] = 0;
rot_sun[1] = 0;
speed_axis[1] = 3 ;
speed_sun[1] = 1 ;
radius[1] = 0.2 ;
translation[1][0] = 2.5 ;
translation[1][1] = 2.5;
color_array[2][0] = 0.0;
color_array[2][1] = 1.0;
color_array[2][2] = 0.0;
rot_axis[2] = 0;
rot_sun[2] = 0; /* EARTH */
speed_axis[2] = 1 ;
speed_sun[2] = 1 ;
radius[2] = 0.4 ;
translation[2][0] = 3.8 ;
translation[2][1] = -3.8 ;
color_array[3][0] = 0.5;
color_array[3][1] = 0.5;
color_array[3][2] = 0.5;
rot_axis[3] = 0;
rot_sun[3] = 0;
speed_axis[3] = 6 ; /* MARS */
speed_sun[3] = 1 ;
radius[3] = 0.3 ;
translation[3][0] = -4.7 ;
translation[3][1] = -4.7 ;
color_array[4][0] = 0.7;
color_array[4][1] = 0.3;
color_array[4][2] = 0.6;
3 / 11
C/OpenGL Code for 3D Flight through Solar System
Written by Administrator
Tuesday, 07 July 2009 03:11 - Last Updated Tuesday, 07 July 2009 03:29
rot_axis[4] = 0;
rot_sun[4] = 0; /* JUPITER */
speed_axis[4] = 8 ;
speed_sun[4] = 1 ;
radius[4] = 0.8 ;
translation[4][0] = 6.3 ;
translation[4][1] = 6.3 ;
color_array[5][0] = 0.3;
color_array[5][1] = 0.7;
color_array[5][2] = 0.4;
rot_axis[5] = 0;
rot_sun[5] = 0;
speed_axis[5] = 2 ;
speed_sun[5] = 1 ; /* SATURN */
radius[5] = 0.6 ;
translation[5][0] = 7.8 ;
translation[5][1] = -7.8 ;
color_array[6][0] = 0.3;
color_array[6][1] = 0.8;
color_array[6][2] = 0.2;
rot_axis[6] = 0;
rot_sun[6] = 0;
speed_axis[6] = 3 ;
speed_sun[6] = 1 ; /* URANUS */
radius[6] = 0.55 ;
translation[6][0] = 9.0 ;
translation[6][1] = 9.0 ;
color_array[7][0] = 0.4;
color_array[7][1] = 0.4;
color_array[7][2] = 0.9;
rot_axis[7] = 0;
rot_sun[7] = 0;
speed_axis[7] = 2 ; /* NEPTUNE */
speed_sun[7] = 1 ;
radius[7] = 0.57 ;
translation[7][0] = -10.7 ;
translation[7][1] = 10.7;
color_array[8][0] = 0.7;
color_array[8][1] = 0.0;
color_array[8][2] = 0.0;
4 / 11
C/OpenGL Code for 3D Flight through Solar System
Written by Administrator
Tuesday, 07 July 2009 03:11 - Last Updated Tuesday, 07 July 2009 03:29
rot_axis[8] = 0;
rot_sun[8] = 0; /* PLUTO */
speed_axis[8] = 9 ;
speed_sun[8] = 1 ;
radius[8] = 0.120 ;
translation[8][0] = 17.0 ;
translation[8][1] = 0.0 ;
}
/**************************************************/
void display(void)
{
int i ,y1;
double moon_radius = 0.12 ;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
5 / 11
C/OpenGL Code for 3D Flight through Solar System
Written by Administrator
Tuesday, 07 July 2009 03:11 - Last Updated Tuesday, 07 July 2009 03:29
for(y1=1;y1<=95;y1++)
glCallList(startList+y1) ;
glPopMatrix() ;
}
if(i == 2)
{
glPushMatrix() ;
glColor3f(1.0,1.0,1.0) ;
glRotatef((GLfloat)(0-rot_moon),0.0,1.0,0.0);
glTranslatef((float)(translation[i][0]-3.0),0.0,0.0) ;/*(float)(translation[i][1]+3.0)) ;*/
glutWireSphere((GLfloat) moon_radius,10,8) ;
glPopMatrix() ;
}
glPopMatrix() ;
}
glPopMatrix();
glutSwapBuffers() ;
}
/****************************************/
}
/**************************************/
void help(void)
{
system("mozilla help.html") ;
}
/********************************************/
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h,0.5, 250.0);
glMatrixMode(GL_MODELVIEW);
6 / 11
C/OpenGL Code for 3D Flight through Solar System
Written by Administrator
Tuesday, 07 July 2009 03:11 - Last Updated Tuesday, 07 July 2009 03:29
glLoadIdentity();
gluLookAt (0.0,5.0,20.0, 0.0, 0.0, 0.0, 0.0, 1.0,0.0);
}
/***********************************************/
void copyright(void)
{
system("mozilla kb.html") ;
}
/****************************************************/
void idle(void)
{
int i,j,k ;
static int counter;
if ((counter++)%1000!=0) return;
if (!animation) return;
if (picked==0){
j=0;
i=9;
} else {
j=picked-1;
i=picked;
}
for (k=j; k<i; k++){
if(k == 2)
rot_moon = rot_moon + 13 ;
if (rotate)
{
if(global_key == 'd')
rot_axis[k] = ((int)rot_axis[k] + (int)speed_axis[k])%360 ;
if(global_key == 'D')
rot_axis[k] = ((int)rot_axis[k] - (int)speed_axis[k])%360 ;
}
if (revolute)
{ if(global_key == 'y')
rot_sun[k] = ((int)rot_sun[k] + (int)speed_sun[k])%360 ;
if(global_key == 'Y')
rot_sun[k] = ((int)rot_sun[k] - (int)speed_sun[k])%360 ;
}
}
glutPostRedisplay() ;
7 / 11
C/OpenGL Code for 3D Flight through Solar System
Written by Administrator
Tuesday, 07 July 2009 03:11 - Last Updated Tuesday, 07 July 2009 03:29
/******************************************************/
void keyboard (unsigned char key, int x, int y)
{
int i,j,k,h,solar = 0;
char t ;
global_key = key ;
if(key>='0' && key<='9')
{
picked = key - '0';
glutPostRedisplay() ;
return ;
}
glutIdleFunc(NULL);
if (picked==0){
j=0;
i=9;
} else {
j=picked-1;
i=picked;
}
switch (key) {
case 'd':
revolute=0;
rotate=1;
animation = 1 ;
if(j == 2)
rot_moon = rot_moon + 13 ;
for(k = j; k < i ; k++)
{
rot_axis[k] = ((int) rot_axis[k] + (int)speed_axis[i])%360;
}
break;
case 'D':
revolute=0;
rotate=1;
animation = 1;
if(j == 2) rot_moon = rot_moon + 13 ;
for(k = j;k < i ;k++)
{
rot_axis[k] = ((int)rot_axis[k] - (int)speed_axis[k])%360 ;
}
break;
case 'y':
rotate=0;
revolute=1;
8 / 11
C/OpenGL Code for 3D Flight through Solar System
Written by Administrator
Tuesday, 07 July 2009 03:11 - Last Updated Tuesday, 07 July 2009 03:29
animation = 1;
if(j == 2) rot_moon = rot_moon + 13 ;
9 / 11
C/OpenGL Code for 3D Flight through Solar System
Written by Administrator
Tuesday, 07 July 2009 03:11 - Last Updated Tuesday, 07 July 2009 03:29
}
/*****************************************************/
int main(int argc, char** argv)
{
int i = 0;
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
10 / 11
C/OpenGL Code for 3D Flight through Solar System
Written by Administrator
Tuesday, 07 July 2009 03:11 - Last Updated Tuesday, 07 July 2009 03:29
11 / 11