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