Panda3D
|
00001 // Filename: physxKitchen.cxx 00002 // Created by: enn0x (12Oct09) 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 "physxKitchen.h" 00016 #include "physxConvexMesh.h" 00017 #include "physxConvexMeshDesc.h" 00018 #include "physxTriangleMesh.h" 00019 #include "physxTriangleMeshDesc.h" 00020 #include "physxFileStream.h" 00021 #include "physxMemoryReadBuffer.h" 00022 #include "physxMemoryWriteBuffer.h" 00023 #include "physxClothMesh.h" 00024 #include "physxClothMeshDesc.h" 00025 #include "physxSoftBodyMesh.h" 00026 #include "physxSoftBodyMeshDesc.h" 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: PhysxKitchen::set_cooking_params 00030 // Access: Published 00031 // Description: Sets two parameters which affect mesh cooking: 00032 // 00033 // Skin width for convex meshes: 00034 // Specifies the amount to inflate the convex mesh by 00035 // when the new convex hull generator is used. 00036 // Inflating the mesh allows the user to hide 00037 // interpenetration errors by increasing the size of 00038 // the collision mesh with respect to the size of the 00039 // rendered geometry. 00040 // Default value: 0.025f 00041 // 00042 // Hint to choose speed or less memory for collision 00043 // structures. 00044 // Default value: false 00045 //////////////////////////////////////////////////////////////////// 00046 void PhysxKitchen:: 00047 set_cooking_params(float skinWidth, bool hintCollisionSpeed) { 00048 00049 NxCookingParams params; 00050 00051 params.targetPlatform = PLATFORM_PC; 00052 params.skinWidth = skinWidth; 00053 params.hintCollisionSpeed = hintCollisionSpeed; 00054 00055 _cooking->NxSetCookingParams(params); 00056 } 00057 00058 //////////////////////////////////////////////////////////////////// 00059 // Function: PhysxKitchen::cook_convex_mesh 00060 // Access: Published 00061 // Description: 00062 //////////////////////////////////////////////////////////////////// 00063 bool PhysxKitchen:: 00064 cook_convex_mesh(const PhysxConvexMeshDesc &meshDesc, const Filename &filename) { 00065 00066 nassertr_always(!filename.empty(), false); 00067 nassertr_always(filename.touch(), false); 00068 nassertr_always(meshDesc.is_valid(), false); 00069 00070 PhysxFileStream fs = PhysxFileStream(filename, false); 00071 return _cooking->NxCookConvexMesh(meshDesc.get_desc(), fs); 00072 } 00073 00074 //////////////////////////////////////////////////////////////////// 00075 // Function: PhysxKitchen::cook_triangle_mesh 00076 // Access: Published 00077 // Description: 00078 //////////////////////////////////////////////////////////////////// 00079 bool PhysxKitchen:: 00080 cook_triangle_mesh(const PhysxTriangleMeshDesc &meshDesc, const Filename &filename) { 00081 00082 nassertr_always(!filename.empty(), false); 00083 nassertr_always(filename.touch(), false); 00084 nassertr_always(meshDesc.is_valid(), false); 00085 00086 PhysxFileStream fs = PhysxFileStream(filename, false); 00087 return _cooking->NxCookTriangleMesh(meshDesc.get_desc(), fs); 00088 } 00089 00090 //////////////////////////////////////////////////////////////////// 00091 // Function: PhysxKitchen::cook_cloth_mesh 00092 // Access: Published 00093 // Description: 00094 //////////////////////////////////////////////////////////////////// 00095 bool PhysxKitchen:: 00096 cook_cloth_mesh(const PhysxClothMeshDesc &meshDesc, const Filename &filename) { 00097 00098 nassertr_always(!filename.empty(), false); 00099 nassertr_always(filename.touch(), false); 00100 nassertr_always(meshDesc.is_valid(), false); 00101 00102 PhysxFileStream fs = PhysxFileStream(filename, false); 00103 return _cooking->NxCookClothMesh(meshDesc.get_desc(), fs); 00104 } 00105 00106 //////////////////////////////////////////////////////////////////// 00107 // Function: PhysxKitchen::cook_soft_body_mesh 00108 // Access: Published 00109 // Description: 00110 //////////////////////////////////////////////////////////////////// 00111 bool PhysxKitchen:: 00112 cook_soft_body_mesh(const PhysxSoftBodyMeshDesc &meshDesc, const Filename &filename) { 00113 00114 nassertr_always(!filename.empty(), false); 00115 nassertr_always(filename.touch(), false); 00116 nassertr_always(meshDesc.is_valid(), false); 00117 00118 PhysxFileStream fs = PhysxFileStream(filename, false); 00119 return _cooking->NxCookSoftBodyMesh(meshDesc.get_desc(), fs); 00120 } 00121 00122 //////////////////////////////////////////////////////////////////// 00123 // Function: PhysxKitchen::cook_texcoords 00124 // Access: Published 00125 // Description: 00126 //////////////////////////////////////////////////////////////////// 00127 bool PhysxKitchen:: 00128 cook_texcoords(const PhysxClothMeshDesc &meshDesc, const Filename &filename) { 00129 00130 nassertr_always(!filename.empty(), false); 00131 nassertr_always(filename.touch(), false); 00132 nassertr_always(meshDesc.is_valid(), false); 00133 00134 const plist<LPoint2f> texcoords = meshDesc.get_texcoords(); 00135 00136 // Write texcoords to binary file 00137 PhysxFileStream fs = PhysxFileStream(filename.c_str(), false); 00138 00139 // Header 00140 fs.storeByte('N'); 00141 fs.storeByte('X'); 00142 fs.storeByte('S'); 00143 fs.storeByte(1); 00144 fs.storeByte('T'); 00145 fs.storeByte('E'); 00146 fs.storeByte('X'); 00147 fs.storeByte('C'); 00148 fs.storeByte(1); 00149 00150 // Size 00151 fs.storeDword(texcoords.size()); 00152 00153 // Texcoords 00154 plist<LPoint2f>::const_iterator it; 00155 for(it=texcoords.begin(); it!=texcoords.end(); it++) { 00156 LPoint2f v = *it; 00157 00158 fs.storeFloat(v.get_x()); 00159 fs.storeFloat(v.get_y()); 00160 } 00161 00162 return true; 00163 } 00164 00165 //////////////////////////////////////////////////////////////////// 00166 // Function: PhysxKitchen::cook_convex_mesh 00167 // Access: Published 00168 // Description: 00169 //////////////////////////////////////////////////////////////////// 00170 PhysxConvexMesh *PhysxKitchen:: 00171 cook_convex_mesh(const PhysxConvexMeshDesc &meshDesc) { 00172 00173 nassertr_always(meshDesc.is_valid(), false); 00174 00175 PhysxMemoryWriteBuffer buffer; 00176 bool status = _cooking->NxCookConvexMesh(meshDesc.get_desc(), buffer); 00177 nassertr(status, NULL); 00178 00179 NxPhysicsSDK *sdk = NxGetPhysicsSDK(); 00180 nassertr(sdk, NULL); 00181 00182 PhysxConvexMesh *mesh = new PhysxConvexMesh(); 00183 nassertr(mesh, NULL); 00184 00185 NxConvexMesh *meshPtr = sdk->createConvexMesh(PhysxMemoryReadBuffer(buffer.data)); 00186 nassertr(meshPtr, NULL); 00187 00188 mesh->link(meshPtr); 00189 00190 return mesh; 00191 } 00192 00193 //////////////////////////////////////////////////////////////////// 00194 // Function: PhysxKitchen::cook_triangle_mesh 00195 // Access: Published 00196 // Description: 00197 //////////////////////////////////////////////////////////////////// 00198 PhysxTriangleMesh *PhysxKitchen:: 00199 cook_triangle_mesh(const PhysxTriangleMeshDesc &meshDesc) { 00200 00201 nassertr_always(meshDesc.is_valid(), false); 00202 00203 PhysxMemoryWriteBuffer buffer; 00204 bool status = _cooking->NxCookTriangleMesh(meshDesc.get_desc(), buffer); 00205 nassertr(status, NULL); 00206 00207 NxPhysicsSDK *sdk = NxGetPhysicsSDK(); 00208 nassertr(sdk, NULL); 00209 00210 PhysxTriangleMesh *mesh = new PhysxTriangleMesh(); 00211 nassertr(mesh, NULL); 00212 00213 NxTriangleMesh *meshPtr = sdk->createTriangleMesh(PhysxMemoryReadBuffer(buffer.data)); 00214 nassertr(meshPtr, NULL); 00215 00216 mesh->link(meshPtr); 00217 00218 return mesh; 00219 } 00220 00221 //////////////////////////////////////////////////////////////////// 00222 // Function: PhysxKitchen::cook_cloth_mesh 00223 // Access: Published 00224 // Description: 00225 //////////////////////////////////////////////////////////////////// 00226 PhysxClothMesh *PhysxKitchen:: 00227 cook_cloth_mesh(const PhysxClothMeshDesc &meshDesc) { 00228 00229 nassertr_always(meshDesc.is_valid(), false); 00230 00231 PhysxMemoryWriteBuffer wbuffer; 00232 bool status = _cooking->NxCookClothMesh(meshDesc.get_desc(), wbuffer); 00233 nassertr(status, NULL); 00234 00235 NxPhysicsSDK *sdk = NxGetPhysicsSDK(); 00236 nassertr(sdk, NULL); 00237 00238 PhysxClothMesh *mesh = new PhysxClothMesh(); 00239 nassertr(mesh, NULL); 00240 00241 PhysxMemoryReadBuffer rbuffer(wbuffer.data); 00242 NxClothMesh *meshPtr = sdk->createClothMesh(rbuffer); 00243 nassertr(meshPtr, NULL); 00244 00245 mesh->link(meshPtr); 00246 00247 return mesh; 00248 } 00249 00250 //////////////////////////////////////////////////////////////////// 00251 // Function: PhysxKitchen::cook_soft_body_mesh 00252 // Access: Published 00253 // Description: 00254 //////////////////////////////////////////////////////////////////// 00255 PhysxSoftBodyMesh *PhysxKitchen:: 00256 cook_soft_body_mesh(const PhysxSoftBodyMeshDesc &meshDesc) { 00257 00258 nassertr_always(meshDesc.is_valid(), false); 00259 00260 PhysxMemoryWriteBuffer wbuffer; 00261 bool status = _cooking->NxCookSoftBodyMesh(meshDesc.get_desc(), wbuffer); 00262 nassertr(status, NULL); 00263 00264 NxPhysicsSDK *sdk = NxGetPhysicsSDK(); 00265 nassertr(sdk, NULL); 00266 00267 PhysxSoftBodyMesh *mesh = new PhysxSoftBodyMesh(); 00268 nassertr(mesh, NULL); 00269 00270 PhysxMemoryReadBuffer rbuffer(wbuffer.data); 00271 NxSoftBodyMesh *meshPtr = sdk->createSoftBodyMesh(rbuffer); 00272 nassertr(meshPtr, NULL); 00273 00274 mesh->link(meshPtr); 00275 00276 return mesh; 00277 } 00278