Torque3D Documentation / _generateds / occlusionVolume.cpp

occlusionVolume.cpp

Engine/source/T3D/occlusionVolume.cpp

More...

Public Functions

ConsoleDocClass(OcclusionVolume , "@brief An invisible shape that causes objects hidden from view behind it <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> not be <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">rendered.\n\n</a>" "<a href="/coding/class/classocclusionvolume/">OcclusionVolume</a> is <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> class <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> scene optimization. It's <a href="/coding/file/x86unixmain_8cpp/#x86unixmain_8cpp_1a217dbf8b442f20279ea00b898af96f52">main</a> use is <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> outdoor spaces where zones " "and portals do not help in optimizing scene culling as they almost only make sense <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> modeling visibility " "in indoor scenarios (and <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> connecting indoor spaces <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> outdoor spaces).\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" "During rendering, every object that is fully behind an occluder \<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" "Be aware that occluders add overhead <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> scene culling. Only <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> this overhead is outweighed by the time " "saved by not rendering hidden objects, is the occluder actually effective. Because of this, chose only those " "spots <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> placing occluders where <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> significant number of objects will be culled from points that the player " "will actually be at during the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">game.\n\n</a>" "Like zones and portals, OcclusionVolumes may have <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> default box shape or <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> more complex \<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" " @see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Scene::maxOccludersPerZone\n</a>" " @see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Scene::occluderMinWidthPercentage\n</a>" " @see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Scene::occluderMinHeightPercentage\n</a>" " @ingroup enviroMisc" )

Detailed Description

Public Functions

ConsoleDocClass(OcclusionVolume , "@brief An invisible shape that causes objects hidden from view behind it <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> not be <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">rendered.\n\n</a>" "<a href="/coding/class/classocclusionvolume/">OcclusionVolume</a> is <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> class <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> scene optimization. It's <a href="/coding/file/x86unixmain_8cpp/#x86unixmain_8cpp_1a217dbf8b442f20279ea00b898af96f52">main</a> use is <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> outdoor spaces where zones " "and portals do not help in optimizing scene culling as they almost only make sense <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> modeling visibility " "in indoor scenarios (and <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> connecting indoor spaces <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> outdoor spaces).\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" "During rendering, every object that is fully behind an occluder \<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" "Be aware that occluders add overhead <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> scene culling. Only <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> this overhead is outweighed by the time " "saved by not rendering hidden objects, is the occluder actually effective. Because of this, chose only those " "spots <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> placing occluders where <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> significant number of objects will be culled from points that the player " "will actually be at during the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">game.\n\n</a>" "Like zones and portals, OcclusionVolumes may have <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> default box shape or <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> more complex \<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" " @see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Scene::maxOccludersPerZone\n</a>" " @see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Scene::occluderMinWidthPercentage\n</a>" " @see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Scene::occluderMinHeightPercentage\n</a>" " @ingroup enviroMisc" )

IMPLEMENT_CO_NETOBJECT_V1(OcclusionVolume )

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