2 #include "globaldefs.h"
3 #include "debugging/debugging.h"
5 #include "iscenegraph.h"
6 #include "iselection.h"
7 #include "stream/stringstream.h"
8 #include "stream/textfilestream.h"
12 // this is very evil, but right now there is no better way
13 #include "../../radiant/brush.h"
16 const int MAX_MATERIAL_NAME = 20;
19 Abstract baseclass for modelexporters
20 the class collects all the data which then gets
21 exported through the WriteToFile method.
26 ExportData( const std::set<std::string>& ignorelist, collapsemode mode, bool limNames, bool objs );
27 virtual ~ExportData( void );
29 virtual void BeginBrush( Brush& b );
30 virtual void AddBrushFace( Face& f );
31 virtual void EndBrush( void );
33 virtual bool WriteToFile( const std::string& path, collapsemode mode ) const = 0;
42 std::list<const Face*> faces;
45 std::list<group> groups;
49 // "textures/common/caulk" -> "caulk"
50 void GetShaderNameFromShaderPath( const char* path, std::string& name );
54 const std::set<std::string>& ignorelist;
57 ExportData::ExportData( const std::set<std::string>& _ignorelist, collapsemode _mode, bool _limNames, bool _objs )
59 ignorelist( _ignorelist ){
62 // in this mode, we need just one group
63 if ( mode == COLLAPSE_ALL ) {
64 groups.push_back( group() );
65 current = &groups.back();
66 current->name = "all";
70 ExportData::~ExportData( void ){
74 void ExportData::BeginBrush( Brush& b ){
75 // create a new group for each brush
76 if ( mode == COLLAPSE_NONE ) {
77 groups.push_back( group() );
78 current = &groups.back();
80 StringOutputStream str( 256 );
81 str << "Brush" << (const unsigned int)groups.size();
82 current->name = str.c_str();
86 void ExportData::EndBrush( void ){
87 // all faces of this brush were on the ignorelist, discard the emptygroup
88 if ( mode == COLLAPSE_NONE ) {
89 ASSERT_NOTNULL( current );
90 if ( current->faces.empty() ) {
97 void ExportData::AddBrushFace( Face& f ){
98 std::string shadername;
99 GetShaderNameFromShaderPath( f.GetShader(), shadername );
101 // ignore faces from ignore list
102 if ( ignorelist.find( shadername ) != ignorelist.end() ) {
106 if ( mode == COLLAPSE_BY_MATERIAL ) {
107 // find a group for this material
109 const std::list<group>::iterator end( groups.end() );
110 for ( std::list<group>::iterator it( groups.begin() ); it != end; ++it )
112 if ( it->name == shadername ) {
117 // no group found, create one
119 groups.push_back( group() );
120 current = &groups.back();
121 current->name = shadername;
125 ASSERT_NOTNULL( current );
127 // add face to current group
128 current->faces.push_back( &f );
131 globalOutputStream() << "Added Face to group " << current->name.c_str() << "\n";
135 void ExportData::GetShaderNameFromShaderPath( const char* path, std::string& name ){
136 std::string tmp( path );
138 size_t last_slash = tmp.find_last_of( "/" );
140 if ( last_slash != std::string::npos && last_slash == ( tmp.length() - 1 ) ) {
144 name = tmp.substr( last_slash + 1, tmp.length() - last_slash );
148 globalOutputStream() << "Last: " << (const unsigned int) last_slash << " " << "length: " << (const unsigned int)tmp.length() << "Name: " << name.c_str() << "\n";
153 Exporter writing facedata as wavefront object
155 class ExportDataAsWavefront : public ExportData
163 ExportDataAsWavefront( const std::set<std::string>& _ignorelist, collapsemode _mode, bool _expmat, bool _limNames, bool _objs )
164 : ExportData( _ignorelist, _mode, _limNames, _objs ){
166 limNames = _limNames;
170 bool WriteToFile( const std::string& path, collapsemode mode ) const;
173 bool ExportDataAsWavefront::WriteToFile( const std::string& path, collapsemode mode ) const {
174 std::string objFile = path;
176 if ( path.compare( path.length() - 4, 4, ".obj" ) != 0 ) {
180 std::string mtlFile = objFile.substr( 0, objFile.length() - 4 ) + ".mtl";
182 std::set<std::string> materials;
184 TextFileOutputStream out( objFile.c_str() );
186 if ( out.failed() ) {
187 globalErrorStream() << "Unable to open file\n";
191 out << "# Wavefront Objectfile exported with radiants brushexport plugin 3.0 by Thomas 'namespace' Nitschke, spam@codecreator.net\n\n";
194 size_t last = mtlFile.find_last_of( "//" );
195 std::string mtllib = mtlFile.substr( last + 1, mtlFile.size() - last ).c_str();
196 out << "mtllib " << mtllib.c_str() << "\n";
199 unsigned int vertex_count = 0;
201 const std::list<ExportData::group>::const_iterator gend( groups.end() );
202 for ( std::list<ExportData::group>::const_iterator git( groups.begin() ); git != gend; ++git )
204 typedef std::multimap<std::string, std::string> bm;
206 typedef std::pair<std::string, std::string> String_Pair;
208 const std::list<const Face*>::const_iterator end( git->faces.end() );
210 // submesh starts here
217 out << git->name.c_str() << "\n";
220 if ( expmat && mode == COLLAPSE_ALL ) {
221 out << "usemtl material" << "\n\n";
222 materials.insert( "material" );
225 for ( std::list<const Face*>::const_iterator it( git->faces.begin() ); it != end; ++it )
227 const Winding& w( ( *it )->getWinding() );
230 for ( size_t i = 0; i < w.numpoints; ++i )
231 out << "v " << FloatFormat( w[i].vertex.x(), 1, 6 ) << " " << FloatFormat( w[i].vertex.z(), 1, 6 ) << " " << FloatFormat( w[i].vertex.y(), 1, 6 ) << "\n";
235 for ( std::list<const Face*>::const_iterator it( git->faces.begin() ); it != end; ++it )
237 const Winding& w( ( *it )->getWinding() );
240 for ( size_t i = 0; i < w.numpoints; ++i )
241 out << "vt " << FloatFormat( w[i].texcoord.x(), 1, 6 ) << " " << FloatFormat( w[i].texcoord.y(), 1, 6 ) << "\n";
244 for ( std::list<const Face*>::const_iterator it( git->faces.begin() ); it != end; ++it )
246 const Winding& w( ( *it )->getWinding() );
249 StringOutputStream faceLine( 256 );
251 for ( size_t i = 0; i < w.numpoints; ++i, ++vertex_count )
253 faceLine << " " << vertex_count + 1 << "/" << vertex_count + 1;
256 if ( mode != COLLAPSE_ALL ) {
257 materials.insert( ( *it )->getShader().getShader() );
258 brushMaterials.insert( String_Pair( ( *it )->getShader().getShader(), faceLine.c_str() ) );
261 out << faceLine.c_str();
265 if ( mode != COLLAPSE_ALL ) {
270 for ( bm::iterator iter = brushMaterials.begin(); iter != brushMaterials.end(); iter++ )
272 mat = ( *iter ).first.c_str();
273 faces = ( *iter ).second.c_str();
275 if ( mat != lastMat ) {
276 if ( limNames && mat.size() > MAX_MATERIAL_NAME ) {
277 out << "\nusemtl " << mat.substr( mat.size() - MAX_MATERIAL_NAME, mat.size() ).c_str();
280 out << "\nusemtl " << mat.c_str();
284 out << faces.c_str();
293 TextFileOutputStream outMtl( mtlFile.c_str() );
294 if ( outMtl.failed() ) {
295 globalErrorStream() << "Unable to open material file\n";
299 outMtl << "# Wavefront material file exported with " RADIANT_NAME " brushexport plugin.\n";
300 outMtl << "# Material Count: " << (const Unsigned)materials.size() << "\n\n";
301 for ( std::set<std::string>::const_iterator it( materials.begin() ); it != materials.end(); ++it )
303 if ( limNames && it->size() > MAX_MATERIAL_NAME ) {
304 outMtl << "newmtl " << it->substr( it->size() - MAX_MATERIAL_NAME, it->size() ).c_str() << "\n";
307 outMtl << "newmtl " << it->c_str() << "\n";
316 class ForEachFace : public BrushVisitor
319 ForEachFace( ExportData& _exporter )
320 : exporter( _exporter )
323 void visit( Face& face ) const {
324 exporter.AddBrushFace( face );
328 ExportData& exporter;
331 class ForEachSelected : public SelectionSystem::Visitor
334 ForEachSelected( ExportData& _exporter )
335 : exporter( _exporter )
338 void visit( scene::Instance& instance ) const {
339 BrushInstance* bptr = InstanceTypeCast<BrushInstance>::cast( instance );
341 Brush& brush( bptr->getBrush() );
343 exporter.BeginBrush( brush );
344 ForEachFace face_vis( exporter );
345 brush.forEachFace( face_vis );
351 ExportData& exporter;
354 bool ExportSelection( const std::set<std::string>& ignorelist, collapsemode m, bool exmat, const std::string& path, bool limNames, bool objs ){
355 ExportDataAsWavefront exporter( ignorelist, m, exmat, limNames, objs );
357 ForEachSelected vis( exporter );
358 GlobalSelectionSystem().foreachSelected( vis );
360 return exporter.WriteToFile( path, m );