forest.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #ifndef _H_FOREST_
  23. #define _H_FOREST_
  24. #ifndef _FORESTITEM_H_
  25. #include "forest/forestItem.h"
  26. #endif
  27. #ifndef _FORESTDATAFILE_H_
  28. #include "forest/forestDataFile.h"
  29. #endif
  30. #ifndef _MATHUTIL_FRUSTUM_H_
  31. #include "math/util/frustum.h"
  32. #endif
  33. #ifndef _GFXTEXTUREHANDLE_H_
  34. #include "gfx/gfxTextureHandle.h"
  35. #endif
  36. #ifndef _COLLISION_H_
  37. #include "collision/collision.h"
  38. #endif
  39. #ifndef _SCENEOBJECT_H_
  40. #include "scene/sceneObject.h"
  41. #endif
  42. #ifndef _SIGNAL_H_
  43. #include "core/util/tSignal.h"
  44. #endif
  45. #ifndef __RESOURCE_H__
  46. #include "core/resource.h"
  47. #endif
  48. #ifndef _CONVEX_H_
  49. #include "collision/convex.h"
  50. #endif
  51. class TSShapeInstance;
  52. class Forest;
  53. class ForestItemData;
  54. class ForestData;
  55. class GBitmap;
  56. class IForestCollision;
  57. class IForestMask;
  58. class PhysicsBody;
  59. struct ObjectRenderInst;
  60. struct TreePlacementInfo;
  61. class ForestRayInfo;
  62. class SceneZoneSpaceManager;
  63. struct TreeInfo
  64. {
  65. U32 treeId;
  66. SimObjectId treeTypeId;
  67. F32 distance;
  68. SimObjectId forestId;
  69. Point3F position;
  70. };
  71. typedef Signal<void( Forest *forest )> ForestCreatedSignal;
  72. ///
  73. class Forest : public SceneObject
  74. {
  75. friend class CreateForestEvent;
  76. friend class ForestConvex;
  77. protected:
  78. typedef SceneObject Parent;
  79. /// Collision and Physics
  80. /// @{
  81. Convex* mConvexList;
  82. /// @}
  83. /// The name of the planting data file.
  84. StringTableEntry mDataFileName;
  85. /// The forest data file which defines planting.
  86. Resource<ForestData> mData;
  87. /// Used to scale the tree LODs when rendering into
  88. /// reflections. It should be greater or equal to 1.
  89. F32 mReflectionLodScalar;
  90. /// Set when rezoning of forest cells is required.
  91. bool mZoningDirty;
  92. /// Debug helpers.
  93. static bool smForceImposters;
  94. static bool smDisableImposters;
  95. static bool smDrawCells;
  96. static bool smDrawBounds;
  97. enum MaskBits
  98. {
  99. MediaMask = Parent::NextFreeMask << 1,
  100. LodMask = Parent::NextFreeMask << 2,
  101. NextFreeMask = Parent::NextFreeMask << 3
  102. };
  103. static U32 smTotalCells;
  104. static U32 smCellsRendered;
  105. static U32 smCellItemsRendered;
  106. static U32 smCellsBatched;
  107. static U32 smCellItemsBatched;
  108. static F32 smAverageItemsPerCell;
  109. static void _clearStats(bool);
  110. void _renderCellBounds( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  111. void _onZoningChanged( SceneZoneSpaceManager *zoneManager );
  112. static ForestCreatedSignal smCreatedSignal;
  113. static ForestCreatedSignal smDestroyedSignal;
  114. public:
  115. static ForestCreatedSignal& getCreatedSignal() { return smCreatedSignal; }
  116. static ForestCreatedSignal& getDestroyedSignal() { return smDestroyedSignal; }
  117. Forest();
  118. virtual ~Forest();
  119. DECLARE_CONOBJECT(Forest);
  120. static void consoleInit();
  121. static void initPersistFields();
  122. // SimObject
  123. bool onAdd();
  124. void onRemove();
  125. /// Overloaded from SceneObject to properly update
  126. /// the client side forest when changes occur within
  127. /// the mission editor.
  128. void inspectPostApply();
  129. /// Overloaded from SceneObject for updating the
  130. /// client side position of the forest.
  131. void setTransform( const MatrixF &mat );
  132. void prepRenderImage( SceneRenderState *state );
  133. bool isTreeInRange( const Point2F& point, F32 radius ) const;
  134. // Network
  135. U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
  136. void unpackUpdate( NetConnection *conn, BitStream *stream );
  137. //IForestCollision *getCollision() const { return mCollision; }
  138. // SceneObject - Collision
  139. virtual void buildConvex( const Box3F& box, Convex* convex );
  140. virtual bool buildPolyList( PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere );
  141. virtual bool castRay( const Point3F &start, const Point3F &end, RayInfo *outInfo );
  142. virtual bool castRayRendered( const Point3F &start, const Point3F &end, RayInfo *outInfo );
  143. virtual bool collideBox( const Point3F &start, const Point3F &end, RayInfo *outInfo );
  144. // SceneObject - Other
  145. virtual void applyRadialImpulse( const Point3F &origin, F32 radius, F32 magnitude );
  146. bool castRayBase( const Point3F &start, const Point3F &end, RayInfo *outInfo, bool rendered );
  147. const Resource<ForestData>& getData() const { return mData; }
  148. Resource<ForestData>& getData() { return mData; }
  149. //bool buildPolyList(AbstractPolyList* polyList, const Box3F &box, const SphereF& sphere);
  150. //void buildConvex(const Box3F& box, Convex* convex);
  151. void getLocalWindTrees( const Point3F &camPos, F32 radius, Vector<TreePlacementInfo> *placementInfo );
  152. /// Called to create a new empty planting data file and
  153. /// assign it to this forest.
  154. void createNewFile();
  155. ///
  156. void saveDataFile( const char *path = NULL );
  157. ///
  158. void clear() { mData->clear(); }
  159. /// Called to rebuild the collision state.
  160. void updateCollision();
  161. };
  162. #endif // _H_FOREST_