Panda3D
physxConvexMeshDesc.cxx
1 // Filename: physxConvexMeshDesc.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 "physxConvexMeshDesc.h"
16 #include "physxManager.h"
17 
18 #include "nodePathCollection.h"
19 #include "geomNode.h"
20 #include "geomVertexReader.h"
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: PhysxConvexMeshDesc::set_num_vertices
24 // Access: Published
25 // Description: Sets the number of vertices to be stored within
26 // this convex 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 //
32 // The number of vertices in a single convex mesh has
33 // to be smaller than 256.
34 ////////////////////////////////////////////////////////////////////
36 set_num_vertices(unsigned int numVertices) {
37 
38  nassertv_always(numVertices < 256);
39 
40  if (_desc.points) {
41  delete [] _vertices;
42  }
43 
44  _vertices = new NxVec3[numVertices];
45 
46  _desc.numVertices = numVertices;
47  _desc.points = _vertices;
48 }
49 
50 ////////////////////////////////////////////////////////////////////
51 // Function: PhysxConvexMeshDesc::set_vertex
52 // Access: Published
53 // Description: Sets a single vertex. You have to call the function
54 // set_num_vertices before you can call this function.
55 ////////////////////////////////////////////////////////////////////
57 set_vertex(unsigned int idx, const LPoint3f &vert) {
58 
59  nassertv(_desc.numVertices > idx);
60  _vertices[idx] = PhysxManager::point3_to_nxVec3(vert);
61 }
62 
63 ////////////////////////////////////////////////////////////////////
64 // Function: PhysxConvexMeshDesc::get_desc
65 // Access: Public
66 // Description:
67 ////////////////////////////////////////////////////////////////////
68 const NxConvexMeshDesc &PhysxConvexMeshDesc::
69 get_desc() const {
70 
71  return _desc;
72 }
73 
74 ////////////////////////////////////////////////////////////////////
75 // Function: PhysxConvexMeshDesc::set_from_node_path
76 // Access: Published
77 // Description: A convenience method to set the mesh data from
78 // a NodePath in a single call. The method iterates
79 // over the NodePath geoms and collects data for
80 // the convex mesh.
81 //
82 // Do not use the following function when using this
83 // one:
84 // - set_num_vertices
85 // - set_vertex
86 ////////////////////////////////////////////////////////////////////
89 
90  pvector<LPoint3f> dataVertices;
91 
92  // Collect data from NodePath
93  NodePathCollection npc = np.find_all_matches( "**/+GeomNode" );
94  for (int i=0; i<npc.get_num_paths(); i++) {
95  NodePath gnp = npc.get_path(i);
96  GeomNode *gnode = DCAST(GeomNode, gnp.node());
97 
98  for (int j=0; j<gnode->get_num_geoms(); j++) {
99  CPT(Geom) geom = gnode->get_geom(j);
100  CPT(GeomVertexData) vdata = geom->get_vertex_data();
101  GeomVertexReader reader = GeomVertexReader(vdata, InternalName::get_vertex());
102 
103  while (!reader.is_at_end()) {
104  dataVertices.push_back(reader.get_data3f());
105  }
106  }
107  }
108 
109  // Set descriptor members
110  int i;
111 
112  NxU32 numVertices = dataVertices.size();
113 
114  _vertices = new NxVec3[numVertices];
115 
116  i = 0;
118  for (it=dataVertices.begin(); it!=dataVertices.end(); it++) {
119  LPoint3f v = *it;
120 
121  _vertices[i].x = v.get_x();
122  _vertices[i].y = v.get_y();
123  _vertices[i].z = v.get_z();
124  i++;
125  }
126 
127  _desc.numVertices = numVertices;
128  _desc.points = _vertices;
129 }
130 
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:77
void set_from_node_path(const NodePath &np)
A convenience method to set the mesh data from a NodePath in a single call.
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
void set_vertex(unsigned int idx, const LPoint3f &vert)
Sets a single vertex.
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
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.
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...
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
This is a set of zero or more NodePaths.