tsForestItemData.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "forest/ts/tsForestItemData.h"
  24. #include "forest/ts/tsForestCellBatch.h"
  25. #include "core/resourceManager.h"
  26. #include "ts/tsShapeInstance.h"
  27. #include "ts/tsLastDetail.h"
  28. #include "sim/netConnection.h"
  29. #include "materials/materialManager.h"
  30. #include "forest/windDeformation.h"
  31. using namespace Torque;
  32. IMPLEMENT_CO_DATABLOCK_V1(TSForestItemData);
  33. ConsoleDocClass( TSForestItemData,
  34. "@brief Concrete implementation of ForestItemData which loads and renders "
  35. "dts format shapeFiles.\n\n"
  36. "@ingroup Forest"
  37. );
  38. TSForestItemData::TSForestItemData()
  39. : mIsClientObject( false ),
  40. mShapeInstance( NULL )
  41. {
  42. }
  43. TSForestItemData::~TSForestItemData()
  44. {
  45. }
  46. bool TSForestItemData::preload( bool server, String &errorBuffer )
  47. {
  48. mIsClientObject = !server;
  49. if ( !SimDataBlock::preload( server, errorBuffer ) )
  50. return false;
  51. return true;
  52. }
  53. void TSForestItemData::_updateCollisionDetails()
  54. {
  55. mCollisionDetails.clear();
  56. mLOSDetails.clear();
  57. mShape->findColDetails( false, &mCollisionDetails, &mLOSDetails );
  58. }
  59. bool TSForestItemData::onAdd()
  60. {
  61. if ( !Parent::onAdd() )
  62. return false;
  63. // Register for the resource change signal.
  64. ResourceManager::get().getChangedSignal().notify( this, &TSForestItemData::_onResourceChanged );
  65. return true;
  66. }
  67. void TSForestItemData::onRemove()
  68. {
  69. // Remove the resource change signal.
  70. ResourceManager::get().getChangedSignal().remove( this, &TSForestItemData::_onResourceChanged );
  71. SAFE_DELETE( mShapeInstance );
  72. Parent::onRemove();
  73. }
  74. void TSForestItemData::inspectPostApply()
  75. {
  76. Parent::inspectPostApply();
  77. SAFE_DELETE( mShapeInstance );
  78. _loadShape();
  79. }
  80. void TSForestItemData::_onResourceChanged( const Torque::Path &path )
  81. {
  82. U32 assetStatus = ShapeAsset::getAssetErrCode(mShapeAsset);
  83. if (assetStatus != AssetBase::Ok && assetStatus != AssetBase::UsingFallback)
  84. {
  85. return;
  86. }
  87. if ( path != Path(mShapeAsset->getShapeFilePath()) )
  88. return;
  89. SAFE_DELETE( mShapeInstance );
  90. _loadShape();
  91. getReloadSignal().trigger();
  92. }
  93. void TSForestItemData::_loadShape()
  94. {
  95. U32 assetStatus = ShapeAsset::getAssetErrCode(mShapeAsset);
  96. if (assetStatus != AssetBase::Ok && assetStatus != AssetBase::UsingFallback)
  97. {
  98. return;
  99. }
  100. _setShape(mShapeAssetId);
  101. if ( !(bool)mShape )
  102. return;
  103. if ( mIsClientObject &&
  104. !mShape->preloadMaterialList(mShapeAsset->getShapeFilePath()) )
  105. return;
  106. // Lets add an autobillboard detail if don't have one.
  107. //_checkLastDetail();
  108. _updateCollisionDetails();
  109. }
  110. TSShapeInstance* TSForestItemData::_getShapeInstance() const
  111. {
  112. // Create the shape instance if we haven't already.
  113. if ( !mShapeInstance && mShape )
  114. {
  115. // Create the instance.
  116. mShapeInstance = new TSShapeInstance( mShape, true );
  117. // So we can make OpCode collision calls.
  118. mShapeInstance->prepCollision();
  119. // Get the material features adding the wind effect if
  120. // we have a positive wind scale and have vertex color
  121. // data which is used for the weighting.
  122. FeatureSet features = MATMGR->getDefaultFeatures();
  123. if ( mWindScale > 0.0f && mShape->getVertexFormat()->hasColor() )
  124. {
  125. // We create our own cloned material list to
  126. // enable the wind effects.
  127. features.addFeature( MFT_WindEffect );
  128. mShapeInstance->cloneMaterialList( &features );
  129. }
  130. }
  131. return mShapeInstance;
  132. }
  133. void TSForestItemData::_checkLastDetail()
  134. {
  135. U32 assetStatus = ShapeAsset::getAssetErrCode(mShapeAsset);
  136. if (assetStatus != AssetBase::Ok && assetStatus != AssetBase::UsingFallback)
  137. {
  138. return;
  139. }
  140. const S32 dl = mShape->mSmallestVisibleDL;
  141. const TSDetail *detail = &mShape->details[dl];
  142. // TODO: Expose some real parameters to the datablock maybe?
  143. if ( detail->subShapeNum != -1 )
  144. {
  145. mShape->addImposter(mShapeAsset->getShapeFilePath(), 10, 4, 0, 0, 256, 0, 0 );
  146. // HACK: If i don't do this it crashes!
  147. while ( mShape->detailCollisionAccelerators.size() < mShape->details.size() )
  148. mShape->detailCollisionAccelerators.push_back( NULL );
  149. }
  150. }
  151. TSLastDetail* TSForestItemData::getLastDetail() const
  152. {
  153. // Gotta call this first of the last detail isn't created!
  154. if (!_getShapeInstance())
  155. return NULL;
  156. const S32 dl = mShape->mSmallestVisibleDL;
  157. const TSDetail* detail = &mShape->details[dl];
  158. if ( detail->subShapeNum >= 0 ||
  159. mShape->billboardDetails.size() <= dl )
  160. return NULL;
  161. return mShape->billboardDetails[dl];
  162. }
  163. ForestCellBatch* TSForestItemData::allocateBatch() const
  164. {
  165. TSLastDetail* lastDetail = getLastDetail();
  166. if ( !lastDetail )
  167. return NULL;
  168. return new TSForestCellBatch( lastDetail );
  169. }
  170. bool TSForestItemData::canBillboard( const SceneRenderState *state, const ForestItem &item, F32 distToCamera ) const
  171. {
  172. PROFILE_SCOPE( TSForestItemData_canBillboard );
  173. if ( !mShape )
  174. return false;
  175. // Use the shape instance to do the work it normally does.
  176. TSShapeInstance *shapeInstance = _getShapeInstance();
  177. const S32 dl = shapeInstance->setDetailFromDistance( state, distToCamera / item.getScale() );
  178. // This item has a null LOD... lets consider
  179. // that as being billboarded.
  180. if ( dl < 0 )
  181. return true;
  182. const TSDetail *detail = &mShape->details[dl];
  183. if ( detail->subShapeNum < 0 && dl < mShape->billboardDetails.size() )
  184. return true;
  185. return false;
  186. }
  187. bool TSForestItemData::render( TSRenderState *rdata, const ForestItem &item ) const
  188. {
  189. PROFILE_SCOPE( TSForestItemData_render );
  190. // This shouldn't happen normally at runtime, but during
  191. // development a file change notification on a bad file
  192. // can cause us to get here without a shape.
  193. TSShapeInstance *shapeInst = _getShapeInstance();
  194. if ( !shapeInst )
  195. return false;
  196. const F32 scale = item.getScale();
  197. // Figure out the distance of this item to the camera.
  198. const SceneRenderState *state = rdata->getSceneState();
  199. F32 dist = ( item.getPosition() - state->getDiffuseCameraPosition() ).len();
  200. // TODO: Selecting the lod seems more expensive than
  201. // it should be... we should look to optimize this.
  202. if ( shapeInst->setDetailFromDistance( state, dist / scale ) < 0 )
  203. return false;
  204. // TSShapeInstance::render() uses the
  205. // world matrix for the RenderInst.
  206. MatrixF worldMat = item.getTransform();
  207. worldMat.scale( scale );
  208. GFX->setWorldMatrix( worldMat );
  209. rdata->setMaterialHint( (void*)&item );
  210. // This isn't documented well, but these calls
  211. // don't really render... rather they batch the
  212. // shape to be rendered by the render instance
  213. // manager later on.
  214. shapeInst->animate();
  215. shapeInst->render( *rdata );
  216. return true;
  217. }