Panda3D
lineSegs.cxx
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 lineSegs.cxx
10  * @author drose
11  * @date 2002-03-16
12  */
13 
14 #include "lineSegs.h"
15 #include "renderState.h"
16 #include "renderModeAttrib.h"
17 #include "geom.h"
18 #include "geomLinestrips.h"
19 #include "geomPoints.h"
20 #include "geomVertexReader.h"
21 #include "geomVertexWriter.h"
22 #include "colorAttrib.h"
23 
24 /**
25  * Constructs a LineSegs object, which can be used to create any number of
26  * disconnected lines or points of various thicknesses and colors through the
27  * visible scene. After creating the object, call move_to() and draw_to()
28  * repeatedly to describe the path, then call create() to create a GeomNode
29  * which will render the described path.
30  */
32 LineSegs(const std::string &name) : Namable(name) {
33  _color.set(1.0f, 1.0f, 1.0f, 1.0f);
34  _thick = 1.0f;
35 }
36 
37 
38 /**
39 
40  */
41 LineSegs::
42 ~LineSegs() {
43 }
44 
45 
46 /**
47  * Removes any lines in progress and resets to the initial empty state.
48  */
49 void LineSegs::
50 reset() {
51  _list.clear();
52 }
53 
54 
55 /**
56  * Moves the pen to the given point without drawing a line. When followed by
57  * draw_to(), this marks the first point of a line segment; when followed by
58  * move_to() or create(), this creates a single point.
59  */
60 void LineSegs::
61 move_to(const LVecBase3 &v) {
62  // We create a new SegmentList with the initial point in it.
63  SegmentList segs;
64  segs.push_back(Point(v, _color));
65 
66  // And add this list to the list of segments.
67  _list.push_back(segs);
68 }
69 
70 /**
71  * Draws a line segment from the pen's last position (the last call to move_to
72  * or draw_to) to the indicated point. move_to() and draw_to() only update
73  * tables; the actual drawing is performed when create() is called.
74  */
75 void LineSegs::
76 draw_to(const LVecBase3 &v) {
77  if (_list.empty()) {
78  // Let our first call to draw_to() be an implicit move_to().
79  move_to(v);
80 
81  } else {
82  // Get the current SegmentList, which was the last one we added to the
83  // LineList.
84  SegmentList &segs = _list.back();
85 
86  // Add the new point.
87  segs.push_back(Point(v, _color));
88  }
89 }
90 
91 /**
92  * Returns true if move_to() or draw_to() have not been called since the last
93  * reset() or create(), false otherwise.
94  */
95 bool LineSegs::
97  return _list.empty();
98 }
99 
100 /**
101  * Returns the nth point or vertex of the line segment sequence generated by
102  * the last call to create(). The first move_to() generates vertex 0;
103  * subsequent move_to() and draw_to() calls generate consecutively higher
104  * vertex numbers.
105  */
106 LVertex LineSegs::
107 get_vertex(int n) const {
108  nassertr(_created_data != nullptr, LVertex::zero());
109  GeomVertexReader vertex(_created_data, InternalName::get_vertex());
110  vertex.set_row_unsafe(n);
111  return vertex.get_data3();
112 }
113 
114 /**
115  * Moves the nth point or vertex of the line segment sequence generated by the
116  * last call to create(). The first move_to() generates vertex 0; subsequent
117  * move_to() and draw_to() calls generate consecutively higher vertex numbers.
118  */
119 void LineSegs::
120 set_vertex(int n, const LVertex &vert) {
121  nassertv(_created_data != nullptr);
122  GeomVertexWriter vertex(_created_data, InternalName::get_vertex());
123  vertex.set_row_unsafe(n);
124  vertex.set_data3(vert);
125 }
126 
127 /**
128  * Returns the color of the nth point or vertex.
129  */
130 LColor LineSegs::
131 get_vertex_color(int n) const {
132  nassertr(_created_data != nullptr, LColor::zero());
133  GeomVertexReader color(_created_data, InternalName::get_color());
134  color.set_row_unsafe(n);
135  return color.get_data4();
136 }
137 
138 /**
139  * Changes the vertex color of the nth point or vertex. See set_vertex().
140  */
141 void LineSegs::
142 set_vertex_color(int n, const LColor &c) {
143  nassertv(_created_data != nullptr);
144  GeomVertexWriter color(_created_data, InternalName::get_color());
145  color.set_row_unsafe(n);
146  color.set_data4(c);
147 }
148 
149 /**
150  * Returns the pen's current position. The next call to draw_to() will draw a
151  * line segment from this point.
152  */
153 const LVertex &LineSegs::
155  if (_list.empty()) {
156  // Our pen isn't anywhere. We'll put it somewhere.
157  move_to(LVertex(0.0f, 0.0f, 0.0f));
158  }
159 
160  return _list.back().back()._point;
161 }
162 
163 /**
164  * Appends to an existing GeomNode a new Geom that will render the series of
165  * line segments and points described via calls to move_to() and draw_to().
166  * The lines and points are created with the color and thickness established
167  * by calls to set_color() and set_thickness().
168  *
169  * If dynamic is true, the line segments will be created with the dynamic Geom
170  * setting, optimizing them for runtime vertex animation.
171  */
173 create(GeomNode *previous, bool dynamic) {
174  if (!_list.empty()) {
175  CPT(RenderAttrib) thick = RenderModeAttrib::make(RenderModeAttrib::M_unchanged, _thick);
176  CPT(RenderAttrib) vtxcolor = ColorAttrib::make_vertex();
177  CPT(RenderState) state = RenderState::make(thick, vtxcolor);
178 
179  _created_data = new GeomVertexData
180  ("lineSegs", GeomVertexFormat::get_v3cp(),
181  dynamic ? Geom::UH_dynamic : Geom::UH_static);
182  GeomVertexWriter vertex(_created_data, InternalName::get_vertex());
183  GeomVertexWriter color(_created_data, InternalName::get_color());
184 
185  PT(GeomLinestrips) lines = new GeomLinestrips(Geom::UH_static);
186  PT(GeomPoints) points = new GeomPoints(Geom::UH_static);
187 
188  int v = 0;
189  LineList::const_iterator ll;
190  SegmentList::const_iterator sl;
191 
192  for (ll = _list.begin(); ll != _list.end(); ll++) {
193  const SegmentList &segs = (*ll);
194 
195  if (segs.size() < 2) {
196  // A segment of length 1 is just a point.
197  for (sl = segs.begin(); sl != segs.end(); sl++) {
198  points->add_vertex(v);
199  vertex.add_data3((*sl)._point);
200  color.add_data4((*sl)._color);
201  v++;
202  }
203  points->close_primitive();
204 
205  } else {
206  // A segment of length 2 or more is a line segment or segments.
207  for (sl = segs.begin(); sl != segs.end(); sl++) {
208  lines->add_vertex(v);
209  vertex.add_data3((*sl)._point);
210  color.add_data4((*sl)._color);
211  v++;
212  }
213  lines->close_primitive();
214  }
215  }
216 
217  if (lines->get_num_vertices() != 0) {
218  PT(Geom) geom = new Geom(_created_data);
219  geom->add_primitive(lines);
220  previous->add_geom(geom, state);
221  }
222  if (points->get_num_vertices() != 0) {
223  PT(Geom) geom = new Geom(_created_data);
224  geom->add_primitive(points);
225  previous->add_geom(geom, state);
226  }
227 
228  // And reset for next time.
229  reset();
230  }
231 
232  return previous;
233 }
Geom
A container for geometry primitives.
Definition: geom.h:54
GeomVertexWriter::set_data4
void set_data4(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z, PN_stdfloat w)
Sets the write row to a particular 4-component value, and advances the write row.
Definition: geomVertexWriter.I:670
geomVertexWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
LineSegs::draw_to
void draw_to(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z)
Draws a line segment from the pen's last position (the last call to move_to or draw_to) to the indica...
Definition: lineSegs.I:94
pvector
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
GeomVertexData
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
Definition: geomVertexData.h:68
LineSegs::reset
void reset()
Removes any lines in progress and resets to the initial empty state.
Definition: lineSegs.cxx:50
geomVertexReader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomPoints
Defines a series of disconnected points.
Definition: geomPoints.h:23
GeomVertexWriter::add_data3
void add_data3(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z)
Sets the write row to a particular 3-component value, and advances the write row.
Definition: geomVertexWriter.I:1132
colorAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomVertexWriter
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
Definition: geomVertexWriter.h:55
RenderAttrib
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:51
GeomVertexReader
This object provides a high-level interface for quickly reading a sequence of numeric values from a v...
Definition: geomVertexReader.h:47
GeomLinestrips
Defines a series of line strips.
Definition: geomLinestrips.h:23
RenderState
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:47
renderState.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomNode
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:34
renderModeAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
LineSegs::move_to
void move_to(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z)
Moves the pen to the given point without drawing a line.
Definition: lineSegs.I:84
geom.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomVertexWriter::set_row_unsafe
void set_row_unsafe(int row)
Sets the start row to the indicated value, without internal checks.
Definition: geomVertexWriter.I:285
LineSegs::set_vertex_color
void set_vertex_color(int vertex, const LColor &c)
Changes the vertex color of the nth point or vertex.
Definition: lineSegs.cxx:142
LineSegs::get_vertex
get_vertex
Returns the nth point or vertex of the line segment sequence generated by the last call to create().
Definition: lineSegs.h:58
geomLinestrips.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Namable
A base class for all things which can have a name.
Definition: namable.h:26
lineSegs.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
LineSegs::get_vertex_color
get_vertex_color
Returns the color of the nth point or vertex.
Definition: lineSegs.h:63
GeomVertexWriter::set_data3
void set_data3(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z)
Sets the write row to a particular 3-component value, and advances the write row.
Definition: geomVertexWriter.I:640
LineSegs::get_current_position
const LVertex & get_current_position()
Returns the pen's current position.
Definition: lineSegs.cxx:154
geomPoints.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
LineSegs::create
GeomNode * create(bool dynamic=false)
Creates a new GeomNode that will render the series of line segments and points described via calls to...
Definition: lineSegs.I:108
GeomNode::add_geom
void add_geom(Geom *geom, const RenderState *state=RenderState::make_empty())
Adds a new Geom to the node.
Definition: geomNode.cxx:584
LineSegs::is_empty
bool is_empty()
Returns true if move_to() or draw_to() have not been called since the last reset() or create(),...
Definition: lineSegs.cxx:96
LineSegs::LineSegs
LineSegs(const std::string &name="lines")
Constructs a LineSegs object, which can be used to create any number of disconnected lines or points ...
Definition: lineSegs.cxx:32
GeomVertexFormat::get_v3cp
static const GeomVertexFormat * get_v3cp()
Returns a standard vertex format with a packed color and a 3-component vertex position.
Definition: geomVertexFormat.I:287
GeomVertexWriter::add_data4
void add_data4(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z, PN_stdfloat w)
Sets the write row to a particular 4-component value, and advances the write row.
Definition: geomVertexWriter.I:1164
LineSegs::set_vertex
void set_vertex(int n, const LVertex &vert)
Moves the nth point or vertex of the line segment sequence generated by the last call to create().
Definition: lineSegs.cxx:120