home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible / OpenGL_Superbible_Waite_Group_Press_1996.iso / book / chapt10 / tank / microsoft / renderworld.c < prev    next >
C/C++ Source or Header  |  1996-07-07  |  18KB  |  699 lines

  1. // RenderWorld.c
  2. // This file actually renders the world
  3.  
  4. #include <windows.h>    // Normal Windows stuff
  5. #include <math.h>
  6. #include <gl/gl.h>        // Core OpenGL functions
  7. #include <gl/glu.h>        // OpenGL Utility functions
  8. #include <gl/glaux.h>
  9. #include "externs.h"    // Data shared between files
  10. #include "glutils.h"    // Utilities for this application
  11.  
  12.  
  13. // Prevent anoying compiler warnings - only used locally
  14. #define fsin(x)    (float)sin(x)
  15. #define fcos(x) (float)cos(x)
  16.  
  17. // Useful structure for storing coordinates of objects. We declare
  18. // an array of these to hold the positions of the various types of objects
  19. struct COORD
  20.     {
  21.     float x;
  22.     float z;
  23.     };
  24.  
  25.  
  26. // Maximum number of each type of object
  27. #define MAX_EACH    15
  28.  
  29. // Arrays of objects
  30. struct COORD Pyramids[MAX_EACH];
  31. struct COORD Slabs[MAX_EACH];
  32. struct COORD Pillers[MAX_EACH];
  33.  
  34.  
  35.  
  36. void RandomizePositions(void);
  37. float RandomPos(void);
  38. void PlacePyramids(void);
  39. void PlaceSlabs(void);
  40. void PlacePillers(void);
  41. void DrawGround(void);
  42. void DrawTank(void);
  43. void DrawRobot(void);
  44.  
  45. ///////////////////////////////////////////////////////////////////////////////
  46. // Pick a random value between -1000.0 and 1000.0
  47. float RandomPos(void)
  48.     {
  49.     double dRet = 0.0f;
  50.  
  51.     dRet = (double)rand();            // between 0 and RAND_MAX
  52.     dRet = dRet/(double)RAND_MAX;    // between 0 and 1.0
  53.     dRet *= 2000.0f;                // between 0 and 2000
  54.     dRet -= 1000.0f;                // between -1000 and 1000
  55.  
  56.     return (float)dRet;
  57.     }
  58.  
  59.  
  60.  
  61. ///////////////////////////////////////////////////////////////////////////////
  62. // Position the objects in the scene
  63. void RandomizePositions()
  64.     {
  65.     int i;
  66.  
  67.     // Pick two random values for x and z coordinates
  68.     for(i = 0; i < MAX_EACH; i++)
  69.         {
  70.         Pyramids[i].x = RandomPos();
  71.         Pyramids[i].z = RandomPos();
  72.  
  73.         Slabs[i].x = RandomPos();
  74.         Slabs[i].z = RandomPos();
  75.  
  76.         Pillers[i].x = RandomPos();
  77.         Pillers[i].z = RandomPos();
  78.         }
  79.     }
  80.  
  81.  
  82. ////////////////////////////////////////////////////////////////////////////////
  83. // Put the Pyramids in place Draws each one individually
  84. void PlacePyramids(void)
  85.     {
  86.     int i;
  87.  
  88.     for(i = 0; i < MAX_EACH; i++)
  89.         {
  90.         glPushMatrix();
  91.             glTranslatef(Pyramids[i].x, 0.0f, Pyramids[i].z);
  92.             DrawPyramid();
  93.         glPopMatrix();
  94.         }
  95.     }
  96.  
  97.  
  98.  
  99. ///////////////////////////////////////////////////////////////////////////////
  100. // Put the slabs in place
  101. void PlaceSlabs(void)
  102.     {
  103.     int i;
  104.  
  105.     // Loop through, and place each one
  106.     for(i = 0; i < MAX_EACH; i++)
  107.         {
  108.         glPushMatrix();
  109.             glTranslatef(Slabs[i].x, 21.0f, Slabs[i].z);
  110.             DrawSlab();
  111.         glPopMatrix();
  112.         }
  113.     }
  114.  
  115.  
  116.  
  117. ////////////////////////////////////////////////////////////////////////////////
  118. // Put the Pillers in place
  119. void PlacePillers(void)
  120.     {
  121.     int i;
  122.     // Place each Piller
  123.     for(i = 0; i < MAX_EACH; i++)
  124.         {
  125.         glPushMatrix();
  126.             glTranslatef(Pillers[i].x, 23.5f, Pillers[i].z);
  127.             DrawPiller();
  128.         glPopMatrix();
  129.         }
  130.     }
  131.  
  132.  
  133.  
  134.  
  135. ////////////////////////////////////////////////////////////////////////////////
  136. // Render the entire scene
  137. void RenderWorld(void)
  138.     {
  139.     glCallList(nWorldList);
  140.  
  141.     // Pillers not place in display list because of auxSolidBox problems
  142.     PlacePillers();
  143.  
  144.     // Draw the robot or the tank. If we draw both, our view will be from
  145.     // the inside of whichever one we are.
  146.     if(bTank)
  147.         DrawRobot();
  148.     else
  149.         DrawTank();
  150.     }
  151.  
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. /// Objects contained in the scene
  155.  
  156.  
  157. /////////////////////////////////////////////////////////////////////////////////
  158. // Draw the ground just as a grid of lines. No transformation to take place
  159. void DrawGround(void)
  160.     {
  161.     int r,c;
  162.     int nStep = 30;
  163.  
  164.     // Draw Ground in Green
  165.     glRGB(0,255,0);
  166.  
  167.     // The ground has a normal too
  168.     glNormal3f(0.0f, 1.0f, 0.0f);
  169.  
  170.     // Just draw a bunch of horizontal and vertical lines
  171.     glBegin(GL_LINES);
  172.  
  173.     for(r = -1000; r  <= 1000; r += nStep)
  174.         {
  175.         glVertex3f((float)r, 0.0f, -1000.0f);
  176.         glVertex3f((float)r, 0.0f, 1000.0f);
  177.         }
  178.  
  179.     for(c = -1000; c <= 1000; c += nStep)
  180.         {
  181.         glVertex3f(1000.0f, 0.0f, (float)c);
  182.         glVertex3f(-1000.0f, 0.0f, (float)c);
  183.         }
  184.  
  185.     glEnd();
  186.     }
  187.  
  188.  
  189.  
  190.  
  191. ////////////////////////////////////////////////////////////////////////////
  192. // A Piller or column
  193. void DrawPiller()
  194.     {
  195.     GLfloat vNormal[3];
  196.  
  197.     GLfloat x,z,angle;  // Storage for coordinates and angles
  198.     GLfloat fStep = (float)PI/4.0f;
  199.  
  200.     GLfloat fHeight = 20.0f;
  201.     GLfloat fRadius = 7.0f;
  202.  
  203.     // Grey
  204.     glRGB(128,128,128);
  205.  
  206.     // Normals are easy for cylinders
  207.     glBegin(GL_QUAD_STRIP);
  208.         vNormal[0] = fRadius;
  209.         vNormal[1] = fHeight;
  210.         vNormal[2] = 0.0f;
  211.         ReduceToUnit(vNormal);
  212.  
  213.         glNormal3fv(vNormal);
  214.         glVertex3f(fRadius, fHeight, 0.0f);
  215.         glVertex3f(fRadius, -fHeight, 0.0f);
  216.  
  217.     for(angle = fStep; angle < 3.0f*PI; angle += fStep)
  218.         {
  219.         x = fRadius*fsin(angle);
  220.         z = fRadius*fcos(angle);
  221.  
  222.         vNormal[0] = x;
  223.         vNormal[1] = fHeight;
  224.         vNormal[2] = z;
  225.         ReduceToUnit(vNormal);
  226.  
  227.         glNormal3fv(vNormal);
  228.         glVertex3f(x, fHeight, z );
  229.         glVertex3f(x, -fHeight, z);
  230.         }
  231.  
  232.     glEnd();
  233.  
  234.     // Draw the box at both ends
  235.     glPushMatrix();
  236.     glTranslatef(0.0f, -fHeight-(fRadius/4), 0.0f);
  237.     auxSolidBox(fRadius*2.5, fRadius/2, fRadius*2.5);
  238.     glTranslatef(0.0f, (fHeight+(fRadius/4))*2, 0.0f);
  239.     auxSolidBox(fRadius*2.5, fRadius/2, fRadius*2.5);
  240.     glPopMatrix();
  241.     }
  242.  
  243.  
  244. ///////////////////////////////////////////////////////////////////////////////
  245. // Draw a pyramid
  246. void DrawPyramid(void)
  247.     {
  248.     GLfloat vNormal[3];
  249.     GLfloat vvVertices[3][3];
  250.  
  251.     GLfloat fWidth = 15.0f;
  252.     GLfloat fHeight = 30.0f;
  253.  
  254.  
  255.     // Red Pyramids
  256.     glRGB(255,0,0);
  257.  
  258.  
  259.     // Simple, build with four triangles.
  260.     glBegin(GL_TRIANGLES);
  261.         vvVertices[0][0] = fWidth;
  262.         vvVertices[0][1] = 0.0f;
  263.         vvVertices[0][2] = fWidth;
  264.  
  265.         vvVertices[1][0] = fWidth;
  266.         vvVertices[1][1] = 0.0f;
  267.         vvVertices[1][2] = -fWidth;
  268.  
  269.         vvVertices[0][0] = 0.0f;
  270.         vvVertices[0][1] = fHeight;
  271.         vvVertices[0][2] = 0.0f;
  272.         calcNormal(vvVertices, vNormal);
  273.         
  274.         glNormal3fv(vNormal);
  275.         glVertex3f(fWidth, 0.0f, fWidth);
  276.         glVertex3f(fWidth, 0.0f, -fWidth);
  277.         glVertex3f(0.0f, fHeight, 0.0f);
  278.  
  279.  
  280.         vvVertices[0][0] = fWidth;
  281.         vvVertices[0][1] = 0.0f;
  282.         vvVertices[0][2] = fWidth;
  283.  
  284.         vvVertices[1][0] = 0.0f;
  285.         vvVertices[1][1] = fHeight;
  286.         vvVertices[1][2] = 0.0f;
  287.  
  288.         vvVertices[0][0] = -fWidth;
  289.         vvVertices[0][1] = 0.0f;
  290.         vvVertices[0][2] = fWidth;
  291.         calcNormal(vvVertices, vNormal);
  292.  
  293.         glNormal3fv(vNormal);
  294.         glVertex3f(fWidth, 0.0f, fWidth);
  295.         glVertex3f(0.0f, fHeight, 0.0f);
  296.         glVertex3f(-fWidth, 0.0f, fWidth);
  297.  
  298.         vvVertices[0][0] = -fWidth;
  299.         vvVertices[0][1] = 0.0f;
  300.         vvVertices[0][2] = fWidth;
  301.  
  302.         vvVertices[1][0] = 0.0f;
  303.         vvVertices[1][1] = fHeight;
  304.         vvVertices[1][2] = 0.0f;
  305.  
  306.         vvVertices[0][0] = -fWidth;
  307.         vvVertices[0][1] = 0.0f;
  308.         vvVertices[0][2] = -fWidth;
  309.         calcNormal(vvVertices, vNormal);
  310.  
  311.         glNormal3fv(vNormal);
  312.         glVertex3f(-fWidth, 0.0f, fWidth);
  313.         glVertex3f(0.0f, fHeight, 0.0f);
  314.         glVertex3f(-fWidth, 0.0f, -fWidth);
  315.  
  316.  
  317.         vvVertices[0][0] = -fWidth;
  318.         vvVertices[0][1] = 0.0f;
  319.         vvVertices[0][2] = -fWidth;
  320.  
  321.         vvVertices[1][0] = 0.0f;
  322.         vvVertices[1][1] = fHeight;
  323.         vvVertices[1][2] = 0.0f;
  324.  
  325.         vvVertices[0][0] = fWidth;
  326.         vvVertices[0][1] = 0.0f;
  327.         vvVertices[0][2] = -fWidth;
  328.         calcNormal(vvVertices, vNormal);
  329.  
  330.         glNormal3fv(vNormal);
  331.         glVertex3f(-fWidth, 0.0f, -fWidth);
  332.         glVertex3f(0.0f, fHeight, 0.0f);
  333.         glVertex3f(fWidth, 0.0f, -fWidth);
  334.     glEnd();
  335.     }
  336.  
  337.  
  338. ///////////////////////////////////////////////////////////////////////////////
  339. // Draw the Slabs
  340. void DrawSlab()
  341.     {
  342.     GLfloat fWidth = 12.0f;
  343.     GLfloat fHeight = 20.0f;
  344.     GLfloat fDepth = 7.0f;
  345.     GLfloat fEdge = 1.0f;
  346.     GLfloat vNormal[3];
  347.     GLfloat vvVertices[3][3];
  348.  
  349.     // Blue Slabs
  350.     glRGB(0,0, 255);
  351.  
  352.     // Simple, build with four triangles.
  353.     glBegin(GL_QUADS);
  354.         // Front
  355.         glNormal3f(0.0f, 0.0f, -1.0f);
  356.         glVertex3f(fWidth, fHeight, -fDepth);
  357.         glVertex3f(fWidth, -fHeight, -fDepth);
  358.         glVertex3f(-fWidth, -fHeight, -fDepth);
  359.         glVertex3f(-fWidth, fHeight, -fDepth);
  360.  
  361.         // Back
  362.         glNormal3f(0.0f, 0.0f, 1.0f);
  363.         glVertex3f(-fWidth, fHeight, fDepth);
  364.         glVertex3f(-fWidth, -fHeight, fDepth);
  365.         glVertex3f(fWidth, -fHeight, fDepth);
  366.         glVertex3f(fWidth, fHeight, fDepth);
  367.  
  368.         // Top
  369.         glNormal3f(0.0f, 1.0f, 0.0f);
  370.         glVertex3f(fWidth-fEdge, fHeight+fEdge, fDepth-fEdge);
  371.         glVertex3f(fWidth-fEdge, fHeight+fEdge, -(fDepth-fEdge));
  372.         glVertex3f(-(fWidth-fEdge), fHeight+fEdge, -(fDepth-fEdge));
  373.         glVertex3f(-(fWidth-fEdge), fHeight+fEdge, fDepth-fEdge);
  374.  
  375.         // Bottom
  376.         glNormal3f(0.0f, -1.0f, 0.0f);
  377.         glVertex3f(-(fWidth-fEdge), -(fHeight+fEdge), fDepth-fEdge);
  378.         glVertex3f(-(fWidth-fEdge), -(fHeight+fEdge), -(fDepth-fEdge));
  379.         glVertex3f(fWidth-fEdge, -(fHeight+fEdge), -(fDepth-fEdge));
  380.         glVertex3f(fWidth-fEdge, -(fHeight+fEdge), fDepth-fEdge);
  381.  
  382.         // Left
  383.         glNormal3f(1.0f, 0.0f, 0.0f);
  384.         glVertex3f(fWidth, fHeight, fDepth);
  385.         glVertex3f(fWidth, -fHeight, fDepth);
  386.         glVertex3f(fWidth, -fHeight, -fDepth);
  387.         glVertex3f(fWidth, fHeight, -fDepth);
  388.  
  389.         // Right
  390.         glNormal3f(-1.0f, 0.0f, 0.0f);
  391.         glVertex3f(-fWidth, fHeight, fDepth);
  392.         glVertex3f(-fWidth, fHeight, -fDepth);
  393.         glVertex3f(-fWidth, -fHeight, -fDepth);
  394.         glVertex3f(-fWidth, -fHeight, fDepth);
  395.  
  396.         // Top Bevels
  397.         vvVertices[0][0] = fWidth-fEdge;
  398.         vvVertices[0][1] = fHeight+fEdge;
  399.         vvVertices[0][2] = fDepth-fEdge;
  400.  
  401.         vvVertices[1][0] = -(fWidth-fEdge);
  402.         vvVertices[1][1] = fHeight+fEdge;
  403.         vvVertices[1][2] = fDepth-fEdge;
  404.  
  405.         vvVertices[2][0] = -fWidth;
  406.         vvVertices[2][1] = fHeight;
  407.         vvVertices[2][2] = fDepth;
  408.         calcNormal(vvVertices,vNormal);
  409.  
  410.         glNormal3fv(vNormal);
  411.         glVertex3f(fWidth-fEdge, fHeight+fEdge, fDepth-fEdge);
  412.         glVertex3f(-(fWidth-fEdge), fHeight+fEdge, fDepth-fEdge);
  413.         glVertex3f(-fWidth, fHeight, fDepth);
  414.         glVertex3f(fWidth, fHeight, fDepth);
  415.  
  416.  
  417.         vvVertices[0][0] = fWidth;
  418.         vvVertices[0][1] = fHeight;
  419.         vvVertices[0][2] = -fDepth;
  420.  
  421.         vvVertices[1][0] = fWidth-fEdge;
  422.         vvVertices[1][1] = fHeight+fEdge;
  423.         vvVertices[1][2] = -(fDepth-fEdge);
  424.  
  425.         vvVertices[2][0] = fWidth-fEdge;
  426.         vvVertices[2][1] = fHeight+fEdge;
  427.         vvVertices[2][2] = fDepth-fEdge;
  428.         calcNormal(vvVertices,vNormal);
  429.  
  430.         glNormal3fv(vNormal);
  431.         glVertex3f(fWidth, fHeight, -fDepth);
  432.         glVertex3f(fWidth-fEdge, fHeight+fEdge, -(fDepth-fEdge));
  433.         glVertex3f(fWidth-fEdge, fHeight+fEdge, fDepth-fEdge);
  434.         glVertex3f(fWidth, fHeight, fDepth);
  435.  
  436.         vvVertices[0][0] = fWidth;
  437.         vvVertices[0][1] = fHeight;
  438.         vvVertices[0][2] = -fDepth;
  439.  
  440.         vvVertices[1][0] = -fWidth;
  441.         vvVertices[1][1] = fHeight;
  442.         vvVertices[1][2] = -fDepth;
  443.  
  444.         vvVertices[2][0] = -(fWidth-fEdge);
  445.         vvVertices[2][1] = fHeight+fEdge;
  446.         vvVertices[2][2] = -(fDepth-fEdge);
  447.         calcNormal(vvVertices,vNormal);
  448.  
  449.         glNormal3fv(vNormal);
  450.         glVertex3f(fWidth, fHeight, -fDepth);
  451.         glVertex3f(-fWidth, fHeight, -fDepth);
  452.         glVertex3f(-(fWidth-fEdge), fHeight+fEdge, -(fDepth-fEdge));
  453.         glVertex3f(fWidth-fEdge, fHeight+fEdge, -(fDepth-fEdge));
  454.  
  455.  
  456.         vvVertices[0][0] = -fWidth;
  457.         vvVertices[0][1] = fHeight;
  458.         vvVertices[0][2] = fDepth;
  459.  
  460.         vvVertices[1][0] = -(fWidth-fEdge);
  461.         vvVertices[1][1] = fHeight+fEdge;
  462.         vvVertices[1][2] = fDepth-fEdge;
  463.  
  464.         vvVertices[2][0] = -(fWidth-fEdge);
  465.         vvVertices[2][1] = fHeight+fEdge;
  466.         vvVertices[2][2] = -(fDepth-fEdge);
  467.         calcNormal(vvVertices,vNormal);
  468.  
  469.         glNormal3fv(vNormal);
  470.         glVertex3f(-fWidth, fHeight, fDepth);
  471.         glVertex3f(-(fWidth-fEdge), fHeight+fEdge, fDepth-fEdge);
  472.         glVertex3f(-(fWidth-fEdge), fHeight+fEdge, -(fDepth-fEdge));
  473.         glVertex3f(-fWidth, fHeight, -fDepth);
  474.  
  475.  
  476.         // Bottom Bevels
  477.         vvVertices[0][0] = fWidth;
  478.         vvVertices[0][1] = -fHeight;
  479.         vvVertices[0][2] = fDepth;
  480.  
  481.         vvVertices[1][0] = -fWidth;
  482.         vvVertices[1][1] = -fHeight;
  483.         vvVertices[1][2] = fDepth;
  484.  
  485.         vvVertices[2][0] = -(fWidth-fEdge);
  486.         vvVertices[2][1] = -(fHeight+fEdge);
  487.         vvVertices[2][2] = fDepth-fEdge;
  488.         calcNormal(vvVertices,vNormal);
  489.  
  490.         glNormal3fv(vNormal);
  491.         glVertex3f(fWidth, -fHeight, fDepth);
  492.         glVertex3f(-fWidth, -fHeight, fDepth);
  493.         glVertex3f(-(fWidth-fEdge), -(fHeight+fEdge), fDepth-fEdge);
  494.         glVertex3f(fWidth-fEdge, -(fHeight+fEdge), fDepth-fEdge);
  495.  
  496.  
  497.         vvVertices[0][0] = fWidth;
  498.         vvVertices[0][1] = -fHeight;
  499.         vvVertices[0][2] = fDepth;
  500.  
  501.         vvVertices[1][0] = fWidth-fEdge;
  502.         vvVertices[1][1] = -(fHeight+fEdge);
  503.         vvVertices[1][2] = fDepth-fEdge;
  504.  
  505.         vvVertices[2][0] = fWidth-fEdge;
  506.         vvVertices[2][1] = -(fHeight+fEdge);
  507.         vvVertices[2][2] = -(fDepth-fEdge);
  508.         calcNormal(vvVertices,vNormal);
  509.  
  510.         glNormal3fv(vNormal);
  511.         glVertex3f(fWidth, -fHeight, fDepth);
  512.         glVertex3f(fWidth-fEdge, -(fHeight+fEdge), fDepth-fEdge);
  513.         glVertex3f(fWidth-fEdge, -(fHeight+fEdge), -(fDepth-fEdge));
  514.         glVertex3f(fWidth, -fHeight, -fDepth);
  515.                 
  516.  
  517.         vvVertices[0][0] = fWidth-fEdge;
  518.         vvVertices[0][1] = -(fHeight+fEdge);
  519.         vvVertices[0][2] = -(fDepth-fEdge);
  520.  
  521.         vvVertices[1][0] = -(fWidth-fEdge);
  522.         vvVertices[1][1] = -(fHeight+fEdge);
  523.         vvVertices[1][2] = -(fDepth-fEdge);
  524.  
  525.         vvVertices[2][0] = -fWidth;
  526.         vvVertices[2][1] = -fHeight;
  527.         vvVertices[2][2] = -fDepth;
  528.         calcNormal(vvVertices,vNormal);
  529.  
  530.         glNormal3fv(vNormal);
  531.         glVertex3f(fWidth-fEdge, -(fHeight+fEdge), -(fDepth-fEdge));
  532.         glVertex3f(-(fWidth-fEdge), -(fHeight+fEdge), -(fDepth-fEdge));
  533.         glVertex3f(-fWidth, -fHeight, -fDepth);
  534.         glVertex3f(fWidth, -fHeight, -fDepth);
  535.  
  536.  
  537.         vvVertices[0][0] = -fWidth;
  538.         vvVertices[0][1] = -fHeight;
  539.         vvVertices[0][2] = -fDepth;
  540.  
  541.         vvVertices[1][0] = -(fWidth-fEdge);
  542.         vvVertices[1][1] = -(fHeight+fEdge);
  543.         vvVertices[1][2] = -(fDepth-fEdge);
  544.  
  545.         vvVertices[2][0] = -(fWidth-fEdge);
  546.         vvVertices[2][1] = -(fHeight+fEdge);
  547.         vvVertices[2][2] = fDepth-fEdge;
  548.         calcNormal(vvVertices,vNormal);
  549.  
  550.         glNormal3fv(vNormal);
  551.         glVertex3f(-fWidth, -fHeight, -fDepth);
  552.         glVertex3f(-(fWidth-fEdge), -(fHeight+fEdge), -(fDepth-fEdge));
  553.         glVertex3f(-(fWidth-fEdge), -(fHeight+fEdge), fDepth-fEdge);
  554.         glVertex3f(-fWidth, -fHeight, fDepth);
  555.     glEnd();
  556.     }
  557.  
  558.  
  559. ///////////////////////////////////////////////////////////////////////////////
  560. // Draw the Wheel (actually just a disk)
  561. void DrawWheel()
  562.     {
  563.     GLfloat x,y,z,angle;  // Storage for coordinates and angles
  564.     GLfloat fStep = (float)PI/4.0f;
  565.     GLfloat fWidth = 3.0f;
  566.     GLfloat fSize = 10.0f;
  567.     GLfloat vNormal[3];
  568.  
  569.     // Dark Grey Wheels
  570.     glRGB(198, 198, 198);
  571.  
  572.     // Center of fan is at the origin
  573.     for(z = 0.0f; z <= fWidth; z+= fWidth)
  574.         {
  575.         if(z > 0.0)
  576.             {
  577.             glFrontFace(GL_CW);    // Switch to clock wise for back
  578.             glNormal3f(0.0f, 0.0f, 1.0f);
  579.             }
  580.         else
  581.             glNormal3f(0.0f, 0.0f, -1.0f);
  582.  
  583.         // Begin a new triangle fan to cover the bottom
  584.         glBegin(GL_TRIANGLE_FAN);
  585.  
  586.         glVertex3f(0.0f, 0.0f, z);
  587.         for(angle = 0.0f; angle < (2.0f*(float)PI); angle += fStep)
  588.             {
  589.             // Calculate x and y position of the next vertex
  590.             x = fSize*fsin(angle);
  591.             y = fSize*fcos(angle);
  592.     
  593.             // Specify the next vertex for the triangle fan
  594.             glVertex3f(x, y, z);
  595.             }
  596.  
  597.         // Done drawing the fan that covers the bottom
  598.         glEnd();
  599.         }
  600.  
  601.     glFrontFace(GL_CCW);    // Switch back to counter clock wise
  602.     for(angle = 0.0f; angle < 3.0f*(float)PI; angle += fStep)
  603.         {
  604.         x = fSize*fsin(angle);
  605.         y = fSize*fcos(angle);
  606.  
  607.         glBegin(GL_QUADS);
  608.             vNormal[0] = x;
  609.             vNormal[1] = y;
  610.             vNormal[2] = 0.0f;
  611.             ReduceToUnit(vNormal);
  612.             glNormal3fv(vNormal);
  613.  
  614.             glVertex3f(x, y, 0.0f);
  615.             glVertex3f(x, y, fWidth);
  616.  
  617.             x = fSize*fsin(angle+fStep);
  618.             y = fSize*fcos(angle+fStep);
  619.             
  620.             vNormal[0] = x;
  621.             vNormal[1] = y;
  622.             vNormal[2] = fWidth;
  623.             ReduceToUnit(vNormal);
  624.  
  625.             glNormal3fv(vNormal);
  626.             glVertex3f(x, y, fWidth);
  627.             glVertex3f(x, y, 0.0f);
  628.         glEnd();
  629.         }
  630.     }
  631.  
  632.  
  633. ///////////////////////////////////////////////////////////////////////////////
  634. // Draw the Tank. This consists of just a box and four "wheels"
  635. void DrawTank(void)
  636.     {
  637.     // Position the tank
  638.     glPushMatrix();
  639.     
  640.         // Place and orient tank
  641.         glTranslatef(tankPos.xPos, 10.0f, tankPos.zPos);
  642.         glRotatef(dRadToDeg(tankPos.radsFromEast+((float)PI/2.0f)), 0.0f, 1.0f, 0.0f);
  643.  
  644.         // Draw body of tank
  645.         glRGB(255,255,0);
  646.         glPushMatrix();
  647.             auxSolidBox(25.0f, 11.0f, 40.0f);
  648.         glPopMatrix();
  649.  
  650.         // Draw the wheels
  651.         glPushMatrix();
  652.             // Orient them in the correct direction
  653.             glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
  654.             
  655.             glTranslatef(15.0f, 0.0f, 12.0f);
  656.             DrawWheel();
  657.             glTranslatef(-30.0f, 0.0f, 0.0f);
  658.             DrawWheel();
  659.             glTranslatef(0.0f, 0.0f, -25.0f);
  660.             DrawWheel();
  661.             glTranslatef(30.0f, 0.0f, 0.0f);
  662.             DrawWheel();
  663.         glPopMatrix();
  664.  
  665.     glPopMatrix();
  666.     }
  667.  
  668.  
  669. //////////////////////////////////////////////////////////////////////////////
  670. // Draw the Robot. A Box with cone legs and Sphere eyes
  671. void DrawRobot(void)
  672.     {
  673.     // Position the robot
  674.     glPushMatrix();
  675.     
  676.     // Center is 30 above the ground
  677.     glTranslatef(robotPos.xPos, 30.0f, robotPos.zPos);
  678.  
  679.     glRotatef(dRadToDeg(robotPos.radsFromEast+((float)PI/2.0f)), 0.0f, 1.0f, 0.0f);
  680.     // Body Color
  681.     glRGB(255,255,0);
  682.     auxSolidBox(20.0f, 30.0f, 10.0f);
  683.  
  684.     // Eye Color
  685.     glRGB(0,255,255);
  686.     glTranslatef(5.0f, 10.0f, 5.0f);
  687.     auxSolidSphere(3.0f);
  688.     glTranslatef(-10.0f, 0.0f, 0.0f);
  689.     auxSolidSphere(3.0f);
  690.  
  691.     // Leg color
  692.     glRGB(255,0,255);
  693.     glTranslatef(0.0f, -26.0f, -5.0f);
  694.     auxSolidCylinder(3.0f, 15.0f);
  695.     glTranslatef(10.0f, 0.0f, 0.0f);
  696.     auxSolidCylinder(3.0f, 15.0f);
  697.     
  698.     glPopMatrix();
  699.     }