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 inline void FacePolygon_exportXML(const Winding& w, const BasicVector3<double>& normal, XMLImporter& importer)
255 DynamicElement element("polygon");
259 sprintf(tmp, "%f", normal.x());
260 element.insertAttribute("nx", tmp);
262 sprintf(tmp, "%f", normal.y());
263 element.insertAttribute("ny", tmp);
265 sprintf(tmp, "%f", normal.z());
266 element.insertAttribute("nz", tmp);
268 importer.pushElement(element);
270 for(unsigned int i = 0; i < w.numpoints; ++i)
272 DynamicElement c("vertex");
274 sprintf(tmp, "%f", w.points[i].vertex.x());
275 c.insertAttribute("x", tmp);
277 sprintf(tmp, "%f", w.points[i].vertex.y());
278 c.insertAttribute("y", tmp);
280 sprintf(tmp, "%f", w.points[i].vertex.z());
281 c.insertAttribute("z", tmp);
283 sprintf(tmp, "%f", w.points[i].texcoord.x());
284 c.insertAttribute("s", tmp);
286 sprintf(tmp, "%f", w.points[i].texcoord.y());
287 c.insertAttribute("t", tmp);
289 importer.pushElement(c);
290 importer.popElement(c.name());
293 importer.popElement(element.name());
296 class FaceXMLExporter
300 FaceXMLExporter(const Face& face) : m_face(face)
303 void exportXML(XMLImporter& importer)
305 bool bAlternateTexdef = (Face::m_type == eBrushTypeQuake3BP || Face::m_type == eBrushTypeDoom3 || Face::m_type == eBrushTypeQuake4);
309 StaticElement element("shader");
310 importer.pushElement(element);
311 importer << m_face.getShader().getShader();
312 importer.popElement(element.name());
315 FacePolygon_exportXML(m_face.getWinding(), m_face.getPlane().plane3().normal(), importer);
316 FacePlane_exportXML(m_face.getPlane(), importer);
318 if(!bAlternateTexdef)
320 FaceTexdef_exportXML(m_face.getTexdef(), importer);
324 FaceTexdef_BP_exportXML(m_face.getTexdef(), importer);
327 FaceShader_ContentsFlagsValue_exportXML(m_face.getShader(), importer);
332 class BrushXMLImporter : public XMLImporter
348 xml_state_t(EState state)
358 std::vector<xml_state_t> m_xml_state;
359 char m_faceImporter[sizeof(FaceXMLImporter)];
362 FaceXMLImporter& faceImporter()
364 return *reinterpret_cast<FaceXMLImporter*>(m_faceImporter);
368 BrushXMLImporter(Brush& brush) : m_brush(brush)
370 m_xml_state.push_back(xml_state_t::eDefault);
372 void pushElement(const XMLElement& element)
374 switch(m_xml_state.back().state())
376 case xml_state_t::eDefault:
377 ASSERT_MESSAGE(strcmp(element.name(), "brush") == 0, "parse error");
378 m_xml_state.push_back(xml_state_t::eBrush);
380 case xml_state_t::eBrush:
381 ASSERT_MESSAGE(strcmp(element.name(), "plane") == 0, "parse error");
382 m_xml_state.push_back(xml_state_t::eFace);
383 m_brush.push_back(FaceSmartPointer(new Face(&m_brush)));
384 constructor(faceImporter(), makeReference(*m_brush.back()));
385 m_brush.planeChanged();
386 m_brush.shaderChanged();
388 case xml_state_t::eFace:
389 m_xml_state.push_back(xml_state_t::eFace);
390 faceImporter().pushElement(element);
394 void popElement(const char* name)
396 ASSERT_MESSAGE(!m_xml_state.empty(), "parse error");
397 m_xml_state.pop_back();
399 switch(m_xml_state.back().state())
401 case xml_state_t::eDefault:
403 case xml_state_t::eBrush:
404 destructor(faceImporter());
406 case xml_state_t::eFace:
407 faceImporter().popElement(name);
411 std::size_t write(const char* data, std::size_t length)
413 switch(m_xml_state.back().state())
415 case xml_state_t::eFace:
416 return faceImporter().write(data, length);
425 class BrushXMLExporter : public XMLExporter
427 const Brush& m_brush;
430 BrushXMLExporter(const Brush& brush) : m_brush(brush)
433 void exportXML(XMLImporter& importer)
435 m_brush.evaluateBRep(); // ensure b-rep is up-to-date, so that non-contributing faces can be identified.
436 ASSERT_MESSAGE(m_brush.hasContributingFaces(), "exporting an empty brush");
438 const StaticElement brushElement("brush");
439 importer.pushElement(brushElement);
441 for(Brush::const_iterator i = m_brush.begin(); i != m_brush.end(); ++i)
443 if((*i)->contributes())
445 const StaticElement element("plane");
446 importer.pushElement(element);
447 FaceXMLExporter(*(*i)).exportXML(importer);
448 importer.popElement(element.name());
452 importer.popElement(brushElement.name());