accumulationVolume.cpp 10 KB

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