accumulationVolume.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. GFXTexHandle gLevelAccuMap;
  46. //#define DEBUG_DRAW
  47. IMPLEMENT_CO_NETOBJECT_V1( AccumulationVolume );
  48. ConsoleDocClass( AccumulationVolume,
  49. "@brief An invisible shape that allow objects within it to have an accumulation map.\n\n"
  50. "AccumulationVolume is used to add additional realism to a scene. It's main use is in outdoor scenes "
  51. " where objects could benefit from overlaying environment accumulation textures such as sand, snow, etc.\n\n"
  52. "Objects within the volume must have accumulation enabled in their material. \n\n"
  53. "@ingroup enviroMisc"
  54. );
  55. //-----------------------------------------------------------------------------
  56. AccumulationVolume::AccumulationVolume()
  57. : mTransformDirty( true ),
  58. mSilhouetteExtractor( mPolyhedron )
  59. {
  60. VECTOR_SET_ASSOCIATION( mWSPoints );
  61. VECTOR_SET_ASSOCIATION( mVolumeQueryList );
  62. //mObjectFlags.set( VisualOccluderFlag );
  63. mNetFlags.set( Ghostable | ScopeAlways );
  64. mObjScale.set( 1.f, 1.f, 1.f );
  65. mObjBox.set(
  66. Point3F( -0.5f, -0.5f, -0.5f ),
  67. Point3F( 0.5f, 0.5f, 0.5f )
  68. );
  69. mObjToWorld.identity();
  70. mWorldToObj.identity();
  71. // Accumulation Texture.
  72. mTextureName = "";
  73. mAccuTexture = NULL;
  74. resetWorldBox();
  75. }
  76. AccumulationVolume::~AccumulationVolume()
  77. {
  78. mAccuTexture = NULL;
  79. }
  80. void AccumulationVolume::initPersistFields()
  81. {
  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 numPoints = mPolyhedron.getNumPoints();
  165. const PolyhedronType::PointType* points = getPolyhedron().getPoints();
  166. mWSPoints.setSize( numPoints );
  167. for( U32 i = 0; i < numPoints; ++ 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. stream->write( mTextureName );
  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. stream->read( &mTextureName );
  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. mTextureName = name;
  208. if ( isClientObject() && mTextureName.isNotEmpty() )
  209. {
  210. mAccuTexture.set(mTextureName, &GFXDefaultStaticDiffuseProfile, "AccumulationVolume::mAccuTexture");
  211. if ( mAccuTexture.isNull() )
  212. Con::warnf( "AccumulationVolume::setTexture - Unable to load texture: %s", mTextureName.c_str() );
  213. }
  214. refreshVolumes();
  215. }
  216. //-----------------------------------------------------------------------------
  217. // Static Functions
  218. //-----------------------------------------------------------------------------
  219. bool AccumulationVolume::_setTexture( void *object, const char *index, const char *data )
  220. {
  221. AccumulationVolume* volume = reinterpret_cast< AccumulationVolume* >( object );
  222. volume->setTexture( data );
  223. return false;
  224. }
  225. void AccumulationVolume::refreshVolumes()
  226. {
  227. // This function tests each accumulation object to
  228. // see if it's within the bounds of an accumulation
  229. // volume. If so, it will pass on the accumulation
  230. // texture of the volume to the object.
  231. // This function should only be called when something
  232. // global like change of volume or material occurs.
  233. // Clear old data.
  234. for (S32 n = 0; n < smAccuObjects.size(); ++n)
  235. {
  236. SimObjectPtr<SceneObject> object = smAccuObjects[n];
  237. if ( object.isValid() )
  238. object->mAccuTex = gLevelAccuMap;
  239. }
  240. //
  241. for (S32 i = 0; i < smAccuVolumes.size(); ++i)
  242. {
  243. SimObjectPtr<AccumulationVolume> volume = smAccuVolumes[i];
  244. if ( volume.isNull() ) continue;
  245. for (S32 n = 0; n < smAccuObjects.size(); ++n)
  246. {
  247. SimObjectPtr<SceneObject> object = smAccuObjects[n];
  248. if ( object.isNull() ) continue;
  249. if ( volume->containsPoint(object->getPosition()) )
  250. object->mAccuTex = volume->mAccuTexture;
  251. }
  252. }
  253. }
  254. // Accumulation Object Management.
  255. void AccumulationVolume::addObject(SimObjectPtr<SceneObject> object)
  256. {
  257. smAccuObjects.push_back(object);
  258. refreshVolumes();
  259. }
  260. void AccumulationVolume::removeObject(SimObjectPtr<SceneObject> object)
  261. {
  262. smAccuObjects.remove(object);
  263. refreshVolumes();
  264. }
  265. void AccumulationVolume::updateObject(SceneObject* object)
  266. {
  267. // This function is called when an individual object
  268. // has been moved. Tests to see if it's in any of the
  269. // accumulation volumes.
  270. // We use ZERO instead of NULL so the accumulation
  271. // texture will be updated in renderMeshMgr.
  272. object->mAccuTex = gLevelAccuMap;
  273. for (S32 i = 0; i < smAccuVolumes.size(); ++i)
  274. {
  275. SimObjectPtr<AccumulationVolume> volume = smAccuVolumes[i];
  276. if ( volume.isNull() ) continue;
  277. if ( volume->containsPoint(object->getPosition()) )
  278. object->mAccuTex = volume->mAccuTexture;
  279. }
  280. }