1 #pragma once
2 
3 /* greebo: This visits every brush/face and exports it into the given TextFileOutputStream
4  *
5  * Inherits from BrushVisitor class in radiant/brush.h
6  */
7 #include "../brush/BrushVisit.h"
8 #include "stream/stringstream.h"
9 #include "stream/textfilestream.h"
10 
11 /* Exporterclass which will pass every visit-call to a special formatexporter.
12  */
13 class CExportFormatWavefront: public BrushVisitor
14 {
15 		TextFileOutputStream& m_file;
16 
17 		StringOutputStream vertexbuffer;
18 		StringOutputStream texcoordbuffer;
19 		StringOutputStream facebuffer;
20 
21 		size_t vertices;
22 		size_t exported;
23 
24 	public:
25 
CExportFormatWavefront(TextFileOutputStream & file)26 		CExportFormatWavefront (TextFileOutputStream& file) :
27 			m_file(file)
28 		{
29 			exported = 0;
30 			vertices = 0;
31 		}
32 
~CExportFormatWavefront(void)33 		virtual ~CExportFormatWavefront (void)
34 		{
35 		}
36 
visit(scene::Instance & instance)37 		void visit (scene::Instance& instance)
38 		{
39 			BrushInstance* bptr = Instance_getBrush(instance);
40 			if (bptr) {
41 				Brush& brush(bptr->getBrush());
42 				m_file << "\ng " << brush.name() << exported << "\n";
43 				brush.forEachFace(*this);
44 				m_file << vertexbuffer.c_str() << "\n";
45 				m_file << texcoordbuffer.c_str();
46 				m_file << facebuffer.c_str() << "\n";
47 				vertexbuffer.clear();
48 				texcoordbuffer.clear();
49 				facebuffer.clear();
50 				++exported;
51 			}
52 		}
53 
visit(Face & face)54 		void visit (Face& face) const
55 		{
56 			// cast the stupid const away
57 			const_cast<CExportFormatWavefront*> (this)->visit(face);
58 		}
59 
visit(Face & face)60 		void visit (Face& face)
61 		{
62 			size_t v_start = vertices;
63 			const Winding& w(face.getWinding());
64 			for (size_t i = 0; i < w.size(); ++i) {
65 				vertexbuffer << "v " << w[i].vertex.x() << " " << w[i].vertex.y() << " " << w[i].vertex.z() << "\n";
66 				texcoordbuffer << "vt " << w[i].texcoord.x() << " " << w[i].texcoord.y() << "\n";
67 				++vertices;
68 			}
69 
70 			facebuffer << "\nf";
71 			for (size_t i = v_start; i < vertices; ++i)
72 				facebuffer << " " << i + 1 << "/" << i + 1;
73 		}
74 }; // class CExportFormatWavefront
75