accumulationVolume.cpp 10 KB

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