NGL  6.5
The NCCA Graphics Library
XMLSerializer.cpp
Go to the documentation of this file.
1 #include "XMLSerializer.h"
4 #include <boost/algorithm/string.hpp>
5 #include <boost/lexical_cast.hpp>
6 #include "Camera.h"
7 #include "Light.h"
8 #include "Material.h"
9 #include "Plane.h"
10 #include "Transformation.h"
11 #include "fmt/format.h"
12 namespace ngl
13 {
14 
15 
16  void XMLSerializer::writeToXML(const std::string &_s, const std::string &_tag)
17  {
18  char *nodeName = m_doc.allocate_string(_tag.c_str()); // Allocate string and copy name into it
19 
20  char *value = m_doc.allocate_string(_s.c_str());
22  m_doc.append_node(root);
23  }
24 
26  {
27  char *nodeName = m_doc.allocate_string(_tag.c_str()); // Allocate string and copy name into it
28  char *value = m_doc.allocate_string(_s.c_str());
30  _parent->append_node(root);
31  }
32 
34  {
35  char *nodeName = m_doc.allocate_string(_tag.c_str()); // Allocate string and copy name into it
37  m_currentNode=root;
39  }
40 
42  {
43  char *nodeName = m_doc.allocate_string(_tag.c_str()); // Allocate string and copy name into it
45  m_currentNode=root;
46  _parent->append_node(m_currentNode);
47  }
48 
50  {
51  rapidxml::xml_node<>* declatation;
52 
54  declatation->append_attribute(m_doc.allocate_attribute("version", "1.0"));
55  declatation->append_attribute(m_doc.allocate_attribute("encoding", "us-ascii"));
56  m_doc.append_node(declatation);
57  m_fname=_fname;
58  m_mode=_mode;
59  m_currentNode=0;
60  }
61 
63  {
64  if(m_mode ==WRITE)
65  {
66  std::ofstream file_stored(m_fname.c_str());
67  file_stored << m_doc;
68  file_stored.close();
69  m_doc.clear();
70  }
71  }
72 
74  {
75 
76  }
77 
78  void XMLSerializer::write(const AABB &_s)
79  {
80 
81  }
82 
84  {
85 
86  }
87 
88  void XMLSerializer::write(const BBox &_s)
89  {
90 
91  }
92 
94  {
95 
96  }
97 
99  {
100 
101  }
102 
104  {
105 
106  }
107 
108 
109 
110  void XMLSerializer::write(const Camera &_s,const std::string &_tag)
111  {
112 
113  char *nodeName = m_doc.allocate_string(_tag.c_str()); // Allocate string and copy name into it
115  m_doc.append_node(root);
116  writeToXML(fmt::format(" {}", _s.getFOV() ),root,"fov");
117  writeToXML(fmt::format(" {}" , _s.getAspect() ),root,"aspect");
118  writeToXML(fmt::format(" {}", _s.getNear() ),root,"near");
119  writeToXML(fmt::format(" {}", _s.getFar() ),root,"far");
120  write(_s.getEye(),root,"eye");
121  write(_s.getLook(),root,"look");
122  write(_s.getUp(),root,"up");
123 
124  }
125 
127  {
128 
129  }
130 
132  {
133  writeToXML(fmt::format(" {} {} {} {}" , _s.m_r , _s.m_g , _s.m_b , _s.m_a ),_tag);
134  }
135 
136 
138  {
139  writeToXML(fmt::format(" {} {} {} {}" , _s.m_r , _s.m_g , _s.m_b , _s.m_a ),_parent,_tag);
140  }
141 
143  {
144 
145  }
146 
147  void XMLSerializer::write(const Light &_s,const std::string &_tag)
148  {
149  char *nodeName = m_doc.allocate_string(_tag.c_str()); // Allocate string and copy name into it
151  m_doc.append_node(root);
152 
153  write(_s.getPos(),root,"position");
154  write(_s.getColour(),root,"colour");
155  write(_s.getSpecColour(),root,"specColour");
156  //writeToXML(boost::str( boost::format(" {}") % _s.));
157  // m_position.writeXML(_doc,root,"position");
158  // m_diffuse.writeXML(_doc,root,"colour");
159  // m_specular.writeXML(_doc,root,"specColour");
160  // char *value = _doc.allocate_string(boost::str( boost::format(" {}") % m_constantAtten).c_str());
161  // rapidxml::xml_node<>* child = _doc.allocate_node(rapidxml::node_element, "constantAtten",value);
162  // root->append_node(child);
163 
164  // value = _doc.allocate_string(boost::str( boost::format(" {}") % m_linearAtten).c_str());
165  // child = _doc.allocate_node(rapidxml::node_element, "linearAtten",value);
166  // root->append_node(child);
167 
168  // value = _doc.allocate_string(boost::str( boost::format(" {}") % m_quadraticAtten).c_str());
169  // child = _doc.allocate_node(rapidxml::node_element, "quadraticAtten",value);
170  // root->append_node(child);
171  }
172 
174  {
175 
176  }
177 
179  {
180  writeToXML(fmt::format(" {0} {1} {2} {3} {4} {5} {6} {7} {8}"
181  , _s.m_openGL[0] ,_s.m_openGL[1] , _s.m_openGL[2]
182  , _s.m_openGL[3] , _s.m_openGL[4] , _s.m_openGL[5]
183  , _s.m_openGL[6] , _s.m_openGL[7] , _s.m_openGL[8] ),_tag);
184 
185  }
186 
188  {
189 
190  }
191 
193  {
194  Real *openGL= _s.openGL();
195 
196  writeToXML(fmt::format(" {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15}"
197  , openGL[0] , openGL[1] , openGL[2] , openGL[3]
198  , openGL[4] , openGL[5] , openGL[6] , openGL[7]
199  , openGL[8] , openGL[9] , openGL[10] , openGL[11]
200  , openGL[12] , openGL[13] , openGL[14] , openGL[15]),_tag);
201 
202  }
203 
204  void XMLSerializer::read(Material &_s)
205  {
206 
207  }
208 
209  void XMLSerializer::write(const Material &_s, const std::string &_tag)
210  {
211  char *nodeName = m_doc.allocate_string(_tag.c_str()); // Allocate string and copy name into it
212 
214  m_doc.append_node(root);
215  write(_s.getDiffuse(),root,"diffuse");
216  write(_s.getSpecular(),root,"specular");
217  write(_s.getAmbient(),root,"ambient");
218  writeToXML(fmt::format(" {0}" , _s.getSpecularExponent()),root,"specularExponent");
219  writeToXML(fmt::format(" {0}" , _s.getTransparency()),root,"transparency");
220  writeToXML(fmt::format(" {0}" , _s.getRoughness() ),root,"roughness");
221  }
222 
224  {
225 
226  }
227 
228  void XMLSerializer::write(const PathCamera &_s, const std::string &_tag)
229  {
230 
231  }
232 
234  {
235 
236  }
237 
238  void XMLSerializer::write(const Plane &_s,const std::string &_tag)
239  {
240 
241  }
242 
244  {
245 
246  }
247 
248 
249  void XMLSerializer::write(const Quaternion &_s, const std::string &_tag)
250  {
251  writeToXML(fmt::format(" {0} {1} {2} {3}" , _s.getS() , _s.getX() , _s.getY() , _s.getZ() ),_tag);
252 
253  }
254 
256  {
257 
258  }
259 
260  void XMLSerializer::write(const SpotLight &_s, const std::string &_tag)
261  {
262 
263  }
264 
265 
267  {
268 
269  }
270 
271  void XMLSerializer::write(const Transformation &_s, const std::string &_tag)
272  {
273 
274 
275 
276  char *nodeName = m_doc.allocate_string(_tag.c_str()); // Allocate string and copy name into it
277 
279  m_doc.append_node(root);
280 
281  write(_s.getPosition(),root,"position");
282  write(_s.getRotation(),root,"rotation");
283  write(_s.getScale(),root,"scale");
284 
285  }
286 
288  {
289 
290  }
291 
292  void XMLSerializer::write(const Vec2 &_s,std::string _tag)
293  {
294  writeToXML(fmt::format(" {0} {1} " , _s.m_x , _s.m_y ),_tag);
295 
296 
297  }
298 
300  {
301  writeToXML(fmt::format(" {0} {1} " , _s.m_x , _s.m_y ),_parent,_tag);
302  }
303 
304 
305 
307  {
308 
309  }
310 
311  void XMLSerializer::write(const Vec3 &_s, std::string _tag)
312  {
313  writeToXML(fmt::format(" {0} {1} {2} " , _s.m_x , _s.m_y , _s.m_z ),_tag);
314  }
316  {
317 
318  writeToXML(fmt::format(" {0} {1} {2} " , _s.m_x , _s.m_y , _s.m_z ),_parent,_tag);
319 
320  }
322  {
323  // if(_doc->tokenType() != QXmlStreamReader::StartElement &&
324  // _doc->name() == "Vec3")
325  // {
326  // std::cerr<<"error with token type for xml parse\n";
327  // }
328 
329 
330  }
331 
332  void XMLSerializer::write(const Vec4 &_s, std::string _tag)
333  {
334  writeToXML(fmt::format(" {0} {1} {2} {3}" , _s.m_x , _s.m_y , _s.m_z , _s.m_w),_tag);
335  }
336 
337 
339  {
340  writeToXML(fmt::format(" {0} {1} {2} {3}" , _s.m_x , _s.m_y , _s.m_z , _s.m_w),_parent,_tag);
341  }
342 
343 
344 }
void writeToXML(const std::string &_s, const std::string &_tag)
Mat3 basic 3x3 matrix for certain glsl ops.
Definition: Mat3.h:43
Generic Bezier Curve Class allowing the user to generate basic curves using a number of different con...
Definition: BezierCurve.h:45
simple Vec2 encapsulates a 3 float object like glsl Vec2 but not maths use the Vec2 class for maths a...
Definition: Vec2.h:49
rapidxml::xml_document m_doc
simple class to hold colour information and set the basic opengl colour state. also has overloaded me...
Definition: Colour.h:40
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
Real m_r
red component of the colour tuple
Definition: Colour.h:160
Real m_b
blue component of the colour tuple
Definition: Colour.h:168
std::array< Real, 9 > m_openGL
The matrix in m_openGL 16 Real array format usefull for OpenGL fv formats mapped to m_m[][] elements ...
Definition: Mat3.h:243
Ch * allocate_string(const Ch *source=0, std::size_t size=0)
Definition: rapidxml.hpp:476
Simple class to encapsulate OpenGL Light functions this will fill in the following structure...
Definition: Light.h:68
Vec3 getScale() const noexcept
function to get the scale
Vec3 getPosition() const noexcept
function to get the position
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3175
Real getZ() const noexcept
accesor for the z vector components
Definition: Quaternion.h:113
Real getNear() const noexcept
get the near clip value
Definition: Camera.h:254
An element node. Name contains element name. Value contains text of first data node.
Definition: rapidxml.hpp:146
virtual void read(AABB &_s)
read from an AABB must be overriden in child
Definition: AABB.h:42
Real getFar() const noexcept
get the far clip value
Definition: Camera.h:259
void append_attribute(xml_attribute< Ch > *attribute)
Definition: rapidxml.hpp:1217
GLsizei const GLfloat * value
Definition: glew.h:1852
Real m_x
x component of the Vec2
Definition: Vec2.h:256
Inherits from Camera and adds a path for both eye and look using two Bezier Curves.
Definition: PathCamera.h:33
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
Colour getSpecColour() const noexcept
returns the current light Specular colour
Definition: Light.h:152
implementation files for RibExport class
Definition: AABB.cpp:22
Colour getColour() const noexcept
returns the current light colour
Definition: Light.h:147
a simple mathmatical representation of a plane
Encapsulation of OpenGL spotlight inherits from the Light class.
Definition: SpotLight.h:58
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
Real m_g
green component of the colour tuple
Definition: Colour.h:164
XMLSerializer(const std::string &_fname, ACCESSMODE _mode)
ctor, uses RAII to open the file, flag indicates if we have read or write mode
Real m_y
y component
Definition: Vec3.h:311
Vec4 getLook() const noexcept
get the look position
Definition: Camera.h:219
an encapsulation of an OpenGL Light
Real getX() const noexcept
accesor for the x vector components
Definition: Quaternion.h:103
a simple transformation object containing rot / tx / scale and final matrix
Real m_y
y component of the Vec2
Definition: Vec2.h:260
XML Serialization for NGL.
Real m_z
z component
Definition: Vec4.h:323
simple camera class to allow movement in an opengl scene. a lot of this stuff is from the HILL book C...
Definition: Camera.h:54
Vec3 getRotation() const noexcept
function to get the rotation
Real getS() const noexcept
accesor for the scalar part
Definition: Quaternion.h:98
Real m_x
x component
Definition: Vec3.h:310
Vec4 getUp() const noexcept
get the up vector
Definition: Camera.h:224
Real m_x
x component
Definition: Vec4.h:321
A declaration node. Name and value are empty. Declaration parameters (version, encoding and standalon...
Definition: rapidxml.hpp:150
void addNode(const std::string &_tag)
void append_node(xml_node< Ch > *child)
Definition: rapidxml.hpp:1097
xml_node< Ch > * allocate_node(node_type type, const Ch *name=0, const Ch *value=0, std::size_t name_size=0, std::size_t value_size=0)
Definition: rapidxml.hpp:415
Transformation describes a transformation (translate, scale, rotation) modifed by j macey and include...
Real m_a
alpha the transparent element
Definition: Colour.h:172
Simple Bounding box class used in various parts of ngl and other example programs.
Definition: BBox.h:40
virtual ~XMLSerializer()
dtor will close the file
a simple camera class based on the Hill Book
virtual void write(const AABB &_s)
write from an AABB must be overriden in child
Matrix Class to do simple matrix operations included operator overloaded functions for maths and matr...
Definition: Mat4.h:58
std::string m_fname
Vec4 getPos() const noexcept
returns the current light position as a Vector
Definition: Light.h:142
Vec4 getEye() const noexcept
get the eye position
Definition: Camera.h:205
This file contains rapidxml printer implementation.
Real getY() const noexcept
accesor for the y vector components
Definition: Quaternion.h:108
Real m_w
w component 0 for vector 1 for point
Definition: Vec4.h:324
Real m_y
y component
Definition: Vec4.h:322
xml_attribute< Ch > * allocate_attribute(const Ch *name=0, const Ch *value=0, std::size_t name_size=0, std::size_t value_size=0)
Definition: rapidxml.hpp:447
Real getFOV() const noexcept
get the fov value
Definition: Camera.h:244
rapidxml::xml_node * m_currentNode
GLsizei const GLchar *const * string
Definition: glew.h:1847
Real getAspect() const noexcept
get the aspect value
Definition: Camera.h:249
Real * openGL() noexcept
accesor to the openGL matrix
Definition: Mat4.h:239
Real m_z
z component
Definition: Vec3.h:312