OGLplus (0.52.0) a C++ wrapper for OpenGL

sampler.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_SAMPLER_1107121519_HPP
14 #define OGLPLUS_SAMPLER_1107121519_HPP
15 
16 #include <oglplus/glfunc.hpp>
17 #include <oglplus/math/vector.hpp>
18 #include <oglplus/error/object.hpp>
21 #include <oglplus/data_type.hpp>
23 #include <oglplus/texture_wrap.hpp>
24 #include <oglplus/texture_compare.hpp>
26 #include <oglplus/texture_unit.hpp>
27 #include <cassert>
28 
29 namespace oglplus {
30 
31 // if samplers are available
32 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_3_3 || GL_ARB_sampler_objects
33 
35 
43 template <>
44 class ObjGenDelOps<tag::Sampler>
45 {
46 protected:
47  static void Gen(tag::Generate, GLsizei count, GLuint* names)
48  {
49  assert(names != nullptr);
50  OGLPLUS_GLFUNC(GenSamplers)(count, names);
51  OGLPLUS_CHECK_SIMPLE(GenSamplers);
52  }
53 #if GL_VERSION_4_5 || GL_ARB_direct_state_access
54  static void Gen(tag::Create, GLsizei count, GLuint* names)
55  {
56  assert(names != nullptr);
57  OGLPLUS_GLFUNC(CreateSamplers)(count, names);
58  OGLPLUS_CHECK_SIMPLE(CreateSamplers);
59  }
60 #endif
61 
62  static void Delete(GLsizei count, GLuint* names)
63  {
64  assert(names != nullptr);
65  OGLPLUS_GLFUNC(DeleteSamplers)(count, names);
66  OGLPLUS_VERIFY_SIMPLE(DeleteSamplers);
67  }
68 
69  static GLboolean IsA(GLuint name)
70  {
71  assert(name != 0);
72  GLboolean result = OGLPLUS_GLFUNC(IsSampler)(name);
73  OGLPLUS_VERIFY_SIMPLE(IsSampler);
74  return result;
75  }
76 };
77 
79 template <>
80 class ObjBindingOps<tag::Sampler>
81 {
82 private:
83  static GLenum _binding_query(TextureUnitSelector target);
84 protected:
85  static GLuint _binding(TextureUnitSelector target);
86 public:
89 
91 
95  static SamplerName Binding(Target target)
96  {
97  return SamplerName(_binding(target));
98  }
99 
101 
105  static void Bind(
106  Target target,
107  SamplerName sampler
108  )
109  {
110  OGLPLUS_GLFUNC(BindSampler)(
111  GLuint(target),
112  GetGLName(sampler)
113  );
114  OGLPLUS_VERIFY(
115  BindSampler,
116  ObjectError,
117  Object(sampler).
118  Index(GLuint(target))
119  );
120  }
121 
122 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_4_4 || GL_ARB_multi_bind
123  static void Bind(
124  GLuint first,
125  GLsizei count,
126  const GLuint* names
127  )
128  {
129  OGLPLUS_GLFUNC(BindSamplers)(
130  first,
131  count,
132  names
133  );
134  OGLPLUS_VERIFY_SIMPLE(BindSamplers);
135  }
136 
138 
145  static void Bind(
146  GLuint first,
147  const Sequence<SamplerName>& samplers
148  )
149  {
150  Bind(first, GLsizei(samplers.size()), GetGLNames(samplers));
151  }
152 #endif
153 };
154 
156 
159 template <>
161  : public SamplerName
162  , public ObjBindingOps<tag::Sampler>
163 {
164 protected:
165  ObjCommonOps(void){ }
166 public:
168 
170 
174  void Bind(Target target) const
175  {
176  Bind(target, *this);
177  }
178 };
179 
181 
183 template <>
184 class ObjectOps<tag::DirectState, tag::Sampler>
185  : public ObjZeroOps<tag::DirectState, tag::Sampler>
186 {
187 protected:
188  ObjectOps(void){ }
189 public:
190  GLint GetIntParam(GLenum query) const
191  {
192  GLint result = 0;
193  OGLPLUS_GLFUNC(GetSamplerParameteriv)(
194  _name,
195  query,
196  &result
197  );
198  OGLPLUS_CHECK(
199  GetSamplerParameteriv,
200  ObjectError,
201  Object(*this).
202  EnumParam(query)
203  );
204  return result;
205  }
206 
207  GLfloat GetFloatParam(GLenum query) const
208  {
209  GLfloat result = 0;
210  OGLPLUS_GLFUNC(GetSamplerParameterfv)(
211  _name,
212  query,
213  &result
214  );
215  OGLPLUS_CHECK(
216  GetSamplerParameterfv,
217  ObjectError,
218  Object(*this).
219  EnumParam(query)
220  );
221  return result;
222  }
223 
225 
231  {
232  GLfloat result[4];
233  OGLPLUS_GLFUNC(GetSamplerParameterfv)(
234  _name,
235  GL_TEXTURE_BORDER_COLOR,
236  result
237  );
238  OGLPLUS_CHECK(
239  GetSamplerParameterfv,
240  ObjectError,
241  Object(*this)
242  );
243  return Vector<GLfloat, 4>(result, 4);
244  }
245 
247 
253  {
254  OGLPLUS_GLFUNC(SamplerParameterfv)(
255  _name,
256  GL_TEXTURE_BORDER_COLOR,
257  Data(color)
258  );
259  OGLPLUS_CHECK(
260  SamplerParameterfv,
261  ObjectError,
262  Object(*this)
263  );
264  }
265 
267 
273  {
274  GLint result[4];
275  OGLPLUS_GLFUNC(GetSamplerParameterIiv)(
276  _name,
277  GL_TEXTURE_BORDER_COLOR,
278  result
279  );
280  OGLPLUS_CHECK(
281  GetSamplerParameterIiv,
282  ObjectError,
283  Object(*this)
284  );
285  return Vector<GLint, 4>(result, 4);
286  }
287 
289 
295  {
296  OGLPLUS_GLFUNC(SamplerParameterIiv)(
297  _name,
298  GL_TEXTURE_BORDER_COLOR,
299  Data(color)
300  );
301  OGLPLUS_CHECK(
302  SamplerParameterIiv,
303  ObjectError,
304  Object(*this)
305  );
306  }
307 
309 
315  {
316  GLuint result[4];
317  OGLPLUS_GLFUNC(GetSamplerParameterIuiv)(
318  _name,
319  GL_TEXTURE_BORDER_COLOR,
320  result
321  );
322  OGLPLUS_CHECK(
323  GetSamplerParameterIuiv,
324  ObjectError,
325  Object(*this)
326  );
327  return Vector<GLuint, 4>(result, 4);
328  }
329 
331 
337  {
338  OGLPLUS_GLFUNC(SamplerParameterIuiv)(
339  _name,
340  GL_TEXTURE_BORDER_COLOR,
341  Data(color)
342  );
343  OGLPLUS_CHECK(
344  SamplerParameterIuiv,
345  ObjectError,
346  Object(*this)
347  );
348  }
349 
351 
357  {
358  return TextureCompareMode(GetIntParam(
359  GL_TEXTURE_COMPARE_MODE
360  ));
361  }
362 
364 
370  {
371  OGLPLUS_GLFUNC(SamplerParameteri)(
372  _name,
373  GL_TEXTURE_COMPARE_MODE,
374  GLenum(mode)
375  );
376  OGLPLUS_CHECK(
377  SamplerParameteri,
378  ObjectError,
379  Object(*this).
380  EnumParam(mode)
381  );
382  }
383 
385 
391  {
392  return CompareFunction(GetIntParam(
393  GL_TEXTURE_COMPARE_FUNC
394  ));
395  }
396 
398 
404  {
405  OGLPLUS_GLFUNC(SamplerParameteri)(
406  _name,
407  GL_TEXTURE_COMPARE_FUNC,
408  GLenum(func)
409  );
410  OGLPLUS_CHECK(
411  SamplerParameteri,
412  ObjectError,
413  Object(*this).
414  EnumParam(func)
415  );
416  }
417 
419 
424  GLfloat LODBias(void) const
425  {
426  return GetFloatParam(GL_TEXTURE_LOD_BIAS);
427  }
428 
430 
435  void LODBias(GLfloat value)
436  {
437  OGLPLUS_GLFUNC(SamplerParameterf)(
438  _name,
439  GL_TEXTURE_LOD_BIAS,
440  value
441  );
442  OGLPLUS_CHECK(
443  SamplerParameterf,
444  ObjectError,
445  Object(*this)
446  );
447  }
448 
450 
456  void Filter(TextureFilter filter) const
457  {
458  OGLPLUS_GLFUNC(SamplerParameteri)(
459  _name,
460  GL_TEXTURE_MIN_FILTER,
461  GLenum(filter)
462  );
463  OGLPLUS_CHECK(
464  SamplerParameteri,
465  ObjectError,
466  Object(*this).
467  EnumParam(filter)
468  );
469  OGLPLUS_GLFUNC(SamplerParameteri)(
470  _name,
471  GL_TEXTURE_MAG_FILTER,
472  GLenum(filter)
473  );
474  OGLPLUS_CHECK(
475  SamplerParameteri,
476  ObjectError,
477  Object(*this).
478  EnumParam(filter)
479  );
480  }
481 
483 
489  {
490  return TextureMagFilter(GetIntParam(
491  GL_TEXTURE_MAG_FILTER
492  ));
493  }
494 
496 
502  {
503  OGLPLUS_GLFUNC(SamplerParameteri)(
504  _name,
505  GL_TEXTURE_MAG_FILTER,
506  GLenum(filter)
507  );
508  OGLPLUS_CHECK(
509  SamplerParameteri,
510  ObjectError,
511  Object(*this).
512  EnumParam(filter)
513  );
514  }
515 
517 
523  {
524  return TextureMinFilter(GetIntParam(
525  GL_TEXTURE_MIN_FILTER
526  ));
527  }
528 
530 
536  {
537  OGLPLUS_GLFUNC(SamplerParameteri)(
538  _name,
539  GL_TEXTURE_MIN_FILTER,
540  GLenum(filter)
541  );
542  OGLPLUS_CHECK(
543  SamplerParameteri,
544  ObjectError,
545  Object(*this).
546  EnumParam(filter)
547  );
548  }
549 
551 
556  GLfloat MinLOD(void) const
557  {
558  return GetFloatParam(GL_TEXTURE_MIN_LOD);
559  }
560 
562 
567  void MinLOD(GLfloat value)
568  {
569  OGLPLUS_GLFUNC(SamplerParameterf)(
570  _name,
571  GL_TEXTURE_MIN_LOD,
572  value
573  );
574  OGLPLUS_CHECK(
575  SamplerParameterf,
576  ObjectError,
577  Object(*this)
578  );
579  }
580 
582 
587  GLfloat MaxLOD(void) const
588  {
589  return GetFloatParam(GL_TEXTURE_MAX_LOD);
590  }
591 
593 
598  void MaxLOD(GLfloat value)
599  {
600  OGLPLUS_GLFUNC(SamplerParameterf)(
601  _name,
602  GL_TEXTURE_MAX_LOD,
603  value
604  );
605  OGLPLUS_CHECK(
606  SamplerParameterf,
607  ObjectError,
608  Object(*this)
609  );
610  }
611 
613 
618  {
619  return TextureWrap(GetIntParam(GLenum(coord)));
620  }
621 
623 
628  {
629  OGLPLUS_GLFUNC(SamplerParameteri)(
630  _name,
631  GLenum(coord),
632  GLenum(mode)
633  );
634  OGLPLUS_CHECK(
635  SamplerParameteri,
636  ObjectError,
637  Object(*this).
638  EnumParam(mode)
639  );
640  }
641 
643 
648  TextureWrap WrapS(void) const
649  {
650  return Wrap(TextureWrapCoord::S);
651  }
652 
654 
659  void WrapS(TextureWrap mode)
660  {
661  Wrap(TextureWrapCoord::S, mode);
662  }
663 
665 
670  TextureWrap WrapT(void) const
671  {
672  return Wrap(TextureWrapCoord::T);
673  }
674 
676 
681  void WrapT(TextureWrap mode)
682  {
683  Wrap(TextureWrapCoord::T, mode);
684  }
685 
687 
692  TextureWrap WrapR(void) const
693  {
694  return Wrap(TextureWrapCoord::R);
695  }
696 
698 
703  void WrapR(TextureWrap mode)
704  {
705  Wrap(TextureWrapCoord::R, mode);
706  }
707 
708 #if OGLPLUS_DOCUMENTATION_ONLY || GL_ARB_seamless_cubemap_per_texture
709 
715  bool Seamless(void) const
716  {
717  return GetIntParam(GL_TEXTURE_CUBE_MAP_SEAMLESS) == GL_TRUE;
718  }
719 
721 
726  void Seamless(bool enable)
727  {
728  OGLPLUS_GLFUNC(SamplerParameteri)(
729  _name,
730  GL_TEXTURE_CUBE_MAP_SEAMLESS,
731  enable?GL_TRUE:GL_FALSE
732  );
733  OGLPLUS_CHECK(
734  SamplerParameteri,
735  ObjectError,
736  Object(*this)
737  );
738  }
739 #endif
740 };
741 
743 typedef ObjectOps<tag::DirectState, tag::Sampler>
745 
746 // Helper class for syntax-sugar operators
747 struct SamplerOpsAndSlot
748 {
749  SamplerOps& sam;
750  GLint slot;
751 
752  SamplerOpsAndSlot(SamplerOps& sa, GLint sl)
753  : sam(sa)
754  , slot(sl)
755  { }
756 };
757 
758 // syntax sugar operators
759 inline SamplerOpsAndSlot operator | (
760  SamplerOps& sam,
761  GLuint slot
762 )
763 {
764  return SamplerOpsAndSlot(sam, slot);
765 }
766 
767 // Bind
768 inline SamplerOps& operator << (
769  SamplerOps& sam,
770  TextureUnitSelector tus
771 )
772 {
773  sam.Bind(tus);
774  return sam;
775 }
776 
777 // Filter
778 inline SamplerOps& operator << (
779  SamplerOps& sam,
780  TextureFilter filter
781 )
782 {
783  sam.Filter(filter);
784  return sam;
785 }
786 
787 // MinFilter
788 inline SamplerOps& operator << (
789  SamplerOps& sam,
790  TextureMinFilter filter
791 )
792 {
793  sam.MinFilter(filter);
794  return sam;
795 }
796 
797 // MagFilter
798 inline SamplerOps& operator << (
799  SamplerOps& sam,
800  TextureMagFilter filter
801 )
802 {
803  sam.MagFilter(filter);
804  return sam;
805 }
806 
807 // CompareMode
808 inline SamplerOps& operator << (
809  SamplerOps& sam,
810  TextureCompareMode mode
811 )
812 {
813  sam.CompareMode(mode);
814  return sam;
815 }
816 
817 // CompareFunc
818 inline SamplerOps& operator << (
819  SamplerOps& sam,
820  CompareFunction func
821 )
822 {
823  sam.CompareFunc(func);
824  return sam;
825 }
826 
827 // Wrap
828 inline SamplerOps& operator << (
829  SamplerOps& sam,
830  TextureWrap wrap
831 )
832 {
833  sam.WrapR(wrap);
834  sam.WrapT(wrap);
835  sam.WrapS(wrap);
836  return sam;
837 }
838 
839 // Wrap
840 inline SamplerOps& operator << (
841  SamplerOpsAndSlot sas,
842  TextureWrap wrap
843 )
844 {
845  switch(sas.slot)
846  {
847  case 0: sas.sam.WrapS(wrap); break;
848  case 1: sas.sam.WrapT(wrap); break;
849  case 2: sas.sam.WrapR(wrap); break;
850  default: assert(!"Invalid texture wrap slot");
851  }
852  return sas.sam;
853 }
854 
855 // BorderColor
856 template <typename T>
857 inline SamplerOps& operator << (
858  SamplerOps& sam,
859  const Vector<T, 4>& col
860 )
861 {
862  sam.BorderColor(col);
863  return sam;
864 }
865 
867 
870 typedef ObjectZero<ObjZeroOps<tag::DirectState, tag::Sampler>>
872 
874 
877 typedef Object<SamplerOps> Sampler;
878 
879 #endif // sampler object
880 
881 } // namespace oglplus
882 
883 #endif // include guard
Texture wrap enumerations.
void MinLOD(GLfloat value)
Sets minimal LOD value.
Definition: sampler.hpp:567
void BorderColor(Vector< GLuint, 4 > color)
Sets the texture border color.
Definition: sampler.hpp:336
Common base class for Object name sequences.
Definition: fwd.hpp:139
void CompareMode(TextureCompareMode mode)
Sets the compare mode.
Definition: sampler.hpp:369
GLfloat LODBias(void) const
Gets the LOD bias value.
Definition: sampler.hpp:424
TextureWrap WrapR(void) const
Gets the wrap parameter for the R coordinate.
Definition: sampler.hpp:692
void MagFilter(TextureMagFilter filter)
Sets the magnification filter.
Definition: sampler.hpp:501
TextureUnitSelector Target
Sampler bind targets.
Definition: sampler.hpp:88
Vector< GLint, 4 > BorderColor(TypeTag< GLint >) const
Gets the texture border color.
Definition: sampler.hpp:272
void WrapT(TextureWrap mode)
Sets the wrap parameter for the T coordinate.
Definition: sampler.hpp:681
void Bind(Target target) const
Binds this sampler to the specified target (texture unit)
Definition: sampler.hpp:174
void Filter(TextureFilter filter) const
Sets both the minification and magnification filter.
Definition: sampler.hpp:456
void BorderColor(Vector< GLfloat, 4 > color)
Sets the texture border color.
Definition: sampler.hpp:252
bool Seamless(void) const
Gets the seamless cubemap setting.
Definition: sampler.hpp:715
TextureWrap WrapT(void) const
Gets the wrap parameter for the T coordinate.
Definition: sampler.hpp:670
TextureFilter
Texture filter enumeration.
Definition: texture_filter.hpp:29
CompareFunction CompareFunc(void) const
Gets the compare function.
Definition: sampler.hpp:390
void BorderColor(Vector< GLint, 4 > color)
Sets the texture border color.
Definition: sampler.hpp:294
TextureMinFilter MinFilter(void) const
Gets the minification filter.
Definition: sampler.hpp:522
TextureMinFilter
Texture minification filter enumeration.
Definition: texture_filter.hpp:71
TextureWrap Wrap(TextureWrapCoord coord) const
Gets the wrap parameter (TEXTURE_WRAP_*)
Definition: sampler.hpp:617
void CompareFunc(CompareFunction func)
Sets the compare function.
Definition: sampler.hpp:403
Declaration of OGLplus object-related error.
Object< SamplerOps > Sampler
An oglplus_object encapsulating the OpenGL sampler functionality.
Definition: sampler.hpp:877
Vector< GLfloat, 4 > BorderColor(TypeTag< GLfloat >) const
Gets the texture border color.
Definition: sampler.hpp:230
Generic OpenGL object wrapper.
A tag template used mainly for data-type-based function overload dispatching.
Definition: data_type.hpp:23
Vector< GLuint, 4 > BorderColor(TypeTag< GLuint >) const
Gets the texture border color.
Definition: sampler.hpp:314
std::size_t size(void) const
Returns the size of the sequence.
Definition: seq_tpl.hpp:166
void MaxLOD(GLfloat value)
Sets maximal LOD value.
Definition: sampler.hpp:598
TextureMagFilter
Texture magnification filter enumeration.
Definition: texture_filter.hpp:50
Implements operations applicable to named (non-zero) objects.
Definition: wrap_tpl.hpp:45
TextureWrap
Texture wrap enumeration.
Definition: texture_wrap.hpp:51
GLuint GetGLName(ObjectName< ObjTag > named)
Returns the GLuint OpenGL name assigned to named object.
Definition: name.hpp:38
static void Bind(Target target, SamplerName sampler)
Binds the specified sampler to the specified target (tex. unit)
Definition: sampler.hpp:105
Class wrapping sampler functions (with direct state access)
Definition: sampler.hpp:184
TextureCompareMode CompareMode(void) const
Gets the compare mode.
Definition: sampler.hpp:356
TextureMagFilter MagFilter(void) const
Gets the magnification filter.
Definition: sampler.hpp:488
OpenGL test/comparison function enumeration.
Sequence of Object names.
TextureCompareMode
Texture compare mode enumeration.
Definition: texture_compare.hpp:29
static void Bind(GLuint first, const Sequence< SamplerName > &samplers)
Sequentially bind samplers to texture units starting with first.
Definition: sampler.hpp:145
Helper macro for optional checking of availability of GL function.
TextureWrap WrapS(void) const
Gets the wrap parameter for the S coordinate.
Definition: sampler.hpp:648
const GLuint * GetGLNames(const Sequence< ObjName > &sequence)
Returns a pointer to array of GL object names stored in a sequence.
Definition: sequence.hpp:23
Texture magnification/minification filter enumerations.
Implements operations applicable to any object including object 0 (zero)
Definition: fwd.hpp:157
Data type-related declarations.
ObjectZero< ObjZeroOps< tag::DirectState, tag::Sampler > > NoSampler
Class that can be used to unbind the currently bound sampler.
Definition: sampler.hpp:871
GLfloat MinLOD(void) const
Gets minimal LOD value.
Definition: sampler.hpp:556
void MinFilter(TextureMinFilter filter)
Sets the minification filter.
Definition: sampler.hpp:535
Exception class for GL object-related errors.
Definition: object.hpp:24
void Wrap(TextureWrapCoord coord, TextureWrap mode)
Sets the wrap parameter (TEXTURE_WRAP_*)
Definition: sampler.hpp:627
GLfloat MaxLOD(void) const
Gets maximal LOD value.
Definition: sampler.hpp:587
ObjectOps< tag::DirectState, tag::Sampler > SamplerOps
Sampler operations with direct state access.
Definition: sampler.hpp:744
void Seamless(bool enable)
Sets the seamless cubemap setting.
Definition: sampler.hpp:726
TextureWrapCoord
Texture wrap parameter coordinate enumeration.
Definition: texture_wrap.hpp:28
void WrapR(TextureWrap mode)
Sets the wrap parameter for the R coordinate.
Definition: sampler.hpp:703
Type for the texture unit selector (implementation-dependent limited) number.
Definition: texture_unit.hpp:22
Implements operations applicable to any object and any operation kind.
Definition: fwd.hpp:151
void WrapS(TextureWrap mode)
Sets the wrap parameter for the S coordinate.
Definition: sampler.hpp:659
void LODBias(GLfloat value)
Sets the LOD bias value.
Definition: sampler.hpp:435
CompareFunction
Comparison function type enumeration.
Definition: compare_function.hpp:30
Basic template for vector types.
Definition: fwd.hpp:43
Texture and image unit selectors.
A common template for "named" objects like textures, buffers, etc.
Definition: fwd.hpp:136
A vector class.
static SamplerName Binding(Target target)
Returns the current Sampler bound to specified target (tex. unit)
Definition: sampler.hpp:95

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