home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP08 / LINETYPE.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-09  |  3.3 KB  |  126 lines

  1. //
  2. // File name: LineType.HPP
  3. //
  4. // Description: A fixed-point implementation of a polygon edge 
  5. //             algorithm
  6. //
  7. // Author: John De Goes
  8. //
  9. // Project: Cutting Edge 3D Game Programming
  10. //
  11.  
  12. #ifndef LINETYPEHPP
  13. #define LINETYPEHPP
  14.  
  15. const XSTEP_PREC = 10;
  16. const ZSTEP_PREC = 26;
  17.  
  18. #include <Dos.H>
  19. #include <Math.H>
  20.  
  21. #include "Point2D.HPP"
  22. #include "3DClass.HPP"
  23.  
  24.  
  25. // Ceiling step constant:
  26. const long CEIL_FRACT = ( ( 1 << XSTEP_PREC ) - 1 );
  27.  
  28. // A class that steps on the ceiling of integers - always steps
  29. // on Y, rendering it useless for anything but polygon
  30. // rasterization:
  31. class CeilLine {
  32. protected:
  33.   long X1, X2, Y1, Y2;
  34.   long X, StepX, StepZ, Z;
  35.   long EdgeHeight, Y;
  36.   void inline Step ();
  37.   void inline Step ( long Amount );
  38. public:
  39.   CeilLine () { EdgeHeight = 0; }  // The constructor
  40.   void inline Init ( Point2D P1, Point2D P2 ); // The initialize function
  41.   void inline ClipTop ( const int Top );  // Object-precision clip 
  42.                                           // against top of viewport
  43.   void operator ++ () { Step (); }        // Steps polygon edge
  44.   void operator ++ ( int ) { Step (); }
  45.   void operator += ( long Amount ) { Step ( Amount ); } // Steps edge by "Amount"
  46.   Point2D inline operator + ( long Amount );   // Returns a point P + Amount
  47.   long GetX () { return ( X >> XSTEP_PREC ); } // Returns the current X coordinate
  48.   long GetY () { return Y; } // Returns the current Y coordinate
  49.   long GetZ () { return Z; } // Returns the current 1/Z coordinate
  50.   long Height () { return EdgeHeight; } // Returns the current poly height
  51. };
  52.  
  53. void inline CeilLine::Init ( Point2D P1, Point2D P2 )
  54.    {
  55.    // Calculate initial values for polygon edge:
  56.    long FWidth, DeltaZ, Z1, Z2;
  57.    X1 = P1.X; X2 = P2.X;
  58.    Y1 = P1.Y; Y2 = P2.Y;
  59.    Z1 = P1.Z; Z2 = P2.Z;
  60.  
  61.    EdgeHeight = ( Y2 - Y1 );
  62.    FWidth = ( X2 - X1 ) << XSTEP_PREC;
  63.    DeltaZ = ( Z2 - Z1 );
  64.  
  65.    X = ( X1 << XSTEP_PREC ) + CEIL_FRACT;
  66.    Y = Y1;
  67.    Z = Z1 + ZTrans;
  68.  
  69.    if ( EdgeHeight )
  70.       {
  71.       StepX = FWidth / EdgeHeight;
  72.       StepZ = DeltaZ / EdgeHeight;
  73.       }
  74.    else {
  75.         StepX = 0;
  76.         StepZ = 0;
  77.         }
  78.    }
  79.  
  80. void inline CeilLine::Step ()
  81.    {
  82.    // Step the edge:
  83.    X += StepX;
  84.    ++Y; Z += StepZ;
  85.    --EdgeHeight;
  86.    }
  87.  
  88. void inline CeilLine::Step ( long Amount )
  89.    {
  90.    // Step the edge by "Amount":
  91.    X += ( StepX * Amount );
  92.    Y += ( Amount );
  93.    Z += ( StepZ * Amount );
  94.    EdgeHeight -= ( Amount );
  95.    }
  96.  
  97. Point2D inline CeilLine::operator + ( long Amount )
  98.    {
  99.    // Return a point P + Amount:
  100.    Point2D Temp;
  101.    Temp.X = X + ( StepX * Amount );
  102.    Temp.Y = Y + ( Amount );
  103.    Temp.Z = Z + ( StepZ * Amount );
  104.    return Temp;
  105.    }
  106.  
  107. void CeilLine::ClipTop ( const int Top )
  108.    {
  109.    // Clip the polygon edge against the top of the viewport -
  110.    // Note: must be called directly after edge initialization:
  111.    double SlopeX;
  112.    long Step, Height = Y2 - Y1;
  113.    if ( ( Y < Top ) && ( Height ) )
  114.       {
  115.       Step = Top - Y;
  116.       SlopeX = ( double ) ( X2 - X1 ) / ( double ) Height;
  117.       X = ( X1 << XSTEP_PREC ) + 
  118.            ( ( long ) ( SlopeX * ( double ) Step ) << XSTEP_PREC ) + 
  119.           CEIL_FRACT;
  120.       Y = Top;
  121.       Z += StepZ * Step;
  122.       EdgeHeight -= Step;
  123.       }
  124.    }
  125.  
  126. #endif