Repository navigation
Expand file tree
/
Copy pathScene.cpp
More file actions
431 lines (362 loc) · 14.7 KB
/
Copy pathScene.cpp
File metadata and controls
431 lines (362 loc) · 14.7 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
#include "Scene.h"
#include <assimp/DefaultLogger.hpp>
#include <assimp/Importer.hpp>
#include <assimp/postprocess.h>
#include <assimp/scene.h>
#include <assimp/mesh.h>
#include <assimp/material.h>
#include <assimp/GltfMaterial.h>
#include <assimp/camera.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/trigonometric.hpp>
#include <bx/file.h>
#include <bimg/decode.h>
#include <algorithm>
bx::DefaultAllocator Scene::allocator;
Scene::Scene() :
skyColor({ 0.53f, 0.81f, 0.98f }), // https://en.wikipedia.org/wiki/Sky_blue#Light_sky_blue
ambientLight({ { 0.03f, 0.03f, 0.03f } })
{
clear();
Assimp::DefaultLogger::set(&logSource);
}
void Scene::init()
{
Mesh::PosNormalTangentTex0Vertex::init();
}
void Scene::clear()
{
if(loaded)
{
for(Mesh& mesh : meshes)
{
bgfx::destroy(mesh.vertexBuffer);
bgfx::destroy(mesh.indexBuffer);
mesh.vertexBuffer = BGFX_INVALID_HANDLE;
mesh.indexBuffer = BGFX_INVALID_HANDLE;
}
for(Material& mat : materials)
{
if(bgfx::isValid(mat.baseColorTexture))
{
bgfx::destroy(mat.baseColorTexture);
mat.baseColorTexture = BGFX_INVALID_HANDLE;
}
if(bgfx::isValid(mat.metallicRoughnessTexture))
{
bgfx::destroy(mat.metallicRoughnessTexture);
mat.metallicRoughnessTexture = BGFX_INVALID_HANDLE;
}
if(bgfx::isValid(mat.normalTexture))
{
bgfx::destroy(mat.normalTexture);
mat.normalTexture = BGFX_INVALID_HANDLE;
}
if(bgfx::isValid(mat.occlusionTexture))
{
bgfx::destroy(mat.occlusionTexture);
mat.occlusionTexture = BGFX_INVALID_HANDLE;
}
if(bgfx::isValid(mat.emissiveTexture))
{
bgfx::destroy(mat.emissiveTexture);
mat.emissiveTexture = BGFX_INVALID_HANDLE;
}
}
meshes.clear();
materials.clear();
pointLights.shutdown();
pointLights.lights.clear();
}
minBounds = maxBounds = { 0.0f, 0.0f, 0.0f };
center = { 0.0f, 0.0f, 0.0f };
diagonal = 0.0f;
camera = Camera();
loaded = false;
}
bool Scene::load(const char* file)
{
clear();
pointLights.init();
Assimp::Importer importer;
// Settings for aiProcess_SortByPType
// only take triangles or higher (polygons are triangulated during import)
importer.SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE, aiPrimitiveType_LINE | aiPrimitiveType_POINT);
// Settings for aiProcess_SplitLargeMeshes
// Limit vertices to 65k (we use 16-bit indices)
importer.SetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT, std::numeric_limits<uint16_t>::max());
unsigned int flags =
aiProcessPreset_TargetRealtime_Quality | // some optimizations and safety checks
aiProcess_OptimizeMeshes | // minimize number of meshes
aiProcess_PreTransformVertices | // apply node matrices
aiProcess_FixInfacingNormals | aiProcess_TransformUVCoords | // apply UV transformations
//aiProcess_FlipWindingOrder | // we cull clock-wise, keep the default CCW winding order
aiProcess_MakeLeftHanded | // we set GLM_FORCE_LEFT_HANDED and use left-handed bx matrix functions
aiProcess_FlipUVs; // bimg loads textures with flipped Y (top left is 0,0)
const aiScene* scene = nullptr;
try
{
scene = importer.ReadFile(file, flags);
}
catch(const std::exception& e)
{
Log->error("{}", e.what());
}
if(scene)
{
if(!(scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE))
{
for(unsigned int i = 0; i < scene->mNumMeshes; i++)
{
try
{
meshes.push_back(loadMesh(scene->mMeshes[i]));
}
catch(std::exception& e)
{
Log->warn("{}", e.what());
}
}
center = minBounds + (maxBounds - minBounds) / 2.0f;
glm::vec3 extent = glm::abs(maxBounds - minBounds);
diagonal = glm::sqrt(glm::dot(extent, extent));
char dir[bx::kMaxFilePath] = "";
bx::strCopy(dir, BX_COUNTOF(dir), bx::FilePath(file).getPath());
for(unsigned int i = 0; i < scene->mNumMaterials; i++)
{
try
{
materials.push_back(loadMaterial(scene->mMaterials[i], dir));
}
catch(std::exception& e)
{
// material not loaded, use default
// really only happens if there is no diffuse color
materials.push_back(Material());
Log->warn("{}", e.what());
}
}
// bring opaque meshes to the front so alpha blending works
// still need depth sorting for scenes with overlapping transparent meshes
std::partition(
meshes.begin(), meshes.end(), [this](const Mesh& mesh) { return !materials[mesh.material].blend; });
if(scene->HasCameras())
{
camera = loadCamera(scene->mCameras[0]);
}
else
{
Log->info("No camera");
camera.lookAt(center - glm::vec3(0.0f, 0.0f, diagonal / 2.0f), center, glm::vec3(0.0f, 1.0f, 0.0f));
camera.zFar = diagonal;
camera.zNear = camera.zFar / 50.0f;
}
loaded = true;
}
else
Log->error("Scene is incomplete or invalid");
}
return loaded;
}
Mesh Scene::loadMesh(const aiMesh* mesh)
{
if(mesh->mPrimitiveTypes != aiPrimitiveType_TRIANGLE)
throw std::runtime_error("Mesh has incompatible primitive type");
if(mesh->mNumVertices > (std::numeric_limits<uint16_t>::max() + 1u))
throw std::runtime_error("Mesh has too many vertices (> uint16_t::max + 1)");
constexpr size_t coords = 0;
bool hasTexture = mesh->mNumUVComponents[coords] == 2 && mesh->mTextureCoords[coords] != nullptr;
// vertices
uint32_t stride = Mesh::PosNormalTangentTex0Vertex::layout.getStride();
const bgfx::Memory* vertexMem = bgfx::alloc(mesh->mNumVertices * stride);
for(unsigned int i = 0; i < mesh->mNumVertices; i++)
{
unsigned int offset = i * stride;
Mesh::PosNormalTangentTex0Vertex& vertex = *(Mesh::PosNormalTangentTex0Vertex*)(vertexMem->data + offset);
aiVector3D pos = mesh->mVertices[i];
vertex.x = pos.x;
vertex.y = pos.y;
vertex.z = pos.z;
minBounds = glm::min(minBounds, { pos.x, pos.y, pos.z });
maxBounds = glm::max(maxBounds, { pos.x, pos.y, pos.z });
aiVector3D nrm = mesh->mNormals[i];
vertex.nx = nrm.x;
vertex.ny = nrm.y;
vertex.nz = nrm.z;
aiVector3D tan = mesh->mTangents[i];
vertex.tx = tan.x;
vertex.ty = tan.y;
vertex.tz = tan.z;
if(hasTexture)
{
aiVector3D uv = mesh->mTextureCoords[coords][i];
vertex.u = uv.x;
vertex.v = uv.y;
}
}
bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(vertexMem, Mesh::PosNormalTangentTex0Vertex::layout);
// indices (triangles)
const bgfx::Memory* iMem = bgfx::alloc(mesh->mNumFaces * 3 * sizeof(uint16_t));
uint16_t* indices = (uint16_t*)iMem->data;
for(unsigned int i = 0; i < mesh->mNumFaces; i++)
{
assert(mesh->mFaces[i].mNumIndices == 3);
indices[(3 * i) + 0] = (uint16_t)mesh->mFaces[i].mIndices[0];
indices[(3 * i) + 1] = (uint16_t)mesh->mFaces[i].mIndices[1];
indices[(3 * i) + 2] = (uint16_t)mesh->mFaces[i].mIndices[2];
}
bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(iMem);
return { vbh, ibh, mesh->mMaterialIndex };
}
Material Scene::loadMaterial(const aiMaterial* material, const char* dir)
{
Material out;
// technically there is a difference between MASK and BLEND mode
// but for our purposes it's enough if we sort properly
aiString alphaMode;
material->Get(AI_MATKEY_GLTF_ALPHAMODE, alphaMode);
aiString alphaModeOpaque;
alphaModeOpaque.Set("OPAQUE");
out.blend = alphaMode != alphaModeOpaque;
material->Get(AI_MATKEY_TWOSIDED, out.doubleSided);
// texture files
aiString fileBaseColor, fileMetallicRoughness, fileNormals, fileOcclusion, fileEmissive;
material->GetTexture(AI_MATKEY_BASE_COLOR_TEXTURE, &fileBaseColor);
// TODO AI_MATKEY_METALLIC_TEXTURE + AI_MATKEY_ROUGHNESS_TEXTURE
material->GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &fileMetallicRoughness);
material->GetTexture(aiTextureType_NORMALS, 0, &fileNormals);
// TODO aiTextureType_AMBIENT_OCCLUSION, what's the difference?
material->GetTexture(aiTextureType_LIGHTMAP, 0, &fileOcclusion);
material->GetTexture(aiTextureType_EMISSIVE, 0, &fileEmissive);
// diffuse
if(fileBaseColor.length > 0)
{
aiString pathBaseColor;
pathBaseColor.Set(dir);
pathBaseColor.Append(fileBaseColor.C_Str());
out.baseColorTexture = loadTexture(pathBaseColor.C_Str(), true /* sRGB */);
}
aiColor4D baseColorFactor;
if(AI_SUCCESS == material->Get(AI_MATKEY_BASE_COLOR, baseColorFactor))
out.baseColorFactor = { baseColorFactor.r, baseColorFactor.g, baseColorFactor.b, baseColorFactor.a };
out.baseColorFactor = glm::clamp(out.baseColorFactor, 0.0f, 1.0f);
// metallic/roughness
if(fileMetallicRoughness.length > 0)
{
aiString pathMetallicRoughness;
pathMetallicRoughness.Set(dir);
pathMetallicRoughness.Append(fileMetallicRoughness.C_Str());
out.metallicRoughnessTexture = loadTexture(pathMetallicRoughness.C_Str());
}
ai_real metallicFactor;
if(AI_SUCCESS == material->Get(AI_MATKEY_METALLIC_FACTOR, metallicFactor))
out.metallicFactor = glm::clamp(metallicFactor, 0.0f, 1.0f);
ai_real roughnessFactor;
if(AI_SUCCESS == material->Get(AI_MATKEY_ROUGHNESS_FACTOR, roughnessFactor))
out.roughnessFactor = glm::clamp(roughnessFactor, 0.0f, 1.0f);
// normal map
if(fileNormals.length > 0)
{
aiString pathNormals;
pathNormals.Set(dir);
pathNormals.Append(fileNormals.C_Str());
out.normalTexture = loadTexture(pathNormals.C_Str());
}
ai_real normalScale;
if(AI_SUCCESS == material->Get(AI_MATKEY_GLTF_TEXTURE_SCALE(aiTextureType_NORMALS, 0), normalScale))
out.normalScale = normalScale;
// occlusion texture
if(fileOcclusion == fileMetallicRoughness)
{
// some GLTF files combine metallic/roughness and occlusion values into one texture
// don't load it twice
out.occlusionTexture = out.metallicRoughnessTexture;
}
else if(fileOcclusion.length > 0)
{
aiString pathOcclusion;
pathOcclusion.Set(dir);
pathOcclusion.Append(fileOcclusion.C_Str());
out.occlusionTexture = loadTexture(pathOcclusion.C_Str());
}
ai_real occlusionStrength;
if(AI_SUCCESS == material->Get(AI_MATKEY_GLTF_TEXTURE_STRENGTH(aiTextureType_LIGHTMAP, 0), occlusionStrength))
out.occlusionStrength = glm::clamp(occlusionStrength, 0.0f, 1.0f);
// emissive texture
if(fileEmissive.length > 0)
{
aiString pathEmissive;
pathEmissive.Set(dir);
pathEmissive.Append(fileEmissive.C_Str());
out.emissiveTexture = loadTexture(pathEmissive.C_Str(), true /* sRGB */);
}
aiColor3D emissiveFactor;
if(AI_SUCCESS == material->Get(AI_MATKEY_COLOR_EMISSIVE, emissiveFactor))
out.emissiveFactor = { emissiveFactor.r, emissiveFactor.g, emissiveFactor.b };
out.emissiveFactor = glm::clamp(out.emissiveFactor, 0.0f, 1.0f);
return out;
}
Camera Scene::loadCamera(const aiCamera* camera)
{
float aspect = camera->mAspect == 0.0f ? 16.0f / 9.0f : camera->mAspect;
glm::vec3 pos(camera->mPosition.x, camera->mPosition.y, camera->mPosition.z);
glm::vec3 target(camera->mLookAt.x, camera->mLookAt.y, camera->mLookAt.z);
glm::vec3 up(camera->mUp.x, camera->mUp.y, camera->mUp.z);
Camera cam;
cam.lookAt(pos, target, up);
// convert horizontal half angle (radians) to vertical full angle (degrees)
cam.fov = glm::degrees(2.0f * glm::atan(glm::tan(camera->mHorizontalFOV) / aspect));
cam.zNear = camera->mClipPlaneNear;
cam.zFar = camera->mClipPlaneFar;
return cam;
}
bgfx::TextureHandle Scene::loadTexture(const char* file, bool sRGB)
{
void* data = nullptr;
uint32_t size = 0;
bx::FileReader reader;
bx::Error err;
if(bx::open(&reader, file, &err))
{
size = (uint32_t)bx::getSize(&reader);
data = BX_ALLOC(&allocator, size);
bx::read(&reader, data, size, &err);
bx::close(&reader);
}
if(!err.isOk())
{
BX_FREE(&allocator, data);
throw std::runtime_error(err.getMessage().getPtr());
}
bimg::ImageContainer* image = bimg::imageParse(&allocator, data, size);
if(image)
{
// the callback gets called when bgfx is done using the data (after 2 frames)
const bgfx::Memory* mem = bgfx::makeRef(
image->m_data,
image->m_size,
[](void*, void* data) { bimg::imageFree((bimg::ImageContainer*)data); },
image);
BX_FREE(&allocator, data);
// default wrap mode is repeat, there's no flag for it
uint64_t textureFlags = BGFX_TEXTURE_NONE | BGFX_SAMPLER_MIN_ANISOTROPIC | BGFX_SAMPLER_MAG_ANISOTROPIC;
if(sRGB)
textureFlags |= BGFX_TEXTURE_SRGB;
if(bgfx::isTextureValid(0, false, image->m_numLayers, (bgfx::TextureFormat::Enum)image->m_format, textureFlags))
{
bgfx::TextureHandle tex = bgfx::createTexture2D((uint16_t)image->m_width,
(uint16_t)image->m_height,
image->m_numMips > 1,
image->m_numLayers,
(bgfx::TextureFormat::Enum)image->m_format,
textureFlags,
mem);
//bgfx::setName(tex, file); // causes debug errors with DirectX SetPrivateProperty duplicate
return tex;
}
else
throw std::runtime_error("Unsupported image format");
}
BX_FREE(&allocator, data);
throw std::runtime_error(err.getMessage().getPtr());
}