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

  1. //
  2. // File Name: StarF.hpp
  3. //
  4. // Description: The header file for the starfield demo
  5. //
  6. // Author: John De Goes
  7. //
  8. // Project: Cutting Edge 3D Game Programming
  9. //
  10.  
  11. // ------------------------------------------------------------
  12. // | Global include files:                                    |
  13. // ------------------------------------------------------------
  14.  
  15. #include <Stdlib.h>
  16.  
  17. // An approximation of pi - can't use const without compiler
  18. // warning
  19. #define PI 3.141592654
  20.  
  21. // Macros
  22. #define COS(a) (CosTable[a&DEGREEMASK])
  23. #define SIN(a) (SinTable[a&DEGREEMASK])
  24.  
  25. // The number of degrees in a circle:
  26. const unsigned int NUMBEROFDEGREES = 1024;
  27. const unsigned int DEGREEMASK = NUMBEROFDEGREES-1;
  28.  
  29. // Trig tables
  30. extern float CosTable[NUMBEROFDEGREES];
  31. extern float SinTable[NUMBEROFDEGREES];
  32.  
  33. // A "point in space" structure:
  34. struct Vertex {
  35. float Wx, Wy, Wz;  // The world X, Y and Z.
  36. };
  37.  
  38. // A "point on the screen" structure:
  39. struct ScreenVertex {
  40. long X, Y, Color;
  41. };
  42.  
  43. // A view matrix class - view only, not true matrix class
  44. class Matrix3D {
  45. protected:
  46. // Protected matrix functions
  47. void MergeMatrix(float NewMatrix[4][4]);
  48. float Matrix[4][4];
  49. public:
  50. // Misc functions:
  51. void Rotate(int Xa, int Ya, int Za);
  52. void Translate(float Xt, float Yt, float Zt);
  53. void Scale(float Xs, float Ys, float Zs);
  54. void Shear(float Xs, float Ys);
  55. void Initialize();
  56. Vertex &Transform(Vertex &V);
  57. };
  58.  
  59. class Star {
  60. protected:
  61. // Protected data:
  62. Vertex Point3D;
  63. ScreenVertex Point2D;
  64. int Visible;
  65. // Protected member functions:
  66. void Project();
  67. void DrawPoint(unsigned char *Buffer);
  68. public:
  69. // Public functions:
  70. void inline Initialize( float Nx, float Ny, float Nz );
  71. void Transform(Matrix3D Matrix) { Matrix.Transform(Point3D); }
  72. void Show(unsigned char *Buffer);
  73. // Constructors:
  74. Star() { Initialize(random(300)-150, random(300)-150, (random(4000)+100)); }
  75. Star( float X, float Y, float Z ) { Initialize( X, Y, Z ); }
  76. };
  77.