Guest User

Untitled

a guest
Feb 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. void Shader::setBool(const std::string &name, bool value) const
  2. {
  3. glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
  4. }
  5. void Shader::setInt(const std::string &name, int value) const
  6. {
  7. glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
  8. }
  9. void Shader::setFloat(const std::string &name, float value) const
  10. {
  11. glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
  12. }
  13.  
  14. class Shader
  15. {
  16. public:
  17. Shader();
  18. Shader(std::string const& vertex_path, std::string const& fragment_path);
  19.  
  20. //Activates the shader
  21. void use();
  22.  
  23. using size_type = unsigned int;
  24.  
  25. //Uniform utility functions
  26. template<typename T>
  27. void setUniform(std::string const& name, T const& v0) const
  28. {
  29. using RemovedRefCV = std::remove_cv_t<std::remove_reference_t<T>>;
  30. if constexpr (Traits::IsValidType<RemovedRefCV> >())
  31. {
  32. auto location = glGetUniformLocation(name);
  33.  
  34. if constexpr(std::is_same_v<RemovedRefCV, int>)
  35. {
  36. glUniform1i(location, v0);
  37. }
  38. else if constexpr (std::is_same_v<RemovedRefCV, unsigned int>)
  39. {
  40. glUniform1ui(location, v0);
  41. }
  42. else if constexpr (std::is_same_v<RemovedRefCV, float>)
  43. {
  44. glUniform1f(location, v0);
  45. }
  46. }
  47. else
  48. {
  49. static_assert(false, "Shader::set<std::string const& name, T v0>: Type T must be int, unsigned int or float");
  50. }
  51. }
  52.  
  53. template<typename T>
  54. void setUniform(std::string const& name, T const& v0, T const& v1) const
  55. {
  56. using RemovedRefCV = std::remove_cv_t<std::remove_reference_t<T>>;
  57. if constexpr (Traits::IsValidType<RemovedRefCV> > ())
  58. {
  59. auto location = glGetUniformLocation(name);
  60.  
  61. if constexpr(std::is_same_v<RemovedRefCV, int>)
  62. {
  63. glUniform2i(location, v0, v1);
  64. }
  65. else if constexpr (std::is_same_v<RemovedRefCV, unsigned int>)
  66. {
  67. glUniform2ui(location, v0, v1);
  68. }
  69. else if constexpr (std::is_same_v<RemovedRefCV, float>)
  70. {
  71. glUniform2f(location, v0, v1);
  72. }
  73. }
  74. else
  75. {
  76. static_assert(false, "Shader::set<std::string const& name, T v0, T v1>: Type T must be int, unsigned int or float");
  77. }
  78. }
  79.  
  80. template<typename T>
  81. void setUniform(std::string const& name, T const& v0, T const& v1, T const& v2) const
  82. {
  83. using RemovedRefCV = std::remove_cv_t<std::remove_reference_t<T>>;
  84. if constexpr (Traits::IsValidType<RemovedRefCV> > ())
  85. {
  86. auto location = glGetUniformLocation(name);
  87.  
  88. if constexpr(std::is_same_v<RemovedRefCV, int>)
  89. {
  90. glUniform3i(location, v0, v1, v2);
  91. }
  92. else if constexpr (std::is_same_v<RemovedRefCV, unsigned int>)
  93. {
  94. glUniform3ui(location, v0, v1, v2);
  95. }
  96. else if constexpr (std::is_same_v<RemovedRefCV, float>)
  97. {
  98. glUniform3f(location, v0, v1, v2);
  99. }
  100. }
  101. else
  102. {
  103. static_assert(false, "Shader::set<std::string const& name, T v0, T v1, T v2>: Type T must be int, unsigned int or float");
  104. }
  105. }
  106.  
  107. template<typename T>
  108. void setUniform(std::string const& name, T const& v0, T const& v1, T const& v2, T const& v3) const
  109. {
  110. using RemovedRefCV = std::remove_cv_t<std::remove_reference_t<T>>;
  111. if constexpr (Traits::IsValidType<RemovedRefCV> > ())
  112. {
  113. auto location = glGetUniformLocation(name);
  114.  
  115. if constexpr(std::is_same_v<RemovedRefCV, int>)
  116. {
  117. glUniform4i(location, v0, v1, v2, v3);
  118. }
  119. else if constexpr (std::is_same_v<RemovedRefCV, unsigned int>)
  120. {
  121. glUniform4ui(location, v0, v1, v2, v3);
  122. }
  123. else if constexpr (std::is_same_v<RemovedRefCV, float>)
  124. {
  125. glUniform4f(location, v0, v1, v2, v3);
  126. }
  127. }
  128. else
  129. {
  130. static_assert(false, "Shader::set<std::string const& name, T v0, T v1, T v2, T v3>: Type T must be int, unsigned int or float");
  131. }
  132. }
  133.  
  134. template<typename T>
  135. void setUniform(std::string const& name, size_type count, const T* v) const
  136. {
  137.  
  138. }
  139.  
  140. private:
  141. GLuint ID;
  142. bool loaded;
  143.  
  144. class Traits
  145. {
  146. private:
  147. template<typename T, typename = std::void_t<>>
  148. struct IsValidTypeHelper : std::false_type
  149. {
  150.  
  151. };
  152.  
  153. template<typename T>
  154. struct IsValidTypeHelper<T, std::enable_if_t<std::is_same_v<decltype(T), float> ||
  155. std::is_same_v<decltype(T), int> ||
  156. std::is_same_v<decltype(T), unsigned int>
  157. >
  158. >
  159. : std::true_type {};
  160.  
  161. template<typename T>
  162. struct IsValidTypeT : IsValidTypeHelper<T>::type {};
  163.  
  164. public:
  165. template<typename T> using IsValidType = typename IsValidTypeT<T>::type;
  166. };
  167. };
Add Comment
Please, Sign In to add comment