qbsp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena 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 III Arena 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 Foobar; 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 "imagelib.h"
  23. #include "threads.h"
  24. #include "bspfile.h"
  25. #include "shaders.h"
  26. #include "mesh.h"
  27. #define MAX_PATCH_SIZE 32
  28. #define CLIP_EPSILON 0.1
  29. #define PLANENUM_LEAF -1
  30. #define HINT_PRIORITY 1000
  31. typedef struct parseMesh_s {
  32. struct parseMesh_s *next;
  33. mesh_t mesh;
  34. shaderInfo_t *shaderInfo;
  35. qboolean grouped; // used during shared edge grouping
  36. struct parseMesh_s *groupChain;
  37. } parseMesh_t;
  38. typedef struct bspface_s {
  39. struct bspface_s *next;
  40. int planenum;
  41. int priority; // added to value calculation
  42. qboolean checked;
  43. qboolean hint;
  44. winding_t *w;
  45. } bspface_t;
  46. typedef struct plane_s {
  47. vec3_t normal;
  48. vec_t dist;
  49. int type;
  50. struct plane_s *hash_chain;
  51. } plane_t;
  52. typedef struct side_s {
  53. int planenum;
  54. float texMat[2][3]; // brush primitive texture matrix
  55. // for old brush coordinates mode
  56. float vecs[2][4]; // texture coordinate mapping
  57. winding_t *winding;
  58. winding_t *visibleHull; // convex hull of all visible fragments
  59. struct shaderInfo_s *shaderInfo;
  60. int contents; // from shaderInfo
  61. int surfaceFlags; // from shaderInfo
  62. int value; // from shaderInfo
  63. qboolean visible; // choose visble planes first
  64. qboolean bevel; // don't ever use for bsp splitting, and don't bother
  65. // making windings for it
  66. qboolean backSide; // generated side for a q3map_backShader
  67. } side_t;
  68. #define MAX_BRUSH_SIDES 1024
  69. typedef struct bspbrush_s {
  70. struct bspbrush_s *next;
  71. int entitynum; // editor numbering
  72. int brushnum; // editor numbering
  73. struct shaderInfo_s *contentShader;
  74. int contents;
  75. qboolean detail;
  76. qboolean opaque;
  77. int outputNumber; // set when the brush is written to the file list
  78. int portalareas[2];
  79. struct bspbrush_s *original; // chopped up brushes will reference the originals
  80. vec3_t mins, maxs;
  81. int numsides;
  82. side_t sides[6]; // variably sized
  83. } bspbrush_t;
  84. typedef struct drawsurf_s {
  85. shaderInfo_t *shaderInfo;
  86. bspbrush_t *mapBrush; // not valid for patches
  87. side_t *side; // not valid for patches
  88. struct drawsurf_s *nextOnShader; // when sorting by shader for lightmaps
  89. int fogNum; // set by FogDrawSurfs
  90. int lightmapNum; // -1 = no lightmap
  91. int lightmapX, lightmapY;
  92. int lightmapWidth, lightmapHeight;
  93. int numVerts;
  94. drawVert_t *verts;
  95. int numIndexes;
  96. int *indexes;
  97. // for faces only
  98. int planeNum;
  99. vec3_t lightmapOrigin; // also used for flares
  100. vec3_t lightmapVecs[3]; // also used for flares
  101. // for patches only
  102. qboolean patch;
  103. int patchWidth;
  104. int patchHeight;
  105. // for misc_models only
  106. qboolean miscModel;
  107. qboolean flareSurface;
  108. } mapDrawSurface_t;
  109. typedef struct drawSurfRef_s {
  110. struct drawSurfRef_s *nextRef;
  111. int outputNumber;
  112. } drawSurfRef_t;
  113. typedef struct node_s {
  114. // both leafs and nodes
  115. int planenum; // -1 = leaf node
  116. struct node_s *parent;
  117. vec3_t mins, maxs; // valid after portalization
  118. bspbrush_t *volume; // one for each leaf/node
  119. // nodes only
  120. side_t *side; // the side that created the node
  121. struct node_s *children[2];
  122. qboolean hint;
  123. int tinyportals;
  124. vec3_t referencepoint;
  125. // leafs only
  126. qboolean opaque; // view can never be inside
  127. qboolean areaportal;
  128. int cluster; // for portalfile writing
  129. int area; // for areaportals
  130. bspbrush_t *brushlist; // fragments of all brushes in this leaf
  131. drawSurfRef_t *drawSurfReferences; // references to patches pushed down
  132. int occupied; // 1 or greater can reach entity
  133. entity_t *occupant; // for leak file testing
  134. struct portal_s *portals; // also on nodes during construction
  135. } node_t;
  136. typedef struct portal_s {
  137. plane_t plane;
  138. node_t *onnode; // NULL = outside box
  139. node_t *nodes[2]; // [0] = front side of plane
  140. struct portal_s *next[2];
  141. winding_t *winding;
  142. qboolean sidefound; // false if ->side hasn't been checked
  143. qboolean hint;
  144. side_t *side; // NULL = non-visible
  145. } portal_t;
  146. typedef struct {
  147. node_t *headnode;
  148. node_t outside_node;
  149. vec3_t mins, maxs;
  150. } tree_t;
  151. extern int entity_num;
  152. extern qboolean noprune;
  153. extern qboolean nodetail;
  154. extern qboolean fulldetail;
  155. extern qboolean nowater;
  156. extern qboolean noCurveBrushes;
  157. extern qboolean fakemap;
  158. extern qboolean coplanar;
  159. extern qboolean nofog;
  160. extern qboolean testExpand;
  161. extern qboolean showseams;
  162. extern vec_t microvolume;
  163. extern char outbase[32];
  164. extern char source[1024];
  165. extern int samplesize; //sample size in units
  166. extern int novertexlighting;
  167. extern int nogridlighting;
  168. //=============================================================================
  169. // brush.c
  170. int CountBrushList (bspbrush_t *brushes);
  171. bspbrush_t *AllocBrush (int numsides);
  172. void FreeBrush (bspbrush_t *brushes);
  173. void FreeBrushList (bspbrush_t *brushes);
  174. bspbrush_t *CopyBrush (bspbrush_t *brush);
  175. void DrawBrushList (bspbrush_t *brush);
  176. void WriteBrushList (char *name, bspbrush_t *brush, qboolean onlyvis);
  177. void PrintBrush (bspbrush_t *brush);
  178. qboolean BoundBrush (bspbrush_t *brush);
  179. qboolean CreateBrushWindings (bspbrush_t *brush);
  180. bspbrush_t *BrushFromBounds (vec3_t mins, vec3_t maxs);
  181. vec_t BrushVolume (bspbrush_t *brush);
  182. void WriteBspBrushMap (char *name, bspbrush_t *list);
  183. void FilterDetailBrushesIntoTree( entity_t *e, tree_t *tree );
  184. void FilterStructuralBrushesIntoTree( entity_t *e, tree_t *tree );
  185. //=============================================================================
  186. // map.c
  187. extern int entitySourceBrushes;
  188. // mapplanes[ num^1 ] will always be the mirror or mapplanes[ num ]
  189. // nummapplanes will always be even
  190. extern plane_t mapplanes[MAX_MAP_PLANES];
  191. extern int nummapplanes;
  192. extern vec3_t map_mins, map_maxs;
  193. extern char mapIndexedShaders[MAX_MAP_BRUSHSIDES][MAX_QPATH];
  194. extern int numMapIndexedShaders;
  195. extern entity_t *mapent;
  196. #define MAX_BUILD_SIDES 300
  197. extern bspbrush_t *buildBrush;
  198. void LoadMapFile (char *filename);
  199. int FindFloatPlane (vec3_t normal, vec_t dist);
  200. int PlaneTypeForNormal (vec3_t normal);
  201. bspbrush_t *FinishBrush( void );
  202. mapDrawSurface_t *AllocDrawSurf( void );
  203. mapDrawSurface_t *DrawSurfaceForSide( bspbrush_t *b, side_t *s, winding_t *w );
  204. //=============================================================================
  205. //=============================================================================
  206. // draw.c
  207. extern vec3_t draw_mins, draw_maxs;
  208. extern qboolean drawflag;
  209. void Draw_ClearWindow (void);
  210. void DrawWinding (winding_t *w);
  211. void GLS_BeginScene (void);
  212. void GLS_Winding (winding_t *w, int code);
  213. void GLS_EndScene (void);
  214. //=============================================================================
  215. // csg
  216. bspbrush_t *MakeBspBrushList ( bspbrush_t *brushes, vec3_t clipmins, vec3_t clipmaxs);
  217. //=============================================================================
  218. // brushbsp
  219. #define PSIDE_FRONT 1
  220. #define PSIDE_BACK 2
  221. #define PSIDE_BOTH (PSIDE_FRONT|PSIDE_BACK)
  222. #define PSIDE_FACING 4
  223. int BoxOnPlaneSide (vec3_t mins, vec3_t maxs, plane_t *plane);
  224. qboolean WindingIsTiny (winding_t *w);
  225. void SplitBrush (bspbrush_t *brush, int planenum,
  226. bspbrush_t **front, bspbrush_t **back);
  227. tree_t *AllocTree (void);
  228. node_t *AllocNode (void);
  229. tree_t *BrushBSP (bspbrush_t *brushlist, vec3_t mins, vec3_t maxs);
  230. //=============================================================================
  231. // portals.c
  232. void MakeHeadnodePortals (tree_t *tree);
  233. void MakeNodePortal (node_t *node);
  234. void SplitNodePortals (node_t *node);
  235. qboolean Portal_Passable(portal_t *p);
  236. qboolean FloodEntities (tree_t *tree);
  237. void FillOutside (node_t *headnode);
  238. void FloodAreas (tree_t *tree);
  239. bspface_t *VisibleFaces(entity_t *e, tree_t *tree);
  240. void FreePortal (portal_t *p);
  241. void MakeTreePortals (tree_t *tree);
  242. //=============================================================================
  243. // glfile.c
  244. void OutputWinding( winding_t *w, FILE *glview );
  245. void WriteGLView( tree_t *tree, char *source );
  246. //=============================================================================
  247. // leakfile.c
  248. void LeakFile( tree_t *tree );
  249. //=============================================================================
  250. // prtfile.c
  251. void NumberClusters( tree_t *tree );
  252. void WritePortalFile( tree_t *tree );
  253. //=============================================================================
  254. // writebsp.c
  255. void SetModelNumbers (void);
  256. void SetLightStyles (void);
  257. int EmitShader( const char *shader );
  258. void BeginBSPFile (void);
  259. void EndBSPFile (void);
  260. void BeginModel (void);
  261. void EndModel( node_t *headnode );
  262. //=============================================================================
  263. // tree.c
  264. void FreeTree (tree_t *tree);
  265. void FreeTree_r (node_t *node);
  266. void PrintTree_r (node_t *node, int depth);
  267. void FreeTreePortals_r (node_t *node);
  268. //=============================================================================
  269. // patch.c
  270. extern int numMapPatches;
  271. mapDrawSurface_t *DrawSurfaceForMesh( mesh_t *m );
  272. void ParsePatch( void );
  273. mesh_t *SubdivideMesh( mesh_t in, float maxError, float minLength );
  274. void PatchMapDrawSurfs( entity_t *e );
  275. //=============================================================================
  276. // lightmap.c
  277. void AllocateLightmaps( entity_t *e );
  278. //=============================================================================
  279. // tjunction.c
  280. void FixTJunctions( entity_t *e );
  281. //=============================================================================
  282. // fog.c
  283. void FogDrawSurfs( void );
  284. winding_t *WindingFromDrawSurf( mapDrawSurface_t *ds );
  285. //=============================================================================
  286. // facebsp.c
  287. bspface_t *BspFaceForPortal( portal_t *p );
  288. bspface_t *MakeStructuralBspFaceList( bspbrush_t *list );
  289. bspface_t *MakeVisibleBspFaceList( bspbrush_t *list );
  290. tree_t *FaceBSP( bspface_t *list );
  291. //=============================================================================
  292. // misc_model.c
  293. extern int c_triangleModels;
  294. extern int c_triangleSurfaces;
  295. extern int c_triangleVertexes;
  296. extern int c_triangleIndexes;
  297. void AddTriangleModels( tree_t *tree );
  298. //=============================================================================
  299. // surface.c
  300. extern mapDrawSurface_t mapDrawSurfs[MAX_MAP_DRAW_SURFS];
  301. extern int numMapDrawSurfs;
  302. mapDrawSurface_t *AllocDrawSurf( void );
  303. void MergeSides( entity_t *e, tree_t *tree );
  304. void SubdivideDrawSurfs( entity_t *e, tree_t *tree );
  305. void MakeDrawSurfaces( bspbrush_t *b );
  306. void ClipSidesIntoTree( entity_t *e, tree_t *tree );
  307. void FilterDrawsurfsIntoTree( entity_t *e, tree_t *tree );
  308. //==============================================================================
  309. // brush_primit.c
  310. #define BPRIMIT_UNDEFINED 0
  311. #define BPRIMIT_OLDBRUSHES 1
  312. #define BPRIMIT_NEWBRUSHES 2
  313. extern int g_bBrushPrimit;
  314. void ComputeAxisBase( vec3_t normal, vec3_t texX, vec3_t texY);