- Collada exporter texture references in materials are now written correctly

- removed various optional xml attributes from Collada Exporter which caused warnings in OpenCollada



git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@1174 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
pull/5/head
ulfjorensen 2012-02-18 15:20:28 +00:00
parent 1fef449654
commit 6cb8ca9652
2 changed files with 66 additions and 18 deletions

View File

@ -122,7 +122,9 @@ void ColladaExporter::WriteHeader()
mOutput << startstr << "<authoring_tool>Assimp Collada Exporter</authoring_tool>" << endstr; mOutput << startstr << "<authoring_tool>Assimp Collada Exporter</authoring_tool>" << endstr;
PopTag(); PopTag();
mOutput << startstr << "</contributor>" << endstr; mOutput << startstr << "</contributor>" << endstr;
mOutput << startstr << "<unit meter=\"1.0\" name=\"meter\" />" << endstr; mOutput << startstr << "<created>2000-01-01T23:59:59</created>" << endstr;
mOutput << startstr << "<modified>2000-01-01T23:59:59</modified>" << endstr;
mOutput << startstr << "<unit name=\"centimeter\" meter=\"0.01\" />" << endstr;
mOutput << startstr << "<up_axis>Y_UP</up_axis>" << endstr; mOutput << startstr << "<up_axis>Y_UP</up_axis>" << endstr;
PopTag(); PopTag();
mOutput << startstr << "</asset>" << endstr; mOutput << startstr << "</asset>" << endstr;
@ -152,7 +154,7 @@ void ColladaExporter::WriteImageEntry( const Surface& pSurface, const std::strin
{ {
if( !pSurface.texture.empty() ) if( !pSurface.texture.empty() )
{ {
mOutput << startstr << "<image id=\"" << pNameAdd << "\" name=\"" << pNameAdd << "\">" << endstr; mOutput << startstr << "<image id=\"" << pNameAdd << "\">" << endstr;
PushTag(); PushTag();
mOutput << startstr << "<init_from>" << pSurface.texture << "</init_from>" << endstr; mOutput << startstr << "<init_from>" << pSurface.texture << "</init_from>" << endstr;
PopTag(); PopTag();
@ -177,6 +179,35 @@ void ColladaExporter::WriteTextureColorEntry( const Surface& pSurface, const std
mOutput << startstr << "</" << pTypeName << ">" << endstr; mOutput << startstr << "</" << pTypeName << ">" << endstr;
} }
// ------------------------------------------------------------------------------------------------
// Writes the two parameters necessary for referencing a texture in an effect entry
void ColladaExporter::WriteTextureParamEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pMatName)
{
// if surface is a texture, write out the sampler and the surface parameters necessary to reference the texture
if( !pSurface.texture.empty() )
{
mOutput << startstr << "<newparam sid=\"" << pMatName << "-" << pTypeName << "-surface\">" << endstr;
PushTag();
mOutput << startstr << "<surface type=\"2D\">" << endstr;
PushTag();
mOutput << startstr << "<init_from>" << pMatName << "-" << pTypeName << "-image</init_from>" << endstr;
PopTag();
mOutput << startstr << "</surface>" << endstr;
PopTag();
mOutput << startstr << "</newparam>" << endstr;
mOutput << startstr << "<newparam sid=\"" << pMatName << "-" << pTypeName << "-sampler\">" << endstr;
PushTag();
mOutput << startstr << "<sampler2D>" << endstr;
PushTag();
mOutput << startstr << "<source>" << pMatName << "-" << pTypeName << "-surface</source>" << endstr;
PopTag();
mOutput << startstr << "</sampler2D>" << endstr;
PopTag();
mOutput << startstr << "</newparam>" << endstr;
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Writes the material setup // Writes the material setup
void ColladaExporter::WriteMaterials() void ColladaExporter::WriteMaterials()
@ -191,7 +222,10 @@ void ColladaExporter::WriteMaterials()
aiString name; aiString name;
if( mat->Get( AI_MATKEY_NAME, name) != aiReturn_SUCCESS ) if( mat->Get( AI_MATKEY_NAME, name) != aiReturn_SUCCESS )
name = "mat"; name = "mat";
materials[a].name = boost::lexical_cast<std::string> (a) + name.C_Str(); materials[a].name = std::string( "m") + boost::lexical_cast<std::string> (a) + name.C_Str();
size_t pos;
while( (pos = materials[a].name.find( '#')) != std::string::npos )
materials[a].name[pos] = 'x';
ReadMaterialSurface( materials[a].ambient, mat, aiTextureType_AMBIENT, AI_MATKEY_COLOR_AMBIENT); ReadMaterialSurface( materials[a].ambient, mat, aiTextureType_AMBIENT, AI_MATKEY_COLOR_AMBIENT);
ReadMaterialSurface( materials[a].diffuse, mat, aiTextureType_DIFFUSE, AI_MATKEY_COLOR_DIFFUSE); ReadMaterialSurface( materials[a].diffuse, mat, aiTextureType_DIFFUSE, AI_MATKEY_COLOR_DIFFUSE);
@ -209,12 +243,12 @@ void ColladaExporter::WriteMaterials()
for( std::vector<Material>::const_iterator it = materials.begin(); it != materials.end(); ++it ) for( std::vector<Material>::const_iterator it = materials.begin(); it != materials.end(); ++it )
{ {
const Material& mat = *it; const Material& mat = *it;
WriteImageEntry( mat.ambient, mat.name + "_ambient_image"); WriteImageEntry( mat.ambient, mat.name + "-ambient-image");
WriteImageEntry( mat.diffuse, mat.name + "_diffuse_image"); WriteImageEntry( mat.diffuse, mat.name + "-diffuse-image");
WriteImageEntry( mat.specular, mat.name + "_specular_image"); WriteImageEntry( mat.specular, mat.name + "-specular-image");
WriteImageEntry( mat.emissive, mat.name + "_emissive_image"); WriteImageEntry( mat.emissive, mat.name + "-emissive-image");
WriteImageEntry( mat.reflective, mat.name + "_reflective_image"); WriteImageEntry( mat.reflective, mat.name + "-reflective-image");
WriteImageEntry( mat.normal, mat.name + "_normal_image"); WriteImageEntry( mat.normal, mat.name + "-normal-image");
} }
PopTag(); PopTag();
mOutput << startstr << "</library_images>" << endstr; mOutput << startstr << "</library_images>" << endstr;
@ -230,18 +264,23 @@ void ColladaExporter::WriteMaterials()
PushTag(); PushTag();
mOutput << startstr << "<profile_COMMON>" << endstr; mOutput << startstr << "<profile_COMMON>" << endstr;
PushTag(); PushTag();
// write sampler- and surface params for the texture entries
WriteTextureParamEntry( mat.emissive, "emissive", mat.name);
WriteTextureParamEntry( mat.ambient, "ambient", mat.name);
WriteTextureParamEntry( mat.diffuse, "diffuse", mat.name);
WriteTextureParamEntry( mat.specular, "specular", mat.name);
WriteTextureParamEntry( mat.reflective, "reflective", mat.name);
mOutput << startstr << "<technique sid=\"standard\">" << endstr; mOutput << startstr << "<technique sid=\"standard\">" << endstr;
PushTag(); PushTag();
mOutput << startstr << "<phong>" << endstr; mOutput << startstr << "<phong>" << endstr;
PushTag(); PushTag();
WriteTextureColorEntry( mat.ambient, "ambient", mat.name + "_ambient_image"); WriteTextureColorEntry( mat.emissive, "emission", mat.name + "-emissive-sampler");
WriteTextureColorEntry( mat.diffuse, "diffuse", mat.name + "_diffuse_image"); WriteTextureColorEntry( mat.ambient, "ambient", mat.name + "-ambient-sampler");
WriteTextureColorEntry( mat.specular, "specular", mat.name + "_specular_image"); WriteTextureColorEntry( mat.diffuse, "diffuse", mat.name + "-diffuse-sampler");
WriteTextureColorEntry( mat.emissive, "emission", mat.name + "_emissive_image"); WriteTextureColorEntry( mat.specular, "specular", mat.name + "-specular-sampler");
WriteTextureColorEntry( mat.reflective, "reflective", mat.name + "_reflective_image");
if( !mat.normal.texture.empty() )
WriteTextureColorEntry( mat.normal, "bump", mat.name + "_normal_image");
mOutput << startstr << "<shininess>" << endstr; mOutput << startstr << "<shininess>" << endstr;
PushTag(); PushTag();
@ -249,6 +288,13 @@ void ColladaExporter::WriteMaterials()
PopTag(); PopTag();
mOutput << startstr << "</shininess>" << endstr; mOutput << startstr << "</shininess>" << endstr;
WriteTextureColorEntry( mat.reflective, "reflective", mat.name + "-reflective-sampler");
// deactivated because the Collada spec PHONG model does not allow other textures.
// if( !mat.normal.texture.empty() )
// WriteTextureColorEntry( mat.normal, "bump", mat.name + "-normal-sampler");
PopTag(); PopTag();
mOutput << startstr << "</phong>" << endstr; mOutput << startstr << "</phong>" << endstr;
PopTag(); PopTag();
@ -340,12 +386,12 @@ void ColladaExporter::WriteGeometry( size_t pIndex)
for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a ) for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a )
{ {
if( mesh->HasTextureCoords( a) ) if( mesh->HasTextureCoords( a) )
mOutput << startstr << "<input semantic=\"TEXCOORD\" source=\"#" << idstr << "-tex" << a << "\" set=\"" << a << "\" />" << endstr; mOutput << startstr << "<input semantic=\"TEXCOORD\" source=\"#" << idstr << "-tex" << a << "\" " /*<< "set=\"" << a << "\"" */ << " />" << endstr;
} }
for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a ) for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a )
{ {
if( mesh->HasVertexColors( a) ) if( mesh->HasVertexColors( a) )
mOutput << startstr << "<input semantic=\"COLOR\" source=\"#" << idstr << "-color" << a << "\" set=\"" << a << "\" />" << endstr; mOutput << startstr << "<input semantic=\"COLOR\" source=\"#" << idstr << "-color" << a << "\" " /*<< set=\"" << a << "\"" */ << " />" << endstr;
} }
PopTag(); PopTag();

View File

@ -136,6 +136,8 @@ protected:
void ReadMaterialSurface( Surface& poSurface, const aiMaterial* pSrcMat, aiTextureType pTexture, const char* pKey, size_t pType, size_t pIndex); void ReadMaterialSurface( Surface& poSurface, const aiMaterial* pSrcMat, aiTextureType pTexture, const char* pKey, size_t pType, size_t pIndex);
/// Writes an image entry for the given surface /// Writes an image entry for the given surface
void WriteImageEntry( const Surface& pSurface, const std::string& pNameAdd); void WriteImageEntry( const Surface& pSurface, const std::string& pNameAdd);
/// Writes the two parameters necessary for referencing a texture in an effect entry
void WriteTextureParamEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pMatName);
/// Writes a color-or-texture entry into an effect definition /// Writes a color-or-texture entry into an effect definition
void WriteTextureColorEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pImageName); void WriteTextureColorEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pImageName);
}; };