home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / library / black17.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-25  |  12.1 KB  |  402 lines

  1.  
  2. // BLACK17.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.  
  95. // T Y P E D E F S ///////////////////////////////////////////////////////////
  96.  
  97.  
  98. // needed for qsort
  99.  
  100. typedef int (*qsort_cast)(const void *, const void *);
  101.  
  102. typedef float matrix_4x4[4][4];       // the standard 4x4 homogenous matrix
  103.  
  104. typedef float matrix_1x4[4];          // a 1x4 matrix or a row vector
  105.  
  106. // this structure holds a vector or a simple 3-D point
  107.  
  108. typedef struct vector_3d_typ
  109.         {
  110.  
  111.         float x,y,z,w;    // a 3-D vector along with normalization factor
  112.                           // if needed
  113.  
  114.         } point_3d,vector_3d,*point_3d_ptr,*vector_3d_ptr;
  115.  
  116. // this function holds a objects orientation or direction relative to the
  117. // axis of a left handed system
  118.  
  119. typedef struct dir_3d_typ
  120.         {
  121.  
  122.         int ang_x,    // angle relative to x axis
  123.             ang_y,    // angle relative to y axis
  124.             ang_z;    // angle relative to z axis
  125.  
  126.         } dir_3d, *dir_3d_ptr;
  127.  
  128. // this stucture holds a polygon, but is used internaly by the object defintion
  129.  
  130. typedef struct polygon_typ
  131.         {
  132.  
  133.         int num_points;   // number of points in polygon (usually 3 or 4)
  134.  
  135.         int vertex_list[MAX_POINTS_PER_POLY];  // the index number of vertices
  136.  
  137.         int color;       // color of polygon
  138.  
  139.         int shade;       // the final shade of color after lighting
  140.  
  141.         int shading;     // type of lighting, flat or constant shading
  142.  
  143.         int two_sided;   // flags if the polygon is two sided
  144.  
  145.         int visible;     // used to remove backfaces
  146.  
  147.         int active;      // used to turn faces on and off
  148.  
  149.         int clipped;     // flags that polygon has been clipped or removed
  150.  
  151.         float normal_length; // pre-computed magnitude of normal
  152.  
  153.  
  154.         } polygon, *polygon_ptr;
  155.  
  156. // this structure holds a final polygon facet and is self contained
  157.  
  158. typedef struct facet_typ
  159.         {
  160.  
  161.         int num_points;  // number of vertices
  162.  
  163.         int color;       // color of polygon
  164.  
  165.         int shade;       // the final shade of color after lighting
  166.  
  167.         int shading;     // type of shading to use
  168.  
  169.         int two_sided;   // is the facet two sided
  170.  
  171.         int visible;     // is the facet transparent
  172.  
  173.         int clipped;     // has this poly been clipped
  174.  
  175.         int active;      // used to turn faces on and off
  176.  
  177.         point_3d vertex_list[MAX_POINTS_PER_POLY]; // the points that make
  178.                                                    // up the polygon facet
  179.  
  180.         float normal_length;  // holds pre-computed length of normal
  181.  
  182.         int average_z;        // holds average z, used in sorting
  183.  
  184.         } facet, *facet_ptr;
  185.  
  186. // this structure holds an object
  187.  
  188. typedef struct object_typ
  189.         {
  190.  
  191.         int id;             // identification number of object
  192.  
  193.         int num_vertices;   // total number of vertices in object
  194.  
  195.         point_3d vertices_local[MAX_VERTICES_PER_OBJECT];  // local vertices
  196.  
  197.         point_3d vertices_world[MAX_VERTICES_PER_OBJECT];  // world vertices
  198.  
  199.         point_3d vertices_camera[MAX_VERTICES_PER_OBJECT]; // camera vertices
  200.  
  201.         int num_polys;      // the number of polygons in the object
  202.  
  203.         polygon polys[MAX_POLYS_PER_OBJECT]; // the polygons that make up the object
  204.  
  205.         float radius;       // the average radius of object
  206.  
  207.         int state;          // state of object
  208.  
  209.         point_3d world_pos; // position of object in world coordinates
  210.  
  211.         } object, *object_ptr;
  212.  
  213.  
  214. // fixed point type
  215.  
  216. typedef long fixed;
  217.  
  218.  
  219. // P R O T O T Y P E S //////////////////////////////////////////////////////
  220.  
  221. int Load_Palette_Disk(char *filename, RGB_palette_ptr the_palette);
  222.  
  223. int Save_Palette_Disk(char *filename, RGB_palette_ptr the_palette);
  224.  
  225. float Compute_Object_Radius(object_ptr the_object);
  226.  
  227. void Build_Look_Up_Tables(void);
  228.  
  229. float Dot_Product_3D(vector_3d_ptr u,vector_3d_ptr v);
  230.  
  231. void Make_Vector_3D(point_3d_ptr init,
  232.                     point_3d_ptr term,
  233.                     vector_3d_ptr result);
  234.  
  235. void Cross_Product_3D(vector_3d_ptr u,
  236.                       vector_3d_ptr v,
  237.                       vector_3d_ptr normal);
  238.  
  239.  
  240. float Vector_Mag_3D(vector_3d_ptr v);
  241.  
  242. void Mat_Print_4x4(matrix_4x4 a);
  243.  
  244. void Mat_Print_1x4(matrix_1x4 a);
  245.  
  246. void Mat_Mul_4x4_4x4(matrix_4x4 a,
  247.                      matrix_4x4 b,
  248.                      matrix_4x4 result);
  249.  
  250. void Mat_Mul_1x4_4x4(matrix_1x4 a,
  251.                      matrix_4x4 b,
  252.                      matrix_1x4 result);
  253.  
  254. void Mat_Identity_4x4(matrix_4x4 a);
  255.  
  256. void Mat_Zero_4x4(matrix_4x4 a);
  257.  
  258. void Mat_Copy_4x4(matrix_4x4 source, matrix_4x4 destination);
  259.  
  260. void Local_To_World_Object(object_ptr the_object);
  261.  
  262. void Create_World_To_Camera(void);
  263.  
  264. void World_To_Camera_Object(object_ptr the_object);
  265.  
  266. void Rotate_Object(object_ptr the_object,int angle_x,int angle_y,int angle_z);
  267.  
  268. void Translate_Object(object_ptr the_object,int x_trans,int y_trans,int z_trans);
  269.  
  270. int Objects_Collide(object_ptr object_1, object_ptr object_2);
  271.  
  272. void Scale_Object(object_ptr the_object,float scale_factor);
  273.  
  274. void Position_Object(object_ptr the_object,int x,int y,int z);
  275.  
  276. char *PLG_Get_Line(char *string, int max_length, FILE *fp);
  277.  
  278. int PLG_Load_Object(object_ptr the_object,char *filename,float scale);
  279.  
  280. void Clip_Object_3D(object_ptr the_object, int mode);
  281.  
  282. void Remove_Backfaces_And_Shade(object_ptr the_object);
  283.  
  284. int Remove_Object(object_ptr the_object, int mode);
  285.  
  286. void Generate_Poly_List(object_ptr the_object,int mode);
  287.  
  288. int Poly_Compare(facet **arg1, facet **arg2);
  289.  
  290. void Sort_Poly_List(void);
  291.  
  292. void Print_Poly_List(void);
  293.  
  294. void Draw_Poly_List(void);
  295.  
  296. void Draw_Triangle_2D(int x1,int y1,
  297.                       int x2,int y2,
  298.                       int x3,int y3,int color);
  299.  
  300. void Draw_Top_Triangle(int x1,int y1, int x2,int y2, int x3,int y3,int color);
  301.  
  302. void Draw_Bottom_Triangle(int x1,int y1, int x2,int y2, int x3,int y3,int color);
  303.  
  304. void Triangle_Line(unsigned char far *dest_addr,
  305.                    unsigned int xs,
  306.                    unsigned int xe,
  307.                    int color);
  308.  
  309. void Draw_Triangle_2D(int x1,int y1,
  310.                       int x2,int y2,
  311.                       int x3,int y3,int color);
  312.  
  313.  
  314. void Make_Grey_Palette(void);
  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. // 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.