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 #ifndef __RENDERWORLDLOCAL_H__
30 #define __RENDERWORLDLOCAL_H__
32 // assume any lightDef or entityDef index above this is an internal error
33 const int LUDICROUS_INDEX = 10000;
36 typedef struct portal_s {
37 int intoArea; // area this portal leads to
38 idWinding * w; // winding points have counter clockwise ordering seen this area
39 idPlane plane; // view must be on the positive side of the plane to cross
40 struct portal_s * next; // next portal of the area
41 struct doublePortal_s * doublePortal;
45 typedef struct doublePortal_s {
46 struct portal_s * portals[2];
47 int blockingBits; // PS_BLOCK_VIEW, PS_BLOCK_AIR, etc, set by doors that shut them off
49 // A portal will be considered closed if it is past the
50 // fog-out point in a fog volume. We only support a single
51 // fog volume over each portal.
52 idRenderLightLocal * fogLight;
53 struct doublePortal_s * nextFoggedPortal;
57 typedef struct portalArea_s {
59 int connectedAreaNum[NUM_PORTAL_ATTRIBUTES]; // if two areas have matching connectedAreaNum, they are
60 // not separated by a portal with the apropriate PS_BLOCK_* blockingBits
61 int viewCount; // set by R_FindViewLightsAndEntities
62 portal_t * portals; // never changes after load
63 areaReference_t entityRefs; // head/tail of doubly linked list, may change
64 areaReference_t lightRefs; // head/tail of doubly linked list, may change
68 static const int CHILDREN_HAVE_MULTIPLE_AREAS = -2;
69 static const int AREANUM_SOLID = -1;
72 int children[2]; // negative numbers are (-1 - areaNumber), 0 = solid
73 int commonChildrenArea; // if all children are either solid or a single area,
74 // this is the area number, else CHILDREN_HAVE_MULTIPLE_AREAS
78 class idRenderWorldLocal : public idRenderWorld {
81 virtual ~idRenderWorldLocal();
83 virtual qhandle_t AddEntityDef( const renderEntity_t *re );
84 virtual void UpdateEntityDef( qhandle_t entityHandle, const renderEntity_t *re );
85 virtual void FreeEntityDef( qhandle_t entityHandle );
86 virtual const renderEntity_t *GetRenderEntity( qhandle_t entityHandle ) const;
88 virtual qhandle_t AddLightDef( const renderLight_t *rlight );
89 virtual void UpdateLightDef( qhandle_t lightHandle, const renderLight_t *rlight );
90 virtual void FreeLightDef( qhandle_t lightHandle );
91 virtual const renderLight_t *GetRenderLight( qhandle_t lightHandle ) const;
93 virtual bool CheckAreaForPortalSky( int areaNum );
95 virtual void GenerateAllInteractions();
96 virtual void RegenerateWorld();
98 virtual void ProjectDecalOntoWorld( const idFixedWinding &winding, const idVec3 &projectionOrigin, const bool parallel, const float fadeDepth, const idMaterial *material, const int startTime );
99 virtual void ProjectDecal( qhandle_t entityHandle, const idFixedWinding &winding, const idVec3 &projectionOrigin, const bool parallel, const float fadeDepth, const idMaterial *material, const int startTime );
100 virtual void ProjectOverlay( qhandle_t entityHandle, const idPlane localTextureAxis[2], const idMaterial *material );
101 virtual void RemoveDecals( qhandle_t entityHandle );
103 virtual void SetRenderView( const renderView_t *renderView );
104 virtual void RenderScene( const renderView_t *renderView );
106 virtual int NumAreas( void ) const;
107 virtual int PointInArea( const idVec3 &point ) const;
108 virtual int BoundsInAreas( const idBounds &bounds, int *areas, int maxAreas ) const;
109 virtual int NumPortalsInArea( int areaNum );
110 virtual exitPortal_t GetPortal( int areaNum, int portalNum );
112 virtual guiPoint_t GuiTrace( qhandle_t entityHandle, const idVec3 start, const idVec3 end ) const;
113 virtual bool ModelTrace( modelTrace_t &trace, qhandle_t entityHandle, const idVec3 &start, const idVec3 &end, const float radius ) const;
114 virtual bool Trace( modelTrace_t &trace, const idVec3 &start, const idVec3 &end, const float radius, bool skipDynamic = true, bool skipPlayer = false ) const;
115 virtual bool FastWorldTrace( modelTrace_t &trace, const idVec3 &start, const idVec3 &end ) const;
117 virtual void DebugClearLines( int time );
118 virtual void DebugLine( const idVec4 &color, const idVec3 &start, const idVec3 &end, const int lifetime = 0, const bool depthTest = false );
119 virtual void DebugArrow( const idVec4 &color, const idVec3 &start, const idVec3 &end, int size, const int lifetime = 0 );
120 virtual void DebugWinding( const idVec4 &color, const idWinding &w, const idVec3 &origin, const idMat3 &axis, const int lifetime = 0, const bool depthTest = false );
121 virtual void DebugCircle( const idVec4 &color, const idVec3 &origin, const idVec3 &dir, const float radius, const int numSteps, const int lifetime = 0, const bool depthTest = false );
122 virtual void DebugSphere( const idVec4 &color, const idSphere &sphere, const int lifetime = 0, bool depthTest = false );
123 virtual void DebugBounds( const idVec4 &color, const idBounds &bounds, const idVec3 &org = vec3_origin, const int lifetime = 0 );
124 virtual void DebugBox( const idVec4 &color, const idBox &box, const int lifetime = 0 );
125 virtual void DebugFrustum( const idVec4 &color, const idFrustum &frustum, const bool showFromOrigin = false, const int lifetime = 0 );
126 virtual void DebugCone( const idVec4 &color, const idVec3 &apex, const idVec3 &dir, float radius1, float radius2, const int lifetime = 0 );
127 virtual void DebugScreenRect( const idVec4 &color, const idScreenRect &rect, const viewDef_t *viewDef, const int lifetime = 0 );
128 virtual void DebugAxis( const idVec3 &origin, const idMat3 &axis );
130 virtual void DebugClearPolygons( int time );
131 virtual void DebugPolygon( const idVec4 &color, const idWinding &winding, const int lifeTime = 0, const bool depthTest = false );
133 virtual void DrawText( const char *text, const idVec3 &origin, float scale, const idVec4 &color, const idMat3 &viewAxis, const int align = 1, const int lifetime = 0, bool depthTest = false );
135 //-----------------------
137 idStr mapName; // ie: maps/tim_dm2.proc, written to demoFile
138 ID_TIME_T mapTimeStamp; // for fast reloads of the same level
140 areaNode_t * areaNodes;
143 portalArea_t * portalAreas;
145 int connectedAreaNum; // incremented every time a door portal state changes
147 idScreenRect * areaScreenRect;
149 doublePortal_t * doublePortals;
150 int numInterAreaPortals;
152 idList<idRenderModel *> localModels;
154 idList<idRenderEntityLocal*> entityDefs;
155 idList<idRenderLightLocal*> lightDefs;
157 idBlockAlloc<areaReference_t, 1024> areaReferenceAllocator;
158 idBlockAlloc<idInteraction, 256> interactionAllocator;
159 idBlockAlloc<areaNumRef_t, 1024> areaNumRefAllocator;
161 // all light / entity interactions are referenced here for fast lookup without
162 // having to crawl the doubly linked lists. EnntityDefs are sequential for better
163 // cache access, because the table is accessed by light in idRenderWorldLocal::CreateLightDefInteractions()
164 // Growing this table is time consuming, so we add a pad value to the number
165 // of entityDefs and lightDefs
166 idInteraction ** interactionTable;
167 int interactionTableWidth; // entityDefs
168 int interactionTableHeight; // lightDefs
171 bool generateAllInteractionsCalled;
173 //-----------------------
174 // RenderWorld_load.cpp
176 idRenderModel * ParseModel( idLexer *src );
177 idRenderModel * ParseShadowModel( idLexer *src );
178 void SetupAreaRefs();
179 void ParseInterAreaPortals( idLexer *src );
180 void ParseNodes( idLexer *src );
181 int CommonChildrenArea_r( areaNode_t *node );
185 void TouchWorldModels( void );
186 void AddWorldModelEntities();
187 void ClearPortalStates();
188 virtual bool InitFromMap( const char *mapName );
190 //--------------------------
191 // RenderWorld_portals.cpp
193 idScreenRect ScreenRectFromWinding( const idWinding *w, viewEntity_t *space );
194 bool PortalIsFoggedOut( const portal_t *p );
195 void FloodViewThroughArea_r( const idVec3 origin, int areaNum, const struct portalStack_s *ps );
196 void FlowViewThroughPortals( const idVec3 origin, int numPlanes, const idPlane *planes );
197 void FloodLightThroughArea_r( idRenderLightLocal *light, int areaNum, const struct portalStack_s *ps );
198 void FlowLightThroughPortals( idRenderLightLocal *light );
199 areaNumRef_t * FloodFrustumAreas_r( const idFrustum &frustum, const int areaNum, const idBounds &bounds, areaNumRef_t *areas );
200 areaNumRef_t * FloodFrustumAreas( const idFrustum &frustum, areaNumRef_t *areas );
201 bool CullEntityByPortals( const idRenderEntityLocal *entity, const struct portalStack_s *ps );
202 void AddAreaEntityRefs( int areaNum, const struct portalStack_s *ps );
203 bool CullLightByPortals( const idRenderLightLocal *light, const struct portalStack_s *ps );
204 void AddAreaLightRefs( int areaNum, const struct portalStack_s *ps );
205 void AddAreaRefs( int areaNum, const struct portalStack_s *ps );
206 void BuildConnectedAreas_r( int areaNum );
207 void BuildConnectedAreas( void );
208 void FindViewLightsAndEntities( void );
210 int NumPortals( void ) const;
211 qhandle_t FindPortal( const idBounds &b ) const;
212 void SetPortalState( qhandle_t portal, int blockingBits );
213 int GetPortalState( qhandle_t portal );
214 bool AreasAreConnected( int areaNum1, int areaNum2, portalConnection_t connection );
215 void FloodConnectedAreas( portalArea_t *area, int portalAttributeIndex );
216 idScreenRect & GetAreaScreenRect( int areaNum ) const { return areaScreenRect[areaNum]; }
219 //--------------------------
220 // RenderWorld_demo.cpp
222 void StartWritingDemo( idDemoFile *demo );
223 void StopWritingDemo();
224 bool ProcessDemoCommand( idDemoFile *readDemo, renderView_t *demoRenderView, int *demoTimeOffset );
227 void WriteRenderView( const renderView_t *renderView );
228 void WriteVisibleDefs( const viewDef_t *viewDef );
229 void WriteFreeLight( qhandle_t handle );
230 void WriteFreeEntity( qhandle_t handle );
231 void WriteRenderLight( qhandle_t handle, const renderLight_t *light );
232 void WriteRenderEntity( qhandle_t handle, const renderEntity_t *ent );
233 void ReadRenderEntity();
234 void ReadRenderLight();
237 //--------------------------
240 void ResizeInteractionTable();
242 void AddEntityRefToArea( idRenderEntityLocal *def, portalArea_t *area );
243 void AddLightRefToArea( idRenderLightLocal *light, portalArea_t *area );
245 void RecurseProcBSP_r( modelTrace_t *results, int parentNodeNum, int nodeNum, float p1f, float p2f, const idVec3 &p1, const idVec3 &p2 ) const;
247 void BoundsInAreas_r( int nodeNum, const idBounds &bounds, int *areas, int *numAreas, int maxAreas ) const;
249 float DrawTextLength( const char *text, float scale, int len = 0 );
251 void FreeInteractions();
253 void PushVolumeIntoTree_r( idRenderEntityLocal *def, idRenderLightLocal *light, const idSphere *sphere, int numPoints, const idVec3 (*points), int nodeNum );
255 void PushVolumeIntoTree( idRenderEntityLocal *def, idRenderLightLocal *light, int numPoints, const idVec3 (*points) );
257 //-------------------------------
259 void CreateLightDefInteractions( idRenderLightLocal *ldef );
262 #endif /* !__RENDERWORLDLOCAL_H__ */