Advertisement
CzarnyBarszcz

grafika_zadanie5

Mar 21st, 2021 (edited)
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1.  
  2. #include <windows.h>
  3. #ifdef __APPLE__
  4. #include <GLUT/glut.h>
  5. #else
  6. #include <GL/glut.h>
  7. #endif
  8.  
  9. #include <stdlib.h>
  10.  
  11. static int slices = 30;
  12. static int stacks = 30;
  13.  
  14. /* GLUT callback Handlers */
  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.     glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  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.  
  36.         //glRotated(60,1,0,0);
  37.         //glRotated(a,0,0,1);
  38.     glPushMatrix();
  39.         glColor3d(1,1,0);
  40.         glTranslated(0,0,-6);
  41.         glutSolidSphere(1.5,slices,stacks);
  42.         glPushMatrix();
  43.             glColor3d(0,0.5,1);
  44.             glRotated(a*2,0,0,1);
  45.             glTranslated(3.0,0.5,-2);
  46.             glScaled(1.0,1.03,1.0);
  47.             glRotated(a,0,0,1);
  48.             //glutSolidSphere(0.3,slices,stacks);
  49.             glutWireSphere(0.3,slices,stacks);
  50.             glRotated(a,0,0,0.1);
  51.             glColor3d(1,1.0,1);
  52.             glTranslated(0.0,0.5,0.0);
  53.             glutSolidSphere(0.1,slices,stacks);
  54.     glPopMatrix();
  55.     glPopMatrix();
  56.  
  57.     glutSwapBuffers();
  58. }
  59.  
  60.  
  61. static void key(unsigned char key, int x, int y)
  62. {
  63.     switch (key)
  64.     {
  65.         case 27 :
  66.         case 'q':
  67.             exit(0);
  68.             break;
  69.  
  70.         case '+':
  71.             slices++;
  72.             stacks++;
  73.             break;
  74.  
  75.         case '-':
  76.             if (slices>3 && stacks>3)
  77.             {
  78.                 slices--;
  79.                 stacks--;
  80.             }
  81.             break;
  82.     }
  83.  
  84.     glutPostRedisplay();
  85. }
  86.  
  87. static void idle(void)
  88. {
  89.     glutPostRedisplay();
  90. }
  91.  
  92. const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
  93. const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
  94. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  95. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  96.  
  97. const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
  98. const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
  99. const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
  100. const GLfloat high_shininess[] = { 100.0f };
  101.  
  102. /* Program entry point */
  103.  
  104. int main(int argc, char *argv[])
  105. {
  106.     glutInit(&argc, argv);
  107.     glutInitWindowSize(640,480);
  108.     glutInitWindowPosition(10,10);
  109.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  110.  
  111.     glutCreateWindow("GLUT Shapes");
  112.     glutReshapeFunc(resize);
  113.     glutDisplayFunc(display);
  114.     glutKeyboardFunc(key);
  115.     glutIdleFunc(idle);
  116.  
  117.     glClearColor(0,0,0,1);
  118.     glEnable(GL_CULL_FACE);
  119.     glCullFace(GL_BACK);
  120.  
  121.     glEnable(GL_DEPTH_TEST);
  122.     glDepthFunc(GL_LESS);
  123.  
  124.     glEnable(GL_LIGHT0);
  125.     glEnable(GL_NORMALIZE);
  126.     glEnable(GL_COLOR_MATERIAL);
  127.     glEnable(GL_LIGHTING);
  128.  
  129.     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
  130.     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
  131.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  132.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  133.  
  134.     glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
  135.     glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
  136.     glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
  137.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  138.  
  139.     glutMainLoop();
  140.  
  141.     return EXIT_SUCCESS;
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement