Panda3D
physxClothMeshDesc.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 physxClothMeshDesc.cxx
10  * @author enn0x
11  * @date 2010-03-28
12  */
13 
14 #include "physxClothMeshDesc.h"
15 #include "physxManager.h"
16 
17 #include "nodePathCollection.h"
18 #include "geomNode.h"
19 #include "geomVertexReader.h"
20 
21 /**
22  * Sets the number of vertices to be stored within this triangle mesh. The
23  * function allocates memory for the vertices, but it does not set any
24  * vertices.
25  *
26  * This method must be called before any calls to set_vertex are done!
27  */
29 set_num_vertices(unsigned int numVertices) {
30 
31  // Vertices
32  if (_desc.points) {
33  delete [] _points;
34  }
35 
36  _points = new NxVec3[numVertices];
37 
38  _desc.numVertices = numVertices;
39  _desc.points = _points;
40 
41  // Texture coordinates
42  if (_texcoords) {
43  delete [] _texcoords;
44  }
45 
46  _texcoords = new LPoint2f[numVertices];
47 }
48 
49 /**
50  * Sets a single vertex. You have to call the function set_num_vertices
51  * before you can call this function.
52  */
54 set_vertex(unsigned int idx, const LPoint3f &vert, const LPoint2f &texcoord) {
55 
56  nassertv(_desc.numVertices > idx);
57 
58  _points[idx] = PhysxManager::point3_to_nxVec3(vert);
59  _texcoords[idx] = texcoord;
60 }
61 
62 /**
63  * Sets the number of triangles to be stored in this triangle mesh.
64  *
65  * This method must be called before any calls to set_triangle are done!
66  */
68 set_num_triangles(unsigned int numTriangles) {
69 
70  if (_desc.triangles) {
71  delete [] _triangles;
72  }
73 
74  _triangles = new NxU32[3 * numTriangles];
75 
76  _desc.numTriangles = numTriangles;
77  _desc.triangles = _triangles;
78 }
79 
80 /**
81  * Sets a single triangle, by providing the three indices i1, i2, i3.
82  */
84 set_triangle(unsigned int idx,
85  unsigned int i1, unsigned int i2, unsigned int i3) {
86 
87  nassertv(_desc.numTriangles > idx);
88 
89  idx = 3 * idx;
90  _triangles[idx] = i1;
91  _triangles[idx + 1] = i2;
92  _triangles[idx + 2] = i3;
93 }
94 
95 /**
96  * A convenience method to set the mesh data from a NodePath in a single call.
97  * The method iterates over the NodePath geoms and collects data for the
98  * triangle mesh.
99  *
100  * Do not use the following function when using this one: - set_num_vertices -
101  * set_vertex - set_num_triangles - set_triangle
102  */
105 
106  pvector<LPoint3f> dataVertices;
107  pvector<LPoint2f> dataTexcoords;
108  pvector<int> dataIndices;
109 
110  // Collect data from NodePath
111  NodePathCollection npc = np.find_all_matches( "**/+GeomNode" );
112  for (int i=0; i<npc.get_num_paths(); i++) {
113  NodePath gnp = npc.get_path(i);
114  GeomNode *gnode = DCAST(GeomNode, gnp.node());
115 
116  for (int j=0; j<gnode->get_num_geoms(); j++) {
117  CPT(Geom) geom = gnode->get_geom(j);
118  CPT(GeomVertexData) vdata = geom->get_vertex_data();
119  GeomVertexReader reader;
120 
121  // Vertices
122  reader = GeomVertexReader(vdata, InternalName::get_vertex());
123  while (!reader.is_at_end()) {
124  dataVertices.push_back(reader.get_data3f());
125  }
126 
127  // Texcoords
128  reader = GeomVertexReader(vdata, InternalName::get_texcoord());
129  while (!reader.is_at_end()) {
130  dataTexcoords.push_back(reader.get_data2f());
131  }
132 
133  // Indices
134  for (int k=0; k<geom->get_num_primitives(); k++) {
135 
136  CPT(GeomPrimitive) prim = geom->get_primitive(k);
137  prim = prim->decompose();
138 
139  for (int l=0; l<prim->get_num_primitives(); l++) {
140 
141  int s = prim->get_primitive_start(l);
142  int e = prim->get_primitive_end(l);
143 
144  for (int l=s; l<e; l++) {
145  dataIndices.push_back(prim->get_vertex(l));
146  }
147  }
148  }
149  }
150  }
151 
152  // Set descriptor members
153  int i;
154 
155  NxU32 numVertices = dataVertices.size();
156  NxU32 numTriangles = dataIndices.size() / 3;
157 
158  _points = new NxVec3[numVertices];
159  _triangles = new NxU32[3 * numTriangles];
160  _texcoords = new LPoint2f[numVertices];
161 
162  i = 0;
164  for (vit=dataVertices.begin(); vit!=dataVertices.end(); vit++) {
165  LPoint3f v = *vit;
166 
167  _points[i] = PhysxManager::point3_to_nxVec3(v);
168  i++;
169  }
170 
171  i = 0;
173  for (tcit=dataTexcoords.begin(); tcit!=dataTexcoords.end(); tcit++) {
174  LPoint2f tc = *tcit;
175 
176  _texcoords[i] = tc;
177  i++;
178  }
179 
180  i = 0;
182  for(iit=dataIndices.begin(); iit!=dataIndices.end(); iit++) {
183  NxU32 idx = *iit;
184 
185  _triangles[i] = idx;
186  i++;
187  }
188 
189  _desc.numVertices = numVertices;
190  _desc.points = _points;
191  _desc.numTriangles = numTriangles;
192  _desc.triangles = _triangles;
193 }
void set_from_node_path(const NodePath &np)
A convenience method to set the mesh data from a NodePath in a single call.
NodePathCollection find_all_matches(const std::string &path) const
Returns the complete set of all NodePaths that begin with this NodePath and can be extended by path.
Definition: nodePath.cxx:354
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static NxVec3 point3_to_nxVec3(const LPoint3f &p)
Converts from LPoint3f to NxVec3.
Definition: physxManager.I:63
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
Definition: geomPrimitive.h:56
const LVecBase3f & get_data3f()
Returns the data associated with the read row, expressed as a 3-component value, and advances the rea...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
void set_vertex(unsigned int idx, const LPoint3f &vert, const LPoint2f &texcoord)
Sets a single vertex.
void set_num_triangles(unsigned int n)
Sets the number of triangles to be stored in this triangle mesh.
get_path
Returns the nth NodePath in the collection.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
void set_triangle(unsigned int idx, unsigned int i1, unsigned int i2, unsigned int i3)
Sets a single triangle, by providing the three indices i1, i2, i3.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A container for geometry primitives.
Definition: geom.h:54
const LVecBase2f & get_data2f()
Returns the data associated with the read row, expressed as a 2-component value, and advances the rea...
void set_num_vertices(unsigned int n)
Sets the number of vertices to be stored within this triangle mesh.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PandaNode * node() const
Returns the referenced node of the path.
Definition: nodePath.I:227
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
get_num_geoms
Returns the number of geoms in the node.
Definition: geomNode.h:71
This object provides a high-level interface for quickly reading a sequence of numeric values from a v...
bool is_at_end() const
Returns true if the reader is currently at the end of the list of vertices, false otherwise.
get_num_paths
Returns the number of NodePaths in the collection.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:161
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:34
This is a set of zero or more NodePaths.