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