home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP09 / PHONG / PHONGDM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-19  |  3.8 KB  |  138 lines

  1. //
  2. // File name: PhongDM.CPP
  3. //
  4. // Description: Part of the Phong shading demo
  5. //
  6. // Author: John De Goes
  7. //
  8. // Project: Cutting Edge 3D Game Programming
  9. //
  10.  
  11. #include <DOS.H>
  12. #include <Bios.H>
  13. #include <Time.H>
  14. #include <Conio.H>
  15. #include <Stdlib.H>
  16. #include <Iostream.H>
  17.  
  18. #include "32Bit.HPP"
  19. #include "Palette.HPP"
  20. #include "DrawPoly.HPP"
  21.  
  22. const int KEY_WAITING = 1;
  23. const double MOVE = 0.03125;
  24.  
  25. void ProjectPoly ( Point2D *Poly2D, Point3D *Poly3D, int NP )
  26.    {
  27.    for ( short int N = 0; N < NP; N++ )
  28.        {
  29.        Poly2D [ N ].X = Poly3D [ N ].Wx * 120.0F / 
  30.                         Poly3D [ N ].Wz + 160.0F;
  31.  
  32.        Poly2D [ N ].Y = Poly3D [ N ].Wy * -120.0F /
  33.                         Poly3D [ N ].Wz + 100.0F;
  34.        }
  35.    }
  36.  
  37. void inline ShowPoly ( Point3D *Poly3D, 
  38.                        int NP, unsigned char *Dest )
  39.    {
  40.    Point2D Poly2D [ 10 ];
  41.    ProjectPoly ( Poly2D, Poly3D, NP );
  42.    DrawPoly ( Poly2D, Poly3D, NP, Dest );
  43.    }
  44.  
  45. void NormLight ()
  46.    {
  47.    double Mag;
  48.    Mag = sqrt ( Light.X * Light.X +
  49.                 Light.Y * Light.Y +
  50.                 Light.Z * Light.Z );
  51.    Light.X /= Mag;
  52.    Light.Y /= Mag;
  53.    Light.Z /= Mag;
  54.    }
  55.  
  56. void SetPalette ()
  57.    {
  58.    short int Inten;
  59.    for ( short int N = 0; N < 256; N++ )
  60.        {
  61.        Inten = ( short ) ( ( short ) N >> ( short ) 2 );
  62.        setpalreg ( N, Inten, Inten, Inten );
  63.        }
  64.    }
  65.  
  66. void main ()
  67.    {
  68.    unsigned char *VidMem = VideoAddress ();
  69.    long StartTime, EndTime, TotalTime, Ticks = 0;
  70.    unsigned char Input = 0;
  71.    unsigned char *Buffer = new unsigned char [ 64000 ];
  72.    Point3D Poly1 [] = { { 0,     100, 150,   // The first 
  73.                                              // vertex
  74.  
  75.                              0,  100, -50 }, // The first 
  76.                                              // vertex' normal
  77.  
  78.                         { 100,  -100, 150,   // The second 
  79.                                              // vertex
  80.  
  81.                             100, -100, -50 },// The second 
  82.                                              // vertex' normal
  83.  
  84.                         { -100, -100, 150,   // The third
  85.                                              // vertex
  86.  
  87.                          -100, -100, -50 } };// The third
  88.                                              // vertex' normal
  89.  
  90.    // Normalize the light source vector to 1:
  91.    NormLight ();
  92.  
  93.    // Set the video mode:
  94.    SetVideo ( 0x13 );
  95.  
  96.    // Set the palette to shades of gray:
  97.    SetPalette ();
  98.  
  99.    StartTime = clock ();
  100.    while ( Input != 27 )
  101.          {
  102.          if ( _bios_keybrd ( _KEYBRD_READY ) )
  103.             {
  104.             Input = ( unsigned char ) _bios_keybrd ( _KEYBRD_READ );
  105.             if ( ( Input == 'r' ) || ( Input == 'R' ) )
  106.                {
  107.                Light.X += MOVE;
  108.                NormLight ();
  109.                }
  110.             else if ( ( Input == 'l' ) || ( Input == 'L' ) )
  111.                     {
  112.                     Light.X -= MOVE;
  113.                     NormLight ();
  114.                     }
  115.             else if ( ( Input == 'u' ) || ( Input == 'U' ) )
  116.                     {
  117.                     Light.Y += MOVE;
  118.                     NormLight ();
  119.                     }
  120.             else if ( ( Input == 'd' ) || ( Input == 'D' ) )
  121.                     {
  122.                     Light.Y -= MOVE;
  123.                     NormLight ();
  124.                     }
  125.             }
  126.          setmem ( Buffer, 64000, 0 );
  127.          ShowPoly ( Poly1, 3, Buffer );
  128.          memmove ( VidMem, Buffer, 64000 );
  129.          ++Ticks;
  130.          }
  131.    EndTime = clock ();
  132.  
  133.    SetVideo ( 0x03 );
  134.    TotalTime = ( double ) ( EndTime - StartTime ) / ( double ) CLK_TCK;
  135.    cout << "Polygons per second = " 
  136.         << ( 1.0F / ( ( double ) TotalTime / ( double ) Ticks ) );
  137.    delete [] Buffer;
  138.    }