diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2018-08-10 22:21:44 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-08-10 22:21:44 -0700 |
| commit | 56d8a752d84e984afab20de5980edf10fe6c06f5 (patch) | |
| tree | eb1491b940daebc6afd200202347191d77f76112 /tools/gfx/model.cpp | |
| parent | 73ff6907d723003d30e400f661876e7960de574f (diff) | |
Improve model-viewer support for lights (#626)
* Improve model-viewer support for lights
The main visible change here is that the model-viewer example supports
multiple light sources, with a basic UI for adding new light sources to
the scene, and for manipulating the ones that are there.
Along the way I also refactored the `IMaterial` decomposition to be a
bit less naive, while still only including a completely naive
Blinn-Phong implementation.
I also went ahead and spruced up the `cube.obj` file so that it has
multiple materials, although it is still a completely uninteresting
asset.
* Fixup: Windows SDK version
Diffstat (limited to 'tools/gfx/model.cpp')
| -rw-r--r-- | tools/gfx/model.cpp | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/tools/gfx/model.cpp b/tools/gfx/model.cpp index c8218102e..0173ce950 100644 --- a/tools/gfx/model.cpp +++ b/tools/gfx/model.cpp @@ -205,6 +205,12 @@ Result ModelLoader::load( std::vector<tinyobj::shape_t> objShapes; std::vector<tinyobj::material_t> objMaterials; + std::string baseDir; + if( auto lastSlash = strrchr(inputPath, '/') ) + { + baseDir = std::string(inputPath, lastSlash); + } + std::string diagnostics; bool shouldTriangulate = true; bool success = tinyobj::LoadObj( @@ -213,7 +219,7 @@ Result ModelLoader::load( &objMaterials, &diagnostics, inputPath, - nullptr, + baseDir.size() ? baseDir.c_str() : nullptr, shouldTriangulate); if(!diagnostics.empty()) @@ -238,6 +244,13 @@ Result ModelLoader::load( 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()) { @@ -416,6 +429,8 @@ Result ModelLoader::load( std::vector<void*> meshes; + void* defaultMaterial = nullptr; + for(auto& objShape : objShapes) { size_t objIndexCounter = 0; @@ -424,7 +439,21 @@ Result ModelLoader::load( { size_t objFaceIndex = objFaceCounter++; int faceMaterialID = objShape.mesh.material_ids[objFaceIndex]; - void* faceMaterial = materials[faceMaterialID]; + 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)) { |
