Panda3D
|
00001 // Filename: physxConvexMeshDesc.cxx 00002 // Created by: enn0x (11Oct09) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "physxConvexMeshDesc.h" 00016 #include "physxManager.h" 00017 00018 #include "nodePathCollection.h" 00019 #include "geomNode.h" 00020 #include "geomVertexReader.h" 00021 00022 //////////////////////////////////////////////////////////////////// 00023 // Function: PhysxConvexMeshDesc::set_num_vertices 00024 // Access: Published 00025 // Description: Sets the number of vertices to be stored within 00026 // this convex mesh. The function allocates memory 00027 // for the vertices, but it does not set any vertices. 00028 // 00029 // This method must be called before any calls to 00030 // set_vertex are done! 00031 // 00032 // The number of vertices in a single convex mesh has 00033 // to be smaller than 256. 00034 //////////////////////////////////////////////////////////////////// 00035 void PhysxConvexMeshDesc:: 00036 set_num_vertices(unsigned int numVertices) { 00037 00038 nassertv_always(numVertices < 256); 00039 00040 if (_desc.points) { 00041 delete [] _vertices; 00042 } 00043 00044 _vertices = new NxVec3[numVertices]; 00045 00046 _desc.numVertices = numVertices; 00047 _desc.points = _vertices; 00048 } 00049 00050 //////////////////////////////////////////////////////////////////// 00051 // Function: PhysxConvexMeshDesc::set_vertex 00052 // Access: Published 00053 // Description: Sets a single vertex. You have to call the function 00054 // set_num_vertices before you can call this function. 00055 //////////////////////////////////////////////////////////////////// 00056 void PhysxConvexMeshDesc:: 00057 set_vertex(unsigned int idx, const LPoint3f &vert) { 00058 00059 nassertv(_desc.numVertices > idx); 00060 _vertices[idx] = PhysxManager::point3_to_nxVec3(vert); 00061 } 00062 00063 //////////////////////////////////////////////////////////////////// 00064 // Function: PhysxConvexMeshDesc::get_desc 00065 // Access: Public 00066 // Description: 00067 //////////////////////////////////////////////////////////////////// 00068 const NxConvexMeshDesc &PhysxConvexMeshDesc:: 00069 get_desc() const { 00070 00071 return _desc; 00072 } 00073 00074 //////////////////////////////////////////////////////////////////// 00075 // Function: PhysxConvexMeshDesc::set_from_node_path 00076 // Access: Published 00077 // Description: A convenience method to set the mesh data from 00078 // a NodePath in a single call. The method iterates 00079 // over the NodePath geoms and collects data for 00080 // the convex mesh. 00081 // 00082 // Do not use the following function when using this 00083 // one: 00084 // - set_num_vertices 00085 // - set_vertex 00086 //////////////////////////////////////////////////////////////////// 00087 void PhysxConvexMeshDesc:: 00088 set_from_node_path(const NodePath &np) { 00089 00090 pvector<LPoint3f> dataVertices; 00091 00092 // Collect data from NodePath 00093 NodePathCollection npc = np.find_all_matches( "**/+GeomNode" ); 00094 for (int i=0; i<npc.get_num_paths(); i++) { 00095 NodePath gnp = npc.get_path(i); 00096 GeomNode *gnode = DCAST(GeomNode, gnp.node()); 00097 00098 for (int j=0; j<gnode->get_num_geoms(); j++) { 00099 CPT(Geom) geom = gnode->get_geom(j); 00100 CPT(GeomVertexData) vdata = geom->get_vertex_data(); 00101 GeomVertexReader reader = GeomVertexReader(vdata, InternalName::get_vertex()); 00102 00103 while (!reader.is_at_end()) { 00104 dataVertices.push_back(reader.get_data3f()); 00105 } 00106 } 00107 } 00108 00109 // Set descriptor members 00110 int i; 00111 00112 NxU32 numVertices = dataVertices.size(); 00113 00114 _vertices = new NxVec3[numVertices]; 00115 00116 i = 0; 00117 pvector<LPoint3f>::const_iterator it; 00118 for (it=dataVertices.begin(); it!=dataVertices.end(); it++) { 00119 LPoint3f v = *it; 00120 00121 _vertices[i].x = v.get_x(); 00122 _vertices[i].y = v.get_y(); 00123 _vertices[i].z = v.get_z(); 00124 i++; 00125 } 00126 00127 _desc.numVertices = numVertices; 00128 _desc.points = _vertices; 00129 } 00130