]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/brushexport/export.cpp
Merge branch 'polishing' into 'master'
[xonotic/netradiant.git] / contrib / brushexport / export.cpp
1 #include "export.h"
2 #include "globaldefs.h"
3 #include "debugging/debugging.h"
4 #include "ibrush.h"
5 #include "iscenegraph.h"
6 #include "iselection.h"
7 #include "stream/stringstream.h"
8 #include "stream/textfilestream.h"
9
10 #include <map>
11
12 // this is very evil, but right now there is no better way
13 #include "../../radiant/brush.h"
14
15 // for limNames
16 const int MAX_MATERIAL_NAME = 20;
17
18 /*
19     Abstract baseclass for modelexporters
20     the class collects all the data which then gets
21     exported through the WriteToFile method.
22  */
23 class ExportData
24 {
25 public:
26 ExportData( const std::set<std::string>& ignorelist, collapsemode mode, bool limNames, bool objs );
27 virtual ~ExportData( void );
28
29 virtual void BeginBrush( Brush& b );
30 virtual void AddBrushFace( Face& f );
31 virtual void EndBrush( void );
32
33 virtual bool WriteToFile( const std::string& path, collapsemode mode ) const = 0;
34
35 protected:
36
37 // a group of faces
38 class group
39 {
40 public:
41 std::string name;
42 std::list<const Face*> faces;
43 };
44
45 std::list<group> groups;
46
47 private:
48
49 // "textures/common/caulk" -> "caulk"
50 void GetShaderNameFromShaderPath( const char* path, std::string& name );
51
52 group* current;
53 collapsemode mode;
54 const std::set<std::string>& ignorelist;
55 };
56
57 ExportData::ExportData( const std::set<std::string>& _ignorelist, collapsemode _mode, bool _limNames, bool _objs )
58         :   mode( _mode ),
59         ignorelist( _ignorelist ){
60         current = 0;
61
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";
67         }
68 }
69
70 ExportData::~ExportData( void ){
71
72 }
73
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();
79
80                 StringOutputStream str( 256 );
81                 str << "Brush" << (const unsigned int)groups.size();
82                 current->name = str.c_str();
83         }
84 }
85
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() ) {
91                         groups.pop_back();
92                         current = 0;
93                 }
94         }
95 }
96
97 void ExportData::AddBrushFace( Face& f ){
98         std::string shadername;
99         GetShaderNameFromShaderPath( f.GetShader(), shadername );
100
101         // ignore faces from ignore list
102         if ( ignorelist.find( shadername ) != ignorelist.end() ) {
103                 return;
104         }
105
106         if ( mode == COLLAPSE_BY_MATERIAL ) {
107                 // find a group for this material
108                 current = 0;
109                 const std::list<group>::iterator end( groups.end() );
110                 for ( std::list<group>::iterator it( groups.begin() ); it != end; ++it )
111                 {
112                         if ( it->name == shadername ) {
113                                 current = &( *it );
114                         }
115                 }
116
117                 // no group found, create one
118                 if ( !current ) {
119                         groups.push_back( group() );
120                         current = &groups.back();
121                         current->name = shadername;
122                 }
123         }
124
125         ASSERT_NOTNULL( current );
126
127         // add face to current group
128         current->faces.push_back( &f );
129
130 #if GDEF_DEBUG
131         globalOutputStream() << "Added Face to group " << current->name.c_str() << "\n";
132 #endif
133 }
134
135 void ExportData::GetShaderNameFromShaderPath( const char* path, std::string& name ){
136         std::string tmp( path );
137
138         size_t last_slash = tmp.find_last_of( "/" );
139
140         if ( last_slash != std::string::npos && last_slash == ( tmp.length() - 1 ) ) {
141                 name = path;
142         }
143         else{
144                 name = tmp.substr( last_slash + 1, tmp.length() - last_slash );
145         }
146
147 #if GDEF_DEBUG
148         globalOutputStream() << "Last: " << (const unsigned int) last_slash << " " << "length: " << (const unsigned int)tmp.length() << "Name: " << name.c_str() << "\n";
149 #endif
150 }
151
152 /*
153     Exporter writing facedata as wavefront object
154  */
155 class ExportDataAsWavefront : public ExportData
156 {
157 private:
158 bool expmat;
159 bool limNames;
160 bool objs;
161
162 public:
163 ExportDataAsWavefront( const std::set<std::string>& _ignorelist, collapsemode _mode, bool _expmat, bool _limNames, bool _objs )
164         : ExportData( _ignorelist, _mode, _limNames, _objs ){
165         expmat = _expmat;
166         limNames = _limNames;
167         objs = _objs;
168 }
169
170 bool WriteToFile( const std::string& path, collapsemode mode ) const;
171 };
172
173 bool ExportDataAsWavefront::WriteToFile( const std::string& path, collapsemode mode ) const {
174         std::string objFile = path;
175
176         if ( path.compare( path.length() - 4, 4, ".obj" ) != 0 ) {
177                 objFile += ".obj";
178         }
179
180         std::string mtlFile = objFile.substr( 0, objFile.length() - 4 ) + ".mtl";
181
182         std::set<std::string> materials;
183
184         TextFileOutputStream out( objFile.c_str() );
185
186         if ( out.failed() ) {
187                 globalErrorStream() << "Unable to open file\n";
188                 return false;
189         }
190
191         out << "# Wavefront Objectfile exported with radiants brushexport plugin 3.0 by Thomas 'namespace' Nitschke, spam@codecreator.net\n\n";
192
193         if ( expmat ) {
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";
197         }
198
199         unsigned int vertex_count = 0;
200
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 )
203         {
204                 typedef std::multimap<std::string, std::string> bm;
205                 bm brushMaterials;
206                 typedef std::pair<std::string, std::string> String_Pair;
207
208                 const std::list<const Face*>::const_iterator end( git->faces.end() );
209
210                 // submesh starts here
211                 if ( objs ) {
212                         out << "\no ";
213                 }
214                 else {
215                         out << "\ng ";
216                 }
217                 out << git->name.c_str() << "\n";
218
219                 // material
220                 if ( expmat && mode == COLLAPSE_ALL ) {
221                         out << "usemtl material" << "\n\n";
222                         materials.insert( "material" );
223                 }
224
225                 for ( std::list<const Face*>::const_iterator it( git->faces.begin() ); it != end; ++it )
226                 {
227                         const Winding& w( ( *it )->getWinding() );
228
229                         // vertices
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";
232                 }
233                 out << "\n";
234
235                 for ( std::list<const Face*>::const_iterator it( git->faces.begin() ); it != end; ++it )
236                 {
237                         const Winding& w( ( *it )->getWinding() );
238
239                         // texcoords
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";
242                 }
243
244                 for ( std::list<const Face*>::const_iterator it( git->faces.begin() ); it != end; ++it )
245                 {
246                         const Winding& w( ( *it )->getWinding() );
247
248                         // faces
249                         StringOutputStream faceLine( 256 );
250                         faceLine << "\nf";
251                         for ( size_t i = 0; i < w.numpoints; ++i, ++vertex_count )
252                         {
253                                 faceLine << " " << vertex_count + 1 << "/" << vertex_count + 1;
254                         }
255
256                         if ( mode != COLLAPSE_ALL ) {
257                                 materials.insert( ( *it )->getShader().getShader() );
258                                 brushMaterials.insert( String_Pair( ( *it )->getShader().getShader(), faceLine.c_str() ) );
259                         }
260                         else {
261                                 out << faceLine.c_str();
262                         }
263                 }
264
265                 if ( mode != COLLAPSE_ALL ) {
266                         std::string lastMat;
267                         std::string mat;
268                         std::string faces;
269
270                         for ( bm::iterator iter = brushMaterials.begin(); iter != brushMaterials.end(); iter++ )
271                         {
272                                 mat = ( *iter ).first.c_str();
273                                 faces = ( *iter ).second.c_str();
274
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();
278                                         }
279                                         else {
280                                                 out << "\nusemtl " << mat.c_str();
281                                         }
282                                 }
283
284                                 out << faces.c_str();
285                                 lastMat = mat;
286                         }
287                 }
288
289                 out << "\n";
290         }
291
292         if ( expmat ) {
293                 TextFileOutputStream outMtl( mtlFile.c_str() );
294                 if ( outMtl.failed() ) {
295                         globalErrorStream() << "Unable to open material file\n";
296                         return false;
297                 }
298
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 )
302                 {
303                         if ( limNames && it->size() > MAX_MATERIAL_NAME ) {
304                                 outMtl << "newmtl " << it->substr( it->size() - MAX_MATERIAL_NAME, it->size() ).c_str() << "\n";
305                         }
306                         else {
307                                 outMtl << "newmtl " << it->c_str() << "\n";
308                         }
309                 }
310         }
311
312         return true;
313 }
314
315
316 class ForEachFace : public BrushVisitor
317 {
318 public:
319 ForEachFace( ExportData& _exporter )
320         : exporter( _exporter )
321 {}
322
323 void visit( Face& face ) const {
324         exporter.AddBrushFace( face );
325 }
326
327 private:
328 ExportData& exporter;
329 };
330
331 class ForEachSelected : public SelectionSystem::Visitor
332 {
333 public:
334 ForEachSelected( ExportData& _exporter )
335         : exporter( _exporter )
336 {}
337
338 void visit( scene::Instance& instance ) const {
339         BrushInstance* bptr = InstanceTypeCast<BrushInstance>::cast( instance );
340         if ( bptr ) {
341                 Brush& brush( bptr->getBrush() );
342
343                 exporter.BeginBrush( brush );
344                 ForEachFace face_vis( exporter );
345                 brush.forEachFace( face_vis );
346                 exporter.EndBrush();
347         }
348 }
349
350 private:
351 ExportData& exporter;
352 };
353
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 );
356
357         ForEachSelected vis( exporter );
358         GlobalSelectionSystem().foreachSelected( vis );
359
360         return exporter.WriteToFile( path, m );
361 }