qtopengl_obj_model.h
Go to the documentation of this file.
1 
11 #ifndef QTOPENGL_OBJ_MODEL_H
12 #define QTOPENGL_OBJ_MODEL_H
13 
14 class QTextStream;
15 class QString;
16 
17 #include <argos3/core/utility/datatypes/datatypes.h>
18 #include <argos3/core/utility/math/vector3.h>
19 #include <argos3/core/utility/math/vector2.h>
20 
21 #include <algorithm>
22 #include <array>
23 #include <map>
24 #include <string>
25 #include <vector>
26 
27 #ifdef __APPLE__
28 #include <glu.h>
29 #else
30 #include <GL/glu.h>
31 #endif
32 
33 namespace argos {
34 
36 
37  public:
38 
39  struct SMaterial {
40  /* OpenGL material arrays */
41  std::array<GLfloat, 4> Ambient = {{0.0f, 0.0f, 0.0f, 1.0f}};
42  std::array<GLfloat, 4> Diffuse = {{0.0f, 0.0f, 0.0f, 1.0f}};
43  std::array<GLfloat, 4> Emission = {{0.0f, 0.0f, 0.0f, 1.0f}};
44  std::array<GLfloat, 4> Specular = {{0.0f, 0.0f, 0.0f, 1.0f}};
45  std::array<GLfloat, 1> Shininess = {{0.0f}};
46  /* material transparency */
47  GLfloat Alpha = 1.0f;
48  /* enable the transparency effect for this material? */
49  bool EnableTransparency = false;
50  /* convenience typedef for an iterator over a std::map of SMaterial */
51  typedef std::map<std::string, SMaterial>::iterator TMapIterator;
52  };
53 
54  public:
55 
56  CQTOpenGLObjModel(const std::string& str_model);
57 
58  SMaterial& GetMaterial(const std::string& str_material);
59 
60  void Draw() const;
61 
62  private:
63 
64  struct SVertex {
65  SVertex(const CVector3& c_position = CVector3::ZERO,
66  const CVector3& c_normal = CVector3::ZERO,
67  const CVector2& c_texture = CVector2::ZERO) :
68  Position(c_position),
69  Normal(c_normal),
70  Texture(c_texture) {}
71 
72  CVector3 Position;
73  CVector3 Normal;
74  CVector2 Texture;
75  };
76 
77  struct STriangle {
78  STriangle(const SMaterial::TMapIterator& it_material,
79  const std::array<GLuint, 3>& arr_indices) :
80  Material(it_material),
81  Indices(arr_indices) {}
82 
83  SMaterial::TMapIterator Material;
84  std::array<GLuint, 3> Indices;
85  };
86 
87  struct SMesh {
88  SMesh(SMaterial::TMapIterator t_material) :
89  Material(t_material) {}
90 
91  std::vector<GLuint> Triangles;
92  SMaterial::TMapIterator Material;
93  };
94 
95  private:
96 
97  const QString& GetModelDir() const;
98 
99  void ImportGeometry(QTextStream& c_text_stream);
100 
101  void ImportMaterials(QTextStream& c_text_stream);
102 
103  GLuint AddVertex(SInt32 n_key,
104  const CVector3& c_position,
105  const CVector3& c_normal,
106  const CVector2& c_texture);
107 
108  void BuildMeshes();
109 
110  void GenerateOpenGLVectors();
111 
112  private:
113  /* parsed data */
114  std::vector<CVector3> m_vecVertexCoords;
115  std::vector<CVector2> m_vecTextureCoords;
116  std::vector<CVector3> m_vecNormals;
117  /* high-level primitives */
118  std::map<std::string, SMaterial> m_mapMaterials;
119  std::vector<SVertex> m_vecVertices;
120  std::vector<STriangle> m_vecTriangles;
121  std::vector<SMesh> m_vecMeshes;
122  std::multimap<SInt32, GLuint> m_mapVertexCache;
123  /* low-level primitives */
124  std::vector<GLdouble> m_vecOpenGLPositions;
125  std::vector<GLdouble> m_vecOpenGLNormals;
126  };
127 
128 }
129 
130 #endif
131 
signed int SInt32
32-bit signed integer.
Definition: datatypes.h:93
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
A 2D vector class.
Definition: vector2.h:27
static const CVector2 ZERO
The zero vector (0,0)
Definition: vector2.h:42
A 3D vector class.
Definition: vector3.h:31
static const CVector3 ZERO
The zero vector (0,0,0)
Definition: vector3.h:45
CQTOpenGLObjModel(const std::string &str_model)
SMaterial & GetMaterial(const std::string &str_material)
std::map< std::string, SMaterial >::iterator TMapIterator
std::array< GLfloat, 1 > Shininess