Computer Graphics Project "Planet Orbit" Source Code with Glut OpenGL

Hello there !!!  This time I will post a project in the past. It is about rotation and applied to make earth orbital with moon and sun. As reminder, for you who haven’t install glut you can download it in here and follow the instruction I uploaded too. You can download this orbital project in here too.



The image above is the result of orbital project. Here is the source code for this project.

#include <windows.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>

void myinit()
{
  glClearColor(0.0, 0.0, 0.0, 1.0);
  glColor3f(1.0, 0.0, 0.0);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-400.0, 400.0, -400.0, 400.0,-400.0,400.0);
  glMatrixMode(GL_MODELVIEW);
  glShadeModel(GL_FLAT);
  glEnable(GL_DEPTH_TEST);
}
float putaransatu, putarandua = 0;
typedef struct{
  float x,y;
}
point2D_t;

void drawDot(int x,int y){
  glBegin(GL_POINTS);
    glVertex2i(x,y);
  glEnd();
}

/*void bintang(void)
{ static int tick=0;
  static point2D_t bintang[1000];
  int i;

  if(tick%100==0){
    for(i=0;i<10;i++){
      bintang[i].x=rand()%900-400;
      bintang[i].y=rand()%900-400;
  }
  glColor3f(1,1,1);
  glPointSize(4);
    for(i=0;i<30;i++)
      drawDot(bintang[i].x,bintang[i].y);
  }

}*/

void display(void)
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    putaransatu += 0.2;
    putarandua += 1;

    glPushMatrix();

  //matahari
    glColor3f(1,0.5,0.0);
    glRotated(putaransatu,0,0,1);
    glutWireSphere(65.0, 30000, 3);

  //jarak 1
    glTranslatef(-55, -55, 0);

  //jarak 2
    glTranslatef(-45, -45, 0);

  //jarak 3
    glTranslatef(-80, -80, 0);

  //bumi
  glColor3f(0.2,0.2,0.7);
    glutWireSphere(45.0, 2500, 3);


  //bulan
  glColor3f(1,1,1);
    glBegin(GL_LINE_STRIP);

    for (float x=0; x<=6.28; x+=0.001)
    {
        glVertex2f(cos(x)*60,sin(x)*60);
    }
    glEnd();

  glColor3f(1,1,0);
    glRotated(putarandua, 0, 0, 1);
    glTranslatef(40, 40, 0);
    glutWireSphere(10.0, 100, 4);

    glPopMatrix();


    glPushMatrix();

  //glEnd();

  //garis orbit
  glColor3f(1,1,1);
    glBegin(GL_LINE_STRIP);
    for (float x=0; x<=6.28; x+=0.001)
    {
        glVertex2f(cos(x)*250,sin(x)*250);
    }
    glEnd();

    glPopMatrix();
    glFlush();
    glutPostRedisplay();
}

int main(int argc, char* argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(1000,1000);
    glutInitWindowPosition(0,0);
    glutCreateWindow("planet");
    glutDisplayFunc(display);
    myinit();
   glutMainLoop();
 return 0;
}



Comments

Popular posts from this blog

Quantization of Image Data Implementation in C#

Computer Graphics Project "MINION" Source Code with Glut OpenGL