2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
5 This file is part of Quake 2 Tools source code.
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
30 #define MAX_BRUSH_SIDES 128
31 #define CLIP_EPSILON 0.1
33 #define BOGUS_RANGE 8192
35 #define TEXINFO_NODE -1 // side is allready on a node
37 typedef struct plane_s
42 struct plane_s *hash_chain;
60 struct side_s *original; // bspbrush_t sides will reference the mapbrush_t sides
61 int contents; // from miptex
62 int surf; // from miptex
63 qboolean visible; // choose visble planes first
64 qboolean tested; // this plane allready checked as a split
65 qboolean bevel; // don't ever use for bsp splitting
68 typedef struct brush_s
78 side_t *original_sides;
81 #define PLANENUM_LEAF -1
87 struct face_s *next; // on node
89 // the chain of faces off of a node can be merged or split,
90 // but each face_t along the way will remain in the chain
91 // until the entire tree is freed
92 struct face_s *merged; // if set, this face isn't valid anymore
93 struct face_s *split[2]; // if set, this face isn't valid anymore
95 struct portal_s *portal;
98 int contents; // faces in different contents can't merge
102 qboolean badstartvert; // tjunctions cannot be fixed without a midpoint vertex
103 int vertexnums[MAXEDGES];
108 typedef struct bspbrush_s
110 struct bspbrush_s *next;
112 int side, testside; // side of node during construction
113 mapbrush_t *original;
115 side_t sides[6]; // variably sized
120 #define MAX_NODE_BRUSHES 8
121 typedef struct node_s
123 // both leafs and nodes
124 int planenum; // -1 = leaf node
125 struct node_s *parent;
126 vec3_t mins, maxs; // valid after portalization
127 bspbrush_t *volume; // one for each leaf/node
130 qboolean detail_seperator; // a detail brush caused the split
131 side_t *side; // the side that created the node
132 struct node_s *children[2];
136 bspbrush_t *brushlist; // fragments of all brushes in this leaf
137 int contents; // OR of all brush contents
138 int occupied; // 1 or greater can reach entity
139 entity_t *occupant; // for leak file testing
140 int cluster; // for portalfile writing
141 int area; // for areaportals
142 struct portal_s *portals; // also on nodes during construction
145 typedef struct portal_s
148 node_t *onnode; // NULL = outside box
149 node_t *nodes[2]; // [0] = front side of plane
150 struct portal_s *next[2];
153 qboolean sidefound; // false if ->side hasn't been checked
154 side_t *side; // NULL = non-visible
155 face_t *face[2]; // output face in bsp file
165 extern int entity_num;
167 extern plane_t mapplanes[MAX_MAP_PLANES];
168 extern int nummapplanes;
170 extern int nummapbrushes;
171 extern mapbrush_t mapbrushes[MAX_MAP_BRUSHES];
173 extern vec3_t map_mins, map_maxs;
175 #define MAX_MAP_SIDES (MAX_MAP_BRUSHES*6)
177 extern int nummapbrushsides;
178 extern side_t brushsides[MAX_MAP_SIDES];
180 extern qboolean noprune;
181 extern qboolean nodetail;
182 extern qboolean fulldetail;
183 extern qboolean nomerge;
184 extern qboolean nosubdiv;
185 extern qboolean nowater;
186 extern qboolean noweld;
187 extern qboolean noshare;
188 extern qboolean notjunc;
190 extern vec_t microvolume;
192 extern char outbase[32];
194 extern char source[1024];
196 void LoadMapFile (char *filename);
197 int FindFloatPlane (vec3_t normal, vec_t dist);
199 //=============================================================================
212 #define MAX_MAP_TEXTURES 1024
214 extern textureref_t textureref[MAX_MAP_TEXTURES];
216 int FindMiptex (char *name);
218 int TexinfoForBrushTexture (plane_t *plane, brush_texture_t *bt, vec3_t origin);
220 //=============================================================================
222 void FindGCD (int *v);
224 mapbrush_t *Brush_LoadEntity (entity_t *ent);
225 int PlaneTypeForNormal (vec3_t normal);
226 qboolean MakeBrushPlanes (mapbrush_t *b);
227 int FindIntPlane (int *inormal, int *iorigin);
228 void CreateBrush (int brushnum);
231 //=============================================================================
235 extern vec3_t draw_mins, draw_maxs;
236 extern qboolean drawflag;
238 void Draw_ClearWindow (void);
239 void DrawWinding (winding_t *w);
241 void GLS_BeginScene (void);
242 void GLS_Winding (winding_t *w, int code);
243 void GLS_EndScene (void);
245 //=============================================================================
249 bspbrush_t *MakeBspBrushList (int startbrush, int endbrush,
250 vec3_t clipmins, vec3_t clipmaxs);
251 bspbrush_t *ChopBrushes (bspbrush_t *head);
252 bspbrush_t *InitialBrushList (bspbrush_t *list);
253 bspbrush_t *OptimizedBrushList (bspbrush_t *list);
255 void WriteBrushMap (char *name, bspbrush_t *list);
257 //=============================================================================
261 void WriteBrushList (char *name, bspbrush_t *brush, qboolean onlyvis);
263 bspbrush_t *CopyBrush (bspbrush_t *brush);
265 void SplitBrush (bspbrush_t *brush, int planenum,
266 bspbrush_t **front, bspbrush_t **back);
268 tree_t *AllocTree (void);
269 node_t *AllocNode (void);
270 bspbrush_t *AllocBrush (int numsides);
271 int CountBrushList (bspbrush_t *brushes);
272 void FreeBrush (bspbrush_t *brushes);
273 vec_t BrushVolume (bspbrush_t *brush);
275 void BoundBrush (bspbrush_t *brush);
276 void FreeBrushList (bspbrush_t *brushes);
278 tree_t *BrushBSP (bspbrush_t *brushlist, vec3_t mins, vec3_t maxs);
280 //=============================================================================
284 int VisibleContents (int contents);
286 void MakeHeadnodePortals (tree_t *tree);
287 void MakeNodePortal (node_t *node);
288 void SplitNodePortals (node_t *node);
290 qboolean Portal_VisFlood (portal_t *p);
292 qboolean FloodEntities (tree_t *tree);
293 void FillOutside (node_t *headnode);
294 void FloodAreas (tree_t *tree);
295 void MarkVisibleSides (tree_t *tree, int start, int end);
296 void FreePortal (portal_t *p);
297 void EmitAreaPortals (node_t *headnode);
299 void MakeTreePortals (tree_t *tree);
301 //=============================================================================
305 void OutputWinding (winding_t *w, FILE *glview);
306 void WriteGLView (tree_t *tree, char *source);
308 //=============================================================================
312 void LeakFile (tree_t *tree);
314 //=============================================================================
318 void WritePortalFile (tree_t *tree);
320 //=============================================================================
324 void SetModelNumbers (void);
325 void SetLightStyles (void);
327 void BeginBSPFile (void);
328 void WriteBSP (node_t *headnode);
329 void EndBSPFile (void);
330 void BeginModel (void);
331 void EndModel (void);
333 //=============================================================================
337 void MakeFaces (node_t *headnode);
338 void FixTjuncs (node_t *headnode);
339 int GetEdge2 (int v1, int v2, face_t *f);
341 face_t *AllocFace (void);
342 void FreeFace (face_t *f);
344 void MergeNodeFaces (node_t *node);
346 //=============================================================================
350 void FreeTree (tree_t *tree);
351 void FreeTree_r (node_t *node);
352 void PrintTree_r (node_t *node, int depth);
353 void FreeTreePortals_r (node_t *node);
354 void PruneNodes_r (node_t *node);
355 void PruneNodes (node_t *node);