00001 #ifndef WAVEFRONTBRUSHVISITOR_H_ 00002 #define WAVEFRONTBRUSHVISITOR_H_ 00003 00004 /* greebo: This visits every brush/face and exports it into the given TextFileOutputStream 00005 * 00006 * Inherits from BrushVisitor class in radiant/brush.h 00007 */ 00008 #include "../brush/brush.h" // for BrushVisitor declaration 00009 #include "stream/stringstream.h" 00010 #include "stream/textfilestream.h" 00011 00012 /* Exporterclass which will pass every visit-call to a special formatexporter. 00013 */ 00014 class CExportFormatWavefront: public BrushVisitor 00015 { 00016 TextFileOutputStream& m_file; 00017 00018 StringOutputStream vertexbuffer; 00019 StringOutputStream texcoordbuffer; 00020 StringOutputStream facebuffer; 00021 00022 size_t vertices; 00023 size_t exported; 00024 00025 public: 00026 00027 CExportFormatWavefront (TextFileOutputStream& file) : 00028 m_file(file) 00029 { 00030 exported = 0; 00031 vertices = 0; 00032 } 00033 00034 virtual ~CExportFormatWavefront (void) 00035 { 00036 } 00037 00038 void visit (scene::Instance& instance) 00039 { 00040 BrushInstance* bptr = InstanceTypeCast<BrushInstance>::cast(instance); 00041 if (bptr) { 00042 Brush& brush(bptr->getBrush()); 00043 m_file << "\ng " << brush.name() << exported << "\n"; 00044 brush.forEachFace(*this); 00045 m_file << vertexbuffer.c_str() << "\n"; 00046 m_file << texcoordbuffer.c_str(); 00047 m_file << facebuffer.c_str() << "\n"; 00048 vertexbuffer.clear(); 00049 texcoordbuffer.clear(); 00050 facebuffer.clear(); 00051 ++exported; 00052 } 00053 } 00054 00055 void visit (Face& face) const 00056 { 00057 // cast the stupid const away 00058 const_cast<CExportFormatWavefront*> (this)->visit(face); 00059 } 00060 00061 void visit (Face& face) 00062 { 00063 size_t v_start = vertices; 00064 const Winding& w(face.getWinding()); 00065 for (size_t i = 0; i < w.numpoints; ++i) { 00066 vertexbuffer << "v " << w[i].vertex.x() << " " << w[i].vertex.y() << " " << w[i].vertex.z() << "\n"; 00067 texcoordbuffer << "vt " << w[i].texcoord.x() << " " << w[i].texcoord.y() << "\n"; 00068 ++vertices; 00069 } 00070 00071 facebuffer << "\nf"; 00072 for (size_t i = v_start; i < vertices; ++i) 00073 facebuffer << " " << i + 1 << "/" << i + 1; 00074 } 00075 }; // class CExportFormatWavefront 00076 00077 #endif /*WAVEFRONTBRUSHVISITOR_H_*/