speedTreeNode.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Filename: speedTreeNode.h
  2. // Created by: drose (30Sep10)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #ifndef SPEEDTREENODE_H
  15. #define SPEEDTREENODE_H
  16. #include "pandabase.h"
  17. #include "pandaNode.h"
  18. #include "pointerTo.h"
  19. #include "stTree.h"
  20. #include "stTransform.h"
  21. #include "stTerrain.h"
  22. #include "callbackObject.h"
  23. #include "loaderOptions.h"
  24. #include "transformState.h"
  25. #include "nodePath.h"
  26. #include "pStatCollector.h"
  27. #include "randomizer.h"
  28. #include "speedtree_api.h"
  29. class Loader;
  30. // There is a SpeedTree bug that prevents reliably deleting a
  31. // CForestRender object, as of version 5.2. Presumably it will be
  32. // fixed beginning in version 5.3.
  33. #if SPEEDTREE_VERSION_MAJOR > 5 || (SPEEDTREE_VERSION_MAJOR == 5 && SPEEDTREE_VERSION_MINOR >= 3)
  34. #undef ST_DELETE_FOREST_HACK
  35. #else
  36. #define ST_DELETE_FOREST_HACK
  37. #endif
  38. ////////////////////////////////////////////////////////////////////
  39. // Class : SpeedTreeNode
  40. // Description : Interfaces with the SpeedTree library to render
  41. // SpeedTree objects, especially trees, within the
  42. // Panda3D scene graph.
  43. //
  44. // SpeedTree also includes some support for a simple
  45. // terrain system, which is available here as well.
  46. // SpeedTree's rather lame grass system is not presently
  47. // exposed.
  48. ////////////////////////////////////////////////////////////////////
  49. class EXPCL_PANDASPEEDTREE SpeedTreeNode : public PandaNode {
  50. private:
  51. // This definition is required by InstanceList, below.
  52. typedef pvector<SpeedTree::CInstance> STInstances;
  53. PUBLISHED:
  54. // This nested class keeps a linear list of transforms, for the
  55. // purpose of recording instances of a particular STTree. It is
  56. // used below.
  57. class InstanceList {
  58. public:
  59. INLINE InstanceList(const STTree *tree);
  60. INLINE bool operator < (const InstanceList &other) const;
  61. PUBLISHED:
  62. INLINE const STTree *get_tree() const;
  63. INLINE int get_num_instances() const;
  64. INLINE STTransform get_instance(int n) const;
  65. MAKE_SEQ(get_instances, get_num_instances, get_instance);
  66. INLINE void set_instance(int n, const STTransform &transform);
  67. INLINE int add_instance(const STTransform &transform);
  68. INLINE void remove_instance(int n);
  69. void output(ostream &out) const;
  70. void write(ostream &out, int indent_level = 0) const;
  71. public:
  72. void write_datagram(BamWriter *manager, Datagram &dg);
  73. void fillin(DatagramIterator &scan, BamReader *manager);
  74. private:
  75. PT(STTree) _tree;
  76. STInstances _instances;
  77. friend class SpeedTreeNode;
  78. };
  79. PUBLISHED:
  80. SpeedTreeNode(const string &name);
  81. virtual ~SpeedTreeNode();
  82. INLINE bool is_valid() const;
  83. INLINE int get_num_trees() const;
  84. INLINE const STTree *get_tree(int n) const;
  85. MAKE_SEQ(get_trees, get_num_trees, get_tree);
  86. const InstanceList &get_instance_list(int n) const;
  87. MAKE_SEQ(get_instance_lists, get_num_trees, get_instance_list);
  88. INLINE STTree *modify_tree(int n);
  89. int count_total_instances() const;
  90. InstanceList &add_tree(const STTree *tree);
  91. int remove_tree(const STTree *tree);
  92. void remove_all_trees();
  93. bool has_instance_list(const STTree *tree) const;
  94. const InstanceList &get_instance_list(const STTree *tree) const;
  95. InstanceList &modify_instance_list(const STTree *tree);
  96. void add_instance(const STTree *tree, const STTransform &transform);
  97. void add_instances(const NodePath &root, const TransformState *transform = TransformState::make_identity());
  98. void add_instances_from(const SpeedTreeNode *other);
  99. void add_instances_from(const SpeedTreeNode *other, const TransformState *transform);
  100. void add_random_instances(const STTree *tree, int quantity,
  101. PN_stdfloat x_min, PN_stdfloat x_max,
  102. PN_stdfloat y_min, PN_stdfloat y_max,
  103. PN_stdfloat scale_min, PN_stdfloat scale_max,
  104. PN_stdfloat height_min, PN_stdfloat height_max,
  105. PN_stdfloat slope_min, PN_stdfloat slope_max,
  106. Randomizer &randomizer = Randomizer());
  107. bool add_from_stf(const Filename &stf_filename,
  108. const LoaderOptions &options = LoaderOptions());
  109. bool add_from_stf(istream &in, const Filename &pathname,
  110. const LoaderOptions &options = LoaderOptions(),
  111. Loader *loader = NULL);
  112. bool setup_terrain(const Filename &terrain_file);
  113. void set_terrain(STTerrain *terrain);
  114. INLINE void clear_terrain();
  115. INLINE bool has_terrain() const;
  116. INLINE STTerrain *get_terrain() const;
  117. void snap_to_terrain();
  118. void reload_config();
  119. void set_wind(double strength, const LVector3 &direction);
  120. INLINE void set_time_delta(double delta);
  121. INLINE double get_time_delta() const;
  122. INLINE static void set_global_time_delta(double delta);
  123. INLINE static double get_global_time_delta();
  124. static bool authorize(const string &license = "");
  125. public:
  126. SpeedTreeNode(const SpeedTreeNode &copy);
  127. virtual PandaNode *make_copy() const;
  128. virtual PandaNode *combine_with(PandaNode *other);
  129. virtual void apply_attribs_to_vertices(const AccumulatedAttribs &attribs,
  130. int attrib_types,
  131. GeomTransformer &transformer);
  132. virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
  133. virtual bool is_renderable() const;
  134. virtual void add_for_draw(CullTraverser *trav, CullTraverserData &data);
  135. void prepare_scene(GraphicsStateGuardianBase *gsgbase, const RenderState *net_state);
  136. virtual void compute_internal_bounds(CPT(BoundingVolume) &internal_bounds,
  137. int &internal_vertices,
  138. int pipeline_stage,
  139. Thread *current_thread) const;
  140. virtual void output(ostream &out) const;
  141. virtual void write(ostream &out, int indent_level) const;
  142. static void write_error(ostream &out);
  143. protected:
  144. void set_transparent_texture_mode(SpeedTree::ETextureAlphaRenderMode eMode) const;
  145. private:
  146. void init_node();
  147. void r_add_instances(PandaNode *node, const TransformState *transform,
  148. Thread *current_thread);
  149. void repopulate();
  150. void update_terrain_cells();
  151. bool validate_api(GraphicsStateGuardian *gsg);
  152. void draw_callback(CallbackData *cbdata);
  153. void render_forest_into_shadow_maps();
  154. void setup_for_render(GraphicsStateGuardian *gsg);
  155. void cull_forest();
  156. void print_forest_stats(const SpeedTree::CForest::SPopulationStats &forest_stats) const;
  157. private:
  158. class DrawCallback : public CallbackObject {
  159. public:
  160. ALLOC_DELETED_CHAIN(DrawCallback);
  161. INLINE DrawCallback(SpeedTreeNode *node);
  162. virtual void do_callback(CallbackData *cbdata);
  163. private:
  164. PT(SpeedTreeNode) _node;
  165. public:
  166. static TypeHandle get_class_type() {
  167. return _type_handle;
  168. }
  169. static void init_type() {
  170. CallbackObject::init_type();
  171. register_type(_type_handle, "SpeedTreeNode::DrawCallback",
  172. CallbackObject::get_class_type());
  173. }
  174. virtual TypeHandle get_type() const {
  175. return get_class_type();
  176. }
  177. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  178. private:
  179. static TypeHandle _type_handle;
  180. };
  181. private:
  182. string _os_shaders_dir;
  183. // A list of instances per each unique tree.
  184. typedef ov_set<InstanceList *, IndirectLess<InstanceList> > Trees;
  185. Trees _trees;
  186. #ifdef ST_DELETE_FOREST_HACK
  187. SpeedTree::CForestRender &_forest_render;
  188. #else
  189. SpeedTree::CForestRender _forest_render;
  190. #endif // ST_DELETE_FOREST_HACK
  191. SpeedTree::CView _view;
  192. SpeedTree::SForestCullResultsRender _visible_trees;
  193. SpeedTree::CForest::SPopulationStats _population_stats;
  194. bool _needs_repopulate;
  195. bool _is_valid;
  196. PT(STTerrain) _terrain;
  197. SpeedTree::CTerrainRender _terrain_render;
  198. SpeedTree::STerrainCullResults _visible_terrain;
  199. SpeedTree::Vec3 _light_dir;
  200. class ShadowInfo {
  201. public:
  202. ShadowInfo() {};
  203. SpeedTree::CView _light_view;
  204. SpeedTree::SForestCullResultsRender _light_cull;
  205. PN_stdfloat _shadow_split;
  206. };
  207. typedef pvector<ShadowInfo> ShadowInfos;
  208. ShadowInfos _shadow_infos;
  209. double _time_delta;
  210. static double _global_time_delta;
  211. static bool _authorized;
  212. static bool _done_first_init;
  213. static PStatCollector _cull_speedtree_pcollector;
  214. static PStatCollector _cull_speedtree_shadows_pcollector;
  215. static PStatCollector _cull_speedtree_trees_pcollector;
  216. static PStatCollector _cull_speedtree_terrain_pcollector;
  217. static PStatCollector _draw_speedtree_pcollector;
  218. static PStatCollector _draw_speedtree_shadows_pcollector;
  219. static PStatCollector _draw_speedtree_trees_pcollector;
  220. static PStatCollector _draw_speedtree_terrain_pcollector;
  221. static PStatCollector _draw_speedtree_terrain_update_pcollector;
  222. public:
  223. static void register_with_read_factory();
  224. virtual void write_datagram(BamWriter *manager, Datagram &dg);
  225. protected:
  226. static TypedWritable *make_from_bam(const FactoryParams &params);
  227. void fillin(DatagramIterator &scan, BamReader *manager);
  228. public:
  229. static TypeHandle get_class_type() {
  230. return _type_handle;
  231. }
  232. static void init_type() {
  233. PandaNode::init_type();
  234. register_type(_type_handle, "SpeedTreeNode",
  235. PandaNode::get_class_type());
  236. DrawCallback::init_type();
  237. }
  238. virtual TypeHandle get_type() const {
  239. return get_class_type();
  240. }
  241. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  242. private:
  243. static TypeHandle _type_handle;
  244. friend class SpeedTreeNode::DrawCallback;
  245. };
  246. INLINE ostream &operator << (ostream &out, const SpeedTreeNode::InstanceList &instances) {
  247. instances.output(out);
  248. return out;
  249. }
  250. #include "speedTreeNode.I"
  251. #endif