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