1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4 
5    This file is part of GtkRadiant.
6 
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    GtkRadiant is distributed in the hope that it will be useful,
13    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.
16 
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 /* Files:
23 
24    brushbsp.c
25    csg.c
26    faces.c
27    gldraw.c
28    glfile.c
29    leakfile.c
30    map.c
31    nodraw.c
32    portals.c
33    prtfile.c
34    qbsp3.c
35    textures.c
36    tree.c
37    writebsp.c
38 
39  */
40 
41 #include "cmdlib.h"
42 #include "mathlib.h"
43 #include "scriplib.h"
44 #include "polylib.h"
45 #include "q2_threads.h"
46 #include "bspfile.h"
47 #include "inout.h"
48 
49 #ifdef WIN32
50 	#ifdef NDEBUG                           // Don't show in a Release build
51 		#pragma warning(disable : 4305)     // truncate from double to float
52 		#pragma warning(disable : 4244)     // conversion from double to float
53 		#pragma warning(disable : 4018)     // signed/unsigned mismatch
54 	#endif
55 #endif
56 
57 #define MAX_BRUSH_SIDES 128
58 #define CLIP_EPSILON    0.1
59 
60 #define BOGUS_RANGE 8192
61 
62 #define TEXINFO_NODE        -1      // side is allready on a node
63 
64 typedef struct plane_s
65 {
66 	vec3_t normal;
67 	vec_t dist;
68 	int type;
69 	struct plane_s  *hash_chain;
70 } plane_t;
71 
72 typedef struct
73 {
74 	vec_t shift[2];
75 	vec_t rotate;
76 	vec_t scale[2];
77 	char name[32];
78 	int flags;
79 	int value;
80 } brush_texture_t;
81 
82 typedef struct side_s
83 {
84 	int planenum;
85 	int texinfo;
86 	winding_t   *winding;
87 	struct side_s   *original;  // bspbrush_t sides will reference the mapbrush_t sides
88 	int contents;               // from miptex
89 	int surf;                   // from miptex
90 	qboolean visible;           // choose visble planes first
91 	qboolean tested;            // this plane allready checked as a split
92 	qboolean bevel;             // don't ever use for bsp splitting
93 } side_t;
94 
95 typedef struct brush_s
96 {
97 	int entitynum;
98 	int brushnum;
99 
100 	int contents;
101 
102 	vec3_t mins, maxs;
103 
104 	int numsides;
105 	side_t  *original_sides;
106 } mapbrush_t;
107 
108 #define PLANENUM_LEAF           -1
109 
110 #define MAXEDGES        20
111 
112 typedef struct face_s
113 {
114 	struct face_s   *next;      // on node
115 
116 	// the chain of faces off of a node can be merged or split,
117 	// but each face_t along the way will remain in the chain
118 	// until the entire tree is freed
119 	struct face_s   *merged;    // if set, this face isn't valid anymore
120 	struct face_s   *split[2];  // if set, this face isn't valid anymore
121 
122 	struct portal_s *portal;
123 	int texinfo;
124 	int planenum;
125 	int contents;               // faces in different contents can't merge
126 	int outputnumber;
127 	winding_t       *w;
128 	int numpoints;
129 	qboolean badstartvert;          // tjunctions cannot be fixed without a midpoint vertex
130 	int vertexnums[MAXEDGES];
131 } face_t;
132 
133 
134 
135 typedef struct bspbrush_s
136 {
137 	struct bspbrush_s   *next;
138 	vec3_t mins, maxs;
139 	int side, testside;         // side of node during construction
140 	mapbrush_t  *original;
141 	int numsides;
142 	side_t sides[6];            // variably sized
143 } bspbrush_t;
144 
145 
146 
147 #define MAX_NODE_BRUSHES    8
148 typedef struct node_s
149 {
150 	// both leafs and nodes
151 	int planenum;               // -1 = leaf node
152 	struct node_s   *parent;
153 	vec3_t mins, maxs;          // valid after portalization
154 	bspbrush_t      *volume;    // one for each leaf/node
155 
156 	// nodes only
157 	qboolean detail_seperator;          // a detail brush caused the split
158 	side_t          *side;      // the side that created the node
159 	struct node_s   *children[2];
160 	face_t          *faces;
161 
162 	// leafs only
163 	bspbrush_t      *brushlist; // fragments of all brushes in this leaf
164 	int contents;               // OR of all brush contents
165 	int occupied;               // 1 or greater can reach entity
166 	entity_t        *occupant;  // for leak file testing
167 	int cluster;                // for portalfile writing
168 	int area;                   // for areaportals
169 	struct portal_s *portals;   // also on nodes during construction
170 } node_t;
171 
172 typedef struct portal_s
173 {
174 	plane_t plane;
175 	node_t      *onnode;        // NULL = outside box
176 	node_t      *nodes[2];      // [0] = front side of plane
177 	struct portal_s *next[2];
178 	winding_t   *winding;
179 
180 	qboolean sidefound;         // false if ->side hasn't been checked
181 	side_t      *side;          // NULL = non-visible
182 	face_t      *face[2];       // output face in bsp file
183 } portal_t;
184 
185 typedef struct
186 {
187 	node_t      *headnode;
188 	node_t outside_node;
189 	vec3_t mins, maxs;
190 } tree_t;
191 
192 extern int entity_num;
193 
194 extern plane_t mapplanes[MAX_MAP_PLANES];
195 extern int nummapplanes;
196 
197 extern int nummapbrushes;
198 extern mapbrush_t mapbrushes[MAX_MAP_BRUSHES];
199 
200 extern vec3_t map_mins, map_maxs;
201 
202 #define MAX_MAP_SIDES       ( MAX_MAP_BRUSHES * 6 )
203 
204 extern int nummapbrushsides;
205 extern side_t brushsides[MAX_MAP_SIDES];
206 
207 extern qboolean noprune;
208 extern qboolean nodetail;
209 extern qboolean fulldetail;
210 extern qboolean nomerge;
211 extern qboolean nosubdiv;
212 extern qboolean nowater;
213 extern qboolean noweld;
214 extern qboolean noshare;
215 extern qboolean notjunc;
216 
217 extern vec_t microvolume;
218 
219 extern char outbase[32];
220 
221 extern char source[1024];
222 
223 void    LoadMapFile( char *filename );
224 int     FindFloatPlane( vec3_t normal, vec_t dist );
225 
226 //=============================================================================
227 
228 // textures.c
229 
230 typedef struct
231 {
232 	char name[64];
233 	int flags;
234 	int value;
235 	int contents;
236 	char animname[64];
237 } textureref_t;
238 
239 #define MAX_MAP_TEXTURES    1024
240 
241 extern textureref_t textureref[MAX_MAP_TEXTURES];
242 
243 int FindMiptex( char *name );
244 
245 int TexinfoForBrushTexture( plane_t *plane, brush_texture_t *bt, vec3_t origin );
246 
247 //=============================================================================
248 
249 void FindGCD( int *v );
250 
251 mapbrush_t *Brush_LoadEntity( entity_t *ent );
252 int PlaneTypeForNormal( vec3_t normal );
253 qboolean MakeBrushPlanes( mapbrush_t *b );
254 int     FindIntPlane( int *inormal, int *iorigin );
255 void    CreateBrush( int brushnum );
256 
257 
258 //=============================================================================
259 
260 // draw.c
261 
262 extern vec3_t draw_mins, draw_maxs;
263 extern qboolean drawflag;
264 
265 void Draw_ClearWindow( void );
266 void DrawWinding( winding_t *w );
267 
268 void GLS_BeginScene( void );
269 void GLS_Winding( winding_t *w, int code );
270 void GLS_EndScene( void );
271 
272 //=============================================================================
273 
274 // csg
275 
276 bspbrush_t *MakeBspBrushList( int startbrush, int endbrush,
277 							  vec3_t clipmins, vec3_t clipmaxs );
278 bspbrush_t *ChopBrushes( bspbrush_t *head );
279 bspbrush_t *InitialBrushList( bspbrush_t *list );
280 bspbrush_t *OptimizedBrushList( bspbrush_t *list );
281 
282 void WriteBrushMap( char *name, bspbrush_t *list );
283 
284 //=============================================================================
285 
286 // brushbsp
287 
288 void WriteBrushList( char *name, bspbrush_t *brush, qboolean onlyvis );
289 
290 bspbrush_t *CopyBrush( bspbrush_t *brush );
291 
292 void SplitBrush( bspbrush_t *brush, int planenum,
293 				 bspbrush_t **front, bspbrush_t **back );
294 
295 tree_t *AllocTree( void );
296 node_t *AllocNode( void );
297 bspbrush_t *AllocBrush( int numsides );
298 int CountBrushList( bspbrush_t *brushes );
299 void FreeBrush( bspbrush_t *brushes );
300 vec_t BrushVolume( bspbrush_t *brush );
301 
302 void BoundBrush( bspbrush_t *brush );
303 void FreeBrushList( bspbrush_t *brushes );
304 
305 tree_t *BrushBSP( bspbrush_t *brushlist, vec3_t mins, vec3_t maxs );
306 
307 //=============================================================================
308 
309 // portals.c
310 
311 int VisibleContents( int contents );
312 
313 void MakeHeadnodePortals( tree_t *tree );
314 void MakeNodePortal( node_t *node );
315 void SplitNodePortals( node_t *node );
316 
317 qboolean    Portal_VisFlood( portal_t *p );
318 
319 qboolean FloodEntities( tree_t *tree );
320 void FillOutside( node_t *headnode );
321 void FloodAreas( tree_t *tree );
322 void MarkVisibleSides( tree_t *tree, int start, int end );
323 void FreePortal( portal_t *p );
324 void EmitAreaPortals( node_t *headnode );
325 
326 void MakeTreePortals( tree_t *tree );
327 
328 //=============================================================================
329 
330 // glfile.c
331 
332 void OutputWinding( winding_t *w, FILE *glview );
333 void WriteGLView( tree_t *tree, char *source );
334 
335 //=============================================================================
336 
337 // leakfile.c
338 
339 //void LeakFile (tree_t *tree);
340 xmlNodePtr LeakFile( tree_t *tree );
341 
342 //=============================================================================
343 
344 // prtfile.c
345 
346 void WritePortalFile( tree_t *tree );
347 
348 //=============================================================================
349 
350 // writebsp.c
351 
352 void SetModelNumbers( void );
353 void SetLightStyles( void );
354 
355 void BeginBSPFile( void );
356 void WriteBSP( node_t *headnode );
357 void EndBSPFile( void );
358 void BeginModel( void );
359 void EndModel( void );
360 
361 //=============================================================================
362 
363 // faces.c
364 
365 void MakeFaces( node_t *headnode );
366 void FixTjuncs( node_t *headnode );
367 int GetEdge2( int v1, int v2,  face_t *f );
368 
369 face_t  *AllocFace( void );
370 void FreeFace( face_t *f );
371 
372 void MergeNodeFaces( node_t *node );
373 
374 //=============================================================================
375 
376 // tree.c
377 
378 void FreeTree( tree_t *tree );
379 void FreeTree_r( node_t *node );
380 void PrintTree_r( node_t *node, int depth );
381 void FreeTreePortals_r( node_t *node );
382 void PruneNodes_r( node_t *node );
383 void PruneNodes( node_t *node );
384 
385 //=============================================================================
386 
387 // externs
388 
389 extern char *mapname;
390 extern char game[64];
391