From 5b7ec62b1792fe7cbfbd13f40730530c71214536 Mon Sep 17 00:00:00 2001 From: keith2018 Date: Sun, 26 Nov 2023 10:21:26 +0800 Subject: [PATCH] Fix texture wrap mode parsing with assimp --- .gitignore | 1 + src/Viewer/Material.h | 4 +++- src/Viewer/ModelLoader.cpp | 48 +++++++++++++++++++++++--------------- src/Viewer/ModelLoader.h | 1 + src/Viewer/Viewer.cpp | 6 ++--- 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 94616ea3..485d9e1e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ bin/ out/ build/ +test_build/ cmake-build-*/ CMakeSettings.json diff --git a/src/Viewer/Material.h b/src/Viewer/Material.h index 4813bbd6..0802f216 100644 --- a/src/Viewer/Material.h +++ b/src/Viewer/Material.h @@ -99,7 +99,9 @@ struct TextureData { size_t width = 0; size_t height = 0; std::vector>> data; - WrapMode wrapMode = Wrap_REPEAT; + WrapMode wrapModeU = Wrap_REPEAT; + WrapMode wrapModeV = Wrap_REPEAT; + WrapMode wrapModeW = Wrap_REPEAT; }; class MaterialObject { diff --git a/src/Viewer/ModelLoader.cpp b/src/Viewer/ModelLoader.cpp index 56ff3d5c..10c90fcd 100644 --- a/src/Viewer/ModelLoader.cpp +++ b/src/Viewer/ModelLoader.cpp @@ -150,7 +150,9 @@ bool ModelLoader::loadSkybox(const std::string &filepath) { texData.width = skyboxTex[0]->getWidth(); texData.height = skyboxTex[0]->getHeight(); texData.data = std::move(skyboxTex); - texData.wrapMode = Wrap_CLAMP_TO_EDGE; + texData.wrapModeU = Wrap_CLAMP_TO_EDGE; + texData.wrapModeV = Wrap_CLAMP_TO_EDGE; + texData.wrapModeW = Wrap_CLAMP_TO_EDGE; } else { skyboxTex.resize(1); skyboxTex[0] = loadTextureFile(filepath); @@ -160,7 +162,9 @@ bool ModelLoader::loadSkybox(const std::string &filepath) { texData.width = skyboxTex[0]->getWidth(); texData.height = skyboxTex[0]->getHeight(); texData.data = std::move(skyboxTex); - texData.wrapMode = Wrap_CLAMP_TO_EDGE; + texData.wrapModeU = Wrap_CLAMP_TO_EDGE; + texData.wrapModeV = Wrap_CLAMP_TO_EDGE; + texData.wrapModeW = Wrap_CLAMP_TO_EDGE; } skyboxMaterialCache_[filepath] = material; @@ -349,7 +353,7 @@ void ModelLoader::processMaterial(const aiMaterial *ai_material, return; } for (size_t i = 0; i < ai_material->GetTextureCount(textureType); i++) { - aiTextureMapMode texMapMode[2]; + aiTextureMapMode texMapMode[2]; // [u, v] aiString texPath; aiReturn retStatus = ai_material->GetTexture(textureType, i, &texPath, nullptr, nullptr, nullptr, nullptr, @@ -384,27 +388,13 @@ void ModelLoader::processMaterial(const aiMaterial *ai_material, auto buffer = loadTextureFile(absolutePath); if (buffer) { - WrapMode mode; - switch (texMapMode[0]) { - case aiTextureMapMode_Wrap: - mode = Wrap_REPEAT; - break; - case aiTextureMapMode_Clamp: - mode = Wrap_CLAMP_TO_EDGE; - break; - case aiTextureMapMode_Mirror: - mode = Wrap_MIRRORED_REPEAT; - break; - default: - mode = Wrap_REPEAT; - break; - } auto &texData = material.textureData[texType]; texData.tag = absolutePath; texData.width = buffer->getWidth(); texData.height = buffer->getHeight(); texData.data = {buffer}; - texData.wrapMode = mode; + texData.wrapModeU = convertTexWrapMode(texMapMode[0]); + texData.wrapModeV = convertTexWrapMode(texMapMode[1]); } else { LOGE("load texture failed: %s, path: %s", Material::materialTexTypeStr(texType), absolutePath.c_str()); } @@ -428,6 +418,26 @@ BoundingBox ModelLoader::convertBoundingBox(const aiAABB &aabb) { return ret; } +WrapMode ModelLoader::convertTexWrapMode(const aiTextureMapMode &mode) { + WrapMode retWrapMode; + switch (mode) { + case aiTextureMapMode_Wrap: + retWrapMode = Wrap_REPEAT; + break; + case aiTextureMapMode_Clamp: + retWrapMode = Wrap_CLAMP_TO_EDGE; + break; + case aiTextureMapMode_Mirror: + retWrapMode = Wrap_MIRRORED_REPEAT; + break; + default: + retWrapMode = Wrap_REPEAT; + break; + } + + return retWrapMode; +} + glm::mat4 ModelLoader::adjustModelCenter(BoundingBox &bounds) { glm::mat4 modelTransform(1.0f); glm::vec3 trans = (bounds.max + bounds.min) / -2.f; diff --git a/src/Viewer/ModelLoader.h b/src/Viewer/ModelLoader.h index c1cb7482..edf0fed5 100644 --- a/src/Viewer/ModelLoader.h +++ b/src/Viewer/ModelLoader.h @@ -57,6 +57,7 @@ class ModelLoader { static glm::mat4 convertMatrix(const aiMatrix4x4 &m); static BoundingBox convertBoundingBox(const aiAABB &aabb); + static WrapMode convertTexWrapMode(const aiTextureMapMode &mode); static glm::mat4 adjustModelCenter(BoundingBox &bounds); void preloadTextureFiles(const aiScene *scene, const std::string &resDir); diff --git a/src/Viewer/Viewer.cpp b/src/Viewer/Viewer.cpp index b8de0830..7f1fdefc 100644 --- a/src/Viewer/Viewer.cpp +++ b/src/Viewer/Viewer.cpp @@ -512,8 +512,8 @@ void Viewer::setupTextures(Material &material) { texDesc.multiSample = false; SamplerDesc sampler{}; - sampler.wrapS = kv.second.wrapMode; - sampler.wrapT = kv.second.wrapMode; + sampler.wrapS = kv.second.wrapModeU; + sampler.wrapT = kv.second.wrapModeV; sampler.filterMin = Filter_LINEAR; sampler.filterMag = Filter_LINEAR; @@ -526,7 +526,7 @@ void Viewer::setupTextures(Material &material) { } case MaterialTexType_CUBE: { texDesc.type = TextureType_CUBE; - sampler.wrapR = kv.second.wrapMode; + sampler.wrapR = kv.second.wrapModeW; break; } default: {