2 #include "debugging/debugging.h"
\r
4 #include "iscenegraph.h"
\r
5 #include "iselection.h"
\r
6 #include "stream/stringstream.h"
\r
7 #include "stream/textfilestream.h"
\r
9 // this is very evil, but right now there is no better way
\r
10 #include "../../radiant/brush.h"
\r
13 Abstract baseclass for modelexporters
\r
14 the class collects all the data which then gets
\r
15 exported through the WriteToFile method.
\r
20 ExportData(const std::set<std::string>& ignorelist, collapsemode mode);
\r
21 virtual ~ExportData(void);
\r
23 virtual void BeginBrush(Brush& b);
\r
24 virtual void AddBrushFace(Face& f);
\r
25 virtual void EndBrush(void);
\r
27 virtual bool WriteToFile(const std::string& path) const = 0;
\r
36 std::list<const Face*> faces;
\r
39 std::list<group> groups;
\r
43 // "textures/common/caulk" -> "caulk"
\r
44 void GetShaderNameFromShaderPath(const char* path, std::string& name);
\r
48 const std::set<std::string>& ignorelist;
\r
51 ExportData::ExportData(const std::set<std::string>& _ignorelist, collapsemode _mode)
\r
53 ignorelist(_ignorelist)
\r
57 // in this mode, we need just one group
\r
58 if(mode == COLLAPSE_ALL)
\r
60 groups.push_back(group());
\r
61 current = &groups.back();
\r
62 current->name = "all";
\r
66 ExportData::~ExportData(void)
\r
71 void ExportData::BeginBrush(Brush& b)
\r
73 // create a new group for each brush
\r
74 if(mode == COLLAPSE_NONE)
\r
76 groups.push_back(group());
\r
77 current = &groups.back();
\r
79 StringOutputStream str(256);
\r
80 str << "Brush" << (const unsigned int)groups.size();
\r
81 current->name = str.c_str();
\r
85 void ExportData::EndBrush(void)
\r
87 // all faces of this brush were on the ignorelist, discard the emptygroup
\r
88 if(mode == COLLAPSE_NONE)
\r
90 ASSERT_NOTNULL(current);
\r
91 if(current->faces.empty())
\r
99 void ExportData::AddBrushFace(Face& f)
\r
101 std::string shadername;
\r
102 GetShaderNameFromShaderPath(f.GetShader(), shadername);
\r
104 // faces mit materials auf der ignoreliste ignorieren
\r
105 if(ignorelist.find(shadername) != ignorelist.end())
\r
108 if(mode == COLLAPSE_BY_MATERIAL)
\r
110 // find a group for this material
\r
112 const std::list<group>::iterator end(groups.end());
\r
113 for(std::list<group>::iterator it(groups.begin()); it != end; ++it)
\r
115 if(it->name == shadername)
\r
119 // no group found, create one
\r
122 groups.push_back(group());
\r
123 current = &groups.back();
\r
124 current->name = shadername;
\r
128 ASSERT_NOTNULL(current);
\r
130 // add face to current group
\r
131 current->faces.push_back(&f);
\r
134 globalOutputStream() << "Added Face to group " << current->name.c_str() << "\n";
\r
138 void ExportData::GetShaderNameFromShaderPath(const char* path, std::string& name)
\r
140 std::string tmp(path);
\r
142 size_t last_slash = tmp.find_last_of("/");
\r
144 if(last_slash != std::string::npos && last_slash == (tmp.length() - 1))
\r
147 name = tmp.substr(last_slash + 1, tmp.length() - last_slash);
\r
149 globalOutputStream() << "Last: " << last_slash << " " << "lenght: " << (const unsigned int)tmp.length() << "Name: " << name.c_str() << "\n";
\r
153 Exporter writing facedata as wavefront object
\r
155 class ExportDataAsWavefront : public ExportData
\r
158 ExportDataAsWavefront(const std::set<std::string>& _ignorelist, collapsemode _mode)
\r
159 : ExportData(_ignorelist, _mode)
\r
163 bool WriteToFile(const std::string& path) const;
\r
166 bool ExportDataAsWavefront::WriteToFile(const std::string& path) const
\r
168 TextFileOutputStream out(path.c_str());
\r
172 globalErrorStream() << "Unable to open file\n";
\r
176 out << "# Wavefront Objectfile exported with radiants brushexport plugin 2.0 by Thomas 'namespace' Nitschke, spam@codecreator.net\n\n";
\r
178 unsigned int vertex_count = 0;
\r
180 const std::list<ExportData::group>::const_iterator gend(groups.end());
\r
181 for(std::list<ExportData::group>::const_iterator git(groups.begin()); git != gend; ++git)
\r
183 const std::list<const Face*>::const_iterator end(git->faces.end());
\r
185 // submesh starts here
\r
186 out << "\ng " << git->name.c_str() << "\n";
\r
188 for(std::list<const Face*>::const_iterator it(git->faces.begin()); it != end; ++it)
\r
190 const Winding& w((*it)->getWinding());
\r
193 for(size_t i = 0; i < w.numpoints; ++i)
\r
194 out << "v " << FloatFormat(w[i].vertex.x(), 1, 6) << " " << FloatFormat(w[i].vertex.y(), 1, 6) << " " << FloatFormat(w[i].vertex.z(), 1, 6) << "\n";
\r
198 for(std::list<const Face*>::const_iterator it(git->faces.begin()); it != end; ++it)
\r
200 const Winding& w((*it)->getWinding());
\r
203 for(size_t i = 0; i < w.numpoints; ++i)
\r
204 out << "vt " << FloatFormat(w[i].texcoord.x(), 1, 6) << " " << FloatFormat(w[i].texcoord.y(), 1, 6) << "\n";
\r
207 for(std::list<const Face*>::const_iterator it(git->faces.begin()); it != end; ++it)
\r
209 const Winding& w((*it)->getWinding());
\r
213 for(size_t i = 0; i < w.numpoints; ++i, ++vertex_count)
\r
215 out << " " << vertex_count+1 << "/" << vertex_count+1;
\r
225 class ForEachFace : public BrushVisitor
\r
228 ForEachFace(ExportData& _exporter)
\r
229 : exporter(_exporter)
\r
232 void visit(Face& face) const
\r
234 exporter.AddBrushFace(face);
\r
238 ExportData& exporter;
\r
241 class ForEachSelected : public SelectionSystem::Visitor
\r
244 ForEachSelected(ExportData& _exporter)
\r
245 : exporter(_exporter)
\r
248 void visit(scene::Instance& instance) const
\r
250 BrushInstance* bptr = InstanceTypeCast<BrushInstance>::cast(instance);
\r
253 Brush& brush(bptr->getBrush());
\r
255 exporter.BeginBrush(brush);
\r
256 ForEachFace face_vis(exporter);
\r
257 brush.forEachFace(face_vis);
\r
258 exporter.EndBrush();
\r
263 ExportData& exporter;
\r
266 bool ExportSelection(const std::set<std::string>& ignorelist, collapsemode m, const std::string& path)
\r
268 ExportDataAsWavefront exporter(ignorelist, m);
\r
270 ForEachSelected vis(exporter);
\r
271 GlobalSelectionSystem().foreachSelected(vis);
\r
273 return exporter.WriteToFile(path);
\r