1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8 
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.
13 
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.
18 
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/>.
21 
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.
23 
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.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __INTERACTION_H__
30 #define __INTERACTION_H__
31 
32 #include "idlib/bv/Frustum.h"
33 #include "renderer/Model.h"
34 #include "renderer/tr_local.h"
35 
36 /*
37 ===============================================================================
38 
39 	Interaction between entityDef surfaces and a lightDef.
40 
41 	Interactions with no lightTris and no shadowTris are still
42 	valid, because they show that a given entityDef / lightDef
43 	do not interact, even though they share one or more areas.
44 
45 ===============================================================================
46 */
47 
48 #define LIGHT_TRIS_DEFERRED			((srfTriangles_t *)-1)
49 #define LIGHT_CULL_ALL_FRONT		((byte *)-1)
50 #define	LIGHT_CLIP_EPSILON			0.1f
51 
52 
53 typedef struct {
54 	// For each triangle a byte set to 1 if facing the light origin.
55 	byte *					facing;
56 
57 	// For each vertex a byte with the bits [0-5] set if the
58 	// vertex is at the back side of the corresponding clip plane.
59 	// If the 'cullBits' pointer equals LIGHT_CULL_ALL_FRONT all
60 	// vertices are at the front of all the clip planes.
61 	byte *					cullBits;
62 
63 	// Clip planes in surface space used to calculate the cull bits.
64 	idPlane					localClipPlanes[6];
65 } srfCullInfo_t;
66 
67 
68 typedef struct {
69 	// if lightTris == LIGHT_TRIS_DEFERRED, then the calculation of the
70 	// lightTris has been deferred, and must be done if ambientTris is visible
71 	srfTriangles_t *		lightTris;
72 
73 	// shadow volume triangle surface
74 	srfTriangles_t *		shadowTris;
75 
76 	// so we can check ambientViewCount before adding lightTris, and get
77 	// at the shared vertex and possibly shadowVertex caches
78 	srfTriangles_t *		ambientTris;
79 
80 	const idMaterial *		shader;
81 
82 	int						expCulled;			// only for the experimental shadow buffer renderer
83 
84 	srfCullInfo_t			cullInfo;
85 } surfaceInteraction_t;
86 
87 
88 typedef struct areaNumRef_s {
89 	struct areaNumRef_s *	next;
90 	int						areaNum;
91 } areaNumRef_t;
92 
93 
94 class idRenderEntityLocal;
95 class idRenderLightLocal;
96 
97 class idInteraction {
98 public:
99 	// this may be 0 if the light and entity do not actually intersect
100 	// -1 = an untested interaction
101 	int						numSurfaces;
102 
103 	// if there is a whole-entity optimized shadow hull, it will
104 	// be present as a surfaceInteraction_t with a NULL ambientTris, but
105 	// possibly having a shader to specify the shadow sorting order
106 	surfaceInteraction_t *	surfaces;
107 
108 	// get space from here, if NULL, it is a pre-generated shadow volume from dmap
109 	idRenderEntityLocal *	entityDef;
110 	idRenderLightLocal *	lightDef;
111 
112 	idInteraction *			lightNext;				// for lightDef chains
113 	idInteraction *			lightPrev;
114 	idInteraction *			entityNext;				// for entityDef chains
115 	idInteraction *			entityPrev;
116 
117 public:
118 							idInteraction( void );
119 
120 	// because these are generated and freed each game tic for active elements all
121 	// over the world, we use a custom pool allocater to avoid memory allocation overhead
122 	// and fragmentation
123 	static idInteraction *	AllocAndLink( idRenderEntityLocal *edef, idRenderLightLocal *ldef );
124 
125 	// unlinks from the entity and light, frees all surfaceInteractions,
126 	// and puts it back on the free list
127 	void					UnlinkAndFree( void );
128 
129 	// free the interaction surfaces
130 	void					FreeSurfaces( void );
131 
132 	// makes the interaction empty for when the light and entity do not actually intersect
133 	// all empty interactions are linked at the end of the light's and entity's interaction list
134 	void					MakeEmpty( void );
135 
136 	// returns true if the interaction is empty
IsEmpty(void)137 	bool					IsEmpty( void ) const { return ( numSurfaces == 0 ); }
138 
139 	// returns true if the interaction is not yet completely created
IsDeferred(void)140 	bool					IsDeferred( void ) const { return ( numSurfaces == -1 ); }
141 
142 	// returns true if the interaction has shadows
143 	bool					HasShadows( void ) const;
144 
145 	// counts up the memory used by all the surfaceInteractions, which
146 	// will be used to determine when we need to start purging old interactions
147 	int						MemoryUsed( void );
148 
149 	// makes sure all necessary light surfaces and shadow surfaces are created, and
150 	// calls R_LinkLightSurf() for each one
151 	void					AddActiveInteraction( void );
152 
153 private:
154 	enum {
155 		FRUSTUM_UNINITIALIZED,
156 		FRUSTUM_INVALID,
157 		FRUSTUM_VALID,
158 		FRUSTUM_VALIDAREAS,
159 	}						frustumState;
160 	idFrustum				frustum;				// frustum which contains the interaction
161 	areaNumRef_t *			frustumAreas;			// numbers of the areas the frustum touches
162 
163 	int						dynamicModelFrameCount;	// so we can tell if a callback model animated
164 
165 private:
166 	// actually create the interaction
167 	void					CreateInteraction( const idRenderModel *model );
168 
169 	// unlink from entity and light lists
170 	void					Unlink( void );
171 
172 	// try to determine if the entire interaction, including shadows, is guaranteed
173 	// to be outside the view frustum
174 	bool					CullInteractionByViewFrustum( const idFrustum &viewFrustum );
175 
176 	// determine the minimum scissor rect that will include the interaction shadows
177 	// projected to the bounds of the light
178 	idScreenRect			CalcInteractionScissorRectangle( const idFrustum &viewFrustum );
179 };
180 
181 
182 void R_CalcInteractionFacing( const idRenderEntityLocal *ent, const srfTriangles_t *tri, const idRenderLightLocal *light, srfCullInfo_t &cullInfo );
183 void R_CalcInteractionCullBits( const idRenderEntityLocal *ent, const srfTriangles_t *tri, const idRenderLightLocal *light, srfCullInfo_t &cullInfo );
184 void R_FreeInteractionCullInfo( srfCullInfo_t &cullInfo );
185 
186 void R_ShowInteractionMemory_f( const idCmdArgs &args );
187 
188 #endif /* !__INTERACTION_H__ */
189