OGLplus (0.52.0) a C++ wrapper for OpenGL

vertex_attrib.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_VERTEX_ATTRIB_1107121519_HPP
14 #define OGLPLUS_VERTEX_ATTRIB_1107121519_HPP
15 
16 #include <oglplus/fwd.hpp>
17 #include <oglplus/glfunc.hpp>
18 #include <oglplus/data_type.hpp>
20 #include <oglplus/string/ref.hpp>
22 #include <oglplus/object/name.hpp>
25 #include <oglplus/prog_var/varpara_fns.hpp>
26 #include <oglplus/prog_var/set_ops.hpp>
28 
29 #include <type_traits>
30 
31 namespace oglplus {
32 
33 template <>
34 class ProgVarLocOps<tag::VertexAttrib>
35 {
36 private:
37  static const char* MsgGettingInactive(void);
38 protected:
39  static const char* MsgUsingInactive(void);
40 public:
42 
49  static void BindLocation(
50  ProgramName program,
51  VertexAttribSlot location,
52  StrCRef identifier
53  )
54  {
55  OGLPLUS_GLFUNC(BindAttribLocation)(
56  GetGLName(program),
57  GLuint(location),
58  identifier.c_str()
59  );
60  OGLPLUS_CHECK(
61  BindAttribLocation,
62  ProgVarError,
63  Program(program).
64  Identifier(identifier).
65  Index(GLuint(location))
66  );
67  }
68 
70 
82  static GLint GetLocation(
83  ProgramName program,
84  StrCRef identifier,
85  bool active_only
86  )
87  {
88  GLint result = OGLPLUS_GLFUNC(GetAttribLocation)(
89  GetGLName(program),
90  identifier.c_str()
91  );
92  OGLPLUS_CHECK(
93  GetAttribLocation,
94  ProgVarError,
95  Program(program).
96  Identifier(identifier)
97  );
98  OGLPLUS_HANDLE_ERROR_IF(
99  active_only && (result < 0),
100  GL_INVALID_OPERATION,
101  MsgGettingInactive(),
102  ProgVarError,
103  Program(program).
104  Identifier(identifier)
105  );
106  return result;
107  }
108 
109  static VertexAttribSlot GetLocation(
110  ProgramName program,
111  StrCRef identifier
112  )
113  {
114  return VertexAttribSlot(GetLocation(program, identifier, true));
115  }
116 
118 
122  static bool QueryActiveLocation(
123  ProgramName program,
124  StrCRef identifier,
125  VertexAttribSlot& location
126  )
127  {
128  GLint result = GetLocation(program, identifier, false);
129  if(result < 0) return false;
130  location = VertexAttribSlot(result);
131  return true;
132  }
133 
135 
180  static bool QueryCommonLocation(
181  const Sequence<ProgramName>& programs,
182  StrCRef identifier,
183  VertexAttribSlot& location
184  );
185 
187 
227  static VertexAttribSlot GetCommonLocation(
228  const Sequence<ProgramName>& programs,
229  StrCRef identifier
230  );
231 };
232 
233 template <>
234 class ProgVarSetters<tag::ImplicitSel, tag::VertexAttrib, tag::NativeTypes>
235 {
236 protected:
237  OGLPLUS_ERROR_CONTEXT(VertexAttrib, VertexAttrib)
238 
239  OGLPLUS_AUX_VARPARA_FNS(VertexAttrib, f, t, GLfloat)
240 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_3_0
241  OGLPLUS_AUX_VARPARA_FNS(VertexAttrib, d, t, GLdouble)
242  OGLPLUS_AUX_VARPARA_FNS(VertexAttrib, s, t, GLshort)
243 
244  OGLPLUS_AUX_VARPARA_FNS(VertexAttribI, i, t, GLint)
245  OGLPLUS_AUX_VARPARA_FNS(VertexAttribI, ui, t, GLuint)
246 #endif
247 
248  OGLPLUS_AUX_VARPARA_FNS(VertexAttrib, fv, v, GLfloat)
249 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_3_0
250  OGLPLUS_AUX_VARPARA_FNS(VertexAttrib, dv, v, GLdouble)
251  OGLPLUS_AUX_VARPARA_FNS(VertexAttrib, sv, v, GLshort)
252 
253  OGLPLUS_AUX_VARPARA_FNS(VertexAttribI, i, v, GLint)
254  OGLPLUS_AUX_VARPARA_FNS(VertexAttribI, ui, v, GLuint)
255 #endif
256 };
257 
258 template <>
259 class ProgVarCommonOps<tag::VertexAttrib>
260  : public ProgVarLoc<tag::VertexAttrib>
261 {
262 protected:
263  ProgVarCommonOps(VertexAttribLoc valoc)
264  : ProgVarLoc<tag::VertexAttrib>(valoc)
265  { }
266 
267  // Functions for autodetection of values-per-vertex
268  template <typename T>
269  static GLint _get_vpv(T*) { return 1; }
270 
271  template <typename T, std::size_t N>
272  static GLint _get_vpv(Vector<T, N>*) { return N; }
273 
274  template <typename T, std::size_t Rows, std::size_t Cols>
275  static GLint _get_vpv(Matrix<T, Rows, Cols>*) { return Rows*Cols; }
276 
277  // Functions for autodetection of element type
278  template <typename T>
279  static T _get_et(T* p);
280 
281  template <typename T, std::size_t N>
282  static T _get_et(Vector<T, N>*);
283 
284  template <typename T, std::size_t Rows, std::size_t Cols>
285  static T _get_et(Matrix<T, Rows, Cols>*);
286 public:
287  void Bind(StrCRef identifier)
288  {
289  BindLocation(
290  this->Program(),
291  this->_location,
292  identifier
293  );
294  }
295 
296 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_3_3
297 
303  void Divisor(GLuint divisor) const
304  {
305  OGLPLUS_GLFUNC(VertexAttribDivisor)(
306  _location,
307  divisor
308  );
309  OGLPLUS_CHECK_SIMPLE(VertexAttribDivisor);
310  }
311 #endif
312 };
313 
315 
318 template <typename OpsTag, typename T>
319 class ProgVarGetSetOps<OpsTag, tag::VertexAttrib, T>
320  : public ProgVarCommonOps<tag::VertexAttrib>
321  , public ProgVarBaseSetOps<OpsTag, tag::VertexAttrib, tag::NativeTypes, T, 16>
322 {
323 protected:
324  ProgVarGetSetOps(VertexAttribLoc valoc)
325  : ProgVarCommonOps<tag::VertexAttrib>(valoc)
326  { }
327 public:
329 
333  void SetValue(T value)
334  {
335  this->_do_set(_program, _location, value);
336  }
337 };
338 
339 template <typename OpsTag, typename T, std::size_t N>
340 class ProgVarGetSetOps<OpsTag, tag::VertexAttrib, Vector<T, N>>
341  : public ProgVarCommonOps<tag::VertexAttrib>
342  , public ProgVarBaseSetOps<OpsTag, tag::VertexAttrib, tag::NativeTypes, T, 4>
343 {
344 protected:
345  ProgVarGetSetOps(VertexAttribLoc valoc)
346  : ProgVarCommonOps<tag::VertexAttrib>(valoc)
347  { }
348 public:
349  void SetValue(const Vector<T, N>& value)
350  {
351  this->template _do_set<N>(_program, _location, Data(value));
352  }
353 };
354 
355 template <typename OpsTag, typename T, std::size_t R, std::size_t C>
356 class ProgVarGetSetOps<OpsTag, tag::VertexAttrib, Matrix<T, R, C>>
357  : public ProgVarCommonOps<tag::VertexAttrib>
358  , public ProgVarBaseSetOps<OpsTag, tag::VertexAttrib, tag::NativeTypes, T, 16>
359 {
360 protected:
361  ProgVarGetSetOps(VertexAttribLoc valoc)
362  : ProgVarCommonOps<tag::VertexAttrib>(valoc)
363  { }
364 public:
365  void SetValue(const Matrix<T, R, C>& value)
366  {
367  this->template _do_set<R*C>(_program, _location, Data(value));
368  }
369 };
370 
372  VertexAttrib,
373  tag::ImplicitSel,
374  tag::VertexAttrib,
375  tag::NoTypecheck
376 )
377 
378 
382 class VertexArrayAttrib
383  : public ProgVarCommonOps<tag::VertexAttrib>
384 {
385 public:
387 
391  VertexArrayAttrib(VertexAttribSlot location)
392  : ProgVarCommonOps<tag::VertexAttrib>(VertexAttribLoc(GLint(location)))
393  { }
394 
396 
400  VertexArrayAttrib(ProgramName program, VertexAttribSlot location)
401  : ProgVarCommonOps<tag::VertexAttrib>(
402  VertexAttribLoc(program, GLint(location))
403  ){ }
404 
406 
410  VertexArrayAttrib(ProgramName program, StrCRef identifier)
411  : ProgVarCommonOps<tag::VertexAttrib>(
412  VertexAttribLoc(program, identifier)
413  ){ }
414 
416 
451  const VertexArrayAttrib& Setup(
452  GLint values_per_vertex,
453  DataType data_type
454  ) const
455  {
456  if(data_type == DataType::Float)
457  {
458  Pointer(
459  values_per_vertex,
460  data_type,
461  false,
462  0,
463  nullptr
464  );
465  }
466 #ifdef GL_DOUBLE
467  else if(data_type == DataType::Double)
468  {
469  LPointer(
470  values_per_vertex,
471  data_type,
472  0,
473  nullptr
474  );
475  }
476 #endif
477  else
478  {
479  IPointer(
480  values_per_vertex,
481  data_type,
482  0,
483  nullptr
484  );
485  }
486  return *this;
487  }
488 
489  const VertexArrayAttrib& Setup(
490  GLint values_per_vertex,
491  std::integral_constant<DataType, DataType::Float>
492  ) const
493  {
494  return Pointer(
495  values_per_vertex,
497  false,
498  0,
499  nullptr
500  );
501  }
502 
503 #ifdef GL_DOUBLE
504  const VertexArrayAttrib& Setup(
505  GLint values_per_vertex,
506  std::integral_constant<DataType, DataType::Double>
507  ) const
508  {
509  return LPointer(
510  values_per_vertex,
512  0,
513  nullptr
514  );
515  }
516 #endif
517 
518  template <DataType DataTypeValue>
519  const VertexArrayAttrib& Setup(
520  GLint values_per_vertex,
521  std::integral_constant<DataType, DataTypeValue>
522  ) const
523  {
524  return IPointer(
525  values_per_vertex,
526  DataTypeValue,
527  0,
528  nullptr
529  );
530  }
531 
533 
541  template <typename T>
542  const VertexArrayAttrib& Setup(GLuint n = 1) const
543  {
544  typedef decltype(_get_et((T*)nullptr)) elem_type;
545 
546  return Setup(
547  _get_vpv((T*)nullptr)*n,
548  typename DataTypeCT<elem_type>::type()
549  );
550  }
551 
553 
557  const VertexArrayAttrib& Pointer(
558  GLint values_per_vertex,
559  DataType data_type,
560  bool normalized,
561  GLsizei stride,
562  const void* pointer
563  ) const
564  {
565  OGLPLUS_GLFUNC(VertexAttribPointer)(
566  _location,
567  values_per_vertex,
568  GLenum(data_type),
569  normalized ? GL_TRUE : GL_FALSE,
570  stride,
571  pointer
572  );
573  OGLPLUS_CHECK(
574  VertexAttribPointer,
575  Error,
576  EnumParam(data_type).
577  Index(_location)
578  );
579  return *this;
580  }
581 
583 
587  const VertexArrayAttrib& IPointer(
588  GLuint values_per_vertex,
589  DataType data_type,
590  GLsizei stride,
591  const void* pointer
592  ) const
593  {
594  OGLPLUS_GLFUNC(VertexAttribIPointer)(
595  _location,
596  values_per_vertex,
597  GLenum(data_type),
598  stride,
599  pointer
600  );
601  OGLPLUS_CHECK(
602  VertexAttribIPointer,
603  Error,
604  EnumParam(data_type).
605  Index(_location)
606  );
607  return *this;
608  }
609 
610 
611 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_4_2 || GL_ARB_vertex_attrib_64bit
612 
617  const VertexArrayAttrib& LPointer(
618  GLuint values_per_vertex,
619  DataType data_type,
620  GLsizei stride,
621  const void* pointer
622  ) const
623  {
624  OGLPLUS_GLFUNC(VertexAttribLPointer)(
625  _location,
626  values_per_vertex,
627  GLenum(data_type),
628  stride,
629  pointer
630  );
631  OGLPLUS_CHECK(VertexAttribLPointer,
632  Error,
633  EnumParam(data_type).
634  Index(_location)
635  );
636  return *this;
637  }
638 #else
639  const VertexArrayAttrib& LPointer(
640  GLuint,
641  DataType,
642  GLsizei,
643  const void*
644  ) const
645  {
646  assert(!
647  "The glVertexAttribLPointer function is "
648  "required but not available! Double-precision "
649  "vertex attribute values are not supported."
650  );
651  return *this;
652  }
653 #endif
654 
655 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_4_3 || GL_ARB_vertex_attrib_binding
656 
661  const VertexArrayAttrib& Format(
662  GLint values_per_vertex,
663  DataType data_type,
664  bool normalized,
665  GLuint relative_offset
666  ) const
667  {
668  OGLPLUS_GLFUNC(VertexAttribFormat)(
669  _location,
670  values_per_vertex,
671  GLenum(data_type),
672  normalized ? GL_TRUE : GL_FALSE,
673  relative_offset
674  );
675  OGLPLUS_CHECK(
676  VertexAttribFormat,
677  Error,
678  EnumParam(data_type).
679  Index(_location)
680  );
681  return *this;
682  }
683 
685 
689  const VertexArrayAttrib& IFormat(
690  GLint values_per_vertex,
691  DataType data_type,
692  GLuint relative_offset
693  ) const
694  {
695  OGLPLUS_GLFUNC(VertexAttribIFormat)(
696  _location,
697  values_per_vertex,
698  GLenum(data_type),
699  relative_offset
700  );
701  OGLPLUS_CHECK(
702  VertexAttribIFormat,
703  Error,
704  EnumParam(data_type).
705  Index(_location)
706  );
707  return *this;
708  }
709 
711 
715  const VertexArrayAttrib& LFormat(
716  GLint values_per_vertex,
717  DataType data_type,
718  GLuint relative_offset
719  ) const
720  {
721  OGLPLUS_GLFUNC(VertexAttribLFormat)(
722  _location,
723  values_per_vertex,
724  GLenum(data_type),
725  relative_offset
726  );
727  OGLPLUS_CHECK(
728  VertexAttribLFormat,
729  Error,
730  EnumParam(data_type).
731  Index(_location)
732  );
733  return *this;
734  }
735 #endif
736 
738 
742  const VertexArrayAttrib& Enable(void) const
743  {
744  OGLPLUS_GLFUNC(EnableVertexAttribArray)(_location);
745  OGLPLUS_VERIFY(
746  EnableVertexArrayAttrib,
747  Error,
748  Index(_location)
749  );
750  return *this;
751  }
752 
754 
758  const VertexArrayAttrib& Disable(void) const
759  {
760  OGLPLUS_GLFUNC(DisableVertexAttribArray)(_location);
761  OGLPLUS_VERIFY(
762  DisableVertexArrayAttrib,
763  Error,
764  Index(_location)
765  );
766  return *this;
767  }
768 };
769 
771 
776 template <std::size_t N>
777 inline VertexArrayAttrib operator | (
778  ProgramName program,
779  const GLchar (&identifier)[N]
780 )
781 {
782  return VertexArrayAttrib(program, identifier);
783 }
784 
786 
791 inline VertexArrayAttrib operator | (
792  ProgramName program,
793  GLuint location
794 )
795 {
796  return VertexArrayAttrib(program, VertexAttribSlot(location));
797 }
798 
799 } // namespace oglplus
800 
801 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
802 #include <oglplus/vertex_attrib.ipp>
803 #endif
804 
805 #endif // include guard
const Char * c_str(void) const
Returns the null-terminated c-string.
Definition: ref_tpl.hpp:200
Program variable wrapper.
Program variable location wrapper.
VertexAttribSlot object.
DataType
OpenGL data type enumeration.
Definition: data_type.hpp:34
void SetValue(T value)
Set the value of the vertex attribute.
Definition: vertex_attrib.hpp:333
ProgVarLoc(void)
Default construction.
Definition: location.hpp:55
Forward declarations.
OGLPLUS_DECLARE_PROG_VAR(Uniform, tag::ImplicitSel, tag::Uniform, tag::NoTypecheck) typedef Uniform< GLint > UniformSampler
Uniform sampler.
GLuint GetGLName(ObjectName< ObjTag > named)
Returns the GLuint OpenGL name assigned to named object.
Definition: name.hpp:38
String reference.
Sequence of Object names.
String const reference wrapper template.
Definition: ref_tpl.hpp:72
Helper macro for optional checking of availability of GL function.
Data type-related declarations.
Type for the vertex attribute slot (implementation-dependent limited) number.
Definition: vertex_attrib_slot.hpp:27
Exception class for general OpenGL errors.
Definition: basic.hpp:43
Base class for OpenGL "named" objects.
Declaration of OGLplus program-variable-related error.
A common template for "named" objects like textures, buffers, etc.
Definition: fwd.hpp:136
PixelDataFormat Format
Alias for PixelDataFormat.
Definition: pixel_data.hpp:66

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