Panda3D
|
00001 // Filename: physxTriangleMeshDesc.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 "physxTriangleMeshDesc.h" 00016 #include "physxManager.h" 00017 00018 #include "nodePathCollection.h" 00019 #include "geomNode.h" 00020 #include "geomVertexReader.h" 00021 00022 //////////////////////////////////////////////////////////////////// 00023 // Function: PhysxTriangleMeshDesc::set_num_vertices 00024 // Access: Published 00025 // Description: Sets the number of vertices to be stored within 00026 // this triangle 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 void PhysxTriangleMeshDesc:: 00033 set_num_vertices(unsigned int numVertices) { 00034 00035 if (_desc.points) { 00036 delete [] _vertices; 00037 } 00038 00039 _vertices = new NxVec3[numVertices]; 00040 00041 _desc.numVertices = numVertices; 00042 _desc.points = _vertices; 00043 } 00044 00045 //////////////////////////////////////////////////////////////////// 00046 // Function: PhysxTriangleMeshDesc::set_vertex 00047 // Access: Published 00048 // Description: Sets a single vertex. You have to call the function 00049 // set_num_vertices before you can call this function. 00050 //////////////////////////////////////////////////////////////////// 00051 void PhysxTriangleMeshDesc:: 00052 set_vertex(unsigned int idx, const LPoint3f &vert) { 00053 00054 nassertv(_desc.numVertices > idx); 00055 _vertices[idx] = PhysxManager::point3_to_nxVec3(vert); 00056 } 00057 00058 //////////////////////////////////////////////////////////////////// 00059 // Function: PhysxTriangleMeshDesc::set_num_triangles 00060 // Access: Published 00061 // Description: Sets the number of triangles to be stored in this 00062 // triangle mesh. 00063 // 00064 // This method must be called before any calls to 00065 // set_triangle are done! 00066 //////////////////////////////////////////////////////////////////// 00067 void PhysxTriangleMeshDesc:: 00068 set_num_triangles(unsigned int numTriangles, bool use_material_indices) { 00069 00070 if (_desc.triangles) { 00071 delete [] _triangles; 00072 } 00073 00074 if (_desc.materialIndices) { 00075 delete [] _materials; 00076 } 00077 00078 _triangles = new NxU32[3 * numTriangles]; 00079 00080 _desc.numTriangles = numTriangles; 00081 _desc.triangles = _triangles; 00082 00083 if (use_material_indices == true) { 00084 _materials = new NxMaterialIndex[numTriangles]; 00085 _desc.materialIndices = _materials; 00086 } 00087 } 00088 00089 //////////////////////////////////////////////////////////////////// 00090 // Function: PhysxTriangleMeshDesc::set_triangles 00091 // Access: Published 00092 // Description: Sets a single triangle, by providing the three 00093 // indices i1, i2, i3. 00094 //////////////////////////////////////////////////////////////////// 00095 void PhysxTriangleMeshDesc:: 00096 set_triangle(unsigned int idx, 00097 unsigned int i1, unsigned int i2, unsigned int i3, 00098 unsigned int material_index) { 00099 00100 nassertv(_desc.numTriangles > idx); 00101 00102 if (_desc.materialIndices) { 00103 _materials[idx] = (NxMaterialIndex) material_index; 00104 } 00105 00106 idx = 3 * idx; 00107 _triangles[idx] = i1; 00108 _triangles[idx + 1] = i2; 00109 _triangles[idx + 2] = i3; 00110 } 00111 00112 //////////////////////////////////////////////////////////////////// 00113 // Function: PhysxTriangleMeshDesc::get_desc 00114 // Access: Public 00115 // Description: 00116 //////////////////////////////////////////////////////////////////// 00117 const NxTriangleMeshDesc &PhysxTriangleMeshDesc:: 00118 get_desc() const { 00119 00120 return _desc; 00121 } 00122 00123 //////////////////////////////////////////////////////////////////// 00124 // Function: PhysxTriangleMeshDesc::set_from_node_path 00125 // Access: Published 00126 // Description: A convenience method to set the mesh data from 00127 // a NodePath in a single call. The method iterates 00128 // over the NodePath geoms and collects data for 00129 // the triangle mesh. 00130 // 00131 // Do not use the following function when using this 00132 // one: 00133 // - set_num_vertices 00134 // - set_vertex 00135 // - set_num_triangles 00136 // - set_triangle 00137 //////////////////////////////////////////////////////////////////// 00138 void PhysxTriangleMeshDesc:: 00139 set_from_node_path(const NodePath &np) { 00140 00141 pvector<LPoint3f> dataVertices; 00142 pvector<int> dataIndices; 00143 00144 // Collect data from NodePath 00145 NodePathCollection npc = np.find_all_matches( "**/+GeomNode" ); 00146 for (int i=0; i<npc.get_num_paths(); i++) { 00147 NodePath gnp = npc.get_path(i); 00148 GeomNode *gnode = DCAST(GeomNode, gnp.node()); 00149 00150 for (int j=0; j<gnode->get_num_geoms(); j++) { 00151 CPT(Geom) geom = gnode->get_geom(j); 00152 00153 // Vertices 00154 CPT(GeomVertexData) vdata = geom->get_vertex_data(); 00155 GeomVertexReader reader = GeomVertexReader(vdata, InternalName::get_vertex()); 00156 00157 while (!reader.is_at_end()) { 00158 dataVertices.push_back(reader.get_data3f()); 00159 } 00160 00161 // Indices 00162 for (int k=0; k<geom->get_num_primitives(); k++) { 00163 00164 CPT(GeomPrimitive) prim = geom->get_primitive(k); 00165 prim = prim->decompose(); 00166 00167 for (int l=0; l<prim->get_num_primitives(); l++) { 00168 00169 int s = prim->get_primitive_start(l); 00170 int e = prim->get_primitive_end(l); 00171 00172 for (int l=s; l<e; l++) { 00173 dataIndices.push_back(prim->get_vertex(l)); 00174 } 00175 } 00176 } 00177 } 00178 } 00179 00180 // Set descriptor members 00181 int i; 00182 00183 NxU32 numVertices = dataVertices.size(); 00184 NxU32 numTriangles = dataIndices.size() / 3; 00185 00186 _vertices = new NxVec3[numVertices]; 00187 _triangles = new NxU32[3 * numTriangles]; 00188 00189 i = 0; 00190 pvector<LPoint3f>::const_iterator vit; 00191 for (vit=dataVertices.begin(); vit!=dataVertices.end(); vit++) { 00192 LPoint3f v = *vit; 00193 00194 _vertices[i].x = v.get_x(); 00195 _vertices[i].y = v.get_y(); 00196 _vertices[i].z = v.get_z(); 00197 i++; 00198 } 00199 00200 i = 0; 00201 pvector<int>::const_iterator iit; 00202 for(iit=dataIndices.begin(); iit!=dataIndices.end(); iit++) { 00203 NxU32 idx = *iit; 00204 00205 _triangles[i] = idx; 00206 i++; 00207 } 00208 00209 _desc.numVertices = numVertices; 00210 _desc.points = _vertices; 00211 _desc.numTriangles = numTriangles; 00212 _desc.triangles = _triangles; 00213 } 00214