better texture loading for obj models

This commit is contained in:
Anselme 2017-08-29 21:32:36 +02:00
parent c9050f5ea4
commit 97fcafccce

View File

@ -297,6 +297,22 @@ std::vector<Mesh*> 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