forestItem.cpp
Engine/source/forest/forestItem.cpp
Public Functions
ConsoleDocClass(ForestItemData , "@brief Base class <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> defining <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> type of ForestItem. It does not implement " "loading or rendering of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">shapeFile.\n\n</a>" "@ingroup <a href="/coding/class/classforest/">Forest</a>" )
Detailed Description
Public Functions
ConsoleDocClass(ForestItemData , "@brief Base class <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> defining <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> type of ForestItem. It does not implement " "loading or rendering of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">shapeFile.\n\n</a>" "@ingroup <a href="/coding/class/classforest/">Forest</a>" )
IMPLEMENT_CO_DATABLOCK_V1(ForestItemData )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "forest/forestItem.h" 26 27#include "forest/forestCollision.h" 28 29#include "core/stream/bitStream.h" 30#include "console/consoleTypes.h" 31 32IMPLEMENT_CO_DATABLOCK_V1(ForestItemData); 33 34ConsoleDocClass( ForestItemData, 35 "@brief Base class for defining a type of ForestItem. It does not implement " 36 "loading or rendering of the shapeFile.\n\n" 37 "@ingroup Forest" 38); 39 40SimSet* ForestItemData::smSet = NULL; 41 42 43ForestItemData::ForestItemData() 44 : mNeedPreload( true ), 45 mShapeFile( NULL ), 46 mRadius( 1 ), 47 mCollidable( true ), 48 mWindScale( 0.0f ), 49 mTrunkBendScale( 0.0f ), 50 mWindBranchAmp( 0.0f ), 51 mWindDetailAmp( 0.0f ), 52 mWindDetailFreq( 0.0f ), 53 mMass( 5.0f ), 54 mRigidity( 10.0f ), 55 mTightnessCoefficient( 0.4f ), 56 mDampingCoefficient( 0.7f ) 57{ 58} 59 60void ForestItemData::initPersistFields() 61{ 62 Parent::initPersistFields(); 63 64 addGroup( "Media" ); 65 66 addField( "shapeFile", TypeShapeFilename, Offset( mShapeFile, ForestItemData ), 67 "Shape file for this item type" ); 68 69 addField( "collidable", TypeBool, Offset( mCollidable, ForestItemData ), 70 "Can other objects or spacial queries hit items of this type." ); 71 72 addField( "radius", TypeF32, Offset( mRadius, ForestItemData ), 73 "Radius used during placement to ensure items are not crowded." ); 74 75 endGroup( "Media" ); 76 77 addGroup( "Wind" ); 78 79 addField( "mass", TypeF32, Offset( mMass, ForestItemData ), 80 "Mass used in calculating spring forces on the trunk. Generally how " 81 "springy a plant is." ); 82 83 addField( "rigidity", TypeF32, Offset( mRigidity, ForestItemData ), 84 "Rigidity used in calculating spring forces on the trunk. How much the plant resists the wind force" ); 85 86 addField( "tightnessCoefficient", TypeF32, Offset( mTightnessCoefficient, ForestItemData ), 87 "Coefficient used in calculating spring forces on the trunk. " 88 "How much the plant resists bending." ); 89 90 addField( "dampingCoefficient", TypeF32, Offset( mDampingCoefficient, ForestItemData ), 91 "Coefficient used in calculating spring forces on the trunk. " 92 "Causes oscillation and forces to decay faster over time." ); 93 94 addField( "windScale", TypeF32, Offset( mWindScale, ForestItemData ), 95 "Overall scale to the effect of wind." ); 96 97 addField( "trunkBendScale", TypeF32, Offset( mTrunkBendScale, ForestItemData ), 98 "Overall bend amount of the tree trunk by wind and impacts." ); 99 100 addField( "branchAmp", TypeF32, Offset( mWindBranchAmp, ForestItemData ), 101 "Amplitude of the effect on larger branches." ); 102 103 addField( "detailAmp", TypeF32, Offset( mWindDetailAmp, ForestItemData ), 104 "Amplitude of the winds effect on leafs/fronds." ); 105 106 addField( "detailFreq", TypeF32, Offset( mWindDetailFreq, ForestItemData ), 107 "Frequency (speed) of the effect on leafs/fronds." ); 108 109 endGroup( "Wind" ); 110} 111 112void ForestItemData::consoleInit() 113{ 114} 115 116SimSet* ForestItemData::getSet() 117{ 118 if ( !smSet ) 119 { 120 if ( Sim::findObject( "ForestItemDataSet", smSet ) ) 121 return smSet; 122 123 smSet = new SimSet; 124 smSet->assignName( "ForestItemDataSet" ); 125 smSet->registerObject(); 126 Sim::getRootGroup()->addObject( smSet ); 127 } 128 129 return smSet; 130} 131 132ForestItemData* ForestItemData::find( const char *name ) 133{ 134 ForestItemData *result = dynamic_cast<ForestItemData*>( getSet()->findObjectByInternalName( name ) ); 135 if ( !result ) 136 Sim::findObject( name, result ); 137 138 return result; 139} 140 141void ForestItemData::onNameChange( const char *name ) 142{ 143 setInternalName( name ); 144} 145 146bool ForestItemData::onAdd() 147{ 148 if ( !Parent::onAdd() ) 149 return false; 150 151 getSet()->addObject( this ); 152 153 return true; 154} 155 156void ForestItemData::packData(BitStream* stream) 157{ 158 Parent::packData(stream); 159 160 String localName = getInternalName(); 161 if ( localName.isEmpty() ) 162 localName = getName(); 163 164 stream->write( localName ); 165 166 stream->writeString(mShapeFile); 167 168 stream->writeFlag( mCollidable ); 169 170 stream->write( mRadius ); 171 172 stream->write( mMass ); 173 stream->write( mRigidity ); 174 stream->write( mTightnessCoefficient ); 175 stream->write( mDampingCoefficient ); 176 177 stream->write( mWindScale ); 178 stream->write( mTrunkBendScale ); 179 stream->write( mWindBranchAmp ); 180 stream->write( mWindDetailAmp ); 181 stream->write( mWindDetailFreq ); 182} 183 184void ForestItemData::unpackData(BitStream* stream) 185{ 186 Parent::unpackData(stream); 187 188 String localName; 189 stream->read( &localName ); 190 setInternalName( localName ); 191 192 char readBuffer[1024]; 193 194 stream->readString(readBuffer); 195 mShapeFile = StringTable->insert(readBuffer); 196 197 mCollidable = stream->readFlag(); 198 199 stream->read( &mRadius ); 200 201 stream->read( &mMass ); 202 stream->read( &mRigidity ); 203 stream->read( &mTightnessCoefficient ); 204 stream->read( &mDampingCoefficient ); 205 206 stream->read( &mWindScale ); 207 stream->read( &mTrunkBendScale ); 208 stream->read( &mWindBranchAmp ); 209 stream->read( &mWindDetailAmp ); 210 stream->read( &mWindDetailFreq ); 211} 212 213const ForestItem ForestItem::Invalid; 214 215ForestItem::ForestItem() 216 : mDataBlock( NULL ), 217 mTransform( true ), 218 mScale( 0.0f ), 219 mKey( 0 ), 220 mRadius( 0.0f ), 221 mWorldBox( 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f ) 222{ 223} 224 225ForestItem::~ForestItem() 226{ 227} 228 229void ForestItem::setTransform( const MatrixF &xfm, F32 scale ) 230{ 231 mTransform = xfm; 232 mScale = scale; 233 234 // Cache the world box to improve culling performance. 235 VectorF objScale( mScale, mScale, mScale ); 236 mWorldBox = getObjBox(); 237 mWorldBox.minExtents.convolve( objScale ); 238 mWorldBox.maxExtents.convolve( objScale ); 239 mTransform.mul( mWorldBox ); 240 241 // Generate a radius that encompasses the entire box. 242 mRadius = ( mWorldBox.maxExtents - mWorldBox.minExtents ).len() / 2.0f; 243} 244 245void ForestItem::setData( ForestItemData *data ) 246{ 247 mDataBlock = data; 248} 249 250 251