Interaction.h 6.8 KB

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