Fix Obj texture parsing error: where obj texture map statement contains texture option, it will fail to parse the correct image url, as a result, the texture won't be shown. Besides, we also add clamp texture mode for obj
parent
08cbc0974b
commit
0860254a2b
|
@ -161,6 +161,19 @@ struct Material
|
||||||
aiString textureSpecularity;
|
aiString textureSpecularity;
|
||||||
aiString textureOpacity;
|
aiString textureOpacity;
|
||||||
aiString textureDisp;
|
aiString textureDisp;
|
||||||
|
enum TextureType
|
||||||
|
{
|
||||||
|
TextureDiffuseType = 0,
|
||||||
|
TextureSpecularType,
|
||||||
|
TextureAmbientType,
|
||||||
|
TextureBumpType,
|
||||||
|
TextureNormalType,
|
||||||
|
TextureSpecularityType,
|
||||||
|
TextureOpacityType,
|
||||||
|
TextureDispType,
|
||||||
|
TextureTypeCount
|
||||||
|
};
|
||||||
|
bool clamp[TextureTypeCount];
|
||||||
|
|
||||||
//! Ambient color
|
//! Ambient color
|
||||||
aiColor3D ambient;
|
aiColor3D ambient;
|
||||||
|
@ -186,6 +199,10 @@ struct Material
|
||||||
, ior (1.f)
|
, ior (1.f)
|
||||||
{
|
{
|
||||||
// empty
|
// empty
|
||||||
|
for (size_t i = 0; i < TextureTypeCount; ++i)
|
||||||
|
{
|
||||||
|
clamp[i] = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
|
|
|
@ -548,29 +548,93 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc
|
||||||
mat->AddProperty( &pCurrentMaterial->ior, 1, AI_MATKEY_REFRACTI );
|
mat->AddProperty( &pCurrentMaterial->ior, 1, AI_MATKEY_REFRACTI );
|
||||||
|
|
||||||
// Adding textures
|
// Adding textures
|
||||||
if ( 0 != pCurrentMaterial->texture.length )
|
if ( 0 != pCurrentMaterial->texture.length )
|
||||||
|
{
|
||||||
mat->AddProperty( &pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0));
|
mat->AddProperty( &pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0));
|
||||||
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDiffuseType])
|
||||||
|
{
|
||||||
|
int bClamp = 1;
|
||||||
|
mat->AddProperty<int>(&bClamp, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
|
||||||
|
mat->AddProperty<int>(&bClamp, 1, AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( 0 != pCurrentMaterial->textureAmbient.length )
|
if ( 0 != pCurrentMaterial->textureAmbient.length )
|
||||||
|
{
|
||||||
mat->AddProperty( &pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0));
|
mat->AddProperty( &pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0));
|
||||||
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureAmbientType])
|
||||||
|
{
|
||||||
|
int bClamp = 1;
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_AMBIENT(0));
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_AMBIENT(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( 0 != pCurrentMaterial->textureSpecular.length )
|
if ( 0 != pCurrentMaterial->textureSpecular.length )
|
||||||
|
{
|
||||||
mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));
|
mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));
|
||||||
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularType])
|
||||||
|
{
|
||||||
|
int bClamp = 1;
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_SPECULAR(0));
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_SPECULAR(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( 0 != pCurrentMaterial->textureBump.length )
|
if ( 0 != pCurrentMaterial->textureBump.length )
|
||||||
|
{
|
||||||
mat->AddProperty( &pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0));
|
mat->AddProperty( &pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0));
|
||||||
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureBumpType])
|
||||||
|
{
|
||||||
|
int bClamp = 1;
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_HEIGHT(0));
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_HEIGHT(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( 0 != pCurrentMaterial->textureNormal.length )
|
if ( 0 != pCurrentMaterial->textureNormal.length )
|
||||||
|
{
|
||||||
mat->AddProperty( &pCurrentMaterial->textureNormal, AI_MATKEY_TEXTURE_NORMALS(0));
|
mat->AddProperty( &pCurrentMaterial->textureNormal, AI_MATKEY_TEXTURE_NORMALS(0));
|
||||||
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureNormalType])
|
||||||
|
{
|
||||||
|
int bClamp = 1;
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_NORMALS(0));
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_NORMALS(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( 0 != pCurrentMaterial->textureDisp.length )
|
if ( 0 != pCurrentMaterial->textureDisp.length )
|
||||||
|
{
|
||||||
mat->AddProperty( &pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0) );
|
mat->AddProperty( &pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0) );
|
||||||
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDispType])
|
||||||
|
{
|
||||||
|
int bClamp = 1;
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_DISPLACEMENT(0));
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_DISPLACEMENT(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( 0 != pCurrentMaterial->textureOpacity.length )
|
if ( 0 != pCurrentMaterial->textureOpacity.length )
|
||||||
|
{
|
||||||
mat->AddProperty( &pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0));
|
mat->AddProperty( &pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0));
|
||||||
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureOpacityType])
|
||||||
|
{
|
||||||
|
int bClamp = 1;
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_OPACITY(0));
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_OPACITY(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( 0 != pCurrentMaterial->textureSpecularity.length )
|
if ( 0 != pCurrentMaterial->textureSpecularity.length )
|
||||||
|
{
|
||||||
mat->AddProperty( &pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0));
|
mat->AddProperty( &pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0));
|
||||||
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularityType])
|
||||||
|
{
|
||||||
|
int bClamp = 1;
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_SHININESS(0));
|
||||||
|
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_SHININESS(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Store material property info in material array in scene
|
// Store material property info in material array in scene
|
||||||
pScene->mMaterials[ pScene->mNumMaterials ] = mat;
|
pScene->mMaterials[ pScene->mNumMaterials ] = mat;
|
||||||
|
|
|
@ -263,47 +263,138 @@ void ObjFileMtlImporter::createMaterial()
|
||||||
// Gets a texture name from data.
|
// Gets a texture name from data.
|
||||||
void ObjFileMtlImporter::getTexture() {
|
void ObjFileMtlImporter::getTexture() {
|
||||||
aiString *out( NULL );
|
aiString *out( NULL );
|
||||||
|
int clampIndex = -1;
|
||||||
|
|
||||||
const char *pPtr( &(*m_DataIt) );
|
const char *pPtr( &(*m_DataIt) );
|
||||||
if ( !ASSIMP_strincmp( pPtr, DiffuseTexture.c_str(), DiffuseTexture.size() ) ) {
|
if ( !ASSIMP_strincmp( pPtr, DiffuseTexture.c_str(), DiffuseTexture.size() ) ) {
|
||||||
// Diffuse texture
|
// Diffuse texture
|
||||||
out = & m_pModel->m_pCurrentMaterial->texture;
|
out = & m_pModel->m_pCurrentMaterial->texture;
|
||||||
|
clampIndex = ObjFile::Material::TextureDiffuseType;
|
||||||
} else if ( !ASSIMP_strincmp( pPtr,AmbientTexture.c_str(),AmbientTexture.size() ) ) {
|
} else if ( !ASSIMP_strincmp( pPtr,AmbientTexture.c_str(),AmbientTexture.size() ) ) {
|
||||||
// Ambient texture
|
// Ambient texture
|
||||||
out = & m_pModel->m_pCurrentMaterial->textureAmbient;
|
out = & m_pModel->m_pCurrentMaterial->textureAmbient;
|
||||||
|
clampIndex = ObjFile::Material::TextureAmbientType;
|
||||||
} else if (!ASSIMP_strincmp( pPtr, SpecularTexture.c_str(), SpecularTexture.size())) {
|
} else if (!ASSIMP_strincmp( pPtr, SpecularTexture.c_str(), SpecularTexture.size())) {
|
||||||
// Specular texture
|
// Specular texture
|
||||||
out = & m_pModel->m_pCurrentMaterial->textureSpecular;
|
out = & m_pModel->m_pCurrentMaterial->textureSpecular;
|
||||||
|
clampIndex = ObjFile::Material::TextureSpecularType;
|
||||||
} else if ( !ASSIMP_strincmp( pPtr, OpacityTexture.c_str(), OpacityTexture.size() ) ) {
|
} else if ( !ASSIMP_strincmp( pPtr, OpacityTexture.c_str(), OpacityTexture.size() ) ) {
|
||||||
// Opacity texture
|
// Opacity texture
|
||||||
out = & m_pModel->m_pCurrentMaterial->textureOpacity;
|
out = & m_pModel->m_pCurrentMaterial->textureOpacity;
|
||||||
|
clampIndex = ObjFile::Material::TextureOpacityType;
|
||||||
} else if (!ASSIMP_strincmp( pPtr,"map_ka",6)) {
|
} else if (!ASSIMP_strincmp( pPtr,"map_ka",6)) {
|
||||||
// Ambient texture
|
// Ambient texture
|
||||||
out = & m_pModel->m_pCurrentMaterial->textureAmbient;
|
out = & m_pModel->m_pCurrentMaterial->textureAmbient;
|
||||||
|
clampIndex = ObjFile::Material::TextureAmbientType;
|
||||||
} else if ( !ASSIMP_strincmp( pPtr, BumpTexture1.c_str(), BumpTexture1.size() ) ||
|
} else if ( !ASSIMP_strincmp( pPtr, BumpTexture1.c_str(), BumpTexture1.size() ) ||
|
||||||
!ASSIMP_strincmp( pPtr, BumpTexture2.c_str(), BumpTexture2.size() ) ||
|
!ASSIMP_strincmp( pPtr, BumpTexture2.c_str(), BumpTexture2.size() ) ||
|
||||||
!ASSIMP_strincmp( pPtr, BumpTexture3.c_str(), BumpTexture3.size() ) ) {
|
!ASSIMP_strincmp( pPtr, BumpTexture3.c_str(), BumpTexture3.size() ) ) {
|
||||||
// Bump texture
|
// Bump texture
|
||||||
out = & m_pModel->m_pCurrentMaterial->textureBump;
|
out = & m_pModel->m_pCurrentMaterial->textureBump;
|
||||||
|
clampIndex = ObjFile::Material::TextureBumpType;
|
||||||
} else if (!ASSIMP_strincmp( pPtr,NormalTexture.c_str(), NormalTexture.size())) {
|
} else if (!ASSIMP_strincmp( pPtr,NormalTexture.c_str(), NormalTexture.size())) {
|
||||||
// Normal map
|
// Normal map
|
||||||
out = & m_pModel->m_pCurrentMaterial->textureNormal;
|
out = & m_pModel->m_pCurrentMaterial->textureNormal;
|
||||||
|
clampIndex = ObjFile::Material::TextureNormalType;
|
||||||
} else if (!ASSIMP_strincmp( pPtr, DisplacementTexture.c_str(), DisplacementTexture.size() ) ) {
|
} else if (!ASSIMP_strincmp( pPtr, DisplacementTexture.c_str(), DisplacementTexture.size() ) ) {
|
||||||
// Displacement texture
|
// Displacement texture
|
||||||
out = &m_pModel->m_pCurrentMaterial->textureDisp;
|
out = &m_pModel->m_pCurrentMaterial->textureDisp;
|
||||||
|
clampIndex = ObjFile::Material::TextureDispType;
|
||||||
} else if (!ASSIMP_strincmp( pPtr, SpecularityTexture.c_str(),SpecularityTexture.size() ) ) {
|
} else if (!ASSIMP_strincmp( pPtr, SpecularityTexture.c_str(),SpecularityTexture.size() ) ) {
|
||||||
// Specularity scaling (glossiness)
|
// Specularity scaling (glossiness)
|
||||||
out = & m_pModel->m_pCurrentMaterial->textureSpecularity;
|
out = & m_pModel->m_pCurrentMaterial->textureSpecularity;
|
||||||
|
clampIndex = ObjFile::Material::TextureSpecularityType;
|
||||||
} else {
|
} else {
|
||||||
DefaultLogger::get()->error("OBJ/MTL: Encountered unknown texture type");
|
DefaultLogger::get()->error("OBJ/MTL: Encountered unknown texture type");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_pModel->m_pCurrentMaterial->clamp[clampIndex] = getClamp();
|
||||||
|
skipTextureOption();
|
||||||
|
|
||||||
std::string strTexture;
|
std::string strTexture;
|
||||||
m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strTexture );
|
m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strTexture );
|
||||||
out->Set( 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<DataArrayIt>(m_DataIt, m_DataItEnd, uiLine);
|
||||||
|
if (itEnd != m_DataItEnd)
|
||||||
|
--itEnd;
|
||||||
|
|
||||||
|
std::string line(m_DataIt, itEnd);
|
||||||
|
|
||||||
|
std::vector<std::string> token;
|
||||||
|
const unsigned int numToken = tokenize<std::string>( 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
|
||||||
|
* /////////////////////////////////////////////////////////////////////////////
|
||||||
|
* 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 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"
|
||||||
|
*
|
||||||
|
* /////////////////////////////////////////////////////////////////////////////
|
||||||
|
*/
|
||||||
|
void ObjFileMtlImporter::skipTextureOption()
|
||||||
|
{
|
||||||
|
unsigned int uiLine;
|
||||||
|
DataArrayIt itEnd = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, uiLine);
|
||||||
|
if (itEnd != m_DataItEnd) {
|
||||||
|
--itEnd;
|
||||||
|
--itEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
char token = ' ';
|
||||||
|
if (*itEnd == '\"')
|
||||||
|
{
|
||||||
|
token = '\"';
|
||||||
|
--itEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (itEnd != m_DataIt)
|
||||||
|
{
|
||||||
|
if (token == '\"' )
|
||||||
|
{
|
||||||
|
if (*itEnd == token)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (isSeparator(*itEnd))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
--itEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_DataIt = itEnd;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
} // Namespace Assimp
|
} // Namespace Assimp
|
||||||
|
|
|
@ -92,6 +92,8 @@ private:
|
||||||
void createMaterial();
|
void createMaterial();
|
||||||
/// Get texture name from loaded data.
|
/// Get texture name from loaded data.
|
||||||
void getTexture();
|
void getTexture();
|
||||||
|
bool getClamp();
|
||||||
|
void skipTextureOption();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//! Absolute pathname
|
//! Absolute pathname
|
||||||
|
|
Loading…
Reference in New Issue