Facebook
From xxx, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 253
  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. #include <windows.h>
  15. #ifdef __APPLE__
  16. #include <GLUT/glut.h>
  17. #else
  18. #include <GL/glut.h>
  19. #endif
  20.  
  21. #include <stdlib.h>
  22.  
  23. static int slices = 16;
  24. static int stacks = 16;
  25. const GLfloat *swiatlo;
  26. /* GLUT callback Handlers */
  27.  
  28. static void resize(int width, int height)
  29. {
  30.     const float ar = (float) width / (float) height;
  31.  
  32.     glViewport(0, 0, width, height);
  33.     glMatrixMode(GL_PROJECTION);
  34.     glLoadIdentity();
  35.     glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  36.  
  37.     glMatrixMode(GL_MODELVIEW);
  38.     glLoadIdentity() ;
  39. }
  40.  
  41. static void display(void)
  42. {
  43.     const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  44.     const double a = t*90.0;
  45.  
  46.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  47.     glColor3d(1,0,0);
  48.  
  49.     //glLightfv (GL_LIGHT0, GL_POSITION, swiatlo); // ustawienie światła pod podłogą
  50.     glPushMatrix ();
  51.     glFrontFace (GL_CW); // zmiana domyślnego nawinięcia
  52.     glScalef (1.0f, -1.0f, 1.0f); // przeskalowanie położenia wszystkich aktorów
  53.  
  54.     glPushMatrix();
  55.         glTranslated(0,1,-6);
  56.         glRotated(300,1,0,0);
  57.         glRotated(a,1,1,0);
  58.         glutSolidCube(1);
  59.     glPopMatrix();
  60.  
  61.     glFrontFace (GL_CCW); // przywrócenie domyślnego nawinięcia
  62.     glPopMatrix ();
  63.     glDisable (GL_LIGHTING); // wyłączenie oświetlenia
  64.     glEnable (GL_BLEND); // włączenie mieszania kolorów
  65.     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // równanie mieszania kolorów
  66.  
  67.     glPushMatrix();
  68.     glTranslated(0,0,-7);
  69.     glRotated(10,1,0,0);
  70.     double dx = 0.5;
  71.     double dy = 0.5;
  72.  
  73.     glBegin(GL_QUADS);
  74.     for(double x=-2; x<2; x+=dx)
  75.         for(double y=-20; y<20;y+=dy)
  76.     {
  77.         if(static_cast<int> ((x+y)/dx)%2==0)
  78.             glColor4f(0,0,1,0.5);
  79.         else
  80.             glColor4f(0,1,0,0.5);
  81.             glVertex3f(x, 0, y);
  82.             glVertex3f(x, 0, y+dy);
  83.             glVertex3f(x+dx, 0, y +dy);
  84.             glVertex3f(x+dx, 0 ,y);
  85.     }
  86.     glEnd();
  87.     glPopMatrix();
  88.  
  89.     glDisable (GL_BLEND); // wyłączenie mieszania
  90.     glEnable (GL_LIGHTING); // włączenie oświetlenia
  91.     //glLightfv (GL_LIGHT0, GL_POSITION, swiatlo); // ustawienie światła nad podłogą
  92.  
  93.     glPushMatrix();
  94.         glTranslated(0,1,-6);
  95.         glRotated(60,0,1,1);
  96.         glRotated(-a,0,0,1);
  97.         glutSolidCube(1);
  98.     glPopMatrix();
  99.  
  100.     // ustalenie koloru mgły
  101.     GLfloat Kolor_mgly[] = {0.5, 0.5, 0.5, 1.0};
  102.     // ustawienia parametrów mgły
  103.     glFogfv(GL_FOG_COLOR, Kolor_mgly); // ustawienie koloru mgły
  104.     glFogf(GL_FOG_START, 10.0f); // początek mgły
  105.     glFogf(GL_FOG_END, 100.0f); // koniec mgły
  106.     glFogf(GL_FOG_DENSITY, 0.2f); // gęstość mgły
  107.     glFogf(GL_FOG_MODE, GL_EXP); // rozkład mgły
  108.     glEnable(GL_FOG); // włączeni emgły
  109.  
  110.     glutSwapBuffers();
  111. }
  112.  
  113.  
  114. static void key(unsigned char key, int x, int y)
  115. {
  116.     switch (key)
  117.     {
  118.         case 27 :
  119.         case 'q':
  120.             exit(0);
  121.             break;
  122.  
  123.         case '+':
  124.             slices++;
  125.             stacks++;
  126.             break;
  127.  
  128.         case '-':
  129.             if (slices>3 && stacks>3)
  130.             {
  131.                 slices--;
  132.                 stacks--;
  133.             }
  134.             break;
  135.     }
  136.  
  137.     glutPostRedisplay();
  138. }
  139.  
  140. static void idle(void)
  141. {
  142.     glutPostRedisplay();
  143. }
  144.  
  145. const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
  146. const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
  147. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  148. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  149.  
  150. const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
  151. const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
  152. const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
  153. const GLfloat high_shininess[] = { 100.0f };
  154.  
  155. /* Program entry point */
  156.  
  157. int main(int argc, char *argv[])
  158. {
  159.     glutInit(&argc, argv);
  160.     glutInitWindowSize(640,480);
  161.     glutInitWindowPosition(10,10);
  162.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  163.  
  164.     glutCreateWindow("GLUT Shapes");
  165.  
  166.     glutReshapeFunc(resize);
  167.     glutDisplayFunc(display);
  168.     glutKeyboardFunc(key);
  169.     glutIdleFunc(idle);
  170.  
  171.     glClearColor(1,1,1,1);
  172.     glEnable(GL_CULL_FACE);
  173.     glCullFace(GL_BACK);
  174.  
  175.     glEnable(GL_DEPTH_TEST);
  176.     glDepthFunc(GL_LESS);
  177.  
  178.     glEnable(GL_LIGHT0);
  179.     glEnable(GL_NORMALIZE);
  180.     glEnable(GL_COLOR_MATERIAL);
  181.     glEnable(GL_LIGHTING);
  182.  
  183.     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
  184.     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
  185.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  186.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  187.  
  188.     glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
  189.     glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
  190.     glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
  191.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  192.  
  193.     glutMainLoop();
  194.  
  195.     glColor4f (1.0f, 0.0f, 0.0f, 0.4f);
  196.     glDisable (GL_LIGHTING);
  197.     glEnable (GL_BLEND);
  198.     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  199.     glutSolidCube (0.3f);
  200.     glDisable (GL_BLEND);
  201.     glEnable (GL_LIGHTING);
  202.  
  203.     return EXIT_SUCCESS;
  204. }
  205.