Advertisement
Karim_Gabr

Untitled

Mar 29th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <glut.h>
  3.  
  4. float angle = 0.0f;
  5.  
  6. bool pause = false;
  7.  
  8. double zoom_in_out_x = 200.0f;
  9. double zoom_in_out_y = 200.0f;
  10. double zoom_in_out_z = 0.0f;
  11.  
  12. double upvector_x = 1.0f;
  13. double upvector_y = 0.0f;
  14. double upvector_z = 1.0f;
  15.  
  16. void renderScene(void);
  17. void changeSize(int width, int height);
  18. void processSpecialKeys(int key, int x, int y);
  19. void processNormalKeys(unsigned char key, int x, int y);
  20.  
  21. int main(int argc, char **argv)
  22. {
  23.     // initialize glut and create window
  24.     glutInit(&argc,argv);
  25.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  26.     glutInitWindowPosition(100, 100);
  27.     glutInitWindowSize(320, 320);
  28.     glutCreateWindow("Solar System");
  29.    
  30.     // register callback functions
  31.     glutDisplayFunc(renderScene);
  32.     glutReshapeFunc(changeSize);
  33.     glutIdleFunc(renderScene);
  34.  
  35.    
  36.     // register keyboard entry
  37.     glutSpecialFunc(processSpecialKeys);
  38.     glutKeyboardFunc(processNormalKeys);
  39.  
  40.     // enter glut event processing loop
  41.     glutMainLoop();
  42.    
  43.     return 1;
  44. }
  45.  
  46. void renderScene(void)
  47. {
  48.     // clear color and depth buffers
  49.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  50.  
  51.     // reset transformations
  52.     glLoadIdentity();
  53.  
  54.     // set camera initial position
  55.     gluLookAt(0.0f, zoom_in_out_y, zoom_in_out_z,
  56.               0.0f, 0.0f, 0.0f,
  57.               upvector_x, upvector_y, upvector_z);
  58.  
  59.     // draw earth
  60.     glPushMatrix();
  61.         glColor3f(0.0, 1.0, 0.0);
  62.         glRotatef(angle, 0.0f, 1.0f, 0.0f);
  63.         glTranslatef(0.0f, 0.0f, 50.0f);
  64.         glutSolidSphere(10, 50, 50);
  65.         //draw moon
  66.         glPushMatrix();
  67.             glColor3f(0.5, 0.5, 0.5);
  68.             glRotatef(angle, 0.0f, 1.0f, 0.0f);
  69.             glTranslatef(0.0f, 0.0f, 20.0f);
  70.             glutSolidSphere(5, 50, 50);
  71.         glPopMatrix();
  72.     glPopMatrix();
  73.  
  74.     // draw sun
  75.     glPushMatrix();
  76.         glColor3f(1.0f, 1.0f, 0.0f);
  77.         glTranslatef(0.0f, 0.0f, 0.0f);
  78.         glutSolidSphere(20, 50, 50);
  79.     glPopMatrix();
  80.  
  81.     //draw stars
  82.     for (int i = 1; i <= 5; i++)
  83.     {
  84.         glPushMatrix();
  85.             glColor3f(1.0f, 1.0f, 1.0f);
  86.             glTranslatef(50.0f, 50.0f, 0.0f + i * 10);
  87.             glutSolidSphere(2, 50, 50);
  88.         glPopMatrix();
  89.  
  90.         glPushMatrix();
  91.             glColor3f(1.0f, 1.0f, 1.0f);
  92.             glTranslatef(-50.0f, 50.0f, 0.0f + i * 10);
  93.             glutSolidSphere(2, 50, 50);
  94.         glPopMatrix();
  95.  
  96.         glPushMatrix();
  97.         glColor3f(1.0f, 1.0f, 1.0f);
  98.         glTranslatef(50.0f, -50.0f, 0.0f + i * 10);
  99.         glutSolidSphere(2, 50, 50);
  100.         glPopMatrix();
  101.  
  102.         glPushMatrix();
  103.         glColor3f(1.0f, 1.0f, 1.0f);
  104.         glTranslatef(50.0f, 50.0f, 0.0f - i * 10);
  105.         glutSolidSphere(2, 50, 50);
  106.         glPopMatrix();
  107.     }
  108.  
  109.     //rotate
  110.     if(pause == false)
  111.         angle += 0.1f;
  112.  
  113.     glutSwapBuffers();
  114. }
  115.  
  116. void changeSize(int width, int height)
  117. {
  118.  
  119.     // prevent division by zero when window is too short
  120.     if (height == 0) height = 1;
  121.     float ratio = 1.0* width / height;
  122.  
  123.     // use the projection matrix
  124.     glMatrixMode(GL_PROJECTION);
  125.  
  126.     // reset matrix
  127.     glLoadIdentity();
  128.  
  129.     // set the viewport to be the entire window
  130.     glViewport(0, 0, width, height);
  131.  
  132.     // set the correct perspective
  133.     gluPerspective(45, ratio, 1, 1000);
  134.  
  135.     // Get Back to the Modelview
  136.     glMatrixMode(GL_MODELVIEW);
  137. }
  138.  
  139. void processNormalKeys(unsigned char key, int x, int y)
  140. {
  141.     switch (key)
  142.     {
  143.         // press esc to exit
  144.         case 27:
  145.             exit(0);break;
  146.         // press space to pause rotation
  147.         case 32:
  148.             pause = !pause; break;
  149.         // press + to zoom in
  150.         case 43:
  151.             zoom_in_out_y -= 5; break;
  152.         // press - to zoom out
  153.         case 45:
  154.             zoom_in_out_y += 5; break;
  155.         default:break;
  156.     }
  157. }
  158.  
  159. void processSpecialKeys(int key, int x, int y)
  160. {
  161.     switch (key)
  162.     {
  163.         // press left key to rotate camera angle left
  164.         case GLUT_KEY_LEFT:
  165.             upvector_x -= 0.1f;
  166.             upvector_y += 0.1f;
  167.             upvector_z += 0.1f;
  168.             zoom_in_out_x += 20;
  169.             zoom_in_out_z -= 20;
  170.             break;
  171.         // press right key to rotate camera angle right
  172.         case GLUT_KEY_RIGHT:
  173.             upvector_x += 0.1f;
  174.             upvector_y -= 0.1f;
  175.             upvector_z -= 0.1f;
  176.             zoom_in_out_x -= 20;
  177.             zoom_in_out_z += 20;
  178.             break;
  179.         // press up key to rotate camera angle up
  180.         case GLUT_KEY_UP:
  181.             upvector_x += 0.1f;
  182.             upvector_y -= 0.1f;
  183.             upvector_z += 0.1f;
  184.             zoom_in_out_y += 20;
  185.             zoom_in_out_z -= 20;
  186.             break;
  187.         // press down key to rotate camera angle down
  188.         case GLUT_KEY_DOWN:
  189.             upvector_x -= 0.1f;
  190.             upvector_y += 0.1f;
  191.             upvector_z -= 0.1f;
  192.             zoom_in_out_y -= 20;
  193.             zoom_in_out_z += 20;
  194.             break;
  195.         default:break;
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement