tsForestItemData.cpp 7.4 KB

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