From: The physx SDK. chm/GUIDE/collision detection/meshes/cooking
To create a triangle mesh, you must first cook it into a fixed format. in this format, the SDK can efficiently perform conflict detection. The cooking process may be very slow. Therefore, the cooking operation is usually performed offline only once. After the process is completed, generated stream of the result data is saved as a document. The next time you load it, you only need to load it.
Use PPU for Cook. There are also some precautions. For details, see the mesh paging section. Practical Program Use the nxcooking library cook mesh. First, you must call the following interface to initialize an nxcooking library object:
Static Nxcookinginterface * Gcooking = Nxgetcookinglib (nx_physx_sdk_version );
Gcooking -> Nxinitcooking (); Then, use
Nxtrianglemeshdesc The object describes a grid and passes it as a parameter to the function.
Nxcooktrianglemesh () , This function will process the mesh description and use the stream --
Nxstream Physx requires the user to implement a read/write operation interface-then stream can write it into the file or memory. // Build physical model
Nxtrianglemeshdesc bunnydesc;
Bunnydesc. numvertices = Bunny_nbvertices;
Bunnydesc. numtriangles = Bunny_nbfaces;
Bunnydesc. pointstridebytes = Sizeof (Nxvec3 );
Bunnydesc. trianglestridebytes = 3 * Sizeof (Nxu32 );
Bunnydesc. Points = Gbunnyvertices;
Bunnydesc. triangles = Gbunnytriangles;
Bunnydesc. flags = 0 ;
Gcooking -> Nxcooktrianglemesh (bunnydesc, userstream ( " C: \ cooked. Bin " , False )); Next, when the application needs to create a triangular mesh, load the mesh data stored by stream using the following function. Bunnytrianglemesh = Gphysicssdk -> Createtrianglemesh (userstream ( " C: \ TMP. Bin " , True ));
Nxcookconvexmesh () /
Nxcookclothmesh () And
Nxcooktrianglemesh () The usage is the same. Note: : The cooking operation is greatly affected by the precision, and relies heavily on the FPU mode of the floating point operation unit. Cross-platform, ensure that the precision of FPU is consistent with the rounding method. In the preceding example, the flag Member is not set. Flag can be used to set some special flag. For example
- Nx_mf_flipnormals-Reverse Order of the three vertices in the index list (counter-clockwise by default), that is, reverse normal
- Nx_mf_16_bit_indices-The Data Length of the vertex index is 16 bits instead of 32 bits.
- Nx_mf_convex-Allows the mesh to be viewed as convex and automatically optimized
The normal line of the triangle surface is not explicitly described. It can calculate (V1-V0) x (v2-V0) v0, V1, and v2 as three vertices of the triangle. The counter-clockwise (right-hand rule right handed coordinate system) is clockwise. Algorithm They are all the same.
Grid considerations:
- Ensure the orientation of the normal. Conflict Detection only correctly calculates the shape from the front.
- Do not repeat the vertex. If the two triangles share one vertex, This vertex can only appear once in the vertex list, but is indexed twice in the index list. Because duplicate vertices are two vertices for physx, which may cause conflict detection errors.
- Do not use t-joint or non-manifold edge. (Because both of these two items require the sharing points in the triangle to be regarded as two points .)
- Once the mesh Cook is complete, both the mass and inertia tensor are calculated.
- The materials of different triangles in the same mesh can be different. For details, see the material chapter.
Then you can use mesh to create a shape and actor. The key is to set the meshdata of the triangle mesh shape description to the mesh after the previous cooking. Nxtrianglemeshshapedesc bunnyshapedesc;
Bunnyshapedesc. meshdata = Bunnytrianglemesh;
Nxbodydesc bodydesc;
Nxactordesc actordesc;
Actordesc. shapes. pushback ( & Bunnyshapedesc );
Actordesc. Body = & Bodydesc;
Actordesc. Density = 1.0f ;
Nxactor * Newactor = Gscene -> Createactor (actordesc );