home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_12 / black11.h next >
Encoding:
C/C++ Source or Header  |  1995-05-21  |  11.4 KB  |  366 lines

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