2
0

occlusionVolume.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "T3D/occlusionVolume.h"
  24. #include "scene/sceneManager.h"
  25. #include "scene/sceneRenderState.h"
  26. #include "gfx/gfxDevice.h"
  27. #include "gfx/gfxDrawUtil.h"
  28. #include "gfx/sim/debugDraw.h"
  29. #include "util/tempAlloc.h"
  30. #include "math/mPolyhedron.impl.h"
  31. //#define DEBUG_DRAW
  32. IMPLEMENT_CO_NETOBJECT_V1( OcclusionVolume );
  33. ConsoleDocClass( OcclusionVolume,
  34. "@brief An invisible shape that causes objects hidden from view behind it to not be rendered.\n\n"
  35. "OcclusionVolume is a class for scene optimization. It's main use is for outdoor spaces where zones "
  36. "and portals do not help in optimizing scene culling as they almost only make sense for modeling visibility "
  37. "in indoor scenarios (and for connecting indoor spaces to outdoor spaces).\n\n"
  38. "During rendering, every object that is fully behind an occluder \n\n"
  39. "Be aware that occluders add overhead to scene culling. Only if this overhead is outweighed by the time "
  40. "saved by not rendering hidden objects, is the occluder actually effective. Because of this, chose only those "
  41. "spots for placing occluders where a significant number of objects will be culled from points that the player "
  42. "will actually be at during the game.\n\n"
  43. "Like zones and portals, OcclusionVolumes may have a default box shape or a more complex \n\n"
  44. "@see Scene::maxOccludersPerZone\n"
  45. "@see Scene::occluderMinWidthPercentage\n"
  46. "@see Scene::occluderMinHeightPercentage\n"
  47. "@ingroup enviroMisc"
  48. );
  49. //-----------------------------------------------------------------------------
  50. OcclusionVolume::OcclusionVolume()
  51. : mTransformDirty( true ),
  52. mSilhouetteExtractor( mPolyhedron )
  53. {
  54. VECTOR_SET_ASSOCIATION( mWSPoints );
  55. mObjectFlags.set( VisualOccluderFlag );
  56. mObjScale.set( 1.f, 1.f, 1.f );
  57. mObjBox.set(
  58. Point3F( -0.5f, -0.5f, -0.5f ),
  59. Point3F( 0.5f, 0.5f, 0.5f )
  60. );
  61. mObjToWorld.identity();
  62. mWorldToObj.identity();
  63. resetWorldBox();
  64. }
  65. //-----------------------------------------------------------------------------
  66. void OcclusionVolume::consoleInit()
  67. {
  68. // Disable rendering of occlusion volumes by default.
  69. getStaticClassRep()->mIsRenderEnabled = false;
  70. }
  71. //-----------------------------------------------------------------------------
  72. bool OcclusionVolume::onAdd()
  73. {
  74. if( !Parent::onAdd() )
  75. return false;
  76. // Set up the silhouette extractor.
  77. mSilhouetteExtractor = SilhouetteExtractorType( mPolyhedron );
  78. return true;
  79. }
  80. //-----------------------------------------------------------------------------
  81. void OcclusionVolume::_renderObject( ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat )
  82. {
  83. Parent::_renderObject( ri, state, overrideMat );
  84. #ifdef DEBUG_DRAW
  85. if( state->isDiffusePass() )
  86. DebugDrawer::get()->drawPolyhedronDebugInfo( mPolyhedron, getTransform(), getScale() );
  87. #endif
  88. }
  89. //-----------------------------------------------------------------------------
  90. void OcclusionVolume::setTransform( const MatrixF& mat )
  91. {
  92. Parent::setTransform( mat );
  93. mTransformDirty = true;
  94. }
  95. //-----------------------------------------------------------------------------
  96. void OcclusionVolume::buildSilhouette( const SceneCameraState& cameraState, Vector< Point3F >& outPoints )
  97. {
  98. // Extract the silhouette of the polyhedron. This works differently
  99. // depending on whether we project orthogonally or in perspective.
  100. TempAlloc< U32 > indices( mPolyhedron.getNumPoints() );
  101. U32 numPoints;
  102. if( cameraState.getFrustum().isOrtho() )
  103. {
  104. // Transform the view direction into object space.
  105. Point3F osViewDir;
  106. getWorldTransform().mulV( cameraState.getViewDirection(), &osViewDir );
  107. // And extract the silhouette.
  108. SilhouetteExtractorOrtho< PolyhedronType > extractor( mPolyhedron );
  109. numPoints = extractor.extractSilhouette( osViewDir, indices, indices.size );
  110. }
  111. else
  112. {
  113. // Create a transform to go from view space to object space.
  114. MatrixF camView( true );
  115. camView.scale( Point3F( 1.0f / getScale().x, 1.0f / getScale().y, 1.0f / getScale().z ) );
  116. camView.mul( getRenderWorldTransform() );
  117. camView.mul( cameraState.getViewWorldMatrix() );
  118. // Do a perspective-correct silhouette extraction.
  119. numPoints = mSilhouetteExtractor.extractSilhouette(
  120. camView,
  121. indices, indices.size );
  122. }
  123. // If we haven't yet, transform the polyhedron's points
  124. // to world space.
  125. if( mTransformDirty )
  126. {
  127. const U32 numPolyPoints = mPolyhedron.getNumPoints();
  128. const PolyhedronType::PointType* points = getPolyhedron().getPoints();
  129. mWSPoints.setSize(numPolyPoints);
  130. for( U32 i = 0; i < numPolyPoints; ++ i )
  131. {
  132. Point3F p = points[ i ];
  133. p.convolve( getScale() );
  134. getTransform().mulP( p, &mWSPoints[ i ] );
  135. }
  136. mTransformDirty = false;
  137. }
  138. // Now store the points.
  139. outPoints.setSize( numPoints );
  140. for( U32 i = 0; i < numPoints; ++ i )
  141. outPoints[ i ] = mWSPoints[ indices[ i ] ];
  142. }