Advertisement
Guest User

glucik

a guest
May 21st, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 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. #include <stdlib.h>
  8. using namespace std;
  9. static int slices = 16;
  10. static int stacks = 16;
  11.  
  12. /* GLUT callback Handlers */
  13.  
  14. static void resize(int width, int height)
  15. {
  16.     const float ar = (float) width / (float) height;
  17.  
  18.     glViewport(0, 0, width, height);
  19.     glMatrixMode(GL_PROJECTION);
  20.     glLoadIdentity();
  21.     glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  22.  
  23.     glMatrixMode(GL_MODELVIEW);
  24.     glLoadIdentity() ;
  25. }
  26.  
  27. static void display(void)
  28. {
  29.     const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  30.     const double a = t*90.0;
  31.     const GLfloat fPozycja_swiatla_pod[3] = {0,0,-7};
  32.     const GLfloat fPozycja_swiatla[3] = {0,0,7};
  33.  
  34.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  35.     glColor3d(1,0,0);
  36.  
  37.      glLightfv (GL_LIGHT0, GL_POSITION, fPozycja_swiatla_pod); // ustawienie światła pod podłogą
  38.      glPushMatrix ();
  39.      glFrontFace (GL_CW); // zmiana domyślnego nawinięcia
  40.      glScalef (1.0f, 1.0f, 1.0f); // przeskalowanie położenia wszystkich aktorów
  41.      glPushMatrix();
  42.         glTranslated(0,-2,-12);
  43.         glRotated(180,1,0,0);
  44.         glRotated(a,0,0,1);
  45.         //glutSolidTorus(0.25,2,slices,stacks);
  46.         glutSolidTeapot(1.25);
  47.      glPopMatrix();
  48.      glFrontFace (GL_CCW); // przywrócenie domyślnego nawinięcia
  49.      glPopMatrix ();
  50.  
  51.     /*
  52.     glPushMatrix();
  53.         glTranslated(0,3.1,-12);
  54.         glRotated(0,1,0,0);
  55.         glRotated(a,0,0,1);
  56.         glutSolidTorus(0.25,2,slices,stacks);
  57.     glPopMatrix();
  58.  
  59.     glPushMatrix();
  60.         glTranslated(0,-1.9,-12);
  61.         glRotated(180,1,0,0);
  62.         glRotated(a,0,0,1);
  63.         glutSolidTorus(0.25,2,slices,stacks);
  64.     glPopMatrix();
  65. */
  66.  
  67.     glDisable (GL_LIGHTING); // wyłączenie oświetlenia
  68.     glEnable (GL_BLEND); // włączenie mieszania kolorów
  69.     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // równanie mieszania kolorów
  70.  
  71.     glPushMatrix();
  72.         glTranslated(7,0,-7);
  73.         glRotated(10,1,0,0);
  74.         double dx=0.5;
  75.         double dy=0.5;
  76.         glBegin(GL_QUADS);
  77.         for(double x=-20;x<2;x+=dx){
  78.             for(double y=-20;y<20;y+=dy){
  79.                 if(static_cast<int>((x+y)/dx)%2==0)
  80.                     glColor4f(0,0,1,0.5);
  81.                 else
  82.                     glColor4f(0,1,0,0.5);
  83.                     glVertex3f(x,0,y);
  84.                     glVertex3f(x,0,y+dy);
  85.                     glVertex3f(x+dx,0,y+dy);
  86.                     glVertex3f(x+dx,0,y);
  87.             }
  88.         }
  89.         glEnd();
  90.     glPopMatrix();
  91.  
  92.     glDisable (GL_BLEND); // wyłączenie mieszania
  93.     glEnable (GL_LIGHTING); // włączenie oświetlenia
  94.     glLightfv (GL_LIGHT0, GL_POSITION, fPozycja_swiatla); // ustawienie światła nad podłogą
  95.  
  96.     glPushMatrix();
  97.         glColor3f(1,0,0);
  98.         glTranslated(0,3.1,-12);
  99.         glRotated(0,1,0,0);
  100.         glRotated(a,0,0,1);
  101.         //glutSolidTorus(0.25,2,slices,stacks);
  102.         glutSolidTeapot(1.25);
  103.     glPopMatrix();
  104.     glutSwapBuffers();
  105. }
  106.  
  107.  
  108.  
  109. static void key(unsigned char key, int x, int y)
  110. {
  111.     switch (key)
  112.     {
  113.         case 27 :
  114.         case 'q':
  115.             exit(0);
  116.             break;
  117.  
  118.         case '+':
  119.             slices++;
  120.             stacks++;
  121.             break;
  122.  
  123.         case '-':
  124.             if (slices>3 && stacks>3)
  125.             {
  126.                 slices--;
  127.                 stacks--;
  128.             }
  129.             break;
  130.     }
  131.  
  132.     glutPostRedisplay();
  133. }
  134.  
  135. static void idle(void)
  136. {
  137.     glutPostRedisplay();
  138. }
  139.  
  140. const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
  141. const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
  142. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  143. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  144.  
  145. const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
  146. const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
  147. const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
  148. const GLfloat high_shininess[] = { 100.0f };
  149.  
  150. /* Program entry point */
  151.  
  152. int main(int argc, char *argv[])
  153. {
  154.     glutInit(&argc, argv);
  155.     glutInitWindowSize(1280,720);
  156.     glutInitWindowPosition(10,10);
  157.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  158.  
  159.     glutCreateWindow("GLUT Shapes");
  160.  
  161.     glutReshapeFunc(resize);
  162.     glutDisplayFunc(display);
  163.     glutKeyboardFunc(key);
  164.     glutIdleFunc(idle);
  165.  
  166.  
  167.  
  168.     glClearColor(1,1,1,1);
  169.     glEnable(GL_CULL_FACE);
  170.     glCullFace(GL_BACK);
  171.  
  172.     glEnable(GL_DEPTH_TEST);
  173.     glDepthFunc(GL_LESS);
  174.  
  175.     glEnable(GL_LIGHT0);
  176.     glEnable(GL_NORMALIZE);
  177.     glEnable(GL_COLOR_MATERIAL);
  178.     glEnable(GL_LIGHTING);
  179.  
  180.     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
  181.     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
  182.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  183.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  184.  
  185.     glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
  186.     glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
  187.     glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
  188.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  189.  
  190.     glutMainLoop();
  191.  
  192.     return EXIT_SUCCESS;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement