Panda3D
physxConvexMeshDesc.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 physxConvexMeshDesc.cxx
10  * @author enn0x
11  * @date 2009-10-11
12  */
13 
14 #include "physxConvexMeshDesc.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 convex 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  *
28  * The number of vertices in a single convex mesh has to be smaller than 256.
29  */
31 set_num_vertices(unsigned int numVertices) {
32 
33  nassertv_always(numVertices < 256);
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  * Sets a single vertex. You have to call the function set_num_vertices
47  * before you can call this function.
48  */
50 set_vertex(unsigned int idx, const LPoint3f &vert) {
51 
52  nassertv(_desc.numVertices > idx);
53  _vertices[idx] = PhysxManager::point3_to_nxVec3(vert);
54 }
55 
56 /**
57  *
58  */
59 const NxConvexMeshDesc &PhysxConvexMeshDesc::
60 get_desc() const {
61 
62  return _desc;
63 }
64 
65 /**
66  * A convenience method to set the mesh data from a NodePath in a single call.
67  * The method iterates over the NodePath geoms and collects data for the
68  * convex mesh.
69  *
70  * Do not use the following function when using this one: - set_num_vertices -
71  * set_vertex
72  */
74 set_from_node_path(const NodePath &np) {
75 
76  pvector<LPoint3f> dataVertices;
77 
78  // Collect data from NodePath
79  NodePathCollection npc = np.find_all_matches( "**/+GeomNode" );
80  for (int i=0; i<npc.get_num_paths(); i++) {
81  NodePath gnp = npc.get_path(i);
82  GeomNode *gnode = DCAST(GeomNode, gnp.node());
83 
84  for (int j=0; j<gnode->get_num_geoms(); j++) {
85  CPT(Geom) geom = gnode->get_geom(j);
86  CPT(GeomVertexData) vdata = geom->get_vertex_data();
87  GeomVertexReader reader = GeomVertexReader(vdata, InternalName::get_vertex());
88 
89  while (!reader.is_at_end()) {
90  dataVertices.push_back(reader.get_data3f());
91  }
92  }
93  }
94 
95  // Set descriptor members
96  int i;
97 
98  NxU32 numVertices = dataVertices.size();
99 
100  _vertices = new NxVec3[numVertices];
101 
102  i = 0;
104  for (it=dataVertices.begin(); it!=dataVertices.end(); it++) {
105  LPoint3f v = *it;
106 
107  _vertices[i].x = v.get_x();
108  _vertices[i].y = v.get_y();
109  _vertices[i].z = v.get_z();
110  i++;
111  }
112 
113  _desc.numVertices = numVertices;
114  _desc.points = _vertices;
115 }
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:34
get_num_geoms
Returns the number of geoms in the node.
Definition: geomNode.h:71
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
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.
const LVecBase3f & get_data3f()
Returns the data associated with the read row, expressed as a 3-component value, and advances the rea...
A container for geometry primitives.
Definition: geom.h:54
This is a set of zero or more NodePaths.
get_num_paths
Returns the number of NodePaths in the collection.
get_path
Returns the nth NodePath in the collection.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:159
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:358
PandaNode * node() const
Returns the referenced node of the path.
Definition: nodePath.I:227
void set_vertex(unsigned int idx, const LPoint3f &vert)
Sets a single vertex.
void set_from_node_path(const NodePath &np)
A convenience method to set the mesh data from a NodePath in a single call.
void set_num_vertices(unsigned int n)
Sets the number of vertices to be stored within this convex mesh.
static NxVec3 point3_to_nxVec3(const LPoint3f &p)
Converts from LPoint3f to NxVec3.
Definition: physxManager.I:63
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.