Advertisement
Guest User

Untitled

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