2 #include "globaldefs.h"
3 #include "debugging/debugging.h"
5 #include "iscenegraph.h"
6 #include "iselection.h"
7 #include "stream/stringstream.h"
8 #include "stream/textfilestream.h"
12 // this is very evil, but right now there is no better way
13 #include "../../radiant/brush.h"
16 const int MAX_MATERIAL_NAME = 20;
19 Abstract baseclass for modelexporters
20 the class collects all the data which then gets
21 exported through the WriteToFile method.
25 ExportData(const std::set<std::string> &ignorelist, collapsemode mode, bool limNames, bool objs);
27 virtual ~ExportData(void);
29 virtual void BeginBrush(Brush &b);
31 virtual void AddBrushFace(Face &f);
33 virtual void EndBrush(void);
35 virtual bool WriteToFile(const std::string &path, collapsemode mode) const = 0;
43 std::list<const Face *> faces;
46 std::list<group> groups;
50 // "textures/common/caulk" -> "caulk"
51 void GetShaderNameFromShaderPath(const char *path, std::string &name);
55 const std::set<std::string> &ignorelist;
58 ExportData::ExportData(const std::set<std::string> &_ignorelist, collapsemode _mode, bool _limNames, bool _objs)
60 ignorelist(_ignorelist)
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";
72 ExportData::~ExportData(void)
77 void ExportData::BeginBrush(Brush &b)
79 // create a new group for each brush
80 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) {
94 ASSERT_NOTNULL(current);
95 if (current->faces.empty()) {
102 void ExportData::AddBrushFace(Face &f)
104 std::string shadername;
105 GetShaderNameFromShaderPath(f.GetShader(), shadername);
107 // ignore faces from ignore list
108 if (ignorelist.find(shadername) != ignorelist.end()) {
112 if (mode == COLLAPSE_BY_MATERIAL) {
113 // find a group for this material
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) {
122 // 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);
153 globalOutputStream() << "Last: " << (const unsigned int) last_slash << " " << "length: "
154 << (const unsigned int) tmp.length() << "Name: " << name.c_str() << "\n";
159 Exporter writing facedata as wavefront object
161 class ExportDataAsWavefront : public ExportData {
168 ExportDataAsWavefront(const std::set<std::string> &_ignorelist, collapsemode _mode, bool _expmat, bool _limNames,
170 : ExportData(_ignorelist, _mode, _limNames, _objs)
173 limNames = _limNames;
177 bool WriteToFile(const std::string &path, collapsemode mode) const;
180 bool ExportDataAsWavefront::WriteToFile(const std::string &path, collapsemode mode) const
182 std::string objFile = path;
184 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());
195 globalErrorStream() << "Unable to open file\n";
200 << "# Wavefront Objectfile exported with radiants brushexport plugin 3.0 by Thomas 'namespace' Nitschke, spam@codecreator.net\n\n";
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";
208 unsigned int vertex_count = 0;
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;
214 typedef std::pair<std::string, std::string> String_Pair;
216 const std::list<const Face *>::const_iterator end(git->faces.end());
218 // submesh starts here
224 out << git->name.c_str() << "\n";
227 if (expmat && mode == COLLAPSE_ALL) {
228 out << "usemtl material" << "\n\n";
229 materials.insert("material");
232 for (std::list<const Face *>::const_iterator it(git->faces.begin()); it != end; ++it) {
233 const Winding &w((*it)->getWinding());
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";
243 for (std::list<const Face *>::const_iterator it(git->faces.begin()); it != end; ++it) {
244 const Winding &w((*it)->getWinding());
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)
253 for (std::list<const Face *>::const_iterator it(git->faces.begin()); it != end; ++it) {
254 const Winding &w((*it)->getWinding());
257 StringOutputStream faceLine(256);
259 for (size_t i = 0; i < w.numpoints; ++i, ++vertex_count) {
260 faceLine << " " << vertex_count + 1 << "/" << vertex_count + 1;
263 if (mode != COLLAPSE_ALL) {
264 materials.insert((*it)->getShader().getShader());
265 brushMaterials.insert(String_Pair((*it)->getShader().getShader(), faceLine.c_str()));
267 out << faceLine.c_str();
271 if (mode != COLLAPSE_ALL) {
276 for (bm::iterator iter = brushMaterials.begin(); iter != brushMaterials.end(); iter++) {
277 mat = (*iter).first.c_str();
278 faces = (*iter).second.c_str();
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();
284 out << "\nusemtl " << mat.c_str();
288 out << faces.c_str();
297 TextFileOutputStream outMtl(mtlFile.c_str());
298 if (outMtl.failed()) {
299 globalErrorStream() << "Unable to open material file\n";
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";
309 outMtl << "newmtl " << it->c_str() << "\n";
318 class ForEachFace : public BrushVisitor {
320 ForEachFace(ExportData &_exporter)
321 : exporter(_exporter)
324 void visit(Face &face) const
326 exporter.AddBrushFace(face);
330 ExportData &exporter;
333 class ForEachSelected : public SelectionSystem::Visitor {
335 ForEachSelected(ExportData &_exporter)
336 : exporter(_exporter)
339 void visit(scene::Instance &instance) const
341 BrushInstance *bptr = InstanceTypeCast<BrushInstance>::cast(instance);
343 Brush &brush(bptr->getBrush());
345 exporter.BeginBrush(brush);
346 ForEachFace face_vis(exporter);
347 brush.forEachFace(face_vis);
353 ExportData &exporter;
356 bool ExportSelection(const std::set<std::string> &ignorelist, collapsemode m, bool exmat, const std::string &path,
357 bool limNames, bool objs)
359 ExportDataAsWavefront exporter(ignorelist, m, exmat, limNames, objs);
361 ForEachSelected vis(exporter);
362 GlobalSelectionSystem().foreachSelected(vis);
364 return exporter.WriteToFile(path, m);