From 2750f8da2499f16ebe152056c6df479591b19684 Mon Sep 17 00:00:00 2001 From: Francesco Guastella Date: Thu, 15 Oct 2015 15:46:35 +0200 Subject: [PATCH] Added support for reflection maps for OBJ files --- code/ObjFileData.h | 8 ++++++ code/ObjFileImporter.cpp | 16 +++++++++++ code/ObjFileMtlImporter.cpp | 57 +++++++++++++++++++++++++++++++++---- code/ObjFileMtlImporter.h | 3 +- 4 files changed, 78 insertions(+), 6 deletions(-) diff --git a/code/ObjFileData.h b/code/ObjFileData.h index 2d5100ba8..9aa44576b 100644 --- a/code/ObjFileData.h +++ b/code/ObjFileData.h @@ -156,6 +156,7 @@ struct Material aiString textureEmissive; aiString textureBump; aiString textureNormal; + aiString textureReflection[6]; aiString textureSpecularity; aiString textureOpacity; aiString textureDisp; @@ -167,6 +168,13 @@ struct Material TextureEmissiveType, TextureBumpType, TextureNormalType, + TextureReflectionSphereType, + TextureReflectionCubeTopType, + TextureReflectionCubeBottomType, + TextureReflectionCubeFrontType, + TextureReflectionCubeBackType, + TextureReflectionCubeLeftType, + TextureReflectionCubeRightType, TextureSpecularityType, TextureOpacityType, TextureDispType, diff --git a/code/ObjFileImporter.cpp b/code/ObjFileImporter.cpp index 7bf6154df..ebf75b3c4 100644 --- a/code/ObjFileImporter.cpp +++ b/code/ObjFileImporter.cpp @@ -636,6 +636,22 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc } } + if( 0 != pCurrentMaterial->textureReflection[0].length ) + { + ObjFile::Material::TextureType type = 0 != pCurrentMaterial->textureReflection[1].length ? + ObjFile::Material::TextureReflectionCubeTopType : + ObjFile::Material::TextureReflectionSphereType; + + unsigned count = type == ObjFile::Material::TextureReflectionSphereType ? 1 : 6; + for( unsigned i = 0; i < count; i++ ) + mat->AddProperty(&pCurrentMaterial->textureReflection[i], AI_MATKEY_TEXTURE_REFLECTION(i)); + + if(pCurrentMaterial->clamp[type]) + //TODO addTextureMappingModeProperty should accept an index to handle clamp option for each + //texture of a cubemap + addTextureMappingModeProperty(mat, aiTextureType_REFLECTION); + } + if ( 0 != pCurrentMaterial->textureDisp.length ) { mat->AddProperty( &pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0) ); diff --git a/code/ObjFileMtlImporter.cpp b/code/ObjFileMtlImporter.cpp index c993cd203..5b5160a44 100644 --- a/code/ObjFileMtlImporter.cpp +++ b/code/ObjFileMtlImporter.cpp @@ -64,6 +64,7 @@ static const std::string BumpTexture1 = "map_bump"; static const std::string BumpTexture2 = "map_Bump"; static const std::string BumpTexture3 = "bump"; static const std::string NormalTexture = "map_Kn"; +static const std::string ReflectionTexture = "refl"; static const std::string DisplacementTexture = "disp"; static const std::string SpecularityTexture = "map_ns"; @@ -200,6 +201,7 @@ void ObjFileMtlImporter::load() case 'm': // Texture case 'b': // quick'n'dirty - for 'bump' sections + case 'r': // quick'n'dirty - for 'refl' sections { getTexture(); m_DataIt = skipLine( m_DataIt, m_DataItEnd, m_uiLine ); @@ -332,6 +334,9 @@ void ObjFileMtlImporter::getTexture() { // Normal map out = & m_pModel->m_pCurrentMaterial->textureNormal; clampIndex = ObjFile::Material::TextureNormalType; + } else if(!ASSIMP_strincmp( pPtr, ReflectionTexture.c_str(), ReflectionTexture.size() ) ) { + // Reflection texture(s) + //Do nothing here } else if (!ASSIMP_strincmp( pPtr, DisplacementTexture.c_str(), DisplacementTexture.size() ) ) { // Displacement texture out = &m_pModel->m_pCurrentMaterial->textureDisp; @@ -346,7 +351,7 @@ void ObjFileMtlImporter::getTexture() { } bool clamp = false; - getTextureOption(clamp); + getTextureOption(clamp, clampIndex, out); m_pModel->m_pCurrentMaterial->clamp[clampIndex] = clamp; std::string texture; @@ -369,7 +374,7 @@ void ObjFileMtlImporter::getTexture() { * Because aiMaterial supports clamp option, so we also want to return it * ///////////////////////////////////////////////////////////////////////////// */ -void ObjFileMtlImporter::getTextureOption(bool &clamp) +void ObjFileMtlImporter::getTextureOption(bool &clamp, int &clampIndex, aiString *&out) { m_DataIt = getNextToken(m_DataIt, m_DataItEnd); @@ -392,13 +397,55 @@ void ObjFileMtlImporter::getTextureOption(bool &clamp) skipToken = 2; } - else if ( !ASSIMP_strincmp(pPtr, BlendUOption.c_str(), BlendUOption.size()) + else if( !ASSIMP_strincmp( pPtr, TypeOption.c_str(), TypeOption.size() ) ) + { + DataArrayIt it = getNextToken( m_DataIt, m_DataItEnd ); + char value[ 12 ]; + CopyNextWord( it, m_DataItEnd, value, sizeof( value ) / sizeof( *value ) ); + if( !ASSIMP_strincmp( value, "cube_top", 8 ) ) + { + clampIndex = ObjFile::Material::TextureReflectionCubeTopType; + out = &m_pModel->m_pCurrentMaterial->textureReflection[0]; + } + else if( !ASSIMP_strincmp( value, "cube_bottom", 11 ) ) + { + clampIndex = ObjFile::Material::TextureReflectionCubeBottomType; + out = &m_pModel->m_pCurrentMaterial->textureReflection[1]; + } + else if( !ASSIMP_strincmp( value, "cube_front", 10 ) ) + { + clampIndex = ObjFile::Material::TextureReflectionCubeFrontType; + out = &m_pModel->m_pCurrentMaterial->textureReflection[2]; + } + else if( !ASSIMP_strincmp( value, "cube_back", 9 ) ) + { + clampIndex = ObjFile::Material::TextureReflectionCubeBackType; + out = &m_pModel->m_pCurrentMaterial->textureReflection[3]; + } + else if( !ASSIMP_strincmp( value, "cube_left", 9 ) ) + { + clampIndex = ObjFile::Material::TextureReflectionCubeLeftType; + out = &m_pModel->m_pCurrentMaterial->textureReflection[4]; + } + else if( !ASSIMP_strincmp( value, "cube_right", 10 ) ) + { + clampIndex = ObjFile::Material::TextureReflectionCubeRightType; + out = &m_pModel->m_pCurrentMaterial->textureReflection[5]; + } + else if( !ASSIMP_strincmp( value, "sphere", 6 ) ) + { + clampIndex = ObjFile::Material::TextureReflectionSphereType; + out = &m_pModel->m_pCurrentMaterial->textureReflection[0]; + } + + skipToken = 2; + } + else if (!ASSIMP_strincmp(pPtr, BlendUOption.c_str(), BlendUOption.size()) || !ASSIMP_strincmp(pPtr, BlendVOption.c_str(), BlendVOption.size()) || !ASSIMP_strincmp(pPtr, BoostOption.c_str(), BoostOption.size()) || !ASSIMP_strincmp(pPtr, ResolutionOption.c_str(), ResolutionOption.size()) || !ASSIMP_strincmp(pPtr, BumpOption.c_str(), BumpOption.size()) - || !ASSIMP_strincmp(pPtr, ChannelOption.c_str(), ChannelOption.size()) - || !ASSIMP_strincmp(pPtr, TypeOption.c_str(), TypeOption.size()) ) + || !ASSIMP_strincmp(pPtr, ChannelOption.c_str(), ChannelOption.size())) { skipToken = 2; } diff --git a/code/ObjFileMtlImporter.h b/code/ObjFileMtlImporter.h index 3278e380b..8bcfc51e1 100644 --- a/code/ObjFileMtlImporter.h +++ b/code/ObjFileMtlImporter.h @@ -43,6 +43,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include struct aiColor3D; +struct aiString; namespace Assimp { @@ -89,7 +90,7 @@ private: void createMaterial(); /// Get texture name from loaded data. void getTexture(); - void getTextureOption(bool &clamp); + void getTextureOption(bool &clamp, int &clampIndex, aiString *&out); private: //! Absolute pathname