OGLplus (0.52.0) a C++ wrapper for OpenGL

wrapper.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_SHAPES_WRAPPER_1202020923_HPP
14 #define OGLPLUS_SHAPES_WRAPPER_1202020923_HPP
15 
16 #include <oglplus/config/compiler.hpp>
17 #include <oglplus/config/basic.hpp>
18 #include <oglplus/string/def.hpp>
19 #include <oglplus/object/array.hpp>
21 #include <oglplus/vertex_array.hpp>
23 #include <oglplus/buffer.hpp>
24 #include <oglplus/program.hpp>
25 #include <oglplus/context.hpp>
26 
27 #include <oglplus/math/sphere.hpp>
28 
29 #include <oglplus/shapes/draw.hpp>
31 
32 #include <vector>
33 #include <functional>
34 #include <iterator>
35 #include <cassert>
36 
37 namespace oglplus {
38 namespace shapes {
39 
42 {
43 protected:
44  FaceOrientation _face_winding;
45  // helper object encapsulating shape drawing instructions
46  shapes::DrawingInstructions _shape_instr;
47 
48  // index type properties
49  shapes::ElementIndexInfo _index_info;
50 
51  Context _gl;
52 
53  // A vertex array object for the rendered shape
54  Optional<VertexArray> _vao;
55 
56  // VBOs for the shape's vertex attributes
57  Array<Buffer> _vbos;
58 
59  // numbers of values per vertex for the individual attributes
60  std::vector<GLuint> _npvs;
61 
62  // names of the individual vertex attributes
63  std::vector<String> _names;
64 
65  // the origin and radius of the bounding sphere
66  Spheref _bounding_sphere;
67 
68  template <class ShapeBuilder, class ShapeIndices, typename Iterator>
69  void _init(
70  const ShapeBuilder& builder,
71  const ShapeIndices& shape_indices,
72  Iterator name,
73  Iterator end
74  )
75  {
76  NoVertexArray().Bind();
77  typename ShapeBuilder::VertexAttribs vert_attr_info;
78  unsigned i = 0;
79  std::vector<GLfloat> data;
80  while(name != end)
81  {
82  auto getter = vert_attr_info.VertexAttribGetter(
83  data,
84  *name
85  );
86  if(getter != nullptr)
87  {
88  _vbos[i].Bind(Buffer::Target::Array);
89  _npvs[i] = getter(builder, data);
90  _names[i] = *name;
91 
92  Buffer::Data(Buffer::Target::Array, data);
93  }
94  ++name;
95  ++i;
96  }
97 
98  if(!shape_indices.empty())
99  {
100  assert((i+1) == _npvs.size());
101  assert((i+1) == _vbos.size());
102 
103  _npvs[i] = 1;
104  _vbos[i].Bind(Buffer::Target::ElementArray);
105  Buffer::Data(
106  Buffer::Target::ElementArray,
107  shape_indices
108  );
109  }
110 
111  builder.BoundingSphere(_bounding_sphere);
112  }
113 public:
114  template <typename Iterator, class ShapeBuilder, class Selector>
116  Iterator names_begin,
117  Iterator names_end,
118  const ShapeBuilder& builder,
119  Selector selector
120  ): _face_winding(builder.FaceWinding())
121  , _shape_instr(builder.Instructions(selector))
122  , _index_info(builder)
123  , _vbos(std::distance(names_begin, names_end)+1)
124  , _npvs(std::distance(names_begin, names_end)+1, 0)
125  , _names(std::distance(names_begin, names_end))
126  {
127  this->_init(
128  builder,
129  builder.Indices(selector),
130  names_begin,
131  names_end
132  );
133  }
134 
136  : _face_winding(temp._face_winding)
137  , _shape_instr(std::move(temp._shape_instr))
138  , _index_info(temp._index_info)
139  , _gl(std::move(temp._gl))
140  , _vao(std::move(temp._vao))
141  , _vbos(std::move(temp._vbos))
142  , _npvs(std::move(temp._npvs))
143  , _names(std::move(temp._names))
144  { }
145 
146 #if !OGLPLUS_NO_DELETED_FUNCTIONS
147  ShapeWrapperBase(const ShapeWrapperBase&) = delete;
148 #else
149 private:
151 public:
152 #endif
153 
154  VertexArray VAOForProgram(const ProgramOps& prog) const;
155 
156  void UseInProgram(const ProgramOps& prog)
157  {
158  _vao = VAOForProgram(prog);
159  }
160 
161  void Use(void)
162  {
163  _vao.Bind();
164  }
165 
166  FaceOrientation FaceWinding(void) const
167  {
168  return _face_winding;
169  }
170 
171  void Draw(void) const
172  {
173  _gl.FrontFace(_face_winding);
174  _shape_instr.Draw(_index_info, 1, 0);
175  }
176 
177  void Draw(GLuint inst_count) const
178  {
179  _gl.FrontFace(_face_winding);
180  _shape_instr.Draw(_index_info, inst_count, 0);
181  }
182 
183  void Draw(GLuint inst_count, GLuint base_inst) const
184  {
185  _gl.FrontFace(_face_winding);
186  _shape_instr.Draw(_index_info, inst_count, base_inst);
187  }
188 
189  void Draw(const std::function<bool (GLuint)>& drawing_driver) const
190  {
191  _gl.FrontFace(_face_winding);
192  _shape_instr.Draw(_index_info, 1, 0, drawing_driver);
193  }
194 
195  const Spheref& BoundingSphere(void) const
196  {
197  return _bounding_sphere;
198  }
199 };
200 
202 template <typename Selector>
204  : public ShapeWrapperBase
205 {
206 private:
207  static Selector _sel(void) { return Selector(); }
208 public:
210  : ShapeWrapperBase(static_cast<ShapeWrapperBase&&>(temp))
211  { }
212 
213  template <typename StdRange, class ShapeBuilder>
215  const StdRange& names,
216  const ShapeBuilder& builder
217  ): ShapeWrapperBase(names.begin(), names.end(), builder, _sel())
218  { }
219 
220  template <typename StdRange, class ShapeBuilder>
222  const StdRange& names,
223  const ShapeBuilder& builder,
224  const ProgramOps& prog
225  ): ShapeWrapperBase(names.begin(), names.end(), builder, _sel())
226  {
227  UseInProgram(prog);
228  }
229 
230 #if !OGLPLUS_NO_INITIALIZER_LISTS
231  template <class ShapeBuilder>
233  const std::initializer_list<const GLchar*>& names,
234  const ShapeBuilder& builder
235  ): ShapeWrapperBase(names.begin(), names.end(), builder, _sel())
236  { }
237 
238  template <class ShapeBuilder>
240  const std::initializer_list<const GLchar*>& names,
241  const ShapeBuilder& builder,
242  const ProgramOps& prog
243  ): ShapeWrapperBase(names.begin(), names.end(), builder, _sel())
244  {
245  UseInProgram(prog);
246  }
247 #endif
248 
249  template <class ShapeBuilder>
251  const GLchar** names,
252  unsigned name_count,
253  const ShapeBuilder& builder
254  ): ShapeWrapperBase(names, names+name_count, builder, _sel())
255  { }
256 
257  template <class ShapeBuilder>
259  const GLchar** names,
260  unsigned name_count,
261  const ShapeBuilder& builder,
262  const ProgramOps& prog
263  ): ShapeWrapperBase(names, names+name_count, builder, _sel())
264  {
265  UseInProgram(prog);
266  }
267 
268  template <class ShapeBuilder>
270  const GLchar* name,
271  const ShapeBuilder& builder
272  ): ShapeWrapperBase(&name, (&name)+1, builder, _sel())
273  { }
274 
275  template <class ShapeBuilder>
277  const GLchar* name,
278  const ShapeBuilder& builder,
279  const ProgramOps& prog
280  ): ShapeWrapperBase(&name, (&name)+1, builder, _sel())
281  {
282  UseInProgram(prog);
283  }
284 };
285 
288 
289 } // shapes
290 } // oglplus
291 
292 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
293 #include <oglplus/shapes/wrapper.ipp>
294 #endif // OGLPLUS_LINK_LIBRARY
295 
296 #endif // include guard
Implementation of shape draw instructions.
VertexArray wrappers.
Wraps instructions and VBOs and VAO used to render a shape built by a ShapeBuilder.
Definition: wrapper.hpp:203
Sphere utility class.
static void FrontFace(FaceOrientation orientation)
Sets the polygon facing mode.
Definition: rasterization.hpp:37
String type definition and related functions.
Declaration of OpenGL's state wrapper.
Helper class storing information about shape element index datatype.
Definition: draw.hpp:30
Template wrapper for Objects, making them optional.
FaceOrientation
Face orientation enumeration.
Definition: face_mode.hpp:62
Wrapper for the current OpenGL context operations.
Definition: context.hpp:61
Wraps instructions and VAO+VBOs used to render a shape built by a ShapeBuilder.
Definition: wrapper.hpp:41
Program wrappers.
std::size_t size(void) const
Returns the number of instances in the array.
Definition: array.hpp:102
Array data structure.
ObjectZero< ObjZeroOps< tag::ImplicitSel, tag::VertexArray > > NoVertexArray
An oglplus_object encapsulating vertex array zero functionality.
Definition: vertex_array.hpp:144
Classes providing additional information about the shape builders.
Buffer wrappers.
Class encapsulating the instructions for drawing of a shape.
Definition: draw.hpp:219
Class wrapping program functions (with direct state access)
Definition: program.hpp:176
VertexAttrib wrappers.
Object< VertexArrayOps > VertexArray
An oglplus_object encapsulating vertex array object functionality.
Definition: vertex_array.hpp:150

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