home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP03 / EXTRA / 3DCLASS.HPP < prev    next >
C/C++ Source or Header  |  1995-10-24  |  2KB  |  99 lines

  1. //
  2. //
  3. //
  4.  
  5. #define PI 3.141592654
  6. #include <Stdio.h>
  7.  
  8. #define COS(a) ( CosTable [a & (DEGREECOUNT - 1)] )
  9. #define SIN(a) ( SinTable [a & (DEGREECOUNT - 1)] )
  10.  
  11. const unsigned int DEGREECOUNT = 1024;
  12.  
  13. extern double CosTable [ DEGREECOUNT ];
  14. extern double SinTable [ DEGREECOUNT ];
  15.  
  16. void InitMath ();
  17.  
  18. struct Vertex {
  19. double Wx, Wy, Wz;  // The world X, Y and Z.
  20. };
  21.  
  22. struct Vector {
  23. double X, Y, Z, Distance;    // X, Y and Z direction.
  24. };
  25.  
  26. struct ScreenVertex {
  27. long X, Y; //The screen x and y of a point.
  28. double Zp;   //The proportional (1/z) z at (x,y) point.
  29. };
  30.  
  31. // A view matrix class
  32. class Matrix3D {
  33. protected:
  34. void MergeMatrix ( double NewMatrix[4][4] );
  35. double Matrix [ 4 ] [ 4 ];
  36. int Xr, Zr, Yr;
  37. public:
  38. Matrix3D () { InitMath (); Zr = Yr = Zr = 0; }
  39. void Rotate ( int Xa, int Ya, int Za );
  40. void Translate ( double Xt, double Yt, double Zt );
  41. void Scale ( double Xs, double Ys, double Zs );
  42. void Shear ( double Xs, double Ys );
  43. void Initialize ();
  44. Vertex inline &Transform ( Vertex &V );
  45. Vector inline &Transform ( Vector &V );
  46. };
  47.  
  48. class Triangle {
  49. protected:
  50. void Project ();
  51. void Rasterize ( unsigned char *Buffer );
  52. void CalculateNormal ();
  53. void Backface ();
  54. public:
  55. Vector Normal;              // The surface normal
  56. Vertex VPoint [ 3 ];  // A pointer to the triangle's vertices
  57. ScreenVertex SPoint [ 3 ];
  58. unsigned char Visible, Color;         // Visible flag and color info
  59. void Transform ( Matrix3D &Matrix ); // The transform function
  60. int Load ( FILE *File );         // A load function for 
  61.                                  // ASCII RAW files
  62. void inline Display ( unsigned char *Buffer );
  63. };
  64.  
  65. class TriangleWorld {
  66. public:
  67. Triangle *World;
  68. unsigned int TriCount;
  69. int Load ( char *FileName );
  70. void Transform ( Matrix3D &Matrix );
  71. void Display ( unsigned char *Buffer );
  72. ~TriangleWorld ()
  73.    {
  74.    if ( World )
  75.       delete [] World;
  76.    }
  77. };
  78.  
  79. class PolyEdge {
  80. protected:
  81. long X, Y, StepX, StepY, Numerator, Denominator, ErrorTerm;
  82. long EdgeHeight, Zr;
  83. public:
  84. void inline Step ();
  85. PolyEdge () { }
  86. PolyEdge ( ScreenVertex &P1, ScreenVertex &P2 )
  87.    {
  88.    Initialize ( P1, P2 );
  89.    }
  90. void Initialize ( ScreenVertex &P1, ScreenVertex &P2 );
  91. long Height () { return EdgeHeight; }
  92. long GetX () { return X; }
  93. long GetY () { return Y; }
  94. long GetZr () { return Zr; }
  95. long GetStepY() { return StepY; }
  96. void operator ++ () { Step (); }
  97. void operator ++ (int) { Step (); }
  98. };
  99.