tsForestItemData.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. if (mShapeAsset.isNull()) return;
  83. if ( path != Path(mShapeAsset->getShapeFilePath()) )
  84. return;
  85. SAFE_DELETE( mShapeInstance );
  86. _loadShape();
  87. getReloadSignal().trigger();
  88. }
  89. void TSForestItemData::_loadShape()
  90. {
  91. if (mShapeAsset.isNull()) return;
  92. _setShape(mShapeAssetId);
  93. if ( !(bool)mShape )
  94. return;
  95. if ( mIsClientObject &&
  96. !mShape->preloadMaterialList(mShapeAsset->getShapeFilePath()) )
  97. return;
  98. // Lets add an autobillboard detail if don't have one.
  99. //_checkLastDetail();
  100. _updateCollisionDetails();
  101. }
  102. TSShapeInstance* TSForestItemData::_getShapeInstance() const
  103. {
  104. // Create the shape instance if we haven't already.
  105. if ( !mShapeInstance && mShape )
  106. {
  107. // Create the instance.
  108. mShapeInstance = new TSShapeInstance( mShape, true );
  109. // So we can make OpCode collision calls.
  110. mShapeInstance->prepCollision();
  111. // Get the material features adding the wind effect if
  112. // we have a positive wind scale and have vertex color
  113. // data which is used for the weighting.
  114. FeatureSet features = MATMGR->getDefaultFeatures();
  115. if ( mWindScale > 0.0f && mShape->getVertexFormat()->hasColor() )
  116. {
  117. // We create our own cloned material list to
  118. // enable the wind effects.
  119. features.addFeature( MFT_WindEffect );
  120. mShapeInstance->cloneMaterialList( &features );
  121. }
  122. }
  123. return mShapeInstance;
  124. }
  125. void TSForestItemData::_checkLastDetail()
  126. {
  127. if (mShapeAsset.isNull()) return;
  128. const S32 dl = mShape->mSmallestVisibleDL;
  129. const TSDetail *detail = &mShape->details[dl];
  130. // TODO: Expose some real parameters to the datablock maybe?
  131. if ( detail->subShapeNum != -1 )
  132. {
  133. mShape->addImposter(mShapeAsset->getShapeFilePath(), 10, 4, 0, 0, 256, 0, 0 );
  134. // HACK: If i don't do this it crashes!
  135. while ( mShape->detailCollisionAccelerators.size() < mShape->details.size() )
  136. mShape->detailCollisionAccelerators.push_back( NULL );
  137. }
  138. }
  139. TSLastDetail* TSForestItemData::getLastDetail() const
  140. {
  141. // Gotta call this first of the last detail isn't created!
  142. if (!_getShapeInstance())
  143. return NULL;
  144. const S32 dl = mShape->mSmallestVisibleDL;
  145. const TSDetail* detail = &mShape->details[dl];
  146. if ( detail->subShapeNum >= 0 ||
  147. mShape->billboardDetails.size() <= dl )
  148. return NULL;
  149. return mShape->billboardDetails[dl];
  150. }
  151. ForestCellBatch* TSForestItemData::allocateBatch() const
  152. {
  153. TSLastDetail* lastDetail = getLastDetail();
  154. if ( !lastDetail )
  155. return NULL;
  156. return new TSForestCellBatch( lastDetail );
  157. }
  158. bool TSForestItemData::canBillboard( const SceneRenderState *state, const ForestItem &item, F32 distToCamera ) const
  159. {
  160. PROFILE_SCOPE( TSForestItemData_canBillboard );
  161. if ( !mShape )
  162. return false;
  163. // Use the shape instance to do the work it normally does.
  164. TSShapeInstance *shapeInstance = _getShapeInstance();
  165. const S32 dl = shapeInstance->setDetailFromDistance( state, distToCamera / item.getScale() );
  166. // This item has a null LOD... lets consider
  167. // that as being billboarded.
  168. if ( dl < 0 )
  169. return true;
  170. const TSDetail *detail = &mShape->details[dl];
  171. if ( detail->subShapeNum < 0 && dl < mShape->billboardDetails.size() )
  172. return true;
  173. return false;
  174. }
  175. bool TSForestItemData::render( TSRenderState *rdata, const ForestItem &item ) const
  176. {
  177. PROFILE_SCOPE( TSForestItemData_render );
  178. // This shouldn't happen normally at runtime, but during
  179. // development a file change notification on a bad file
  180. // can cause us to get here without a shape.
  181. TSShapeInstance *shapeInst = _getShapeInstance();
  182. if ( !shapeInst )
  183. return false;
  184. const F32 scale = item.getScale();
  185. // Figure out the distance of this item to the camera.
  186. const SceneRenderState *state = rdata->getSceneState();
  187. F32 dist = ( item.getPosition() - state->getDiffuseCameraPosition() ).len();
  188. // TODO: Selecting the lod seems more expensive than
  189. // it should be... we should look to optimize this.
  190. if ( shapeInst->setDetailFromDistance( state, dist / scale ) < 0 )
  191. return false;
  192. // TSShapeInstance::render() uses the
  193. // world matrix for the RenderInst.
  194. MatrixF worldMat = item.getTransform();
  195. worldMat.scale( scale );
  196. GFX->setWorldMatrix( worldMat );
  197. rdata->setMaterialHint( (void*)&item );
  198. // This isn't documented well, but these calls
  199. // don't really render... rather they batch the
  200. // shape to be rendered by the render instance
  201. // manager later on.
  202. shapeInst->animate();
  203. shapeInst->render( *rdata );
  204. return true;
  205. }