2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
26 ===========================================================================
29 #include "../../../renderer/tr_local.h"
32 typedef struct primitive_s {
33 struct primitive_s *next;
35 // only one of these will be non-NULL
36 struct bspbrush_s * brush;
37 struct mapTri_s * tris;
42 struct optimizeGroup_s *groups;
43 // we might want to add other fields later
47 idMapEntity * mapEntity; // points into mapFile_t data
50 primitive_t * primitives;
58 // chains of mapTri_t are the general unit of processing
59 typedef struct mapTri_s {
60 struct mapTri_s * next;
62 const idMaterial * material;
63 void * mergeGroup; // we want to avoid merging triangles
64 // from different fixed groups, like guiSurfs and mirrors
65 int planeNum; // not set universally, just in some areas
68 const struct hashVert_s *hashVert[3];
69 struct optVertex_s *optVert[3];
79 #define MAX_PATCH_SIZE 32
81 #define PLANENUM_LEAF -1
83 typedef struct parseMesh_s {
84 struct parseMesh_s *next;
86 const idMaterial * material;
89 typedef struct bspface_s {
90 struct bspface_s * next;
92 bool portal; // all portals will be selected before
94 bool checked; // used by SelectSplitPlaneNum()
99 idVec4 v[2]; // the offset value will always be in the 0.0 to 1.0 range
102 typedef struct side_s {
105 const idMaterial * material;
106 textureVectors_t texVec;
108 idWinding * winding; // only clipped to the other sides of the brush
109 idWinding * visibleHull; // also clipped to the solid parts of the world
113 typedef struct bspbrush_s {
114 struct bspbrush_s * next;
115 struct bspbrush_s * original; // chopped up brushes will reference the originals
117 int entitynum; // editor numbering for messages
118 int brushnum; // editor numbering for messages
120 const idMaterial * contentShader; // one face's shader will determine the volume attributes
124 int outputNumber; // set when the brush is written to the file list
128 side_t sides[6]; // variably sized
132 typedef struct drawSurfRef_s {
133 struct drawSurfRef_s * nextRef;
138 typedef struct node_s {
139 // both leafs and nodes
140 int planenum; // -1 = leaf node
141 struct node_s * parent;
142 idBounds bounds; // valid after portalization
145 side_t * side; // the side that created the node
146 struct node_s * children[2];
147 int nodeNumber; // set after pruning
150 bool opaque; // view can never be inside
152 uBrush_t * brushlist; // fragments of all brushes in this leaf
153 // needed for FindSideForPortal
155 int area; // determined by flood filling up to areaportals
156 int occupied; // 1 or greater can reach entity
157 uEntity_t * occupant; // for leak file testing
159 struct uPortal_s * portals; // also on nodes during construction
163 typedef struct uPortal_s {
165 node_t *onnode; // NULL = outside box
166 node_t *nodes[2]; // [0] = front side of plane
167 struct uPortal_s *next[2];
171 // a tree_t is created by FaceBSP()
172 typedef struct tree_s {
178 #define MAX_QPATH 256 // max length of a game pathname
181 idRenderLightLocal def;
182 char name[MAX_QPATH]; // for naming the shadow volume surface and interactions
183 srfTriangles_t *shadowTris;
186 #define MAX_GROUP_LIGHTS 16
188 typedef struct optimizeGroup_s {
189 struct optimizeGroup_s *nextGroup;
191 idBounds bounds; // set in CarveGroupsByLight
193 // all of these must match to add a triangle to the triList
194 bool smoothed; // curves will never merge with brushes
197 const idMaterial * material;
199 mapLight_t * groupLights[MAX_GROUP_LIGHTS]; // lights effecting this list
200 void * mergeGroup; // if this differs (guiSurfs, mirrors, etc), the
201 // groups will not be combined into model surfaces
202 // after optimization
203 textureVectors_t texVec;
208 mapTri_t * regeneratedTris; // after each island optimization
209 idVec3 axis[2]; // orthogonal to the plane, so optimization can be 2D
212 // all primitives from the map are added to optimzeGroups, creating new ones as needed
213 // each optimizeGroup is then split into the map areas, creating groups in each area
214 // each optimizeGroup is then divided by each light, creating more groups
215 // the final list of groups is then tjunction fixed against all groups, then optimized internally
216 // multiple optimizeGroups will be merged together into .proc surfaces, but no further optimization
220 //=============================================================================
226 SO_MERGE_SURFACES, // 1
227 SO_CULL_OCCLUDED, // 2
228 SO_CLIP_OCCLUDERS, // 3
234 // mapFileBase will contain the qpath without any extension: "maps/test_box"
235 char mapFileBase[1024];
239 idPlaneSet mapPlanes;
242 uEntity_t *uEntities;
246 idList<mapLight_t*> mapLights;
252 bool verboseentities;
259 bool noClipSides; // don't cut sides by solid leafs, use the entire thing
260 bool noLightCarve; // extra triangle subdivision by light frustums
261 shadowOptLevel_t shadowOptLevel;
262 bool noShadow; // don't create optimized shadow volumes
267 int totalShadowTriangles;
268 int totalShadowVerts;
271 extern dmapGlobals_t dmapGlobals;
273 int FindFloatPlane( const idPlane &plane, bool *fixedDegeneracies = NULL );
276 //=============================================================================
281 #define CLIP_EPSILON 0.1f
284 #define PSIDE_FRONT 1
286 #define PSIDE_BOTH (PSIDE_FRONT|PSIDE_BACK)
287 #define PSIDE_FACING 4
289 int CountBrushList (uBrush_t *brushes);
290 uBrush_t *AllocBrush (int numsides);
291 void FreeBrush (uBrush_t *brushes);
292 void FreeBrushList (uBrush_t *brushes);
293 uBrush_t *CopyBrush (uBrush_t *brush);
294 void DrawBrushList (uBrush_t *brush);
295 void PrintBrush (uBrush_t *brush);
296 bool BoundBrush (uBrush_t *brush);
297 bool CreateBrushWindings (uBrush_t *brush);
298 uBrush_t *BrushFromBounds( const idBounds &bounds );
299 float BrushVolume (uBrush_t *brush);
300 void WriteBspBrushMap( const char *name, uBrush_t *list );
302 void FilterBrushesIntoTree( uEntity_t *e );
304 void SplitBrush( uBrush_t *brush, int planenum, uBrush_t **front, uBrush_t **back);
305 node_t *AllocNode( void );
308 //=============================================================================
312 bool LoadDMapFile( const char *filename );
313 void FreeOptimizeGroupList( optimizeGroup_t *groups );
314 void FreeDMapFile( void );
316 //=============================================================================
318 // draw.cpp -- draw debug views either directly, or through glserv.exe
320 void Draw_ClearWindow( void );
321 void DrawWinding( const idWinding *w );
322 void DrawAuxWinding( const idWinding *w );
324 void DrawLine( idVec3 v1, idVec3 v2, int color );
326 void GLS_BeginScene( void );
327 void GLS_Winding( const idWinding *w, int code );
328 void GLS_Triangle( const mapTri_t *tri, int code );
329 void GLS_EndScene( void );
333 //=============================================================================
337 #define MAX_INTER_AREA_PORTALS 1024
344 extern interAreaPortal_t interAreaPortals[MAX_INTER_AREA_PORTALS];
345 extern int numInterAreaPortals;
347 bool FloodEntities( tree_t *tree );
348 void FillOutside( uEntity_t *e );
349 void FloodAreas( uEntity_t *e );
350 void MakeTreePortals( tree_t *tree );
351 void FreePortal( uPortal_t *p );
353 //=============================================================================
355 // glfile.cpp -- write a debug file to be viewd with glview.exe
357 void OutputWinding( idWinding *w, idFile *glview );
358 void WriteGLView( tree_t *tree, char *source );
360 //=============================================================================
364 void LeakFile( tree_t *tree );
366 //=============================================================================
370 tree_t *AllocTree( void );
372 void FreeTree( tree_t *tree );
374 void FreeTree_r( node_t *node );
375 void FreeTreePortals_r( node_t *node );
378 bspface_t *MakeStructuralBspFaceList( primitive_t *list );
379 bspface_t *MakeVisibleBspFaceList( primitive_t *list );
380 tree_t *FaceBSP( bspface_t *list );
382 //=============================================================================
386 mapTri_t *CullTrisInOpaqueLeafs( mapTri_t *triList, tree_t *tree );
387 void ClipSidesByTree( uEntity_t *e );
388 void SplitTrisToSurfaces( mapTri_t *triList, tree_t *tree );
389 void PutPrimitivesInAreas( uEntity_t *e );
390 void Prelight( uEntity_t *e );
392 //=============================================================================
396 struct hashVert_s *GetHashVert( idVec3 &v );
397 void HashTriangles( optimizeGroup_t *groupList );
398 void FreeTJunctionHash( void );
399 int CountGroupListTris( const optimizeGroup_t *groupList );
400 void FixEntityTjunctions( uEntity_t *e );
401 void FixAreaGroupsTjunctions( optimizeGroup_t *groupList );
402 void FixGlobalTjunctions( uEntity_t *e );
404 //=============================================================================
406 // optimize.cpp -- trianlge mesh reoptimization
408 // the shadow volume optimizer call internal optimizer routines, normal triangles
409 // will just be done by OptimizeEntity()
412 typedef struct optVertex_s {
414 idVec3 pv; // projected against planar axis, third value is 0
415 struct optEdge_s *edges;
416 struct optVertex_s *islandLink;
418 bool emited; // when regenerating triangles
421 typedef struct optEdge_s {
422 optVertex_t *v1, *v2;
423 struct optEdge_s *islandLink;
425 bool created; // not one of the original edges
426 bool combined; // combined from two or more colinear edges
427 struct optTri_s *frontTri, *backTri;
428 struct optEdge_s *v1link, *v2link;
431 typedef struct optTri_s {
432 struct optTri_s *next;
439 optimizeGroup_t *group;
446 void OptimizeEntity( uEntity_t *e );
447 void OptimizeGroupList( optimizeGroup_t *groupList );
449 //=============================================================================
453 mapTri_t *AllocTri( void );
454 void FreeTri( mapTri_t *tri );
455 int CountTriList( const mapTri_t *list );
456 mapTri_t *MergeTriLists( mapTri_t *a, mapTri_t *b );
457 mapTri_t *CopyTriList( const mapTri_t *a );
458 void FreeTriList( mapTri_t *a );
459 mapTri_t *CopyMapTri( const mapTri_t *tri );
460 float MapTriArea( const mapTri_t *tri );
461 mapTri_t *RemoveBadTris( const mapTri_t *tri );
462 void BoundTriList( const mapTri_t *list, idBounds &b );
463 void DrawTri( const mapTri_t *tri );
464 void FlipTriList( mapTri_t *tris );
465 void TriVertsFromOriginal( mapTri_t *tri, const mapTri_t *original );
466 void PlaneForTri( const mapTri_t *tri, idPlane &plane );
467 idWinding *WindingForTri( const mapTri_t *tri );
468 mapTri_t *WindingToTriList( const idWinding *w, const mapTri_t *originalTri );
469 void ClipTriList( const mapTri_t *list, const idPlane &plane, float epsilon, mapTri_t **front, mapTri_t **back );
471 //=============================================================================
475 srfTriangles_t *ShareMapTriVerts( const mapTri_t *tris );
476 void WriteOutputFile( void );
478 //=============================================================================
482 srfTriangles_t *CreateLightShadow( optimizeGroup_t *shadowerGroups, const mapLight_t *light );
483 void FreeBeamTree( struct beamTree_s *beamTree );
485 void CarveTriByBeamTree( const struct beamTree_s *beamTree, const mapTri_t *tri, mapTri_t **lit, mapTri_t **unLit );