Browse code

Add OpenGL boilerplate

Robert Cranston authored on 08/03/2026 00:46:06
Showing 4 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1 @@
1
+/sdf-winding
0 2
new file mode 100644
... ...
@@ -0,0 +1,6 @@
1
+CXXFLAGS += -std=c++11
2
+CXXFLAGS += -Wall -Wextra -Wpedantic
3
+CXXFLAGS += -Og -g -fsanitize=undefined,address
4
+LDLIBS   += -lGL -lGLEW -lglfw
5
+
6
+sdf-winding:
... ...
@@ -10,12 +10,20 @@ the inside from the outside of a glyph][TrueType]:
10 10
 
11 11
 I have not read a single word of that reference manual, proceed accordingly.
12 12
 
13
+To build, run `make`. You need the following dependencies on your system:
14
+
15
+-   OpenGL
16
+-   [GLEW][]
17
+-   [GLFW][]
18
+
13 19
 [`sdf-winding`]:  https://git.rcrnstn.net/rcrnstn/sdf-winding
14 20
 [glyph]:          https://en.wikipedia.org/wiki/Glyph
15 21
 [winding number]: https://en.wikipedia.org/wiki/Winding_number
16 22
 [SDF]:            https://en.wikipedia.org/wiki/Signed_distance_function
17 23
 [Figure 7]:       https://developer.apple.com/fonts/TrueType-Reference-Manual/RM02/FE6.gif
18 24
 [TrueType]:       https://developer.apple.com/fonts/TrueType-Reference-Manual/RM02/Chap2.html#distinguishing
25
+[GLEW]:           https://glew.sourceforge.net
26
+[GLFW]:           https://www.glfw.org
19 27
 
20 28
 ## License
21 29
 
22 30
new file mode 100644
... ...
@@ -0,0 +1,96 @@
1
+#include <iostream>
2
+
3
+#include <GL/glew.h>
4
+#include <GLFW/glfw3.h>
5
+
6
+constexpr auto width  = 640;
7
+constexpr auto height = 480;
8
+constexpr auto title  = "sdf-winding";
9
+
10
+static void APIENTRY debug_message_callback(
11
+    GLenum          /* source   */,
12
+    GLenum          /* type     */,
13
+    GLuint          /* id       */,
14
+    GLenum          /* severity */,
15
+    GLsizei         /* length   */,
16
+    GLchar  const *    message,
17
+    void    const * /* param    */
18
+)
19
+{
20
+    std::cerr << message << std::endl;
21
+};
22
+
23
+int main()
24
+{
25
+    /// Window/context
26
+    glfwInit();
27
+    auto window = glfwCreateWindow(width, height, title, nullptr, nullptr);
28
+    glfwMakeContextCurrent(window);
29
+    glewInit();
30
+
31
+    /// Debug
32
+    glEnable(GL_DEBUG_OUTPUT);
33
+    glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
34
+    glDebugMessageCallback(debug_message_callback, nullptr);
35
+
36
+    /// Shader
37
+    auto make_program = [](char const * fragment_source)
38
+    {
39
+        auto program = glCreateProgram();
40
+        auto attach  = [&](GLenum type, char const * source)
41
+        {
42
+            auto shader = glCreateShader(type);
43
+            glShaderSource(shader, 1, &source, nullptr);
44
+            glCompileShader(shader);
45
+            glAttachShader(program, shader);
46
+        };
47
+        attach(GL_VERTEX_SHADER, R"(
48
+            #version 140
49
+
50
+            out vec2 tex_coord;
51
+
52
+            void main()
53
+            {
54
+                tex_coord = vec2[](
55
+                    vec2(0, 0),
56
+                    vec2(2, 0),
57
+                    vec2(0, 2)
58
+                )[gl_VertexID];
59
+                gl_Position = vec4(tex_coord*2-1, 0, 1);
60
+            }
61
+        )");
62
+        attach(GL_FRAGMENT_SHADER, fragment_source);
63
+        glLinkProgram(program);
64
+        return program;
65
+    };
66
+    auto tex_coord_program = make_program(R"(
67
+        #version 140
68
+
69
+        in vec2 tex_coord;
70
+
71
+        void main()
72
+        {
73
+            gl_FragColor = vec4(tex_coord, 0, 1);
74
+        }
75
+    )");
76
+
77
+    /// Draw loop
78
+    while (glfwPollEvents(), !glfwWindowShouldClose(window))
79
+    {
80
+        //// Input
81
+        if (glfwGetKey(window, GLFW_KEY_Q))
82
+            glfwSetWindowShouldClose(window, GLFW_TRUE);
83
+
84
+        //// Draw
85
+        glClear(GL_COLOR_BUFFER_BIT);
86
+        glUseProgram(tex_coord_program);
87
+        glDrawArrays(GL_TRIANGLES, 0, 3);
88
+
89
+        //// Swap
90
+        glfwSwapBuffers(window);
91
+    }
92
+
93
+    /// Cleanup
94
+    glfwDestroyWindow(window);
95
+    glfwTerminate();
96
+}