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