NewtonMesh* CreateQuadClothPatch(DemoEntityManager* const scene, int size_x, int size_z) { size_x += 1; size_z += 1; dAssert(size_x <= 129); dAssert(size_z <= 129); dFloat dimension = 0.125f; dBigVector* const points = new dBigVector[size_x * size_z]; int* const faceIndexCount = new int[(size_x - 1) * (size_z - 1)]; int* const faceVertexIndex = new int[4 * (size_x - 1) * (size_z - 1)]; dFloat y = 0.0f; int vertexCount = 0; for (int i = 0; i < size_z; i++) { dFloat z = (i - size_z / 2) * dimension; for (int j = 0; j < size_x; j++) { dFloat x = (j - size_x / 2) * dimension; points[vertexCount] = dVector(x, y, z, 0.0f); vertexCount++; } } int faceCount = 0; for (int i = 0; i < size_z - 1; i++) { for (int j = 0; j < size_x - 1; j++) { faceIndexCount[faceCount] = 4; faceVertexIndex[faceCount * 4 + 0] = (i + 0) * size_x + j + 0; faceVertexIndex[faceCount * 4 + 1] = (i + 0) * size_x + j + 1; faceVertexIndex[faceCount * 4 + 2] = (i + 1) * size_x + j + 1; faceVertexIndex[faceCount * 4 + 3] = (i + 1) * size_x + j + 0; faceCount++; } } dMatrix aligmentUV(dGetIdentityMatrix()); NewtonMeshVertexFormat vertexFormat; NewtonMeshClearVertexFormat(&vertexFormat); vertexFormat.m_faceCount = faceCount; vertexFormat.m_faceIndexCount = faceIndexCount; vertexFormat.m_vertex.m_data = &points[0][0]; vertexFormat.m_vertex.m_indexList = faceVertexIndex; vertexFormat.m_vertex.m_strideInBytes = sizeof(dBigVector); NewtonMesh* const clothPatch = NewtonMeshCreate(scene->GetNewton()); NewtonMeshBuildFromVertexListIndexList(clothPatch, &vertexFormat); int material = LoadTexture("persianRug.tga"); NewtonMeshApplyBoxMapping(clothPatch, material, material, material, &aligmentUV[0][0]); delete[] points; delete[] faceIndexCount; delete[] faceVertexIndex; return clothPatch; }
/* void dMeshNodeInfo::BuildFromVertexListIndexList(int faceCount, const int* const faceIndexCount, const int* faceMaterialIndex, const dFloat* const vertex, int vertexStrideInBytes, const int* vertexIndex, const dFloat* const normal, int normalStrideInBytes, const int* normalIndex, const dFloat* const uv0, int uv0StrideInBytes, const int* uv0Index, const dFloat* const uv1, int uv1StrideInBytes, const int* uv1Index) */ void dMeshNodeInfo::BuildFromVertexListIndexList(const NewtonMeshVertexFormat* const format) { /* NewtonMeshBuildFromPointListIndexList(m_mesh, faceCount, faceIndexCount, faceMaterialIndex, vertex, vertexStrideInBytes, vertexIndex,normal, normalStrideInBytes, normalIndex, uv0, uv0StrideInBytes, uv0Index, uv1, uv1StrideInBytes, uv1Index); */ NewtonMeshBuildFromVertexListIndexList (m_mesh, format); }
void dMeshNodeInfo::BuildFromVertexListIndexList(int faceCount, const int* const faceIndexCount, const int* faceMaterialIndex, const dFloat* const vertex, int vertexStrideInBytes, const int* vertexIndex, const dFloat* const normal, int normalStrideInBytes, const int* normalIndex, const dFloat* const uv0, int uv0StrideInBytes, const int* uv0Index, const dFloat* const uv1, int uv1StrideInBytes, const int* uv1Index) { NewtonMeshBuildFromVertexListIndexList(m_mesh, faceCount, faceIndexCount, faceMaterialIndex, vertex, vertexStrideInBytes, vertexIndex,normal, normalStrideInBytes, normalIndex, uv0, uv0StrideInBytes, uv0Index, uv1, uv1StrideInBytes, uv1Index); }
// create a mesh using the NewtonMesh low lever interface static NewtonBody* CreateSimpleBox_NewtonMesh (DemoEntityManager* const scene, const dVector& origin, const dVector& scale, dFloat mass) { dBigVector array[8]; dBigVector scale1 (scale); for (int i = 0; i < 8; i ++) { dBigVector p(&BoxPoints[i * 4]); array[i] = scale1 * p; } NewtonMeshVertexFormat vertexFormat; NewtonMeshClearVertexFormat(&vertexFormat); vertexFormat.m_faceCount = 10; vertexFormat.m_faceIndexCount = faceIndexList; vertexFormat.m_faceMaterial = faceMateriaIndexList; vertexFormat.m_vertex.m_data = &array[0][0]; vertexFormat.m_vertex.m_indexList = BoxIndices; vertexFormat.m_vertex.m_strideInBytes = sizeof (dBigVector); vertexFormat.m_normal.m_data = normal; vertexFormat.m_normal.m_indexList = faceNormalIndex; vertexFormat.m_normal.m_strideInBytes = 3 * sizeof (dFloat); // all channel are now optionals so we not longer has to pass default values // vertexFormat.m_uv0.m_data = uv0; // vertexFormat.m_uv0.m_indexList = uv0_indexList; // vertexFormat.m_uv0.m_strideInBytes = 2 * sizeof (dFloat); // now we create and empty mesh NewtonMesh* const newtonMesh = NewtonMeshCreate(scene->GetNewton()); NewtonMeshBuildFromVertexListIndexList(newtonMesh, &vertexFormat); // now we can use this mesh for lot of stuff, we can apply UV, we can decompose into convex, NewtonCollision* const collision = NewtonCreateConvexHullFromMesh(scene->GetNewton(), newtonMesh, 0.001f, 0); // for now we will simple make simple Box, make a visual Mesh DemoMesh* const visualMesh = new DemoMesh (newtonMesh); dMatrix matrix (dGetIdentityMatrix()); matrix.m_posit = origin; matrix.m_posit.m_w = 1.0f; NewtonBody* const body = CreateSimpleSolid(scene, visualMesh, mass, matrix, collision, 0); dVector veloc(1, 0, 2, 0); NewtonBodySetVelocity(body, &veloc[0]); visualMesh->Release(); NewtonDestroyCollision(collision); NewtonMeshDestroy (newtonMesh); return body; }
static NewtonBody* CreateSimpleNewtonMeshBox (DemoEntityManager* const scene, const dVector& origin, const dVector& scale, dFloat mass) { // the vertex array, vertices's has for values, x, y, z, w // w is use as a id to have multiple copy of the same very, like for example mesh that share more than two edges. // in most case w can be set to 0.0 static dFloat64 BoxPoints[] = { -1.0, -1.0, -1.0, 0.0, -1.0, -1.0, 1.0, 0.0, -1.0, 1.0, 1.0, 0.0, -1.0, 1.0, -1.0, 0.0, 1.0, -1.0, -1.0, 0.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, -1.0, 0.0, }; // the vertex index list is an array of all the face, in any order, the can be convex or concave, // and has and variable umber of indices static int BoxIndices[] = { 2,3,0,1, // this is quad 5,2,1, // triangle 6,2,5, // another triangle 5,1,0,4, // another quad 2,7,3, // and so on 6,7,2, 3,4,0, 7,4,3, 7,5,4, 6,5,7 }; // the number of index for each face is specified by an array of consecutive face index static int faceIndexList [] = {4, 3, 3, 4, 3, 3, 3, 3, 3, 3}; // each face can have an arbitrary index that the application can use as a material index // for example the index point to a texture, we can have the each face of the cube with a different texture static int faceMateriaIndexList [] = {0, 4, 4, 2, 3, 3, 3, 3, 3, 3}; // the normal is specified per vertex and each vertex can have a unique normal or a duplicated // for example a cube has 6 normals static dFloat normal[] = { 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, -1.0, }; static int faceNormalIndex [] = { 0, 0, 0, 0, // first face uses the first normal of each vertex 3, 3, 3, // second face uses the third normal 3, 3, 3, // third face uses the fifth normal 1, 1, 1, 1, // third face use the second normal 2, 2, 2, // and so on 2, 2, 2, 4, 2, 1, // a face can have per vertex normals 4, 4, 4, 5, 5, 5, // two coplanar face can even has different normals 3, 2, 0, }; /* // the UV are encode the same way as the vertex an the normals, a UV list and an index list // since we do not have UV we can assign the all to zero static dFloat uv0[] = { 0, 0}; static int uv0_indexList [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; */ dBigVector array[8]; dBigVector scale1 (scale); for (int i = 0; i < 8; i ++) { dBigVector p(&BoxPoints[i * 4]); array[i] = scale1 * p; } NewtonMeshVertexFormat vertexFormat; NewtonMeshClearVertexFormat(&vertexFormat); vertexFormat.m_faceCount = 10; vertexFormat.m_faceIndexCount = faceIndexList; vertexFormat.m_faceMaterial = faceMateriaIndexList; vertexFormat.m_vertex.m_data = &array[0][0]; vertexFormat.m_vertex.m_indexList = BoxIndices; vertexFormat.m_vertex.m_strideInBytes = sizeof (dBigVector); vertexFormat.m_normal.m_data = normal; vertexFormat.m_normal.m_indexList = faceNormalIndex; vertexFormat.m_normal.m_strideInBytes = 3 * sizeof (dFloat); // all channel are now optionals so we not longer has to pass default values // vertexFormat.m_uv0.m_data = uv0; // vertexFormat.m_uv0.m_indexList = uv0_indexList; // vertexFormat.m_uv0.m_strideInBytes = 2 * sizeof (dFloat); // now we create and empty mesh NewtonMesh* const newtonMesh = NewtonMeshCreate(scene->GetNewton()); NewtonMeshBuildFromVertexListIndexList(newtonMesh, &vertexFormat); // now we can use this mesh for lot of stuff, we can apply UV, we can decompose into convex, NewtonCollision* const collision = NewtonCreateConvexHullFromMesh(scene->GetNewton(), newtonMesh, 0.001f, 0); // for now we will simple make simple Box, make a visual Mesh DemoMesh* const visualMesh = new DemoMesh (newtonMesh); dMatrix matrix (dGetIdentityMatrix()); matrix.m_posit = origin; matrix.m_posit.m_w = 1.0f; NewtonBody* const body = CreateSimpleSolid (scene, visualMesh, mass, matrix, collision, 0); visualMesh->Release(); NewtonDestroyCollision(collision); NewtonMeshDestroy (newtonMesh); return body; }
bool DeserializeMesh (const NewtonMesh* const mesh, TiXmlElement* const rootNode) { // load all the vertexData TiXmlElement* const pointElement = (TiXmlElement*) rootNode->FirstChild ("points"); int positionCount; TiXmlElement* const positionsElement = (TiXmlElement*) pointElement->FirstChild ("position"); positionsElement->Attribute("float4", &positionCount); dFloat* const positions = new dFloat[4 * positionCount]; dStringToFloatArray (positionsElement->Attribute("floats"), positions, 4 * positionCount); int normalCount; TiXmlElement* normalsElement = (TiXmlElement*) pointElement->FirstChild ("normal"); normalsElement->Attribute("float3", &normalCount); dFloat* const normals = new dFloat[3 * normalCount]; dStringToFloatArray (normalsElement->Attribute("floats"), normals, 3 * normalCount); int uv0Count; TiXmlElement* uv0Element = (TiXmlElement*) pointElement->FirstChild ("uv0"); uv0Element->Attribute("float2", &uv0Count); dFloat* const uv0 = new dFloat[2 * uv0Count]; dStringToFloatArray (uv0Element->Attribute("floats"), uv0, 2 * uv0Count); int uv1Count; TiXmlElement* uv1Element = (TiXmlElement*) pointElement->FirstChild ("uv1"); uv1Element->Attribute("float2", &uv1Count); dFloat* const uv1 = new dFloat[2 * uv1Count]; dStringToFloatArray (uv1Element->Attribute("floats"), uv1, 2 * uv1Count); //load face informations TiXmlElement* polygonsElement = (TiXmlElement*) rootNode->FirstChild ("polygons"); int faceCount; polygonsElement->Attribute("count", &faceCount); int* const faceIndexCount = new int[faceCount]; dStringToIntArray (polygonsElement->Attribute("faceIndexCount"), faceIndexCount, faceCount); int* const faceMaterials = new int [faceCount]; TiXmlElement* materialElement = (TiXmlElement*) polygonsElement->FirstChild ("faceMaterial"); dStringToIntArray (materialElement->Attribute("index"), faceMaterials, faceCount); int indexCount = 0; for (int i = 0; i < faceCount; i ++) { indexCount += faceIndexCount[i]; } int* const positionVertexIndex = new int [indexCount]; TiXmlElement* const positionVertexIndexElement = (TiXmlElement*) polygonsElement->FirstChild ("position"); dStringToIntArray (positionVertexIndexElement->Attribute("index"), positionVertexIndex, indexCount); int* const normalVertexIndex = new int [indexCount]; TiXmlElement* const normalVertexIndexElement = (TiXmlElement*) polygonsElement->FirstChild ("normal"); dStringToIntArray (normalVertexIndexElement->Attribute("index"), normalVertexIndex, indexCount); int* const uv0VertexIndex = new int [indexCount]; TiXmlElement* const uv0VertexIndexElement = (TiXmlElement*) polygonsElement->FirstChild ("uv0"); dStringToIntArray (uv0VertexIndexElement->Attribute("index"), uv0VertexIndex, indexCount); int* const uv1VertexIndex = new int [indexCount]; TiXmlElement* const uv1VertexIndexElement = (TiXmlElement*) polygonsElement->FirstChild ("uv1"); dStringToIntArray (uv1VertexIndexElement->Attribute("index"), uv1VertexIndex, indexCount); // BuildFromVertexListIndexList(faceCount, faceIndexCount, faceMaterials, // &positions[0], 4 * sizeof (dFloat), positionVertexIndex, // &normals[0], 3 * sizeof (dFloat), normalVertexIndex, // &uv0[0], 2 * sizeof (dFloat), uv0VertexIndex, // &uv1[0], 2 * sizeof (dFloat), uv1VertexIndex); NewtonMeshBuildFromVertexListIndexList(mesh, faceCount, faceIndexCount, faceMaterials, &positions[0], 4 * sizeof (dFloat), positionVertexIndex, &normals[0], 3 * sizeof (dFloat), normalVertexIndex, &uv0[0], 2 * sizeof (dFloat), uv0VertexIndex, &uv1[0], 2 * sizeof (dFloat), uv1VertexIndex); delete[] uv1VertexIndex; delete[] uv0VertexIndex; delete[] normalVertexIndex; delete[] positionVertexIndex; delete[] faceMaterials; delete[] faceIndexCount; delete[] uv1; delete[] uv0; delete[] normals; delete[] positions; return true; }