forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.cpp
More file actions
435 lines (346 loc) · 9.93 KB
/
Copy pathmodel.cpp
File metadata and controls
435 lines (346 loc) · 9.93 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// model.cpp
//
// Copyright (C) 2004-2010, Celestia Development Team
// Original version by Chris Laurel <claurel@gmail.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "model.h"
#include <cassert>
#include <functional>
#include <algorithm>
using namespace cmod;
using namespace Eigen;
using namespace std;
Model::Model()
{
textureUsage.fill(false);
}
Model::~Model()
{
for (const auto mesh : meshes)
delete mesh;
}
/*! Return the material with the specified index, or nullptr if
* the index is out of range. The returned material pointer
* is const.
*/
const Material*
Model::getMaterial(unsigned int index) const
{
if (index < materials.size())
return materials[index];
else
return nullptr;
}
/*! Add a new material to the model's material library; the
* return value is the number of materials in the model.
*/
unsigned int
Model::addMaterial(const Material* m)
{
// Update the texture map usage information for the model. Since
// the material being added isn't necessarily used by a mesh within
// the model, we could potentially end up with false positives--this
// won't cause any rendering troubles, but could hurt performance
// if it forces multipass rendering when it's not required.
for (int i = 0; i < Material::TextureSemanticMax; i++)
{
if (m->maps[i] != nullptr)
{
textureUsage[i] = true;
}
}
materials.push_back(m);
return materials.size();
}
unsigned int
Model::getMaterialCount() const
{
return materials.size();
}
unsigned int
Model::getVertexCount() const
{
unsigned int count = 0;
for (const auto mesh : meshes)
count += mesh->getVertexCount();
return count;
}
unsigned int
Model::getPrimitiveCount() const
{
unsigned int count = 0;
for (const auto mesh : meshes)
count += mesh->getPrimitiveCount();
return count;
}
unsigned int
Model::getMeshCount() const
{
return meshes.size();
}
Mesh*
Model::getMesh(unsigned int index) const
{
if (index < meshes.size())
return meshes[index];
else
return nullptr;
}
unsigned int
Model::addMesh(Mesh* m)
{
meshes.push_back(m);
return meshes.size();
}
bool
Model::pick(const Eigen::Vector3d& rayOrigin,
const Eigen::Vector3d& rayDirection,
Mesh::PickResult* result) const
{
double maxDistance = 1.0e30;
double closest = maxDistance;
Mesh::PickResult closestResult;
for (const auto mesh : meshes)
{
Mesh::PickResult result;
if (mesh->pick(rayOrigin, rayDirection, &result))
{
if (result.distance < closest)
{
closestResult = result;
closestResult.mesh = mesh;
closest = result.distance;
}
}
}
if (closest != maxDistance)
{
if (result != nullptr)
{
*result = closestResult;
}
return true;
}
return false;
}
bool
Model::pick(const Vector3d& rayOrigin, const Vector3d& rayDirection, double& distance) const
{
Mesh::PickResult result;
bool hit = pick(rayOrigin, rayDirection, &result);
if (hit)
{
distance = result.distance;
}
return hit;
}
/*! Translate and scale a model. The transformation applied to
* each vertex in the model is:
* v' = (v + translation) * scale
*/
void
Model::transform(const Vector3f& translation, float scale)
{
for (const auto mesh : meshes)
mesh->transform(translation, scale);
}
void
Model::normalize(const Vector3f& centerOffset)
{
AlignedBox<float, 3> bbox;
for (const auto mesh : meshes)
bbox.extend(mesh->getBoundingBox());
Vector3f center = (bbox.min() + bbox.max()) * 0.5f + centerOffset;
Vector3f extents = bbox.max() - bbox.min();
float maxExtent = extents.maxCoeff();
transform(-center, 2.0f / maxExtent);
normalized = true;
}
static bool
operator<(const Material::Color& c0, const Material::Color& c1)
{
if (c0.red() < c1.red())
return true;
if (c0.red() > c1.red())
return false;
if (c0.green() < c1.green())
return true;
if (c0.green() > c1.green())
return false;
return c0.blue() < c1.blue();
}
// Define an ordering for materials; required for elimination of duplicate
// materials.
static bool
operator<(const Material& m0, const Material& m1)
{
// Checking opacity first and doing it backwards is deliberate. It means
// that after sorting, translucent materials will end up with higher
// material indices than opaque ones. Ultimately, after sorting
// mesh primitive groups by material, translucent groups will end up
// rendered after opaque ones.
if (m0.opacity < m1.opacity)
return true;
if (m0.opacity > m1.opacity)
return false;
// Reverse sense of comparison here--additive blending is 1, normal
// blending is 0, and we'd prefer to render additively blended submeshes
// last.
if (m0.blend > m1.blend)
return true;
if (m0.blend < m1.blend)
return false;
if (m0.diffuse < m1.diffuse)
return true;
if (m1.diffuse < m0.diffuse)
return false;
if (m0.emissive < m1.emissive)
return true;
if (m1.emissive < m0.emissive)
return false;
if (m0.specular < m1.specular)
return true;
if (m1.specular < m0.specular)
return false;
if (m0.specularPower < m1.specularPower)
return true;
if (m0.specularPower > m1.specularPower)
return false;
for (unsigned int i = 0; i < Material::TextureSemanticMax; i++)
{
if (m0.maps[i] < m1.maps[i])
return true;
if (m0.maps[i] > m1.maps[i])
return false;
}
// Materials are identical
return false;
}
// Simple struct that pairs an index with a material; the index is used to
// keep track of the original material index after sorting.
struct IndexedMaterial
{
int originalIndex;
const Material* material;
};
static bool
operator<(const IndexedMaterial& im0, const IndexedMaterial& im1)
{
return *(im0.material) < *(im1.material);
}
static bool
operator!=(const IndexedMaterial& im0, const IndexedMaterial& im1)
{
return im0 < im1 || im1 < im0;
}
void
Model::uniquifyMaterials()
{
// No work to do if there's just a single material
if (materials.size() <= 1)
return;
// Create an array of materials with the indices attached
vector<IndexedMaterial> indexedMaterials;
unsigned int i;
for (i = 0; i < materials.size(); i++)
{
IndexedMaterial im;
im.originalIndex = i;
im.material = materials[i];
indexedMaterials.push_back(im);
}
// Sort the indexed materials so that we can uniquify them
sort(indexedMaterials.begin(), indexedMaterials.end());
vector<const Material*> uniqueMaterials;
vector<unsigned int> materialMap(materials.size());
vector<unsigned int> duplicateMaterials;
// From the sorted material list construct the list of unique materials
// and a map to convert old material indices into indices that can be
// used with the uniquified material list.
unsigned int uniqueMaterialCount = 0;
for (i = 0; i < indexedMaterials.size(); i++)
{
if (i == 0 || indexedMaterials[i] != indexedMaterials[i - 1])
{
uniqueMaterialCount++;
uniqueMaterials.push_back(indexedMaterials[i].material);
}
else
{
duplicateMaterials.push_back(i);
}
materialMap[indexedMaterials[i].originalIndex] = uniqueMaterialCount - 1;
}
// Remap all the material indices in the model. Even if no materials have
// been eliminated we've still sorted them by opacity, which is useful
// when reordering meshes so that translucent ones are rendered last.
for (const auto mesh : meshes)
mesh->remapMaterials(materialMap);
for (const auto i : duplicateMaterials)
delete indexedMaterials[i].material;
materials = uniqueMaterials;
}
void
Model::determineOpacity()
{
for (unsigned int i = 0; i < materials.size(); i++)
{
if ((materials[i]->opacity > 0.01f && materials[i]->opacity < 1.0f) ||
materials[i]->blend == Material::AdditiveBlend)
{
opaque = false;
return;
}
}
opaque = true;
}
bool
Model::usesTextureType(Material::TextureSemantic t) const
{
return textureUsage[static_cast<int>(t)];
}
class MeshComparatorAdapter
{
public:
MeshComparatorAdapter(const Model::MeshComparator& c) :
comparator(c)
{
}
bool operator()(const Mesh* a, const Mesh* b) const
{
return comparator(*a, *b);
}
private:
const Model::MeshComparator& comparator;
};
// Look at the material used by last primitive group in the mesh for the
// opacity of the whole model. This is a very crude way to check the opacity
// of a mesh and misses many cases.
static unsigned int
getMeshMaterialIndex(const Mesh& mesh)
{
if (mesh.getGroupCount() == 0)
return 0;
return mesh.getGroup(mesh.getGroupCount() - 1)->materialIndex;
}
bool
Model::OpacityComparator::operator()(const Mesh& a, const Mesh& b) const
{
// Because materials are sorted by opacity, we can just compare
// the material index.
return getMeshMaterialIndex(a) > getMeshMaterialIndex(b);
}
void
Model::sortMeshes(const MeshComparator& comparator)
{
// Sort submeshes by material; if materials have been uniquified,
// then the submeshes will be ordered so that opaque ones are first.
for (const auto mesh : meshes)
mesh->aggregateByMaterial();
// Sort the meshes so that completely opaque ones are first
sort(meshes.begin(), meshes.end(), MeshComparatorAdapter(comparator));
}