#include "EntityMerger.h" #include "Transform3.h" #include "SceneEntity.h" #include "Shape.h" #include "Geometry.h" using namespace std; namespace CHCDemoEngine { EntityMerger::EntityMerger(SceneEntity *entity1, SceneEntity *entity2): mEntity1(entity1), mEntity2(entity2) {} EntityMerger::~EntityMerger() {} SceneEntity *EntityMerger::Merge() { Shape *shape1 = mEntity1->GetShape(0); Shape *shape2 = mEntity2->GetShape(0); SceneEntity *ent = new SceneEntity(new Transform3()); Shape *shape = MergeShapes(shape1, shape2, ent); LODLevel lodLevel(0); lodLevel.AddShape(shape); ent->AddLODLevel(lodLevel); return ent; } Shape *EntityMerger::MergeShapes(Shape *shape1, Shape *shape2, SceneEntity *parent) { Material *mat = shape1->GetMaterial(); Geometry *geom1 = shape1->GetGeometry(); Geometry *geom2 = shape2->GetGeometry(); Geometry *geom = MergeGeometry(geom1, geom2); Shape *shape = new Shape(geom, mat); return shape; } Geometry *EntityMerger::MergeGeometry(Geometry *geom1, Geometry *geom2) { const int size1 = geom1->GetNumTriangles() * 3; const int size2 = geom2->GetNumTriangles() * 3; const int size = size1 + size2; Vector3 *vertices = new Vector3[size]; Vector3 *normals = new Vector3[size]; Texcoord2 *texcoords; if (geom1->mTexCoords && geom2->mTexCoords) texcoords = new Texcoord2[size]; else texcoords = NULL; int i = 0; for (; i < size1; ++ i) { vertices[i] = geom1->mVertices[i]; normals[i] = geom1->mNormals[i]; if (texcoords) texcoords[i] = geom1->mTexCoords[i]; } for (; i < size; ++ i) { int idx = i - size1; vertices[i] = geom2->mVertices[idx]; normals[i] = geom2->mNormals[idx]; if (texcoords) texcoords[i] = geom2->mTexCoords[idx]; } Geometry *geom = new Geometry(vertices, normals, texcoords, size, true); return geom; } Shape *EntityMerger::MergeShapes(const ShapeContainer &shapes, SceneEntity *parent) { /*Material *mat = shape1->GetMaterial(); Geometry *geom1 = shape1->GetGeometry(); Geometry *geom2 = shape2->GetGeometry(); Geometry *geom = MergeGeometry(geometries); Shape *shape = new Shape(geom, mat, parent); return shape;*/ return NULL; } Geometry *EntityMerger::MergeGeometries(const GeometryContainer &geometries) { int size = 0; bool hasTexCoords = true; for (size_t i = 0; i < geometries.size(); ++ i) { Geometry *geom = geometries[i]; size += geom->GetNumTriangles() * 3; if (geom->mTexCoords) hasTexCoords = false; } Vector3 *vertices = new Vector3[size]; Vector3 *normals = new Vector3[size]; Texcoord2 *texcoords; if (hasTexCoords) texcoords = new Texcoord2[size]; else texcoords = NULL; int totalIdx = 0; for (size_t i = 0; i < geometries.size(); ++ i) { Geometry *geom = geometries[i]; for (int j = 0; j < geom->GetNumTriangles() * 3; ++ j) { vertices[totalIdx + j] = geom->mVertices[j]; normals[totalIdx + j] = geom->mNormals[j]; if (hasTexCoords) texcoords[totalIdx + j] = geom->mTexCoords[j]; } totalIdx += geom->GetNumTriangles() * 3; } Geometry *geom = new Geometry(vertices, normals, texcoords, size, true); return geom; } }