Browse code

Add uniform GLM

Robert Cranston authored on 16/05/2020 10:39:49
Showing 5 changed files

... ...
@@ -21,6 +21,7 @@ common(
21 21
     PACKAGES
22 22
         OpenGL
23 23
         GLEW
24
+        glm
24 25
     FETCHCONTENT
25 26
         https://git.rcrnstn.net/rcrnstn/cxx-str
26 27
         https://git.rcrnstn.net/rcrnstn/gltest
... ...
@@ -31,4 +32,5 @@ common(
31 32
         cxx-str
32 33
     DEPENDENCIES_TESTS
33 34
         gltest
35
+        glm
34 36
 )
... ...
@@ -14,8 +14,10 @@ Overview of usage:
14 14
 
15 15
 ```cpp
16 16
 #include <GL/glew.h>
17
+#include <glm/glm.hpp>
17 18
 
18 19
 #include <glshader.hpp>
20
+#include <glshader_glm.hpp>
19 21
 
20 22
 
21 23
 // Global data.
... ...
@@ -29,6 +31,7 @@ int main()
29 31
 {
30 32
     // Local data.
31 33
     auto light_count = 3;
34
+    auto color = glm::vec4{1, 0, 0, 1};
32 35
 
33 36
     // Global settings.
34 37
     Shader::root("assets/shaders");
... ...
@@ -53,6 +56,7 @@ int main()
53 56
     player
54 57
         .use()
55 58
         .uniform("light_count", light_count)
59
+        .uniform("color", color)
56 60
         .validate();
57 61
 }
58 62
 ```
... ...
@@ -271,6 +275,15 @@ literals) or use an explicit cast to get the `GLfloat` specialization.
271 275
 
272 276
 [`delete`]: https://en.cppreference.com/w/cpp/language/function#Deleted_functions
273 277
 
278
+#### OpenGL Mathematics (GLM)
279
+
280
+[OpenGL Mathematics (GLM)][] specializations are provided in the include file
281
+`glshader_glm.hpp`. OpenGL Mathematics (GLM) uses [OpenGL Shading Language
282
+(GLSL)][] naming conventions.
283
+
284
+[OpenGL Mathematics (GLM)]: https://glm.g-truc.net
285
+[OpenGL Shading Language (GLSL)]: https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language
286
+
274 287
 #### User-defined uniforms
275 288
 
276 289
 Support for e.g. third party mathematics libraries can be added by providing
... ...
@@ -287,6 +300,7 @@ Public (interface):
287 300
 
288 301
 -   [OpenGL][], system (e.g. [`libgl1-mesa-dev`][]).
289 302
 -   [OpenGL Extension Wrangler (GLEW)][], system (e.g. [`libglew-dev`][]).
303
+-   [OpenGL Mathematics (GLM)][], **optional**, system (e.g. [`libglm-dev`][]).
290 304
 
291 305
 Private (build):
292 306
 
... ...
@@ -295,12 +309,15 @@ Private (build):
295 309
 Private (tests):
296 310
 
297 311
 -   [GLFW][], system (e.g. [`libglfw3-dev`][]).
312
+-   [OpenGL Mathematics (GLM)][], system (e.g. [`libglm-dev`][]).
298 313
 -   [`gltest`][], downloaded as part of the CMake configure step.
299 314
 
300 315
 [OpenGL Extension Wrangler (GLEW)]: http://glew.sourceforge.net
316
+[OpenGL Mathematics (GLM)]: https://glm.g-truc.net
301 317
 [GLFW]: https://www.glfw.org
302 318
 [`libgl1-mesa-dev`]: https://packages.debian.org/search?keywords=libgl1-mesa-dev
303 319
 [`libglew-dev`]: https://packages.debian.org/search?keywords=libglew-dev
320
+[`libglm-dev`]: https://packages.debian.org/search?keywords=libglm-dev
304 321
 [`libglfw3-dev`]: https://packages.debian.org/search?keywords=libglfw3-dev
305 322
 [`str`]: https://git.rcrnstn.net/rcrnstn/str
306 323
 [`gltest`]: https://git.rcrnstn.net/rcrnstn/gltest
... ...
@@ -8,9 +8,10 @@
8 8
 
9 9
 
10 10
 uniform float blue;
11
+uniform vec4 color;
11 12
 
12 13
 
13 14
 void main()
14 15
 {
15
-    gl_FragColor = vec4(red, green, blue, 1.0);
16
+    gl_FragColor = vec4(red, green, blue, color.a);
16 17
 }
17 18
new file mode 100644
... ...
@@ -0,0 +1,73 @@
1
+#ifndef GLSHADER_GLM_HPP_
2
+#define GLSHADER_GLM_HPP_
3
+
4
+
5
+#include <GL/glew.h>
6
+
7
+#include <glm/glm.hpp>
8
+#include <glm/gtc/type_ptr.hpp>
9
+
10
+#include <glshader.hpp>
11
+
12
+
13
+#define GLSHADER_UNIFORM_GLM_N_(N, GLM_TYPE, SUFFIX) \
14
+    GLSHADER_UNIFORM( \
15
+        glm::GLM_TYPE##N, \
16
+        glUniform##N##SUFFIX##v( \
17
+            location, 1, glm::value_ptr(value) \
18
+        ); \
19
+    )
20
+
21
+#define GLSHADER_UNIFORM_GLM_N_BOOL_(N) \
22
+    GLSHADER_UNIFORM( \
23
+        glm::bvec##N, \
24
+        GLint int_value[N]; \
25
+        for (auto i = 0; i < N; ++i) \
26
+            int_value[i] = value[i]; \
27
+        glUniform##N##iv( \
28
+            location, 1, &int_value[0] \
29
+        ); \
30
+    )
31
+
32
+#define GLSHADER_UNIFORM_GLM_MATRIX_N_(N) \
33
+    GLSHADER_UNIFORM( \
34
+        glm::mat##N, \
35
+        glUniformMatrix##N##fv( \
36
+            location, 1, GL_FALSE, glm::value_ptr(value) \
37
+        ); \
38
+    )
39
+
40
+#define GLSHADER_UNIFORM_GLM_MATRIX_N_M_(N, M) \
41
+    GLSHADER_UNIFORM( \
42
+        glm::mat##N##x##M, \
43
+        glUniformMatrix##N##x##M##fv( \
44
+            location, 1, GL_FALSE, glm::value_ptr(value) \
45
+        ); \
46
+    )
47
+
48
+GLSHADER_UNIFORM_GLM_N_BOOL_(2)
49
+GLSHADER_UNIFORM_GLM_N_BOOL_(3)
50
+GLSHADER_UNIFORM_GLM_N_BOOL_(4)
51
+GLSHADER_UNIFORM_GLM_N_(2, ivec, i)
52
+GLSHADER_UNIFORM_GLM_N_(3, ivec, i)
53
+GLSHADER_UNIFORM_GLM_N_(4, ivec, i)
54
+GLSHADER_UNIFORM_GLM_N_(2, uvec, ui)
55
+GLSHADER_UNIFORM_GLM_N_(3, uvec, ui)
56
+GLSHADER_UNIFORM_GLM_N_(4, uvec, ui)
57
+GLSHADER_UNIFORM_GLM_N_(2, vec, f)
58
+GLSHADER_UNIFORM_GLM_N_(3, vec, f)
59
+GLSHADER_UNIFORM_GLM_N_(4, vec, f)
60
+
61
+GLSHADER_UNIFORM_GLM_MATRIX_N_(2)
62
+GLSHADER_UNIFORM_GLM_MATRIX_N_(3)
63
+GLSHADER_UNIFORM_GLM_MATRIX_N_(4)
64
+
65
+GLSHADER_UNIFORM_GLM_MATRIX_N_M_(2, 3)
66
+GLSHADER_UNIFORM_GLM_MATRIX_N_M_(2, 4)
67
+GLSHADER_UNIFORM_GLM_MATRIX_N_M_(3, 2)
68
+GLSHADER_UNIFORM_GLM_MATRIX_N_M_(3, 4)
69
+GLSHADER_UNIFORM_GLM_MATRIX_N_M_(4, 2)
70
+GLSHADER_UNIFORM_GLM_MATRIX_N_M_(4, 3)
71
+
72
+
73
+#endif // GLSHADER_GLM_HPP_
... ...
@@ -1,8 +1,10 @@
1 1
 #include <string>
2 2
 
3 3
 #include <GL/glew.h>
4
+#include <glm/glm.hpp>
4 5
 
5 6
 #include <glshader.hpp>
7
+#include <glshader_glm.hpp>
6 8
 #include <gltest.hpp>
7 9
 
8 10
 
... ...
@@ -161,6 +163,9 @@ GLTEST(2, 0, 640, 480, glshader)
161 163
         "got error GL_INVALID_OPERATION (wrong type?)."
162 164
     )
163 165
 
166
+    // Uniform OpenGL Mathematics (GLM).
167
+    glm::vec4 color{1.0F};
168
+
164 169
     auto all = Shader({
165 170
         "tests/all.vert",
166 171
         "tests/all.frag",
... ...
@@ -169,6 +174,7 @@ GLTEST(2, 0, 640, 480, glshader)
169 174
     all
170 175
         .use()
171 176
         .uniform("blue", blue)
177
+        .uniform("color", color)
172 178
         .validate();
173 179
 
174 180
     constexpr auto size = 0.5F;