Panda3D
physxKitchen.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 physxKitchen.cxx
10  * @author enn0x
11  * @date 2009-10-12
12  */
13 
14 #include "physxKitchen.h"
15 #include "physxConvexMesh.h"
16 #include "physxConvexMeshDesc.h"
17 #include "physxTriangleMesh.h"
18 #include "physxTriangleMeshDesc.h"
19 #include "physxFileStream.h"
20 #include "physxMemoryReadBuffer.h"
21 #include "physxMemoryWriteBuffer.h"
22 #include "physxClothMesh.h"
23 #include "physxClothMeshDesc.h"
24 #include "physxSoftBodyMesh.h"
25 #include "physxSoftBodyMeshDesc.h"
26 
27 /**
28  * Sets two parameters which affect mesh cooking:
29  *
30  * Skin width for convex meshes: Specifies the amount to inflate the convex
31  * mesh by when the new convex hull generator is used. Inflating the mesh
32  * allows the user to hide interpenetration errors by increasing the size of
33  * the collision mesh with respect to the size of the rendered geometry.
34  * Default value: 0.025f
35  *
36  * Hint to choose speed or less memory for collision structures. Default
37  * value: false
38  */
39 void PhysxKitchen::
40 set_cooking_params(float skinWidth, bool hintCollisionSpeed) {
41 
42  NxCookingParams params;
43 
44  params.targetPlatform = PLATFORM_PC;
45  params.skinWidth = skinWidth;
46  params.hintCollisionSpeed = hintCollisionSpeed;
47 
48  _cooking->NxSetCookingParams(params);
49 }
50 
51 /**
52  *
53  */
54 bool PhysxKitchen::
55 cook_convex_mesh(const PhysxConvexMeshDesc &meshDesc, const Filename &filename) {
56 
57  nassertr_always(!filename.empty(), false);
58  nassertr_always(filename.touch(), false);
59  nassertr_always(meshDesc.is_valid(), false);
60 
61  PhysxFileStream fs = PhysxFileStream(filename, false);
62  return _cooking->NxCookConvexMesh(meshDesc.get_desc(), fs);
63 }
64 
65 /**
66  *
67  */
68 bool PhysxKitchen::
69 cook_triangle_mesh(const PhysxTriangleMeshDesc &meshDesc, const Filename &filename) {
70 
71  nassertr_always(!filename.empty(), false);
72  nassertr_always(filename.touch(), false);
73  nassertr_always(meshDesc.is_valid(), false);
74 
75  PhysxFileStream fs = PhysxFileStream(filename, false);
76  return _cooking->NxCookTriangleMesh(meshDesc.get_desc(), fs);
77 }
78 
79 /**
80  *
81  */
82 bool PhysxKitchen::
83 cook_cloth_mesh(const PhysxClothMeshDesc &meshDesc, const Filename &filename) {
84 
85  nassertr_always(!filename.empty(), false);
86  nassertr_always(filename.touch(), false);
87  nassertr_always(meshDesc.is_valid(), false);
88 
89  PhysxFileStream fs = PhysxFileStream(filename, false);
90  return _cooking->NxCookClothMesh(meshDesc.get_desc(), fs);
91 }
92 
93 /**
94  *
95  */
96 bool PhysxKitchen::
97 cook_soft_body_mesh(const PhysxSoftBodyMeshDesc &meshDesc, const Filename &filename) {
98 
99  nassertr_always(!filename.empty(), false);
100  nassertr_always(filename.touch(), false);
101  nassertr_always(meshDesc.is_valid(), false);
102 
103  PhysxFileStream fs = PhysxFileStream(filename, false);
104  return _cooking->NxCookSoftBodyMesh(meshDesc.get_desc(), fs);
105 }
106 
107 /**
108  *
109  */
110 bool PhysxKitchen::
111 cook_texcoords(const PhysxClothMeshDesc &meshDesc, const Filename &filename) {
112 
113  nassertr_always(!filename.empty(), false);
114  nassertr_always(filename.touch(), false);
115  nassertr_always(meshDesc.is_valid(), false);
116 
117  const plist<LPoint2f> texcoords = meshDesc.get_texcoords();
118 
119  // Write texcoords to binary file
120  PhysxFileStream fs = PhysxFileStream(filename.c_str(), false);
121 
122  // Header
123  fs.storeByte('N');
124  fs.storeByte('X');
125  fs.storeByte('S');
126  fs.storeByte(1);
127  fs.storeByte('T');
128  fs.storeByte('E');
129  fs.storeByte('X');
130  fs.storeByte('C');
131  fs.storeByte(1);
132 
133  // Size
134  fs.storeDword(texcoords.size());
135 
136  // Texcoords
137  plist<LPoint2f>::const_iterator it;
138  for(it=texcoords.begin(); it!=texcoords.end(); it++) {
139  LPoint2f v = *it;
140 
141  fs.storeFloat(v.get_x());
142  fs.storeFloat(v.get_y());
143  }
144 
145  return true;
146 }
147 
148 /**
149  *
150  */
151 PhysxConvexMesh *PhysxKitchen::
152 cook_convex_mesh(const PhysxConvexMeshDesc &meshDesc) {
153 
154  nassertr_always(meshDesc.is_valid(), nullptr);
155 
156  PhysxMemoryWriteBuffer buffer;
157  bool status = _cooking->NxCookConvexMesh(meshDesc.get_desc(), buffer);
158  nassertr(status, nullptr);
159 
160  NxPhysicsSDK *sdk = NxGetPhysicsSDK();
161  nassertr(sdk, nullptr);
162 
163  PhysxConvexMesh *mesh = new PhysxConvexMesh();
164  nassertr(mesh, nullptr);
165 
166  NxConvexMesh *meshPtr = sdk->createConvexMesh(PhysxMemoryReadBuffer(buffer.data));
167  nassertr(meshPtr, nullptr);
168 
169  mesh->link(meshPtr);
170 
171  return mesh;
172 }
173 
174 /**
175  *
176  */
177 PhysxTriangleMesh *PhysxKitchen::
178 cook_triangle_mesh(const PhysxTriangleMeshDesc &meshDesc) {
179 
180  nassertr_always(meshDesc.is_valid(), nullptr);
181 
182  PhysxMemoryWriteBuffer buffer;
183  bool status = _cooking->NxCookTriangleMesh(meshDesc.get_desc(), buffer);
184  nassertr(status, nullptr);
185 
186  NxPhysicsSDK *sdk = NxGetPhysicsSDK();
187  nassertr(sdk, nullptr);
188 
189  PhysxTriangleMesh *mesh = new PhysxTriangleMesh();
190  nassertr(mesh, nullptr);
191 
192  NxTriangleMesh *meshPtr = sdk->createTriangleMesh(PhysxMemoryReadBuffer(buffer.data));
193  nassertr(meshPtr, nullptr);
194 
195  mesh->link(meshPtr);
196 
197  return mesh;
198 }
199 
200 /**
201  *
202  */
203 PhysxClothMesh *PhysxKitchen::
204 cook_cloth_mesh(const PhysxClothMeshDesc &meshDesc) {
205 
206  nassertr_always(meshDesc.is_valid(), nullptr);
207 
208  PhysxMemoryWriteBuffer wbuffer;
209  bool status = _cooking->NxCookClothMesh(meshDesc.get_desc(), wbuffer);
210  nassertr(status, nullptr);
211 
212  NxPhysicsSDK *sdk = NxGetPhysicsSDK();
213  nassertr(sdk, nullptr);
214 
215  PhysxClothMesh *mesh = new PhysxClothMesh();
216  nassertr(mesh, nullptr);
217 
218  PhysxMemoryReadBuffer rbuffer(wbuffer.data);
219  NxClothMesh *meshPtr = sdk->createClothMesh(rbuffer);
220  nassertr(meshPtr, nullptr);
221 
222  mesh->link(meshPtr);
223 
224  return mesh;
225 }
226 
227 /**
228  *
229  */
230 PhysxSoftBodyMesh *PhysxKitchen::
231 cook_soft_body_mesh(const PhysxSoftBodyMeshDesc &meshDesc) {
232 
233  nassertr_always(meshDesc.is_valid(), nullptr);
234 
235  PhysxMemoryWriteBuffer wbuffer;
236  bool status = _cooking->NxCookSoftBodyMesh(meshDesc.get_desc(), wbuffer);
237  nassertr(status, nullptr);
238 
239  NxPhysicsSDK *sdk = NxGetPhysicsSDK();
240  nassertr(sdk, nullptr);
241 
242  PhysxSoftBodyMesh *mesh = new PhysxSoftBodyMesh();
243  nassertr(mesh, nullptr);
244 
245  PhysxMemoryReadBuffer rbuffer(wbuffer.data);
246  NxSoftBodyMesh *meshPtr = sdk->createSoftBodyMesh(rbuffer);
247  nassertr(meshPtr, nullptr);
248 
249  mesh->link(meshPtr);
250 
251  return mesh;
252 }
bool is_valid() const
Returns true if the descriptor is valid.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL list.
Definition: plist.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool touch() const
Updates the modification time of the file to the current time.
Definition: filename.cxx:2225
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
bool is_valid() const
Returns true if the descriptor is valid.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A Convex Mesh.
void set_cooking_params(float skinWidth, bool hintCollisionSpeed)
Sets two parameters which affect mesh cooking:
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.
bool is_valid() const
Returns true if the descriptor is valid.
bool is_valid() const
Returns true if the descriptor is valid.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.