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