Advertisement
Karim_Gabr

Untitled

Mar 29th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <glut.h>
  3.  
  4. float angle = 0.0f;
  5.  
  6. float red = 1.0f, blue = 1.0f, green = 1.0f;
  7.  
  8. float zoom_in_out = 45.0f;
  9. float change_angle_up = 0.0;
  10.  
  11. void renderScene(void);
  12. void changeSize(int width, int height);
  13. void processSpecialKeys(int key, int x, int y);
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.     // initialize glut and create window
  18.     glutInit(&argc,argv);
  19.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  20.     glutInitWindowPosition(100, 100);
  21.     glutInitWindowSize(320, 320);
  22.     glutCreateWindow("Solar System");
  23.    
  24.     // register callback functions
  25.     glutDisplayFunc(renderScene);
  26.     glutReshapeFunc(changeSize);
  27.     glutIdleFunc(renderScene);
  28.  
  29.    
  30.     // register keyboard entry
  31.     glutSpecialFunc(processSpecialKeys);
  32.  
  33.     // enter glut event processing loop
  34.     glutMainLoop();
  35.    
  36.     return 1;
  37. }
  38.  
  39. void renderScene(void)
  40. {
  41.     // clear color and depth buffers
  42.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  43.  
  44.     // reset transformations
  45.     glLoadIdentity();
  46.  
  47.     // set camera
  48.     gluLookAt(50.0f, 50.0f, zoom_in_out,
  49.               0.0f, 0.0f, 0.0f,
  50.               0.5f, 0.5f, 0.5f);
  51.  
  52.     // draw earth
  53.     glPushMatrix();
  54.         glColor3f(0.5, 1, 0.5);
  55.         glRotatef(angle, 0.0f, 1.0f, 0.0f);
  56.         glTranslatef(0.0f, 0.0f, 50.0f);
  57.         glPushMatrix();
  58.             glColor3f(1, 1,1);
  59.             glRotatef(angle, 0.0f, 0.0f, 1.0f);
  60.             glTranslatef(0.1f,0.1f,0.0f);
  61.             glutSolidSphere(5, 50, 50);
  62.         glPopMatrix();
  63.         glutSolidSphere(5, 50, 50);
  64.     glPopMatrix();
  65.  
  66.     // draw sun
  67.     glPushMatrix();
  68.         glColor3f(1.0f, 1.0f, 0.0f);
  69.         glTranslatef(0.0f, 0.0f, 0.0f);
  70.         glutSolidSphere(10, 50, 50);
  71.     glPopMatrix();
  72.  
  73.  
  74.  
  75.     //rotate
  76.     angle += 0.1f;
  77.  
  78.     glutSwapBuffers();
  79. }
  80.  
  81. void changeSize(int width, int height)
  82. {
  83.  
  84.     // prevent division by zero when window is too short
  85.     if (height == 0) height = 1;
  86.     float ratio = 1.0* width / height;
  87.  
  88.     // use the projection matrix
  89.     glMatrixMode(GL_PROJECTION);
  90.  
  91.     // reset matrix
  92.     glLoadIdentity();
  93.  
  94.     // set the viewport to be the entire window
  95.     glViewport(0, 0, width, height);
  96.  
  97.     // set the correct perspective
  98.     gluPerspective(45, ratio, 1, 1000);
  99.  
  100.     // Get Back to the Modelview
  101.     glMatrixMode(GL_MODELVIEW);
  102. }
  103.  
  104. void processSpecialKeys(int key, int x, int y)
  105. {
  106.     switch (key)
  107.     {
  108.     case GLUT_KEY_LEFT:
  109.         break;
  110.     case GLUT_KEY_RIGHT:
  111.         break;
  112.     case GLUT_KEY_UP:
  113.         //if(zoom_in_out != 30.0f)
  114.         zoom_in_out -= 3.0f;
  115.         break;
  116.     case GLUT_KEY_DOWN:
  117.         //if (zoom_in_out != 60.0f)
  118.         zoom_in_out += 3.0f;
  119.         break;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement