Browse code

Add implementation

Robert Cranston authored on 22/12/2021 14:14:41
Showing 4 changed files

... ...
@@ -2,11 +2,49 @@
2 2
 
3 3
 A [GLSL][]/[OpenGL][] [\>=3.2][] [normal][] visualization library.
4 4
 
5
+A [geometry shader][] is used to visualize the normals from the same geometry
6
+as when drawing normally.
7
+
5 8
 [`glvisnormals`]: https://git.rcrnstn.net/rcrnstn/glvisnormals
6 9
 [GLSL]: https://en.wikipedia.org/wiki/OpenGL_Shading_Language
7 10
 [OpenGL]: https://en.wikipedia.org/wiki/OpenGL
8 11
 [\>=3.2]: https://en.wikipedia.org/wiki/OpenGL#Version_history
9 12
 [normal]: https://en.wikipedia.org/wiki/Normal_(geometry)
13
+[geometry shader]: https://www.khronos.org/opengl/wiki/Geometry_Shader
14
+
15
+## Requirements
16
+
17
+[Geometry shader][]s require OpenGL [\>=3.2][] or the
18
+[`ARB_geometry_shader4`][] extension.
19
+
20
+[`ARB_geometry_shader4`]: https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_geometry_shader4.txt
21
+
22
+## Usage
23
+
24
+Link the shader program with:
25
+
26
+-   `visnormals.vert`
27
+-   `visnormals.geom`
28
+-   `visnormals.frag`
29
+
30
+The following vertex attributes need to be set:
31
+
32
+-   `vec3 vert_position`
33
+-   `vec3 vert_normal` (assumed to be of unit length)
34
+-   `mat4 vert_model` (assumed to be an isometry, i.e. no scaling or skewing)
35
+
36
+The following uniforms need to be set:
37
+
38
+-   `mat4  view_projection`
39
+-   `float scale`
40
+-   `vec4  color`
41
+
42
+The following outputs are written by the fragment shader:
43
+
44
+-   ` vec4 frag_color`
45
+
46
+Make the shader program current and issue the same draw call as would be done
47
+when drawing normally.
10 48
 
11 49
 ## Build system
12 50
 
13 51
new file mode 100644
... ...
@@ -0,0 +1,13 @@
1
+#version 150
2
+
3
+
4
+uniform vec4 color;
5
+
6
+
7
+out vec4 frag_color;
8
+
9
+
10
+void main()
11
+{
12
+    frag_color = color;
13
+}
0 14
new file mode 100644
... ...
@@ -0,0 +1,32 @@
1
+#version 150
2
+
3
+
4
+layout(triangles) in;
5
+layout(line_strip, max_vertices = 6) out;
6
+
7
+
8
+uniform mat4  view_projection;
9
+uniform float scale;
10
+
11
+
12
+in vec4 geom_normal[3];
13
+
14
+
15
+void primitive_(int i)
16
+{
17
+    gl_Position = view_projection *
18
+        (gl_in[i].gl_Position);
19
+    EmitVertex();
20
+    gl_Position = view_projection *
21
+        (gl_in[i].gl_Position + scale * geom_normal[i]);
22
+    EmitVertex();
23
+    EndPrimitive();
24
+}
25
+
26
+
27
+void main()
28
+{
29
+    primitive_(0);
30
+    primitive_(1);
31
+    primitive_(2);
32
+}
0 33
new file mode 100644
... ...
@@ -0,0 +1,16 @@
1
+#version 150
2
+
3
+
4
+in vec3 vert_position;
5
+in vec3 vert_normal;
6
+in mat4 vert_model;
7
+
8
+
9
+out vec4 geom_normal;
10
+
11
+
12
+void main()
13
+{
14
+    gl_Position = vert_model * vec4(vert_position, 1);
15
+    geom_normal = vert_model * vec4(vert_normal,   0);
16
+}