2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_BRUSHXML_H)
23 #define INCLUDED_BRUSHXML_H
25 #include "stream/stringstream.h"
26 #include "xml/xmlelement.h"
30 inline void FaceTexdef_BP_importXML(FaceTexdef& texdef, const char* xmlContent)
32 StringTokeniser content(xmlContent);
34 texdef.m_projection.m_brushprimit_texdef.coords[0][0] = static_cast<float>(atof(content.getToken()));
35 texdef.m_projection.m_brushprimit_texdef.coords[0][1] = static_cast<float>(atof(content.getToken()));
36 texdef.m_projection.m_brushprimit_texdef.coords[0][2] = static_cast<float>(atof(content.getToken()));
37 texdef.m_projection.m_brushprimit_texdef.coords[1][0] = static_cast<float>(atof(content.getToken()));
38 texdef.m_projection.m_brushprimit_texdef.coords[1][1] = static_cast<float>(atof(content.getToken()));
39 texdef.m_projection.m_brushprimit_texdef.coords[1][2] = static_cast<float>(atof(content.getToken()));
41 inline void FaceTexdef_importXML(FaceTexdef& texdef, const char* xmlContent)
43 StringTokeniser content(xmlContent);
45 texdef.m_projection.m_texdef.shift[0] = static_cast<float>(atof(content.getToken()));
46 texdef.m_projection.m_texdef.shift[1] = static_cast<float>(atof(content.getToken()));
47 texdef.m_projection.m_texdef.rotate = static_cast<float>(atof(content.getToken()));
48 texdef.m_projection.m_texdef.scale[0] = static_cast<float>(atof(content.getToken()));
49 texdef.m_projection.m_texdef.scale[1] = static_cast<float>(atof(content.getToken()));
51 ASSERT_MESSAGE(texdef_sane(texdef.m_projection.m_texdef), "FaceTexdef_importXML: bad texdef");
54 inline void FacePlane_importXML(FacePlane& facePlane, const char* xmlContent)
56 StringTokeniser content(xmlContent);
58 for (int i = 0; i < 3; ++i)
60 for (int j = 0; j < 3; ++j)
62 facePlane.planePoints()[i][j] = atof(content.getToken());
65 facePlane.MakePlane();
84 StringOutputStream m_content;
86 xml_state_t(EState state)
94 const char* content() const
96 return m_content.c_str();
98 std::size_t write(const char* buffer, std::size_t length)
100 return m_content.write(buffer, length);
104 std::vector<xml_state_t> m_xml_state;
107 FaceXMLImporter(Face& face) : m_face(face)
109 m_xml_state.push_back(xml_state_t::eDefault);
113 m_face.planeChanged();
116 void pushElement(const XMLElement& element)
118 ASSERT_MESSAGE(m_xml_state.back().state() == xml_state_t::eDefault, "parse error");
120 if(strcmp(element.name(), "planepts") == 0)
122 m_xml_state.push_back(xml_state_t::ePlanePts);
124 else if(strcmp(element.name(), "texdef") == 0)
126 m_xml_state.push_back(xml_state_t::eTexdef);
128 else if(strcmp(element.name(), "bpmatrix") == 0)
130 m_xml_state.push_back(xml_state_t::eBPMatrix);
132 else if(strcmp(element.name(), "flags") == 0)
134 m_xml_state.push_back(xml_state_t::eFlags);
136 else if(strcmp(element.name(), "shader") == 0)
138 m_xml_state.push_back(xml_state_t::eShader);
141 void popElement(const char* name)
143 ASSERT_MESSAGE(m_xml_state.back().state() != xml_state_t::eDefault, "parse error");
145 switch(m_xml_state.back().state())
147 case xml_state_t::ePlanePts:
149 FacePlane_importXML(m_face.getPlane(), m_xml_state.back().content());
152 case xml_state_t::eTexdef:
154 FaceTexdef_importXML(m_face.getTexdef(), m_xml_state.back().content());
157 case xml_state_t::eBPMatrix:
159 FaceTexdef_BP_importXML(m_face.getTexdef(), m_xml_state.back().content());
162 case xml_state_t::eFlags:
164 StringTokeniser content(m_xml_state.back().content());
166 m_face.getShader().m_flags.m_contentFlags = atoi(content.getToken());
167 m_face.getShader().m_flags.m_surfaceFlags = atoi(content.getToken());
168 m_face.getShader().m_flags.m_value = atoi(content.getToken());
171 case xml_state_t::eShader:
173 m_face.getShader().setShader(m_xml_state.back().content());
180 m_xml_state.pop_back();
182 std::size_t write(const char* data, std::size_t length)
184 ASSERT_MESSAGE(!m_xml_state.empty(), "parse error");
185 return m_xml_state.back().write(data, length);
190 inline void FaceTexdef_exportXML(const FaceTexdef& texdef, XMLImporter& importer)
192 StaticElement element("texdef");
193 importer.pushElement(element);
195 ASSERT_MESSAGE(texdef_sane(texdef.m_projection.m_texdef), "FaceTexdef_exportXML: bad texdef");
197 importer << texdef.m_projection.m_texdef.shift[0]
198 << ' ' << texdef.m_projection.m_texdef.shift[1]
199 << ' ' << texdef.m_projection.m_texdef.rotate
200 << ' ' << texdef.m_projection.m_texdef.scale[0]
201 << ' ' << texdef.m_projection.m_texdef.scale[1];
203 importer.popElement(element.name());
205 inline void FaceTexdef_BP_exportXML(const FaceTexdef& texdef, XMLImporter& importer)
207 StaticElement element("texdef");
208 importer.pushElement(element);
210 for(int i = 0; i < 2; ++i)
212 for(int j = 0; j < 3; ++j)
214 importer << texdef.m_projection.m_brushprimit_texdef.coords[i][j] << ' ';
218 importer.popElement(element.name());
220 inline void FaceShader_ContentsFlagsValue_exportXML(const FaceShader& faceShader, XMLImporter& importer)
222 StaticElement element("flags");
223 importer.pushElement(element);
226 importer << faceShader.m_flags.m_contentFlags
227 << ' ' << faceShader.m_flags.m_surfaceFlags
228 << ' ' << faceShader.m_flags.m_value;
231 importer.popElement(element.name());
234 inline void FacePlane_exportXML(const FacePlane& facePlane, XMLImporter& importer)
236 StaticElement element("planepts");
237 importer.pushElement(element);
241 for (int i=0 ; i<3 ; i++)
243 for (int j=0 ; j<3 ; j++)
245 importer << Face::m_quantise(facePlane.planePoints()[i][j]) << ' ';
250 importer.popElement(element.name());
253 class FaceXMLExporter
257 FaceXMLExporter(const Face& face) : m_face(face)
260 void exportXML(XMLImporter& importer)
262 bool bAlternateTexdef = (Face::m_type == eBrushTypeQuake3BP || Face::m_type == eBrushTypeDoom3 || Face::m_type == eBrushTypeQuake4);
266 StaticElement element("shader");
267 importer.pushElement(element);
268 importer << m_face.getShader().getShader();
269 importer.popElement(element.name());
272 FacePlane_exportXML(m_face.getPlane(), importer);
274 if(!bAlternateTexdef)
276 FaceTexdef_exportXML(m_face.getTexdef(), importer);
280 FaceTexdef_BP_exportXML(m_face.getTexdef(), importer);
283 FaceShader_ContentsFlagsValue_exportXML(m_face.getShader(), importer);
288 class BrushXMLImporter : public XMLImporter
304 xml_state_t(EState state)
314 std::vector<xml_state_t> m_xml_state;
315 char m_faceImporter[sizeof(FaceXMLImporter)];
318 FaceXMLImporter& faceImporter()
320 return *reinterpret_cast<FaceXMLImporter*>(m_faceImporter);
324 BrushXMLImporter(Brush& brush) : m_brush(brush)
326 m_xml_state.push_back(xml_state_t::eDefault);
328 void pushElement(const XMLElement& element)
330 switch(m_xml_state.back().state())
332 case xml_state_t::eDefault:
333 ASSERT_MESSAGE(strcmp(element.name(), "brush") == 0, "parse error");
334 m_xml_state.push_back(xml_state_t::eBrush);
336 case xml_state_t::eBrush:
337 ASSERT_MESSAGE(strcmp(element.name(), "plane") == 0, "parse error");
338 m_xml_state.push_back(xml_state_t::eFace);
339 m_brush.push_back(FaceSmartPointer(new Face(&m_brush)));
340 constructor(faceImporter(), makeReference(*m_brush.back()));
341 m_brush.planeChanged();
342 m_brush.shaderChanged();
344 case xml_state_t::eFace:
345 m_xml_state.push_back(xml_state_t::eFace);
346 faceImporter().pushElement(element);
350 void popElement(const char* name)
352 ASSERT_MESSAGE(!m_xml_state.empty(), "parse error");
353 m_xml_state.pop_back();
355 switch(m_xml_state.back().state())
357 case xml_state_t::eDefault:
359 case xml_state_t::eBrush:
360 destructor(faceImporter());
362 case xml_state_t::eFace:
363 faceImporter().popElement(name);
367 std::size_t write(const char* data, std::size_t length)
369 switch(m_xml_state.back().state())
371 case xml_state_t::eFace:
372 return faceImporter().write(data, length);
381 class BrushXMLExporter : public XMLExporter
383 const Brush& m_brush;
386 BrushXMLExporter(const Brush& brush) : m_brush(brush)
389 void exportXML(XMLImporter& importer)
391 m_brush.evaluateBRep(); // ensure b-rep is up-to-date, so that non-contributing faces can be identified.
392 ASSERT_MESSAGE(m_brush.hasContributingFaces(), "exporting an empty brush");
394 const StaticElement brushElement("brush");
395 importer.pushElement(brushElement);
397 for(Brush::const_iterator i = m_brush.begin(); i != m_brush.end(); ++i)
399 if((*i)->contributes())
401 const StaticElement element("plane");
402 importer.pushElement(element);
403 FaceXMLExporter(*(*i)).exportXML(importer);
404 importer.popElement(element.name());
408 importer.popElement(brushElement.name());