diff --git a/src/application.cpp b/src/application.cpp index a8b46d1..7e8b0fb 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -103,7 +103,6 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph } /* default shaders */ -#if 0 { Shader::VertexParams vertParams{}; vertParams.has_normal = true; @@ -115,60 +114,10 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph shaderSettings.cull_backface = true; shaderSettings.write_z = true; shaderSettings.render_order = 0; - auto texturedShader = std::make_unique(renderer(), GetResourcePath("engine/shaders/standard.vert").c_str(), - GetResourcePath("engine/shaders/standard.frag").c_str(), shaderSettings); - GetResourceManager()->AddPersistent("builtin.standard", std::move(texturedShader)); - } -#endif - { - Shader::VertexParams vertParams{}; - vertParams.has_normal = true; - vertParams.has_tangent = true; - vertParams.has_uv0 = true; - Shader::ShaderSettings shaderSettings{}; - shaderSettings.vertexParams = vertParams; - shaderSettings.alpha_blending = false; - shaderSettings.cull_backface = true; - shaderSettings.write_z = true; - shaderSettings.render_order = 0; - auto fancyShader = std::make_unique(renderer(), GetResourcePath("engine/shaders/showNormals.vert").c_str(), - GetResourcePath("engine/shaders/showNormals.frag").c_str(), shaderSettings); + auto fancyShader = std::make_unique(renderer(), GetResourcePath("engine/shaders/fancy.vert").c_str(), + GetResourcePath("engine/shaders/fancy.frag").c_str(), shaderSettings); GetResourceManager()->AddPersistent("builtin.fancy", std::move(fancyShader)); } -#if 0 - { - Shader::VertexParams vertParams{}; - vertParams.has_normal = true; - vertParams.has_tangent = true; - vertParams.has_uv0 = true; - Shader::ShaderSettings shaderSettings{}; - shaderSettings.vertexParams = vertParams; - shaderSettings.alpha_blending = false; - shaderSettings.cull_backface = true; - shaderSettings.write_z = true; - shaderSettings.render_order = 0; - auto skyboxShader = std::make_unique(renderer(), GetResourcePath("engine/shaders/skybox.vert").c_str(), - GetResourcePath("engine/shaders/skybox.frag").c_str(), shaderSettings); - GetResourceManager()->AddPersistent("builtin.skybox", std::move(skyboxShader)); - } -#endif -#if 0 - { - Shader::VertexParams vertParams{}; - vertParams.has_normal = true; - vertParams.has_tangent = true; - vertParams.has_uv0 = true; - Shader::ShaderSettings shaderSettings{}; - shaderSettings.vertexParams = vertParams; - shaderSettings.alpha_blending = true; - shaderSettings.cull_backface = true; - shaderSettings.write_z = false; - shaderSettings.render_order = 1; - auto quadShader = std::make_unique(renderer(), GetResourcePath("engine/shaders/quad.vert").c_str(), - GetResourcePath("engine/shaders/quad.frag").c_str(), shaderSettings); - GetResourceManager()->AddPersistent("builtin.quad", std::move(quadShader)); - } -#endif /* default textures */ { diff --git a/src/util/gltf_loader.cpp b/src/util/gltf_loader.cpp index 327ab3b..ae98f4e 100644 --- a/src/util/gltf_loader.cpp +++ b/src/util/gltf_loader.cpp @@ -116,11 +116,23 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic) const tg::Scene& s = model.scenes.at(scene_index); + /* Find which texture indices point to normal maps. */ + /* This must be done for the Texture constructor to know to use srgb or not. */ + std::vector tex_index_is_normal_map(model.textures.size(), false); + for (const tg::Material& mat : model.materials) { + int texture_index = mat.normalTexture.index; + if (texture_index != -1) { + assert(texture_index < tex_index_is_normal_map.size()); + tex_index_is_normal_map[texture_index] = true; + } + } + /* load all textures found in the model */ std::vector> textures{}; textures.reserve(model.textures.size()); + size_t texture_idx = 0; for (const tg::Texture& texture : model.textures) { // find the image first // use missing texture image by default @@ -174,9 +186,10 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic) const tg::Image& image = model.images.at(texture.source); if (image.as_is == false && image.bits == 8 && image.component == 4 && image.pixel_type == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) { // create texture on GPU - // TODO: somehow detect if the textue should be srgb or not - textures.back() = std::make_shared(scene.app()->renderer(), image.image.data(), image.width, image.height, samplerInfo, true); + textures.back() = std::make_shared(scene.app()->renderer(), image.image.data(), image.width, image.height, samplerInfo, !tex_index_is_normal_map[texture_idx]); } + + ++texture_idx; } /* load all materials found in model */ @@ -231,7 +244,7 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic) materials.back()->SetAlbedoTexture(textures.at(material.pbrMetallicRoughness.baseColorTexture.index)); } else { - LOG_WARN("Material {} base color texture specifies a UV channel other than zero which is unsupported."); + LOG_WARN("Material {} base color texture specifies a UV channel other than zero which is unsupported.", material.name); LOG_WARN("Material will be created with a white base color"); } } @@ -260,7 +273,7 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic) materials.back()->SetNormalTexture(textures.at(material.normalTexture.index)); } else { - LOG_WARN("Material {} normal texture specifies a UV channel other than zero which is unsupported."); + LOG_WARN("Material {} normal texture specifies a UV channel other than zero which is unsupported.", material.name); LOG_WARN("Material will be created with no normal map"); } } @@ -560,12 +573,19 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic) if (node.mesh != -1) { const auto& primitives = primitive_arrays.at(node.mesh); int i = 0; - for (const EnginePrimitive& prim : primitives) { - auto prim_entity = scene.CreateEntity(std::string("_mesh") + std::to_string(i), e); - auto meshren = scene.AddComponent(prim_entity); - meshren->mesh = prim.mesh; - meshren->material = prim.material; - ++i; + if (primitives.size() == 1) { + auto meshren = scene.AddComponent(e); + meshren->mesh = primitives.front().mesh; + meshren->material = primitives.front().material; + } + else { + for (const EnginePrimitive& prim : primitives) { + auto prim_entity = scene.CreateEntity(std::string("_mesh") + std::to_string(i), e); + auto meshren = scene.AddComponent(prim_entity); + meshren->mesh = prim.mesh; + meshren->material = prim.material; + ++i; + } } } diff --git a/test/res/models/teapot.glb b/test/res/models/teapot.glb index 48951ad..7e89c6b 100644 Binary files a/test/res/models/teapot.glb and b/test/res/models/teapot.glb differ diff --git a/test/src/game.cpp b/test/src/game.cpp index a6ee3de..cd917d8 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -170,7 +170,7 @@ void PlayGame(GameSettings settings) { /* axes */ auto axes = engine::util::LoadMeshFromFile(scene2, app.GetResourcePath("models/MY_AXES.dae"), true); - scene2->GetComponent(axes)->position += glm::vec3{ 20.0f, 20.0f, 0.0f }; + scene2->GetComponent(axes)->position += glm::vec3{20.0f, 20.0f, 0.0f}; } { /* floor */ @@ -191,14 +191,15 @@ void PlayGame(GameSettings settings) scene2->GetComponent(teapot)->scale *= 10.0f; auto teapot2 = engine::util::LoadGLTF(*scene2, app.GetResourcePath("models/teapot.glb")); scene2->GetComponent(teapot2)->scale *= 10.0f; - scene2->GetComponent(teapot2)->position.y += 5.0f; + scene2->GetComponent(teapot2)->position.z += 10.0f; auto custom = scene2->AddComponent(teapot2); custom->onInit = [](void) { return; }; custom->onUpdate = [&](float dt) { - dt = 0.0f; - scene2->GetComponent(teapot2)->rotation *= glm::angleAxis(dt, glm::vec3{ 0.0f, 1.0f, 0.0f }); - scene2->GetComponent(teapot)->rotation *= glm::angleAxis(dt, glm::vec3{ 0.0f, 1.0f, 0.0f }); - }; + dt *= 0.1f; + scene2->GetComponent(teapot2)->rotation *= glm::angleAxis(dt, glm::vec3{0.0f, 1.0f, 0.0f}); + //scene2->GetComponent(teapot2)->rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f); + scene2->GetComponent(teapot)->rotation *= glm::angleAxis(dt, glm::vec3{0.0f, 1.0f, 0.0f}); + }; } { @@ -206,9 +207,15 @@ void PlayGame(GameSettings settings) } auto normalmaptest = engine::util::LoadGLTF(*scene2, app.GetResourcePath("models/normalmaptest.glb")); - scene2->GetComponent(normalmaptest)->position += glm::vec3{ -10.0f, 0.0f, 1.0f }; + scene2->GetComponent(normalmaptest)->position += glm::vec3{-10.0f, 0.0f, 1.0f}; auto normalmaptest_notang = engine::util::LoadGLTF(*scene2, app.GetResourcePath("models/normalmaptest_notang.glb")); - scene2->GetComponent(normalmaptest_notang)->position += glm::vec3{ -10.0f, 10.0f, 1.0f }; + scene2->GetComponent(normalmaptest_notang)->position += glm::vec3{-10.0f, 10.0f, 1.0f}; + auto custom = scene2->AddComponent(normalmaptest); + custom->onInit = [](void) { return; }; + custom->onUpdate = [&](float dt) { + scene2->GetComponent(normalmaptest)->rotation *= glm::angleAxis(dt, glm::vec3{ 0.0f, 0.0f, 1.0f }); + scene2->GetComponent(normalmaptest_notang)->rotation *= glm::angleAxis(dt, glm::vec3{ 0.0f, 0.0f, 1.0f }); + }; } my_scene->GetSystem()->next_scene_ = scene2;