source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/EntityMerger.cpp @ 3071

Revision 3071, 3.2 KB checked in by mattausch, 16 years ago (diff)

worked on dynamic objects

Line 
1#include "EntityMerger.h"
2#include "Transform3.h"
3#include "SceneEntity.h"
4#include "Shape.h"
5#include "Geometry.h"
6
7
8using namespace std;
9
10
11namespace CHCDemoEngine
12{
13
14EntityMerger::EntityMerger(SceneEntity *entity1, SceneEntity *entity2):
15mEntity1(entity1), mEntity2(entity2)
16{}
17
18
19EntityMerger::~EntityMerger()
20{}
21
22
23SceneEntity *EntityMerger::Merge()
24{
25        Shape *shape1 = mEntity1->GetShape(0);
26        Shape *shape2 = mEntity2->GetShape(0);
27
28        SceneEntity *ent = new SceneEntity(new Transform3());
29
30        Shape *shape = MergeShapes(shape1, shape2, ent);
31       
32        LODLevel lodLevel(0);
33
34        lodLevel.AddShape(shape);
35        ent->AddLODLevel(lodLevel);
36
37        return ent;
38}
39
40
41Shape *EntityMerger::MergeShapes(Shape *shape1, Shape *shape2, SceneEntity *parent)
42{
43        Material *mat = shape1->GetMaterial();
44
45        Geometry *geom1 = shape1->GetGeometry();
46        Geometry *geom2 = shape2->GetGeometry();
47
48        Geometry *geom = MergeGeometry(geom1, geom2);
49
50        Shape *shape = new Shape(geom, mat);
51
52        return shape;
53}
54
55
56Geometry *EntityMerger::MergeGeometry(Geometry *geom1, Geometry *geom2)
57{
58        const int size1 = geom1->GetNumTriangles() * 3;
59        const int size2 = geom2->GetNumTriangles() * 3;
60
61        const int size = size1 + size2;
62
63        Vector3 *vertices = new Vector3[size];
64        Vector3 *normals = new Vector3[size];
65
66        Texcoord2 *texcoords;
67       
68        if (geom1->mTexCoords && geom2->mTexCoords)
69                texcoords = new Texcoord2[size];
70        else
71                texcoords = NULL;
72
73        int i = 0;
74
75        for (; i < size1; ++ i)
76        {
77                vertices[i] = geom1->mVertices[i];
78                normals[i] = geom1->mNormals[i];
79
80                if (texcoords)
81                        texcoords[i] = geom1->mTexCoords[i];
82        }
83
84        for (; i < size; ++ i)
85        {
86                int idx = i - size1;
87                vertices[i] = geom2->mVertices[idx];
88                normals[i] = geom2->mNormals[idx];
89               
90                if (texcoords) texcoords[i] = geom2->mTexCoords[idx];
91        }
92
93        Geometry *geom = new Geometry(vertices, normals, texcoords, size, true);
94
95        return geom;
96}
97
98
99Shape *EntityMerger::MergeShapes(const ShapeContainer &shapes, SceneEntity *parent)
100{
101        /*Material *mat = shape1->GetMaterial();
102
103        Geometry *geom1 = shape1->GetGeometry();
104        Geometry *geom2 = shape2->GetGeometry();
105
106        Geometry *geom = MergeGeometry(geometries);
107
108        Shape *shape = new Shape(geom, mat, parent);
109
110        return shape;*/
111        return NULL;
112}
113
114
115Geometry *EntityMerger::MergeGeometries(const GeometryContainer &geometries)
116{
117        int size = 0;
118        bool hasTexCoords = true;
119
120        for (size_t i = 0; i < geometries.size(); ++ i)
121        {
122                Geometry *geom = geometries[i];
123                size += geom->GetNumTriangles() * 3;
124
125                if (geom->mTexCoords)
126                        hasTexCoords = false;
127        }
128
129        Vector3 *vertices = new Vector3[size];
130        Vector3 *normals = new Vector3[size];
131
132        Texcoord2 *texcoords;
133       
134        if (hasTexCoords)
135                texcoords = new Texcoord2[size];
136        else
137                texcoords = NULL;
138
139        int totalIdx = 0;
140
141        for (size_t i = 0; i < geometries.size(); ++ i)
142        {
143                Geometry *geom = geometries[i];
144
145                for (int j = 0; j < geom->GetNumTriangles() * 3; ++ j)
146                {
147                        vertices[totalIdx + j] = geom->mVertices[j];
148                        normals[totalIdx + j] = geom->mNormals[j];
149
150                        if (hasTexCoords) texcoords[totalIdx + j] = geom->mTexCoords[j];
151                }
152
153                totalIdx += geom->GetNumTriangles() * 3;
154        }
155
156
157        Geometry *geom = new Geometry(vertices, normals, texcoords, size, true);
158
159        return geom;
160}
161
162}
Note: See TracBrowser for help on using the repository browser.