From 97fcafccce4e1f8fe2c828fe073685db6979b549 Mon Sep 17 00:00:00 2001 From: Anselme Date: Tue, 29 Aug 2017 21:32:36 +0200 Subject: [PATCH] better texture loading for obj models --- src/tools/loader.cpp | 67 +++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/src/tools/loader.cpp b/src/tools/loader.cpp index bce4edf..6eedfe9 100644 --- a/src/tools/loader.cpp +++ b/src/tools/loader.cpp @@ -297,6 +297,22 @@ std::vector Loader::loadMesh(const std::string &filename){ return meshes; } +void initMaterialTexture(PBRMaterial* mat, PBRMaterial::TextureSlots slot, const std::string & file, int bitsPerPixel = 24) +{ + mat->textures[slot] = RESOURCE_GET(Texture, file); + if (mat->textures[slot] == nullptr) + { + Image* img = Loader::loadImage(file, bitsPerPixel); + if(img != nullptr && img->width == img->height) + { + mat->textures[slot] = new Texture(img); + RESOURCE_ADD(mat->textures[slot], Texture, file); + } + if(img != nullptr) + delete img; + } +} + //load MTL bool Loader::loadMTL(const std::string &filename) { @@ -355,44 +371,19 @@ bool Loader::loadMTL(const std::string &filename) } else if((tokens[0].substr(0,4) == "map_") && tokens.size() == 2) { - if(tokens[0].compare("map_emission") == 0){ - mat->textures[PBRMaterial::EMISSION_SLOT] = RESOURCE_GET(Texture,tokens[1]); - if (mat->textures[PBRMaterial::EMISSION_SLOT] == NULL){ - mat->textures[PBRMaterial::EMISSION_SLOT] = new Texture(loadImage(tokens[1], 24)); - RESOURCE_ADD(mat->textures[PBRMaterial::EMISSION_SLOT], Texture, tokens[1]); - } - } else if(tokens[0].compare("map_albedo") == 0) { - mat->textures[PBRMaterial::ALBEDO_SLOT] = RESOURCE_GET(Texture,tokens[1]); - if (mat->textures[PBRMaterial::ALBEDO_SLOT] == NULL){ - mat->textures[PBRMaterial::ALBEDO_SLOT] = new Texture(loadImage(tokens[1])); - RESOURCE_ADD(mat->textures[PBRMaterial::ALBEDO_SLOT],Texture,tokens[1]); - } - } else if(tokens[0].compare("map_roughness") == 0) { - mat->textures[PBRMaterial::ROUGHNESS_SLOT] = RESOURCE_GET(Texture,tokens[1]); - if (mat->textures[PBRMaterial::ROUGHNESS_SLOT] == NULL){ - mat->textures[PBRMaterial::ROUGHNESS_SLOT] = new Texture(loadImage(tokens[1], 8)); - RESOURCE_ADD(mat->textures[PBRMaterial::ROUGHNESS_SLOT],Texture,tokens[1]); - } - } else if(tokens[0].compare("map_metallic") == 0) { - mat->textures[PBRMaterial::METALLIC_SLOT] = RESOURCE_GET(Texture,tokens[1]); - if (mat->textures[PBRMaterial::METALLIC_SLOT] == NULL){ - mat->textures[PBRMaterial::METALLIC_SLOT] = new Texture(loadImage(tokens[1], 8)); - RESOURCE_ADD(mat->textures[PBRMaterial::METALLIC_SLOT],Texture,tokens[1]); - } - } else if(tokens[0].compare("map_normal") == 0) { - mat->textures[PBRMaterial::NORMALS_SLOT] = RESOURCE_GET(Texture,tokens[1]); - if (mat->textures[PBRMaterial::NORMALS_SLOT] == NULL){ - mat->textures[PBRMaterial::NORMALS_SLOT] = new Texture(loadImage(tokens[1], 24)); - RESOURCE_ADD(mat->textures[PBRMaterial::NORMALS_SLOT],Texture,tokens[1]); - } - hasNormalMap = true; - } else if(tokens[0].compare("map_alpha") == 0) { - mat->textures[PBRMaterial::ALPHA_SLOT] = RESOURCE_GET(Texture,tokens[1]); - if (mat->textures[PBRMaterial::ALPHA_SLOT] == NULL){ - mat->textures[PBRMaterial::ALPHA_SLOT] = new Texture(loadImage(tokens[1], 8)); - RESOURCE_ADD(mat->textures[PBRMaterial::ALPHA_SLOT],Texture,tokens[1]); - } - } else + if(tokens[0].compare("map_emission") == 0) + initMaterialTexture(mat, PBRMaterial::EMISSION_SLOT, tokens[1]); + else if(tokens[0].compare("map_albedo") == 0) + initMaterialTexture(mat, PBRMaterial::ALBEDO_SLOT, tokens[1]); + else if(tokens[0].compare("map_roughness") == 0) + initMaterialTexture(mat, PBRMaterial::ROUGHNESS_SLOT, tokens[1], 8); + else if(tokens[0].compare("map_metallic") == 0) + initMaterialTexture(mat, PBRMaterial::METALLIC_SLOT, tokens[1], 8); + else if(tokens[0].compare("map_normal") == 0) + initMaterialTexture(mat, PBRMaterial::NORMALS_SLOT, tokens[1]); + else if(tokens[0].compare("map_alpha") == 0) + initMaterialTexture(mat, PBRMaterial::ALPHA_SLOT, tokens[1], 8); + else fprintf(stderr, "unsupported material property : \"%s\"\n", tokens[0].c_str()); } else