accumulationVolume.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/accumulationVolume.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 "materials/materialDefinition.h"
  31. #include "materials/materialManager.h"
  32. #include "materials/materialFeatureTypes.h"
  33. #include "materials/matInstance.h"
  34. #include "console/consoleTypes.h"
  35. #include "core/stream/bitStream.h"
  36. #include "gfx/gfxDevice.h"
  37. #include "console/console.h"
  38. #include "console/engineAPI.h"
  39. #include "gfx/gfxTextureHandle.h"
  40. #include "scene/sceneContainer.h"
  41. #include "math/mPolyhedron.impl.h"
  42. #include "scene/mixin/scenePolyhedralObject.impl.h"
  43. Vector< SimObjectPtr<SceneObject> > AccumulationVolume::smAccuObjects;
  44. Vector< SimObjectPtr<AccumulationVolume> > AccumulationVolume::smAccuVolumes;
  45. //#define DEBUG_DRAW
  46. IMPLEMENT_CO_NETOBJECT_V1( AccumulationVolume );
  47. ConsoleDocClass( AccumulationVolume,
  48. "@brief An invisible shape that allow objects within it to have an accumulation map.\n\n"
  49. "AccumulationVolume is used to add additional realism to a scene. It's main use is in outdoor scenes "
  50. " where objects could benefit from overlaying environment accumulation textures such as sand, snow, etc.\n\n"
  51. "Objects within the volume must have accumulation enabled in their material. \n\n"
  52. "@ingroup enviroMisc"
  53. );
  54. //-----------------------------------------------------------------------------
  55. AccumulationVolume::AccumulationVolume()
  56. : mTransformDirty( true ),
  57. mSilhouetteExtractor( mPolyhedron )
  58. {
  59. VECTOR_SET_ASSOCIATION( mWSPoints );
  60. VECTOR_SET_ASSOCIATION( mVolumeQueryList );
  61. //mObjectFlags.set( VisualOccluderFlag );
  62. mNetFlags.set( Ghostable | ScopeAlways );
  63. mObjScale.set( 1.f, 1.f, 1.f );
  64. mObjBox.set(
  65. Point3F( -0.5f, -0.5f, -0.5f ),
  66. Point3F( 0.5f, 0.5f, 0.5f )
  67. );
  68. mObjToWorld.identity();
  69. mWorldToObj.identity();
  70. // Accumulation Texture.
  71. mTextureName = "";
  72. mAccuTexture = NULL;
  73. resetWorldBox();
  74. }
  75. AccumulationVolume::~AccumulationVolume()
  76. {
  77. mAccuTexture = NULL;
  78. }
  79. void AccumulationVolume::initPersistFields()
  80. {
  81. addProtectedField( "texture", TypeStringFilename, Offset( mTextureName, AccumulationVolume ),
  82. &_setTexture, &defaultProtectedGetFn, "Accumulation texture." );
  83. Parent::initPersistFields();
  84. }
  85. //-----------------------------------------------------------------------------
  86. void AccumulationVolume::consoleInit()
  87. {
  88. // Disable rendering of occlusion volumes by default.
  89. getStaticClassRep()->mIsRenderEnabled = false;
  90. }
  91. //-----------------------------------------------------------------------------
  92. bool AccumulationVolume::onAdd()
  93. {
  94. if( !Parent::onAdd() )
  95. return false;
  96. // Prepare some client side things.
  97. if ( isClientObject() )
  98. {
  99. smAccuVolumes.push_back(this);
  100. refreshVolumes();
  101. }
  102. // Set up the silhouette extractor.
  103. mSilhouetteExtractor = SilhouetteExtractorType( mPolyhedron );
  104. return true;
  105. }
  106. void AccumulationVolume::onRemove()
  107. {
  108. if ( isClientObject() )
  109. {
  110. smAccuVolumes.remove(this);
  111. refreshVolumes();
  112. }
  113. Parent::onRemove();
  114. }
  115. //-----------------------------------------------------------------------------
  116. void AccumulationVolume::_renderObject( ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat )
  117. {
  118. Parent::_renderObject( ri, state, overrideMat );
  119. #ifdef DEBUG_DRAW
  120. if( state->isDiffusePass() )
  121. DebugDrawer::get()->drawPolyhedronDebugInfo( mPolyhedron, getTransform(), getScale() );
  122. #endif
  123. }
  124. //-----------------------------------------------------------------------------
  125. void AccumulationVolume::setTransform( const MatrixF& mat )
  126. {
  127. Parent::setTransform( mat );
  128. mTransformDirty = true;
  129. refreshVolumes();
  130. }
  131. //-----------------------------------------------------------------------------
  132. void AccumulationVolume::buildSilhouette( const SceneCameraState& cameraState, Vector< Point3F >& outPoints )
  133. {
  134. // Extract the silhouette of the polyhedron. This works differently
  135. // depending on whether we project orthogonally or in perspective.
  136. TempAlloc< U32 > indices( mPolyhedron.getNumPoints() );
  137. U32 numPoints;
  138. if( cameraState.getFrustum().isOrtho() )
  139. {
  140. // Transform the view direction into object space.
  141. Point3F osViewDir;
  142. getWorldTransform().mulV( cameraState.getViewDirection(), &osViewDir );
  143. // And extract the silhouette.
  144. SilhouetteExtractorOrtho< PolyhedronType > extractor( mPolyhedron );
  145. numPoints = extractor.extractSilhouette( osViewDir, indices, indices.size );
  146. }
  147. else
  148. {
  149. // Create a transform to go from view space to object space.
  150. MatrixF camView( true );
  151. camView.scale( Point3F( 1.0f / getScale().x, 1.0f / getScale().y, 1.0f / getScale().z ) );
  152. camView.mul( getRenderWorldTransform() );
  153. camView.mul( cameraState.getViewWorldMatrix() );
  154. // Do a perspective-correct silhouette extraction.
  155. numPoints = mSilhouetteExtractor.extractSilhouette(
  156. camView,
  157. indices, indices.size );
  158. }
  159. // If we haven't yet, transform the polyhedron's points
  160. // to world space.
  161. if( mTransformDirty )
  162. {
  163. const U32 numPoints = mPolyhedron.getNumPoints();
  164. const PolyhedronType::PointType* points = getPolyhedron().getPoints();
  165. mWSPoints.setSize( numPoints );
  166. for( U32 i = 0; i < numPoints; ++ i )
  167. {
  168. Point3F p = points[ i ];
  169. p.convolve( getScale() );
  170. getTransform().mulP( p, &mWSPoints[ i ] );
  171. }
  172. mTransformDirty = false;
  173. }
  174. // Now store the points.
  175. outPoints.setSize( numPoints );
  176. for( U32 i = 0; i < numPoints; ++ i )
  177. outPoints[ i ] = mWSPoints[ indices[ i ] ];
  178. }
  179. //-----------------------------------------------------------------------------
  180. U32 AccumulationVolume::packUpdate( NetConnection *connection, U32 mask, BitStream *stream )
  181. {
  182. U32 retMask = Parent::packUpdate( connection, mask, stream );
  183. if (stream->writeFlag(mask & InitialUpdateMask))
  184. {
  185. stream->write( mTextureName );
  186. }
  187. return retMask;
  188. }
  189. void AccumulationVolume::unpackUpdate( NetConnection *connection, BitStream *stream )
  190. {
  191. Parent::unpackUpdate( connection, stream );
  192. if (stream->readFlag())
  193. {
  194. stream->read( &mTextureName );
  195. setTexture(mTextureName);
  196. }
  197. }
  198. //-----------------------------------------------------------------------------
  199. void AccumulationVolume::inspectPostApply()
  200. {
  201. Parent::inspectPostApply();
  202. setMaskBits(U32(-1) );
  203. }
  204. void AccumulationVolume::setTexture( const String& name )
  205. {
  206. mTextureName = name;
  207. if ( isClientObject() && mTextureName.isNotEmpty() )
  208. {
  209. mAccuTexture.set(mTextureName, &GFXDefaultStaticDiffuseProfile, "AccumulationVolume::mAccuTexture");
  210. if ( mAccuTexture.isNull() )
  211. Con::warnf( "AccumulationVolume::setTexture - Unable to load texture: %s", mTextureName.c_str() );
  212. }
  213. refreshVolumes();
  214. }
  215. //-----------------------------------------------------------------------------
  216. // Static Functions
  217. //-----------------------------------------------------------------------------
  218. bool AccumulationVolume::_setTexture( void *object, const char *index, const char *data )
  219. {
  220. AccumulationVolume* volume = reinterpret_cast< AccumulationVolume* >( object );
  221. volume->setTexture( data );
  222. return false;
  223. }
  224. void AccumulationVolume::refreshVolumes()
  225. {
  226. // This function tests each accumulation object to
  227. // see if it's within the bounds of an accumulation
  228. // volume. If so, it will pass on the accumulation
  229. // texture of the volume to the object.
  230. // This function should only be called when something
  231. // global like change of volume or material occurs.
  232. // Clear old data.
  233. for (S32 n = 0; n < smAccuObjects.size(); ++n)
  234. {
  235. SimObjectPtr<SceneObject> object = smAccuObjects[n];
  236. if ( object.isValid() )
  237. object->mAccuTex = GFXTexHandle::ZERO;
  238. }
  239. //
  240. for (S32 i = 0; i < smAccuVolumes.size(); ++i)
  241. {
  242. SimObjectPtr<AccumulationVolume> volume = smAccuVolumes[i];
  243. if ( volume.isNull() ) continue;
  244. for (S32 n = 0; n < smAccuObjects.size(); ++n)
  245. {
  246. SimObjectPtr<SceneObject> object = smAccuObjects[n];
  247. if ( object.isNull() ) continue;
  248. if ( volume->containsPoint(object->getPosition()) )
  249. object->mAccuTex = volume->mAccuTexture;
  250. }
  251. }
  252. }
  253. // Accumulation Object Management.
  254. void AccumulationVolume::addObject(SimObjectPtr<SceneObject> object)
  255. {
  256. smAccuObjects.push_back(object);
  257. refreshVolumes();
  258. }
  259. void AccumulationVolume::removeObject(SimObjectPtr<SceneObject> object)
  260. {
  261. smAccuObjects.remove(object);
  262. refreshVolumes();
  263. }
  264. void AccumulationVolume::updateObject(SceneObject* object)
  265. {
  266. // This function is called when an individual object
  267. // has been moved. Tests to see if it's in any of the
  268. // accumulation volumes.
  269. // We use ZERO instead of NULL so the accumulation
  270. // texture will be updated in renderMeshMgr.
  271. object->mAccuTex = GFXTexHandle::ZERO;
  272. for (S32 i = 0; i < smAccuVolumes.size(); ++i)
  273. {
  274. SimObjectPtr<AccumulationVolume> volume = smAccuVolumes[i];
  275. if ( volume.isNull() ) continue;
  276. if ( volume->containsPoint(object->getPosition()) )
  277. object->mAccuTex = volume->mAccuTexture;
  278. }
  279. }