accumulationVolume.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. Vector< SimObjectPtr<SceneObject> > AccumulationVolume::smAccuObjects;
  43. Vector< SimObjectPtr<AccumulationVolume> > AccumulationVolume::smAccuVolumes;
  44. GFXTexHandle gLevelAccuMap;
  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. INIT_ASSET(Texture);
  72. resetWorldBox();
  73. }
  74. AccumulationVolume::~AccumulationVolume()
  75. {
  76. mTexture = nullptr;
  77. }
  78. void AccumulationVolume::initPersistFields()
  79. {
  80. addProtectedField("textureAsset", TypeImageAssetId, Offset(mTextureAssetId, AccumulationVolume),
  81. &_setTexture, &defaultProtectedGetFn, "Accumulation texture.");
  82. addProtectedField( "texture", TypeStringFilename, Offset( mTextureName, AccumulationVolume ),
  83. &_setTexture, &defaultProtectedGetFn, "Accumulation texture." );
  84. Parent::initPersistFields();
  85. }
  86. //-----------------------------------------------------------------------------
  87. void AccumulationVolume::consoleInit()
  88. {
  89. // Disable rendering of occlusion volumes by default.
  90. getStaticClassRep()->mIsRenderEnabled = false;
  91. }
  92. //-----------------------------------------------------------------------------
  93. bool AccumulationVolume::onAdd()
  94. {
  95. if( !Parent::onAdd() )
  96. return false;
  97. // Prepare some client side things.
  98. if ( isClientObject() )
  99. {
  100. smAccuVolumes.push_back(this);
  101. refreshVolumes();
  102. }
  103. // Set up the silhouette extractor.
  104. mSilhouetteExtractor = SilhouetteExtractorType( mPolyhedron );
  105. return true;
  106. }
  107. void AccumulationVolume::onRemove()
  108. {
  109. if ( isClientObject() )
  110. {
  111. smAccuVolumes.remove(this);
  112. refreshVolumes();
  113. }
  114. Parent::onRemove();
  115. }
  116. //-----------------------------------------------------------------------------
  117. void AccumulationVolume::_renderObject( ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat )
  118. {
  119. Parent::_renderObject( ri, state, overrideMat );
  120. #ifdef DEBUG_DRAW
  121. if( state->isDiffusePass() )
  122. DebugDrawer::get()->drawPolyhedronDebugInfo( mPolyhedron, getTransform(), getScale() );
  123. #endif
  124. }
  125. //-----------------------------------------------------------------------------
  126. void AccumulationVolume::setTransform( const MatrixF& mat )
  127. {
  128. Parent::setTransform( mat );
  129. mTransformDirty = true;
  130. refreshVolumes();
  131. }
  132. //-----------------------------------------------------------------------------
  133. void AccumulationVolume::buildSilhouette( const SceneCameraState& cameraState, Vector< Point3F >& outPoints )
  134. {
  135. // Extract the silhouette of the polyhedron. This works differently
  136. // depending on whether we project orthogonally or in perspective.
  137. TempAlloc< U32 > indices( mPolyhedron.getNumPoints() );
  138. U32 numPoints;
  139. if( cameraState.getFrustum().isOrtho() )
  140. {
  141. // Transform the view direction into object space.
  142. Point3F osViewDir;
  143. getWorldTransform().mulV( cameraState.getViewDirection(), &osViewDir );
  144. // And extract the silhouette.
  145. SilhouetteExtractorOrtho< PolyhedronType > extractor( mPolyhedron );
  146. numPoints = extractor.extractSilhouette( osViewDir, indices, indices.size );
  147. }
  148. else
  149. {
  150. // Create a transform to go from view space to object space.
  151. MatrixF camView( true );
  152. camView.scale( Point3F( 1.0f / getScale().x, 1.0f / getScale().y, 1.0f / getScale().z ) );
  153. camView.mul( getRenderWorldTransform() );
  154. camView.mul( cameraState.getViewWorldMatrix() );
  155. // Do a perspective-correct silhouette extraction.
  156. numPoints = mSilhouetteExtractor.extractSilhouette(
  157. camView,
  158. indices, indices.size );
  159. }
  160. // If we haven't yet, transform the polyhedron's points
  161. // to world space.
  162. if( mTransformDirty )
  163. {
  164. const U32 numPolyPoints = mPolyhedron.getNumPoints();
  165. const PolyhedronType::PointType* points = getPolyhedron().getPoints();
  166. mWSPoints.setSize(numPolyPoints);
  167. for( U32 i = 0; i < numPolyPoints; ++ i )
  168. {
  169. Point3F p = points[ i ];
  170. p.convolve( getScale() );
  171. getTransform().mulP( p, &mWSPoints[ i ] );
  172. }
  173. mTransformDirty = false;
  174. }
  175. // Now store the points.
  176. outPoints.setSize( numPoints );
  177. for( U32 i = 0; i < numPoints; ++ i )
  178. outPoints[ i ] = mWSPoints[ indices[ i ] ];
  179. }
  180. //-----------------------------------------------------------------------------
  181. U32 AccumulationVolume::packUpdate( NetConnection *connection, U32 mask, BitStream *stream )
  182. {
  183. U32 retMask = Parent::packUpdate( connection, mask, stream );
  184. if (stream->writeFlag(mask & InitialUpdateMask))
  185. {
  186. PACK_ASSET(connection, Texture);
  187. }
  188. return retMask;
  189. }
  190. void AccumulationVolume::unpackUpdate( NetConnection *connection, BitStream *stream )
  191. {
  192. Parent::unpackUpdate( connection, stream );
  193. if (stream->readFlag())
  194. {
  195. UNPACK_ASSET(connection, Texture);
  196. //setTexture(mTextureName);
  197. }
  198. }
  199. //-----------------------------------------------------------------------------
  200. void AccumulationVolume::inspectPostApply()
  201. {
  202. Parent::inspectPostApply();
  203. setMaskBits(U32(-1) );
  204. }
  205. void AccumulationVolume::setTexture( const String& name )
  206. {
  207. _setTexture(StringTable->insert(name.c_str()));
  208. refreshVolumes();
  209. }
  210. //-----------------------------------------------------------------------------
  211. // Static Functions
  212. //-----------------------------------------------------------------------------
  213. bool AccumulationVolume::_setTexture( void *object, const char *index, const char *data )
  214. {
  215. AccumulationVolume* volume = reinterpret_cast< AccumulationVolume* >( object );
  216. volume->setTexture( data );
  217. return false;
  218. }
  219. void AccumulationVolume::refreshVolumes()
  220. {
  221. // This function tests each accumulation object to
  222. // see if it's within the bounds of an accumulation
  223. // volume. If so, it will pass on the accumulation
  224. // texture of the volume to the object.
  225. // This function should only be called when something
  226. // global like change of volume or material occurs.
  227. // Clear old data.
  228. for (S32 n = 0; n < smAccuObjects.size(); ++n)
  229. {
  230. SimObjectPtr<SceneObject> object = smAccuObjects[n];
  231. if ( object.isValid() )
  232. object->mAccuTex = gLevelAccuMap;
  233. }
  234. //
  235. for (S32 i = 0; i < smAccuVolumes.size(); ++i)
  236. {
  237. SimObjectPtr<AccumulationVolume> volume = smAccuVolumes[i];
  238. if ( volume.isNull() ) continue;
  239. for (S32 n = 0; n < smAccuObjects.size(); ++n)
  240. {
  241. SimObjectPtr<SceneObject> object = smAccuObjects[n];
  242. if ( object.isNull() ) continue;
  243. if ( volume->containsPoint(object->getPosition()) )
  244. object->mAccuTex = volume->getTextureResource();
  245. }
  246. }
  247. }
  248. // Accumulation Object Management.
  249. void AccumulationVolume::addObject(SimObjectPtr<SceneObject> object)
  250. {
  251. smAccuObjects.push_back(object);
  252. refreshVolumes();
  253. }
  254. void AccumulationVolume::removeObject(SimObjectPtr<SceneObject> object)
  255. {
  256. smAccuObjects.remove(object);
  257. refreshVolumes();
  258. }
  259. void AccumulationVolume::updateObject(SceneObject* object)
  260. {
  261. // This function is called when an individual object
  262. // has been moved. Tests to see if it's in any of the
  263. // accumulation volumes.
  264. // We use ZERO instead of NULL so the accumulation
  265. // texture will be updated in renderMeshMgr.
  266. object->mAccuTex = gLevelAccuMap;
  267. for (S32 i = 0; i < smAccuVolumes.size(); ++i)
  268. {
  269. SimObjectPtr<AccumulationVolume> volume = smAccuVolumes[i];
  270. if ( volume.isNull() ) continue;
  271. if ( volume->containsPoint(object->getPosition()) )
  272. object->mAccuTex = volume->getTextureResource();
  273. }
  274. }