OGLplus (0.52.0) a C++ wrapper for OpenGL

blender_mesh.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_SHAPES_BLENDER_MESH_1206011111_HPP
14 #define OGLPLUS_SHAPES_BLENDER_MESH_1206011111_HPP
15 
16 #include <oglplus/face_mode.hpp>
17 #include <oglplus/math/vector.hpp>
18 #include <oglplus/math/matrix.hpp>
19 #include <oglplus/math/sphere.hpp>
20 
21 #include <oglplus/shapes/draw.hpp>
22 
24 
25 #include <oglplus/imports/blend_file.hpp>
26 
27 #include <oglplus/detail/any_iter.hpp>
28 
29 #include <vector>
30 #include <array>
31 #include <stdexcept>
32 #include <cassert>
33 
34 namespace oglplus {
35 namespace shapes {
36 
39  : public DrawingInstructionWriter
40  , public DrawMode
41 {
42 private:
43  struct _loading_options
44  {
45  const char* scene_name;
46  bool load_normals;
47  bool load_tangents;
48  bool load_bitangents;
49  bool load_texcoords;
50  bool load_materials;
51 
52  _loading_options(bool load_all = true)
53  : scene_name(nullptr)
54  {
55  All(load_all);
56  }
57 
58  _loading_options& All(bool load_all = true)
59  {
60  load_normals = load_all;
61  load_tangents = load_all;
62  load_bitangents = load_all;
63  load_texcoords = load_all;
64  load_materials = load_all;
65  return *this;
66  }
67 
68  _loading_options& Nothing(void)
69  {
70  return All(false);
71  }
72 
73  _loading_options& Normals(bool load = true)
74  {
75  load_normals = load;
76  return *this;
77  }
78 
79  _loading_options& Tangents(bool load = true)
80  {
81  load_tangents = load;
82  return *this;
83  }
84 
85  _loading_options& Bitangents(bool load = true)
86  {
87  load_bitangents = load;
88  return *this;
89  }
90 
91  _loading_options& TexCoords(bool load = true)
92  {
93  load_texcoords = load;
94  return *this;
95  }
96 
97  _loading_options& Materials(bool load = true)
98  {
99  load_materials = load;
100  return *this;
101  }
102  };
103 
104  // vertex positions
105  std::vector<GLfloat> _pos_data;
106  // vertex normals
107  std::vector<GLfloat> _nml_data;
108  // vertex tangentials
109  std::vector<GLfloat> _tgt_data;
110  // vertex bitangentials
111  std::vector<GLfloat> _btg_data;
112  // vertex tex coords
113  std::vector<GLfloat> _uvc_data;
114  // material numbers
115  std::vector<GLshort> _mtl_data;
116 
117  // vectors with vertex indices
118  std::vector<GLuint> _idx_data;
119 
120  // the index offsets and counts of individual meshes
121  std::vector<GLuint> _mesh_offsets;
122  std::vector<GLuint> _mesh_n_elems;
123 
124  // find the scene by name or the default scene
125  imports::BlendFileFlatStructBlockData _find_scene(
126  const _loading_options& /*opts*/,
127  imports::BlendFile& blend_file,
128  imports::BlendFileStructGlobBlock& glob_block
129  )
130  {
131  // TODO: find the scene by name
132  return blend_file[glob_block.curscene];
133  }
134 
135  // load a single mesh from a scene
136  void _load_mesh(
137  const _loading_options& opts,
138  imports::BlendFile& blend_file,
139  imports::BlendFileFlatStructBlockData& object_mesh_data,
140  const Mat4f& mesh_matrix,
141  GLuint& index_offset
142  );
143 
144  // load a single object from a scene
145  void _load_object(
146  const _loading_options& opts,
147  aux::AnyInputIter<const char*> names_begin,
148  aux::AnyInputIter<const char*> names_end,
149  imports::BlendFile& blend_file,
150  imports::BlendFileFlatStructBlockData& object_data,
151  imports::BlendFilePointer object_data_ptr,
152  GLuint& index_offset
153  );
154 
155  void _load_meshes(
156  const _loading_options& opts,
157  aux::AnyInputIter<const char*> names_begin,
158  aux::AnyInputIter<const char*> names_end,
159  imports::BlendFile& blend_file
160  );
161 
162  void _call_load_meshes(
163  imports::BlendFile& blend_file,
164  const char* scene_name,
165  aux::AnyInputIter<const char*> names_begin,
166  aux::AnyInputIter<const char*> names_end,
167  _loading_options opts
168  );
169 public:
170  typedef _loading_options LoadingOptions;
171 
172  BlenderMesh(imports::BlendFile& blend_file)
173  {
174  _call_load_meshes(
175  blend_file,
176  nullptr,
177  (const char**)nullptr,
178  (const char**)nullptr,
179  LoadingOptions()
180  );
181  }
182 
183  template <typename NameStr, std::size_t NN>
184  BlenderMesh(
185  imports::BlendFile& blend_file,
186  const std::array<NameStr, NN>& names,
187  LoadingOptions opts = LoadingOptions()
188  )
189  {
190  _call_load_meshes(
191  blend_file,
192  nullptr,
193  names.begin(),
194  names.end(),
195  opts
196  );
197  }
198 
201  {
202  return FaceOrientation::CCW;
203  }
204 
205  typedef GLuint (BlenderMesh::*VertexAttribFunc)(std::vector<GLfloat>&) const;
206 
208  template <typename T>
209  GLuint Positions(std::vector<T>& dest) const
210  {
211  dest.clear();
212  dest.insert(dest.end(), _pos_data.begin(), _pos_data.end());
213  return 3;
214  }
215 
217  template <typename T>
218  GLuint Normals(std::vector<T>& dest) const
219  {
220  dest.clear();
221  dest.insert(dest.end(), _nml_data.begin(), _nml_data.end());
222  return 3;
223  }
224 
226  template <typename T>
227  GLuint Tangents(std::vector<T>& dest) const
228  {
229  dest.clear();
230  dest.insert(dest.end(), _tgt_data.begin(), _tgt_data.end());
231  return 3;
232  }
233 
235  template <typename T>
236  GLuint Bitangents(std::vector<T>& dest) const
237  {
238  dest.clear();
239  dest.insert(dest.end(), _btg_data.begin(), _btg_data.end());
240  return 3;
241  }
242 
244  template <typename T>
245  GLuint TexCoordinates(std::vector<T>& dest) const
246  {
247  dest.clear();
248  dest.insert(dest.end(), _uvc_data.begin(), _uvc_data.end());
249  return 2;
250  }
251 
252  template <typename T>
253  GLuint MaterialNumbers(std::vector<T>& dest) const
254  {
255  dest.clear();
256  dest.insert(dest.end(), _mtl_data.begin(), _mtl_data.end());
257  return 1;
258  }
259 
260 #if OGLPLUS_DOCUMENTATION_ONLY
261 
267  typedef VertexAttribsInfo<BlenderMesh> VertexAttribs;
268 #else
269  typedef VertexAttribsInfo<
270  BlenderMesh,
271  std::tuple<
272  VertexPositionsTag,
273  VertexNormalsTag,
274  VertexTangentsTag,
275  VertexBitangentsTag,
276  VertexTexCoordinatesTag,
277  VertexMaterialNumbersTag
278  >
279  > VertexAttribs;
280 #endif
281 
282  Spheref GetBoundingSphere(void) const;
283 
285  template <typename T>
286  void BoundingSphere(oglplus::Sphere<T>& bounding_sphere) const
287  {
288  bounding_sphere = oglplus::Sphere<T>(GetBoundingSphere());
289  }
290 
292  typedef std::vector<GLuint> IndexArray;
293 
295  IndexArray Indices(Default = Default()) const
296  {
297  return _idx_data;
298  }
299 
301  DrawingInstructions Instructions(Default = Default()) const;
302 };
303 
304 } // shapes
305 } // oglplus
306 
307 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
308 #include <oglplus/shapes/blender_mesh.ipp>
309 #endif // OGLPLUS_LINK_LIBRARY
310 
311 #endif // include guard
Implementation of shape draw instructions.
IndexArray Indices(Default=Default()) const
Returns element indices that are used with the drawing instructions.
Definition: blender_mesh.hpp:295
Sphere utility class.
FaceOrientation
Face orientation enumeration.
Definition: face_mode.hpp:62
OpenGL face type-related enumeration.
GLuint Positions(std::vector< T > &dest) const
Makes the vertex positions and returns the number of values per vertex.
Definition: blender_mesh.hpp:209
GLuint Bitangents(std::vector< T > &dest) const
Makes the vertex bitangents and returns the number of values per vertex.
Definition: blender_mesh.hpp:236
GLuint Tangents(std::vector< T > &dest) const
Makes the vertex tangents and returns the number of values per vertex.
Definition: blender_mesh.hpp:227
Class providing vertex attributes and instructions for drawing of a mesh.
Definition: blender_mesh.hpp:38
VertexAttribsInfo< BlenderMesh > VertexAttribs
Vertex attribute information for this shape builder.
Definition: blender_mesh.hpp:267
void BoundingSphere(oglplus::Sphere< T > &bounding_sphere) const
Queries the bounding sphere coordinates and dimensions.
Definition: blender_mesh.hpp:286
GLuint Normals(std::vector< T > &dest) const
Makes the vertex normals and returns the number of values per vertex.
Definition: blender_mesh.hpp:218
Classes providing additional information about the shape builders.
Class encapsulating the instructions for drawing of a shape.
Definition: draw.hpp:219
CCW: Counter-clockwise.
std::vector< GLuint > IndexArray
The type of the index container returned by Indices()
Definition: blender_mesh.hpp:292
FaceOrientation FaceWinding(void) const
Returns the winding direction of faces.
Definition: blender_mesh.hpp:200
A vector class.
GLuint TexCoordinates(std::vector< T > &dest) const
Makes the texture coordinates and returns the number of values per vertex.
Definition: blender_mesh.hpp:245
DrawingInstructions Instructions(Default=Default()) const
Returns the instructions for rendering of faces.
A matrix class.

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).