The results of physx fabric simulation are stored in two important buffers: vertex buffer and index buffer, rendering can be easily implemented in the graphics library, both DirectX and OpenGL are relatively easy. Here we will introduce the implementation methods in the open-source graphics engine ogre.
Manualobject can be used to manually specify the vertex and index buffer of the rendered object. This is exactly what we want. The specific code is as follows.
Void testcloth: render ()
{
Static nxu32 numvertices = mnumvertices;
Nxu32 numelements = mnumindices;
Numvertices = mnumvertices;
// Clears the last buffer before rendering.
Mmanualobj-> clear ();
Mmanualobj-> begin ("clothmaterial", ogre: renderoperation: ot_triangle_list );
// Copy vertex data first
For (nxu32 I = 0; I <numvertices; I ++)
{
Ogre: vector3 Pos = ogre: vector3 (mvertexrenderbuffer [I]. position. X,
Mvertexrenderbuffer [I]. position. y,
Mvertexrenderbuffer [I]. position. z );
Mmanualobj-> position (POS );
Ogre: vector3 normal = ogre: vector3 (mvertexrenderbuffer [I]. Normal. X,
Mvertexrenderbuffer [I]. Normal. y,
Mvertexrenderbuffer [I]. Normal. z );
Mmanualobj-> normal (normal );
Mmanualobj-> texturecoord (mvertexrenderbuffer [I]. texcoord [0],
Mvertexrenderbuffer [I]. texcoord [1]);
}
// Then copy the index data
For (nxu32 J = 0; j <mtricount; j ++)
{
Mmanualobj-> triangle (mindexrenderbuffer [J * 3],
Mindexrenderbuffer [J * 3 + 1], mindexrenderbuffer [J * 3 + 2]);
}
Mmanualobj-> end ();
}
Testcloth class, modified from the sample code of physx SDK, but replaced the OpenGL rendering code, the other is basically not dynamic.
In the code, mvertexrenderbuffer and mindexrenderbuffer are the vertex and index results returned by the physx SDK after each simulation.
Below is
Source code can be downloaded here