qbsp.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "cmdlib.h"
  19. #include "mathlib.h"
  20. #include "scriplib.h"
  21. #include "polylib.h"
  22. #include "threads.h"
  23. #include "bspfile.h"
  24. #define MAX_BRUSH_SIDES 128
  25. #define CLIP_EPSILON 0.1
  26. #define BOGUS_RANGE 8192
  27. #define TEXINFO_NODE -1 // side is allready on a node
  28. typedef struct plane_s
  29. {
  30. vec3_t normal;
  31. vec_t dist;
  32. int type;
  33. struct plane_s *hash_chain;
  34. } plane_t;
  35. typedef struct
  36. {
  37. vec_t shift[2];
  38. vec_t rotate;
  39. vec_t scale[2];
  40. char name[32];
  41. int flags;
  42. int value;
  43. } brush_texture_t;
  44. typedef struct side_s
  45. {
  46. int planenum;
  47. int texinfo;
  48. winding_t *winding;
  49. struct side_s *original; // bspbrush_t sides will reference the mapbrush_t sides
  50. int contents; // from miptex
  51. int surf; // from miptex
  52. qboolean visible; // choose visble planes first
  53. qboolean tested; // this plane allready checked as a split
  54. qboolean bevel; // don't ever use for bsp splitting
  55. } side_t;
  56. typedef struct brush_s
  57. {
  58. int entitynum;
  59. int brushnum;
  60. int contents;
  61. vec3_t mins, maxs;
  62. int numsides;
  63. side_t *original_sides;
  64. } mapbrush_t;
  65. #define PLANENUM_LEAF -1
  66. #define MAXEDGES 20
  67. typedef struct face_s
  68. {
  69. struct face_s *next; // on node
  70. // the chain of faces off of a node can be merged or split,
  71. // but each face_t along the way will remain in the chain
  72. // until the entire tree is freed
  73. struct face_s *merged; // if set, this face isn't valid anymore
  74. struct face_s *split[2]; // if set, this face isn't valid anymore
  75. struct portal_s *portal;
  76. int texinfo;
  77. int planenum;
  78. int contents; // faces in different contents can't merge
  79. int outputnumber;
  80. winding_t *w;
  81. int numpoints;
  82. qboolean badstartvert; // tjunctions cannot be fixed without a midpoint vertex
  83. int vertexnums[MAXEDGES];
  84. } face_t;
  85. typedef struct bspbrush_s
  86. {
  87. struct bspbrush_s *next;
  88. vec3_t mins, maxs;
  89. int side, testside; // side of node during construction
  90. mapbrush_t *original;
  91. int numsides;
  92. side_t sides[6]; // variably sized
  93. } bspbrush_t;
  94. #define MAX_NODE_BRUSHES 8
  95. typedef struct node_s
  96. {
  97. // both leafs and nodes
  98. int planenum; // -1 = leaf node
  99. struct node_s *parent;
  100. vec3_t mins, maxs; // valid after portalization
  101. bspbrush_t *volume; // one for each leaf/node
  102. // nodes only
  103. qboolean detail_seperator; // a detail brush caused the split
  104. side_t *side; // the side that created the node
  105. struct node_s *children[2];
  106. face_t *faces;
  107. // leafs only
  108. bspbrush_t *brushlist; // fragments of all brushes in this leaf
  109. int contents; // OR of all brush contents
  110. int occupied; // 1 or greater can reach entity
  111. entity_t *occupant; // for leak file testing
  112. int cluster; // for portalfile writing
  113. int area; // for areaportals
  114. struct portal_s *portals; // also on nodes during construction
  115. } node_t;
  116. typedef struct portal_s
  117. {
  118. plane_t plane;
  119. node_t *onnode; // NULL = outside box
  120. node_t *nodes[2]; // [0] = front side of plane
  121. struct portal_s *next[2];
  122. winding_t *winding;
  123. qboolean sidefound; // false if ->side hasn't been checked
  124. side_t *side; // NULL = non-visible
  125. face_t *face[2]; // output face in bsp file
  126. } portal_t;
  127. typedef struct
  128. {
  129. node_t *headnode;
  130. node_t outside_node;
  131. vec3_t mins, maxs;
  132. } tree_t;
  133. extern int entity_num;
  134. extern plane_t mapplanes[MAX_MAP_PLANES];
  135. extern int nummapplanes;
  136. extern int nummapbrushes;
  137. extern mapbrush_t mapbrushes[MAX_MAP_BRUSHES];
  138. extern vec3_t map_mins, map_maxs;
  139. #define MAX_MAP_SIDES (MAX_MAP_BRUSHES*6)
  140. extern int nummapbrushsides;
  141. extern side_t brushsides[MAX_MAP_SIDES];
  142. extern qboolean noprune;
  143. extern qboolean nodetail;
  144. extern qboolean fulldetail;
  145. extern qboolean nomerge;
  146. extern qboolean nosubdiv;
  147. extern qboolean nowater;
  148. extern qboolean noweld;
  149. extern qboolean noshare;
  150. extern qboolean notjunc;
  151. extern vec_t microvolume;
  152. extern char outbase[32];
  153. extern char source[1024];
  154. void LoadMapFile (char *filename);
  155. int FindFloatPlane (vec3_t normal, vec_t dist);
  156. //=============================================================================
  157. // textures.c
  158. typedef struct
  159. {
  160. char name[64];
  161. int flags;
  162. int value;
  163. int contents;
  164. char animname[64];
  165. } textureref_t;
  166. #define MAX_MAP_TEXTURES 1024
  167. extern textureref_t textureref[MAX_MAP_TEXTURES];
  168. int FindMiptex (char *name);
  169. int TexinfoForBrushTexture (plane_t *plane, brush_texture_t *bt, vec3_t origin);
  170. //=============================================================================
  171. void FindGCD (int *v);
  172. mapbrush_t *Brush_LoadEntity (entity_t *ent);
  173. int PlaneTypeForNormal (vec3_t normal);
  174. qboolean MakeBrushPlanes (mapbrush_t *b);
  175. int FindIntPlane (int *inormal, int *iorigin);
  176. void CreateBrush (int brushnum);
  177. //=============================================================================
  178. // draw.c
  179. extern vec3_t draw_mins, draw_maxs;
  180. extern qboolean drawflag;
  181. void Draw_ClearWindow (void);
  182. void DrawWinding (winding_t *w);
  183. void GLS_BeginScene (void);
  184. void GLS_Winding (winding_t *w, int code);
  185. void GLS_EndScene (void);
  186. //=============================================================================
  187. // csg
  188. bspbrush_t *MakeBspBrushList (int startbrush, int endbrush,
  189. vec3_t clipmins, vec3_t clipmaxs);
  190. bspbrush_t *ChopBrushes (bspbrush_t *head);
  191. bspbrush_t *InitialBrushList (bspbrush_t *list);
  192. bspbrush_t *OptimizedBrushList (bspbrush_t *list);
  193. void WriteBrushMap (char *name, bspbrush_t *list);
  194. //=============================================================================
  195. // brushbsp
  196. void WriteBrushList (char *name, bspbrush_t *brush, qboolean onlyvis);
  197. bspbrush_t *CopyBrush (bspbrush_t *brush);
  198. void SplitBrush (bspbrush_t *brush, int planenum,
  199. bspbrush_t **front, bspbrush_t **back);
  200. tree_t *AllocTree (void);
  201. node_t *AllocNode (void);
  202. bspbrush_t *AllocBrush (int numsides);
  203. int CountBrushList (bspbrush_t *brushes);
  204. void FreeBrush (bspbrush_t *brushes);
  205. vec_t BrushVolume (bspbrush_t *brush);
  206. void BoundBrush (bspbrush_t *brush);
  207. void FreeBrushList (bspbrush_t *brushes);
  208. tree_t *BrushBSP (bspbrush_t *brushlist, vec3_t mins, vec3_t maxs);
  209. //=============================================================================
  210. // portals.c
  211. int VisibleContents (int contents);
  212. void MakeHeadnodePortals (tree_t *tree);
  213. void MakeNodePortal (node_t *node);
  214. void SplitNodePortals (node_t *node);
  215. qboolean Portal_VisFlood (portal_t *p);
  216. qboolean FloodEntities (tree_t *tree);
  217. void FillOutside (node_t *headnode);
  218. void FloodAreas (tree_t *tree);
  219. void MarkVisibleSides (tree_t *tree, int start, int end);
  220. void FreePortal (portal_t *p);
  221. void EmitAreaPortals (node_t *headnode);
  222. void MakeTreePortals (tree_t *tree);
  223. //=============================================================================
  224. // glfile.c
  225. void OutputWinding (winding_t *w, FILE *glview);
  226. void WriteGLView (tree_t *tree, char *source);
  227. //=============================================================================
  228. // leakfile.c
  229. void LeakFile (tree_t *tree);
  230. //=============================================================================
  231. // prtfile.c
  232. void WritePortalFile (tree_t *tree);
  233. //=============================================================================
  234. // writebsp.c
  235. void SetModelNumbers (void);
  236. void SetLightStyles (void);
  237. void BeginBSPFile (void);
  238. void WriteBSP (node_t *headnode);
  239. void EndBSPFile (void);
  240. void BeginModel (void);
  241. void EndModel (void);
  242. //=============================================================================
  243. // faces.c
  244. void MakeFaces (node_t *headnode);
  245. void FixTjuncs (node_t *headnode);
  246. int GetEdge2 (int v1, int v2, face_t *f);
  247. face_t *AllocFace (void);
  248. void FreeFace (face_t *f);
  249. void MergeNodeFaces (node_t *node);
  250. //=============================================================================
  251. // tree.c
  252. void FreeTree (tree_t *tree);
  253. void FreeTree_r (node_t *node);
  254. void PrintTree_r (node_t *node, int depth);
  255. void FreeTreePortals_r (node_t *node);
  256. void PruneNodes_r (node_t *node);
  257. void PruneNodes (node_t *node);