Panda3D
physxTriangleMeshDesc.cxx
1 // Filename: physxTriangleMeshDesc.cxx
2 // Created by: enn0x (11Oct09)
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 "physxTriangleMeshDesc.h"
16 #include "physxManager.h"
17 
18 #include "nodePathCollection.h"
19 #include "geomNode.h"
20 #include "geomVertexReader.h"
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: PhysxTriangleMeshDesc::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  if (_desc.points) {
36  delete [] _vertices;
37  }
38 
39  _vertices = new NxVec3[numVertices];
40 
41  _desc.numVertices = numVertices;
42  _desc.points = _vertices;
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: PhysxTriangleMeshDesc::set_vertex
47 // Access: Published
48 // Description: Sets a single vertex. You have to call the function
49 // set_num_vertices before you can call this function.
50 ////////////////////////////////////////////////////////////////////
52 set_vertex(unsigned int idx, const LPoint3f &vert) {
53 
54  nassertv(_desc.numVertices > idx);
55  _vertices[idx] = PhysxManager::point3_to_nxVec3(vert);
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: PhysxTriangleMeshDesc::set_num_triangles
60 // Access: Published
61 // Description: Sets the number of triangles to be stored in this
62 // triangle mesh.
63 //
64 // This method must be called before any calls to
65 // set_triangle are done!
66 ////////////////////////////////////////////////////////////////////
68 set_num_triangles(unsigned int numTriangles, bool use_material_indices) {
69 
70  if (_desc.triangles) {
71  delete [] _triangles;
72  }
73 
74  if (_desc.materialIndices) {
75  delete [] _materials;
76  }
77 
78  _triangles = new NxU32[3 * numTriangles];
79 
80  _desc.numTriangles = numTriangles;
81  _desc.triangles = _triangles;
82 
83  if (use_material_indices == true) {
84  _materials = new NxMaterialIndex[numTriangles];
85  _desc.materialIndices = _materials;
86  }
87 }
88 
89 ////////////////////////////////////////////////////////////////////
90 // Function: PhysxTriangleMeshDesc::set_triangles
91 // Access: Published
92 // Description: Sets a single triangle, by providing the three
93 // indices i1, i2, i3.
94 ////////////////////////////////////////////////////////////////////
96 set_triangle(unsigned int idx,
97  unsigned int i1, unsigned int i2, unsigned int i3,
98  unsigned int material_index) {
99 
100  nassertv(_desc.numTriangles > idx);
101 
102  if (_desc.materialIndices) {
103  _materials[idx] = (NxMaterialIndex) material_index;
104  }
105 
106  idx = 3 * idx;
107  _triangles[idx] = i1;
108  _triangles[idx + 1] = i2;
109  _triangles[idx + 2] = i3;
110 }
111 
112 ////////////////////////////////////////////////////////////////////
113 // Function: PhysxTriangleMeshDesc::get_desc
114 // Access: Public
115 // Description:
116 ////////////////////////////////////////////////////////////////////
117 const NxTriangleMeshDesc &PhysxTriangleMeshDesc::
118 get_desc() const {
119 
120  return _desc;
121 }
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: PhysxTriangleMeshDesc::set_from_node_path
125 // Access: Published
126 // Description: A convenience method to set the mesh data from
127 // a NodePath in a single call. The method iterates
128 // over the NodePath geoms and collects data for
129 // the triangle mesh.
130 //
131 // Do not use the following function when using this
132 // one:
133 // - set_num_vertices
134 // - set_vertex
135 // - set_num_triangles
136 // - set_triangle
137 ////////////////////////////////////////////////////////////////////
140 
141  pvector<LPoint3f> dataVertices;
142  pvector<int> dataIndices;
143 
144  // Collect data from NodePath
145  NodePathCollection npc = np.find_all_matches( "**/+GeomNode" );
146  for (int i=0; i<npc.get_num_paths(); i++) {
147  NodePath gnp = npc.get_path(i);
148  GeomNode *gnode = DCAST(GeomNode, gnp.node());
149 
150  for (int j=0; j<gnode->get_num_geoms(); j++) {
151  CPT(Geom) geom = gnode->get_geom(j);
152 
153  // Vertices
154  CPT(GeomVertexData) vdata = geom->get_vertex_data();
155  GeomVertexReader reader = GeomVertexReader(vdata, InternalName::get_vertex());
156 
157  while (!reader.is_at_end()) {
158  dataVertices.push_back(reader.get_data3f());
159  }
160 
161  // Indices
162  for (int k=0; k<geom->get_num_primitives(); k++) {
163 
164  CPT(GeomPrimitive) prim = geom->get_primitive(k);
165  prim = prim->decompose();
166 
167  for (int l=0; l<prim->get_num_primitives(); l++) {
168 
169  int s = prim->get_primitive_start(l);
170  int e = prim->get_primitive_end(l);
171 
172  for (int l=s; l<e; l++) {
173  dataIndices.push_back(prim->get_vertex(l));
174  }
175  }
176  }
177  }
178  }
179 
180  // Set descriptor members
181  int i;
182 
183  NxU32 numVertices = dataVertices.size();
184  NxU32 numTriangles = dataIndices.size() / 3;
185 
186  _vertices = new NxVec3[numVertices];
187  _triangles = new NxU32[3 * numTriangles];
188 
189  i = 0;
191  for (vit=dataVertices.begin(); vit!=dataVertices.end(); vit++) {
192  LPoint3f v = *vit;
193 
194  _vertices[i].x = v.get_x();
195  _vertices[i].y = v.get_y();
196  _vertices[i].z = v.get_z();
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 = _vertices;
211  _desc.numTriangles = numTriangles;
212  _desc.triangles = _triangles;
213 }
214 
static NxVec3 point3_to_nxVec3(const LPoint3f &p)
Converts from LPoint3f to NxVec3.
Definition: physxManager.I:77
void set_num_vertices(unsigned int n)
Sets the number of vertices to be stored within this triangle mesh.
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
Definition: geomPrimitive.h:63
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
int get_num_geoms() const
Returns the number of geoms in the node.
Definition: geomNode.I:46
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
Definition: geom.h:58
void set_vertex(unsigned int idx, const LPoint3f &vert)
Sets a single vertex.
NodePath get_path(int index) const
Returns the nth NodePath in the collection.
int get_num_paths() const
Returns the number of NodePaths in the collection.
void set_num_triangles(unsigned int n, bool use_material_indices=false)
Sets the number of triangles to be stored in this triangle mesh.
PandaNode * node() const
Returns the referenced node of the path.
Definition: nodePath.I:284
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...
void set_triangle(unsigned int idx, unsigned int i1, unsigned int i2, unsigned int i3, unsigned int material_index=1)
Sets a single triangle, by providing the three indices i1, i2, i3.
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
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
void set_from_node_path(const NodePath &np)
A convenience method to set the mesh data from a NodePath in a single call.
This is a set of zero or more NodePaths.