home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / msc / library / black18.h < prev    next >
Text File  |  1995-04-26  |  12KB  |  402 lines

  1.  
  2. // BLACK18.H - header file
  3.  
  4.  
  5. // D E F I N E S /////////////////////////////////////////////////////////////
  6.  
  7. #define MAX_POINTS_PER_POLY     4   // a maximum of four points per poly
  8. #define MAX_VERTICES_PER_OBJECT 24  // this should be enough
  9. #define MAX_POLYS_PER_OBJECT    16  // this will have to do!
  10. #define MAX_OBJECTS             32  // maximum number of objects in world
  11. #define MAX_POLYS_PER_FRAME     128 // maximum number of polys in a single
  12.                                     // animation frame
  13.  
  14. // polygon shading specifiers for PLG files
  15.  
  16. // format = 0xh3 h2 h1h0
  17. // h3   = shading type
  18. // h2   = special attributes= d3d2d1d0
  19.  
  20. // d3,d2,d1 unused, d0 = two sided flag, 0 = 1 sided, 1 = 2 sided
  21.  
  22. // h1h0 = color 0-255
  23.  
  24. #define ONE_SIDED               0 // number of sides polygon has
  25. #define TWO_SIDED               1
  26.  
  27. #define CONSTANT_SHADING        0
  28. #define FLAT_SHADING            1
  29. #define GOURAUD_SHADING         2 (actually metallic under PLG defintion)
  30. #define SPFX_SHADING            3
  31.  
  32. #define ASPECT_RATIO            (float)0.8  // the aspect ratio
  33. #define INVERSE_ASPECT_RATIO    (float)1.25 // the inverse of the aspect ratio
  34.  
  35. #define HALF_SCREEN_WIDTH       160 // center of screen
  36. #define HALF_SCREEN_HEIGHT      100
  37.  
  38. #define POLY_CLIP_MIN_X         0  // miniumum x,y clip values
  39. #define POLY_CLIP_MIN_Y         0
  40.  
  41. #define POLY_CLIP_MAX_X         319 // maxium x,y clip values
  42. #define POLY_CLIP_MAX_Y         199
  43.  
  44. #define CLIP_Z_MODE             0  // this constant tells the clipper to do a
  45.                                    // simple z extent clip
  46.  
  47. #define CLIP_XYZ_MODE           1  // this constant tells the clipper to do
  48.                                    // a full 3D volume clip
  49.  
  50.  
  51.  
  52. #define OBJECT_CULL_Z_MODE      0  // this constant tells the object culler to do a
  53.                                    // simple z extent clip
  54.  
  55. #define OBJECT_CULL_XYZ_MODE    1  // this constant tells the object culler to do
  56.                                    // a full 3D volume clip
  57.  
  58.  
  59. // these are the constants needed for the shader engine
  60. // note that brightness increses with smaller values and there
  61. // are 16 shades of each color eg. bright blue is 144 and dark blue is 159
  62.  
  63. #define SHADE_GREY         31    // hex value = 1F
  64. #define SHADE_GREEN        111   // hex value = 6F
  65. #define SHADE_BLUE         159   // hex value = 9F
  66. #define SHADE_RED          47    // hex value = 2F
  67. #define SHADE_YELLOW       79    // hex value = 4F
  68. #define SHADE_BROWN        223   // hex value = DF
  69. #define SHADE_LIGHT_BROWN  207   // hex value = CF
  70. #define SHADE_PURPLE       175   // hex value = AF
  71. #define SHADE_CYAN         127   // hex value = 7F
  72. #define SHADE_LAVENDER     191   // hex value = BF
  73.  
  74.  
  75. // defines for object collisions
  76.  
  77. #define NO_COLLISION       0
  78. #define SOFT_COLLISION     1
  79. #define HARD_COLLISION     2
  80.  
  81. // defines for polygon list generator
  82.  
  83. #define RESET_POLY_LIST   0
  84. #define ADD_TO_POLY_LIST  1
  85.  
  86. // fixed point stuff
  87.  
  88. #define FP_SHIFT          16          // 16:16 format
  89. #define FP_SCALE          65526L      // 2^16 = 65536, used to convert floats
  90.  
  91. // M A C R O S ///////////////////////////////////////////////////////////////
  92.  
  93.  
  94. // T Y P E D E F S ///////////////////////////////////////////////////////////
  95.  
  96. typedef float matrix_4x4[4][4];       // the standard 4x4 homogenous matrix
  97.  
  98. typedef float matrix_1x4[4];          // a 1x4 matrix or a row vector
  99.  
  100. // this structure holds a vector or a simple 3-D point
  101.  
  102. typedef struct vector_3d_typ
  103.         {
  104.  
  105.         float x,y,z,w;    // a 3-D vector along with normalization factor
  106.                           // if needed
  107.  
  108.         } point_3d,vector_3d,*point_3d_ptr,*vector_3d_ptr;
  109.  
  110. // this function holds a objects orientation or direction relative to the
  111. // axis of a left handed system
  112.  
  113. typedef struct dir_3d_typ
  114.         {
  115.  
  116.         int ang_x,    // angle relative to x axis
  117.             ang_y,    // angle relative to y axis
  118.             ang_z;    // angle relative to z axis
  119.  
  120.         } dir_3d, *dir_3d_ptr;
  121.  
  122. // this stucture holds a polygon, but is used internaly by the object defintion
  123.  
  124. typedef struct polygon_typ
  125.         {
  126.  
  127.         int num_points;   // number of points in polygon (usually 3 or 4)
  128.  
  129.         int vertex_list[MAX_POINTS_PER_POLY];  // the index number of vertices
  130.  
  131.         int color;       // color of polygon
  132.  
  133.         int shade;       // the final shade of color after lighting
  134.  
  135.         int shading;     // type of lighting, flat or constant shading
  136.  
  137.         int two_sided;   // flags if the polygon is two sided
  138.  
  139.         int visible;     // used to remove backfaces
  140.  
  141.         int active;      // used to turn faces on and off
  142.  
  143.         int clipped;     // flags that polygon has been clipped or removed
  144.  
  145.         float normal_length; // pre-computed magnitude of normal
  146.  
  147.  
  148.         } polygon, *polygon_ptr;
  149.  
  150. // this structure holds a final polygon facet and is self contained
  151.  
  152. typedef struct facet_typ
  153.         {
  154.  
  155.         int num_points;  // number of vertices
  156.  
  157.         int color;       // color of polygon
  158.  
  159.         int shade;       // the final shade of color after lighting
  160.  
  161.         int shading;     // type of shading to use
  162.  
  163.         int two_sided;   // is the facet two sided
  164.  
  165.         int visible;     // is the facet transparent
  166.  
  167.         int clipped;     // has this poly been clipped
  168.  
  169.         int active;      // used to turn faces on and off
  170.  
  171.         point_3d vertex_list[MAX_POINTS_PER_POLY]; // the points that make
  172.                                                    // up the polygon facet
  173.  
  174.         float normal_length;  // holds pre-computed length of normal
  175.  
  176.         int average_z;        // holds average z, used in sorting
  177.  
  178.         } facet, *facet_ptr;
  179.  
  180. // this structure holds an object
  181.  
  182. typedef struct object_typ
  183.         {
  184.  
  185.         int id;             // identification number of object
  186.  
  187.         int num_vertices;   // total number of vertices in object
  188.  
  189.         point_3d vertices_local[MAX_VERTICES_PER_OBJECT];  // local vertices
  190.  
  191.         point_3d vertices_world[MAX_VERTICES_PER_OBJECT];  // world vertices
  192.  
  193.         point_3d vertices_camera[MAX_VERTICES_PER_OBJECT]; // camera vertices
  194.  
  195.         int num_polys;      // the number of polygons in the object
  196.  
  197.         polygon polys[MAX_POLYS_PER_OBJECT]; // the polygons that make up the object
  198.  
  199.         float radius;       // the average radius of object
  200.  
  201.         int state;          // state of object
  202.  
  203.         point_3d world_pos; // position of object in world coordinates
  204.  
  205.         } object, *object_ptr;
  206.  
  207.  
  208. // fixed point type
  209.  
  210. typedef long fixed;
  211.  
  212.  
  213. // P R O T O T Y P E S //////////////////////////////////////////////////////
  214.  
  215. int Load_Palette_Disk(char *filename, RGB_palette_ptr the_palette);
  216.  
  217. int Save_Palette_Disk(char *filename, RGB_palette_ptr the_palette);
  218.  
  219. float Compute_Object_Radius(object_ptr the_object);
  220.  
  221. void Build_Look_Up_Tables(void);
  222.  
  223. float Dot_Product_3D(vector_3d_ptr u,vector_3d_ptr v);
  224.  
  225. void Make_Vector_3D(point_3d_ptr init,
  226.                     point_3d_ptr term,
  227.                     vector_3d_ptr result);
  228.  
  229. void Cross_Product_3D(vector_3d_ptr u,
  230.                       vector_3d_ptr v,
  231.                       vector_3d_ptr normal);
  232.  
  233.  
  234. float Vector_Mag_3D(vector_3d_ptr v);
  235.  
  236. void Mat_Print_4x4(matrix_4x4 a);
  237.  
  238. void Mat_Print_1x4(matrix_1x4 a);
  239.  
  240. void Mat_Mul_4x4_4x4(matrix_4x4 a,
  241.                      matrix_4x4 b,
  242.                      matrix_4x4 result);
  243.  
  244. void Mat_Mul_1x4_4x4(matrix_1x4 a,
  245.                      matrix_4x4 b,
  246.                      matrix_1x4 result);
  247.  
  248. void Mat_Identity_4x4(matrix_4x4 a);
  249.  
  250. void Mat_Zero_4x4(matrix_4x4 a);
  251.  
  252. void Mat_Copy_4x4(matrix_4x4 source, matrix_4x4 destination);
  253.  
  254. void Local_To_World_Object(object_ptr the_object);
  255.  
  256. void Create_World_To_Camera(void);
  257.  
  258. void World_To_Camera_Object(object_ptr the_object);
  259.  
  260. void Rotate_Object(object_ptr the_object,int angle_x,int angle_y,int angle_z);
  261.  
  262. void Translate_Object(object_ptr the_object,int x_trans,int y_trans,int z_trans);
  263.  
  264. int Objects_Collide(object_ptr object_1, object_ptr object_2);
  265.  
  266. void Scale_Object(object_ptr the_object,float scale_factor);
  267.  
  268. void Position_Object(object_ptr the_object,int x,int y,int z);
  269.  
  270. char *PLG_Get_Line(char *string, int max_length, FILE *fp);
  271.  
  272. int PLG_Load_Object(object_ptr the_object,char *filename,float scale);
  273.  
  274. void Clip_Object_3D(object_ptr the_object, int mode);
  275.  
  276. void Remove_Backfaces_And_Shade(object_ptr the_object, int force_color);
  277.  
  278. int Remove_Object(object_ptr the_object, int mode);
  279.  
  280. void Generate_Poly_List(object_ptr the_object,int mode);
  281.  
  282. int Poly_Compare(facet **arg1, facet **arg2);
  283.  
  284. void Sort_Poly_List(void);
  285.  
  286. void Print_Poly_List(void);
  287.  
  288. void Draw_Poly_List(void);
  289.  
  290. void Draw_Triangle_2D(int x1,int y1,
  291.                       int x2,int y2,
  292.                       int x3,int y3,int color);
  293.  
  294. void Draw_Top_Triangle(int x1,int y1, int x2,int y2, int x3,int y3,int color);
  295.  
  296. void Draw_Bottom_Triangle(int x1,int y1, int x2,int y2, int x3,int y3,int color);
  297.  
  298. void Triangle_Line(unsigned char far *dest_addr,
  299.                    unsigned int xs,
  300.                    unsigned int xe,
  301.                    int color);
  302.  
  303. void Draw_Triangle_2D(int x1,int y1,
  304.                       int x2,int y2,
  305.                       int x3,int y3,int color);
  306.  
  307.  
  308. void Make_Grey_Palette(void);
  309.  
  310. void Draw_Object_Wire(object_ptr the_object, int color);
  311.  
  312.  
  313. // new 32 bit functions
  314.  
  315. void Display_Double_Buffer_32(unsigned char far *buffer,int y);
  316.  
  317. void Fill_Double_Buffer_32(int color);
  318.  
  319. // external assembly language functions
  320.  
  321. void fquadcpy(void far *dest, void far *source, long count);
  322.  
  323. void fquadset(void far *dest, long data, long count);
  324.  
  325. void Triangle_32Line(unsigned char far *dest_addr,
  326.                      unsigned int xs,
  327.                      unsigned int xe,
  328.                      int color);
  329.  
  330. void Triangle_16Line(unsigned char far *dest_addr,
  331.                      unsigned int xs,
  332.                      unsigned int xe,
  333.                      int color);
  334.  
  335.  
  336. void Triangle_Asm(void far *dest_addr,int y1,int y3,
  337.                   float xs,float xe,float dx_left,float dx_right,
  338.                   int color);
  339.  
  340.  
  341. // fixed point functions
  342.  
  343. fixed FP_Mul(fixed mulipicand, fixed multiplier);
  344.  
  345. fixed FP_Div(fixed dividend, fixed divisor);
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352. // E X T E R N A L S//////////////////////////////////////////////////////////
  353.  
  354. extern float  clip_near_z,         // the near or hither clipping plane
  355.               clip_far_z,          // the far or yon clipping plane
  356.               screen_width,        // dimensions of the screen
  357.               screen_heigth;
  358.  
  359. extern float viewing_distance;       // distance of projection plane from camera
  360.  
  361. extern point_3d view_point;        // position of camera
  362.  
  363. extern vector_3d light_source;     // position of point light source
  364.  
  365. extern float ambient_light;        // ambient light level
  366.  
  367. extern dir_3d view_angle;          // angle of camera
  368.  
  369. extern matrix_4x4 global_view;        // the global inverse wortld to camera
  370.  
  371. extern RGB_palette color_palette_3d;  // the color palette used for the 3D system
  372.  
  373. extern int num_objects;               // number of objects in the world
  374.  
  375. extern object_ptr world_object_list[MAX_OBJECTS];  // the objects in the world
  376.  
  377. extern int num_polys_frame;                        // the number of polys in this frame
  378.  
  379. extern facet_ptr world_polys[MAX_POLYS_PER_FRAME]; // the visible polygons for this frame
  380.  
  381. extern facet world_poly_storage[MAX_POLYS_PER_FRAME];  // the storage for the visible
  382.                                                 // polygons is pre-allocated
  383.                                                 // so it doesn't need to be
  384.                                                 // allocated frame by frame
  385.  
  386. // look up tables
  387.  
  388. extern float sin_look[360+1],   // SIN from 0 to 360
  389.              cos_look[360+1];   // COSINE from 0 to 360
  390.  
  391. // the clipping region, set it to default on start up
  392.  
  393. extern int poly_clip_min_x,
  394.            poly_clip_min_y,
  395.  
  396.            poly_clip_max_x,
  397.            poly_clip_max_y;
  398.  
  399. extern sprite textures;              // this holds the textures
  400.  
  401.  
  402.