Merge pull request #1537 from assimp/Anatoscope-ObjExporter_nomtl

Anatoscope obj exporter nomtl
pull/1519/merge
Kim Kulling 2017-11-04 10:34:29 +01:00 committed by GitHub
commit d5692ccf30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 8 deletions

View File

@ -83,6 +83,7 @@ void ExportSceneCollada(const char*,IOSystem*, const aiScene*, const ExportPrope
void ExportSceneXFile(const char*,IOSystem*, const aiScene*, const ExportProperties*); void ExportSceneXFile(const char*,IOSystem*, const aiScene*, const ExportProperties*);
void ExportSceneStep(const char*,IOSystem*, const aiScene*, const ExportProperties*); void ExportSceneStep(const char*,IOSystem*, const aiScene*, const ExportProperties*);
void ExportSceneObj(const char*,IOSystem*, const aiScene*, const ExportProperties*); void ExportSceneObj(const char*,IOSystem*, const aiScene*, const ExportProperties*);
void ExportSceneObjNoMtl(const char*,IOSystem*, const aiScene*, const ExportProperties*);
void ExportSceneSTL(const char*,IOSystem*, const aiScene*, const ExportProperties*); void ExportSceneSTL(const char*,IOSystem*, const aiScene*, const ExportProperties*);
void ExportSceneSTLBinary(const char*,IOSystem*, const aiScene*, const ExportProperties*); void ExportSceneSTLBinary(const char*,IOSystem*, const aiScene*, const ExportProperties*);
void ExportScenePly(const char*,IOSystem*, const aiScene*, const ExportProperties*); void ExportScenePly(const char*,IOSystem*, const aiScene*, const ExportProperties*);
@ -115,6 +116,8 @@ Exporter::ExportFormatEntry gExporters[] =
#ifndef ASSIMP_BUILD_NO_OBJ_EXPORTER #ifndef ASSIMP_BUILD_NO_OBJ_EXPORTER
Exporter::ExportFormatEntry( "obj", "Wavefront OBJ format", "obj", &ExportSceneObj, Exporter::ExportFormatEntry( "obj", "Wavefront OBJ format", "obj", &ExportSceneObj,
aiProcess_GenSmoothNormals /*| aiProcess_PreTransformVertices */), aiProcess_GenSmoothNormals /*| aiProcess_PreTransformVertices */),
Exporter::ExportFormatEntry( "objnomtl", "Wavefront OBJ format without material file", "obj", &ExportSceneObjNoMtl,
aiProcess_GenSmoothNormals /*| aiProcess_PreTransformVertices */),
#endif #endif
#ifndef ASSIMP_BUILD_NO_STL_EXPORTER #ifndef ASSIMP_BUILD_NO_STL_EXPORTER

View File

@ -83,12 +83,34 @@ void ExportSceneObj(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene
} }
} }
// ------------------------------------------------------------------------------------------------
// Worker function for exporting a scene to Wavefront OBJ without the material file. Prototyped and registered in Exporter.cpp
void ExportSceneObjNoMtl(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties) {
// invoke the exporter
ObjExporter exporter(pFile, pScene, true);
if (exporter.mOutput.fail() || exporter.mOutputMat.fail()) {
throw DeadlyExportError("output data creation failed. Most likely the file became too large: " + std::string(pFile));
}
// we're still here - export successfully completed. Write both the main OBJ file and the material script
{
std::unique_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
if(outfile == NULL) {
throw DeadlyExportError("could not open output .obj file: " + std::string(pFile));
}
outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
}
}
} // end of namespace Assimp } // end of namespace Assimp
static const std::string MaterialExt = ".mtl"; static const std::string MaterialExt = ".mtl";
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
ObjExporter::ObjExporter(const char* _filename, const aiScene* pScene) ObjExporter::ObjExporter(const char* _filename, const aiScene* pScene, bool noMtl)
: filename(_filename) : filename(_filename)
, pScene(pScene) , pScene(pScene)
, vp() , vp()
@ -108,8 +130,9 @@ ObjExporter::ObjExporter(const char* _filename, const aiScene* pScene)
mOutputMat.imbue(l); mOutputMat.imbue(l);
mOutputMat.precision(16); mOutputMat.precision(16);
WriteGeometryFile(); WriteGeometryFile(noMtl);
WriteMaterialFile(); if (!noMtl)
WriteMaterialFile();
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -236,9 +259,10 @@ void ObjExporter::WriteMaterialFile()
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void ObjExporter::WriteGeometryFile() { void ObjExporter::WriteGeometryFile(bool noMtl) {
WriteHeader(mOutput); WriteHeader(mOutput);
mOutput << "mtllib " << GetMaterialLibName() << endl << endl; if (!noMtl)
mOutput << "mtllib " << GetMaterialLibName() << endl << endl;
// collect mesh geometry // collect mesh geometry
aiMatrix4x4 mBase; aiMatrix4x4 mBase;
@ -284,7 +308,8 @@ void ObjExporter::WriteGeometryFile() {
if (!m.name.empty()) { if (!m.name.empty()) {
mOutput << "g " << m.name << endl; mOutput << "g " << m.name << endl;
} }
mOutput << "usemtl " << m.matname << endl; if (!noMtl)
mOutput << "usemtl " << m.matname << endl;
for(const Face& f : m.faces) { for(const Face& f : m.faces) {
mOutput << f.kind << ' '; mOutput << f.kind << ' ';

View File

@ -62,7 +62,7 @@ namespace Assimp {
class ObjExporter { class ObjExporter {
public: public:
/// Constructor for a specific scene to export /// Constructor for a specific scene to export
ObjExporter(const char* filename, const aiScene* pScene); ObjExporter(const char* filename, const aiScene* pScene, bool noMtl=false);
~ObjExporter(); ~ObjExporter();
std::string GetMaterialLibName(); std::string GetMaterialLibName();
std::string GetMaterialLibFileName(); std::string GetMaterialLibFileName();
@ -97,7 +97,7 @@ private:
void WriteHeader(std::ostringstream& out); void WriteHeader(std::ostringstream& out);
void WriteMaterialFile(); void WriteMaterialFile();
void WriteGeometryFile(); void WriteGeometryFile(bool noMtl=false);
std::string GetMaterialName(unsigned int index); std::string GetMaterialName(unsigned int index);
void AddMesh(const aiString& name, const aiMesh* m, const aiMatrix4x4& mat); void AddMesh(const aiString& name, const aiMesh* m, const aiMatrix4x4& mat);
void AddNode(const aiNode* nd, const aiMatrix4x4& mParent); void AddNode(const aiNode* nd, const aiMatrix4x4& mParent);

View File

@ -205,6 +205,7 @@ protected:
const aiScene *scene = importer.ReadFile( ASSIMP_TEST_MODELS_DIR "/OBJ/spider.obj", 0 ); const aiScene *scene = importer.ReadFile( ASSIMP_TEST_MODELS_DIR "/OBJ/spider.obj", 0 );
EXPECT_NE( nullptr, scene ); EXPECT_NE( nullptr, scene );
EXPECT_EQ( aiReturn_SUCCESS, exporter.Export( scene, "obj", ASSIMP_TEST_MODELS_DIR "/OBJ/spider_test.obj" ) ); EXPECT_EQ( aiReturn_SUCCESS, exporter.Export( scene, "obj", ASSIMP_TEST_MODELS_DIR "/OBJ/spider_test.obj" ) );
EXPECT_EQ( aiReturn_SUCCESS, exporter.Export( scene, "objnomtl", ASSIMP_TEST_MODELS_DIR "/OBJ/spider_nomtl_test.obj" ) );
return true; return true;
} }