Advertisement
Bedi97

Układ_Słoneczny-Piotr_Bednarek

Nov 22nd, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1. #include <windows.h>
  2. #ifdef __APPLE__
  3. #include <GLUT/glut.h>
  4. #else
  5. #include <GL/glut.h>
  6. #endif
  7.  
  8. #include <stdlib.h>
  9.  
  10. using namespace std;
  11.  
  12. static int slices = 16;
  13. static int stacks = 16;
  14. double gx=0, gy=0,gz=-10;
  15.  
  16. static void resize(int width, int height)
  17. {
  18.     const float ar = (float) width / (float) height;
  19.  
  20.     glViewport(0, 0, width, height);
  21.     glMatrixMode(GL_PROJECTION);
  22.     glLoadIdentity();
  23.     gluPerspective(90,1,2,100);
  24.  
  25.     glMatrixMode(GL_MODELVIEW);
  26.     glLoadIdentity() ;
  27. }
  28.  
  29. static void Display(void)
  30. {
  31.     const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  32.     const double a = t*90.0;
  33.  
  34.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  35.     glColor3d(1,1,0);
  36.  
  37.     glPushMatrix();     // Slonce
  38.  
  39.         glTranslated(gx,gy,gz);
  40.         glutSolidSphere(1,slices,stacks);
  41.  
  42.         glPushMatrix();     // Ziemia
  43.  
  44.             glRotated(a,0,1,0);
  45.             glTranslated(0,0,-2);
  46.             glColor3d(0,1,0);
  47.             glutSolidSphere(0.5,slices,stacks);
  48.  
  49.  
  50.             glPushMatrix();     // Ksiezyc
  51.  
  52.                 glRotated(a*2,0,1,1);
  53.                 glTranslated(0,1,-0.5);
  54.                 glColor3d(1,1,1);
  55.                 glutSolidSphere(0.2,slices,stacks);
  56.  
  57.             glPopMatrix();
  58.  
  59.         glPopMatrix();      // Koniec Ziemia
  60.  
  61.         glPushMatrix();     // Saturn + pierscienie
  62.  
  63.             glRotated(a/2,0,1,0);
  64.             glTranslated(0,0,-6);
  65.             glColor3d(0.8,0.9,1);
  66.             glutSolidSphere(1,slices,stacks);
  67.  
  68.             glRotated(a/5,1,0,0);
  69.             glScalef(1,1,0.1);
  70.             glColor3d(0,0.3,0.7);
  71.             glutSolidTorus(0.1,2,slices, stacks);
  72.             glColor3d(0.2,0,0.2);
  73.             glutSolidTorus(0.1,2.5,slices,stacks);
  74.             glColor3d(0.4,0.4,0.7);
  75.             glutSolidTorus(0.1,2.8,slices,stacks);
  76.  
  77.         glPopMatrix();  // Koniec Saturn + pierscienie
  78.  
  79.     glPopMatrix();      // Koniec Slonca
  80.  
  81.     glutSwapBuffers();
  82. }
  83.  
  84. static void mouseMove(int x, int y)
  85. {
  86.     gx = x/1900.0*22-11.;
  87.     gy = -y/1000.0*14+7.;
  88.     Display();
  89. }
  90.  
  91. static void key(unsigned char key, int x, int y)
  92. {
  93.     switch (key)
  94.     {
  95.     case 27 :
  96.     case 'q':
  97.         exit(0);
  98.         break;
  99.  
  100.     case 'e':
  101.         slices++;
  102.         stacks++;
  103.         break;
  104.  
  105.     case 'r':
  106.         if (slices>3 && stacks>3)
  107.         { slices--; stacks--; }
  108.         break;
  109.  
  110.     case 'a':
  111.         gx-=0.1;
  112.         break;
  113.  
  114.     case 'd':
  115.         gx+=0.1;
  116.         break;
  117.  
  118.     case 's':
  119.         gy-=0.1;
  120.         break;
  121.  
  122.     case 'w':
  123.         gy+=0.1;
  124.         break;
  125.     }
  126.  
  127.     glutPostRedisplay();
  128. }
  129.  
  130. static void idle(void)
  131. {
  132.     glutPostRedisplay();
  133. }
  134.  
  135. const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
  136. const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
  137. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  138. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  139.  
  140. const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
  141. const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
  142. const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
  143. const GLfloat high_shininess[] = { 100.0f };
  144.  
  145. /* Program entry point */
  146.  
  147. int main(int argc, char *argv[])
  148. {
  149.     glutInit(&argc, argv);
  150.     glutInitWindowSize(1900,1000);
  151.     glutInitWindowPosition(10,10);
  152.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  153.  
  154.     glutCreateWindow("GLUT Shapes");
  155.  
  156.     glutReshapeFunc(resize);
  157.     glutDisplayFunc(Display);
  158.     glutKeyboardFunc(key);
  159.     glutIdleFunc(idle);
  160.  
  161.  
  162.     // Wywolanie funkcji obslugujacej myszke
  163.  
  164.     glutMotionFunc(mouseMove);
  165.  
  166.  
  167.     glClearColor(0,0,0,1);
  168.     glEnable(GL_CULL_FACE);
  169.     glCullFace(GL_BACK);
  170.  
  171.     glEnable(GL_DEPTH_TEST);
  172.     glDepthFunc(GL_LESS);
  173.  
  174.     glEnable(GL_LIGHT0);
  175.     glEnable(GL_NORMALIZE);
  176.     glEnable(GL_COLOR_MATERIAL);
  177.     glEnable(GL_LIGHTING);
  178.  
  179.     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
  180.     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
  181.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  182.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  183.  
  184.     glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
  185.     glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
  186.     glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
  187.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  188.  
  189.     glutMainLoop();
  190.  
  191.     return EXIT_SUCCESS;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement