forestItem.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/forestItem.h"
  24. #include "forest/forestCollision.h"
  25. #include "core/stream/bitStream.h"
  26. #include "console/consoleTypes.h"
  27. IMPLEMENT_CO_DATABLOCK_V1(ForestItemData);
  28. ConsoleDocClass( ForestItemData,
  29. "@brief Base class for defining a type of ForestItem. It does not implement "
  30. "loading or rendering of the shapeFile.\n\n"
  31. "@ingroup Forest"
  32. );
  33. SimSet* ForestItemData::smSet = NULL;
  34. ForestItemData::ForestItemData()
  35. : mNeedPreload( true ),
  36. mRadius( 1 ),
  37. mCollidable( true ),
  38. mWindScale( 0.0f ),
  39. mTrunkBendScale( 0.0f ),
  40. mWindBranchAmp( 0.0f ),
  41. mWindDetailAmp( 0.0f ),
  42. mWindDetailFreq( 0.0f ),
  43. mMass( 5.0f ),
  44. mRigidity( 10.0f ),
  45. mTightnessCoefficient( 0.4f ),
  46. mDampingCoefficient( 0.7f )
  47. {
  48. INIT_ASSET(Shape);
  49. }
  50. void ForestItemData::initPersistFields()
  51. {
  52. Parent::initPersistFields();
  53. addGroup( "Media" );
  54. INITPERSISTFIELD_SHAPEASSET(Shape, ForestItemData, "Shape asset for this item type");
  55. addProtectedField( "shapeFile", TypeShapeFilename, Offset( mShapeName, ForestItemData ), &_setShapeData, &defaultProtectedGetFn,
  56. "Shape file for this item type", AbstractClassRep::FIELD_HideInInspectors );
  57. addField( "collidable", TypeBool, Offset( mCollidable, ForestItemData ),
  58. "Can other objects or spacial queries hit items of this type." );
  59. addField( "radius", TypeF32, Offset( mRadius, ForestItemData ),
  60. "Radius used during placement to ensure items are not crowded." );
  61. endGroup( "Media" );
  62. addGroup( "Wind" );
  63. addField( "mass", TypeF32, Offset( mMass, ForestItemData ),
  64. "Mass used in calculating spring forces on the trunk. Generally how "
  65. "springy a plant is." );
  66. addField( "rigidity", TypeF32, Offset( mRigidity, ForestItemData ),
  67. "Rigidity used in calculating spring forces on the trunk. How much the plant resists the wind force" );
  68. addField( "tightnessCoefficient", TypeF32, Offset( mTightnessCoefficient, ForestItemData ),
  69. "Coefficient used in calculating spring forces on the trunk. "
  70. "How much the plant resists bending." );
  71. addField( "dampingCoefficient", TypeF32, Offset( mDampingCoefficient, ForestItemData ),
  72. "Coefficient used in calculating spring forces on the trunk. "
  73. "Causes oscillation and forces to decay faster over time." );
  74. addField( "windScale", TypeF32, Offset( mWindScale, ForestItemData ),
  75. "Overall scale to the effect of wind." );
  76. addField( "trunkBendScale", TypeF32, Offset( mTrunkBendScale, ForestItemData ),
  77. "Overall bend amount of the tree trunk by wind and impacts." );
  78. addField( "branchAmp", TypeF32, Offset( mWindBranchAmp, ForestItemData ),
  79. "Amplitude of the effect on larger branches." );
  80. addField( "detailAmp", TypeF32, Offset( mWindDetailAmp, ForestItemData ),
  81. "Amplitude of the winds effect on leafs/fronds." );
  82. addField( "detailFreq", TypeF32, Offset( mWindDetailFreq, ForestItemData ),
  83. "Frequency (speed) of the effect on leafs/fronds." );
  84. endGroup( "Wind" );
  85. }
  86. void ForestItemData::consoleInit()
  87. {
  88. }
  89. SimSet* ForestItemData::getSet()
  90. {
  91. if ( !smSet )
  92. {
  93. if ( Sim::findObject( "ForestItemDataSet", smSet ) )
  94. return smSet;
  95. smSet = new SimSet;
  96. smSet->assignName( "ForestItemDataSet" );
  97. smSet->registerObject();
  98. Sim::getRootGroup()->addObject( smSet );
  99. }
  100. return smSet;
  101. }
  102. ForestItemData* ForestItemData::find( const char *name )
  103. {
  104. ForestItemData *result = dynamic_cast<ForestItemData*>( getSet()->findObjectByInternalName( name ) );
  105. if ( !result )
  106. Sim::findObject( name, result );
  107. return result;
  108. }
  109. void ForestItemData::onNameChange( const char *name )
  110. {
  111. setInternalName( name );
  112. }
  113. bool ForestItemData::onAdd()
  114. {
  115. if ( !Parent::onAdd() )
  116. return false;
  117. getSet()->addObject( this );
  118. return true;
  119. }
  120. void ForestItemData::packData(BitStream* stream)
  121. {
  122. Parent::packData(stream);
  123. String localName = getInternalName();
  124. if ( localName.isEmpty() )
  125. localName = getName();
  126. stream->write( localName );
  127. PACKDATA_ASSET(Shape);
  128. stream->writeFlag( mCollidable );
  129. stream->write( mRadius );
  130. stream->write( mMass );
  131. stream->write( mRigidity );
  132. stream->write( mTightnessCoefficient );
  133. stream->write( mDampingCoefficient );
  134. stream->write( mWindScale );
  135. stream->write( mTrunkBendScale );
  136. stream->write( mWindBranchAmp );
  137. stream->write( mWindDetailAmp );
  138. stream->write( mWindDetailFreq );
  139. }
  140. void ForestItemData::unpackData(BitStream* stream)
  141. {
  142. Parent::unpackData(stream);
  143. String localName;
  144. stream->read( &localName );
  145. setInternalName( localName );
  146. UNPACKDATA_ASSET(Shape);
  147. mCollidable = stream->readFlag();
  148. stream->read( &mRadius );
  149. stream->read( &mMass );
  150. stream->read( &mRigidity );
  151. stream->read( &mTightnessCoefficient );
  152. stream->read( &mDampingCoefficient );
  153. stream->read( &mWindScale );
  154. stream->read( &mTrunkBendScale );
  155. stream->read( &mWindBranchAmp );
  156. stream->read( &mWindDetailAmp );
  157. stream->read( &mWindDetailFreq );
  158. }
  159. const ForestItem ForestItem::Invalid;
  160. ForestItem::ForestItem()
  161. : mDataBlock( NULL ),
  162. mTransform( true ),
  163. mScale( 0.0f ),
  164. mKey( 0 ),
  165. mRadius( 0.0f ),
  166. mWorldBox( 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f )
  167. {
  168. }
  169. ForestItem::~ForestItem()
  170. {
  171. }
  172. void ForestItem::setTransform( const MatrixF &xfm, F32 scale )
  173. {
  174. mTransform = xfm;
  175. mScale = scale;
  176. // Cache the world box to improve culling performance.
  177. VectorF objScale( mScale, mScale, mScale );
  178. mWorldBox = getObjBox();
  179. mWorldBox.minExtents.convolve( objScale );
  180. mWorldBox.maxExtents.convolve( objScale );
  181. mTransform.mul( mWorldBox );
  182. // Generate a radius that encompasses the entire box.
  183. mRadius = ( mWorldBox.maxExtents - mWorldBox.minExtents ).len() / 2.0f;
  184. }
  185. void ForestItem::setData( ForestItemData *data )
  186. {
  187. mDataBlock = data;
  188. }