home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / library / black18.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-25  |  12.1 KB  |  406 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. // needed for qsort
  97.  
  98. typedef int (*qsort_cast)(const void *, const void *);
  99.  
  100. typedef float matrix_4x4[4][4];       // the standard 4x4 homogenous matrix
  101.  
  102. typedef float matrix_1x4[4];          // a 1x4 matrix or a row vector
  103.  
  104. // this structure holds a vector or a simple 3-D point
  105.  
  106. typedef struct vector_3d_typ
  107.         {
  108.  
  109.         float x,y,z,w;    // a 3-D vector along with normalization factor
  110.                           // if needed
  111.  
  112.         } point_3d,vector_3d,*point_3d_ptr,*vector_3d_ptr;
  113.  
  114. // this function holds a objects orientation or direction relative to the
  115. // axis of a left handed system
  116.  
  117. typedef struct dir_3d_typ
  118.         {
  119.  
  120.         int ang_x,    // angle relative to x axis
  121.             ang_y,    // angle relative to y axis
  122.             ang_z;    // angle relative to z axis
  123.  
  124.         } dir_3d, *dir_3d_ptr;
  125.  
  126. // this stucture holds a polygon, but is used internaly by the object defintion
  127.  
  128. typedef struct polygon_typ
  129.         {
  130.  
  131.         int num_points;   // number of points in polygon (usually 3 or 4)
  132.  
  133.         int vertex_list[MAX_POINTS_PER_POLY];  // the index number of vertices
  134.  
  135.         int color;       // color of polygon
  136.  
  137.         int shade;       // the final shade of color after lighting
  138.  
  139.         int shading;     // type of lighting, flat or constant shading
  140.  
  141.         int two_sided;   // flags if the polygon is two sided
  142.  
  143.         int visible;     // used to remove backfaces
  144.  
  145.         int active;      // used to turn faces on and off
  146.  
  147.         int clipped;     // flags that polygon has been clipped or removed
  148.  
  149.         float normal_length; // pre-computed magnitude of normal
  150.  
  151.  
  152.         } polygon, *polygon_ptr;
  153.  
  154. // this structure holds a final polygon facet and is self contained
  155.  
  156. typedef struct facet_typ
  157.         {
  158.  
  159.         int num_points;  // number of vertices
  160.  
  161.         int color;       // color of polygon
  162.  
  163.         int shade;       // the final shade of color after lighting
  164.  
  165.         int shading;     // type of shading to use
  166.  
  167.         int two_sided;   // is the facet two sided
  168.  
  169.         int visible;     // is the facet transparent
  170.  
  171.         int clipped;     // has this poly been clipped
  172.  
  173.         int active;      // used to turn faces on and off
  174.  
  175.         point_3d vertex_list[MAX_POINTS_PER_POLY]; // the points that make
  176.                                                    // up the polygon facet
  177.  
  178.         float normal_length;  // holds pre-computed length of normal
  179.  
  180.         int average_z;        // holds average z, used in sorting
  181.  
  182.         } facet, *facet_ptr;
  183.  
  184. // this structure holds an object
  185.  
  186. typedef struct object_typ
  187.         {
  188.  
  189.         int id;             // identification number of object
  190.  
  191.         int num_vertices;   // total number of vertices in object
  192.  
  193.         point_3d vertices_local[MAX_VERTICES_PER_OBJECT];  // local vertices
  194.  
  195.         point_3d vertices_world[MAX_VERTICES_PER_OBJECT];  // world vertices
  196.  
  197.         point_3d vertices_camera[MAX_VERTICES_PER_OBJECT]; // camera vertices
  198.  
  199.         int num_polys;      // the number of polygons in the object
  200.  
  201.         polygon polys[MAX_POLYS_PER_OBJECT]; // the polygons that make up the object
  202.  
  203.         float radius;       // the average radius of object
  204.  
  205.         int state;          // state of object
  206.  
  207.         point_3d world_pos; // position of object in world coordinates
  208.  
  209.         } object, *object_ptr;
  210.  
  211.  
  212. // fixed point type
  213.  
  214. typedef long fixed;
  215.  
  216.  
  217. // P R O T O T Y P E S //////////////////////////////////////////////////////
  218.  
  219. int Load_Palette_Disk(char *filename, RGB_palette_ptr the_palette);
  220.  
  221. int Save_Palette_Disk(char *filename, RGB_palette_ptr the_palette);
  222.  
  223. float Compute_Object_Radius(object_ptr the_object);
  224.  
  225. void Build_Look_Up_Tables(void);
  226.  
  227. float Dot_Product_3D(vector_3d_ptr u,vector_3d_ptr v);
  228.  
  229. void Make_Vector_3D(point_3d_ptr init,
  230.                     point_3d_ptr term,
  231.                     vector_3d_ptr result);
  232.  
  233. void Cross_Product_3D(vector_3d_ptr u,
  234.                       vector_3d_ptr v,
  235.                       vector_3d_ptr normal);
  236.  
  237.  
  238. float Vector_Mag_3D(vector_3d_ptr v);
  239.  
  240. void Mat_Print_4x4(matrix_4x4 a);
  241.  
  242. void Mat_Print_1x4(matrix_1x4 a);
  243.  
  244. void Mat_Mul_4x4_4x4(matrix_4x4 a,
  245.                      matrix_4x4 b,
  246.                      matrix_4x4 result);
  247.  
  248. void Mat_Mul_1x4_4x4(matrix_1x4 a,
  249.                      matrix_4x4 b,
  250.                      matrix_1x4 result);
  251.  
  252. void Mat_Identity_4x4(matrix_4x4 a);
  253.  
  254. void Mat_Zero_4x4(matrix_4x4 a);
  255.  
  256. void Mat_Copy_4x4(matrix_4x4 source, matrix_4x4 destination);
  257.  
  258. void Local_To_World_Object(object_ptr the_object);
  259.  
  260. void Create_World_To_Camera(void);
  261.  
  262. void World_To_Camera_Object(object_ptr the_object);
  263.  
  264. void Rotate_Object(object_ptr the_object,int angle_x,int angle_y,int angle_z);
  265.  
  266. void Translate_Object(object_ptr the_object,int x_trans,int y_trans,int z_trans);
  267.  
  268. int Objects_Collide(object_ptr object_1, object_ptr object_2);
  269.  
  270. void Scale_Object(object_ptr the_object,float scale_factor);
  271.  
  272. void Position_Object(object_ptr the_object,int x,int y,int z);
  273.  
  274. char *PLG_Get_Line(char *string, int max_length, FILE *fp);
  275.  
  276. int PLG_Load_Object(object_ptr the_object,char *filename,float scale);
  277.  
  278. void Clip_Object_3D(object_ptr the_object, int mode);
  279.  
  280. void Remove_Backfaces_And_Shade(object_ptr the_object, int force_color);
  281.  
  282. int Remove_Object(object_ptr the_object, int mode);
  283.  
  284. void Generate_Poly_List(object_ptr the_object,int mode);
  285.  
  286. int Poly_Compare(facet **arg1, facet **arg2);
  287.  
  288. void Sort_Poly_List(void);
  289.  
  290. void Print_Poly_List(void);
  291.  
  292. void Draw_Poly_List(void);
  293.  
  294. void Draw_Triangle_2D(int x1,int y1,
  295.                       int x2,int y2,
  296.                       int x3,int y3,int color);
  297.  
  298. void Draw_Top_Triangle(int x1,int y1, int x2,int y2, int x3,int y3,int color);
  299.  
  300. void Draw_Bottom_Triangle(int x1,int y1, int x2,int y2, int x3,int y3,int color);
  301.  
  302. void Triangle_Line(unsigned char far *dest_addr,
  303.                    unsigned int xs,
  304.                    unsigned int xe,
  305.                    int color);
  306.  
  307. void Draw_Triangle_2D(int x1,int y1,
  308.                       int x2,int y2,
  309.                       int x3,int y3,int color);
  310.  
  311.  
  312. void Make_Grey_Palette(void);
  313.  
  314. void Draw_Object_Wire(object_ptr the_object, int color);
  315.  
  316.  
  317. // new 32 bit functions
  318.  
  319. void Display_Double_Buffer_32(unsigned char far *buffer,int y);
  320.  
  321. void Fill_Double_Buffer_32(int color);
  322.  
  323. // external assembly language functions
  324.  
  325. void fquadcpy(void far *dest, void far *source, long count);
  326.  
  327. void fquadset(void far *dest, long data, long count);
  328.  
  329. void Triangle_32Line(unsigned char far *dest_addr,
  330.                      unsigned int xs,
  331.                      unsigned int xe,
  332.                      int color);
  333.  
  334. void Triangle_16Line(unsigned char far *dest_addr,
  335.                      unsigned int xs,
  336.                      unsigned int xe,
  337.                      int color);
  338.  
  339.  
  340. void Triangle_Asm(void far *dest_addr,int y1,int y3,
  341.                   float xs,float xe,float dx_left,float dx_right,
  342.                   int color);
  343.  
  344.  
  345. // fixed point functions
  346.  
  347. fixed FP_Mul(fixed mulipicand, fixed multiplier);
  348.  
  349. fixed FP_Div(fixed dividend, fixed divisor);
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356. // E X T E R N A L S//////////////////////////////////////////////////////////
  357.  
  358. extern float  clip_near_z,         // the near or hither clipping plane
  359.               clip_far_z,          // the far or yon clipping plane
  360.               screen_width,        // dimensions of the screen
  361.               screen_heigth;
  362.  
  363. extern float viewing_distance;       // distance of projection plane from camera
  364.  
  365. extern point_3d view_point;        // position of camera
  366.  
  367. extern vector_3d light_source;     // position of point light source
  368.  
  369. extern float ambient_light;        // ambient light level
  370.  
  371. extern dir_3d view_angle;          // angle of camera
  372.  
  373. extern matrix_4x4 global_view;        // the global inverse wortld to camera
  374.  
  375. extern RGB_palette color_palette_3d;  // the color palette used for the 3D system
  376.  
  377. extern int num_objects;               // number of objects in the world
  378.  
  379. extern object_ptr world_object_list[MAX_OBJECTS];  // the objects in the world
  380.  
  381. extern int num_polys_frame;                        // the number of polys in this frame
  382.  
  383. extern facet_ptr world_polys[MAX_POLYS_PER_FRAME]; // the visible polygons for this frame
  384.  
  385. extern facet world_poly_storage[MAX_POLYS_PER_FRAME];  // the storage for the visible
  386.                                                 // polygons is pre-allocated
  387.                                                 // so it doesn't need to be
  388.                                                 // allocated frame by frame
  389.  
  390. // look up tables
  391.  
  392. extern float sin_look[360+1],   // SIN from 0 to 360
  393.              cos_look[360+1];   // COSINE from 0 to 360
  394.  
  395. // the clipping region, set it to default on start up
  396.  
  397. extern int poly_clip_min_x,
  398.            poly_clip_min_y,
  399.  
  400.            poly_clip_max_x,
  401.            poly_clip_max_y;
  402.  
  403. extern sprite textures;              // this holds the textures
  404.  
  405.  
  406.