Panda3D
Loading...
Searching...
No Matches
physxMeshPool.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file physxMeshPool.cxx
10 * @author enn0x
11 * @date 2009-10-14
12 */
13
14#include "physxMeshPool.h"
15#include "physxConvexMesh.h"
16#include "physxTriangleMesh.h"
17#include "physxClothMesh.h"
18#include "physxSoftBodyMesh.h"
19
20#include "physxFileStream.h"
21#include "virtualFileSystem.h"
22
23PhysxMeshPool::ConvexMeshes PhysxMeshPool::_convex_meshes;
24PhysxMeshPool::TriangleMeshes PhysxMeshPool::_triangle_meshes;
25PhysxMeshPool::ClothMeshes PhysxMeshPool::_cloth_meshes;
26PhysxMeshPool::SoftbodyMeshes PhysxMeshPool::_softbody_meshes;
27
28/**
29 *
30 */
31bool PhysxMeshPool::
32check_filename(const Filename &fn) {
33
34 if (!(VirtualFileSystem::get_global_ptr()->exists(fn))) {
35 physx_cat.error() << "File does not exists: " << fn << std::endl;
36 return false;
37 }
38
39 if (!(VirtualFileSystem::get_global_ptr()->is_regular_file(fn))) {
40 physx_cat.error() << "Not a regular file: " << fn << std::endl;
41 return false;
42 }
43
44 return true;
45}
46
47/**
48 *
49 */
50PhysxConvexMesh *PhysxMeshPool::
51load_convex_mesh(const Filename &fn) {
52
53 if (!check_filename(fn)) return nullptr;
54
55 PhysxConvexMesh *mesh;
56
57 ConvexMeshes::iterator it = _convex_meshes.find(fn);
58 if (it == _convex_meshes.end()) {
59 // Not found; load mesh.
60 NxConvexMesh *meshPtr;
61 PhysxFileStream stream = PhysxFileStream(fn, true);
62
63 mesh = new PhysxConvexMesh();
64 nassertr_always(mesh, nullptr);
65
66 NxPhysicsSDK *sdk = NxGetPhysicsSDK();
67 nassertr_always(sdk, nullptr);
68
69 meshPtr = sdk->createConvexMesh(stream);
70 nassertr_always(meshPtr, nullptr);
71
72 mesh->link(meshPtr);
73
74 _convex_meshes.insert(ConvexMeshes::value_type(fn, mesh));
75 }
76 else {
77 // Found; return previously loaded mesh.
78 mesh = (*it).second;
79 }
80
81 return mesh;
82}
83
84/**
85 *
86 */
87PhysxTriangleMesh *PhysxMeshPool::
88load_triangle_mesh(const Filename &fn) {
89
90 if (!check_filename(fn)) return nullptr;
91
93
94 TriangleMeshes::iterator it = _triangle_meshes.find(fn);
95 if (it == _triangle_meshes.end()) {
96 // Not found; load mesh.
97 NxTriangleMesh *meshPtr;
98 PhysxFileStream stream = PhysxFileStream(fn, true);
99
100 mesh = new PhysxTriangleMesh();
101 nassertr_always(mesh, nullptr);
102
103 NxPhysicsSDK *sdk = NxGetPhysicsSDK();
104 nassertr_always(sdk, nullptr);
105
106 meshPtr = sdk->createTriangleMesh(stream);
107 nassertr_always(meshPtr, nullptr);
108
109 mesh->link(meshPtr);
110
111 _triangle_meshes.insert(TriangleMeshes::value_type(fn, mesh));
112 }
113 else {
114 // Found; return previously loaded mesh.
115 mesh = (*it).second;
116 }
117
118 return mesh;
119}
120
121/**
122 *
123 */
124PhysxClothMesh *PhysxMeshPool::
125load_cloth_mesh(const Filename &fn) {
126
127 if (!check_filename(fn)) return nullptr;
128
129 PhysxClothMesh *mesh;
130
131 ClothMeshes::iterator it = _cloth_meshes.find(fn);
132 if (it == _cloth_meshes.end()) {
133 // Not found; load mesh.
134 NxClothMesh *meshPtr;
135 PhysxFileStream stream = PhysxFileStream(fn, true);
136
137 mesh = new PhysxClothMesh();
138 nassertr_always(mesh, nullptr);
139
140 NxPhysicsSDK *sdk = NxGetPhysicsSDK();
141 nassertr_always(sdk, nullptr);
142
143 meshPtr = sdk->createClothMesh(stream);
144 nassertr_always(meshPtr, nullptr);
145
146 mesh->link(meshPtr);
147
148 _cloth_meshes.insert(ClothMeshes::value_type(fn, mesh));
149 }
150 else {
151 // Found; return previously loaded mesh.
152 mesh = (*it).second;
153 }
154
155 return mesh;
156}
157
158/**
159 *
160 */
161PhysxSoftBodyMesh *PhysxMeshPool::
162load_soft_body_mesh(const Filename &fn) {
163
164 if (!check_filename(fn)) return nullptr;
165
166 PhysxSoftBodyMesh *mesh;
167
168 SoftbodyMeshes::iterator it = _softbody_meshes.find(fn);
169 if (it == _softbody_meshes.end()) {
170 // Not found; load mesh.
171 NxSoftBodyMesh *meshPtr;
172 PhysxFileStream stream = PhysxFileStream(fn, true);
173
174 mesh = new PhysxSoftBodyMesh();
175 nassertr_always(mesh, nullptr);
176
177 NxPhysicsSDK *sdk = NxGetPhysicsSDK();
178 nassertr_always(sdk, nullptr);
179
180 meshPtr = sdk->createSoftBodyMesh(stream);
181 nassertr_always(meshPtr, nullptr);
182
183 mesh->link(meshPtr);
184
185 _softbody_meshes.insert(SoftbodyMeshes::value_type(fn, mesh));
186 }
187 else {
188 // Found; return previously loaded mesh.
189 mesh = (*it).second;
190 }
191
192 return mesh;
193}
194
195/**
196 *
197 */
198bool PhysxMeshPool::
199release_convex_mesh(PhysxConvexMesh *mesh) {
200
201 ConvexMeshes::iterator it;
202 for (it=_convex_meshes.begin(); it != _convex_meshes.end(); ++it) {
203 if (mesh == (*it).second) {
204 _convex_meshes.erase(it);
205 return true;
206 }
207 }
208
209 return false;
210}
211
212/**
213 *
214 */
215bool PhysxMeshPool::
216release_triangle_mesh(PhysxTriangleMesh *mesh) {
217
218 TriangleMeshes::iterator it;
219 for (it=_triangle_meshes.begin(); it != _triangle_meshes.end(); ++it) {
220 if (mesh == (*it).second) {
221 _triangle_meshes.erase(it);
222 return true;
223 }
224 }
225
226 return false;
227}
228
229/**
230 *
231 */
232bool PhysxMeshPool::
233release_cloth_mesh(PhysxClothMesh *mesh) {
234
235 ClothMeshes::iterator it;
236 for (it=_cloth_meshes.begin(); it != _cloth_meshes.end(); ++it) {
237 if (mesh == (*it).second) {
238 _cloth_meshes.erase(it);
239 return true;
240 }
241 }
242
243 return false;
244}
245
246/**
247 *
248 */
249bool PhysxMeshPool::
250release_soft_body_mesh(PhysxSoftBodyMesh *mesh) {
251
252 SoftbodyMeshes::iterator it;
253 for (it=_softbody_meshes.begin(); it != _softbody_meshes.end(); ++it) {
254 if (mesh == (*it).second) {
255 _softbody_meshes.erase(it);
256 return true;
257 }
258 }
259
260 return false;
261}
262
263/**
264 *
265 */
266void PhysxMeshPool::
267list_contents() {
268 list_contents(nout);
269}
270
271/**
272 *
273 */
274void PhysxMeshPool::
275list_contents(std::ostream &out) {
276
277 out << "PhysX mesh pool contents:\n";
278
279 // Convex meshes
280 {
281 ConvexMeshes::const_iterator it;
282 for (it=_convex_meshes.begin(); it != _convex_meshes.end(); ++it) {
283 Filename fn = (*it).first;
284 PhysxConvexMesh *mesh = (*it).second;
285
286 out << " " << fn.get_fullpath()
287 << " (convex mesh, " << mesh->ptr()->getReferenceCount()
288 << " references)" << std::endl;
289 }
290 }
291
292 // Triangle meshes
293 {
294 TriangleMeshes::const_iterator it;
295 for (it=_triangle_meshes.begin(); it != _triangle_meshes.end(); ++it) {
296 Filename fn = (*it).first;
297 PhysxTriangleMesh *mesh = (*it).second;
298
299 out << " " << fn.get_fullpath()
300 << " (triangle mesh, " << mesh->ptr()->getReferenceCount()
301 << " references)\n";
302 }
303 }
304
305 // Cloth meshes
306 {
307 ClothMeshes::const_iterator it;
308 for (it=_cloth_meshes.begin(); it != _cloth_meshes.end(); ++it) {
309 Filename fn = (*it).first;
310 PhysxClothMesh *mesh = (*it).second;
311
312 out << " " << fn.get_fullpath()
313 << " (cloth mesh, " << mesh->ptr()->getReferenceCount()
314 << " references)\n";
315 }
316 }
317
318 // Soft body meshes
319 {
320 SoftbodyMeshes::const_iterator it;
321 for (it=_softbody_meshes.begin(); it != _softbody_meshes.end(); ++it) {
322 Filename fn = (*it).first;
323 PhysxSoftBodyMesh *mesh = (*it).second;
324
325 out << " " << fn.get_fullpath()
326 << " (soft body mesh, " << mesh->ptr()->getReferenceCount()
327 << " references)\n";
328 }
329 }
330
331 // Summary
332 NxPhysicsSDK *sdk = NxGetPhysicsSDK();
333
334 out << " Total number of convex meshes: " << sdk->getNbConvexMeshes()
335 << " created, " << _convex_meshes.size() << " registred\n";
336
337 out << " Total number of triangle meshes: " << sdk->getNbTriangleMeshes()
338 << " created, " << _triangle_meshes.size() << " registred\n";
339
340 out << " Total number of cloth meshes: " << sdk->getNbClothMeshes()
341 << " created, " << _cloth_meshes.size() << " registred\n";
342
343 out << " Total number of soft body meshes: " << sdk->getNbSoftBodyMeshes()
344 << " created, " << _softbody_meshes.size() << " registred\n";
345}
The name of a file, such as a texture file or an Egg file.
Definition filename.h:44
std::string get_fullpath() const
Returns the entire filename: directory, basename, extension.
Definition filename.I:338
A Convex Mesh.
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
This is our own Panda specialization on the default STL map.
Definition pmap.h:49
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.