Panda3D
ropeNode.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 ropeNode.h
10  * @author drose
11  * @date 2002-12-04
12  */
13 
14 #ifndef ROPENODE_H
15 #define ROPENODE_H
16 
17 #include "pandabase.h"
18 #include "nurbsCurveEvaluator.h"
19 #include "pandaNode.h"
20 #include "pStatCollector.h"
21 #include "geomVertexFormat.h"
22 
23 class GeomVertexData;
24 
25 /**
26  * This class draws a visible representation of the NURBS curve stored in its
27  * NurbsCurveEvaluator. It automatically recomputes the curve every frame.
28  *
29  * This is not related to NurbsCurve, CubicCurveseg or any of the
30  * ParametricCurve-derived objects in this module. It is a completely
31  * parallel implementation of NURBS curves, and will probably eventually
32  * replace the whole ParametricCurve class hierarchy.
33  */
34 class EXPCL_PANDA_PARAMETRICS RopeNode : public PandaNode {
35 PUBLISHED:
36  explicit RopeNode(const std::string &name);
37 
38 protected:
39  RopeNode(const RopeNode &copy);
40 public:
41  virtual void output(std::ostream &out) const;
42  virtual void write(std::ostream &out, int indent_level = 0) const;
43 
44  virtual PandaNode *make_copy() const;
45 
46  virtual bool safe_to_transform() const;
47  virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
48  virtual bool is_renderable() const;
49 
50 PUBLISHED:
51  enum RenderMode {
52  // Render the rope as a one-pixel thread using a linestrip.
53  RM_thread,
54 
55  // Render the rope as a triangle strip oriented to be perpendicular to the
56  // tube_up vector.
57  RM_tape,
58 
59  // Render the rope as a triangle strip oriented to be perpendicular to the
60  // view vector.
61  RM_billboard,
62 
63  // Render the rope as a hollow tube extruded along its length.
64  RM_tube
65  };
66 
67  enum UVMode {
68  // Don't generate UV's along the curve.
69  UV_none,
70 
71  // Generate UV's based on the parametric coordinates along the curve.
72  UV_parametric,
73 
74  // Generate UV's in proportion to spatial distance along the curve, by
75  // using the distance function to compute the length of each segment.
76  UV_distance,
77 
78  // As above, but don't bother to take the square root of each segment.
79  // The distance is then in proportion to the sum-of-squares of the
80  // segments along the rope. If the segments are similar in length, this
81  // approximates the proportion of UV_distance while avoiding hundreds of
82  // square root operations.
83  UV_distance2,
84  };
85 
86  enum NormalMode {
87  // Don't generate normals.
88  NM_none,
89 
90  // Generate vertex (smooth-shaded) normals.
91  NM_vertex
92  };
93 
94  INLINE void set_curve(NurbsCurveEvaluator *curve);
95  INLINE NurbsCurveEvaluator *get_curve() const;
96 
97  INLINE void set_render_mode(RenderMode render_mode);
98  INLINE RenderMode get_render_mode() const;
99 
100  INLINE void set_uv_mode(UVMode uv_mode);
101  INLINE UVMode get_uv_mode() const;
102 
103  INLINE void set_uv_direction(bool u_dominant);
104  INLINE bool get_uv_direction() const;
105 
106  INLINE void set_uv_scale(PN_stdfloat scale);
107  INLINE PN_stdfloat get_uv_scale() const;
108 
109  INLINE void set_normal_mode(NormalMode normal_mode);
110  INLINE NormalMode get_normal_mode() const;
111 
112  INLINE void set_tube_up(const LVector3 &tube_up);
113  INLINE const LVector3 &get_tube_up() const;
114 
115  INLINE void set_use_vertex_color(bool flag);
116  INLINE bool get_use_vertex_color() const;
117  INLINE static int get_vertex_color_dimension();
118 
119  INLINE void set_num_subdiv(int num_subdiv);
120  INLINE int get_num_subdiv() const;
121 
122  INLINE void set_num_slices(int num_slices);
123  INLINE int get_num_slices() const;
124 
125  INLINE void set_use_vertex_thickness(bool flag);
126  INLINE bool get_use_vertex_thickness() const;
127  INLINE static int get_vertex_thickness_dimension();
128 
129  INLINE void set_thickness(PN_stdfloat thickness);
130  INLINE PN_stdfloat get_thickness() const;
131 
132  INLINE void set_matrix(const LMatrix4 &matrix);
133  INLINE void clear_matrix();
134  INLINE bool has_matrix() const;
135  INLINE const LMatrix4 &get_matrix() const;
136 
137  void reset_bound(const NodePath &rel_to);
138 
139 PUBLISHED:
140  MAKE_PROPERTY(curve, get_curve, set_curve);
141  MAKE_PROPERTY(render_mode, get_render_mode, set_render_mode);
142  MAKE_PROPERTY(uv_mode, get_uv_mode, set_uv_mode);
143  MAKE_PROPERTY(uv_direction, get_uv_direction, set_uv_direction);
144  MAKE_PROPERTY(uv_scale, get_uv_scale, set_uv_scale);
145  MAKE_PROPERTY(normal_mode, get_normal_mode, set_normal_mode);
146  MAKE_PROPERTY(tube_up, get_tube_up, set_tube_up);
147  MAKE_PROPERTY(use_vertex_color, get_use_vertex_color, set_use_vertex_color);
148  MAKE_PROPERTY(vertex_color_dimension, get_vertex_color_dimension);
149  MAKE_PROPERTY(num_subdiv, get_num_subdiv, set_num_subdiv);
150  MAKE_PROPERTY(num_slices, get_num_slices, set_num_slices);
151  MAKE_PROPERTY(use_vertex_thickness, get_use_vertex_thickness, set_use_vertex_thickness);
152  MAKE_PROPERTY(vertex_thickness_dimension, get_vertex_thickness_dimension);
153  MAKE_PROPERTY(thickness, get_thickness, set_thickness);
154  MAKE_PROPERTY2(matrix, has_matrix, get_matrix, set_matrix, clear_matrix);
155 
156 protected:
157  virtual void compute_internal_bounds(CPT(BoundingVolume) &internal_bounds,
158  int &internal_vertices,
159  int pipeline_stage,
160  Thread *current_thread) const;
161 
162 private:
163  CPT(GeomVertexFormat) get_format(bool support_normals) const;
164 
165  PT(BoundingVolume) do_recompute_bounds(const NodePath &rel_to,
166  int pipeline_stage,
167  Thread *current_thread) const;
168  void render_thread(CullTraverser *trav, CullTraverserData &data,
169  NurbsCurveResult *result) const;
170  void render_tape(CullTraverser *trav, CullTraverserData &data,
171  NurbsCurveResult *result) const;
172  void render_billboard(CullTraverser *trav, CullTraverserData &data,
173  NurbsCurveResult *result) const;
174  void render_tube(CullTraverser *trav, CullTraverserData &data,
175  NurbsCurveResult *result) const;
176 
177  class CurveVertex {
178  public:
179  LPoint3 _p;
180  UnalignedLVecBase4 _c;
181  PN_stdfloat _thickness;
182  PN_stdfloat _t;
183  };
184  typedef pvector<CurveVertex> CurveSegment;
185  typedef pvector<CurveSegment> CurveSegments;
186 
187  int get_connected_segments(CurveSegments &curve_segments,
188  const NurbsCurveResult *result) const;
189 
190  void compute_thread_vertices(GeomVertexData *vdata,
191  const CurveSegments &curve_segments,
192  int num_curve_verts) const;
193  void compute_billboard_vertices(GeomVertexData *vdata,
194  const LVector3 &camera_vec,
195  const CurveSegments &curve_segments,
196  int num_curve_verts,
197  NurbsCurveResult *result) const;
198  void compute_tube_vertices(GeomVertexData *vdata,
199  int &num_verts_per_slice,
200  const CurveSegments &curve_segments,
201  int num_curve_verts,
202  NurbsCurveResult *result) const;
203 
204  static void compute_tangent(LVector3 &tangent, const CurveSegment &segment,
205  size_t j, NurbsCurveResult *result);
206  static PN_stdfloat compute_uv_t(PN_stdfloat &dist, const UVMode &uv_mode,
207  PN_stdfloat uv_scale, const CurveSegment &segment,
208  size_t j);
209 
210 
211 private:
212  // This is the data that must be cycled between pipeline stages.
213  class EXPCL_PANDA_PARAMETRICS CData : public CycleData {
214  public:
215  INLINE CData();
216  INLINE CData(const CData &copy);
217  virtual CycleData *make_copy() const;
218  virtual void write_datagram(BamWriter *manager, Datagram &dg) const;
219  virtual void fillin(DatagramIterator &scan, BamReader *manager);
220  virtual TypeHandle get_parent_type() const {
221  return RopeNode::get_class_type();
222  }
223 
224  PT(NurbsCurveEvaluator) _curve;
225  RenderMode _render_mode;
226  UVMode _uv_mode;
227  bool _u_dominant;
228  PN_stdfloat _uv_scale;
229  NormalMode _normal_mode;
230  LVector3 _tube_up;
231  LMatrix4 _matrix;
232  bool _has_matrix;
233  bool _use_vertex_color;
234  int _num_subdiv;
235  int _num_slices;
236  bool _use_vertex_thickness;
237  PN_stdfloat _thickness;
238  };
239 
240  PipelineCycler<CData> _cycler;
241  typedef CycleDataReader<CData> CDReader;
242  typedef CycleDataWriter<CData> CDWriter;
243 
244  static PStatCollector _rope_node_pcollector;
245 
246 public:
247  static void register_with_read_factory();
248  virtual void write_datagram(BamWriter *manager, Datagram &dg);
249 
250 protected:
251  static TypedWritable *make_from_bam(const FactoryParams &params);
252  void fillin(DatagramIterator &scan, BamReader *manager);
253 
254 public:
255  static TypeHandle get_class_type() {
256  return _type_handle;
257  }
258  static void init_type() {
259  PandaNode::init_type();
260  register_type(_type_handle, "RopeNode",
261  PandaNode::get_class_type());
262  }
263  virtual TypeHandle get_type() const {
264  return get_class_type();
265  }
266  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
267 
268 private:
269  static TypeHandle _type_handle;
270 };
271 
272 #include "ropeNode.I"
273 
274 #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_unlocked(), and then provides a transparent read-only ...
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
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
This class defines the physical layout of the vertex data stored within a Geom.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:159
This class is an abstraction for evaluating NURBS curves.
The result of a NurbsCurveEvaluator.
A lightweight class that represents a single element that may be timed and/or counted via stats.
A basic node of the scene graph or data graph.
Definition: pandaNode.h:65
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:204
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:462
static void register_with_read_factory()
Tells the BamReader how to create objects of type PandaNode.
Definition: pandaNode.cxx:3574
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 class draws a visible representation of the NURBS curve stored in its NurbsCurveEvaluator.
Definition: ropeNode.h:34
A thread; that is, a lightweight process.
Definition: thread.h:46
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 our own Panda specialization on the default STL vector.
Definition: pvector.h:42
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.