Panda3D
lodNode.h
1 // Filename: lodNode.h
2 // Created by: drose (06Mar02)
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 
15 #ifndef LODNODE_H
16 #define LODNODE_H
17 
18 #include "pandabase.h"
19 #include "config_pgraphnodes.h"
20 #include "pandaNode.h"
21 #include "luse.h"
22 #include "pvector.h"
23 
24 ////////////////////////////////////////////////////////////////////
25 // Class : LODNode
26 // Description : A Level-of-Detail node. This selects only one of its
27 // children for rendering, according to the distance
28 // from the camera and the table indicated in the
29 // associated LOD object.
30 ////////////////////////////////////////////////////////////////////
31 class EXPCL_PANDA_PGRAPHNODES LODNode : public PandaNode {
32 PUBLISHED:
33  INLINE LODNode(const string &name);
34 
35  static PT(LODNode) make_default_lod(const string &name);
36 
37 protected:
38  INLINE LODNode(const LODNode &copy);
39 public:
40  virtual PandaNode *make_copy() const;
41  virtual bool safe_to_combine() const;
42  virtual bool safe_to_combine_children() const;
43  virtual void xform(const LMatrix4 &mat);
44  virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
45 
46  virtual void output(ostream &out) const;
47 
48  virtual bool is_lod_node() const;
49 
50 PUBLISHED:
51  // The sense of in vs. out distances is as if the object were coming
52  // towards you from far away: it switches "in" at the far distance,
53  // and switches "out" at the close distance. Thus, "in" should be
54  // larger than "out".
55 
56  INLINE void add_switch(PN_stdfloat in, PN_stdfloat out);
57  INLINE bool set_switch(int index, PN_stdfloat in, PN_stdfloat out);
58  INLINE void clear_switches();
59 
60  INLINE int get_num_switches() const;
61  INLINE PN_stdfloat get_in(int index) const;
62  MAKE_SEQ(get_ins, get_num_switches, get_in);
63  INLINE PN_stdfloat get_out(int index) const;
64  MAKE_SEQ(get_outs, get_num_switches, get_out);
65 
66  INLINE int get_lowest_switch() const;
67  INLINE int get_highest_switch() const;
68 
69  INLINE void force_switch(int index);
70  INLINE void clear_force_switch();
71 
72  //for performance tuning, increasing this value should improve performance
73  //at the cost of model quality
74  INLINE void set_lod_scale(PN_stdfloat value);
75  INLINE PN_stdfloat get_lod_scale() const;
76 
77 
78  INLINE void set_center(const LPoint3 &center);
79  INLINE const LPoint3 &get_center() const;
80 
81  void show_switch(int index);
82  void show_switch(int index, const LColor &color);
83  void hide_switch(int index);
84  void show_all_switches();
85  void hide_all_switches();
86  INLINE bool is_any_shown() const;
87 
88  bool verify_child_bounds() const;
89 
90 protected:
91  int compute_child(CullTraverser *trav, CullTraverserData &data);
92 
93  bool show_switches_cull_callback(CullTraverser *trav, CullTraverserData &data);
94  virtual void compute_internal_bounds(CPT(BoundingVolume) &internal_bounds,
95  int &internal_vertices,
96  int pipeline_stage,
97  Thread *current_thread) const;
98 
99  INLINE void consider_verify_lods(CullTraverser *trav, CullTraverserData &data);
100 
101  CPT(TransformState) get_rel_transform(CullTraverser *trav, CullTraverserData &data);
102 
103 private:
104  class CData;
105  void do_show_switch(CData *cdata, int index, const LColor &color);
106  void do_hide_switch(CData *cdata, int index);
107  bool do_verify_child_bounds(const CData *cdata, int index,
108  PN_stdfloat &suggested_radius) const;
109  void do_auto_verify_lods(CullTraverser *trav, CullTraverserData &data);
110 
111  static const LColor &get_default_show_color(int index);
112 
113 protected:
114  class Switch {
115  public:
116  INLINE Switch(PN_stdfloat in, PN_stdfloat out);
117  INLINE PN_stdfloat get_in() const;
118  INLINE PN_stdfloat get_out() const;
119 
120  INLINE void set_range(PN_stdfloat in, PN_stdfloat out);
121  INLINE bool in_range(PN_stdfloat dist) const;
122  INLINE bool in_range_2(PN_stdfloat dist2) const;
123 
124  INLINE void rescale(PN_stdfloat factor);
125 
126  INLINE bool is_shown() const;
127  INLINE void show(const LColor &color);
128  INLINE void hide();
129 
130  INLINE PandaNode *get_ring_viz() const;
131  INLINE PandaNode *get_spindle_viz() const;
132  INLINE const RenderState *get_viz_model_state() const;
133 
134  INLINE void write_datagram(Datagram &destination) const;
135  INLINE void read_datagram(DatagramIterator &source);
136 
137  private:
138  INLINE void clear_ring_viz();
139 
140  void compute_ring_viz();
141  void compute_spindle_viz();
142  void compute_viz_model_state();
143 
144  private:
145  PN_stdfloat _in;
146  PN_stdfloat _out;
147  bool _shown;
148  UnalignedLVecBase4 _show_color;
149  PT(PandaNode) _ring_viz;
150  PT(PandaNode) _spindle_viz;
151  CPT(RenderState) _viz_model_state;
152 
153  public:
154  UpdateSeq _bounds_seq;
155  bool _verify_ok;
156  };
158 
159 private:
160  class EXPCL_PANDA_PGRAPH CData : public CycleData {
161  public:
162  INLINE CData();
163  INLINE CData(const CData &copy);
164  virtual CycleData *make_copy() const;
165 
166  void check_limits();
167 
168  virtual void write_datagram(BamWriter *manager, Datagram &dg) const;
169  virtual void fillin(DatagramIterator &scan, BamReader *manager);
170  virtual TypeHandle get_parent_type() const {
171  return LODNode::get_class_type();
172  }
173 
174  LPoint3 _center;
175  SwitchVector _switch_vector;
176  size_t _lowest, _highest;
177  UpdateSeq _bounds_seq;
178 
179  bool _got_force_switch;
180  int _force_switch;
181  int _num_shown;
182  PN_stdfloat _lod_scale;
183  };
184 
185  PipelineCycler<CData> _cycler;
191 
192 public:
193  static void register_with_read_factory();
194  virtual void write_datagram(BamWriter *manager, Datagram &dg);
195 
196 protected:
197  static TypedWritable *make_from_bam(const FactoryParams &params);
198  void fillin(DatagramIterator &scan, BamReader *manager);
199 
200 public:
201  static TypeHandle get_class_type() {
202  return _type_handle;
203  }
204  static void init_type() {
205  PandaNode::init_type();
206  register_type(_type_handle, "LODNode",
207  PandaNode::get_class_type());
208  }
209  virtual TypeHandle get_type() const {
210  return get_class_type();
211  }
212  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
213 
214 private:
215  static TypeHandle _type_handle;
216 };
217 
218 #include "lodNode.I"
219 
220 #endif
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
This class is similar to CycleDataWriter, except it allows writing to a particular stage of the pipel...
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:50
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is an "unaligned" LVecBase4.
Definition: lvecBase4.h:300
virtual bool safe_to_combine_children() const
Returns true if it is generally safe to combine the children of this PandaNode with each other...
Definition: pandaNode.cxx:295
This collects together the pieces of data that are accumulated for each node while walking the scene ...
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
virtual bool safe_to_combine() const
Returns true if it is generally safe to combine this particular kind of PandaNode with other kinds of...
Definition: pandaNode.cxx:281
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
virtual void xform(const LMatrix4 &mat)
Transforms the contents of this PandaNode by the indicated matrix, if it means anything to do so...
Definition: pandaNode.cxx:377
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
A Level-of-Detail node.
Definition: lodNode.h:31
This template class calls PipelineCycler::read() in the constructor and PipelineCycler::release_read(...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
virtual bool is_lod_node() const
A simple downcast check.
Definition: pandaNode.cxx:2501
virtual PandaNode * make_copy() const
Returns a newly-allocated PandaNode that is a shallow copy of this one.
Definition: pandaNode.cxx:604
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:53
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
This class is similar to CycleDataReader, except it allows reading from a particular stage of the pip...
A thread; that is, a lightweight process.
Definition: thread.h:51
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This is a sequence number that increments monotonically.
Definition: updateSeq.h:43
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling...
Definition: cullTraverser.h:48