-
Notifications
You must be signed in to change notification settings - Fork 49
/
ExportablePrimitive.cpp
214 lines (178 loc) · 8.32 KB
/
ExportablePrimitive.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
#include "externals.h"
#include "Arguments.h"
#include "ExportablePrimitive.h"
#include "ExportableResources.h"
#include "MeshRenderables.h"
#include "accessors.h"
using namespace GLTF::Constants;
using namespace coveo::linq;
namespace Semantic {
inline std::string glTFattributeName(const Kind s, const int setIndex) {
// NOTE: Although Maya has multiple tangent sets, glTW only accepts one.
// Need to dig deeper to understand this correctly.
switch (s) {
case POSITION:
return std::string("POSITION");
case NORMAL:
return std::string("NORMAL");
case TANGENT:
return std::string("TANGENT");
case COLOR:
return std::string("COLOR_") + std::to_string(setIndex);
case TEXCOORD:
return std::string("TEXCOORD_") + std::to_string(setIndex);
case WEIGHTS:
return std::string("WEIGHTS_") + std::to_string(setIndex);
case JOINTS:
return std::string("JOINTS_") + std::to_string(setIndex);
default:
assert(false);
return "UNKNOWN";
}
}
} // namespace Semantic
ExportablePrimitive::ExportablePrimitive(const std::string &name,
const VertexBuffer &vertexBuffer,
ExportableResources &resources,
ExportableMaterial *material) {
auto &args = resources.arguments();
glPrimitive.mode = GLTF::Primitive::TRIANGLES;
glPrimitive.material = material->glMaterial();
auto &vertexIndices = vertexBuffer.indices;
const auto indicesName = args.makeName(name + "/indices");
if (args.force32bitIndices ||
vertexBuffer.maxIndex() > std::numeric_limits<uint16_t>::max()) {
// Use 32-bit indices
glIndices = contiguousAccessor(
indicesName, GLTF::Accessor::Type::SCALAR, WebGL::UNSIGNED_INT,
WebGL::ELEMENT_ARRAY_BUFFER, span(vertexIndices), 1);
glPrimitive.indices = glIndices.get();
} else {
// Use 16-bit indices
std::vector<uint16_t> shortIndices(vertexIndices.size());
std::copy(vertexIndices.begin(), vertexIndices.end(),
shortIndices.begin());
glIndices = contiguousAccessor(
indicesName, GLTF::Accessor::Type::SCALAR, WebGL::UNSIGNED_SHORT,
WebGL::ELEMENT_ARRAY_BUFFER, span(shortIndices), 1);
glPrimitive.indices = glIndices.get();
}
auto componentsPerShapeIndex =
from(vertexBuffer.componentsMap) |
group_by([](auto &pair) { return pair.first.shapeIndex; }) |
to_vector();
// Allocate a glTF morph-target for each blend-shape
const auto shapeCount = componentsPerShapeIndex.size();
if (shapeCount > 1) {
glTargetTable.reserve(shapeCount - 1);
for (size_t i = 1; i < shapeCount; ++i) {
auto glTarget = std::make_unique<GLTF::Primitive::Target>();
glPrimitive.targets.emplace_back(glTarget.get());
glTargetTable.emplace_back(move(glTarget));
}
}
auto mainShapeSemanticSet = args.meshPrimitiveAttributes;
// Don't add texture coordinates if no textures are used, if option is
// enabled
if (args.excludeUnusedTexcoord && !material->hasTextures())
mainShapeSemanticSet.set(Semantic::TEXCOORD, false);
const auto blendShapeSemanticSet =
args.blendPrimitiveAttributes & mainShapeSemanticSet;
for (auto &&group : componentsPerShapeIndex) {
const auto shapeIndex = group.first;
auto &glAttributes =
shapeIndex.isMainShapeIndex()
? glPrimitive.attributes
: glTargetTable.at(shapeIndex.targetIndex())->attributes;
auto &semanticSet = shapeIndex.isMainShapeIndex()
? mainShapeSemanticSet
: blendShapeSemanticSet;
for (auto &&pair : group.second) {
auto &slot = pair.first;
if (semanticSet.test(slot.semantic)) {
auto attributeSlot =
glTFattributeName(slot.semantic, slot.setIndex);
std::string accessorName;
if (!args.disableNameAssignment) {
std::stringstream ss;
ss << name;
if (slot.shapeIndex.isBlendShapeIndex()) {
ss << "/target#" +
std::to_string(slot.shapeIndex.targetIndex());
}
ss << "/vertices/" << attributeSlot;
accessorName = ss.str();
}
auto accessor = contiguousElementAccessor(
accessorName, slot.semantic, slot.shapeIndex, pair.second);
glAttributes[attributeSlot] = accessor.get();
glAccessors.emplace_back(std::move(accessor));
}
}
}
}
ExportablePrimitive::ExportablePrimitive(const std::string &name,
const VertexBuffer &vertexBuffer,
ExportableResources &resources,
const Semantic::Kind debugSemantic,
const ShapeIndex &debugShapeIndex,
const double debugLineLength,
const Color debugLineColor) {
auto &args = resources.arguments();
glPrimitive.mode = GLTF::Primitive::LINES;
const auto positionSlot =
VertexSlot(ShapeIndex::main(), Semantic::POSITION, 0);
const auto vectorSlot = VertexSlot(ShapeIndex::main(), debugSemantic, 0);
const auto positions =
reinterpret_span<Position>(vertexBuffer.componentsMap.at(positionSlot));
const auto vectorComponents =
reinterpret_span<float>(vertexBuffer.componentsMap.at(vectorSlot));
const auto vectorDimension = dimension(debugSemantic, debugShapeIndex);
const auto lineCount = positions.size();
const auto elementCount = lineCount * 2;
std::vector<uint16_t> lineIndices(elementCount);
std::vector<Position> linePoints(elementCount);
std::vector<Color> lineColors(elementCount);
iota(lineIndices.begin(), lineIndices.end(), 0);
fill(lineColors.begin(), lineColors.end(), debugLineColor);
// Add a line from each point
const auto length = static_cast<float>(debugLineLength);
for (auto lineIndex = 0; lineIndex < lineCount; ++lineIndex) {
const auto offset = lineIndex * 2;
const auto vectorOffset = vectorDimension * lineIndex;
const auto vx = vectorComponents[vectorOffset + 0];
const auto vy = vectorComponents[vectorOffset + 1];
const auto vz = vectorComponents[vectorOffset + 2];
auto point = positions[lineIndex];
linePoints[offset + 0] = point;
point[0] += vx * length;
point[1] += vy * length;
point[2] += vz * length;
linePoints[offset + 1] = point;
}
glIndices =
contiguousAccessor(args.makeName(name + "/debug/indices"),
GLTF::Accessor::Type::SCALAR, WebGL::UNSIGNED_SHORT,
WebGL::ELEMENT_ARRAY_BUFFER, span(lineIndices), 1);
glPrimitive.indices = glIndices.get();
auto pointAccessor = contiguousElementAccessor(
args.makeName(name + "/debug/points"), Semantic::Kind::POSITION,
ShapeIndex::main(), reinterpret_span<byte>(linePoints));
glPrimitive.attributes[glTFattributeName(Semantic::Kind::POSITION, 0)] =
pointAccessor.get();
glAccessors.emplace_back(move(pointAccessor));
auto colorAccessor = contiguousElementAccessor(
args.makeName(name + "/debug/colors"), Semantic::Kind::COLOR,
ShapeIndex::main(), reinterpret_span<byte>(lineColors));
glPrimitive.attributes[glTFattributeName(Semantic::Kind::COLOR, 0)] =
colorAccessor.get();
glAccessors.emplace_back(move(colorAccessor));
}
ExportablePrimitive::~ExportablePrimitive() = default;
void ExportablePrimitive::getAllAccessors(
std::vector<GLTF::Accessor *> &accessors) const {
accessors.emplace_back(glIndices.get());
for (auto &&accessor : glAccessors) {
accessors.emplace_back(accessor.get());
}
}