From 05a5c4c42e1f127a2efbe6e79f8b3e184ab03df8 Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 4 Apr 2018 21:30:59 +0200 Subject: [PATCH] Utility/OBJLoader: Add support for emissive/normal maps by using custom keywords --- ChangeLog.md | 1 + include/Nazara/Utility/Formats/MTLParser.hpp | 2 ++ src/Nazara/Utility/Formats/MTLParser.cpp | 26 ++++++++++++++++++++ src/Nazara/Utility/Formats/OBJLoader.cpp | 18 ++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index 3764af281..ef4ee8c35 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -80,6 +80,7 @@ Nazara Engine: - Dual-stack sockets are now supported (by using NetProtocol_Any at creation/opening) - Fixed IPv6 addresses not being correctly encoded/decoded from the socket API. - Fix copy and move semantic on HandledObject and ObjectHandle +- Add support for emissive and normal maps in .mtl loader using custom keywords ([map_]emissive and [map_]normal) Nazara Development Kit: - Added ImageWidget (#139) diff --git a/include/Nazara/Utility/Formats/MTLParser.hpp b/include/Nazara/Utility/Formats/MTLParser.hpp index 0cacd0886..7eb10aaa9 100644 --- a/include/Nazara/Utility/Formats/MTLParser.hpp +++ b/include/Nazara/Utility/Formats/MTLParser.hpp @@ -45,6 +45,8 @@ namespace Nz String decalMap; String diffuseMap; String displacementMap; + String emissiveMap; //< Custom addition: not present in MTL + String normalMap; //< Custom addition: not present in MTL String reflectionMap; String shininessMap; String specularMap; diff --git a/src/Nazara/Utility/Formats/MTLParser.cpp b/src/Nazara/Utility/Formats/MTLParser.cpp index d001588a4..29810b14a 100644 --- a/src/Nazara/Utility/Formats/MTLParser.cpp +++ b/src/Nazara/Utility/Formats/MTLParser.cpp @@ -251,6 +251,32 @@ namespace Nz currentMaterial->reflectionMap = map; } } + else if (keyword == "map_normal" || keyword == "normal") + { + // This is a custom keyword + std::size_t mapPos = m_currentLine.GetWordPosition(1); + if (mapPos != String::npos) + { + String map = m_currentLine.SubString(mapPos); + if (!currentMaterial) + currentMaterial = AddMaterial("default"); + + currentMaterial->normalMap = map; + } + } + else if (keyword == "map_emissive" || keyword == "emissive") + { + // This is a custom keyword + std::size_t mapPos = m_currentLine.GetWordPosition(1); + if (mapPos != String::npos) + { + String map = m_currentLine.SubString(mapPos); + if (!currentMaterial) + currentMaterial = AddMaterial("default"); + + currentMaterial->emissiveMap = map; + } + } else if (keyword == "newmtl") { String materialName = m_currentLine.SubString(m_currentLine.GetWordPosition(1)); diff --git a/src/Nazara/Utility/Formats/OBJLoader.cpp b/src/Nazara/Utility/Formats/OBJLoader.cpp index da69bcac7..43e938bf4 100644 --- a/src/Nazara/Utility/Formats/OBJLoader.cpp +++ b/src/Nazara/Utility/Formats/OBJLoader.cpp @@ -107,6 +107,24 @@ namespace Nz data.SetParameter(MaterialData::DiffuseTexturePath, fullPath); } + if (!mtlMat->emissiveMap.IsEmpty()) + { + String fullPath = mtlMat->emissiveMap; + if (!Nz::File::IsAbsolute(fullPath)) + fullPath.Prepend(baseDir); + + data.SetParameter(MaterialData::EmissiveTexturePath, fullPath); + } + + if (!mtlMat->normalMap.IsEmpty()) + { + String fullPath = mtlMat->normalMap; + if (!Nz::File::IsAbsolute(fullPath)) + fullPath.Prepend(baseDir); + + data.SetParameter(MaterialData::NormalTexturePath, fullPath); + } + if (!mtlMat->specularMap.IsEmpty()) { String fullPath = mtlMat->specularMap;