OGLplus (0.52.0) a C++ wrapper for OpenGL

vert_attr_info.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_SHAPES_VERT_ATTR_INFO_1107121519_HPP
14 #define OGLPLUS_SHAPES_VERT_ATTR_INFO_1107121519_HPP
15 
16 #include <oglplus/string/ref.hpp>
17 
18 #include <tuple>
19 
20 namespace oglplus {
21 namespace shapes {
22 
23 template <class ShapeBuilder, class VertexAttributeTag>
24 struct VertexAttribInfo;
25 
26 #define OGLPLUS_SHAPES_HLPR_MAKE_VERT_ATTR_INFO(ATTR_NAME, GETTER_NAME) \
27 struct Vertex ## GETTER_NAME ## Tag { }; \
28  \
29 template <class ShapeBuilder> \
30 struct VertexAttribInfo<ShapeBuilder, Vertex ## GETTER_NAME ## Tag> \
31 { \
32 public: \
33  static const GLchar* _name(void) \
34  { \
35  return #ATTR_NAME; \
36  } \
37  \
38  template <typename T> \
39  struct _getter_proc \
40  { \
41  typedef GLuint (*type)(const ShapeBuilder&, std::vector<T>&); \
42  };\
43  \
44  template <typename T> \
45  static GLuint _getter_wrapper( \
46  const ShapeBuilder& make, \
47  std::vector<T>& dest \
48  ) \
49  { \
50  return make.GETTER_NAME(dest); \
51  } \
52  \
53  template <typename T> \
54  static typename _getter_proc<T>::type _getter(T*) \
55  { \
56  return &VertexAttribInfo::_getter_wrapper<T>; \
57  } \
58 };
59 
60 OGLPLUS_SHAPES_HLPR_MAKE_VERT_ATTR_INFO(Position, Positions)
61 OGLPLUS_SHAPES_HLPR_MAKE_VERT_ATTR_INFO(Normal, Normals)
62 OGLPLUS_SHAPES_HLPR_MAKE_VERT_ATTR_INFO(Tangent, Tangents)
63 OGLPLUS_SHAPES_HLPR_MAKE_VERT_ATTR_INFO(Bitangent, Bitangents)
64 OGLPLUS_SHAPES_HLPR_MAKE_VERT_ATTR_INFO(TexCoord, TexCoordinates)
65 OGLPLUS_SHAPES_HLPR_MAKE_VERT_ATTR_INFO(Material, MaterialNumbers)
66 
67 #undef OGLPLUS_SHAPES_HLPR_MAKE_VERT_ATTR_INFO
68 
69 #if OGLPLUS_DOCUMENTATION_ONLY
70 
73 class VertexAttribsInfo<ShapeBuiler>
74 {
75 public:
77  bool MakesVertexAttrib(StrCRef name) const;
78 
80 
90  template <typename T>
91  static GetterFunction VertexAttribGetter(StrCRef name);
92 };
93 #else
94 template <class ShapeBuilder, class VertexAttribTags, std::size_t N>
95 class VertexAttribsInfoBase
96 {
97 private:
98  static bool _has_vertex_attrib(
99  StrCRef,
100  std::integral_constant<std::size_t, N>,
101  std::integral_constant<std::size_t, N>
102  )
103  {
104  return false;
105  }
106 
107  template <std::size_t I>
108  static bool _has_vertex_attrib(
109  StrCRef name,
110  std::integral_constant<std::size_t, I>,
111  std::integral_constant<std::size_t, N>
112  )
113  {
114  auto info = VertexAttribInfo<
115  ShapeBuilder,
116  typename std::tuple_element<I, VertexAttribTags>::type
117  >();
118  if(name == info._name()) return true;
119  else return _has_vertex_attrib(
120  name,
121  std::integral_constant<std::size_t, I+1>(),
122  std::integral_constant<std::size_t, N>()
123  );
124  }
125 protected:
126  static bool _has_vertex_attrib(StrCRef name)
127  {
128  return _has_vertex_attrib(
129  name,
130  std::integral_constant<std::size_t, 0>(),
131  std::integral_constant<std::size_t, N>()
132  );
133  }
134 
135  template <typename T>
136  struct _getter_proc
137  {
138  typedef GLuint (*type)(const ShapeBuilder&, std::vector<T>&);
139  };
140 private:
141  template <typename T>
142  static typename _getter_proc<T>::type
143  _find_getter(
144  T*,
145  StrCRef,
146  std::integral_constant<std::size_t, N>,
147  std::integral_constant<std::size_t, N>
148  )
149  {
150  return nullptr;
151  }
152 
153  template <typename T, std::size_t I>
154  static typename _getter_proc<T>::type
155  _find_getter(
156  T* selector,
157  StrCRef name,
158  std::integral_constant<std::size_t, I>,
159  std::integral_constant<std::size_t, N>
160  )
161  {
162  auto info = VertexAttribInfo<
163  ShapeBuilder,
164  typename std::tuple_element<I, VertexAttribTags>::type
165  >();
166  if(std::strcmp(name.c_str(), info._name()) == 0)
167  {
168  return info._getter(selector);
169  }
170  return _find_getter(
171  selector,
172  name,
173  std::integral_constant<std::size_t, I+1>(),
174  std::integral_constant<std::size_t, N>()
175  );
176  }
177 
178 protected:
179  template <typename T>
180  static typename _getter_proc<T>::type
181  _find_getter(T* selector, StrCRef name)
182  {
183  return _find_getter(
184  selector,
185  name,
186  std::integral_constant<std::size_t, 0>(),
187  std::integral_constant<std::size_t, N>()
188  );
189  }
190 };
191 
192 template <class ShapeBuilder, class VertexAttribTags>
193 class VertexAttribsInfo
194  : public VertexAttribsInfoBase<
195  ShapeBuilder,
196  VertexAttribTags,
197  std::tuple_size<VertexAttribTags>::value
198 >
199 {
200 private:
201  typedef VertexAttribsInfoBase<
202  ShapeBuilder,
203  VertexAttribTags,
204  std::tuple_size<VertexAttribTags>::value
205  > _base;
206 public:
207  bool MakesVertexAttrib(StrCRef name) const
208  {
209  return _base::_has_vertex_attrib(name);
210  }
211 
212  template <typename T>
213  static typename _base::template _getter_proc<T>::type
214  VertexAttribGetter(
215  const std::vector<T>& /*selector*/,
216  StrCRef name
217  )
218  {
219  return _base::_find_getter((T*)nullptr, name);
220  }
221 };
222 #endif
223 
224 } // shapes
225 } // oglplus
226 
227 #endif // include guard
const Char * c_str(void) const
Returns the null-terminated c-string.
Definition: ref_tpl.hpp:200
String reference.
String const reference wrapper template.
Definition: ref_tpl.hpp:72

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