You are on page 1of 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

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

GLfloat xcam = 0.0 ;


GLfloat ycam = 0.0 ;
GLfloat zcam = 0.0 ;
GLfloat xrcam = 0.0 ;
GLfloat yrcam = 0.0 ;
GLfloat zrcam = 0.0 ;

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 ;

static int year = 0, day = 0;


void idle(void) ;
int entered_key = 0 ;

void errorCallback(GLenum errorCode)


{
   const GLbyte *estring ;
   estring = gluErrorString(errorCode) ;
   fprintf(stderr,"Quadric Error : %sn",estring) ;
   exit(0) ;
}
/*************************************************************************/
void init(void)

    int y ;
    float z = 0.61 ;
 
   GLUquadricObj *qobj ;
 
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_FLAT);
   glEnable(GL_DEPTH_TEST);

   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() ;

   for(y = 1 ;y <= 95 ; y++)


    {
       gluQuadricDrawStyle(qobj , GLU_POINT);
       gluQuadricNormals(qobj,GLU_NONE);
       glNewList(startList+1,GL_COMPILE);
              gluDisk(qobj,z,z + 0.01,35,2);
       glEndList() ;
       z+=0.01 ;
   }
  
   color_array[0][0] = 1.0;

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();

   gluLookAt (0.0,5.0,20.0, 0.0, 0.0, 0.0, 0.0, 1.0,0.0);


   glTranslatef((GLfloat)xcam,(GLfloat)ycam,(GLfloat)zcam) ;
   glRotatef((GLfloat)xrcam,1.0,0.0,0.0) ;
   glRotatef((GLfloat)yrcam,0.0,1.0,0.0) ;
   glRotatef((GLfloat)zrcam,0.0,0.0,1.0) ;

   glColor3f (1.0, 1.0, 1.0);


   glPushMatrix();
   glPushMatrix() ;
   glRotatef(90,1.0,0.0,0.0) ;
   glCallList(startList);
   glPopMatrix() ;

   glColor3f (1.0, 0.0, 0.0);


   glutWireSphere(1.0, 20, 16);   /* draw sun */
   /* HERE WE ADD THE CODE TO DRAW THE PLANETS */
   for(i = 0 ; i < 9 ; i++ )
   {
       glPushMatrix();
       glColor3f(color_array[i][0],color_array[i][1],color_array[i][2]);
       glRotatef((GLfloat)(0- rot_sun[i]),0.0,1.0,0.0);
       glTranslatef((float) translation[i][0],0.0,(float)translation[i][1]);
       glRotatef((GLfloat)(0 -  rot_axis[i]),0.0,1.0,0.0);
       glutWireSphere((GLfloat)radius[i],10,8);

      if(i ==5 )


       {
          glPushMatrix() ;
          glRotatef(90.0,10,0.0,0.0) ;

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 right_menu(int id)


{
  glutIdleFunc(NULL);
  if(id == 1) exit(0) ;
  else if(id == 2) help();
       else
           copyright() ;

}
/**************************************/
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 ;

         for(k = j ;k < i ; k++ )


     {
       rot_sun[k] = ((int)rot_sun[k] + (int)speed_sun[k])%306 ;
         }
         break;
      case 'Y':
         rotate=0;
         revolute=1;
         animation = 1;

     if(j == 2) rot_moon = rot_moon + 13 ;

         for(k = j ; k < i  ; k++)


     {
      rot_sun[k] = ((int)rot_sun[k] - (int)speed_sun[k])%360 ;
         }
         break;
      case 'H' :
      case 'h' :
         if( i== 1) system("mozilla mercury.html &") ;
         if( i== 2) system("mozilla venus.html &") ;
         if( i== 3) system("mozilla earth.html &") ;
         if( i== 4) system("mozilla mars.html &") ;
         if( i== 5) system("mozilla jupiter.html &") ;
         if( i== 6) system("mozilla saturn.html &") ;
         if( i== 7) system("mozilla uranus.html &") ;
         if( i== 8) system("mozilla  neptune.html &") ;
         if( i== 9) system("mozilla pluto.html &") ;
         break ;
     case 'z' :
          zcam-=1;
          break ;
     case 'Z' :
          zcam+=1 ;
         break ;
     case 'r' :
          xcam+=1 ;
          break ;
     case 'l' :
          xcam-=1 ;
           break ;

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

     case 't' :


           ycam+=1 ;
           break ;
     case 'T' :
           ycam-=1 ;
     case 'a' :
           xrcam+=1 ;
           break ;
     case 'b' :
           yrcam+=1 ;
           break ;
     case 'c' :
           zrcam+=1 ;
           break ;       
     case 'A' :
           xrcam-=1 ;
           break ;
     case 'B':
           yrcam-=1 ;
           break ;
     case 'C' :
           zrcam-=1 ;
           break ;
     case 'R' :
           for(h=0;h<9;h++)
           {
              rot_sun[h] = 0 ;
           }
           break ;    
    case 'S' :
          animation = 0 ;
          break ;
    default:
         break;
   }
    glutIdleFunc(idle) ;
    glutPostRedisplay();

}
/*****************************************************/
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

   glutInitWindowPosition (100, 100);


   glutCreateWindow (argv[0]);
   init();
   glutDisplayFunc(display);
   glutCreateMenu(right_menu) ;
   glutAddMenuEntry("QUIT",1) ;
   glutAddMenuEntry("NEED HELP",2) ;
   glutAddMenuEntry("COPYRIGHT",3) ;
   glutAttachMenu(GLUT_RIGHT_BUTTON) ;
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0;
}

11 / 11

You might also like