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