-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectmodel.cpp
221 lines (190 loc) · 6.28 KB
/
objectmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "objectmodel.h"
#include <map>
#include <QString>
#include <QDebug>
#include "polygon.h"
#include "vertex.h"
#include "material.h"
double min(double a, double b)
{
return (a<b?a:b);
}
double max(double a, double b)
{
return (a>b?a:b);
}
double max(double a, double b, double c)
{
return max(max(a, b), c);
}
/*
*
*/
namespace jEle {
ObjectModel::ObjectModel()
{
}
ObjectModel::ObjectModel(ObjectModel *o) :
materials_by_name(o->materials_by_name)
{
std::map<Vertex*,Vertex*> vMap;
for (int i=0; i<o->vertices.size(); i++)
{
Vertex *newVertex = new Vertex(o->vertices.at(i));
vertices.push_back(newVertex);
vMap[o->vertices.at(i)] = newVertex;
}
for (int i=0; i<o->normals.size(); i++)
{
Vertex *newNormal = new Vertex(o->normals.at(i));
normals.push_back(newNormal);
vMap[o->normals.at(i)] = newNormal;
}
for (int i=0; i<o->materials.size(); i++)
{
Material *newMaterial = new Material(*o->materials.at(i));
materials.push_back(newMaterial);
}
for (int i=0; i<o->textureVertices.size(); i++)
{
Vertex *newTextureVertex = new Vertex(o->textureVertices.at(i));
textureVertices.push_back(newTextureVertex);
vMap[o->textureVertices.at(i)] = newTextureVertex;
}
for (int i=0; i<o->polygons.size(); i++)
{
polygons.push_back(
o->polygons.at(i)->mapClone(
vMap,
materials.at(
o->materials.indexOf(
o->polygons.at(i)->getMaterial())
)
)
);
}
}
ObjectModel::~ObjectModel()
{
while (!polygons.empty()) {
delete polygons.takeFirst();
}
while (!vertices.empty()) {
delete vertices.takeFirst();
}
while (!normals.empty()) {
delete normals.takeFirst();
}
while (!textureVertices.empty()) {
delete textureVertices.takeFirst();
}
while (!materials.empty()) {
delete materials.takeFirst();
}
}
void ObjectModel::addVertex(double x, double y, double z)
{
if (vertices.empty())
{
minX = maxX = x;
minY = maxY = y;
minZ = maxZ = z;
}
else
{
minX = min(minX,x);
minY = min(minY,y);
minZ = min(minZ,z);
maxX = max(maxX,x);
maxY = max(maxY,y);
maxZ = max(maxZ,z);
}
vertices.push_back(new Vertex(x, y, z));
}
void ObjectModel::addVertex(double x, double y, double z, double w)
{
addVertex(x/w, y/w, z/w);
}
void ObjectModel::addNormal(double x, double y, double z)
{
normals.push_back(new Vertex(x, y, z));
}
void ObjectModel::addTextureVertex(double x, double y, double z)
{
textureVertices.push_back(new Vertex(x, y, z));
}
void ObjectModel::addMaterial(QString name, Material *material)
{
materials_by_name[name] = materials.size();
materials.append(material);
}
Polygon *ObjectModel::addPolygon(QList<int> vertex_indexes)
{
return addPolygon("", vertex_indexes, QList<int>(), QList<int>());
}
Polygon *ObjectModel::addPolygon(QString material, QList<int> vertex_indexes, QList<int> texture_indexes, QList<int> normal_indexes)
{
QList<Vertex*> _normals;
for (int i=0; i<normal_indexes.size(); i++) {
int ni = normal_indexes.at(i);
if (ni<normals.size()) {
if (ni<0)
_normals.push_back(normals.at(0));
else
_normals.push_back(normals.at(ni));
}
else
qDebug() << "[ObjectModel::addPolygon()] normal_indexes contains an index that is not valid (" << ni << "|" << normals.size() << ")";
}
QList<Vertex*> _vertices;
for (int i=0; i<vertex_indexes.size(); i++) {
int vi = vertex_indexes.at(i);
if (vi<vertices.size()) {
if (vi<0)
_vertices.push_back(vertices.at(0));
else
_vertices.push_back(vertices.at(vi));
}
else
qDebug() << "[ObjectModel::addPolygon()] vertex_indexes contains an index that is not valid (" << vi << "|" << vertices.size() << ")";
}
QList<Vertex*> _textureVertices;
for (int i=0; i<texture_indexes.size(); i++) {
int ti = texture_indexes.at(i);
if (ti<textureVertices.size()) {
if (ti<0)
_textureVertices.push_back(textureVertices.at(0));
else
_textureVertices.push_back(textureVertices.at(ti));
} else
qDebug() << "[ObjectModel::addPolygon()] texture_indexes contains an index that is not valid (" << ti << "|" << textureVertices.size() << ")";
}
Material *_material = NULL;
if (materials_by_name.contains(material))
_material = materials.at(materials_by_name.value(material));
Polygon *toAdd = new Polygon(_vertices, _textureVertices, _normals, _material);
polygons.push_back(toAdd);
return toAdd;
}
QList<Polygon*> &ObjectModel::getPolygons()
{
return polygons;
}
QList<Vertex*> &ObjectModel::getVertices()
{
return vertices;
}
void ObjectModel::normalize()
{
double scaleX = maxX-minX, scaleY = maxY-minY, scaleZ = maxZ-minZ;
double scale = max(scaleX, scaleY, scaleZ);
double halfScale = scale / 2;
double xOffset = minX + scaleX/2, yOffset = minY + scaleY/2, zOffset = minZ + scaleZ/2;
for (int i=0; i<vertices.size(); i++)
{
vertices.at(i)->setX((vertices.at(i)->x() - xOffset)/halfScale);
vertices.at(i)->setY((vertices.at(i)->y() - yOffset)/halfScale);
vertices.at(i)->setZ((vertices.at(i)->z() - zOffset)/halfScale);
}
}
}