2 #include "debugging/debugging.h"
4 #include "iscenegraph.h"
5 #include "iselection.h"
6 #include "stream/stringstream.h"
7 #include "stream/textfilestream.h"
11 // this is very evil, but right now there is no better way
12 #include "../../radiant/brush.h"
15 #define MAX_MATERIAL_NAME 20
18 Abstract baseclass for modelexporters
19 the class collects all the data which then gets
20 exported through the WriteToFile method.
25 ExportData( const std::set<std::string>& ignorelist, collapsemode mode, bool limNames, bool objs );
26 virtual ~ExportData( void );
28 virtual void BeginBrush( Brush& b );
29 virtual void AddBrushFace( Face& f );
30 virtual void EndBrush( void );
32 virtual bool WriteToFile( const std::string& path, collapsemode mode ) const = 0;
41 std::list<const Face*> faces;
44 std::list<group> groups;
48 // "textures/common/caulk" -> "caulk"
49 void GetShaderNameFromShaderPath( const char* path, std::string& name );
53 const std::set<std::string>& ignorelist;
56 ExportData::ExportData( const std::set<std::string>& _ignorelist, collapsemode _mode, bool _limNames, bool _objs )
58 ignorelist( _ignorelist ){
61 // in this mode, we need just one group
62 if ( mode == COLLAPSE_ALL ) {
63 groups.push_back( group() );
64 current = &groups.back();
65 current->name = "all";
69 ExportData::~ExportData( void ){
73 void ExportData::BeginBrush( Brush& b ){
74 // create a new group for each brush
75 if ( mode == COLLAPSE_NONE ) {
76 groups.push_back( group() );
77 current = &groups.back();
79 StringOutputStream str( 256 );
80 str << "Brush" << (const unsigned int)groups.size();
81 current->name = str.c_str();
85 void ExportData::EndBrush( void ){
86 // all faces of this brush were on the ignorelist, discard the emptygroup
87 if ( mode == COLLAPSE_NONE ) {
88 ASSERT_NOTNULL( current );
89 if ( current->faces.empty() ) {
96 void ExportData::AddBrushFace( Face& f ){
97 std::string shadername;
98 GetShaderNameFromShaderPath( f.GetShader(), shadername );
100 // ignore faces from ignore list
101 if ( ignorelist.find( shadername ) != ignorelist.end() ) {
105 if ( mode == COLLAPSE_BY_MATERIAL ) {
106 // find a group for this material
108 const std::list<group>::iterator end( groups.end() );
109 for ( std::list<group>::iterator it( groups.begin() ); it != end; ++it )
111 if ( it->name == shadername ) {
116 // no group found, create one
118 groups.push_back( group() );
119 current = &groups.back();
120 current->name = shadername;
124 ASSERT_NOTNULL( current );
126 // add face to current group
127 current->faces.push_back( &f );
130 globalOutputStream() << "Added Face to group " << current->name.c_str() << "\n";
134 void ExportData::GetShaderNameFromShaderPath( const char* path, std::string& name ){
135 std::string tmp( path );
137 size_t last_slash = tmp.find_last_of( "/" );
139 if ( last_slash != std::string::npos && last_slash == ( tmp.length() - 1 ) ) {
143 name = tmp.substr( last_slash + 1, tmp.length() - last_slash );
147 globalOutputStream() << "Last: " << (const unsigned int) last_slash << " " << "length: " << (const unsigned int)tmp.length() << "Name: " << name.c_str() << "\n";
152 Exporter writing facedata as wavefront object
154 class ExportDataAsWavefront : public ExportData
162 ExportDataAsWavefront( const std::set<std::string>& _ignorelist, collapsemode _mode, bool _expmat, bool _limNames, bool _objs )
163 : ExportData( _ignorelist, _mode, _limNames, _objs ){
165 limNames = _limNames;
169 bool WriteToFile( const std::string& path, collapsemode mode ) const;
172 bool ExportDataAsWavefront::WriteToFile( const std::string& path, collapsemode mode ) const {
173 std::string objFile = path;
175 if ( path.compare( path.length() - 4, 4, ".obj" ) != 0 ) {
179 std::string mtlFile = objFile.substr( 0, objFile.length() - 4 ) + ".mtl";
181 std::set<std::string> materials;
183 TextFileOutputStream out( objFile.c_str() );
185 if ( out.failed() ) {
186 globalErrorStream() << "Unable to open file\n";
190 out << "# Wavefront Objectfile exported with radiants brushexport plugin 3.0 by Thomas 'namespace' Nitschke, spam@codecreator.net\n\n";
193 size_t last = mtlFile.find_last_of( "//" );
194 std::string mtllib = mtlFile.substr( last + 1, mtlFile.size() - last ).c_str();
195 out << "mtllib " << mtllib.c_str() << "\n";
198 unsigned int vertex_count = 0;
200 const std::list<ExportData::group>::const_iterator gend( groups.end() );
201 for ( std::list<ExportData::group>::const_iterator git( groups.begin() ); git != gend; ++git )
203 typedef std::multimap<std::string, std::string> bm;
205 typedef std::pair<std::string, std::string> String_Pair;
207 const std::list<const Face*>::const_iterator end( git->faces.end() );
209 // submesh starts here
216 out << git->name.c_str() << "\n";
219 if ( expmat && mode == COLLAPSE_ALL ) {
220 out << "usemtl material" << "\n\n";
221 materials.insert( "material" );
224 for ( std::list<const Face*>::const_iterator it( git->faces.begin() ); it != end; ++it )
226 const Winding& w( ( *it )->getWinding() );
229 for ( size_t i = 0; i < w.numpoints; ++i )
230 out << "v " << FloatFormat( w[i].vertex.x(), 1, 6 ) << " " << FloatFormat( w[i].vertex.z(), 1, 6 ) << " " << FloatFormat( w[i].vertex.y(), 1, 6 ) << "\n";
234 for ( std::list<const Face*>::const_iterator it( git->faces.begin() ); it != end; ++it )
236 const Winding& w( ( *it )->getWinding() );
239 for ( size_t i = 0; i < w.numpoints; ++i )
240 out << "vt " << FloatFormat( w[i].texcoord.x(), 1, 6 ) << " " << FloatFormat( w[i].texcoord.y(), 1, 6 ) << "\n";
243 for ( std::list<const Face*>::const_iterator it( git->faces.begin() ); it != end; ++it )
245 const Winding& w( ( *it )->getWinding() );
248 StringOutputStream faceLine( 256 );
250 for ( size_t i = 0; i < w.numpoints; ++i, ++vertex_count )
252 faceLine << " " << vertex_count + 1 << "/" << vertex_count + 1;
255 if ( mode != COLLAPSE_ALL ) {
256 materials.insert( ( *it )->getShader().getShader() );
257 brushMaterials.insert( String_Pair( ( *it )->getShader().getShader(), faceLine.c_str() ) );
260 out << faceLine.c_str();
264 if ( mode != COLLAPSE_ALL ) {
269 for ( bm::iterator iter = brushMaterials.begin(); iter != brushMaterials.end(); iter++ )
271 mat = ( *iter ).first.c_str();
272 faces = ( *iter ).second.c_str();
274 if ( mat != lastMat ) {
275 if ( limNames && mat.size() > MAX_MATERIAL_NAME ) {
276 out << "\nusemtl " << mat.substr( mat.size() - MAX_MATERIAL_NAME, mat.size() ).c_str();
279 out << "\nusemtl " << mat.c_str();
283 out << faces.c_str();
292 TextFileOutputStream outMtl( mtlFile.c_str() );
293 if ( outMtl.failed() ) {
294 globalErrorStream() << "Unable to open material file\n";
298 outMtl << "# Wavefront material file exported with NetRadiants brushexport plugin.\n";
299 outMtl << "# Material Count: " << (const Unsigned)materials.size() << "\n\n";
300 for ( std::set<std::string>::const_iterator it( materials.begin() ); it != materials.end(); ++it )
302 if ( limNames && it->size() > MAX_MATERIAL_NAME ) {
303 outMtl << "newmtl " << it->substr( it->size() - MAX_MATERIAL_NAME, it->size() ).c_str() << "\n";
306 outMtl << "newmtl " << it->c_str() << "\n";
315 class ForEachFace : public BrushVisitor
318 ForEachFace( ExportData& _exporter )
319 : exporter( _exporter )
322 void visit( Face& face ) const {
323 exporter.AddBrushFace( face );
327 ExportData& exporter;
330 class ForEachSelected : public SelectionSystem::Visitor
333 ForEachSelected( ExportData& _exporter )
334 : exporter( _exporter )
337 void visit( scene::Instance& instance ) const {
338 BrushInstance* bptr = InstanceTypeCast<BrushInstance>::cast( instance );
340 Brush& brush( bptr->getBrush() );
342 exporter.BeginBrush( brush );
343 ForEachFace face_vis( exporter );
344 brush.forEachFace( face_vis );
350 ExportData& exporter;
353 bool ExportSelection( const std::set<std::string>& ignorelist, collapsemode m, bool exmat, const std::string& path, bool limNames, bool objs ){
354 ExportDataAsWavefront exporter( ignorelist, m, exmat, limNames, objs );
356 ForEachSelected vis( exporter );
357 GlobalSelectionSystem().foreachSelected( vis );
359 return exporter.WriteToFile( path, m );