accumulationVolume.cpp 10.0 KB

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