refactor the code, also parse texture option explictly
parent
0860254a2b
commit
0b88f72be2
|
@ -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
|
// Creates the material
|
||||||
void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pScene )
|
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));
|
mat->AddProperty( &pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0));
|
||||||
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDiffuseType])
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDiffuseType])
|
||||||
{
|
{
|
||||||
int bClamp = 1;
|
addTextureMappingModeProperty(mat, aiTextureType_DIFFUSE);
|
||||||
mat->AddProperty<int>(&bClamp, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
|
|
||||||
mat->AddProperty<int>(&bClamp, 1, AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -564,9 +571,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc
|
||||||
mat->AddProperty( &pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0));
|
mat->AddProperty( &pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0));
|
||||||
if (pCurrentMaterial->clamp[ObjFile::Material::TextureAmbientType])
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureAmbientType])
|
||||||
{
|
{
|
||||||
int bClamp = 1;
|
addTextureMappingModeProperty(mat, aiTextureType_AMBIENT);
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_AMBIENT(0));
|
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_AMBIENT(0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -575,9 +580,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc
|
||||||
mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));
|
mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));
|
||||||
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularType])
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularType])
|
||||||
{
|
{
|
||||||
int bClamp = 1;
|
addTextureMappingModeProperty(mat, aiTextureType_SPECULAR);
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_SPECULAR(0));
|
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_SPECULAR(0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -586,9 +589,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc
|
||||||
mat->AddProperty( &pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0));
|
mat->AddProperty( &pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0));
|
||||||
if (pCurrentMaterial->clamp[ObjFile::Material::TextureBumpType])
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureBumpType])
|
||||||
{
|
{
|
||||||
int bClamp = 1;
|
addTextureMappingModeProperty(mat, aiTextureType_HEIGHT);
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_HEIGHT(0));
|
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_HEIGHT(0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -597,9 +598,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc
|
||||||
mat->AddProperty( &pCurrentMaterial->textureNormal, AI_MATKEY_TEXTURE_NORMALS(0));
|
mat->AddProperty( &pCurrentMaterial->textureNormal, AI_MATKEY_TEXTURE_NORMALS(0));
|
||||||
if (pCurrentMaterial->clamp[ObjFile::Material::TextureNormalType])
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureNormalType])
|
||||||
{
|
{
|
||||||
int bClamp = 1;
|
addTextureMappingModeProperty(mat, aiTextureType_NORMALS);
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_NORMALS(0));
|
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_NORMALS(0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -608,9 +607,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc
|
||||||
mat->AddProperty( &pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0) );
|
mat->AddProperty( &pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0) );
|
||||||
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDispType])
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDispType])
|
||||||
{
|
{
|
||||||
int bClamp = 1;
|
addTextureMappingModeProperty(mat, aiTextureType_DISPLACEMENT);
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_DISPLACEMENT(0));
|
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_DISPLACEMENT(0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -619,9 +616,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc
|
||||||
mat->AddProperty( &pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0));
|
mat->AddProperty( &pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0));
|
||||||
if (pCurrentMaterial->clamp[ObjFile::Material::TextureOpacityType])
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureOpacityType])
|
||||||
{
|
{
|
||||||
int bClamp = 1;
|
addTextureMappingModeProperty(mat, aiTextureType_OPACITY);
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_OPACITY(0));
|
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_OPACITY(0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -630,9 +625,7 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc
|
||||||
mat->AddProperty( &pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0));
|
mat->AddProperty( &pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0));
|
||||||
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularityType])
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularityType])
|
||||||
{
|
{
|
||||||
int bClamp = 1;
|
addTextureMappingModeProperty(mat, aiTextureType_SHININESS);
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_U_SHININESS(0));
|
|
||||||
mat->AddProperty<int>(&bClamp,1, AI_MATKEY_MAPPINGMODE_V_SHININESS(0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -103,6 +103,7 @@ private:
|
||||||
|
|
||||||
//! \brief Material creation.
|
//! \brief Material creation.
|
||||||
void createMaterials(const ObjFile::Model* pModel, aiScene* pScene);
|
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.
|
//! \brief Appends a child node to a parent node and updates the data structures.
|
||||||
void appendChildToParentNode(aiNode *pParent, aiNode *pChild);
|
void appendChildToParentNode(aiNode *pParent, aiNode *pChild);
|
||||||
|
|
|
@ -61,6 +61,22 @@ static const std::string NormalTexture = "map_Kn";
|
||||||
static const std::string DisplacementTexture = "disp";
|
static const std::string DisplacementTexture = "disp";
|
||||||
static const std::string SpecularityTexture = "map_ns";
|
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
|
// Constructor
|
||||||
ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer,
|
ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer,
|
||||||
|
@ -309,40 +325,15 @@ void ObjFileMtlImporter::getTexture() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_pModel->m_pCurrentMaterial->clamp[clampIndex] = getClamp();
|
bool clamp = false;
|
||||||
skipTextureOption();
|
getTextureOption(clamp);
|
||||||
|
m_pModel->m_pCurrentMaterial->clamp[clampIndex] = clamp;
|
||||||
|
|
||||||
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
|
* Texture Option
|
||||||
* /////////////////////////////////////////////////////////////////////////////
|
* /////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -352,47 +343,62 @@ bool ObjFileMtlImporter::getClamp()
|
||||||
* map_Ka -o 1 1 1 some.png
|
* map_Ka -o 1 1 1 some.png
|
||||||
* map_Kd -clamp on 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
|
* So we need to parse and skip these options, and leave the last part which is
|
||||||
* image, otherwise we will get a wrong url like "-clamp on some.png".
|
* 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"
|
|
||||||
*
|
*
|
||||||
|
* Because aiMaterial supports clamp option, so we also want to return it
|
||||||
* /////////////////////////////////////////////////////////////////////////////
|
* /////////////////////////////////////////////////////////////////////////////
|
||||||
*/
|
*/
|
||||||
void ObjFileMtlImporter::skipTextureOption()
|
void ObjFileMtlImporter::getTextureOption(bool &clamp)
|
||||||
{
|
{
|
||||||
unsigned int uiLine;
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
||||||
DataArrayIt itEnd = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, uiLine);
|
|
||||||
if (itEnd != m_DataItEnd) {
|
|
||||||
--itEnd;
|
|
||||||
--itEnd;
|
|
||||||
}
|
|
||||||
|
|
||||||
char token = ' ';
|
//If there is any more texture option
|
||||||
if (*itEnd == '\"')
|
while (!isEndOfBuffer(m_DataIt, m_DataItEnd) && *m_DataIt == '-')
|
||||||
{
|
{
|
||||||
token = '\"';
|
const char *pPtr( &(*m_DataIt) );
|
||||||
--itEnd;
|
//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)
|
clamp = true;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
|
@ -92,8 +92,7 @@ private:
|
||||||
void createMaterial();
|
void createMaterial();
|
||||||
/// Get texture name from loaded data.
|
/// Get texture name from loaded data.
|
||||||
void getTexture();
|
void getTexture();
|
||||||
bool getClamp();
|
void getTextureOption(bool &clamp);
|
||||||
void skipTextureOption();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//! Absolute pathname
|
//! Absolute pathname
|
||||||
|
|
|
@ -157,7 +157,6 @@ template<class char_t>
|
||||||
inline char_t getName( char_t it, char_t end, std::string &name )
|
inline char_t getName( char_t it, char_t end, std::string &name )
|
||||||
{
|
{
|
||||||
name = "";
|
name = "";
|
||||||
it = getNextToken<char_t>( it, end );
|
|
||||||
if ( isEndOfBuffer( it, end ) )
|
if ( isEndOfBuffer( it, end ) )
|
||||||
return end;
|
return end;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue