Panda3D
|
00001 // Filename: physxMeshPool.cxx 00002 // Created by: enn0x (14Oct09) 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 "physxMeshPool.h" 00016 #include "physxConvexMesh.h" 00017 #include "physxTriangleMesh.h" 00018 #include "physxClothMesh.h" 00019 #include "physxSoftBodyMesh.h" 00020 00021 #include "physxFileStream.h" 00022 #include "virtualFileSystem.h" 00023 00024 PhysxMeshPool::ConvexMeshes PhysxMeshPool::_convex_meshes; 00025 PhysxMeshPool::TriangleMeshes PhysxMeshPool::_triangle_meshes; 00026 PhysxMeshPool::ClothMeshes PhysxMeshPool::_cloth_meshes; 00027 PhysxMeshPool::SoftbodyMeshes PhysxMeshPool::_softbody_meshes; 00028 00029 //////////////////////////////////////////////////////////////////// 00030 // Function: PhysxMeshPool::check_file 00031 // Access: Private 00032 // Description: 00033 //////////////////////////////////////////////////////////////////// 00034 bool PhysxMeshPool:: 00035 check_filename(const Filename &fn) { 00036 00037 if (!(VirtualFileSystem::get_global_ptr()->exists(fn))) { 00038 physx_cat.error() << "File does not exists: " << fn << endl; 00039 return false; 00040 } 00041 00042 if (!(VirtualFileSystem::get_global_ptr()->is_regular_file(fn))) { 00043 physx_cat.error() << "Not a regular file: " << fn << endl; 00044 return false; 00045 } 00046 00047 return true; 00048 } 00049 00050 //////////////////////////////////////////////////////////////////// 00051 // Function: PhysxMeshPool::load_convex_mesh 00052 // Access: Published 00053 // Description: 00054 //////////////////////////////////////////////////////////////////// 00055 PhysxConvexMesh *PhysxMeshPool:: 00056 load_convex_mesh(const Filename &fn) { 00057 00058 if (!check_filename(fn)) return NULL; 00059 00060 PhysxConvexMesh *mesh; 00061 00062 ConvexMeshes::iterator it = _convex_meshes.find(fn); 00063 if (it == _convex_meshes.end()) { 00064 // Not found; load mesh. 00065 NxConvexMesh *meshPtr; 00066 PhysxFileStream stream = PhysxFileStream(fn, true); 00067 00068 mesh = new PhysxConvexMesh(); 00069 nassertr_always(mesh, NULL); 00070 00071 NxPhysicsSDK *sdk = NxGetPhysicsSDK(); 00072 nassertr_always(sdk, NULL); 00073 00074 meshPtr = sdk->createConvexMesh(stream); 00075 nassertr_always(meshPtr, NULL); 00076 00077 mesh->link(meshPtr); 00078 00079 _convex_meshes.insert(ConvexMeshes::value_type(fn, mesh)); 00080 } 00081 else { 00082 // Found; return previously loaded mesh. 00083 mesh = (*it).second; 00084 } 00085 00086 return mesh; 00087 } 00088 00089 //////////////////////////////////////////////////////////////////// 00090 // Function: PhysxMeshPool::load_triangle_mesh 00091 // Access: Published 00092 // Description: 00093 //////////////////////////////////////////////////////////////////// 00094 PhysxTriangleMesh *PhysxMeshPool:: 00095 load_triangle_mesh(const Filename &fn) { 00096 00097 if (!check_filename(fn)) return NULL; 00098 00099 PhysxTriangleMesh *mesh; 00100 00101 TriangleMeshes::iterator it = _triangle_meshes.find(fn); 00102 if (it == _triangle_meshes.end()) { 00103 // Not found; load mesh. 00104 NxTriangleMesh *meshPtr; 00105 PhysxFileStream stream = PhysxFileStream(fn, true); 00106 00107 mesh = new PhysxTriangleMesh(); 00108 nassertr_always(mesh, NULL); 00109 00110 NxPhysicsSDK *sdk = NxGetPhysicsSDK(); 00111 nassertr_always(sdk, NULL); 00112 00113 meshPtr = sdk->createTriangleMesh(stream); 00114 nassertr_always(meshPtr, NULL); 00115 00116 mesh->link(meshPtr); 00117 00118 _triangle_meshes.insert(TriangleMeshes::value_type(fn, mesh)); 00119 } 00120 else { 00121 // Found; return previously loaded mesh. 00122 mesh = (*it).second; 00123 } 00124 00125 return mesh; 00126 } 00127 00128 //////////////////////////////////////////////////////////////////// 00129 // Function: PhysxMeshPool::load_cloth_mesh 00130 // Access: Published 00131 // Description: 00132 //////////////////////////////////////////////////////////////////// 00133 PhysxClothMesh *PhysxMeshPool:: 00134 load_cloth_mesh(const Filename &fn) { 00135 00136 if (!check_filename(fn)) return NULL; 00137 00138 PhysxClothMesh *mesh; 00139 00140 ClothMeshes::iterator it = _cloth_meshes.find(fn); 00141 if (it == _cloth_meshes.end()) { 00142 // Not found; load mesh. 00143 NxClothMesh *meshPtr; 00144 PhysxFileStream stream = PhysxFileStream(fn, true); 00145 00146 mesh = new PhysxClothMesh(); 00147 nassertr_always(mesh, NULL); 00148 00149 NxPhysicsSDK *sdk = NxGetPhysicsSDK(); 00150 nassertr_always(sdk, NULL); 00151 00152 meshPtr = sdk->createClothMesh(stream); 00153 nassertr_always(meshPtr, NULL); 00154 00155 mesh->link(meshPtr); 00156 00157 _cloth_meshes.insert(ClothMeshes::value_type(fn, mesh)); 00158 } 00159 else { 00160 // Found; return previously loaded mesh. 00161 mesh = (*it).second; 00162 } 00163 00164 return mesh; 00165 } 00166 00167 //////////////////////////////////////////////////////////////////// 00168 // Function: PhysxMeshPool::load_soft_body_mesh 00169 // Access: Published 00170 // Description: 00171 //////////////////////////////////////////////////////////////////// 00172 PhysxSoftBodyMesh *PhysxMeshPool:: 00173 load_soft_body_mesh(const Filename &fn) { 00174 00175 if (!check_filename(fn)) return NULL; 00176 00177 PhysxSoftBodyMesh *mesh; 00178 00179 SoftbodyMeshes::iterator it = _softbody_meshes.find(fn); 00180 if (it == _softbody_meshes.end()) { 00181 // Not found; load mesh. 00182 NxSoftBodyMesh *meshPtr; 00183 PhysxFileStream stream = PhysxFileStream(fn, true); 00184 00185 mesh = new PhysxSoftBodyMesh(); 00186 nassertr_always(mesh, NULL); 00187 00188 NxPhysicsSDK *sdk = NxGetPhysicsSDK(); 00189 nassertr_always(sdk, NULL); 00190 00191 meshPtr = sdk->createSoftBodyMesh(stream); 00192 nassertr_always(meshPtr, NULL); 00193 00194 mesh->link(meshPtr); 00195 00196 _softbody_meshes.insert(SoftbodyMeshes::value_type(fn, mesh)); 00197 } 00198 else { 00199 // Found; return previously loaded mesh. 00200 mesh = (*it).second; 00201 } 00202 00203 return mesh; 00204 } 00205 00206 //////////////////////////////////////////////////////////////////// 00207 // Function: PhysxMeshPool::release_convex_mesh 00208 // Access: Published 00209 // Description: 00210 //////////////////////////////////////////////////////////////////// 00211 bool PhysxMeshPool:: 00212 release_convex_mesh(PhysxConvexMesh *mesh) { 00213 00214 ConvexMeshes::iterator it; 00215 for (it=_convex_meshes.begin(); it != _convex_meshes.end(); ++it) { 00216 if (mesh == (*it).second) { 00217 _convex_meshes.erase(it); 00218 return true; 00219 } 00220 } 00221 00222 return false; 00223 } 00224 00225 //////////////////////////////////////////////////////////////////// 00226 // Function: PhysxMeshPool::release_triangle_mesh 00227 // Access: Published 00228 // Description: 00229 //////////////////////////////////////////////////////////////////// 00230 bool PhysxMeshPool:: 00231 release_triangle_mesh(PhysxTriangleMesh *mesh) { 00232 00233 TriangleMeshes::iterator it; 00234 for (it=_triangle_meshes.begin(); it != _triangle_meshes.end(); ++it) { 00235 if (mesh == (*it).second) { 00236 _triangle_meshes.erase(it); 00237 return true; 00238 } 00239 } 00240 00241 return false; 00242 } 00243 00244 //////////////////////////////////////////////////////////////////// 00245 // Function: PhysxMeshPool::release_cloth_mesh 00246 // Access: Published 00247 // Description: 00248 //////////////////////////////////////////////////////////////////// 00249 bool PhysxMeshPool:: 00250 release_cloth_mesh(PhysxClothMesh *mesh) { 00251 00252 ClothMeshes::iterator it; 00253 for (it=_cloth_meshes.begin(); it != _cloth_meshes.end(); ++it) { 00254 if (mesh == (*it).second) { 00255 _cloth_meshes.erase(it); 00256 return true; 00257 } 00258 } 00259 00260 return false; 00261 } 00262 00263 //////////////////////////////////////////////////////////////////// 00264 // Function: PhysxMeshPool::release_soft_body_mesh 00265 // Access: Published 00266 // Description: 00267 //////////////////////////////////////////////////////////////////// 00268 bool PhysxMeshPool:: 00269 release_soft_body_mesh(PhysxSoftBodyMesh *mesh) { 00270 00271 SoftbodyMeshes::iterator it; 00272 for (it=_softbody_meshes.begin(); it != _softbody_meshes.end(); ++it) { 00273 if (mesh == (*it).second) { 00274 _softbody_meshes.erase(it); 00275 return true; 00276 } 00277 } 00278 00279 return false; 00280 } 00281 00282 //////////////////////////////////////////////////////////////////// 00283 // Function: PhysxMeshPool::list_content 00284 // Access: Published 00285 // Description: 00286 //////////////////////////////////////////////////////////////////// 00287 void PhysxMeshPool:: 00288 list_contents() { 00289 list_contents(nout); 00290 } 00291 00292 //////////////////////////////////////////////////////////////////// 00293 // Function: PhysxMeshPool::list_content 00294 // Access: Published 00295 // Description: 00296 //////////////////////////////////////////////////////////////////// 00297 void PhysxMeshPool:: 00298 list_contents(ostream &out) { 00299 00300 out << "PhysX mesh pool contents:\n"; 00301 00302 // Convex meshes 00303 { 00304 ConvexMeshes::const_iterator it; 00305 for (it=_convex_meshes.begin(); it != _convex_meshes.end(); ++it) { 00306 Filename fn = (*it).first; 00307 PhysxConvexMesh *mesh = (*it).second; 00308 00309 out << " " << fn.get_fullpath() 00310 << " (convex mesh, " << mesh->ptr()->getReferenceCount() 00311 << " references)" << endl; 00312 } 00313 } 00314 00315 // Triangle meshes 00316 { 00317 TriangleMeshes::const_iterator it; 00318 for (it=_triangle_meshes.begin(); it != _triangle_meshes.end(); ++it) { 00319 Filename fn = (*it).first; 00320 PhysxTriangleMesh *mesh = (*it).second; 00321 00322 out << " " << fn.get_fullpath() 00323 << " (triangle mesh, " << mesh->ptr()->getReferenceCount() 00324 << " references)\n"; 00325 } 00326 } 00327 00328 // Cloth meshes 00329 { 00330 ClothMeshes::const_iterator it; 00331 for (it=_cloth_meshes.begin(); it != _cloth_meshes.end(); ++it) { 00332 Filename fn = (*it).first; 00333 PhysxClothMesh *mesh = (*it).second; 00334 00335 out << " " << fn.get_fullpath() 00336 << " (cloth mesh, " << mesh->ptr()->getReferenceCount() 00337 << " references)\n"; 00338 } 00339 } 00340 00341 // Soft body meshes 00342 { 00343 SoftbodyMeshes::const_iterator it; 00344 for (it=_softbody_meshes.begin(); it != _softbody_meshes.end(); ++it) { 00345 Filename fn = (*it).first; 00346 PhysxSoftBodyMesh *mesh = (*it).second; 00347 00348 out << " " << fn.get_fullpath() 00349 << " (soft body mesh, " << mesh->ptr()->getReferenceCount() 00350 << " references)\n"; 00351 } 00352 } 00353 00354 // Summary 00355 NxPhysicsSDK *sdk = NxGetPhysicsSDK(); 00356 00357 out << " Total number of convex meshes: " << sdk->getNbConvexMeshes() 00358 << " created, " << _convex_meshes.size() << " registred\n"; 00359 00360 out << " Total number of triangle meshes: " << sdk->getNbTriangleMeshes() 00361 << " created, " << _triangle_meshes.size() << " registred\n"; 00362 00363 out << " Total number of cloth meshes: " << sdk->getNbClothMeshes() 00364 << " created, " << _cloth_meshes.size() << " registred\n"; 00365 00366 out << " Total number of soft body meshes: " << sdk->getNbSoftBodyMeshes() 00367 << " created, " << _softbody_meshes.size() << " registred\n"; 00368 } 00369