SHARE
TWEET

Untitled

a guest May 19th, 2017 36 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <GL/glut.h>
  3.  
  4. static int day = 0;
  5. static int year = 0;
  6. static int months = 0;
  7. static bool hidden = true;
  8. static float dz = 0;
  9. static float dx = 0;
  10. static float dy = 0;
  11.  
  12. void init(void)
  13. {
  14.     glClearColor(0.0,0.0,0.0,0.0);
  15.     glColor3f(1.0,1.0,1.0);
  16. }
  17.  
  18. void display(void)
  19. {
  20.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  21.     if(hidden)
  22.     glEnable(GL_DEPTH_TEST);
  23.     else
  24.     glDisable(GL_DEPTH_TEST);
  25.  
  26.     glPushMatrix();
  27.  
  28.     glEnable(GL_LIGHTING);
  29.     glEnable(GL_LIGHT0);
  30.     GLfloat lightPos[] = {dx,dy,dz,1.0};
  31.     glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  32.  
  33.     GLfloat mDiffs[] = {1.0,1.0,0.0,1.0};
  34.     glMaterialfv(GL_FRONT,GL_DIFFUSE,mDiffs);
  35.     //sun
  36.     glRotatef(year,0.0,1.0,0.0);
  37.     glColor3f(1.0, 1.0,0.0);
  38.     glutSolidSphere(1.0, 20, 16);
  39.     //earth
  40.     //glColor3f(0.0, 0.0,1.0);
  41.     GLfloat mDiffe[] = {0.0,0.0,1.0,1.0};
  42.     glMaterialfv(GL_FRONT,GL_DIFFUSE,mDiffe);
  43.     glTranslatef(2.0,0.0,0.0);
  44.     glRotatef(day,0.0,1.0,0.0);
  45.     glutSolidSphere(0.2, 10, 8);
  46.    
  47.     //moon
  48.     GLfloat mDiff[] = {1.0,1.0,1.0,1.0};
  49.     glMaterialfv(GL_FRONT,GL_DIFFUSE,mDiff);
  50.     //glColor3f(1.0, 1.0,1.0);
  51.     glRotatef(months,1.0,0.0,0.0);
  52.     glTranslatef(0.0,0.5,0.0);
  53.     glutSolidSphere(0.1, 10, 8);
  54.  
  55.     glPopMatrix();
  56.     glutSwapBuffers();
  57.  
  58. }
  59.  
  60. void reshape (int w, int h)
  61. {
  62.     glViewport (0,0, (GLsizei) w, (GLsizei)h);
  63.     glMatrixMode (GL_PROJECTION);
  64.     glLoadIdentity();
  65.     gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
  66.     glMatrixMode(GL_MODELVIEW);
  67.     glLoadIdentity();
  68.     gluLookAt(0.0,0.0,5.0,  0.0,0.0,0.0,    0.0,1.0,0.0);
  69. }
  70.  
  71.  
  72. void specialKey(int key, int x, int y)
  73.   {
  74.  
  75.        switch(key)
  76.        {
  77.      
  78.         case GLUT_KEY_UP:
  79.             dz-=0.25;
  80.             std::cout<<"X:Y:Z = "<<dx<<":"<<dy<<":"<<dz<<std::endl;
  81.             glutPostRedisplay();
  82.           break;
  83.         case GLUT_KEY_DOWN:
  84.             dz+=0.25;
  85.             std::cout<<"X:Y:Z = "<<dx<<":"<<dy<<":"<<dz<<std::endl;
  86.             glutPostRedisplay();
  87.           break;
  88.          
  89.     }
  90.   }
  91.  
  92. void keyboard(unsigned char key, int x, int y)
  93. {
  94.     switch(key)
  95.     {
  96.         case 'd':
  97.                 day = (day + 15) % 360;
  98.                 glutPostRedisplay();
  99.                 break;
  100.         case 'D':
  101.                 day = (day - 15) % 360;
  102.                 glutPostRedisplay();
  103.                 break;
  104.         case 'y':
  105.                 year = (year + 30) % 360;
  106.                 glutPostRedisplay();
  107.                 break;
  108.         case 'Y':
  109.                 year = (year - 30) % 360;
  110.                 glutPostRedisplay();
  111.                 break;
  112.  
  113.         case 'm':
  114.                 months = (months + 12) % 360;
  115.                 glutPostRedisplay();
  116.                 break;
  117.         case 'M':
  118.                 months = (months - 12) % 360;
  119.                 glutPostRedisplay();
  120.                 break;
  121.         case 'a':
  122.                 day = (day + 15) % 360;
  123.                 year = (year + 5) % 360;
  124.                 months = (months + 5) % 360;
  125.                 glutPostRedisplay();
  126.                 break;
  127.         case 'h':
  128.                 hidden = !hidden;
  129.                 glutPostRedisplay();
  130.                 break;
  131.  
  132.         case 27:
  133.                 exit(0);
  134.                 break;
  135.         default:
  136.                 break;
  137.     }
  138. }
  139.  
  140. int main(int argc, char** argv)
  141. {
  142.     glutInit(&argc, argv);
  143.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB| GLUT_DEPTH);
  144.     glutInitWindowSize(600, 600);
  145.     glutInitWindowPosition(50, 200);
  146.     glutCreateWindow("OpenGL-XI");
  147.     glutDisplayFunc(display);
  148.     glutReshapeFunc(reshape);
  149.     glutKeyboardFunc(keyboard);
  150.     glutSpecialFunc(specialKey);
  151.     init();
  152.     glutMainLoop();
  153.     return(0);
  154. }
RAW Paste Data
Challenge yourself this year...
Learn something new in 2017
Top