Robotran C Documentation
VrmlLoader.hh
Go to the documentation of this file.
1 
33 #ifdef OPEN_GL
34 
35 
36 #ifndef _VRML_LOADER_HH_
37 #define _VRML_LOADER_HH_
38 
39 
40 #ifndef deg2rad
41  #define deg2rad(x) x*(3.141592f/180.0f)
42 #endif
43 
44 #ifndef GLM_FORCE_RADIANS
45  #define GLM_FORCE_RADIANS
46 #endif
47 
48 #include <glm.hpp>
49 #include <iostream>
50 #include <string>
51 #include <fstream>
52 #include <sstream>
53 
54 
55 #include <vector>
56 
57 
58 namespace OpenGLMbs{
59 
60 
67 class VrmlNode{
68  public:
69  virtual ~VrmlNode();
70 
71  void SetName(std::string n){}
72 
73  virtual std::ostream& ToString(std::ostream& str) const;
74 
75  virtual void PushVertices(std::vector<glm::vec3> &vert,
76  std::vector<glm::vec3> &col,
77  glm::mat4& abs_transform){}
78 
79  private:
80  std::string name;
81 };
82 
91 class VrmlSFNode : public VrmlNode{
92 
93 };
94 
95 
104 class VrmlGroup : public VrmlSFNode{
105 
106  public:
107 
108  virtual ~VrmlGroup();
109 
110  void AddChild(VrmlNode* newChild){
111  children.push_back(newChild);
112  }
113  std::vector<VrmlNode*> GetChildren() const{ return children; }
114 
115  int GetNbChildren(){
116  return (int) children.size();
117  }
118 
119  virtual std::ostream& ToString(std::ostream& str) const;
120 
121  virtual void PushVertices(std::vector<glm::vec3> &vert,
122  std::vector<glm::vec3> &col,
123  glm::mat4& abs_transform);
124 
125  protected:
126  std::vector<VrmlNode*> children;
127 
128 };
129 
130 
141 class VrmlTransform : public VrmlGroup{
142 
143  public:
144 
145  VrmlTransform();
146 
147  void SetTranslation(glm::vec3 const& vec){ translation=vec; }
148  glm::vec3 GetTranslation() const { return translation; }
149 
150  void SetRotationAxis(glm::vec3 const& vec){ rotation_axis=vec; }
151  glm::vec3 GetRotationAxis() const { return rotation_axis; }
152 
153  void SetRotationAngle(float const& v){ rotation_angle=v; }
154  float GetRotationAngle() const { return rotation_angle; }
155 
156  void SetScale(glm::vec3 const& vec){ scale=vec; }
157  glm::vec3 GetScale() const { return scale; }
158 
159  virtual std::ostream& ToString(std::ostream& str) const;
160 
161  virtual void PushVertices(std::vector<glm::vec3> &vert,
162  std::vector<glm::vec3> &col,
163  glm::mat4& abs_transform);
164 
165 
166  private:
167  glm::vec3 translation;
168  glm::vec3 rotation_axis;
169  float rotation_angle;
170  glm::vec3 scale;
171 };
172 
173 
182 class VrmlMaterial: public VrmlSFNode{
183  public:
184  VrmlMaterial();
185 
186  void SetDiffuseColor(glm::vec3 const& vec){ diffuseColor=vec; }
187  glm::vec3 GetDiffuseColor() const { return diffuseColor; }
188 
189  void SetEmissiveColor(glm::vec3 const& vec){ emissiveColor=vec; }
190  glm::vec3 GetEmissiveColor() const { return emissiveColor; }
191 
192  void SetSpecularColor(glm::vec3 const& vec){ specularColor=vec; }
193  glm::vec3 GetSpecularColor() const { return specularColor; }
194 
195  void SetShininess(float v){shininess = v; }
196  float GetShininess() const{ return shininess;}
197  void SetTransparency(float v){ transparency = v;}
198  float GetTransparency() const{ return transparency;}
199  void SetAmbientIntensity(float v){ ambientIntensity = v;}
200  float GetAmbientIntensity() const{ return ambientIntensity;}
201 
202  private:
203  glm::vec3 diffuseColor;
204  glm::vec3 emissiveColor;
205  glm::vec3 specularColor;
206  float shininess;
207  float transparency;
208  float ambientIntensity;
209 };
210 
211 
221 class VrmlAppearance: public VrmlSFNode{
222  public:
223 
224  VrmlAppearance();
225  ~VrmlAppearance();
226 
227  void SetMaterial(VrmlMaterial* mat) { material = mat; }
228  VrmlMaterial& GetMaterial() const{ return (*material); }
229 
230  private:
231  VrmlMaterial *material;
232 
233 };
234 
244 class VrmlGeometry{
245  public:
246  virtual ~VrmlGeometry(){}
247 
248  virtual void PushVertices(std::vector<glm::vec3> &vert,
249  std::vector<glm::vec3> &col,
250  glm::mat4& abs_transform,
251  glm::vec3 curColor) = 0;
252 
253  virtual std::ostream& ToString(std::ostream& str) const =0;
254 };
255 
262 class VrmlSphere : public VrmlGeometry{
263  public:
264  VrmlSphere();
265  virtual ~VrmlSphere(){}
266 
267  virtual void PushVertices(std::vector<glm::vec3> &vert,
268  std::vector<glm::vec3> &col,
269  glm::mat4& abs_transform,
270  glm::vec3 curColor);
271 
272  virtual std::ostream& ToString(std::ostream& str) const;
273 
274  void SetRadius(float r){radius = r;}
275 
276  private:
277  float radius;
278 };
279 
280 
287 class VrmlCone : public VrmlGeometry{
288  public:
289  VrmlCone();
290  virtual ~VrmlCone(){}
291 
292  virtual void PushVertices(std::vector<glm::vec3> &vert,
293  std::vector<glm::vec3> &col,
294  glm::mat4& abs_transform,
295  glm::vec3 curColor);
296 
297  virtual std::ostream& ToString(std::ostream& str) const;
298 
299  void SetBottomRadius(float r){bottomRadius = r;}
300  void SetHeight(float h){height = h;}
301 
302  private:
303  float bottomRadius;
304  float height;
305 };
306 
312 class VrmlCylinder : public VrmlGeometry{
313  public:
314  VrmlCylinder();
315  virtual ~VrmlCylinder(){}
316 
317  virtual void PushVertices(std::vector<glm::vec3> &vert,
318  std::vector<glm::vec3> &col,
319  glm::mat4& abs_transform,
320  glm::vec3 curColor);
321 
322  virtual std::ostream& ToString(std::ostream& str) const;
323 
324  void SetRadius(float r){radius = r;}
325  void SetHeight(float h){height = h;}
326 
327  private:
328  float radius;
329  float height;
330 };
331 
337 class VrmlBox : public VrmlGeometry{
338  public:
339  VrmlBox();
340  virtual ~VrmlBox(){}
341 
342  virtual void PushVertices(std::vector<glm::vec3> &vert,
343  std::vector<glm::vec3> &col,
344  glm::mat4& abs_transform,
345  glm::vec3 curColor);
346 
347  virtual std::ostream& ToString(std::ostream& str) const;
348 
349  void SetSize(glm::vec3 const& s){size = s;}
350 
351  private:
352  glm::vec3 size;
353 };
354 
355 
368  class VrmlIndexedFaceSet: public VrmlGeometry{
369  public:
370 
371  VrmlIndexedFaceSet();
372  virtual ~VrmlIndexedFaceSet();
373 
374  virtual std::ostream& ToString(std::ostream& str) const;
375 
376  virtual void PushVertices(std::vector<glm::vec3> &vert,
377  std::vector<glm::vec3> &col,
378  glm::mat4& abs_transform,
379  glm::vec3 curColor);
380 
381  void SetSolid(bool b){ solid = b;}
382  bool GetSolid() { return solid;}
383 
384  void SetNormalPerVertex(bool b){ normalPerVertex = b;}
385  bool GetNormalPerVertex() { return normalPerVertex;}
386 
387  void SetColorPerVertex(bool b){ colorPerVertex = b;}
388  bool GetColorPerVertex() { return colorPerVertex;}
389 
390  void SetCreaseAngle(float v){ creaseAngle = v;}
391  float GetCreaseAngle() { return creaseAngle;}
392 
393  void SetVertexCoordInds(std::vector<std::vector<int> >& vec){ face_indices = vec;}
394  void SetNormalInds(std::vector<std::vector<int> >& vec){ normal_indices=vec; }
395  void SetColorInds(std::vector<int>& vec){ color_indices=vec; }
396 
397  void SetVertexCoords(std::vector<glm::vec3>& vec){ vert_coords=vec;}
398  void SetNormals(std::vector<glm::vec3>& vec){ normals=vec; }
399  void SetColors(std::vector<glm::vec3> const& vec){ colors=vec; }
400 
401  private:
402  bool solid;
403  bool normalPerVertex;
404  bool colorPerVertex;
405  float creaseAngle;
406  std::vector<glm::vec3> vert_coords;
407  std::vector<glm::vec3> normals;
408  std::vector<glm::vec3> colors;
409  std::vector<std::vector<int> > face_indices;
410  std::vector<std::vector<int> > normal_indices;
411  std::vector<int> color_indices;
412 };
413 
414 
425 class VrmlShape: public VrmlSFNode{
426  public:
427  virtual ~VrmlShape();
428 
429  void SetGeometry(VrmlGeometry* geom){ geometry = geom; }
430  void SetAppearance(VrmlAppearance* app){ appearance = app; }
431 
432  virtual void PushVertices(std::vector<glm::vec3> &vert,
433  std::vector<glm::vec3> &col,
434  glm::mat4& abs_transform);
435 
436  virtual std::ostream& ToString(std::ostream& str) const;
437 
438 
439  private:
440  VrmlGeometry* geometry;
441  VrmlAppearance* appearance;
442 };
443 
444 
452 class VrmlLoader
453 {
454  public:
455  VrmlLoader();
456  ~VrmlLoader();
457  int ParseVRML(std::string vrml_file);
458 
459  int PushVertices(std::vector<glm::vec3> &vert, std::vector<glm::vec3> &col);
460 
461  private:
462  VrmlGroup* ParseGroup();
463  VrmlTransform* ParseTransform();
464  VrmlShape* ParseShape();
465  VrmlAppearance* ParseAppearance();
466  VrmlMaterial* ParseMaterial();
467  VrmlSphere* ParseSphere();
468  VrmlCone* ParseCone();
469  VrmlCylinder* ParseCylinder();
470  VrmlBox* ParseBox();
471  VrmlIndexedFaceSet* ParseIndexedFaceSet();
472  void ParseChildren(VrmlGroup* group);
473  void GetNextToken();
474  void SearchClosingBracket();
475  void ParseNode(VrmlGroup* group);
476 
477  std::vector<glm::vec3>* ParseCoordinates();
478  std::vector<std::vector<int> >* ParseIndexListForFace();
479  std::vector<int>* ParseIndexListForPt();
480 
481 
482  void ParseIgnoredToken();
483  bool ParseBoolToken();
484  float ParseFloatToken();
485  int ParseIntToken();
486  glm::vec3 ParseFloatVec3();
487 
488  void SkipToClosingBrac(char openBrac, char closingBrac);
489 
490  void CheckSaveName();
491  void CheckOpeningBracket(char brac);
492 
493  void Error(const char* message);
494  void Warning(const char* message);
495  void EndParseElemMsg(const char* node_type, const char* add_msg);
496  void StartParseElemMsg(const char* node_type, const char* add_msg);
497 
498  int current_line_nb;
499 
500  std::string vrml_file;
501  std::string cur_line;
502  char *pch;
503  char* cur_line_char;
504  std::ifstream* file;
505 
506  VrmlGroup* group;
507 
508 };
509 
510 
511 } // end of namespace OpenGLMbs{
512 
513 
514 #endif //_VRML_LOADER_HH_
515 #endif // OPEN_GL
OpenGLMbs
Definition: MpegFrameCaptureOptions.hh:6
VrmlLoader.hh
A basic loader for VRML2.0/VRML97 files.
stl::operator<<
std::ostream & operator<<(std::ostream &out, const point p)
Definition: parse_stl.cpp:11
VrmlShapePoints.hh
File containing points for regular geometries defined in VRML (points copied from Shapes of OPenGL mb...