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