RenderWorld_local.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. 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.
  18. ===========================================================================
  19. */
  20. #ifndef __RENDERWORLDLOCAL_H__
  21. #define __RENDERWORLDLOCAL_H__
  22. #include "BoundsTrack.h"
  23. // assume any lightDef or entityDef index above this is an internal error
  24. const int LUDICROUS_INDEX = 10000;
  25. typedef struct portal_s {
  26. int intoArea; // area this portal leads to
  27. idWinding * w; // winding points have counter clockwise ordering seen this area
  28. idPlane plane; // view must be on the positive side of the plane to cross
  29. struct portal_s * next; // next portal of the area
  30. struct doublePortal_s * doublePortal;
  31. } portal_t;
  32. typedef struct doublePortal_s {
  33. struct portal_s * portals[2];
  34. int blockingBits; // PS_BLOCK_VIEW, PS_BLOCK_AIR, etc, set by doors that shut them off
  35. // A portal will be considered closed if it is past the
  36. // fog-out point in a fog volume. We only support a single
  37. // fog volume over each portal.
  38. idRenderLightLocal * fogLight;
  39. struct doublePortal_s * nextFoggedPortal;
  40. } doublePortal_t;
  41. typedef struct portalArea_s {
  42. int areaNum;
  43. int connectedAreaNum[NUM_PORTAL_ATTRIBUTES]; // if two areas have matching connectedAreaNum, they are
  44. // not separated by a portal with the apropriate PS_BLOCK_* blockingBits
  45. int viewCount; // set by R_FindViewLightsAndEntities
  46. portal_t * portals; // never changes after load
  47. areaReference_t entityRefs; // head/tail of doubly linked list, may change
  48. areaReference_t lightRefs; // head/tail of doubly linked list, may change
  49. } portalArea_t;
  50. static const int CHILDREN_HAVE_MULTIPLE_AREAS = -2;
  51. static const int AREANUM_SOLID = -1;
  52. typedef struct {
  53. idPlane plane;
  54. int children[2]; // negative numbers are (-1 - areaNumber), 0 = solid
  55. int commonChildrenArea; // if all children are either solid or a single area,
  56. // this is the area number, else CHILDREN_HAVE_MULTIPLE_AREAS
  57. } areaNode_t;
  58. struct reusableDecal_t {
  59. qhandle_t entityHandle;
  60. int lastStartTime;
  61. idRenderModelDecal * decals;
  62. };
  63. struct reusableOverlay_t {
  64. qhandle_t entityHandle;
  65. int lastStartTime;
  66. idRenderModelOverlay * overlays;
  67. };
  68. struct portalStack_t;
  69. class idRenderWorldLocal : public idRenderWorld {
  70. public:
  71. idRenderWorldLocal();
  72. virtual ~idRenderWorldLocal();
  73. virtual bool InitFromMap( const char *mapName );
  74. virtual void ResetLocalRenderModels(); // Fixes a crash when switching between expansion packs in Doom3:BFG
  75. virtual qhandle_t AddEntityDef( const renderEntity_t *re );
  76. virtual void UpdateEntityDef( qhandle_t entityHandle, const renderEntity_t *re );
  77. virtual void FreeEntityDef( qhandle_t entityHandle );
  78. virtual const renderEntity_t *GetRenderEntity( qhandle_t entityHandle ) const;
  79. virtual qhandle_t AddLightDef( const renderLight_t *rlight );
  80. virtual void UpdateLightDef( qhandle_t lightHandle, const renderLight_t *rlight );
  81. virtual void FreeLightDef( qhandle_t lightHandle );
  82. virtual const renderLight_t *GetRenderLight( qhandle_t lightHandle ) const;
  83. virtual bool CheckAreaForPortalSky( int areaNum );
  84. virtual void GenerateAllInteractions();
  85. virtual void RegenerateWorld();
  86. virtual void ProjectDecalOntoWorld( const idFixedWinding &winding, const idVec3 &projectionOrigin, const bool parallel, const float fadeDepth, const idMaterial *material, const int startTime );
  87. virtual void ProjectDecal( qhandle_t entityHandle, const idFixedWinding &winding, const idVec3 &projectionOrigin, const bool parallel, const float fadeDepth, const idMaterial *material, const int startTime );
  88. virtual void ProjectOverlay( qhandle_t entityHandle, const idPlane localTextureAxis[2], const idMaterial *material, const int startTime );
  89. virtual void RemoveDecals( qhandle_t entityHandle );
  90. virtual void SetRenderView( const renderView_t *renderView );
  91. virtual void RenderScene( const renderView_t *renderView );
  92. virtual int NumAreas() const;
  93. virtual int PointInArea( const idVec3 &point ) const;
  94. virtual int BoundsInAreas( const idBounds &bounds, int *areas, int maxAreas ) const;
  95. virtual int NumPortalsInArea( int areaNum );
  96. virtual exitPortal_t GetPortal( int areaNum, int portalNum );
  97. virtual guiPoint_t GuiTrace( qhandle_t entityHandle, const idVec3 start, const idVec3 end ) const;
  98. virtual bool ModelTrace( modelTrace_t &trace, qhandle_t entityHandle, const idVec3 &start, const idVec3 &end, const float radius ) const;
  99. virtual bool Trace( modelTrace_t &trace, const idVec3 &start, const idVec3 &end, const float radius, bool skipDynamic = true, bool skipPlayer = false ) const;
  100. virtual bool FastWorldTrace( modelTrace_t &trace, const idVec3 &start, const idVec3 &end ) const;
  101. virtual void DebugClearLines( int time );
  102. virtual void DebugLine( const idVec4 &color, const idVec3 &start, const idVec3 &end, const int lifetime = 0, const bool depthTest = false );
  103. virtual void DebugArrow( const idVec4 &color, const idVec3 &start, const idVec3 &end, int size, const int lifetime = 0 );
  104. virtual void DebugWinding( const idVec4 &color, const idWinding &w, const idVec3 &origin, const idMat3 &axis, const int lifetime = 0, const bool depthTest = false );
  105. 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 );
  106. virtual void DebugSphere( const idVec4 &color, const idSphere &sphere, const int lifetime = 0, bool depthTest = false );
  107. virtual void DebugBounds( const idVec4 &color, const idBounds &bounds, const idVec3 &org = vec3_origin, const int lifetime = 0 );
  108. virtual void DebugBox( const idVec4 &color, const idBox &box, const int lifetime = 0 );
  109. virtual void DebugCone( const idVec4 &color, const idVec3 &apex, const idVec3 &dir, float radius1, float radius2, const int lifetime = 0 );
  110. virtual void DebugScreenRect( const idVec4 &color, const idScreenRect &rect, const viewDef_t *viewDef, const int lifetime = 0 );
  111. virtual void DebugAxis( const idVec3 &origin, const idMat3 &axis );
  112. virtual void DebugClearPolygons( int time );
  113. virtual void DebugPolygon( const idVec4 &color, const idWinding &winding, const int lifeTime = 0, const bool depthTest = false );
  114. 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 );
  115. //-----------------------
  116. idStr mapName; // ie: maps/tim_dm2.proc, written to demoFile
  117. ID_TIME_T mapTimeStamp; // for fast reloads of the same level
  118. areaNode_t * areaNodes;
  119. int numAreaNodes;
  120. portalArea_t * portalAreas;
  121. int numPortalAreas;
  122. int connectedAreaNum; // incremented every time a door portal state changes
  123. idScreenRect * areaScreenRect;
  124. doublePortal_t * doublePortals;
  125. int numInterAreaPortals;
  126. idList<idRenderModel *, TAG_MODEL> localModels;
  127. idList<idRenderEntityLocal*, TAG_ENTITY> entityDefs;
  128. idList<idRenderLightLocal*, TAG_LIGHT> lightDefs;
  129. idBlockAlloc<areaReference_t, 1024> areaReferenceAllocator;
  130. idBlockAlloc<idInteraction, 256> interactionAllocator;
  131. #ifdef ID_PC
  132. static const int MAX_DECAL_SURFACES = 32;
  133. #else
  134. static const int MAX_DECAL_SURFACES = 16;
  135. #endif
  136. idArray<reusableDecal_t, MAX_DECAL_SURFACES> decals;
  137. idArray<reusableOverlay_t, MAX_DECAL_SURFACES> overlays;
  138. // all light / entity interactions are referenced here for fast lookup without
  139. // having to crawl the doubly linked lists. EnntityDefs are sequential for better
  140. // cache access, because the table is accessed by light in idRenderWorldLocal::CreateLightDefInteractions()
  141. // Growing this table is time consuming, so we add a pad value to the number
  142. // of entityDefs and lightDefs
  143. idInteraction ** interactionTable;
  144. int interactionTableWidth; // entityDefs
  145. int interactionTableHeight; // lightDefs
  146. bool generateAllInteractionsCalled;
  147. //-----------------------
  148. // RenderWorld_load.cpp
  149. idRenderModel * ParseModel( idLexer *src, const char *mapName, ID_TIME_T mapTimeStamp, idFile *fileOut );
  150. idRenderModel * ParseShadowModel( idLexer *src, idFile *fileOut );
  151. void SetupAreaRefs();
  152. void ParseInterAreaPortals( idLexer *src, idFile *fileOut );
  153. void ParseNodes( idLexer *src, idFile *fileOut );
  154. int CommonChildrenArea_r( areaNode_t *node );
  155. void FreeWorld();
  156. void ClearWorld();
  157. void FreeDefs();
  158. void TouchWorldModels();
  159. void AddWorldModelEntities();
  160. void ClearPortalStates();
  161. void ReadBinaryAreaPortals( idFile *file );
  162. void ReadBinaryNodes( idFile *file );
  163. idRenderModel * ReadBinaryModel( idFile *file );
  164. idRenderModel * ReadBinaryShadowModel( idFile *file );
  165. //--------------------------
  166. // RenderWorld_portals.cpp
  167. bool CullEntityByPortals( const idRenderEntityLocal *entity, const portalStack_t *ps );
  168. void AddAreaViewEntities( int areaNum, const portalStack_t *ps );
  169. bool CullLightByPortals( const idRenderLightLocal *light, const portalStack_t *ps );
  170. void AddAreaViewLights( int areaNum, const portalStack_t *ps );
  171. void AddAreaToView( int areaNum, const portalStack_t *ps );
  172. idScreenRect ScreenRectFromWinding( const idWinding *w, const viewEntity_t *space );
  173. bool PortalIsFoggedOut( const portal_t *p );
  174. void FloodViewThroughArea_r( const idVec3 & origin, int areaNum, const portalStack_t *ps );
  175. void FlowViewThroughPortals( const idVec3 & origin, int numPlanes, const idPlane *planes );
  176. void BuildConnectedAreas_r( int areaNum );
  177. void BuildConnectedAreas();
  178. void FindViewLightsAndEntities();
  179. void FloodLightThroughArea_r( idRenderLightLocal *light, int areaNum, const portalStack_t *ps );
  180. void FlowLightThroughPortals( idRenderLightLocal *light );
  181. int NumPortals() const;
  182. qhandle_t FindPortal( const idBounds &b ) const;
  183. void SetPortalState( qhandle_t portal, int blockingBits );
  184. int GetPortalState( qhandle_t portal );
  185. bool AreasAreConnected( int areaNum1, int areaNum2, portalConnection_t connection ) const;
  186. void FloodConnectedAreas( portalArea_t *area, int portalAttributeIndex );
  187. idScreenRect & GetAreaScreenRect( int areaNum ) const { return areaScreenRect[areaNum]; }
  188. void ShowPortals();
  189. //--------------------------
  190. // RenderWorld_demo.cpp
  191. void StartWritingDemo( idDemoFile *demo );
  192. void StopWritingDemo();
  193. bool ProcessDemoCommand( idDemoFile *readDemo, renderView_t *demoRenderView, int *demoTimeOffset );
  194. void WriteLoadMap();
  195. void WriteRenderView( const renderView_t *renderView );
  196. void WriteVisibleDefs( const viewDef_t *viewDef );
  197. void WriteFreeLight( qhandle_t handle );
  198. void WriteFreeEntity( qhandle_t handle );
  199. void WriteRenderLight( qhandle_t handle, const renderLight_t *light );
  200. void WriteRenderEntity( qhandle_t handle, const renderEntity_t *ent );
  201. void ReadRenderEntity();
  202. void ReadRenderLight();
  203. //--------------------------
  204. // RenderWorld.cpp
  205. void ResizeInteractionTable();
  206. void AddEntityRefToArea( idRenderEntityLocal *def, portalArea_t *area );
  207. void AddLightRefToArea( idRenderLightLocal *light, portalArea_t *area );
  208. void RecurseProcBSP_r( modelTrace_t *results, int parentNodeNum, int nodeNum, float p1f, float p2f, const idVec3 &p1, const idVec3 &p2 ) const;
  209. void BoundsInAreas_r( int nodeNum, const idBounds &bounds, int *areas, int *numAreas, int maxAreas ) const;
  210. float DrawTextLength( const char *text, float scale, int len = 0 );
  211. void FreeInteractions();
  212. void PushFrustumIntoTree_r( idRenderEntityLocal *def, idRenderLightLocal *light, const frustumCorners_t & corners, int nodeNum );
  213. void PushFrustumIntoTree( idRenderEntityLocal *def, idRenderLightLocal *light, const idRenderMatrix & frustumTransform, const idBounds & frustumBounds );
  214. idRenderModelDecal * AllocDecal( qhandle_t newEntityHandle, int startTime );
  215. idRenderModelOverlay * AllocOverlay( qhandle_t newEntityHandle, int startTime );
  216. //-------------------------------
  217. // tr_light.c
  218. void CreateLightDefInteractions( idRenderLightLocal * const ldef, const int renderViewID );
  219. };
  220. // if an entity / light combination has been evaluated and found to not genrate any surfaces or shadows,
  221. // the constant INTERACTION_EMPTY will be stored in the interaction table, int contrasts to NULL, which
  222. // means that the combination has not yet been tested for having surfaces.
  223. static idInteraction * const INTERACTION_EMPTY = (idInteraction *)1;
  224. void R_ListRenderLightDefs_f( const idCmdArgs &args );
  225. void R_ListRenderEntityDefs_f( const idCmdArgs &args );
  226. #endif /* !__RENDERWORLDLOCAL_H__ */