Skip to content

Commit 5b7ec62

Browse files
committed
Fix texture wrap mode parsing with assimp
1 parent 2502ce1 commit 5b7ec62

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
bin/
55
out/
66
build/
7+
test_build/
78
cmake-build-*/
89
CMakeSettings.json

src/Viewer/Material.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ struct TextureData {
9999
size_t width = 0;
100100
size_t height = 0;
101101
std::vector<std::shared_ptr<Buffer<RGBA>>> data;
102-
WrapMode wrapMode = Wrap_REPEAT;
102+
WrapMode wrapModeU = Wrap_REPEAT;
103+
WrapMode wrapModeV = Wrap_REPEAT;
104+
WrapMode wrapModeW = Wrap_REPEAT;
103105
};
104106

105107
class MaterialObject {

src/Viewer/ModelLoader.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ bool ModelLoader::loadSkybox(const std::string &filepath) {
150150
texData.width = skyboxTex[0]->getWidth();
151151
texData.height = skyboxTex[0]->getHeight();
152152
texData.data = std::move(skyboxTex);
153-
texData.wrapMode = Wrap_CLAMP_TO_EDGE;
153+
texData.wrapModeU = Wrap_CLAMP_TO_EDGE;
154+
texData.wrapModeV = Wrap_CLAMP_TO_EDGE;
155+
texData.wrapModeW = Wrap_CLAMP_TO_EDGE;
154156
} else {
155157
skyboxTex.resize(1);
156158
skyboxTex[0] = loadTextureFile(filepath);
@@ -160,7 +162,9 @@ bool ModelLoader::loadSkybox(const std::string &filepath) {
160162
texData.width = skyboxTex[0]->getWidth();
161163
texData.height = skyboxTex[0]->getHeight();
162164
texData.data = std::move(skyboxTex);
163-
texData.wrapMode = Wrap_CLAMP_TO_EDGE;
165+
texData.wrapModeU = Wrap_CLAMP_TO_EDGE;
166+
texData.wrapModeV = Wrap_CLAMP_TO_EDGE;
167+
texData.wrapModeW = Wrap_CLAMP_TO_EDGE;
164168
}
165169

166170
skyboxMaterialCache_[filepath] = material;
@@ -349,7 +353,7 @@ void ModelLoader::processMaterial(const aiMaterial *ai_material,
349353
return;
350354
}
351355
for (size_t i = 0; i < ai_material->GetTextureCount(textureType); i++) {
352-
aiTextureMapMode texMapMode[2];
356+
aiTextureMapMode texMapMode[2]; // [u, v]
353357
aiString texPath;
354358
aiReturn retStatus = ai_material->GetTexture(textureType, i, &texPath,
355359
nullptr, nullptr, nullptr, nullptr,
@@ -384,27 +388,13 @@ void ModelLoader::processMaterial(const aiMaterial *ai_material,
384388

385389
auto buffer = loadTextureFile(absolutePath);
386390
if (buffer) {
387-
WrapMode mode;
388-
switch (texMapMode[0]) {
389-
case aiTextureMapMode_Wrap:
390-
mode = Wrap_REPEAT;
391-
break;
392-
case aiTextureMapMode_Clamp:
393-
mode = Wrap_CLAMP_TO_EDGE;
394-
break;
395-
case aiTextureMapMode_Mirror:
396-
mode = Wrap_MIRRORED_REPEAT;
397-
break;
398-
default:
399-
mode = Wrap_REPEAT;
400-
break;
401-
}
402391
auto &texData = material.textureData[texType];
403392
texData.tag = absolutePath;
404393
texData.width = buffer->getWidth();
405394
texData.height = buffer->getHeight();
406395
texData.data = {buffer};
407-
texData.wrapMode = mode;
396+
texData.wrapModeU = convertTexWrapMode(texMapMode[0]);
397+
texData.wrapModeV = convertTexWrapMode(texMapMode[1]);
408398
} else {
409399
LOGE("load texture failed: %s, path: %s", Material::materialTexTypeStr(texType), absolutePath.c_str());
410400
}
@@ -428,6 +418,26 @@ BoundingBox ModelLoader::convertBoundingBox(const aiAABB &aabb) {
428418
return ret;
429419
}
430420

421+
WrapMode ModelLoader::convertTexWrapMode(const aiTextureMapMode &mode) {
422+
WrapMode retWrapMode;
423+
switch (mode) {
424+
case aiTextureMapMode_Wrap:
425+
retWrapMode = Wrap_REPEAT;
426+
break;
427+
case aiTextureMapMode_Clamp:
428+
retWrapMode = Wrap_CLAMP_TO_EDGE;
429+
break;
430+
case aiTextureMapMode_Mirror:
431+
retWrapMode = Wrap_MIRRORED_REPEAT;
432+
break;
433+
default:
434+
retWrapMode = Wrap_REPEAT;
435+
break;
436+
}
437+
438+
return retWrapMode;
439+
}
440+
431441
glm::mat4 ModelLoader::adjustModelCenter(BoundingBox &bounds) {
432442
glm::mat4 modelTransform(1.0f);
433443
glm::vec3 trans = (bounds.max + bounds.min) / -2.f;

src/Viewer/ModelLoader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ModelLoader {
5757

5858
static glm::mat4 convertMatrix(const aiMatrix4x4 &m);
5959
static BoundingBox convertBoundingBox(const aiAABB &aabb);
60+
static WrapMode convertTexWrapMode(const aiTextureMapMode &mode);
6061
static glm::mat4 adjustModelCenter(BoundingBox &bounds);
6162

6263
void preloadTextureFiles(const aiScene *scene, const std::string &resDir);

src/Viewer/Viewer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ void Viewer::setupTextures(Material &material) {
512512
texDesc.multiSample = false;
513513

514514
SamplerDesc sampler{};
515-
sampler.wrapS = kv.second.wrapMode;
516-
sampler.wrapT = kv.second.wrapMode;
515+
sampler.wrapS = kv.second.wrapModeU;
516+
sampler.wrapT = kv.second.wrapModeV;
517517
sampler.filterMin = Filter_LINEAR;
518518
sampler.filterMag = Filter_LINEAR;
519519

@@ -526,7 +526,7 @@ void Viewer::setupTextures(Material &material) {
526526
}
527527
case MaterialTexType_CUBE: {
528528
texDesc.type = TextureType_CUBE;
529-
sampler.wrapR = kv.second.wrapMode;
529+
sampler.wrapR = kv.second.wrapModeW;
530530
break;
531531
}
532532
default: {

0 commit comments

Comments
 (0)