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
|
// model.cpp
#include "model.h"
#include "window.h"
#define TINYOBJLOADER_IMPLEMENTATION
#include "../../external/tinyobjloader/tiny_obj_loader.h"
#define STB_IMAGE_IMPLEMENTATION
#include "../../external/stb/stb_image.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "../../external/stb/stb_image_resize.h"
#include "../../external/glm/glm/glm.hpp"
#include "../../external/glm/glm/gtc/matrix_transform.hpp"
#include "../../external/glm/glm/gtc/constants.hpp"
#include <memory>
#include <unordered_map>
#include <unordered_set>
namespace gfx {
// TinyObj provides a tuple type that bundles up indices, but doesn't
// provide equality comparison or hashing for that type. We'd like
// to have a hash function so that we can unique indices.
//
// In the simplest case, we could define hashing and operator== operations
// directly on `tinobj::index_t`, but that would create problems if they
// revise their API.
//
// We will instead define our own wrapper type that supports equality
// comparisons.
//
struct ObjIndexKey
{
tinyobj::index_t index;
};
bool operator==(ObjIndexKey const& left, ObjIndexKey const& right)
{
return left.index.vertex_index == right.index.vertex_index
&& left.index.normal_index == right.index.normal_index
&& left.index.texcoord_index == right.index.texcoord_index;
}
struct Hasher
{
template<typename T>
void add(T const& v)
{
state ^= std::hash<T>()(v) + 0x9e3779b9 + (state << 6) + (state >> 2);
}
size_t state = 0;
};
struct SmoothingGroupVertexID
{
size_t smoothingGroup;
size_t positionID;
};
bool operator==(SmoothingGroupVertexID const& left, SmoothingGroupVertexID const& right)
{
return left.smoothingGroup == right.smoothingGroup
&& left.positionID == right.positionID;
}
}
namespace std
{
template<> struct hash<gfx::ObjIndexKey>
{
size_t operator()(gfx::ObjIndexKey const& key) const
{
gfx::Hasher hasher;
hasher.add(key.index.vertex_index);
hasher.add(key.index.normal_index);
hasher.add(key.index.texcoord_index);
return hasher.state;
}
};
template<> struct hash<gfx::SmoothingGroupVertexID>
{
size_t operator()(gfx::SmoothingGroupVertexID const& id) const
{
gfx::Hasher hasher;
hasher.add(id.smoothingGroup);
hasher.add(id.positionID);
return hasher.state;
}
};
}
namespace gfx
{
ComPtr<ITextureResource> loadTextureImage(
IDevice* device,
char const* path)
{
int extentX = 0;
int extentY = 0;
int originalChannelCount = 0;
int requestedChannelCount = 4; // force to 4-component result
stbi_uc* data = stbi_load(
path,
&extentX,
&extentY,
&originalChannelCount,
requestedChannelCount);
if(!data)
return nullptr;
int channelCount = requestedChannelCount ? requestedChannelCount : originalChannelCount;
Format format;
switch(channelCount)
{
default:
return nullptr;
case 4: format = Format::RGBA_Unorm_UInt8;
// TODO: handle other cases here if/when we stop forcing 4-component
// results when loading the image with stb_image.
}
std::vector<void*> subresourceInitData;
std::vector<ptrdiff_t> mipRowStrides;
ptrdiff_t stride = extentX * channelCount * sizeof(stbi_uc);
subresourceInitData.push_back(data);
mipRowStrides.push_back(stride);
// create down-sampled images for the different mip levels
bool generateMips = true;
if(generateMips)
{
int prevExtentX = extentX;
int prevExtentY = extentY;
stbi_uc* prevData = data;
int prevStride = int(stride);
for(;;)
{
if(prevExtentX == 1 && prevExtentY == 1)
break;
int newExtentX = prevExtentX / 2;
int newExtentY = prevExtentY / 2;
if(!newExtentX) newExtentX = 1;
if(!newExtentY) newExtentY = 1;
stbi_uc* newData = (stbi_uc*) malloc(newExtentX * newExtentY * channelCount * sizeof(stbi_uc));
int newStride = int(newExtentX * channelCount * sizeof(stbi_uc));
stbir_resize_uint8_srgb(
prevData, prevExtentX, prevExtentY, prevStride,
newData, newExtentX, newExtentY, newStride,
channelCount,
STBIR_ALPHA_CHANNEL_NONE,
STBIR_FLAG_ALPHA_PREMULTIPLIED);
subresourceInitData.push_back(newData);
mipRowStrides.push_back(newStride);
prevExtentX = newExtentX;
prevExtentY = newExtentY;
prevData = newData;
prevStride = newStride;
}
}
int mipCount = (int) mipRowStrides.size();
ITextureResource::Desc desc;
desc.init2D(IResource::Type::Texture2D, format, extentX, extentY, mipCount);
ITextureResource::Data initData;
initData.numSubResources = mipCount;
initData.numMips = mipCount;
initData.subResources = &subresourceInitData[0];
initData.mipRowStrides = &mipRowStrides[0];
auto texture =
device->createTextureResource(IResource::Usage::PixelShaderResource, desc, &initData);
free(data);
return texture;
}
static std::string makeString(const char* start, const char* end)
{
return std::string(start, size_t(end - start));
}
Result ModelLoader::load(
char const* inputPath,
void** outModel)
{
// TODO: need to actually allocate/load the data
tinyobj::attrib_t objVertexAttributes;
std::vector<tinyobj::shape_t> objShapes;
std::vector<tinyobj::material_t> objMaterials;
std::string baseDir;
if( auto lastSlash = strrchr(inputPath, '/') )
{
baseDir = makeString(inputPath, lastSlash);
}
std::string diagnostics;
bool shouldTriangulate = true;
bool success = tinyobj::LoadObj(
&objVertexAttributes,
&objShapes,
&objMaterials,
&diagnostics,
inputPath,
baseDir.size() ? baseDir.c_str() : nullptr,
shouldTriangulate);
if(!diagnostics.empty())
{
printf("%s", diagnostics.c_str());
}
if(!success)
{
return SLANG_FAIL;
}
// Translate each material imported by TinyObj into a format that
// we can actually use for rendering.
//
std::vector<void*> materials;
for(auto& objMaterial : objMaterials)
{
MaterialData materialData;
materialData.diffuseColor = glm::vec3(
objMaterial.diffuse[0],
objMaterial.diffuse[1],
objMaterial.diffuse[2]);
materialData.specularColor = glm::vec3(
objMaterial.specular[0],
objMaterial.specular[1],
objMaterial.specular[2]);
materialData.specularity = objMaterial.shininess;
// load any referenced textures here
if(objMaterial.diffuse_texname.length())
{
materialData.diffuseMap = loadTextureImage(
device,
objMaterial.diffuse_texname.c_str());
}
auto material = callbacks->createMaterial(materialData);
materials.push_back(material);
}
// Flip the winding order on all faces if we are asked to...
//
if(loadFlags & LoadFlag::FlipWinding)
{
for(auto& objShape : objShapes)
{
size_t objIndexCounter = 0;
size_t objFaceCounter = 0;
for(auto objFaceVertexCount : objShape.mesh.num_face_vertices)
{
size_t beginIndex = objIndexCounter;
size_t endIndex = beginIndex + objFaceVertexCount;
objIndexCounter = endIndex;
size_t halfCount = objFaceVertexCount / 2;
for(size_t ii = 0; ii < halfCount; ++ii)
{
std::swap(
objShape.mesh.indices[beginIndex + ii],
objShape.mesh.indices[endIndex - (ii + 1)]);
}
}
}
}
// Identify cases where a face has a vertex without a normal, and in that
// case remember that the given vertex needs to be "smoothed" as part of
// the smoothing group for that face. Note that it is possible for the
// same vertex (position) to be part of faces in distinct smoothing groups.
//
std::unordered_map<SmoothingGroupVertexID, size_t> smoothedVertexNormals;
size_t firstSmoothedNormalID = objVertexAttributes.normals.size() / 3;
size_t flatFaceCounter = 0;
for(auto& objShape : objShapes)
{
size_t objIndexCounter = 0;
size_t objFaceCounter = 0;
for(auto objFaceVertexCount : objShape.mesh.num_face_vertices)
{
const size_t flatFaceIndex = flatFaceCounter++;
const size_t objFaceIndex = objFaceCounter++;
size_t smoothingGroup = objShape.mesh.smoothing_group_ids[objFaceIndex];
if(!smoothingGroup)
{
smoothingGroup = ~flatFaceIndex;
}
for(size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex)
{
tinyobj::index_t& objIndex = objShape.mesh.indices[objIndexCounter++];
if(objIndex.normal_index < 0)
{
SmoothingGroupVertexID smoothVertexID;
smoothVertexID.positionID = objIndex.vertex_index;
smoothVertexID.smoothingGroup = smoothingGroup;
if(smoothedVertexNormals.find(smoothVertexID) == smoothedVertexNormals.end())
{
size_t normalID = objVertexAttributes.normals.size() / 3;
objVertexAttributes.normals.push_back(0);
objVertexAttributes.normals.push_back(0);
objVertexAttributes.normals.push_back(0);
smoothedVertexNormals.insert(std::make_pair(smoothVertexID, normalID));
objIndex.normal_index = int(normalID);
}
}
}
}
}
//
// Having identified which vertices we need to smooth, we will make another
// pass to compute face normals and apply them to the vertices that belong
// to the same smoothing group.
//
flatFaceCounter = 0;
for(auto& objShape : objShapes)
{
size_t objIndexCounter = 0;
size_t objFaceCounter = 0;
for(auto objFaceVertexCount : objShape.mesh.num_face_vertices)
{
const size_t flatFaceIndex = flatFaceCounter++;
const size_t objFaceIndex = objFaceCounter++;
size_t smoothingGroup = objShape.mesh.smoothing_group_ids[objFaceIndex];
if(!smoothingGroup)
{
smoothingGroup = ~flatFaceIndex;
}
glm::vec3 faceNormal;
if(objFaceVertexCount >= 3)
{
glm::vec3 v[3];
for(size_t objFaceVertex = 0; objFaceVertex < 3; ++objFaceVertex)
{
tinyobj::index_t objIndex = objShape.mesh.indices[objIndexCounter + objFaceVertex];
if(objIndex.vertex_index >= 0)
{
v[objFaceVertex] = glm::vec3(
objVertexAttributes.vertices[3 * objIndex.vertex_index + 0],
objVertexAttributes.vertices[3 * objIndex.vertex_index + 1],
objVertexAttributes.vertices[3 * objIndex.vertex_index + 2]);
}
}
faceNormal = cross(v[1] - v[0], v[2] - v[0]);
}
// Add this face normal to any to-be-smoothed vertex on the face.
for(size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex)
{
tinyobj::index_t objIndex = objShape.mesh.indices[objIndexCounter++];
SmoothingGroupVertexID smoothVertexID;
smoothVertexID.positionID = objIndex.vertex_index;
smoothVertexID.smoothingGroup = smoothingGroup;
auto ii = smoothedVertexNormals.find(smoothVertexID);
if(ii != smoothedVertexNormals.end())
{
size_t normalID = ii->second;
objVertexAttributes.normals[normalID * 3 + 0] += faceNormal.x;
objVertexAttributes.normals[normalID * 3 + 1] += faceNormal.y;
objVertexAttributes.normals[normalID * 3 + 2] += faceNormal.z;
}
}
}
}
//
// Once we've added all contributions from each smoothing group,
// we can normalize the normals to compute the area-weighted average.
//
size_t normalCount = objVertexAttributes.normals.size() / 3;
for(size_t ii = firstSmoothedNormalID; ii < normalCount; ++ii)
{
glm::vec3 normal = glm::vec3(
objVertexAttributes.normals[3 * ii + 0],
objVertexAttributes.normals[3 * ii + 1],
objVertexAttributes.normals[3 * ii + 2]);
normal = normalize(normal);
objVertexAttributes.normals[3 * ii + 0] = normal.x;
objVertexAttributes.normals[3 * ii + 1] = normal.y;
objVertexAttributes.normals[3 * ii + 2] = normal.z;
}
// TODO: we should sort the faces to group faces with
// the same material ID together, in case they weren't
// grouped in the original file.
// We need to undo the .obj indexing stuff so that we have
// standard position/normal/etc. data in a single flat array
std::unordered_map<ObjIndexKey, Index> mapObjIndexToFlatIndex;
std::vector<Vertex> flatVertices;
std::vector<Index> flatIndices;
MeshData* currentMesh = nullptr;
MeshData currentMeshStorage;
std::vector<void*> meshes;
void* defaultMaterial = nullptr;
for(auto& objShape : objShapes)
{
size_t objIndexCounter = 0;
size_t objFaceCounter = 0;
for(auto objFaceVertexCount : objShape.mesh.num_face_vertices)
{
size_t objFaceIndex = objFaceCounter++;
int faceMaterialID = objShape.mesh.material_ids[objFaceIndex];
void* faceMaterial = nullptr;
if( faceMaterialID < 0 )
{
if( !defaultMaterial )
{
MaterialData defaultMaterialData;
defaultMaterialData.diffuseColor = glm::vec3(0.5, 0.5, 0.5);
defaultMaterial = callbacks->createMaterial(defaultMaterialData);
}
faceMaterial = defaultMaterial;
}
else
{
faceMaterial = materials[faceMaterialID];
}
if(!currentMesh || (faceMaterial != currentMesh->material))
{
// finish old mesh.
if(currentMesh)
{
meshes.push_back(callbacks->createMesh(*currentMesh));
}
// Need to start a new mesh.
currentMesh = ¤tMeshStorage;
currentMesh->material = faceMaterial;
currentMesh->firstIndex = (int)flatIndices.size();
currentMesh->indexCount = 0;
}
for(size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex)
{
tinyobj::index_t objIndex = objShape.mesh.indices[objIndexCounter++];
ObjIndexKey objIndexKey; objIndexKey.index = objIndex;
Index flatIndex = Index(-1);
auto iter = mapObjIndexToFlatIndex.find(objIndexKey);
if(iter != mapObjIndexToFlatIndex.end())
{
flatIndex = iter->second;
}
else
{
Vertex flatVertex;
if(objIndex.vertex_index >= 0)
{
flatVertex.position = scale * glm::vec3(
objVertexAttributes.vertices[3 * objIndex.vertex_index + 0],
objVertexAttributes.vertices[3 * objIndex.vertex_index + 1],
objVertexAttributes.vertices[3 * objIndex.vertex_index + 2]);
}
if(objIndex.normal_index >= 0)
{
flatVertex.normal = glm::vec3(
objVertexAttributes.normals[3 * objIndex.normal_index + 0],
objVertexAttributes.normals[3 * objIndex.normal_index + 1],
objVertexAttributes.normals[3 * objIndex.normal_index + 2]);
}
if(objIndex.texcoord_index >= 0)
{
flatVertex.uv = glm::vec2(
objVertexAttributes.texcoords[2 * objIndex.texcoord_index + 0],
objVertexAttributes.texcoords[2 * objIndex.texcoord_index + 1]);
}
flatIndex = uint32_t(flatVertices.size());
mapObjIndexToFlatIndex.insert(std::make_pair(objIndexKey, flatIndex));
flatVertices.push_back(flatVertex);
}
flatIndices.push_back(flatIndex);
currentMesh->indexCount++;
}
}
}
// finish last mesh.
if(currentMesh)
{
meshes.push_back(callbacks->createMesh(*currentMesh));
}
ModelData modelData;
modelData.vertexCount = (int)flatVertices.size();
modelData.indexCount = (int)flatIndices.size();
modelData.meshCount = int(meshes.size());
modelData.meshes = meshes.data();
IBufferResource::Desc vertexBufferDesc;
vertexBufferDesc.init(modelData.vertexCount * sizeof(Vertex));
vertexBufferDesc.setDefaults(IResource::Usage::VertexBuffer);
modelData.vertexBuffer = device->createBufferResource(
IResource::Usage::VertexBuffer,
vertexBufferDesc,
flatVertices.data());
if(!modelData.vertexBuffer) return SLANG_FAIL;
IBufferResource::Desc indexBufferDesc;
indexBufferDesc.init(modelData.indexCount * sizeof(Index));
vertexBufferDesc.setDefaults(IResource::Usage::IndexBuffer);
modelData.indexBuffer = device->createBufferResource(
IResource::Usage::IndexBuffer,
indexBufferDesc,
flatIndices.data());
if(!modelData.indexBuffer) return SLANG_FAIL;
*outModel = callbacks->createModel(modelData);
return SLANG_OK;
}
} // gfx
|