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  std::vector<glm::vec3> &line_vert,
78  std::vector<glm::vec3> &line_col,
79  glm::mat4& abs_transform){}
80 
81  private:
82  std::string name;
83 };
84 
93 class VrmlSFNode : public VrmlNode{
94 
95 };
96 
97 
106 class VrmlGroup : public VrmlSFNode{
107 
108  public:
109 
110  virtual ~VrmlGroup();
111 
112  void AddChild(VrmlNode* newChild){
113  children.push_back(newChild);
114  }
115  std::vector<VrmlNode*> GetChildren() const{ return children; }
116 
117  int GetNbChildren(){
118  return (int) children.size();
119  }
120 
121  virtual std::ostream& ToString(std::ostream& str) const;
122 
123  virtual void PushVertices(std::vector<glm::vec3> &vert,
124  std::vector<glm::vec3> &col,
125  std::vector<glm::vec3> &line_vert,
126  std::vector<glm::vec3> &line_col,
127  glm::mat4& abs_transform);
128 
129  protected:
130  std::vector<VrmlNode*> children;
131 
132 };
133 
134 
145 class VrmlTransform : public VrmlGroup{
146 
147  public:
148 
149  VrmlTransform();
150 
151  void SetTranslation(glm::vec3 const& vec){ translation=vec; }
152  glm::vec3 GetTranslation() const { return translation; }
153 
154  void SetRotationAxis(glm::vec3 const& vec){ rotation_axis=vec; }
155  glm::vec3 GetRotationAxis() const { return rotation_axis; }
156 
157  void SetRotationAngle(float const& v){ rotation_angle=v; }
158  float GetRotationAngle() const { return rotation_angle; }
159 
160  void SetScale(glm::vec3 const& vec){ scale=vec; }
161  glm::vec3 GetScale() const { return scale; }
162 
163  virtual std::ostream& ToString(std::ostream& str) const;
164 
165  virtual void PushVertices(std::vector<glm::vec3> &vert,
166  std::vector<glm::vec3> &col,
167  std::vector<glm::vec3> &line_vert,
168  std::vector<glm::vec3> &line_col,
169  glm::mat4& abs_transform);
170 
171 
172  private:
173  glm::vec3 translation;
174  glm::vec3 rotation_axis;
175  float rotation_angle;
176  glm::vec3 scale;
177 };
178 
179 
188 class VrmlMaterial: public VrmlSFNode{
189  public:
190  VrmlMaterial();
191 
192  void SetDiffuseColor(glm::vec3 const& vec){ diffuseColor=vec; }
193  glm::vec3 GetDiffuseColor() const { return diffuseColor; }
194 
195  void SetEmissiveColor(glm::vec3 const& vec){ emissiveColor=vec; }
196  glm::vec3 GetEmissiveColor() const { return emissiveColor; }
197 
198  void SetSpecularColor(glm::vec3 const& vec){ specularColor=vec; }
199  glm::vec3 GetSpecularColor() const { return specularColor; }
200 
201  void SetShininess(float v){shininess = v; }
202  float GetShininess() const{ return shininess;}
203  void SetTransparency(float v){ transparency = v;}
204  float GetTransparency() const{ return transparency;}
205  void SetAmbientIntensity(float v){ ambientIntensity = v;}
206  float GetAmbientIntensity() const{ return ambientIntensity;}
207 
208  private:
209  glm::vec3 diffuseColor;
210  glm::vec3 emissiveColor;
211  glm::vec3 specularColor;
212  float shininess;
213  float transparency;
214  float ambientIntensity;
215 };
216 
217 
227 class VrmlAppearance: public VrmlSFNode{
228  public:
229 
230  VrmlAppearance();
231  ~VrmlAppearance();
232 
233  void SetMaterial(VrmlMaterial* mat) { material = mat; }
234  VrmlMaterial& GetMaterial() const{ return (*material); }
235 
236  private:
237  VrmlMaterial *material;
238 
239 };
240 
250 class VrmlGeometry{
251  public:
252  virtual ~VrmlGeometry(){}
253 
254  virtual void PushVertices(std::vector<glm::vec3> &vert,
255  std::vector<glm::vec3> &col,
256  std::vector<glm::vec3> &line_vert,
257  std::vector<glm::vec3> &line_col,
258  glm::mat4& abs_transform,
259  glm::vec3 curColor) = 0;
260 
261  virtual std::ostream& ToString(std::ostream& str) const =0;
262 };
263 
270 class VrmlSphere : public VrmlGeometry{
271  public:
272  VrmlSphere();
273  virtual ~VrmlSphere(){}
274 
275  virtual void PushVertices(std::vector<glm::vec3> &vert,
276  std::vector<glm::vec3> &col,
277  std::vector<glm::vec3> &line_vert,
278  std::vector<glm::vec3> &line_col,
279  glm::mat4& abs_transform,
280  glm::vec3 curColor);
281 
282  virtual std::ostream& ToString(std::ostream& str) const;
283 
284  void SetRadius(float r){radius = r;}
285 
286  private:
287  float radius;
288 };
289 
290 
297 class VrmlCone : public VrmlGeometry{
298  public:
299  VrmlCone();
300  virtual ~VrmlCone(){}
301 
302  virtual void PushVertices(std::vector<glm::vec3> &vert,
303  std::vector<glm::vec3> &col,
304  std::vector<glm::vec3> &line_vert,
305  std::vector<glm::vec3> &line_col,
306  glm::mat4& abs_transform,
307  glm::vec3 curColor);
308 
309  virtual std::ostream& ToString(std::ostream& str) const;
310 
311  void SetBottomRadius(float r){bottomRadius = r;}
312  void SetHeight(float h){height = h;}
313 
314  private:
315  float bottomRadius;
316  float height;
317 };
318 
324 class VrmlCylinder : public VrmlGeometry{
325  public:
326  VrmlCylinder();
327  virtual ~VrmlCylinder(){}
328 
329  virtual void PushVertices(std::vector<glm::vec3> &vert,
330  std::vector<glm::vec3> &col,
331  std::vector<glm::vec3> &line_vert,
332  std::vector<glm::vec3> &line_col,
333  glm::mat4& abs_transform,
334  glm::vec3 curColor);
335 
336  virtual std::ostream& ToString(std::ostream& str) const;
337 
338  void SetRadius(float r){radius = r;}
339  void SetHeight(float h){height = h;}
340 
341  private:
342  float radius;
343  float height;
344 };
345 
351 class VrmlBox : public VrmlGeometry{
352  public:
353  VrmlBox();
354  virtual ~VrmlBox(){}
355 
356  virtual void PushVertices(std::vector<glm::vec3> &vert,
357  std::vector<glm::vec3> &col,
358  std::vector<glm::vec3> &line_vert,
359  std::vector<glm::vec3> &line_col,
360  glm::mat4& abs_transform,
361  glm::vec3 curColor);
362 
363  virtual std::ostream& ToString(std::ostream& str) const;
364 
365  void SetSize(glm::vec3 const& s){size = s;}
366 
367  private:
368  glm::vec3 size;
369 };
370 
371 
384  class VrmlIndexedFaceSet: public VrmlGeometry{
385  public:
386 
387  VrmlIndexedFaceSet();
388  virtual ~VrmlIndexedFaceSet();
389 
390  virtual std::ostream& ToString(std::ostream& str) const;
391 
392  virtual void PushVertices(std::vector<glm::vec3> &vert,
393  std::vector<glm::vec3> &col,
394  std::vector<glm::vec3> &line_vert,
395  std::vector<glm::vec3> &line_col,
396  glm::mat4& abs_transform,
397  glm::vec3 curColor);
398 
399  void SetSolid(bool b){ solid = b;}
400  bool GetSolid() { return solid;}
401 
402  void SetNormalPerVertex(bool b){ normalPerVertex = b;}
403  bool GetNormalPerVertex() { return normalPerVertex;}
404 
405  void SetColorPerVertex(bool b){ colorPerVertex = b;}
406  bool GetColorPerVertex() { return colorPerVertex;}
407 
408  void SetCreaseAngle(float v){ creaseAngle = v;}
409  float GetCreaseAngle() { return creaseAngle;}
410 
411  void SetVertexCoordInds(std::vector<std::vector<int> >& vec){ face_indices = vec;}
412  void SetNormalInds(std::vector<std::vector<int> >& vec){ normal_indices=vec; }
413  void SetColorInds(std::vector<int>& vec){ color_indices=vec; }
414 
415  void SetVertexCoords(std::vector<glm::vec3>& vec){ vert_coords=vec;}
416  void SetNormals(std::vector<glm::vec3>& vec){ normals=vec; }
417  void SetColors(std::vector<glm::vec3> const& vec){ colors=vec; }
418 
419  private:
420  bool solid;
421  bool normalPerVertex;
422  bool colorPerVertex;
423  float creaseAngle;
424  std::vector<glm::vec3> vert_coords;
425  std::vector<glm::vec3> normals;
426  std::vector<glm::vec3> colors;
427  std::vector<std::vector<int> > face_indices;
428  std::vector<std::vector<int> > normal_indices;
429  std::vector<int> color_indices;
430 };
431 
432 
443  class VrmlIndexedLineSet: public VrmlGeometry{
444  public:
445 
446  VrmlIndexedLineSet();
447  virtual ~VrmlIndexedLineSet();
448 
449  virtual std::ostream& ToString(std::ostream& str) const;
450 
451  virtual void PushVertices(std::vector<glm::vec3> &vert,
452  std::vector<glm::vec3> &col,
453  std::vector<glm::vec3> &line_vert,
454  std::vector<glm::vec3> &line_col,
455  glm::mat4& abs_transform,
456  glm::vec3 curColor);
457 
458  void SetColorPerVertex(bool b){ colorPerVertex = b;}
459  bool GetColorPerVertex() { return colorPerVertex;}
460 
461  void SetVertexCoordInds(std::vector<std::vector<int> >& vec){ line_indices = vec;}
462  void SetColorInds(std::vector<int>& vec){ color_indices=vec; }
463 
464  void SetVertexCoords(std::vector<glm::vec3>& vec){ vert_coords=vec;}
465  void SetColors(std::vector<glm::vec3> const& vec){ colors=vec; }
466 
467  private:
468  bool colorPerVertex;
469  std::vector<glm::vec3> vert_coords;
470  std::vector<glm::vec3> colors;
471  std::vector<std::vector<int> > line_indices;
472  std::vector<int> color_indices;
473 };
474 
475 
486 class VrmlShape: public VrmlSFNode{
487  public:
488  virtual ~VrmlShape();
489 
490  void SetGeometry(VrmlGeometry* geom){ geometry = geom; }
491  void SetAppearance(VrmlAppearance* app){ appearance = app; }
492 
493  virtual void PushVertices(std::vector<glm::vec3> &vert,
494  std::vector<glm::vec3> &col,
495  std::vector<glm::vec3> &line_vert,
496  std::vector<glm::vec3> &line_col,
497  glm::mat4& abs_transform);
498 
499  virtual std::ostream& ToString(std::ostream& str) const;
500 
501 
502  private:
503  VrmlGeometry* geometry;
504  VrmlAppearance* appearance;
505 };
506 
507 
515 class VrmlLoader
516 {
517  public:
518  VrmlLoader();
519  ~VrmlLoader();
520  int ParseVRML(std::string vrml_file);
521 
522  int PushVertices(std::vector<glm::vec3> &vert, std::vector<glm::vec3> &col,
523  std::vector<glm::vec3> &line_vert, std::vector<glm::vec3> &line_col);
524 
525  private:
526  VrmlGroup* ParseGroup();
527  VrmlTransform* ParseTransform();
528  VrmlShape* ParseShape();
529  VrmlAppearance* ParseAppearance();
530  VrmlMaterial* ParseMaterial();
531  VrmlSphere* ParseSphere();
532  VrmlCone* ParseCone();
533  VrmlCylinder* ParseCylinder();
534  VrmlBox* ParseBox();
535  VrmlIndexedFaceSet* ParseIndexedFaceSet();
536  VrmlIndexedLineSet* ParseIndexedLineSet();
537  void ParseChildren(VrmlGroup* group);
538  void GetNextToken();
539  void SearchClosingBracket();
540  void ParseNode(VrmlGroup* group);
541 
542  std::vector<glm::vec3>* ParseCoordinates();
543  std::vector<std::vector<int> >* ParseIndexListForFace();
544  std::vector<int>* ParseIndexListForPt();
545 
546 
547  void ParseIgnoredToken();
548  bool ParseBoolToken();
549  float ParseFloatToken();
550  int ParseIntToken();
551  glm::vec3 ParseFloatVec3();
552 
553  void SkipToClosingBrac(char openBrac, char closingBrac);
554 
555  void CheckSaveName();
556  void CheckOpeningBracket(char brac);
557 
558  void Error(const char* message);
559  void Warning(const char* message);
560  void EndParseElemMsg(const char* node_type, const char* add_msg);
561  void StartParseElemMsg(const char* node_type, const char* add_msg);
562 
563  int current_line_nb;
564 
565  std::string vrml_file;
566  std::string cur_line;
567  char *pch;
568  char* cur_line_char;
569  std::ifstream* file;
570 
571  VrmlGroup* group;
572 
573 };
574 
575 
576 } // end of namespace OpenGLMbs{
577 
578 
579 #endif //_VRML_LOADER_HH_
580 #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...