]> icculus.org git repositories - divverent/netradiant.git/blob - radiant/brushxml.h
new args -meta and -patchmeta to -convert with .map file argument
[divverent/netradiant.git] / radiant / brushxml.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20 */
21
22 #if !defined(INCLUDED_BRUSHXML_H)
23 #define INCLUDED_BRUSHXML_H
24
25 #include "stream/stringstream.h"
26 #include "xml/xmlelement.h"
27
28 #include "brush.h"
29
30 inline void FaceTexdef_BP_importXML(FaceTexdef& texdef, const char* xmlContent)
31 {
32   StringTokeniser content(xmlContent);
33
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()));
40 }
41 inline void FaceTexdef_importXML(FaceTexdef& texdef, const char* xmlContent)
42 {
43   StringTokeniser content(xmlContent);
44
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()));
50
51   ASSERT_MESSAGE(texdef_sane(texdef.m_projection.m_texdef), "FaceTexdef_importXML: bad texdef");
52 }
53
54 inline void FacePlane_importXML(FacePlane& facePlane, const char* xmlContent)
55 {
56   StringTokeniser content(xmlContent);
57
58   for (int i = 0; i < 3; ++i)
59   {
60     for (int j = 0; j < 3; ++j)
61     {
62       facePlane.planePoints()[i][j] = atof(content.getToken());
63     }
64   }
65   facePlane.MakePlane();
66 }
67
68
69 class FaceXMLImporter
70 {
71   struct xml_state_t
72   {
73     enum EState
74     {
75       eDefault,
76       ePlanePts,
77       eTexdef,
78       eBPMatrix,
79       eFlags,
80       eShader,
81     };
82
83     EState m_state;
84     StringOutputStream m_content;
85
86     xml_state_t(EState state)
87       : m_state(state)
88     {}
89
90     EState state() const
91     {
92       return m_state;
93     }
94     const char* content() const
95     {
96       return m_content.c_str();
97     }
98     std::size_t write(const char* buffer, std::size_t length)
99     {
100       return m_content.write(buffer, length);
101     }
102   };
103
104   std::vector<xml_state_t> m_xml_state;
105   Face& m_face;
106 public:
107   FaceXMLImporter(Face& face) : m_face(face)
108   {
109     m_xml_state.push_back(xml_state_t::eDefault);
110   }
111   ~FaceXMLImporter()
112   {
113     m_face.planeChanged();
114   }
115
116   void pushElement(const XMLElement& element)
117   {
118     ASSERT_MESSAGE(m_xml_state.back().state() == xml_state_t::eDefault, "parse error");
119
120     if(strcmp(element.name(), "planepts") == 0)
121     {
122       m_xml_state.push_back(xml_state_t::ePlanePts);
123     }
124     else if(strcmp(element.name(), "texdef") == 0)
125     {
126       m_xml_state.push_back(xml_state_t::eTexdef);
127     }
128     else if(strcmp(element.name(), "bpmatrix") == 0)
129     {
130       m_xml_state.push_back(xml_state_t::eBPMatrix);
131     }
132     else if(strcmp(element.name(), "flags") == 0)
133     {
134       m_xml_state.push_back(xml_state_t::eFlags);
135     }
136     else if(strcmp(element.name(), "shader") == 0)
137     {
138       m_xml_state.push_back(xml_state_t::eShader);
139     }
140   }
141   void popElement(const char* name)
142   {
143     ASSERT_MESSAGE(m_xml_state.back().state() != xml_state_t::eDefault, "parse error");
144
145     switch(m_xml_state.back().state())
146     {
147     case xml_state_t::ePlanePts:
148       {
149         FacePlane_importXML(m_face.getPlane(), m_xml_state.back().content());
150       }
151       break;
152     case xml_state_t::eTexdef:
153       {
154         FaceTexdef_importXML(m_face.getTexdef(), m_xml_state.back().content());
155       }
156       break;
157     case xml_state_t::eBPMatrix:
158       {
159         FaceTexdef_BP_importXML(m_face.getTexdef(), m_xml_state.back().content());
160       }
161       break;
162    case xml_state_t::eFlags:
163      {
164         StringTokeniser content(m_xml_state.back().content());
165
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());
169       }
170       break;
171     case xml_state_t::eShader:
172       {
173         m_face.getShader().setShader(m_xml_state.back().content());
174       }
175       break;
176     default:
177       break;
178     }
179
180     m_xml_state.pop_back();
181   }
182   std::size_t write(const char* data, std::size_t length)
183   {
184     ASSERT_MESSAGE(!m_xml_state.empty(), "parse error");
185     return m_xml_state.back().write(data, length);
186   }
187 };
188
189
190 inline void FaceTexdef_exportXML(const FaceTexdef& texdef, XMLImporter& importer)
191 {
192   StaticElement element("texdef");
193   importer.pushElement(element);
194
195   ASSERT_MESSAGE(texdef_sane(texdef.m_projection.m_texdef), "FaceTexdef_exportXML: bad texdef");
196
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];
202
203   importer.popElement(element.name());
204 }
205 inline void FaceTexdef_BP_exportXML(const FaceTexdef& texdef, XMLImporter& importer)
206 {
207   StaticElement element("texdef");
208   importer.pushElement(element);
209
210   for(int i = 0; i < 2; ++i)
211   {
212     for(int j = 0; j < 3; ++j)
213     {
214       importer << texdef.m_projection.m_brushprimit_texdef.coords[i][j] << ' ';
215     }
216   }
217
218   importer.popElement(element.name());
219 }
220 inline void FaceShader_ContentsFlagsValue_exportXML(const FaceShader& faceShader, XMLImporter& importer)
221 {
222   StaticElement element("flags");
223   importer.pushElement(element);
224
225   {
226     importer << faceShader.m_flags.m_contentFlags
227       << ' ' << faceShader.m_flags.m_surfaceFlags
228       << ' ' << faceShader.m_flags.m_value;
229   }
230
231   importer.popElement(element.name());
232 }
233
234 inline void FacePlane_exportXML(const FacePlane& facePlane, XMLImporter& importer)
235 {
236   StaticElement element("planepts");
237   importer.pushElement(element);
238
239   {
240     // write planepts
241     for (int i=0 ; i<3 ; i++)
242     {
243       for (int j=0 ; j<3 ; j++)
244       {
245         importer << Face::m_quantise(facePlane.planePoints()[i][j]) << ' ';
246       }
247     }
248   }
249
250   importer.popElement(element.name());
251 }
252
253 inline void FacePolygon_exportXML(const Winding& w, const BasicVector3<double>& normal, XMLImporter& importer)
254 {
255   DynamicElement element("polygon");
256   
257   char tmp[32];
258   
259   sprintf(tmp, "%f", normal.x());
260   element.insertAttribute("nx", tmp);
261   
262   sprintf(tmp, "%f", normal.y());
263   element.insertAttribute("ny", tmp);
264   
265   sprintf(tmp, "%f", normal.z());
266   element.insertAttribute("nz", tmp);
267   
268   importer.pushElement(element);
269   
270   for(unsigned int i = 0; i < w.numpoints; ++i)
271   {
272         DynamicElement c("vertex");
273         
274         sprintf(tmp, "%f", w.points[i].vertex.x());
275         c.insertAttribute("x", tmp);
276                 
277         sprintf(tmp, "%f", w.points[i].vertex.y());
278         c.insertAttribute("y", tmp);
279         
280         sprintf(tmp, "%f", w.points[i].vertex.z());
281         c.insertAttribute("z", tmp);
282         
283         sprintf(tmp, "%f", w.points[i].texcoord.x());
284         c.insertAttribute("s", tmp);
285         
286         sprintf(tmp, "%f", w.points[i].texcoord.y());
287         c.insertAttribute("t", tmp);
288         
289         importer.pushElement(c);
290         importer.popElement(c.name());
291   }
292
293   importer.popElement(element.name());
294 }
295
296 class FaceXMLExporter
297 {
298   const Face& m_face;
299 public:
300   FaceXMLExporter(const Face& face) : m_face(face)
301   {
302   }
303   void exportXML(XMLImporter& importer)
304   {
305     bool bAlternateTexdef = (Face::m_type == eBrushTypeQuake3BP || Face::m_type == eBrushTypeDoom3 || Face::m_type == eBrushTypeQuake4);
306  
307     // write shader
308     {
309       StaticElement element("shader"); 
310       importer.pushElement(element);
311       importer << m_face.getShader().getShader();
312       importer.popElement(element.name());
313     }
314
315     FacePolygon_exportXML(m_face.getWinding(), m_face.getPlane().plane3().normal(), importer);
316     FacePlane_exportXML(m_face.getPlane(), importer);
317
318     if(!bAlternateTexdef)
319     {
320       FaceTexdef_exportXML(m_face.getTexdef(), importer);
321     }
322     else
323     {
324       FaceTexdef_BP_exportXML(m_face.getTexdef(), importer);
325     }
326
327     FaceShader_ContentsFlagsValue_exportXML(m_face.getShader(), importer);
328   }
329 };
330
331
332 class BrushXMLImporter : public XMLImporter
333 {
334   class xml_state_t
335   {
336   public:
337     enum EState
338     {
339       eDefault,
340       eBrush,
341       eFace,
342     };
343
344   private:
345     EState m_state;
346
347   public:
348     xml_state_t(EState state)
349       : m_state(state)
350     {
351     }
352     EState state() const
353     {
354       return m_state;
355     }
356   };
357
358   std::vector<xml_state_t> m_xml_state;
359   char m_faceImporter[sizeof(FaceXMLImporter)];
360   Brush& m_brush;
361
362   FaceXMLImporter& faceImporter()
363   {
364     return *reinterpret_cast<FaceXMLImporter*>(m_faceImporter);
365   }
366
367 public:
368   BrushXMLImporter(Brush& brush) : m_brush(brush)
369   {
370     m_xml_state.push_back(xml_state_t::eDefault);
371   }
372   void pushElement(const XMLElement& element)
373   {
374     switch(m_xml_state.back().state())
375     {
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);
379       break;
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();
387       break;
388     case xml_state_t::eFace:
389       m_xml_state.push_back(xml_state_t::eFace);
390       faceImporter().pushElement(element);
391       break;
392     }
393   }
394   void popElement(const char* name)
395   {
396     ASSERT_MESSAGE(!m_xml_state.empty(), "parse error");
397     m_xml_state.pop_back();
398
399     switch(m_xml_state.back().state())
400     {
401     case xml_state_t::eDefault:
402       break;
403     case xml_state_t::eBrush:
404       destructor(faceImporter());
405       break;
406     case xml_state_t::eFace:
407       faceImporter().popElement(name);
408       break;
409     }
410   }
411   std::size_t write(const char* data, std::size_t length)
412   {
413     switch(m_xml_state.back().state())
414     {
415     case xml_state_t::eFace:
416       return faceImporter().write(data, length);
417       break;
418     default:
419       break;
420     }
421     return length;
422   }
423 };
424
425 class BrushXMLExporter : public XMLExporter
426 {
427   const Brush& m_brush;
428
429 public:
430   BrushXMLExporter(const Brush& brush) : m_brush(brush)
431   {
432   }
433   void exportXML(XMLImporter& importer)
434   {
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");
437
438     const StaticElement brushElement("brush");
439     importer.pushElement(brushElement);
440
441     for(Brush::const_iterator i = m_brush.begin(); i != m_brush.end(); ++i)
442     {
443       if((*i)->contributes())
444       {
445         const StaticElement element("plane");
446         importer.pushElement(element);
447         FaceXMLExporter(*(*i)).exportXML(importer);
448         importer.popElement(element.name());
449       }
450     }
451
452     importer.popElement(brushElement.name());
453   }
454 };
455
456
457 #endif