Merge pull request #204 from zhgw01/zhgw01/FixObjTextureUrl

Fix Issue #201: When Obj texture map statement contains texture option, assimp will fail to import texture
pull/208/head
Kim Kulling 2013-12-02 10:52:21 -08:00
commit 44773b8f9e
6 changed files with 174 additions and 2 deletions

View File

@ -161,6 +161,19 @@ struct Material
aiString textureSpecularity;
aiString textureOpacity;
aiString textureDisp;
enum TextureType
{
TextureDiffuseType = 0,
TextureSpecularType,
TextureAmbientType,
TextureBumpType,
TextureNormalType,
TextureSpecularityType,
TextureOpacityType,
TextureDispType,
TextureTypeCount
};
bool clamp[TextureTypeCount];
//! Ambient color
aiColor3D ambient;
@ -186,6 +199,10 @@ struct Material
, ior (1.f)
{
// empty
for (size_t i = 0; i < TextureTypeCount; ++i)
{
clamp[i] = false;
}
}
// Destructor

View File

@ -484,6 +484,15 @@ void ObjFileImporter::countObjects(const std::vector<ObjFile::Object*> &rObjects
}
}
// ------------------------------------------------------------------------------------------------
// Add clamp mode property to material if necessary
void ObjFileImporter::addTextureMappingModeProperty(aiMaterial* mat, aiTextureType type, int clampMode)
{
ai_assert( NULL != mat);
mat->AddProperty<int>(&clampMode, 1, AI_MATKEY_MAPPINGMODE_U(type, 0));
mat->AddProperty<int>(&clampMode, 1, AI_MATKEY_MAPPINGMODE_V(type, 0));
}
// ------------------------------------------------------------------------------------------------
// Creates the material
void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pScene )
@ -549,28 +558,76 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc
// Adding textures
if ( 0 != pCurrentMaterial->texture.length )
{
mat->AddProperty( &pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDiffuseType])
{
addTextureMappingModeProperty(mat, aiTextureType_DIFFUSE);
}
}
if ( 0 != pCurrentMaterial->textureAmbient.length )
{
mat->AddProperty( &pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureAmbientType])
{
addTextureMappingModeProperty(mat, aiTextureType_AMBIENT);
}
}
if ( 0 != pCurrentMaterial->textureSpecular.length )
{
mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularType])
{
addTextureMappingModeProperty(mat, aiTextureType_SPECULAR);
}
}
if ( 0 != pCurrentMaterial->textureBump.length )
{
mat->AddProperty( &pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureBumpType])
{
addTextureMappingModeProperty(mat, aiTextureType_HEIGHT);
}
}
if ( 0 != pCurrentMaterial->textureNormal.length )
{
mat->AddProperty( &pCurrentMaterial->textureNormal, AI_MATKEY_TEXTURE_NORMALS(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureNormalType])
{
addTextureMappingModeProperty(mat, aiTextureType_NORMALS);
}
}
if ( 0 != pCurrentMaterial->textureDisp.length )
{
mat->AddProperty( &pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0) );
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDispType])
{
addTextureMappingModeProperty(mat, aiTextureType_DISPLACEMENT);
}
}
if ( 0 != pCurrentMaterial->textureOpacity.length )
{
mat->AddProperty( &pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureOpacityType])
{
addTextureMappingModeProperty(mat, aiTextureType_OPACITY);
}
}
if ( 0 != pCurrentMaterial->textureSpecularity.length )
{
mat->AddProperty( &pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularityType])
{
addTextureMappingModeProperty(mat, aiTextureType_SHININESS);
}
}
// Store material property info in material array in scene
pScene->mMaterials[ pScene->mNumMaterials ] = mat;

View File

@ -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);

View File

@ -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<char> &buffer,
@ -263,47 +279,128 @@ void ObjFileMtlImporter::createMaterial()
// Gets a texture name from data.
void ObjFileMtlImporter::getTexture() {
aiString *out( NULL );
int clampIndex = -1;
const char *pPtr( &(*m_DataIt) );
if ( !ASSIMP_strincmp( pPtr, DiffuseTexture.c_str(), DiffuseTexture.size() ) ) {
// Diffuse texture
out = & m_pModel->m_pCurrentMaterial->texture;
clampIndex = ObjFile::Material::TextureDiffuseType;
} else if ( !ASSIMP_strincmp( pPtr,AmbientTexture.c_str(),AmbientTexture.size() ) ) {
// Ambient texture
out = & m_pModel->m_pCurrentMaterial->textureAmbient;
clampIndex = ObjFile::Material::TextureAmbientType;
} else if (!ASSIMP_strincmp( pPtr, SpecularTexture.c_str(), SpecularTexture.size())) {
// Specular texture
out = & m_pModel->m_pCurrentMaterial->textureSpecular;
clampIndex = ObjFile::Material::TextureSpecularType;
} else if ( !ASSIMP_strincmp( pPtr, OpacityTexture.c_str(), OpacityTexture.size() ) ) {
// Opacity texture
out = & m_pModel->m_pCurrentMaterial->textureOpacity;
clampIndex = ObjFile::Material::TextureOpacityType;
} else if (!ASSIMP_strincmp( pPtr,"map_ka",6)) {
// Ambient texture
out = & m_pModel->m_pCurrentMaterial->textureAmbient;
clampIndex = ObjFile::Material::TextureAmbientType;
} else if ( !ASSIMP_strincmp( pPtr, BumpTexture1.c_str(), BumpTexture1.size() ) ||
!ASSIMP_strincmp( pPtr, BumpTexture2.c_str(), BumpTexture2.size() ) ||
!ASSIMP_strincmp( pPtr, BumpTexture3.c_str(), BumpTexture3.size() ) ) {
// Bump texture
out = & m_pModel->m_pCurrentMaterial->textureBump;
clampIndex = ObjFile::Material::TextureBumpType;
} else if (!ASSIMP_strincmp( pPtr,NormalTexture.c_str(), NormalTexture.size())) {
// Normal map
out = & m_pModel->m_pCurrentMaterial->textureNormal;
clampIndex = ObjFile::Material::TextureNormalType;
} else if (!ASSIMP_strincmp( pPtr, DisplacementTexture.c_str(), DisplacementTexture.size() ) ) {
// Displacement texture
out = &m_pModel->m_pCurrentMaterial->textureDisp;
clampIndex = ObjFile::Material::TextureDispType;
} else if (!ASSIMP_strincmp( pPtr, SpecularityTexture.c_str(),SpecularityTexture.size() ) ) {
// Specularity scaling (glossiness)
out = & m_pModel->m_pCurrentMaterial->textureSpecularity;
clampIndex = ObjFile::Material::TextureSpecularityType;
} else {
DefaultLogger::get()->error("OBJ/MTL: Encountered unknown texture type");
return;
}
bool clamp = false;
getTextureOption(clamp);
m_pModel->m_pCurrentMaterial->clamp[clampIndex] = clamp;
std::string strTexture;
m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strTexture );
out->Set( strTexture );
}
/* /////////////////////////////////////////////////////////////////////////////
* Texture Option
* /////////////////////////////////////////////////////////////////////////////
* According to http://en.wikipedia.org/wiki/Wavefront_.obj_file#Texture_options
* Texture map statement can contains various texture option, for example:
*
* map_Ka -o 1 1 1 some.png
* map_Kd -clamp on some.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::getTextureOption(bool &clamp)
{
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
//If there is any more texture option
while (!isEndOfBuffer(m_DataIt, m_DataItEnd) && *m_DataIt == '-')
{
const char *pPtr( &(*m_DataIt) );
//skip option key and value
int skipToken = 1;
if (!ASSIMP_strincmp(pPtr, ClampOption.c_str(), ClampOption.size()))
{
DataArrayIt it = getNextToken<DataArrayIt>(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 ( !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()) )
{
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;
}
for (int i = 0; i < skipToken; ++i)
{
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
}
}
}
// -------------------------------------------------------------------
} // Namespace Assimp

View File

@ -92,6 +92,7 @@ private:
void createMaterial();
/// Get texture name from loaded data.
void getTexture();
void getTextureOption(bool &clamp);
private:
//! Absolute pathname

View File

@ -157,7 +157,6 @@ template<class char_t>
inline char_t getName( char_t it, char_t end, std::string &name )
{
name = "";
it = getNextToken<char_t>( it, end );
if ( isEndOfBuffer( it, end ) )
return end;