Advertisement
CarlosGoogles

Dona

Nov 22nd, 2017
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. /*
  2.  * GLUT Shapes Demo
  3.  *
  4.  * Written by Nigel Stewart November 2003
  5.  *
  6.  * This program is test harness for the sphere, cone
  7.  * and torus shapes in GLUT.
  8.  *
  9.  * Spinning wireframe and smooth shaded shapes are
  10.  * displayed until the ESC or q key is pressed.  The
  11.  * number of geometry stacks and slices can be adjusted
  12.  * using the + and - keys.
  13.  */
  14.  
  15. #ifdef __APPLE__
  16. #include <GLUT/glut.h>
  17. #else
  18. #include <GL/glut.h>
  19. #endif
  20.  
  21.  
  22. #define PI 3.14159265358979323846
  23.  
  24. #include <stdlib.h>
  25. #include <math.h>
  26.  
  27. #define FOR(i, a, b) for(int i=int(a); i<int(b); i++)
  28.  
  29. static int slices = 16;
  30. static int stacks = 16;
  31.  
  32. /* GLUT callback Handlers */
  33.  
  34. static void resize(int width, int height)
  35. {
  36.     const float ar = (float) width / (float) height;
  37.  
  38.     glViewport(0, 0, width, height);
  39.     glMatrixMode(GL_PROJECTION);
  40.     glLoadIdentity();
  41.     glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  42.  
  43.     glMatrixMode(GL_MODELVIEW);
  44.     glLoadIdentity() ;
  45. }
  46.  
  47. static void display(void)
  48. {
  49.     const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  50.     const double a = t*90.0;
  51.  
  52.  
  53.  
  54.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  55.     glColor3d(1,0,0);
  56.  
  57.  
  58.     glPushMatrix();
  59.         glTranslated(0,0,-6);
  60.         // glRotated(60,1,0,0);
  61.         glRotated(a,0,1,0);
  62.         glTranslated(-2.4, 1.2, 0);
  63.         //glRotated(a,0,1,0);
  64.  
  65.         int iterations = 100;
  66.         double theta = 0.0f, theta2 = 2.0 * PI / slices;
  67.         double omega = 0.0f;
  68.         double r = 1, R = 2;
  69.  
  70.         FOR(i, 0, slices) {
  71.             omega = 0;
  72.             glBegin(GL_QUAD_STRIP);
  73.             FOR(j, 0, slices) {
  74.                     glVertex3f((R + r * cos(omega)) * cos(theta), (R + r * cos(omega)) * sin(theta), r * sin(omega));
  75.                     glVertex3f((R + r * cos(omega)) * cos(theta2), (R + r * cos(omega)) * sin(theta2), r * sin(omega));
  76.                     omega += (2.0 * PI) / slices;
  77.             }
  78.             glVertex3f((R + r * cos(omega)) * cos(theta), (R + r * cos(omega)) * sin(theta), r * sin(omega));
  79.             glVertex3f((R + r * cos(omega)) * cos(theta2), (R + r * cos(omega)) * sin(theta2), r * sin(omega));
  80.             glEnd();
  81.  
  82.             theta = theta2;
  83.             theta2 += 2.0 * PI / slices;
  84.         }
  85.     glPopMatrix();
  86.  
  87.     glutSwapBuffers();
  88. }
  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 '+':
  101.             slices++;
  102.             stacks++;
  103.             break;
  104.  
  105.         case '-':
  106.             if (slices>3 && stacks>3)
  107.             {
  108.                 slices--;
  109.                 stacks--;
  110.             }
  111.             break;
  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.7f, 0.7f, 0.7f, 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. /* Program entry point */
  133.  
  134. int main(int argc, char *argv[])
  135. {
  136.     glutInit(&argc, argv);
  137.     glutInitWindowSize(640,480);
  138.     glutInitWindowPosition(10,10);
  139.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  140.  
  141.     glutCreateWindow("GLUT Shapes");
  142.  
  143.     glutReshapeFunc(resize);
  144.     glutDisplayFunc(display);
  145.     glutKeyboardFunc(key);
  146.     glutIdleFunc(idle);
  147.  
  148.     glClearColor(1,1,1,1);
  149.     glEnable(GL_CULL_FACE);
  150.     glCullFace(GL_BACK);
  151.  
  152.     glEnable(GL_DEPTH_TEST);
  153.     glDepthFunc(GL_LESS);
  154.  
  155.     glEnable(GL_LIGHT0);
  156.     glEnable(GL_NORMALIZE);
  157.     glEnable(GL_COLOR_MATERIAL);
  158.     glEnable(GL_LIGHTING);
  159.  
  160.     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
  161.     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
  162.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  163.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  164.  
  165.     glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
  166.     glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
  167.     glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
  168.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.     glutMainLoop();
  177.  
  178.     return EXIT_SUCCESS;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement