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 #define MAX_MATERIAL_NAME 20
18 Abstract baseclass for modelexporters
19 the class collects all the data which then gets
20 exported through the WriteToFile method.
25 ExportData(const std::set<std::string>& ignorelist, collapsemode mode, bool limNames, bool objs);
26 virtual ~ExportData(void);
28 virtual void BeginBrush(Brush& b);
29 virtual void AddBrushFace(Face& f);
30 virtual void EndBrush(void);
32 virtual bool WriteToFile(const std::string& path, collapsemode mode) const = 0;
41 std::list<const Face*> faces;
44 std::list<group> groups;
48 // "textures/common/caulk" -> "caulk"
49 void GetShaderNameFromShaderPath(const char* path, std::string& name);
53 const std::set<std::string>& ignorelist;
56 ExportData::ExportData(const std::set<std::string>& _ignorelist, collapsemode _mode, bool _limNames, bool _objs)
58 ignorelist(_ignorelist)
62 // in this mode, we need just one group
63 if(mode == COLLAPSE_ALL)
65 groups.push_back(group());
66 current = &groups.back();
67 current->name = "all";
71 ExportData::~ExportData(void)
76 void ExportData::BeginBrush(Brush& b)
78 // create a new group for each brush
79 if(mode == COLLAPSE_NONE)
81 groups.push_back(group());
82 current = &groups.back();
84 StringOutputStream str(256);
85 str << "Brush" << (const unsigned int)groups.size();
86 current->name = str.c_str();
90 void ExportData::EndBrush(void)
92 // all faces of this brush were on the ignorelist, discard the emptygroup
93 if(mode == COLLAPSE_NONE)
95 ASSERT_NOTNULL(current);
96 if(current->faces.empty())
104 void ExportData::AddBrushFace(Face& f)
106 std::string shadername;
107 GetShaderNameFromShaderPath(f.GetShader(), shadername);
109 // ignore faces from ignore list
110 if(ignorelist.find(shadername) != ignorelist.end())
113 if(mode == COLLAPSE_BY_MATERIAL)
115 // find a group for this material
117 const std::list<group>::iterator end(groups.end());
118 for(std::list<group>::iterator it(groups.begin()); it != end; ++it)
120 if(it->name == shadername)
124 // no group found, create one
127 groups.push_back(group());
128 current = &groups.back();
129 current->name = shadername;
133 ASSERT_NOTNULL(current);
135 // add face to current group
136 current->faces.push_back(&f);
139 globalOutputStream() << "Added Face to group " << current->name.c_str() << "\n";
143 void ExportData::GetShaderNameFromShaderPath(const char* path, std::string& name)
145 std::string tmp(path);
147 size_t last_slash = tmp.find_last_of("/");
149 if(last_slash != std::string::npos && last_slash == (tmp.length() - 1))
152 name = tmp.substr(last_slash + 1, tmp.length() - last_slash);
155 globalOutputStream() << "Last: " << (const unsigned int) last_slash << " " << "length: " << (const unsigned int)tmp.length() << "Name: " << name.c_str() << "\n";
160 Exporter writing facedata as wavefront object
162 class ExportDataAsWavefront : public ExportData
170 ExportDataAsWavefront(const std::set<std::string>& _ignorelist, collapsemode _mode, bool _expmat, bool _limNames, bool _objs)
171 : ExportData(_ignorelist, _mode, _limNames, _objs)
174 limNames = _limNames;
178 bool WriteToFile(const std::string& path, collapsemode mode) const;
181 bool ExportDataAsWavefront::WriteToFile(const std::string& path, collapsemode mode) const
183 std::string objFile = path;
185 if(path.compare(path.length() - 4, 4, ".obj") != 0)
188 std::string mtlFile = objFile.substr(0, objFile.length() -4) + ".mtl";
190 std::set<std::string> materials;
192 TextFileOutputStream out(objFile.c_str());
196 globalErrorStream() << "Unable to open file\n";
200 out << "# Wavefront Objectfile exported with radiants brushexport plugin 3.0 by Thomas 'namespace' Nitschke, spam@codecreator.net\n\n";
204 size_t last = mtlFile.find_last_of("//");
205 std::string mtllib = mtlFile.substr(last + 1, mtlFile.size() - last).c_str();
206 out << "mtllib " << mtllib.c_str() << "\n";
209 unsigned int vertex_count = 0;
211 const std::list<ExportData::group>::const_iterator gend(groups.end());
212 for(std::list<ExportData::group>::const_iterator git(groups.begin()); git != gend; ++git)
214 typedef std::multimap<std::string, std::string> bm;
216 typedef std::pair<std::string, std::string> String_Pair;
218 const std::list<const Face*>::const_iterator end(git->faces.end());
220 // submesh starts here
227 out << git->name.c_str() << "\n";
230 if(expmat && mode == COLLAPSE_ALL)
232 out << "usemtl material" << "\n\n";
233 materials.insert("material");
236 for(std::list<const Face*>::const_iterator it(git->faces.begin()); it != end; ++it)
238 const Winding& w((*it)->getWinding());
241 for(size_t i = 0; i < w.numpoints; ++i)
242 out << "v " << FloatFormat(w[i].vertex.x(), 1, 6) << " " << FloatFormat(w[i].vertex.z(), 1, 6) << " " << FloatFormat(w[i].vertex.y(), 1, 6) << "\n";
246 for(std::list<const Face*>::const_iterator it(git->faces.begin()); it != end; ++it)
248 const Winding& w((*it)->getWinding());
251 for(size_t i = 0; i < w.numpoints; ++i)
252 out << "vt " << FloatFormat(w[i].texcoord.x(), 1, 6) << " " << FloatFormat(w[i].texcoord.y(), 1, 6) << "\n";
255 for(std::list<const Face*>::const_iterator it(git->faces.begin()); it != end; ++it)
257 const Winding& w((*it)->getWinding());
260 StringOutputStream faceLine(256);
262 for(size_t i = 0; i < w.numpoints; ++i, ++vertex_count)
264 faceLine << " " << vertex_count+1 << "/" << vertex_count+1;
267 if(mode != COLLAPSE_ALL)
269 materials.insert((*it)->getShader().getShader());
270 brushMaterials.insert(String_Pair((*it)->getShader().getShader(), faceLine.c_str()));
272 out << faceLine.c_str();
276 if(mode != COLLAPSE_ALL)
282 for(bm::iterator iter = brushMaterials.begin(); iter != brushMaterials.end(); iter++)
284 mat = (*iter).first.c_str();
285 faces = (*iter).second.c_str();
289 if(limNames && mat.size() > MAX_MATERIAL_NAME)
291 out << "\nusemtl " << mat.substr(mat.size() - MAX_MATERIAL_NAME, mat.size()).c_str();
293 out << "\nusemtl " << mat.c_str();
297 out << faces.c_str();
307 TextFileOutputStream outMtl(mtlFile.c_str());
310 globalErrorStream() << "Unable to open material file\n";
314 outMtl << "# Wavefront material file exported with NetRadiants brushexport plugin.\n";
315 outMtl << "# Material Count: " << (const Unsigned)materials.size() << "\n\n";
316 for(std::set<std::string>::const_iterator it(materials.begin()); it != materials.end(); ++it)
318 if(limNames && it->size() > MAX_MATERIAL_NAME)
320 outMtl << "newmtl " << it->substr(it->size() - MAX_MATERIAL_NAME, it->size()).c_str() << "\n";
322 outMtl << "newmtl " << it->c_str() << "\n";
331 class ForEachFace : public BrushVisitor
334 ForEachFace(ExportData& _exporter)
335 : exporter(_exporter)
338 void visit(Face& face) const
340 exporter.AddBrushFace(face);
344 ExportData& exporter;
347 class ForEachSelected : public SelectionSystem::Visitor
350 ForEachSelected(ExportData& _exporter)
351 : exporter(_exporter)
354 void visit(scene::Instance& instance) const
356 BrushInstance* bptr = InstanceTypeCast<BrushInstance>::cast(instance);
359 Brush& brush(bptr->getBrush());
361 exporter.BeginBrush(brush);
362 ForEachFace face_vis(exporter);
363 brush.forEachFace(face_vis);
369 ExportData& exporter;
372 bool ExportSelection(const std::set<std::string>& ignorelist, collapsemode m, bool exmat, const std::string& path, bool limNames, bool objs)
374 ExportDataAsWavefront exporter(ignorelist, m, exmat, limNames, objs);
376 ForEachSelected vis(exporter);
377 GlobalSelectionSystem().foreachSelected(vis);
379 return exporter.WriteToFile(path, m);