Panda3D
 All Classes Functions Variables Enumerations
ropeNode.h
1 // Filename: ropeNode.h
2 // Created by: drose (04Dec02)
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 ROPENODE_H
16 #define ROPENODE_H
17 
18 #include "pandabase.h"
19 #include "nurbsCurveEvaluator.h"
20 #include "pandaNode.h"
21 #include "pStatCollector.h"
22 #include "geomVertexFormat.h"
23 
24 class GeomVertexData;
25 
26 ////////////////////////////////////////////////////////////////////
27 // Class : RopeNode
28 // Description : This class draws a visible representation of the
29 // NURBS curve stored in its NurbsCurveEvaluator. It
30 // automatically recomputes the curve every frame.
31 //
32 // This is not related to NurbsCurve, CubicCurveseg or
33 // any of the ParametricCurve-derived objects in this
34 // module. It is a completely parallel implementation
35 // of NURBS curves, and will probably eventually replace
36 // the whole ParametricCurve class hierarchy.
37 ////////////////////////////////////////////////////////////////////
38 class EXPCL_PANDA_PARAMETRICS RopeNode : public PandaNode {
39 PUBLISHED:
40  RopeNode(const string &name);
41 
42 protected:
43  RopeNode(const RopeNode &copy);
44 public:
45  virtual void output(ostream &out) const;
46  virtual void write(ostream &out, int indent_level = 0) const;
47 
48  virtual PandaNode *make_copy() const;
49 
50  virtual bool safe_to_transform() const;
51  virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
52  virtual bool is_renderable() const;
53 
54 PUBLISHED:
55  enum RenderMode {
56  // Render the rope as a one-pixel thread using a linestrip.
57  RM_thread,
58 
59  // Render the rope as a triangle strip oriented to be
60  // perpendicular to the tube_up vector.
61  RM_tape,
62 
63  // Render the rope as a triangle strip oriented to be
64  // perpendicular to the view vector.
65  RM_billboard,
66 
67  // Render the rope as a hollow tube extruded along its length.
68  RM_tube
69  };
70 
71  enum UVMode {
72  // Don't generate UV's along the curve.
73  UV_none,
74 
75  // Generate UV's based on the parametric coordinates along the
76  // curve.
77  UV_parametric,
78 
79  // Generate UV's in proportion to spatial distance along the
80  // curve, by using the distance function to compute the length of
81  // each segment.
82  UV_distance,
83 
84  // As above, but don't bother to take the square root of each
85  // segment. The distance is then in proportion to the
86  // sum-of-squares of the segments along the rope. If the segments
87  // are similar in length, this approximates the proportion of
88  // UV_distance while avoiding hundreds of square root operations.
89  UV_distance2,
90  };
91 
92  enum NormalMode {
93  // Don't generate normals.
94  NM_none,
95 
96  // Generate vertex (smooth-shaded) normals.
97  NM_vertex
98  };
99 
100  INLINE void set_curve(NurbsCurveEvaluator *curve);
101  INLINE NurbsCurveEvaluator *get_curve() const;
102 
103  INLINE void set_render_mode(RenderMode render_mode);
104  INLINE RenderMode get_render_mode() const;
105 
106  INLINE void set_uv_mode(UVMode uv_mode);
107  INLINE UVMode get_uv_mode() const;
108 
109  INLINE void set_uv_direction(bool u_dominant);
110  INLINE bool get_uv_direction() const;
111 
112  INLINE void set_uv_scale(PN_stdfloat scale);
113  INLINE PN_stdfloat get_uv_scale() const;
114 
115  INLINE void set_normal_mode(NormalMode normal_mode);
116  INLINE NormalMode get_normal_mode() const;
117 
118  INLINE void set_tube_up(const LVector3 &tube_up);
119  INLINE const LVector3 &get_tube_up() const;
120 
121  INLINE void set_use_vertex_color(bool flag);
122  INLINE bool get_use_vertex_color() const;
123  INLINE static int get_vertex_color_dimension();
124 
125  INLINE void set_num_subdiv(int num_subdiv);
126  INLINE int get_num_subdiv() const;
127 
128  INLINE void set_num_slices(int num_slices);
129  INLINE int get_num_slices() const;
130 
131  INLINE void set_use_vertex_thickness(bool flag);
132  INLINE bool get_use_vertex_thickness() const;
133  INLINE static int get_vertex_thickness_dimension();
134 
135  INLINE void set_thickness(PN_stdfloat thickness);
136  INLINE PN_stdfloat get_thickness() const;
137 
138  INLINE void set_matrix(const LMatrix4 &matrix);
139  INLINE void clear_matrix();
140  INLINE bool has_matrix() const;
141  INLINE const LMatrix4 &get_matrix() const;
142 
143  void reset_bound(const NodePath &rel_to);
144 
145 protected:
146  virtual void compute_internal_bounds(CPT(BoundingVolume) &internal_bounds,
147  int &internal_vertices,
148  int pipeline_stage,
149  Thread *current_thread) const;
150 
151 private:
152  CPT(GeomVertexFormat) get_format(bool support_normals) const;
153 
154  PT(BoundingVolume) do_recompute_bounds(const NodePath &rel_to,
155  int pipeline_stage,
156  Thread *current_thread) const;
157  void render_thread(CullTraverser *trav, CullTraverserData &data,
158  NurbsCurveResult *result) const;
159  void render_tape(CullTraverser *trav, CullTraverserData &data,
160  NurbsCurveResult *result) const;
161  void render_billboard(CullTraverser *trav, CullTraverserData &data,
162  NurbsCurveResult *result) const;
163  void render_tube(CullTraverser *trav, CullTraverserData &data,
164  NurbsCurveResult *result) const;
165 
166  class CurveVertex {
167  public:
168  LPoint3 _p;
170  PN_stdfloat _thickness;
171  PN_stdfloat _t;
172  };
175 
176  int get_connected_segments(CurveSegments &curve_segments,
177  const NurbsCurveResult *result) const;
178 
179  void compute_thread_vertices(GeomVertexData *vdata,
180  const CurveSegments &curve_segments,
181  int num_curve_verts) const;
182  void compute_billboard_vertices(GeomVertexData *vdata,
183  const LVector3 &camera_vec,
184  const CurveSegments &curve_segments,
185  int num_curve_verts,
186  NurbsCurveResult *result) const;
187  void compute_tube_vertices(GeomVertexData *vdata,
188  int &num_verts_per_slice,
189  const CurveSegments &curve_segments,
190  int num_curve_verts,
191  NurbsCurveResult *result) const;
192 
193  static void compute_tangent(LVector3 &tangent, const CurveSegment &segment,
194  size_t j, NurbsCurveResult *result);
195  static PN_stdfloat compute_uv_t(PN_stdfloat &dist, const UVMode &uv_mode,
196  PN_stdfloat uv_scale, const CurveSegment &segment,
197  size_t j);
198 
199 
200 private:
201  // This is the data that must be cycled between pipeline stages.
202  class EXPCL_PANDA_PARAMETRICS CData : public CycleData {
203  public:
204  INLINE CData();
205  INLINE CData(const CData &copy);
206  virtual CycleData *make_copy() const;
207  virtual void write_datagram(BamWriter *manager, Datagram &dg) const;
208  virtual void fillin(DatagramIterator &scan, BamReader *manager);
209  virtual TypeHandle get_parent_type() const {
210  return RopeNode::get_class_type();
211  }
212 
213  PT(NurbsCurveEvaluator) _curve;
214  RenderMode _render_mode;
215  UVMode _uv_mode;
216  bool _u_dominant;
217  PN_stdfloat _uv_scale;
218  NormalMode _normal_mode;
219  LVector3 _tube_up;
220  LMatrix4 _matrix;
221  bool _has_matrix;
222  bool _use_vertex_color;
223  int _num_subdiv;
224  int _num_slices;
225  bool _use_vertex_thickness;
226  PN_stdfloat _thickness;
227  };
228 
229  PipelineCycler<CData> _cycler;
232 
233  static PStatCollector _rope_node_pcollector;
234 
235 public:
236  static void register_with_read_factory();
237  virtual void write_datagram(BamWriter *manager, Datagram &dg);
238 
239 protected:
240  static TypedWritable *make_from_bam(const FactoryParams &params);
241  void fillin(DatagramIterator &scan, BamReader *manager);
242 
243 public:
244  static TypeHandle get_class_type() {
245  return _type_handle;
246  }
247  static void init_type() {
248  PandaNode::init_type();
249  register_type(_type_handle, "RopeNode",
250  PandaNode::get_class_type());
251  }
252  virtual TypeHandle get_type() const {
253  return get_class_type();
254  }
255  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
256 
257 private:
258  static TypeHandle _type_handle;
259 };
260 
261 #include "ropeNode.I"
262 
263 #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
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:50
This class is an abstraction for evaluating NURBS curves.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is an &quot;unaligned&quot; LVecBase4.
Definition: lvecBase4.h:299
This collects together the pieces of data that are accumulated for each node while walking the scene ...
virtual PandaNode * make_copy() const
Returns a newly-allocated PandaNode that is a shallow copy of this one.
Definition: pandaNode.cxx:604
This class draws a visible representation of the NURBS curve stored in its NurbsCurveEvaluator.
Definition: ropeNode.h:38
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
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
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
A lightweight class that represents a single element that may be timed and/or counted via stats...
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
virtual bool safe_to_transform() const
Returns true if it is generally safe to transform this particular kind of PandaNode by calling the xf...
Definition: pandaNode.cxx:249
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
virtual bool is_renderable() const
Returns true if there is some value to visiting this particular node during the cull traversal for an...
Definition: pandaNode.cxx:578
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class&#39;s make_from_bam() method to read in all...
Definition: cycleData.cxx:68
A thread; that is, a lightweight process.
Definition: thread.h:51
A class to retrieve the individual data elements previously stored in a Datagram. ...
virtual void write_datagram(BamWriter *, Datagram &) const
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: cycleData.cxx:34
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
The result of a NurbsCurveEvaluator.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling...
Definition: cullTraverser.h:48
virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data)
This function will be called during the cull traversal to perform any additional operations that shou...
Definition: pandaNode.cxx:477