Fix normal maps being loaded as srgb

This commit is contained in:
bailehuni 2024-02-25 01:35:07 +00:00
parent 958238acd5
commit 59eb1842e4
4 changed files with 47 additions and 71 deletions

View File

@ -103,7 +103,6 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph
} }
/* default shaders */ /* default shaders */
#if 0
{ {
Shader::VertexParams vertParams{}; Shader::VertexParams vertParams{};
vertParams.has_normal = true; vertParams.has_normal = true;
@ -115,60 +114,10 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph
shaderSettings.cull_backface = true; shaderSettings.cull_backface = true;
shaderSettings.write_z = true; shaderSettings.write_z = true;
shaderSettings.render_order = 0; shaderSettings.render_order = 0;
auto texturedShader = std::make_unique<Shader>(renderer(), GetResourcePath("engine/shaders/standard.vert").c_str(), auto fancyShader = std::make_unique<Shader>(renderer(), GetResourcePath("engine/shaders/fancy.vert").c_str(),
GetResourcePath("engine/shaders/standard.frag").c_str(), shaderSettings); GetResourcePath("engine/shaders/fancy.frag").c_str(), shaderSettings);
GetResourceManager<Shader>()->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<Shader>(renderer(), GetResourcePath("engine/shaders/showNormals.vert").c_str(),
GetResourcePath("engine/shaders/showNormals.frag").c_str(), shaderSettings);
GetResourceManager<Shader>()->AddPersistent("builtin.fancy", std::move(fancyShader)); GetResourceManager<Shader>()->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<Shader>(renderer(), GetResourcePath("engine/shaders/skybox.vert").c_str(),
GetResourcePath("engine/shaders/skybox.frag").c_str(), shaderSettings);
GetResourceManager<Shader>()->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<Shader>(renderer(), GetResourcePath("engine/shaders/quad.vert").c_str(),
GetResourcePath("engine/shaders/quad.frag").c_str(), shaderSettings);
GetResourceManager<Shader>()->AddPersistent("builtin.quad", std::move(quadShader));
}
#endif
/* default textures */ /* default textures */
{ {

View File

@ -116,11 +116,23 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
const tg::Scene& s = model.scenes.at(scene_index); 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<bool> 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 */ /* load all textures found in the model */
std::vector<std::shared_ptr<Texture>> textures{}; std::vector<std::shared_ptr<Texture>> textures{};
textures.reserve(model.textures.size()); textures.reserve(model.textures.size());
size_t texture_idx = 0;
for (const tg::Texture& texture : model.textures) { for (const tg::Texture& texture : model.textures) {
// find the image first // find the image first
// use missing texture image by default // 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); 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) { if (image.as_is == false && image.bits == 8 && image.component == 4 && image.pixel_type == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) {
// create texture on GPU // create texture on GPU
// TODO: somehow detect if the textue should be srgb or not textures.back() = std::make_shared<Texture>(scene.app()->renderer(), image.image.data(), image.width, image.height, samplerInfo, !tex_index_is_normal_map[texture_idx]);
textures.back() = std::make_shared<Texture>(scene.app()->renderer(), image.image.data(), image.width, image.height, samplerInfo, true);
} }
++texture_idx;
} }
/* load all materials found in model */ /* 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)); materials.back()->SetAlbedoTexture(textures.at(material.pbrMetallicRoughness.baseColorTexture.index));
} }
else { 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"); 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)); materials.back()->SetNormalTexture(textures.at(material.normalTexture.index));
} }
else { 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"); LOG_WARN("Material will be created with no normal map");
} }
} }
@ -560,6 +573,12 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
if (node.mesh != -1) { if (node.mesh != -1) {
const auto& primitives = primitive_arrays.at(node.mesh); const auto& primitives = primitive_arrays.at(node.mesh);
int i = 0; int i = 0;
if (primitives.size() == 1) {
auto meshren = scene.AddComponent<MeshRenderableComponent>(e);
meshren->mesh = primitives.front().mesh;
meshren->material = primitives.front().material;
}
else {
for (const EnginePrimitive& prim : primitives) { for (const EnginePrimitive& prim : primitives) {
auto prim_entity = scene.CreateEntity(std::string("_mesh") + std::to_string(i), e); auto prim_entity = scene.CreateEntity(std::string("_mesh") + std::to_string(i), e);
auto meshren = scene.AddComponent<MeshRenderableComponent>(prim_entity); auto meshren = scene.AddComponent<MeshRenderableComponent>(prim_entity);
@ -568,6 +587,7 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
++i; ++i;
} }
} }
}
for (int i : node.children) { for (int i : node.children) {
generateEntities(e, model.nodes.at(i)); generateEntities(e, model.nodes.at(i));

Binary file not shown.

View File

@ -191,12 +191,13 @@ void PlayGame(GameSettings settings)
scene2->GetComponent<engine::TransformComponent>(teapot)->scale *= 10.0f; scene2->GetComponent<engine::TransformComponent>(teapot)->scale *= 10.0f;
auto teapot2 = engine::util::LoadGLTF(*scene2, app.GetResourcePath("models/teapot.glb")); auto teapot2 = engine::util::LoadGLTF(*scene2, app.GetResourcePath("models/teapot.glb"));
scene2->GetComponent<engine::TransformComponent>(teapot2)->scale *= 10.0f; scene2->GetComponent<engine::TransformComponent>(teapot2)->scale *= 10.0f;
scene2->GetComponent<engine::TransformComponent>(teapot2)->position.y += 5.0f; scene2->GetComponent<engine::TransformComponent>(teapot2)->position.z += 10.0f;
auto custom = scene2->AddComponent<engine::CustomComponent>(teapot2); auto custom = scene2->AddComponent<engine::CustomComponent>(teapot2);
custom->onInit = [](void) { return; }; custom->onInit = [](void) { return; };
custom->onUpdate = [&](float dt) { custom->onUpdate = [&](float dt) {
dt = 0.0f; dt *= 0.1f;
scene2->GetComponent<engine::TransformComponent>(teapot2)->rotation *= glm::angleAxis(dt, glm::vec3{0.0f, 1.0f, 0.0f}); scene2->GetComponent<engine::TransformComponent>(teapot2)->rotation *= glm::angleAxis(dt, glm::vec3{0.0f, 1.0f, 0.0f});
//scene2->GetComponent<engine::TransformComponent>(teapot2)->rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
scene2->GetComponent<engine::TransformComponent>(teapot)->rotation *= glm::angleAxis(dt, glm::vec3{0.0f, 1.0f, 0.0f}); scene2->GetComponent<engine::TransformComponent>(teapot)->rotation *= glm::angleAxis(dt, glm::vec3{0.0f, 1.0f, 0.0f});
}; };
} }
@ -209,6 +210,12 @@ void PlayGame(GameSettings settings)
scene2->GetComponent<engine::TransformComponent>(normalmaptest)->position += glm::vec3{-10.0f, 0.0f, 1.0f}; scene2->GetComponent<engine::TransformComponent>(normalmaptest)->position += glm::vec3{-10.0f, 0.0f, 1.0f};
auto normalmaptest_notang = engine::util::LoadGLTF(*scene2, app.GetResourcePath("models/normalmaptest_notang.glb")); auto normalmaptest_notang = engine::util::LoadGLTF(*scene2, app.GetResourcePath("models/normalmaptest_notang.glb"));
scene2->GetComponent<engine::TransformComponent>(normalmaptest_notang)->position += glm::vec3{-10.0f, 10.0f, 1.0f}; scene2->GetComponent<engine::TransformComponent>(normalmaptest_notang)->position += glm::vec3{-10.0f, 10.0f, 1.0f};
auto custom = scene2->AddComponent<engine::CustomComponent>(normalmaptest);
custom->onInit = [](void) { return; };
custom->onUpdate = [&](float dt) {
scene2->GetComponent<engine::TransformComponent>(normalmaptest)->rotation *= glm::angleAxis(dt, glm::vec3{ 0.0f, 0.0f, 1.0f });
scene2->GetComponent<engine::TransformComponent>(normalmaptest_notang)->rotation *= glm::angleAxis(dt, glm::vec3{ 0.0f, 0.0f, 1.0f });
};
} }
my_scene->GetSystem<CameraControllerSystem>()->next_scene_ = scene2; my_scene->GetSystem<CameraControllerSystem>()->next_scene_ = scene2;