static btTriangleIndexVertexArray* GenerateTriMeshData (iMeshWrapper* mesh, int*& indices, size_t& triangleCount, btVector3*& vertices, size_t& vertexCount, csStringID base_id, csStringID colldet_id, float internalScale) { csRef<iTriangleMesh> trimesh = FindColdetTriangleMesh(mesh, base_id, colldet_id); if (!trimesh) return 0; // TODO: remove double vertices csTriangle *c_triangle = trimesh->GetTriangles(); triangleCount = trimesh->GetTriangleCount(); vertexCount = trimesh->GetVertexCount (); delete[] indices; indices = new int[triangleCount * 3]; int indexStride = 3 * sizeof (int); size_t i; int* id = indices; for (i = 0 ; i < triangleCount ; i++) { *id++ = c_triangle[i].a; *id++ = c_triangle[i].b; *id++ = c_triangle[i].c; } delete[] vertices; vertices = new btVector3[vertexCount]; csVector3 *c_vertex = trimesh->GetVertices(); int vertexStride = sizeof (btVector3); for (i = 0 ; i < vertexCount ; i++) vertices[i].setValue (c_vertex[i].x * internalScale, c_vertex[i].y * internalScale, c_vertex[i].z * internalScale); btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray ((int)triangleCount, indices, (int)indexStride, (int)vertexCount, (btScalar*) &vertices[0].x (), vertexStride); return indexVertexArrays; }
csOpcodeCollider::csOpcodeCollider (iMeshWrapper* mesh, csOpcodeCollisionSystem* sys) : scfImplementationType (this), system (sys), mesh (mesh), model (NULL), indexholder (NULL), vertholder (NULL), scale (1.0, 1.0, 1.0) { opcMeshInt.SetCallback (&MeshCallback, this); csRef<iTriangleMesh> triMesh = FindColdetTriangleMesh (mesh, system->baseID, system->colldetID); if (!triMesh) return; csVector3* vertices = triMesh->GetVertices (); size_t vertcount = triMesh->GetVertexCount (); csTriangle* triangles = triMesh->GetTriangles (); size_t tri_count = triMesh->GetTriangleCount (); Opcode::OPCODECREATE OPCC; size_t i; if (tri_count>=1) { model = new Opcode::Model; if (!model) return; vertholder = new Point [vertcount]; indexholder = new unsigned int[3*tri_count]; aabbox.StartBoundingBox (); for (i = 0; i < vertcount; i++) { aabbox.AddBoundingVertex (vertices[i]); vertholder[i].Set (vertices[i].x , vertices[i].y , vertices[i].z); } volume = aabbox.Volume (); int index = 0; for (i = 0 ; i < tri_count ; i++) { indexholder[index++] = triangles[i].a; indexholder[index++] = triangles[i].b; indexholder[index++] = triangles[i].c; } opcMeshInt.SetNbTriangles ((udword)tri_count); opcMeshInt.SetNbVertices ((udword)vertcount); // Mesh data OPCC.mIMesh = &opcMeshInt; OPCC.mSettings.mRules = Opcode::SPLIT_SPLATTER_POINTS | Opcode::SPLIT_GEOM_CENTER; OPCC.mNoLeaf = true; OPCC.mQuantized = true; OPCC.mKeepOriginal = false; OPCC.mCanRemap = false; } else return; // this should create the OPCODE model bool status = model->Build (OPCC); if (!status) { delete model; model = NULL; } }