OGLplus (0.52.0) a C++ wrapper for OpenGL

obj_mesh.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_SHAPES_OBJ_MESH_1304161247_HPP
14 #define OGLPLUS_SHAPES_OBJ_MESH_1304161247_HPP
15 
16 #include <oglplus/face_mode.hpp>
17 #include <oglplus/shapes/draw.hpp>
18 
20 
21 #include <oglplus/detail/any_iter.hpp>
22 
23 #include <oglplus/math/sphere.hpp>
24 
25 #include <array>
26 #include <vector>
27 #include <iostream>
28 #include <cctype>
29 #include <string>
30 
31 namespace oglplus {
32 namespace shapes {
33 
35 class ObjMesh
36  : public DrawingInstructionWriter
37  , public DrawMode
38 {
39 private:
40  struct _loading_options
41  {
42  bool load_normals;
43  bool load_tangents;
44  bool load_bitangents;
45  bool load_texcoords;
46  bool load_materials;
47 
48  _loading_options(bool load_all = true)
49  {
50  All(load_all);
51  }
52 
53  _loading_options& All(bool load_all = true)
54  {
55  load_normals = load_all;
56  load_tangents = load_all;
57  load_bitangents = load_all;
58  load_texcoords = load_all;
59  load_materials = load_all;
60  return *this;
61  }
62 
63  _loading_options& Nothing(void)
64  {
65  return All(false);
66  }
67 
68  _loading_options& Normals(bool load = true)
69  {
70  load_normals = load;
71  return *this;
72  }
73 
74  _loading_options& Tangents(bool load = true)
75  {
76  load_tangents = load;
77  return *this;
78  }
79 
80  _loading_options& Bitangents(bool load = true)
81  {
82  load_bitangents = load;
83  return *this;
84  }
85 
86  _loading_options& TexCoords(bool load = true)
87  {
88  load_texcoords = load;
89  return *this;
90  }
91 
92  _loading_options& Materials(bool load = true)
93  {
94  load_materials = load;
95  return *this;
96  }
97  };
98 
99  // vertex positions
100  std::vector<double> _pos_data;
101  // vertex normals
102  std::vector<double> _nml_data;
103  // vertex tangents
104  std::vector<double> _tgt_data;
105  // vertex bitangents
106  std::vector<double> _btg_data;
107  // vertex tex coords
108  std::vector<double> _tex_data;
109  // material numbers
110  std::vector<GLuint> _mtl_data;
111  // material names
112  std::vector<std::string> _mtl_names;
113 
114  struct _vert_indices
115  {
116  GLuint _pos;
117  GLuint _nml;
118  GLuint _tex;
119  GLuint _mtl;
120 
121  _vert_indices(void)
122  : _pos(0)
123  , _nml(0)
124  , _tex(0)
125  , _mtl(0)
126  { }
127  };
128 
129  // the vertex offsets and counts for individual meshes
130  std::vector<std::string> _mesh_names;
131  std::vector<GLuint> _mesh_offsets;
132  std::vector<GLuint> _mesh_counts;
133 
134  bool _load_index(
135  GLuint& value,
136  GLuint count,
137  std::string::const_iterator& i,
138  std::string::const_iterator& e
139  );
140 
141  bool _load_indices(
142  _vert_indices& indices,
143  const _vert_indices& counts,
144  std::string::const_iterator& i,
145  std::string::const_iterator& e
146  );
147 
148  void _load_meshes(
149  const _loading_options& opts,
150  aux::AnyInputIter<const char*> names_begin,
151  aux::AnyInputIter<const char*> names_end,
152  std::istream& input
153  );
154 
155  void _call_load_meshes(
156  std::istream& input,
157  aux::AnyInputIter<const char*> names_begin,
158  aux::AnyInputIter<const char*> names_end,
159  _loading_options opts
160  );
161 public:
162  typedef _loading_options LoadingOptions;
163 
164  ObjMesh(
165  std::istream& input,
166  LoadingOptions opts = LoadingOptions()
167  )
168  {
169  const char** p = nullptr;
170  _call_load_meshes(input, p, p, opts);
171  }
172 
173  template <typename NameStr, std::size_t NN>
174  ObjMesh(
175  std::istream& input,
176  const std::array<NameStr, NN>& names,
177  LoadingOptions opts = LoadingOptions()
178  )
179  {
180  _call_load_meshes(
181  input,
182  names.begin(),
183  names.end(),
184  opts
185  );
186  }
187 
190  {
191  return FaceOrientation::CCW;
192  }
193 
194  typedef GLuint (ObjMesh::*VertexAttribFunc)(std::vector<GLfloat>&) const;
195 
197  template <typename T>
198  GLuint Positions(std::vector<T>& dest) const
199  {
200  dest.clear();
201  dest.insert(dest.begin(), _pos_data.begin(), _pos_data.end());
202  return 3;
203  }
204 
206  template <typename T>
207  GLuint Normals(std::vector<T>& dest) const
208  {
209  dest.clear();
210  dest.insert(dest.begin(), _nml_data.begin(), _nml_data.end());
211  return 3;
212  }
213 
215  template <typename T>
216  GLuint Tangents(std::vector<T>& dest) const
217  {
218  dest.clear();
219  dest.insert(dest.begin(), _tgt_data.begin(), _tgt_data.end());
220  return 3;
221  }
222 
224  template <typename T>
225  GLuint Bitangents(std::vector<T>& dest) const
226  {
227  dest.clear();
228  dest.insert(dest.begin(), _btg_data.begin(), _btg_data.end());
229  return 3;
230  }
231 
233  template <typename T>
234  GLuint TexCoordinates(std::vector<T>& dest) const
235  {
236  dest.clear();
237  dest.insert(dest.begin(), _tex_data.begin(), _tex_data.end());
238  return 3;
239  }
240 
242  template <typename T>
243  GLuint MaterialNumbers(std::vector<T>& dest) const
244  {
245  dest.clear();
246  dest.insert(dest.begin(), _mtl_data.begin(), _mtl_data.end());
247  return 1;
248  }
249 
251  const std::string& MaterialName(GLuint mat_num) const
252  {
253  return _mtl_names[mat_num];
254  }
255 
257  bool QueryMeshIndex(const std::string& name, GLuint& index) const;
258 
260  GLuint GetMeshIndex(const std::string& name) const;
261 
262 #if OGLPLUS_DOCUMENTATION_ONLY
263 
272  typedef VertexAttribsInfo<ObjMesh> VertexAttribs;
273 #else
274  typedef VertexAttribsInfo<
275  ObjMesh,
276  std::tuple<
277  VertexPositionsTag,
278  VertexNormalsTag,
279  VertexTangentsTag,
280  VertexBitangentsTag,
281  VertexTexCoordinatesTag,
282  VertexMaterialNumbersTag
283  >
284  > VertexAttribs;
285 #endif
286 
287  Spheref MakeBoundingSphere(void) const;
288 
290  template <typename T>
291  void BoundingSphere(oglplus::Sphere<T>& bounding_sphere) const
292  {
293  bounding_sphere = oglplus::Sphere<T>(MakeBoundingSphere());
294  }
295 
297  typedef std::vector<GLuint> IndexArray;
298 
300  IndexArray Indices(Default = Default()) const
301  {
302  return IndexArray();
303  }
304 
307 
310  {
312  }
313 };
314 
315 } // shapes
316 } // oglplus
317 
318 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
319 #include <oglplus/shapes/obj_mesh.ipp>
320 #endif // OGLPLUS_LINK_LIBRARY
321 
322 #endif // include guard
Implementation of shape draw instructions.
IndexArray Indices(Default=Default()) const
Returns element indices that are used with the drawing instructions.
Definition: obj_mesh.hpp:300
GLuint GetMeshIndex(const std::string &name) const
Gets the index of the mesh with the specified name, throws on error.
DrawingInstructions Instructions(Default=Default()) const
Returns the instructions for rendering of faces.
Definition: obj_mesh.hpp:309
VertexAttribsInfo< ObjMesh > VertexAttribs
Vertex attribute information for this shape builder.
Definition: obj_mesh.hpp:272
GLuint Bitangents(std::vector< T > &dest) const
Makes the vertex bi-tangents and returns the number of values per vertex.
Definition: obj_mesh.hpp:225
PrimitiveType
Primitive type enumeration.
Definition: primitive_type.hpp:29
FaceOrientation FaceWinding(void) const
Returns the winding direction of faces.
Definition: obj_mesh.hpp:189
Sphere utility class.
GLuint Tangents(std::vector< T > &dest) const
Makes the vertex tangents and returns the number of values per vertex.
Definition: obj_mesh.hpp:216
GLuint Positions(std::vector< T > &dest) const
Makes the vertex positions and returns the number of values per vertex.
Definition: obj_mesh.hpp:198
Class providing attributes and instructions for drawing of mesh loaded from obj.
Definition: obj_mesh.hpp:35
void BoundingSphere(oglplus::Sphere< T > &bounding_sphere) const
Queries the bounding sphere coordinates and dimensions.
Definition: obj_mesh.hpp:291
GLuint TexCoordinates(std::vector< T > &dest) const
Makes the texture coordinates returns the number of values per vertex.
Definition: obj_mesh.hpp:234
FaceOrientation
Face orientation enumeration.
Definition: face_mode.hpp:62
OpenGL face type-related enumeration.
bool QueryMeshIndex(const std::string &name, GLuint &index) const
Queries the index of the mesh with the specified name.
const std::string & MaterialName(GLuint mat_num) const
Returns the name of the i-th material.
Definition: obj_mesh.hpp:251
DrawingInstructions Instructions(PrimitiveType primitive) const
Returns the instructions for rendering of faces.
Classes providing additional information about the shape builders.
Class encapsulating the instructions for drawing of a shape.
Definition: draw.hpp:219
std::vector< GLuint > IndexArray
The type of the index container returned by Indices()
Definition: obj_mesh.hpp:297
GLuint MaterialNumbers(std::vector< T > &dest) const
Makes the material numbers returns the number of values per vertex.
Definition: obj_mesh.hpp:243
CCW: Counter-clockwise.
GLuint Normals(std::vector< T > &dest) const
Makes the vertex normals and returns the number of values per vertex.
Definition: obj_mesh.hpp:207

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).