NewtonMesh* DemoMesh::CreateNewtonMesh(NewtonWorld* const world, const dMatrix& meshMatrix) { NewtonMesh* const mesh = NewtonMeshCreate(world); NewtonMeshBeginBuild (mesh); dMatrix rotation ((meshMatrix.Inverse4x4()).Transpose4X4()); for (dListNode* node = GetFirst(); node; node = node->GetNext()) { DemoSubMesh& segment = node->GetInfo(); for (int i = 0; i < segment.m_indexCount; i += 3) { NewtonMeshBeginFace(mesh); for (int j = 0; j < 3; j ++) { int index = segment.m_indexes[i + j]; dVector p (meshMatrix.TransformVector(dVector (m_vertex[index * 3 + 0], m_vertex[index * 3 + 1], m_vertex[index * 3 + 2], 0.0f))); dVector n (rotation.RotateVector(dVector (m_normal[index * 3 + 0], m_normal[index * 3 + 1], m_normal[index * 3 + 2], 0.0f))); dAssert ((n.DotProduct3(n)) > 0.0f); n = n.Scale (1.0f / dSqrt (n.DotProduct3(n))); NewtonMeshAddPoint(mesh, p.m_x, p.m_y, p.m_z); NewtonMeshAddNormal(mesh, n.m_x, n.m_y, n.m_z); NewtonMeshAddMaterial(mesh, segment.m_textureHandle); NewtonMeshAddUV0(mesh, m_uv[index * 2 + 0], m_uv[index * 2 + 1]); } NewtonMeshEndFace(mesh); // NewtonMeshAddFace(mesh, 3, &point[0][0], sizeof (point) / 3, segment.m_textureHandle); } } NewtonMeshEndBuild(mesh); return mesh; }
void BuildTetraHedraCube(DemoEntityManager* const scene, int materialID) { dFloat mass = 5.0f; NewtonWorld* const world = scene->GetNewton(); NewtonMesh* const tetraCube = NewtonMeshCreate(scene->GetNewton()); NewtonMeshBeginBuild(tetraCube); BuildTetraSolidBlock(tetraCube, 2, 2, 15, 0.5f, 0.5f, 0.5f); NewtonMeshEndBuild(tetraCube); dMatrix aligmentUV(dGetIdentityMatrix()); int material = LoadTexture("smilli.tga"); NewtonMeshApplyBoxMapping(tetraCube, material, material, material, &aligmentUV[0][0]); NewtonMeshCalculateVertexNormals(tetraCube, 60.0f * dDegreeToRad); // make a deformable collision mesh NewtonCollision* const deformableCollision = NewtonCreateDeformableSolid(world, tetraCube, materialID); //create a rigid body with a deformable mesh m_body = CreateRigidBody(scene, mass, deformableCollision); // create the soft body mesh DemoMesh* const mesh = new TetrahedraSoftMesh(scene, tetraCube, m_body); SetMesh(mesh, dGetIdentityMatrix()); // do not forget to destroy this objects, else you get bad memory leaks. mesh->Release (); NewtonDestroyCollision(deformableCollision); NewtonMeshDestroy(tetraCube); }
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; }
NewtonMesh* CreateCollisionTreeDoubleFaces (NewtonWorld* world, NewtonCollision* optimizedDoubelFacesTree) { NewtonMesh* mesh = NewtonMeshCreate(world); dMatrix matrix (dGetIdentityMatrix()); NewtonMeshBeginFace(mesh); NewtonCollisionForEachPolygonDo (optimizedDoubelFacesTree, &matrix[0][0], ExtrudeFaces, mesh); NewtonMeshEndFace(mesh); return mesh; }
// 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; }
void ConvexApproximationObject::BuildMesh() { // since max does no provide the iNode that will own this mesh I have no choice bu to apply the root matrix to all vertex ConvexApproximationClassDesc* const desc = (ConvexApproximationClassDesc*) ConvexApproximationClassDesc::GetDescriptor(); INode* const sourceNode = desc->m_sourceNode; //dMatrix rootMatrix1 (GetMatrixFromMaxMatrix (sourceNode->GetNodeTM (0))); dMatrix rootMatrix (GetMatrixFromMaxMatrix (sourceNode->GetObjectTM(0))); dVector scale; dMatrix stretchAxis; dMatrix orthogonalRootTransform; rootMatrix.PolarDecomposition (orthogonalRootTransform, scale, stretchAxis); orthogonalRootTransform = orthogonalRootTransform.Inverse(); // create a Newton world, as a manager of everything Newton related stuff NewtonWorld* const world = NewtonCreate (); // create an empty mesh and load the max mesh to it NewtonMesh* const sourceMesh = NewtonMeshCreate (world); // load all faces NewtonMeshBeginFace(sourceMesh); LoadGeometries (sourceMesh, orthogonalRootTransform); NewtonMeshEndFace(sourceMesh); // make a convex approximation form this newton mesh effect desc->m_progress = -1; Interface* const inteface = desc->m_currentInterface; inteface->ProgressStart("Creation Convex approx ...", TRUE, ConvexApproximationClassDesc::ReportMaxProgress, NULL); NewtonMesh* approximationMesh = NewtonMeshApproximateConvexDecomposition (sourceMesh, m_currentConcavity, 0.2f, m_currentMaxCount, 1000, ConvexApproximationClassDesc::ReportProgress); inteface->ProgressEnd(); NewtonMeshDestroy (sourceMesh); // now convert the new mesh to a max poly Object MNMesh& maxMesh = GetMesh(); maxMesh.ClearAndFree(); int faceCount = 0; int vertexCount = NewtonMeshGetVertexCount(approximationMesh); for (void* face = NewtonMeshGetFirstFace(approximationMesh); face; face = NewtonMeshGetNextFace(approximationMesh, face)) { if (!NewtonMeshIsFaceOpen(approximationMesh, face)) { faceCount ++; } } //maxMesh.Clear(); maxMesh.setNumVerts(vertexCount); maxMesh.setNumFaces(faceCount); // add all vertex int vertexStride = NewtonMeshGetVertexStrideInByte(approximationMesh) / sizeof (dFloat64); dFloat64* const vertex = NewtonMeshGetVertexArray (approximationMesh); for (int j = 0; j < vertexCount; j ++) { dVector p (orthogonalRootTransform.TransformVector(dVector (float (vertex[vertexStride * j + 0]), float (vertex[vertexStride * j + 1]), float (vertex[vertexStride * j + 2]), float(1.0f)))); maxMesh.P(j) = Point3 (p.m_x, p.m_y, p.m_z); } // count the number of face and make a face map int faceIndex = 0; for (void* face = NewtonMeshGetFirstFace(approximationMesh); face; face = NewtonMeshGetNextFace(approximationMesh, face)) { if (!NewtonMeshIsFaceOpen(approximationMesh, face)) { int faceIndices[256]; int indexCount = NewtonMeshGetFaceIndexCount (approximationMesh, face); NewtonMeshGetFaceIndices (approximationMesh, face, faceIndices); MNFace* const face = maxMesh.F(faceIndex); face->MakePoly(indexCount, faceIndices, NULL, NULL); face->material = 0; faceIndex ++; } } maxMesh.InvalidateGeomCache(); maxMesh.InvalidateTopoCache(); maxMesh.FillInMesh(); maxMesh.AutoSmooth(45.0f * 3.1416f / 160.0f, false, false); NewtonMeshDestroy (approximationMesh); NewtonDestroy (world); }
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; }
dMeshNodeInfo::dMeshNodeInfo(dScene* world) :dGeometryNodeInfo (world), m_mesh (NewtonMeshCreate(world->GetNewtonWorld())) { }
mesh::mesh(const std::string &mn, const mat4 &tf, const world &_world): /** Don't know if world() is the best thing to pass but it works for now **/ _mesh(NewtonMeshCreate(_world)) { Ogre::MeshPtr meshPtr; try { meshPtr = Ogre::MeshPtr(Ogre::MeshManager::getSingleton().load(mn, Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME)); } catch(...) { return; } const Ogre::Mesh &mesh = *meshPtr; NewtonMeshBeginFace(_mesh); for (unsigned smi = 0; smi < mesh.getNumSubMeshes(); smi++) { Ogre::SubMesh *subMesh = mesh.getSubMesh(smi); Ogre::VertexData *vertexData = (subMesh->useSharedVertices) ? vertexData = mesh.sharedVertexData : vertexData = subMesh->vertexData; Ogre::VertexDeclaration *vertexDecl = vertexData->vertexDeclaration; const Ogre::VertexElement *element = vertexDecl->findElementBySemantic(Ogre::VES_POSITION); Ogre::HardwareVertexBufferSharedPtr vertexHVBSP = vertexData->vertexBufferBinding->getBuffer(element->getSource()); unsigned char *vPtr = (unsigned char*)(vertexHVBSP->lock(Ogre::HardwareBuffer::HBL_READ_ONLY)); Ogre::IndexData *indexData = subMesh->indexData; size_t numIndices = indexData->indexCount; size_t numTris = numIndices / 3; // get pointer! Ogre::HardwareIndexBufferSharedPtr indexHIBSP = indexData->indexBuffer; // 16 or 32 bit indices? bool indicesAre32Bit = (indexHIBSP->getType() == Ogre::HardwareIndexBuffer::IT_32BIT); unsigned long *longPtr = NULL; unsigned short *shortPtr = NULL; if (indicesAre32Bit) longPtr = static_cast<unsigned long*>( indexHIBSP->lock(Ogre::HardwareBuffer::HBL_READ_ONLY)); else shortPtr = static_cast<unsigned short*>( indexHIBSP->lock(Ogre::HardwareBuffer::HBL_READ_ONLY)); //now loop through the indices, getting polygon info! int iOffset = 0; for (size_t i = 0; i < numTris; i++) { vec3 triVertices[3]; unsigned char *vOffset = NULL; float *vertexPosPtr = NULL; int idx = 0; for (int j = 0; j < 3; j++) { if (indicesAre32Bit) idx = longPtr[iOffset + j]; else idx = shortPtr[iOffset + j]; vOffset = vPtr + (idx * vertexHVBSP->getVertexSize()); element->baseVertexPointerToElement(vOffset, &vertexPosPtr); triVertices[j].x = *vertexPosPtr; vertexPosPtr++; triVertices[j].y = *vertexPosPtr; vertexPosPtr++; triVertices[j].z = *vertexPosPtr; vertexPosPtr++; triVertices[j] = tf * triVertices[j]; } // _mesh, 3 vertices (triangle), (float = 4 bytes) * 3 // index = index of sub mesh (easy to recognize) NewtonMeshAddFace(_mesh, 3, &triVertices[0].x, sizeof(float) * 3, smi); iOffset += 3; } //unlock the buffers! vertexHVBSP->unlock(); indexHIBSP->unlock(); } NewtonMeshEndFace(_mesh); }