Skip to content

Commit

Permalink
Merge pull request #55 from keith2018/dev
Browse files Browse the repository at this point in the history
Fix texture wrap mode parsing with assimp
  • Loading branch information
keith2018 authored Nov 26, 2023
2 parents 2502ce1 + 5b7ec62 commit 928103b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
bin/
out/
build/
test_build/
cmake-build-*/
CMakeSettings.json
4 changes: 3 additions & 1 deletion src/Viewer/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ struct TextureData {
size_t width = 0;
size_t height = 0;
std::vector<std::shared_ptr<Buffer<RGBA>>> data;
WrapMode wrapMode = Wrap_REPEAT;
WrapMode wrapModeU = Wrap_REPEAT;
WrapMode wrapModeV = Wrap_REPEAT;
WrapMode wrapModeW = Wrap_REPEAT;
};

class MaterialObject {
Expand Down
48 changes: 29 additions & 19 deletions src/Viewer/ModelLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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());
}
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Viewer/ModelLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Viewer/Viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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: {
Expand Down

0 comments on commit 928103b

Please sign in to comment.