forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh.cpp
More file actions
776 lines (646 loc) · 21.6 KB
/
Copy pathmesh.cpp
File metadata and controls
776 lines (646 loc) · 21.6 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
// mesh.cpp
//
// Copyright (C) 2004-2010, the 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 "mesh.h"
#include <cassert>
#include <iostream>
#include <algorithm>
#include <Eigen/Core>
#include <Eigen/Geometry>
using namespace cmod;
using namespace Eigen;
using namespace std;
static size_t VertexAttributeFormatSizes[Mesh::FormatMax] =
{
4, // Float1
8, // Float2
12, // Float3
16, // Float4,
4, // UByte4
};
Mesh::VertexDescription::VertexDescription(unsigned int _stride,
unsigned int _nAttributes,
VertexAttribute* _attributes) :
stride(_stride),
nAttributes(_nAttributes),
attributes(nullptr)
{
if (nAttributes != 0)
{
attributes = new VertexAttribute[nAttributes];
for (unsigned int i = 0; i < nAttributes; i++)
attributes[i] = _attributes[i];
buildSemanticMap();
}
}
Mesh::VertexDescription::VertexDescription(const VertexDescription& desc) :
stride(desc.stride),
nAttributes(desc.nAttributes),
attributes(nullptr)
{
if (nAttributes != 0)
{
attributes = new VertexAttribute[nAttributes];
for (unsigned int i = 0; i < nAttributes; i++)
attributes[i] = desc.attributes[i];
buildSemanticMap();
}
}
Mesh::VertexDescription&
Mesh::VertexDescription::operator=(const Mesh::VertexDescription& desc)
{
if (this == &desc)
return *this;
if (nAttributes < desc.nAttributes)
{
delete[] attributes;
attributes = new VertexAttribute[desc.nAttributes];
}
nAttributes = desc.nAttributes;
stride = desc.stride;
for (unsigned int i = 0; i < nAttributes; i++)
attributes[i] = desc.attributes[i];
clearSemanticMap();
buildSemanticMap();
return *this;
}
// TODO: This should be called in the constructor; we should start using
// exceptions in Celestia.
bool
Mesh::VertexDescription::validate() const
{
for (unsigned int i = 0; i < nAttributes; i++)
{
VertexAttribute& attr = attributes[i];
// Validate the attribute
if (attr.semantic >= SemanticMax || attr.format >= FormatMax)
return false;
if (attr.offset % 4 != 0)
return false;
if (attr.offset + VertexAttributeFormatSizes[attr.format] > stride)
return false;
// TODO: check for repetition of attributes
// if (vertexAttributeMap[attr->semantic].format != InvalidFormat)
// return false;
}
return true;
}
Mesh::VertexDescription Mesh::VertexDescription::appendingAttributes(const VertexAttribute* newAttributes, int count) const
{
unsigned int totalAttributeCount = nAttributes + count;
VertexAttribute* allAttributes = new VertexAttribute[totalAttributeCount];
memcpy(allAttributes, attributes, sizeof(VertexAttribute) * nAttributes);
unsigned int newStride = stride;
for (int i = 0; i < count; ++i)
{
VertexAttribute attribute = newAttributes[i];
allAttributes[nAttributes + i] = attribute;
newStride += Mesh::getVertexAttributeSize(attribute.format);
}
memcpy(allAttributes + nAttributes, newAttributes, sizeof(VertexAttribute) * count);
VertexDescription desc = VertexDescription(newStride, totalAttributeCount, allAttributes);
delete[] allAttributes;
return desc;
}
Mesh::VertexDescription::~VertexDescription()
{
delete[] attributes;
}
void
Mesh::VertexDescription::buildSemanticMap()
{
for (unsigned int i = 0; i < nAttributes; i++)
semanticMap[attributes[i].semantic] = attributes[i];
}
void
Mesh::VertexDescription::clearSemanticMap()
{
for (auto& i : semanticMap)
i = VertexAttribute();
}
//Mesh::PrimitiveGroup::~PrimitiveGroup()
//{
// TODO: probably should free index list; need to sort out
// ownership issues.
//}
unsigned int
Mesh::PrimitiveGroup::getPrimitiveCount() const
{
switch (prim)
{
case TriList:
return nIndices / 3;
case TriStrip:
case TriFan:
return nIndices - 2;
case LineList:
return nIndices / 2;
case LineStrip:
return nIndices - 2;
case PointList:
case SpriteList:
return nIndices;
default:
// Invalid value
return 0;
}
}
Mesh::~Mesh()
{
for (const auto group : groups)
delete group;
// TODO: this is just to cast away void* and shut up GCC warnings;
// should probably be static_cast<VertexList::VertexPart*>
delete[] static_cast<char*>(vertices);
delete vbResource;
}
void
Mesh::setVertices(unsigned int _nVertices, void* vertexData)
{
if (vertexData == vertices)
return;
// TODO: this is just to cast away void* and shut up GCC warnings;
// should probably be static_cast<VertexList::VertexPart*>
delete[] static_cast<char*>(vertices);
nVertices = _nVertices;
vertices = vertexData;
}
bool
Mesh::setVertexDescription(const VertexDescription& desc)
{
if (!desc.validate())
return false;
vertexDesc = desc;
return true;
}
const Mesh::VertexDescription& Mesh::getVertexDescription() const
{
return vertexDesc;
}
const Mesh::PrimitiveGroup*
Mesh::getGroup(unsigned int index) const
{
if (index >= groups.size())
return nullptr;
return groups[index];
}
Mesh::PrimitiveGroup*
Mesh::getGroup(unsigned int index)
{
if (index >= groups.size())
return nullptr;
return groups[index];
}
unsigned int
Mesh::addGroup(PrimitiveGroup* group)
{
groups.push_back(group);
return groups.size();
}
Mesh::PrimitiveGroup* Mesh::createLinePrimitiveGroup(bool lineStrip, unsigned int nIndices, index32* indices)
{
// Transform LINE_STRIP/LINES to triangle vertices
int transformedVertCount = lineStrip ? (nIndices - 1) * 4 : nIndices * 2;
// Get information of the position attributes
auto positionAttributes = vertexDesc.getAttribute(Position);
int positionSize = getVertexAttributeSize(positionAttributes.format);
int positionOffset = positionAttributes.offset;
int originalStride = vertexDesc.stride;
// Add another position (next line end), and scale factor
// ORIGINAL ATTRIBUTES | NextVCoordAttributeIndex | ScaleFactorAttributeIndex
int stride = originalStride + positionSize + sizeof(float);
// Get line count
int lineCount = lineStrip ? nIndices - 1 : nIndices / 2;
int lineIndexCount = 6 * lineCount;
int lineVertexCount = 4 * lineCount;
// Create buffer to hold the transformed vertices/indices
unsigned char* data = new unsigned char[stride * lineVertexCount];
index32* newIndices = new index32[lineIndexCount];
unsigned char* originalData = (unsigned char *)vertices;
auto ptr = data;
for (int i = 0; i < lineCount; ++i)
{
int thisIndex = indices[lineStrip ? i : i * 2];
int nextIndex = indices[lineStrip ? i + 1 : i * 2 + 1];
unsigned char* origThisVertLoc = originalData + thisIndex * originalStride;
unsigned char* origNextVertLoc = originalData + nextIndex * originalStride;
float *ff = (float *)origThisVertLoc;
float *ffn = (float *)origNextVertLoc;
// Fill the info for the 4 vertices
memcpy(ptr, origThisVertLoc, originalStride);
memcpy(ptr + originalStride, origNextVertLoc + positionOffset, positionSize);
*(float *)&ptr[originalStride + positionSize] = -0.5f;
ptr += stride;
memcpy(ptr, origThisVertLoc, originalStride);
memcpy(ptr + originalStride, origNextVertLoc + positionOffset, positionSize);
*(float *)&ptr[originalStride + positionSize] = 0.5f;
ptr += stride;
memcpy(ptr, origNextVertLoc, originalStride);
memcpy(ptr + originalStride, origThisVertLoc + positionOffset, positionSize);
*(float *)&ptr[originalStride + positionSize] = -0.5f;
ptr += stride;
memcpy(ptr, origNextVertLoc, originalStride);
memcpy(ptr + originalStride, origThisVertLoc + positionOffset, positionSize);
*(float *)&ptr[originalStride + positionSize] = 0.5f;
ptr += stride;
int lineIndex = 6 * i;
index32 newIndex = 4 * i;
// Fill info for the 6 indices
newIndices[lineIndex] = newIndex;
newIndices[lineIndex + 1] = newIndex + 1;
newIndices[lineIndex + 2] = newIndex + 2;
newIndices[lineIndex + 3] = newIndex + 2;
newIndices[lineIndex + 4] = newIndex + 3;
newIndices[lineIndex + 5] = newIndex;
}
array<VertexAttribute, 2> newAttributes = {
VertexAttribute(NextPosition, positionAttributes.format, originalStride),
VertexAttribute(ScaleFactor, Float1, originalStride + positionSize),
};
auto* g = new PrimitiveGroup();
g->vertexOverride = data;
g->vertexCountOverride = lineVertexCount;
g->vertexDescriptionOverride = vertexDesc.appendingAttributes(newAttributes.data(), 2);
g->indicesOverride = newIndices;
g->nIndicesOverride = lineIndexCount;
g->primOverride = PrimitiveGroupType::TriList;
return g;
}
unsigned int
Mesh::addGroup(PrimitiveGroupType prim,
unsigned int materialIndex,
unsigned int nIndices,
index32* indices)
{
PrimitiveGroup* g;
if (prim == LineStrip || prim == LineList)
{
g = createLinePrimitiveGroup(prim == LineStrip, nIndices, indices);
}
else
{
g = new PrimitiveGroup;
g->vertexOverride = nullptr;
g->vertexCountOverride = 0;
g->indicesOverride = nullptr;
g->nIndicesOverride = 0;
g->primOverride = prim;
}
g->nIndices = nIndices;
g->indices = indices;
g->prim = prim;
g->materialIndex = materialIndex;
return addGroup(g);
}
unsigned int
Mesh::getGroupCount() const
{
return groups.size();
}
void
Mesh::clearGroups()
{
for (const auto group : groups)
delete group;
groups.clear();
}
const string&
Mesh::getName() const
{
return name;
}
void
Mesh::setName(const string& _name)
{
name = _name;
}
void
Mesh::remapIndices(const vector<index32>& indexMap)
{
for (auto group : groups)
{
for (index32 i = 0; i < group->nIndices; i++)
{
group->indices[i] = indexMap[group->indices[i]];
}
}
}
void
Mesh::remapMaterials(const vector<unsigned int>& materialMap)
{
for (auto group : groups)
group->materialIndex = materialMap[group->materialIndex];
}
struct PrimitiveGroupComparator
{
bool operator()(const Mesh::PrimitiveGroup* g0, const Mesh::PrimitiveGroup* g1) const
{
return g0->materialIndex < g1->materialIndex;
}
};
void
Mesh::aggregateByMaterial()
{
sort(groups.begin(), groups.end(), PrimitiveGroupComparator());
}
bool
Mesh::pick(const Vector3d& rayOrigin, const Vector3d& rayDirection, PickResult* result) const
{
double maxDistance = 1.0e30;
double closest = maxDistance;
// Pick will automatically fail without vertex positions--no reasonable
// mesh should lack these.
if (vertexDesc.getAttribute(Position).semantic != Position ||
vertexDesc.getAttribute(Position).format != Float3)
{
return false;
}
unsigned int posOffset = vertexDesc.getAttribute(Position).offset;
auto* vdata = reinterpret_cast<char*>(vertices);
// Iterate over all primitive groups in the mesh
for (const auto group : groups)
{
Mesh::PrimitiveGroupType primType = group->prim;
index32 nIndices = group->nIndices;
// Only attempt to compute the intersection of the ray with triangle
// groups.
if ((primType == TriList || primType == TriStrip || primType == TriFan) &&
(nIndices >= 3) &&
!(primType == TriList && nIndices % 3 != 0))
{
unsigned int primitiveIndex = 0;
index32 index = 0;
index32 i0 = group->indices[0];
index32 i1 = group->indices[1];
index32 i2 = group->indices[2];
// Iterate over the triangles in the primitive group
do
{
// Get the triangle vertices v0, v1, and v2
Vector3d v0 = Map<Vector3f>(reinterpret_cast<float*>(vdata + i0 * vertexDesc.stride + posOffset)).cast<double>();
Vector3d v1 = Map<Vector3f>(reinterpret_cast<float*>(vdata + i1 * vertexDesc.stride + posOffset)).cast<double>();
Vector3d v2 = Map<Vector3f>(reinterpret_cast<float*>(vdata + i2 * vertexDesc.stride + posOffset)).cast<double>();
// Compute the edge vectors e0 and e1, and the normal n
Vector3d e0 = v1 - v0;
Vector3d e1 = v2 - v0;
Vector3d n = e0.cross(e1);
// c is the cosine of the angle between the ray and triangle normal
double c = n.dot(rayDirection);
// If the ray is parallel to the triangle, it either misses the
// triangle completely, or is contained in the triangle's plane.
// If it's contained in the plane, we'll still call it a miss.
if (c != 0.0)
{
double t = (n.dot(v0 - rayOrigin)) / c;
if (t < closest && t > 0.0)
{
double m00 = e0.dot(e0);
double m01 = e0.dot(e1);
double m10 = e1.dot(e0);
double m11 = e1.dot(e1);
double det = m00 * m11 - m01 * m10;
if (det != 0.0)
{
Vector3d p = rayOrigin + rayDirection * t;
Vector3d q = p - v0;
double q0 = e0.dot(q);
double q1 = e1.dot(q);
double d = 1.0 / det;
double s0 = (m11 * q0 - m01 * q1) * d;
double s1 = (m00 * q1 - m10 * q0) * d;
if (s0 >= 0.0 && s1 >= 0.0 && s0 + s1 <= 1.0)
{
closest = t;
if (result)
{
result->group = group;
result->primitiveIndex = primitiveIndex;
result->distance = closest;
}
}
}
}
}
// Get the indices for the next triangle
if (primType == TriList)
{
index += 3;
if (index < nIndices)
{
i0 = group->indices[index + 0];
i1 = group->indices[index + 1];
i2 = group->indices[index + 2];
}
}
else if (primType == TriStrip)
{
index += 1;
if (index < nIndices)
{
i0 = i1;
i1 = i2;
i2 = group->indices[index];
// TODO: alternate orientation of triangles in a strip
}
}
else // primType == TriFan
{
index += 1;
if (index < nIndices)
{
index += 1;
i1 = i2;
i2 = group->indices[index];
}
}
primitiveIndex++;
} while (index < nIndices);
}
}
return closest != maxDistance;
}
bool
Mesh::pick(const Vector3d& rayOrigin, const Vector3d& rayDirection, double& distance) const
{
PickResult result;
bool hit = pick(rayOrigin, rayDirection, &result);
if (hit)
{
distance = result.distance;
}
return hit;
}
AlignedBox<float, 3>
Mesh::getBoundingBox() const
{
AlignedBox<float, 3> bbox;
// Return an empty box if there's no position info
if (vertexDesc.getAttribute(Position).format != Float3)
return bbox;
char* vdata = reinterpret_cast<char*>(vertices) + vertexDesc.getAttribute(Position).offset;
if (vertexDesc.getAttribute(PointSize).format == Float1)
{
// Handle bounding box calculation for point sprites. Unlike other
// primitives, point sprite vertices have a non-zero size.
int pointSizeOffset = (int) vertexDesc.getAttribute(PointSize).offset -
(int) vertexDesc.getAttribute(Position).offset;
for (unsigned int i = 0; i < nVertices; i++, vdata += vertexDesc.stride)
{
Vector3f center = Map<Vector3f>(reinterpret_cast<float*>(vdata));
float pointSize = (reinterpret_cast<float*>(vdata + pointSizeOffset))[0];
Vector3f offsetVec = Vector3f::Constant(pointSize);
AlignedBox<float, 3> pointbox(center - offsetVec, center + offsetVec);
bbox.extend(pointbox);
}
}
else
{
for (unsigned int i = 0; i < nVertices; i++, vdata += vertexDesc.stride)
bbox.extend(Map<Vector3f>(reinterpret_cast<float*>(vdata)));
}
return bbox;
}
void
Mesh::transform(const Vector3f& translation, float scale)
{
if (vertexDesc.getAttribute(Position).format != Float3)
return;
char* vdata = reinterpret_cast<char*>(vertices) + vertexDesc.getAttribute(Position).offset;
unsigned int i;
// Scale and translate the vertex positions
for (i = 0; i < nVertices; i++, vdata += vertexDesc.stride)
{
const Vector3f tv = (Map<Vector3f>(reinterpret_cast<float*>(vdata)) + translation) * scale;
Map<Vector3f>(reinterpret_cast<float*>(vdata)) = tv;
}
// Scale and translate the overriden vertex values
for (i = 0; i < getGroupCount(); i++)
{
PrimitiveGroup* group = getGroup(i);
char* vdata = reinterpret_cast<char*>(group->vertexOverride);
if (!vdata)
continue;
auto vertexDesc = group->vertexDescriptionOverride;
int positionOffset = vertexDesc.getAttribute(Position).offset;
int nextPositionOffset = vertexDesc.getAttribute(NextPosition).offset;
for (unsigned int j = 0; j < group->vertexCountOverride; j++, vdata += vertexDesc.stride)
{
Vector3f tv = (Map<Vector3f>(reinterpret_cast<float*>(vdata + positionOffset)) + translation) * scale;
Map<Vector3f>(reinterpret_cast<float*>(vdata + positionOffset)) = tv;
tv = (Map<Vector3f>(reinterpret_cast<float*>(vdata + nextPositionOffset)) + translation) * scale;
Map<Vector3f>(reinterpret_cast<float*>(vdata + nextPositionOffset)) = tv;
}
}
// Point sizes need to be scaled as well
if (vertexDesc.getAttribute(PointSize).format == Float1)
{
vdata = reinterpret_cast<char*>(vertices) + vertexDesc.getAttribute(PointSize).offset;
for (i = 0; i < nVertices; i++, vdata += vertexDesc.stride)
reinterpret_cast<float*>(vdata)[0] *= scale;
}
}
unsigned int
Mesh::getPrimitiveCount() const
{
unsigned int count = 0;
for (const auto group : groups)
count += group->getPrimitiveCount();
return count;
}
Mesh::PrimitiveGroupType
Mesh::parsePrimitiveGroupType(const string& name)
{
if (name == "trilist")
return TriList;
if (name == "tristrip")
return TriStrip;
if (name == "trifan")
return TriFan;
if (name == "linelist")
return LineList;
if (name == "linestrip")
return LineStrip;
if (name == "points")
return PointList;
if (name == "sprites")
return SpriteList;
else
return InvalidPrimitiveGroupType;
}
Mesh::VertexAttributeSemantic
Mesh::parseVertexAttributeSemantic(const string& name)
{
if (name == "position")
return Position;
if (name == "normal")
return Normal;
if (name == "color0")
return Color0;
if (name == "color1")
return Color1;
if (name == "tangent")
return Tangent;
if (name == "texcoord0")
return Texture0;
if (name == "texcoord1")
return Texture1;
if (name == "texcoord2")
return Texture2;
if (name == "texcoord3")
return Texture3;
if (name == "pointsize")
return PointSize;
return InvalidSemantic;
}
Mesh::VertexAttributeFormat
Mesh::parseVertexAttributeFormat(const string& name)
{
if (name == "f1")
return Float1;
if (name == "f2")
return Float2;
if (name == "f3")
return Float3;
if (name == "f4")
return Float4;
if (name == "ub4")
return UByte4;
return InvalidFormat;
}
Material::TextureSemantic
Mesh::parseTextureSemantic(const string& name)
{
if (name == "texture0")
return Material::DiffuseMap;
if (name == "normalmap")
return Material::NormalMap;
if (name == "specularmap")
return Material::SpecularMap;
if (name == "emissivemap")
return Material::EmissiveMap;
return Material::InvalidTextureSemantic;
}
unsigned int
Mesh::getVertexAttributeSize(VertexAttributeFormat fmt)
{
switch (fmt)
{
case Float1:
case UByte4:
return 4;
case Float2:
return 8;
case Float3:
return 12;
case Float4:
return 16;
default:
return 0;
}
}