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