From 0b88f72be2d1c66d7e912d7151998dccf1374300 Mon Sep 17 00:00:00 2001 From: gongwei Date: Mon, 2 Dec 2013 18:07:16 +0800 Subject: [PATCH] refactor the code, also parse texture option explictly --- code/ObjFileImporter.cpp | 41 ++++++------- code/ObjFileImporter.h | 1 + code/ObjFileMtlImporter.cpp | 118 +++++++++++++++++++----------------- code/ObjFileMtlImporter.h | 3 +- code/ObjTools.h | 1 - 5 files changed, 81 insertions(+), 83 deletions(-) diff --git a/code/ObjFileImporter.cpp b/code/ObjFileImporter.cpp index 2e07cf1ec..13dfee203 100644 --- a/code/ObjFileImporter.cpp +++ b/code/ObjFileImporter.cpp @@ -484,6 +484,15 @@ void ObjFileImporter::countObjects(const std::vector &rObjects } } +// ------------------------------------------------------------------------------------------------ +// Add clamp mode property to material if necessary +void ObjFileImporter::addTextureMappingModeProperty(aiMaterial* mat, aiTextureType type, int clampMode) +{ + ai_assert( NULL != mat); + mat->AddProperty(&clampMode, 1, AI_MATKEY_MAPPINGMODE_U(type, 0)); + mat->AddProperty(&clampMode, 1, AI_MATKEY_MAPPINGMODE_V(type, 0)); +} + // ------------------------------------------------------------------------------------------------ // Creates the material void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pScene ) @@ -553,9 +562,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc mat->AddProperty( &pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0)); if (pCurrentMaterial->clamp[ObjFile::Material::TextureDiffuseType]) { - int bClamp = 1; - mat->AddProperty(&bClamp, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0)); - mat->AddProperty(&bClamp, 1, AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0)); + addTextureMappingModeProperty(mat, aiTextureType_DIFFUSE); } } @@ -564,9 +571,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc mat->AddProperty( &pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0)); if (pCurrentMaterial->clamp[ObjFile::Material::TextureAmbientType]) { - int bClamp = 1; - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_AMBIENT(0)); - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_AMBIENT(0)); + addTextureMappingModeProperty(mat, aiTextureType_AMBIENT); } } @@ -575,9 +580,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0)); if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularType]) { - int bClamp = 1; - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_SPECULAR(0)); - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_SPECULAR(0)); + addTextureMappingModeProperty(mat, aiTextureType_SPECULAR); } } @@ -586,9 +589,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc mat->AddProperty( &pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0)); if (pCurrentMaterial->clamp[ObjFile::Material::TextureBumpType]) { - int bClamp = 1; - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_HEIGHT(0)); - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_HEIGHT(0)); + addTextureMappingModeProperty(mat, aiTextureType_HEIGHT); } } @@ -597,9 +598,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc mat->AddProperty( &pCurrentMaterial->textureNormal, AI_MATKEY_TEXTURE_NORMALS(0)); if (pCurrentMaterial->clamp[ObjFile::Material::TextureNormalType]) { - int bClamp = 1; - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_NORMALS(0)); - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_NORMALS(0)); + addTextureMappingModeProperty(mat, aiTextureType_NORMALS); } } @@ -608,9 +607,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc mat->AddProperty( &pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0) ); if (pCurrentMaterial->clamp[ObjFile::Material::TextureDispType]) { - int bClamp = 1; - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_DISPLACEMENT(0)); - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_DISPLACEMENT(0)); + addTextureMappingModeProperty(mat, aiTextureType_DISPLACEMENT); } } @@ -619,9 +616,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc mat->AddProperty( &pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0)); if (pCurrentMaterial->clamp[ObjFile::Material::TextureOpacityType]) { - int bClamp = 1; - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_OPACITY(0)); - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_OPACITY(0)); + addTextureMappingModeProperty(mat, aiTextureType_OPACITY); } } @@ -630,9 +625,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc mat->AddProperty( &pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0)); if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularityType]) { - int bClamp = 1; - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_SHININESS(0)); - mat->AddProperty(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_SHININESS(0)); + addTextureMappingModeProperty(mat, aiTextureType_SHININESS); } } diff --git a/code/ObjFileImporter.h b/code/ObjFileImporter.h index ca931088a..814a65652 100644 --- a/code/ObjFileImporter.h +++ b/code/ObjFileImporter.h @@ -103,6 +103,7 @@ private: //! \brief Material creation. void createMaterials(const ObjFile::Model* pModel, aiScene* pScene); + void addTextureMappingModeProperty(aiMaterial* mat, aiTextureType type, int clampMode = 1); //! \brief Appends a child node to a parent node and updates the data structures. void appendChildToParentNode(aiNode *pParent, aiNode *pChild); diff --git a/code/ObjFileMtlImporter.cpp b/code/ObjFileMtlImporter.cpp index c9969f451..f0d2f1795 100644 --- a/code/ObjFileMtlImporter.cpp +++ b/code/ObjFileMtlImporter.cpp @@ -61,6 +61,22 @@ static const std::string NormalTexture = "map_Kn"; static const std::string DisplacementTexture = "disp"; static const std::string SpecularityTexture = "map_ns"; +// texture option specific token +static const std::string BlendUOption = "-blendu"; +static const std::string BlendVOption = "-blendv"; +static const std::string BoostOption = "-boost"; +static const std::string ModifyMapOption = "-mm"; +static const std::string OffsetOption = "-o"; +static const std::string ScaleOption = "-s"; +static const std::string TurbulenceOption = "-t"; +static const std::string ResolutionOption = "-texres"; +static const std::string ClampOption = "-clamp"; +static const std::string BumpOption = "-bm"; +static const std::string ChannelOption = "-imfchan"; +static const std::string TypeOption = "-type"; + + + // ------------------------------------------------------------------- // Constructor ObjFileMtlImporter::ObjFileMtlImporter( std::vector &buffer, @@ -309,40 +325,15 @@ void ObjFileMtlImporter::getTexture() { return; } - m_pModel->m_pCurrentMaterial->clamp[clampIndex] = getClamp(); - skipTextureOption(); + bool clamp = false; + getTextureOption(clamp); + m_pModel->m_pCurrentMaterial->clamp[clampIndex] = clamp; std::string strTexture; m_DataIt = getName( m_DataIt, m_DataItEnd, strTexture ); out->Set( strTexture ); } -// ------------------------------------------------------------------- -// Try to find if there is a "-clamp on" texture option. It doesn't -// skip part of stream here, that is, it won't modify m_DataIt here -bool ObjFileMtlImporter::getClamp() -{ - unsigned int uiLine; - DataArrayIt itEnd = skipLine(m_DataIt, m_DataItEnd, uiLine); - if (itEnd != m_DataItEnd) - --itEnd; - - std::string line(m_DataIt, itEnd); - - std::vector token; - const unsigned int numToken = tokenize( line, token, " " ); - for (unsigned int i = 0; i < numToken; ++i) - { - if (!ASSIMP_stricmp(token[i], "-clamp") && i + 1 < numToken) - { - if (token[i+1] == "on") - return true; - } - } - - return false; -} - /* ///////////////////////////////////////////////////////////////////////////// * Texture Option * ///////////////////////////////////////////////////////////////////////////// @@ -352,47 +343,62 @@ bool ObjFileMtlImporter::getClamp() * map_Ka -o 1 1 1 some.png * map_Kd -clamp on some.png * - * So we need to skip this option, just keep the last part which is the url of - * image, otherwise we will get a wrong url like "-clamp on some.png". - * Here we also take a special case into account where url contains space: - * - * map_Kd -clamp on "url contains space.png" + * So we need to parse and skip these options, and leave the last part which is + * the url of image, otherwise we will get a wrong url like "-clamp on some.png". * + * Because aiMaterial supports clamp option, so we also want to return it * ///////////////////////////////////////////////////////////////////////////// */ -void ObjFileMtlImporter::skipTextureOption() +void ObjFileMtlImporter::getTextureOption(bool &clamp) { - unsigned int uiLine; - DataArrayIt itEnd = skipLine(m_DataIt, m_DataItEnd, uiLine); - if (itEnd != m_DataItEnd) { - --itEnd; - --itEnd; - } + m_DataIt = getNextToken(m_DataIt, m_DataItEnd); - char token = ' '; - if (*itEnd == '\"') + //If there is any more texture option + while (!isEndOfBuffer(m_DataIt, m_DataItEnd) && *m_DataIt == '-') { - token = '\"'; - --itEnd; - } + const char *pPtr( &(*m_DataIt) ); + //skip option key and value + int skipToken = 1; - while (itEnd != m_DataIt) - { - if (token == '\"' ) + if (!ASSIMP_strincmp(pPtr, ClampOption.c_str(), ClampOption.size())) { - if (*itEnd == token) - break; + DataArrayIt it = getNextToken(m_DataIt, m_DataItEnd); + char value[3]; + CopyNextWord(it, m_DataItEnd, value, sizeof(value) / sizeof(*value)); + if (!ASSIMP_strincmp(value, "on", 2)) + { + clamp = true; + } + + skipToken = 2; } - else if (isSeparator(*itEnd)) + 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()) ) { - break; + skipToken = 2; + } + else if (!ASSIMP_strincmp(pPtr, ModifyMapOption.c_str(), ModifyMapOption.size())) + { + skipToken = 3; + } + else if ( !ASSIMP_strincmp(pPtr, OffsetOption.c_str(), OffsetOption.size()) + || !ASSIMP_strincmp(pPtr, ScaleOption.c_str(), ScaleOption.size()) + || !ASSIMP_strincmp(pPtr, TurbulenceOption.c_str(), TurbulenceOption.size()) + ) + { + skipToken = 4; } - --itEnd; + for (int i = 0; i < skipToken; ++i) + { + m_DataIt = getNextToken(m_DataIt, m_DataItEnd); + } } - - m_DataIt = itEnd; - } // ------------------------------------------------------------------- diff --git a/code/ObjFileMtlImporter.h b/code/ObjFileMtlImporter.h index 32e9a5c67..67609b41b 100644 --- a/code/ObjFileMtlImporter.h +++ b/code/ObjFileMtlImporter.h @@ -92,8 +92,7 @@ private: void createMaterial(); /// Get texture name from loaded data. void getTexture(); - bool getClamp(); - void skipTextureOption(); + void getTextureOption(bool &clamp); private: //! Absolute pathname diff --git a/code/ObjTools.h b/code/ObjTools.h index 41de3f61f..7a9a57769 100644 --- a/code/ObjTools.h +++ b/code/ObjTools.h @@ -157,7 +157,6 @@ template inline char_t getName( char_t it, char_t end, std::string &name ) { name = ""; - it = getNextToken( it, end ); if ( isEndOfBuffer( it, end ) ) return end;