tsForestItemData.cpp 7.4 KB

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