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 Abstract baseclass for modelexporters
16 the class collects all the data which then gets
17 exported through the WriteToFile method.
22 ExportData(const std::set<std::string>& ignorelist, collapsemode mode, bool limNames, bool objs);
23 virtual ~ExportData(void);
25 virtual void BeginBrush(Brush& b);
26 virtual void AddBrushFace(Face& f);
27 virtual void EndBrush(void);
29 virtual bool WriteToFile(const std::string& path, collapsemode mode) const = 0;
38 std::list<const Face*> faces;
41 std::list<group> groups;
45 // "textures/common/caulk" -> "caulk"
46 void GetShaderNameFromShaderPath(const char* path, std::string& name);
50 const std::set<std::string>& ignorelist;
53 ExportData::ExportData(const std::set<std::string>& _ignorelist, collapsemode _mode, bool _limNames, bool _objs)
55 ignorelist(_ignorelist)
59 // in this mode, we need just one group
60 if(mode == COLLAPSE_ALL)
62 groups.push_back(group());
63 current = &groups.back();
64 current->name = "all";
68 ExportData::~ExportData(void)
73 void ExportData::BeginBrush(Brush& b)
75 // create a new group for each brush
76 if(mode == COLLAPSE_NONE)
78 groups.push_back(group());
79 current = &groups.back();
81 StringOutputStream str(256);
82 str << "Brush" << (const unsigned int)groups.size();
83 current->name = str.c_str();
87 void ExportData::EndBrush(void)
89 // all faces of this brush were on the ignorelist, discard the emptygroup
90 if(mode == COLLAPSE_NONE)
92 ASSERT_NOTNULL(current);
93 if(current->faces.empty())
101 void ExportData::AddBrushFace(Face& f)
103 std::string shadername;
104 GetShaderNameFromShaderPath(f.GetShader(), shadername);
106 // ignore faces from ignore list
107 if(ignorelist.find(shadername) != ignorelist.end())
110 if(mode == COLLAPSE_BY_MATERIAL)
112 // find a group for this material
114 const std::list<group>::iterator end(groups.end());
115 for(std::list<group>::iterator it(groups.begin()); it != end; ++it)
117 if(it->name == shadername)
121 // no group found, create one
124 groups.push_back(group());
125 current = &groups.back();
126 current->name = shadername;
130 ASSERT_NOTNULL(current);
132 // add face to current group
133 current->faces.push_back(&f);
136 globalOutputStream() << "Added Face to group " << current->name.c_str() << "\n";
140 void ExportData::GetShaderNameFromShaderPath(const char* path, std::string& name)
142 std::string tmp(path);
144 size_t last_slash = tmp.find_last_of("/");
146 if(last_slash != std::string::npos && last_slash == (tmp.length() - 1))
149 name = tmp.substr(last_slash + 1, tmp.length() - last_slash);
152 globalOutputStream() << "Last: " << last_slash << " " << "length: " << (const unsigned int)tmp.length() << "Name: " << name.c_str() << "\n";
157 Exporter writing facedata as wavefront object
159 class ExportDataAsWavefront : public ExportData
167 ExportDataAsWavefront(const std::set<std::string>& _ignorelist, collapsemode _mode, bool _expmat, bool _limNames, bool _objs)
168 : ExportData(_ignorelist, _mode, _limNames, _objs)
171 limNames = _limNames;
175 bool WriteToFile(const std::string& path, collapsemode mode) const;
178 bool ExportDataAsWavefront::WriteToFile(const std::string& path, collapsemode mode) const
180 std::string objFile = path.substr(0, path.length() -4) + ".obj";
181 std::string mtlFile = path.substr(0, path.length() -4) + ".mtl";
183 std::set<std::string> materials;
185 TextFileOutputStream out(objFile.c_str());
189 globalErrorStream() << "Unable to open file\n";
193 out << "# Wavefront Objectfile exported with radiants brushexport plugin 3.0 by Thomas 'namespace' Nitschke, spam@codecreator.net\n\n";
197 size_t last = mtlFile.find_last_of("//");
198 std::string mtllib = mtlFile.substr(last + 1, mtlFile.size() - last).c_str();
199 out << "mtllib " << mtllib.c_str() << "\n";
202 unsigned int vertex_count = 0;
204 const std::list<ExportData::group>::const_iterator gend(groups.end());
205 for(std::list<ExportData::group>::const_iterator git(groups.begin()); git != gend; ++git)
207 typedef std::multimap<std::string, std::string> bm;
209 typedef std::pair<std::string, std::string> String_Pair;
211 const std::list<const Face*>::const_iterator end(git->faces.end());
213 // submesh starts here
220 out << git->name.c_str() << "\n";
223 if(expmat && mode == COLLAPSE_ALL)
225 out << "usemtl material" << "\n\n";
226 materials.insert("material");
229 for(std::list<const Face*>::const_iterator it(git->faces.begin()); it != end; ++it)
231 const Winding& w((*it)->getWinding());
234 for(size_t i = 0; i < w.numpoints; ++i)
235 out << "v " << FloatFormat(w[i].vertex.x(), 1, 6) << " " << FloatFormat(w[i].vertex.z(), 1, 6) << " " << FloatFormat(w[i].vertex.y(), 1, 6) << "\n";
239 for(std::list<const Face*>::const_iterator it(git->faces.begin()); it != end; ++it)
241 const Winding& w((*it)->getWinding());
244 for(size_t i = 0; i < w.numpoints; ++i)
245 out << "vt " << FloatFormat(w[i].texcoord.x(), 1, 6) << " " << FloatFormat(w[i].texcoord.y(), 1, 6) << "\n";
248 for(std::list<const Face*>::const_iterator it(git->faces.begin()); it != end; ++it)
250 const Winding& w((*it)->getWinding());
253 StringOutputStream faceLine(256);
255 for(size_t i = 0; i < w.numpoints; ++i, ++vertex_count)
257 faceLine << " " << vertex_count+1 << "/" << vertex_count+1;
260 if(mode != COLLAPSE_ALL)
262 materials.insert((*it)->getShader().getShader());
263 brushMaterials.insert(String_Pair((*it)->getShader().getShader(), faceLine.c_str()));
265 out << faceLine.c_str();
269 if(mode != COLLAPSE_ALL)
275 for(bm::iterator iter = brushMaterials.begin(); iter != brushMaterials.end(); iter++)
277 mat = (*iter).first.c_str();
278 faces = (*iter).second.c_str();
282 if(limNames && mat.size() > 20)
284 out << "\nusemtl " << mat.substr(mat.size() - 20, mat.size()).c_str();
286 out << "\nusemtl " << mat.c_str();
290 out << faces.c_str();
300 TextFileOutputStream outMtl(mtlFile.c_str());
303 globalErrorStream() << "Unable to open material file\n";
307 outMtl << "# Wavefront material file exported with GtkRadiants brushexport plugin.\n";
308 outMtl << "# Material Count: " << (const Unsigned)materials.size() << "\n\n";
309 for(std::set<std::string>::const_iterator it(materials.begin()); it != materials.end(); ++it)
311 if(limNames && it->size() > 20)
313 outMtl << "newmtl " << it->substr(it->size() - 20, it->size()).c_str() << "\n";
315 outMtl << "newmtl " << it->c_str() << "\n";
324 class ForEachFace : public BrushVisitor
327 ForEachFace(ExportData& _exporter)
328 : exporter(_exporter)
331 void visit(Face& face) const
333 exporter.AddBrushFace(face);
337 ExportData& exporter;
340 class ForEachSelected : public SelectionSystem::Visitor
343 ForEachSelected(ExportData& _exporter)
344 : exporter(_exporter)
347 void visit(scene::Instance& instance) const
349 BrushInstance* bptr = InstanceTypeCast<BrushInstance>::cast(instance);
352 Brush& brush(bptr->getBrush());
354 exporter.BeginBrush(brush);
355 ForEachFace face_vis(exporter);
356 brush.forEachFace(face_vis);
362 ExportData& exporter;
365 bool ExportSelection(const std::set<std::string>& ignorelist, collapsemode m, bool exmat, const std::string& path, bool limNames, bool objs)
367 ExportDataAsWavefront exporter(ignorelist, m, exmat, limNames, objs);
369 ForEachSelected vis(exporter);
370 GlobalSelectionSystem().foreachSelected(vis);
372 return exporter.WriteToFile(path, m);