Social Icons

Friday 2 May 2014

Solar System Using Opengl IN Graphics


#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>

//float x=0.0;
float a[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};//angle of Rotation
float speed[10]={0.0,0.06,0.05,0.04,0.03,0.02,0.01,0.008,0.007,0.006};//speed of Rotation
float radius[10]=   {5.0,1.5,1.2,1.0,2.0,1.3,1.0,1.0,1.0,1.0};//planet Size
float distancex[10]={0.0,7.0,10.0,13.0,17.0,22.0,26.0,29.0,32.0,35.0};//DIstance Between Planets

  GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
  GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
  GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
  GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
  GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
  GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
  GLfloat skyblue[] = { 0.2, 0.2, 1.0, 1.0 };
  GLfloat mitty[] = { 0.8, 0.8, 1.0, 1.0 };

void display()
{
       int i;
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       glMatrixMode(GL_MODELVIEW);

        for(i=0;i<10;i++)
        {
            glPushMatrix();
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,white);
             glutWireTorus(0.001,distancex[i],50,50);
            if(i==4)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,red);
            else if(i==3)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,blue);
            else if(i==0)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,yellow);
            else
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,mitty);
            glRotatef(a[i],0.0,0.0,1.0);
            glTranslatef(distancex[i],0.0,0.0);
             if(i==6)
            {
                glRotatef(-45,1.0,1.0,0.0);
                glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,green);
                glutWireTorus(0.2,1.5,20,20);
            }

            glPushMatrix();
            glRotatef(a[i],0.0,1.0,0.0);
            glutSolidSphere(radius[i],30,10);
            glPopMatrix();

              glPopMatrix();
          }

      glutSwapBuffers();
}

void timer(int vaulue)
{
    int i;
    for(i=1;i<10;i++)
       {
           a[i] = a[i]+speed[i];
       }
       glutPostRedisplay();
       glutTimerFunc(1000/500,timer,1);
}

void reshape(GLint w, GLint h) {
   glViewport(0, 0, w, h);
 // glClearColor(1.0,1.0,1.0,1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
gluLookAt(5.0,-5.0,10.0,.0,0.0,0.0,0.0,0.0,1.0);
  glMatrixMode(GL_PROJECTION);
  GLfloat aspect = (GLfloat)(w) / (GLfloat)(h);
  glLoadIdentity();
  if (w <= h) {
    // width is smaller, so stretch out the height
    glOrtho(-40.0, 40.0, -40.0/aspect, 40.0/aspect, -40.0, 40.0);
  } else {
    // height is smaller, so stretch out the width
    glOrtho(-40.0*aspect, 40.0*aspect, -40.0, 40.0, -40.0, 40.0);
  }
}

void init() {
 GLfloat direction[] = { 5.0, 0.0, 0.0, 0.0 };
 GLfloat direction1[] = { -5.0, 0.0, 0.0, 0.0 };
 glMaterialf(GL_FRONT, GL_SHININESS, 50);
  glLightfv(GL_LIGHT0, GL_DIFFUSE,yellow);
    glLightfv(GL_LIGHT0, GL_POSITION, direction);
  glLightfv(GL_LIGHT1, GL_DIFFUSE,yellow);
    glLightfv(GL_LIGHT1, GL_POSITION, direction1);

  glEnable(GL_LIGHTING);                // so the renderer considers light
  glEnable(GL_LIGHT0);                  // turn LIGHT0 on
  glEnable(GL_LIGHT1);
  glEnable(GL_DEPTH_TEST);              // so the renderer considers depth
}

// The usual application statup code.
int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(600, 600);
  glutCreateWindow("Solar System Simulation");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
   glutTimerFunc(1000/10,timer,1);
  init();
  glutMainLoop();
}

No comments:

Post a Comment

 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management