| ... | ... |
@@ -27,6 +27,9 @@ constexpr auto frag_color = 0; |
| 27 | 27 |
|
| 28 | 28 |
int main() |
| 29 | 29 |
{
|
| 30 |
+ // Local data. |
|
| 31 |
+ auto light_count = 3; |
|
| 32 |
+ |
|
| 30 | 33 |
// Global settings. |
| 31 | 34 |
Shader::root("assets/shaders");
|
| 32 | 35 |
Shader::defines({
|
| ... | ... |
@@ -49,6 +52,7 @@ int main() |
| 49 | 52 |
// Use. |
| 50 | 53 |
player |
| 51 | 54 |
.use() |
| 55 |
+ .uniform("light_count", light_count)
|
|
| 52 | 56 |
.validate(); |
| 53 | 57 |
} |
| 54 | 58 |
``` |
| ... | ... |
@@ -257,6 +261,16 @@ value is of the wrong type. [`use`](#use) must be called before calling |
| 257 | 261 |
|
| 258 | 262 |
[uniform]: https://www.khronos.org/opengl/wiki/Uniform |
| 259 | 263 |
|
| 264 |
+#### Scalar uniforms |
|
| 265 |
+ |
|
| 266 |
+Template specializations are provided for all `GL*` types corresponding to a |
|
| 267 |
+`glUniform1*` variant. As special cases, `GLboolean` and `bool` map to the `i` |
|
| 268 |
+(`GLint`) variant. The `GLdouble` specialization is [`delete`][]d to avoid |
|
| 269 |
+accidental conversions, change the type (with an `f`/`F` suffix in case of |
|
| 270 |
+literals) or use an explicit cast to get the `GLfloat` specialization. |
|
| 271 |
+ |
|
| 272 |
+[`delete`]: https://en.cppreference.com/w/cpp/language/function#Deleted_functions |
|
| 273 |
+ |
|
| 260 | 274 |
#### User-defined uniforms |
| 261 | 275 |
|
| 262 | 276 |
Support for e.g. third party mathematics libraries can be added by providing |
| ... | ... |
@@ -267,8 +281,6 @@ the supplied variables `TYPE const & value` and `GLint const & location`, |
| 267 | 281 |
probably as parameters to `glUniform*`. `GLSHADER_UNIFORM_DELETE(TYPE)` can be |
| 268 | 282 |
used to [`delete`][] the specialization for a given type. |
| 269 | 283 |
|
| 270 |
-[`delete`]: https://en.cppreference.com/w/cpp/language/function#Deleted_functions |
|
| 271 |
- |
|
| 272 | 284 |
## Dependencies |
| 273 | 285 |
|
| 274 | 286 |
Public (interface): |
| ... | ... |
@@ -155,4 +155,23 @@ inline Shader & Shader::use() |
| 155 | 155 |
} |
| 156 | 156 |
|
| 157 | 157 |
|
| 158 |
+// Uniform scalar template specializations. |
|
| 159 |
+ |
|
| 160 |
+#define GLSHADER_UNIFORM_SCALAR_(TYPE, SUFFIX) \ |
|
| 161 |
+ GLSHADER_UNIFORM( \ |
|
| 162 |
+ TYPE, \ |
|
| 163 |
+ glUniform1##SUFFIX( \ |
|
| 164 |
+ location, value \ |
|
| 165 |
+ ); \ |
|
| 166 |
+ ) |
|
| 167 |
+ |
|
| 168 |
+GLSHADER_UNIFORM_SCALAR_(bool, i) |
|
| 169 |
+GLSHADER_UNIFORM_SCALAR_(GLboolean, i) |
|
| 170 |
+GLSHADER_UNIFORM_SCALAR_(GLint, i) |
|
| 171 |
+GLSHADER_UNIFORM_SCALAR_(GLuint, ui) |
|
| 172 |
+GLSHADER_UNIFORM_SCALAR_(GLfloat, f) |
|
| 173 |
+ |
|
| 174 |
+GLSHADER_UNIFORM_DELETE(GLdouble) |
|
| 175 |
+ |
|
| 176 |
+ |
|
| 158 | 177 |
#endif // GLSHADER_SHADER_HPP_ |
| ... | ... |
@@ -137,6 +137,30 @@ GLTEST(2, 0, 640, 480, glshader) |
| 137 | 137 |
"Failed to initialize vertex input 'vert_unset' of shader program 'tests/vert_unset.vert'." |
| 138 | 138 |
); |
| 139 | 139 |
|
| 140 |
+ // Uniform scalars. |
|
| 141 |
+ constexpr auto blue = 0.25F; |
|
| 142 |
+ GLTEST_EXPECT_EXCEPTION(false, |
|
| 143 |
+ Shader({"tests/uniform_blue.frag"}).uniform("noncurrent", 1.0F),
|
|
| 144 |
+ "Failed to set uniform 'noncurrent' of shader program 'tests/uniform_blue.frag'; " |
|
| 145 |
+ "shader program not current." |
|
| 146 |
+ ) |
|
| 147 |
+ GLTEST_EXPECT_EXCEPTION(false, |
|
| 148 |
+ Shader({"tests/uniform_blue.frag"}).use().uniform("nonexistent", 1.0F),
|
|
| 149 |
+ "Failed to set uniform 'nonexistent' of shader program 'tests/uniform_blue.frag'; " |
|
| 150 |
+ "uniform required but not found." |
|
| 151 |
+ ) |
|
| 152 |
+ Shader({"tests/uniform_blue.frag"}).use().uniform("nonexistent", 1.0F, false);
|
|
| 153 |
+ GLTEST_EXPECT_EXCEPTION(false, |
|
| 154 |
+ Shader({"tests/uniform_blue.frag"}).use().validate(),
|
|
| 155 |
+ "Failed to validate shader program 'tests/uniform_blue.frag'; " |
|
| 156 |
+ "uniform 'blue' not set." |
|
| 157 |
+ ) |
|
| 158 |
+ GLTEST_EXPECT_EXCEPTION(false, |
|
| 159 |
+ Shader({"tests/uniform_blue.frag"}).use().uniform("blue", 1),
|
|
| 160 |
+ "Failed to set uniform 'blue' of shader program 'tests/uniform_blue.frag'; " |
|
| 161 |
+ "got error GL_INVALID_OPERATION (wrong type?)." |
|
| 162 |
+ ) |
|
| 163 |
+ |
|
| 140 | 164 |
auto all = Shader({
|
| 141 | 165 |
"tests/all.vert", |
| 142 | 166 |
"tests/all.frag", |
| ... | ... |
@@ -144,6 +168,7 @@ GLTEST(2, 0, 640, 480, glshader) |
| 144 | 168 |
|
| 145 | 169 |
all |
| 146 | 170 |
.use() |
| 171 |
+ .uniform("blue", blue)
|
|
| 147 | 172 |
.validate(); |
| 148 | 173 |
|
| 149 | 174 |
constexpr auto size = 0.5F; |