forestItem.cpp 7.1 KB

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