refactor the code, also parse texture option explictly

pull/204/head
gongwei 2013-12-02 18:07:16 +08:00 committed by Gongwei Zhang
parent 0860254a2b
commit 0b88f72be2
5 changed files with 81 additions and 83 deletions

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 )
@ -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<int>(&bClamp, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
mat->AddProperty<int>(&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<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_AMBIENT(0));
mat->AddProperty<int>(&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<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_SPECULAR(0));
mat->AddProperty<int>(&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<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_HEIGHT(0));
mat->AddProperty<int>(&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<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_NORMALS(0));
mat->AddProperty<int>(&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<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_DISPLACEMENT(0));
mat->AddProperty<int>(&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<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_OPACITY(0));
mat->AddProperty<int>(&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<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_SHININESS(0));
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_SHININESS(0));
addTextureMappingModeProperty(mat, aiTextureType_SHININESS);
}
}

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,
@ -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<DataArrayIt>( 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<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
* /////////////////////////////////////////////////////////////////////////////
@ -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<DataArrayIt>(m_DataIt, m_DataItEnd, uiLine);
if (itEnd != m_DataItEnd) {
--itEnd;
--itEnd;
}
m_DataIt = getNextToken<DataArrayIt>(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 (!ASSIMP_strincmp(pPtr, ClampOption.c_str(), ClampOption.size()))
{
if (token == '\"' )
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))
{
if (*itEnd == token)
break;
clamp = true;
}
else if (isSeparator(*itEnd))
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()) )
{
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<DataArrayIt>(m_DataIt, m_DataItEnd);
}
}
m_DataIt = itEnd;
}
// -------------------------------------------------------------------

View File

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

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;