home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible / OpenGL_Superbible_Waite_Group_Press_1996.iso / book / chapt3 / auxsolid / auxsolid.c next >
C/C++ Source or Header  |  1996-03-08  |  3KB  |  151 lines

  1. // AuxSolid.c
  2. // OpenGL AUX library predefined objects
  3. // Draws solid objects.
  4.  
  5. #include <windows.h>    // Standard Windows header file
  6. #include <gl\gl.h>        // OpenGL header
  7. #include <gl\glaux.h>    // AUX library header
  8. #include <conio.h>        // Console I/O functions
  9.  
  10. // Selected object
  11. static char cSelection = ' ';
  12.  
  13.  
  14. // The window has changed size
  15. void CALLBACK ChangeSize(GLsizei w, GLsizei h)
  16.     {
  17.     // Prevent a divide by zero
  18.     if(h == 0)
  19.         h = 1;
  20.     
  21.     // Set viewport and clipping volume
  22.     glViewport(0, 0, w, h);
  23.     glLoadIdentity();
  24.  
  25.     if (w <= h) 
  26.         glOrtho (-100.0f, 100.0f, -100.0f, 100.0f*h/w, -100.0, 100.0);
  27.     else 
  28.         glOrtho (-100.0f, 100*w/h, -100.0f, 100.0f, -100.0, 100.00);
  29.  
  30.     }
  31.  
  32.  
  33. // Called by AUX library to draw the scene
  34. void CALLBACK RenderScene(void)
  35.     {
  36.     // Clear the screen
  37.     glClear(GL_COLOR_BUFFER_BIT);
  38.  
  39.     // Rotate one degree around each axis
  40.     // (Note we don't call LoadIdentity() so
  41.     // this rotation is cumulative
  42.     glRotatef(1.0f,1.0f,0.0f,0.0f);    
  43.     glRotatef(1.0f,0.0f,1.0f,0.0f);
  44.     glRotatef(1.0f,0.0f,0.0f,1.0f);
  45.  
  46.     // Draw the selected object
  47.     switch(cSelection)
  48.         {
  49.         case 'a':
  50.             auxSolidCone(30.0f,75.0f);
  51.             break;
  52.  
  53.         case 'b':
  54.             auxSolidCylinder(30.0f, 75.0f);
  55.             break;
  56.  
  57.         case 'c':
  58.             auxSolidDodecahedron(75.0f);
  59.             break;
  60.  
  61.         case 'd':
  62.             auxSolidIcosahedron(75.0f);
  63.             break;
  64.  
  65.         case 'e':
  66.             auxSolidOctahedron(75.0f);
  67.             break;
  68.  
  69.         case 'f':
  70.             auxSolidSphere(75.0f);
  71.             break;
  72.  
  73.         case 'g':
  74.             auxSolidTeapot(50.0f);
  75.             break;
  76.  
  77.         case 'h':
  78.             auxSolidTetrahedron(75.0f);
  79.             break;
  80.  
  81.         case 'i':
  82.             auxSolidTorus(20.0f, 50.0f);
  83.             break;
  84.  
  85.         case 'j':
  86.             auxSolidCube(75.0f);
  87.             break;
  88.  
  89.         case 'k':
  90.             auxSolidBox(75.0f,75.0f,75.0f);
  91.             break;
  92.         
  93.         default:
  94.             break;
  95.         }
  96.  
  97.     glFlush();
  98.  
  99.     // Swap drawing to screen
  100.     auxSwapBuffers();
  101.     }
  102.  
  103.  
  104.  
  105.  
  106. // Main program body
  107. void main(void)
  108.     {
  109.     // Display a menu of objects to draw
  110.     cprintf("Select Solid object to draw:\n\n");
  111.     cprintf("a - Cone\n");
  112.     cprintf("b - Cylinder\n");
  113.     cprintf("c - Dodecahedron\n");
  114.     cprintf("d - Icosahedron\n");
  115.     cprintf("e - Octahedron\n");
  116.     cprintf("f - Sphere\n");
  117.     cprintf("g - Teapot\n");
  118.     cprintf("h - Tetrahedron\n");
  119.     cprintf("i - Torus\n");
  120.     cprintf("j - Cube\n");
  121.     cprintf("k - Box\n");
  122.     
  123.     // Validate and accept selection
  124.     while(cSelection < 'a' || cSelection > 'k')
  125.         {    
  126.         cprintf("\nSelection: ");
  127.         cSelection = getche();
  128.         }
  129.  
  130.     // Setup the AUX library window for double buffer
  131.     auxInitDisplayMode(AUX_DOUBLE | AUX_RGBA);
  132.     auxInitPosition(100,100,250,250);
  133.     auxInitWindow("3D Aux library objects");
  134.  
  135.     // Set background to blue
  136.     glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
  137.  
  138.     // Set drawing color to Red
  139.     glColor3f(1.0f, 0.0f, 0.0f);
  140.  
  141.     // Establish window resize function
  142.     auxReshapeFunc(ChangeSize);
  143.  
  144.     // Establish idle function
  145.     auxIdleFunc(RenderScene);
  146.     
  147.     // Start main loop
  148.     auxMainLoop(RenderScene);
  149.     }
  150.  
  151.