commit f04d9afd4cfb06c3b36cec660f824233d08b9be5
parent ecf0aa26737931e8bf87f0a69f1748089a3855d3
Author: rexim <reximkut@gmail.com>
Date: Sun, 18 Jul 2021 21:14:36 +0700
Introduce uniform registery
Diffstat:
13 files changed, 111 insertions(+), 154 deletions(-)
diff --git a/build.sh b/build.sh
@@ -6,7 +6,7 @@ CC="${CXX:-cc}"
PKGS="sdl2 glew freetype2"
CFLAGS="-Wall -Wextra -std=c11 -pedantic -ggdb"
LIBS=-lm
-SRC="src/main.c src/la.c src/editor.c src/sdl_extra.c src/file.c src/gl_extra.c src/free_glyph.c src/cursor_renderer.c"
+SRC="src/main.c src/la.c src/editor.c src/sdl_extra.c src/file.c src/gl_extra.c src/free_glyph.c src/cursor_renderer.c src/uniforms.c"
if [ `uname` = "Darwin" ]; then
CFLAGS+=" -framework OpenGL"
diff --git a/shaders/camera.vert b/shaders/camera.vert
@@ -1,10 +1,8 @@
uniform vec2 resolution;
-uniform vec2 camera;
-uniform float time;
-// uniform float camera_scale;
+uniform float camera_scale;
+uniform vec2 camera_pos;
vec2 camera_project(vec2 point)
{
- float camera_scale = 1.0 + (sin(time) + 1.0) / 2.0;
- return 2.0 * (point - camera) * camera_scale / resolution;
+ return 2.0 * (point - camera_pos) * camera_scale / resolution;
}
diff --git a/shaders/cursor.vert b/shaders/cursor.vert
@@ -1,9 +1,8 @@
#version 330 core
uniform vec2 resolution;
-uniform vec2 camera;
-uniform vec2 pos;
-uniform float height;
+uniform vec2 cursor_pos;
+uniform float cursor_height;
#define WIDTH 5.0
@@ -15,7 +14,7 @@ void main() {
uv = vec2(float(gl_VertexID & 1),
float((gl_VertexID >> 1) & 1));
gl_Position = vec4(
- camera_project(uv * vec2(WIDTH, height) + pos),
+ camera_project(uv * vec2(WIDTH, cursor_height) + cursor_pos),
0.0,
1.0);
}
diff --git a/shaders/free_glyph.vert b/shaders/free_glyph.vert
@@ -1,9 +1,7 @@
#version 330 core
uniform vec2 resolution;
-uniform vec2 camera;
uniform float time;
-// uniform float camera_scale;
layout(location = 0) in vec2 pos;
layout(location = 1) in vec2 size;
diff --git a/shaders/tile_glyph.frag b/shaders/tile_glyph.frag
@@ -1,51 +0,0 @@
-#version 330 core
-
-#define FONT_WIDTH 128
-#define FONT_HEIGHT 64
-#define FONT_COLS 18
-#define FONT_ROWS 7
-#define FONT_CHAR_WIDTH (FONT_WIDTH / FONT_COLS)
-#define FONT_CHAR_HEIGHT (FONT_HEIGHT / FONT_ROWS)
-#define FONT_CHAR_WIDTH_UV (float(FONT_CHAR_WIDTH) / float(FONT_WIDTH))
-#define FONT_CHAR_HEIGHT_UV (float(FONT_CHAR_HEIGHT) / float(FONT_HEIGHT))
-
-#define ASCII_DISPLAY_LOW 32
-#define ASCII_DISPLAY_HIGH 126
-
-uniform sampler2D font;
-uniform float time;
-uniform vec2 resolution;
-
-in vec2 uv;
-flat in int glyph_ch;
-in vec4 glyph_fg_color;
-in vec4 glyph_bg_color;
-
-float map01(float x)
-{
- return (x + 1) / 2.0;
-}
-
-vec3 hsl2rgb(vec3 c) {
- vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0);
- return c.z + c.y * (rgb-0.5)*(1.0-abs(2.0*c.z-1.0));
-}
-
-void main() {
- int ch = glyph_ch;
- if (!(ASCII_DISPLAY_LOW <= ch && ch <= ASCII_DISPLAY_HIGH)) {
- ch = 63;
- }
-
- int index = ch - 32;
- float x = float(index % FONT_COLS) * FONT_CHAR_WIDTH_UV;
- float y = float(index / FONT_COLS) * FONT_CHAR_HEIGHT_UV;
- vec2 pos = vec2(x, y + FONT_CHAR_HEIGHT_UV);
- vec2 size = vec2(FONT_CHAR_WIDTH_UV, -FONT_CHAR_HEIGHT_UV);
- vec2 t = pos + size * uv;
-
- vec4 tc = texture(font, t);
- vec2 frag_uv = gl_FragCoord.xy / resolution;
- vec4 rainbow = vec4(hsl2rgb(vec3((time + frag_uv.x + frag_uv.y), 0.5, 0.5)), 1.0);
- gl_FragColor = glyph_bg_color * (1.0 - tc.x) + tc.x * glyph_fg_color * rainbow;
-}
diff --git a/shaders/tile_glyph.vert b/shaders/tile_glyph.vert
@@ -1,37 +0,0 @@
-#version 330 core
-
-#define FONT_WIDTH 128
-#define FONT_HEIGHT 64
-#define FONT_COLS 18
-#define FONT_ROWS 7
-#define FONT_CHAR_WIDTH (FONT_WIDTH / FONT_COLS)
-#define FONT_CHAR_HEIGHT (FONT_HEIGHT / FONT_ROWS)
-
-uniform vec2 resolution;
-uniform vec2 scale;
-uniform float time;
-uniform vec2 camera;
-
-layout(location = 0) in ivec2 tile;
-layout(location = 1) in int ch;
-layout(location = 2) in vec4 fg_color;
-layout(location = 3) in vec4 bg_color;
-
-out vec2 uv;
-flat out int glyph_ch;
-out vec4 glyph_fg_color;
-out vec4 glyph_bg_color;
-
-vec2 camera_project(vec2 point);
-
-void main() {
- uv = vec2(float(gl_VertexID & 1), float((gl_VertexID >> 1) & 1));
- vec2 char_size = vec2(float(FONT_CHAR_WIDTH), float(FONT_CHAR_HEIGHT));
- vec2 pos = tile * char_size * scale;
-
- gl_Position = vec4(camera_project(uv * char_size * scale + pos), 0.0, 1.0);
- glyph_ch = ch;
-
- glyph_fg_color = fg_color;
- glyph_bg_color = bg_color;
-}
diff --git a/src/cursor_renderer.c b/src/cursor_renderer.c
@@ -27,13 +27,7 @@ void cursor_renderer_init(Cursor_Renderer *cr,
}
glUseProgram(cr->program);
-
- cr->time_uniform = glGetUniformLocation(cr->program, "time");
- cr->resolution_uniform = glGetUniformLocation(cr->program, "resolution");
- cr->camera_uniform = glGetUniformLocation(cr->program, "camera");
- cr->pos_uniform = glGetUniformLocation(cr->program, "pos");
- cr->height_uniform = glGetUniformLocation(cr->program, "height");
- cr->last_stroke_uniform = glGetUniformLocation(cr->program, "last_stroke");
+ get_uniform_location(cr->program, cr->uniforms);
}
}
@@ -44,7 +38,7 @@ void cursor_renderer_use(const Cursor_Renderer *cr)
void cursor_renderer_move_to(const Cursor_Renderer *cr, Vec2f pos)
{
- glUniform2f(cr->pos_uniform, pos.x, pos.y);
+ glUniform2f(cr->uniforms[UNIFORM_SLOT_CURSOR_POS], pos.x, pos.y);
}
void cursor_renderer_draw()
diff --git a/src/cursor_renderer.h b/src/cursor_renderer.h
@@ -8,16 +8,12 @@
#include <SDL2/SDL_opengl.h>
#include "./la.h"
+#include "./uniforms.h"
typedef struct {
GLuint program;
- GLint time_uniform;
- GLint resolution_uniform;
- GLint camera_uniform;
- GLint pos_uniform;
- GLint height_uniform;
- GLint last_stroke_uniform;
+ GLint uniforms[COUNT_UNIFORM_SLOTS];
} Cursor_Renderer;
void cursor_renderer_init(Cursor_Renderer *cr,
diff --git a/src/free_glyph.c b/src/free_glyph.c
@@ -112,10 +112,7 @@ void free_glyph_buffer_init(Free_Glyph_Buffer *fgb,
}
glUseProgram(fgb->program);
-
- fgb->time_uniform = glGetUniformLocation(fgb->program, "time");
- fgb->resolution_uniform = glGetUniformLocation(fgb->program, "resolution");
- fgb->camera_uniform = glGetUniformLocation(fgb->program, "camera");
+ get_uniform_location(fgb->program, fgb->uniforms);
}
// Glyph Texture Atlas
diff --git a/src/free_glyph.h b/src/free_glyph.h
@@ -13,6 +13,8 @@
#include <ft2build.h>
#include FT_FREETYPE_H
+#include "./uniforms.h"
+
// https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_02
typedef struct {
@@ -59,9 +61,7 @@ typedef struct {
GLuint glyphs_texture;
- GLint time_uniform;
- GLint resolution_uniform;
- GLint camera_uniform;
+ GLint uniforms[COUNT_UNIFORM_SLOTS];
size_t glyphs_count;
Free_Glyph glyphs[FREE_GLYPH_BUFFER_CAP];
diff --git a/src/main.c b/src/main.c
@@ -82,9 +82,10 @@ void render_editor_into_fgb(SDL_Window *window, Free_Glyph_Buffer *fgb, Cursor_R
free_glyph_buffer_use(fgb);
{
- glUniform2f(fgb->resolution_uniform, (float) w, (float) h);
- glUniform1f(fgb->time_uniform, (float) SDL_GetTicks() / 1000.0f);
- glUniform2f(fgb->camera_uniform, camera_pos.x, camera_pos.y);
+ glUniform2f(fgb->uniforms[UNIFORM_SLOT_RESOLUTION], (float) w, (float) h);
+ glUniform1f(fgb->uniforms[UNIFORM_SLOT_TIME], (float) SDL_GetTicks() / 1000.0f);
+ glUniform2f(fgb->uniforms[UNIFORM_SLOT_CAMERA_POS], camera_pos.x, camera_pos.y);
+ glUniform1f(fgb->uniforms[UNIFORM_SLOT_CAMERA_SCALE], 1.0f);
free_glyph_buffer_clear(fgb);
@@ -114,10 +115,11 @@ void render_editor_into_fgb(SDL_Window *window, Free_Glyph_Buffer *fgb, Cursor_R
cursor_renderer_use(cr);
{
- glUniform2f(cr->resolution_uniform, (float) w, (float) h);
- glUniform1f(cr->time_uniform, (float) SDL_GetTicks() / 1000.0f);
- glUniform2f(cr->camera_uniform, camera_pos.x, camera_pos.y);
- glUniform1f(cr->height_uniform, FREE_GLYPH_FONT_SIZE);
+ glUniform2f(cr->uniforms[UNIFORM_SLOT_RESOLUTION], (float) w, (float) h);
+ glUniform1f(cr->uniforms[UNIFORM_SLOT_TIME], (float) SDL_GetTicks() / 1000.0f);
+ glUniform2f(cr->uniforms[UNIFORM_SLOT_CAMERA_POS], camera_pos.x, camera_pos.y);
+ glUniform1f(cr->uniforms[UNIFORM_SLOT_CAMERA_SCALE], 1.0f);
+ glUniform1f(cr->uniforms[UNIFORM_SLOT_CURSOR_HEIGHT], FREE_GLYPH_FONT_SIZE);
cursor_renderer_move_to(cr, cursor_pos);
cursor_renderer_draw();
@@ -142,8 +144,7 @@ int main(int argc, char **argv)
exit(1);
}
- // const char *const font_file_path = "./VictorMono-Regular.ttf";
- const char *const font_file_path = "./ComicNeue-Regular.otf";
+ const char *const font_file_path = "./VictorMono-Regular.ttf";
FT_Face face;
error = FT_New_Face(library, font_file_path, 0, &face);
@@ -249,10 +250,8 @@ int main(int argc, char **argv)
switch (event.key.keysym.sym) {
case SDLK_BACKSPACE: {
editor_backspace(&editor);
-#ifndef TILE_GLYPH_RENDER
cursor_renderer_use(&cr);
- glUniform1f(cr.last_stroke_uniform, (float) SDL_GetTicks() / 1000.0f);
-#endif
+ glUniform1f(cr.uniforms[UNIFORM_SLOT_LAST_STROKE], (float) SDL_GetTicks() / 1000.0f);
}
break;
@@ -265,19 +264,15 @@ int main(int argc, char **argv)
case SDLK_RETURN: {
editor_insert_new_line(&editor);
-#ifndef TILE_GLYPH_RENDER
cursor_renderer_use(&cr);
- glUniform1f(cr.last_stroke_uniform, (float) SDL_GetTicks() / 1000.0f);
-#endif
+ glUniform1f(cr.uniforms[UNIFORM_SLOT_LAST_STROKE], (float) SDL_GetTicks() / 1000.0f);
}
break;
case SDLK_DELETE: {
editor_delete(&editor);
-#ifndef TILE_GLYPH_RENDER
cursor_renderer_use(&cr);
- glUniform1f(cr.last_stroke_uniform, (float) SDL_GetTicks() / 1000.0f);
-#endif
+ glUniform1f(cr.uniforms[UNIFORM_SLOT_LAST_STROKE], (float) SDL_GetTicks() / 1000.0f);
}
break;
@@ -285,39 +280,31 @@ int main(int argc, char **argv)
if (editor.cursor_row > 0) {
editor.cursor_row -= 1;
}
-#ifndef TILE_GLYPH_RENDER
cursor_renderer_use(&cr);
- glUniform1f(cr.last_stroke_uniform, (float) SDL_GetTicks() / 1000.0f);
-#endif
+ glUniform1f(cr.uniforms[UNIFORM_SLOT_LAST_STROKE], (float) SDL_GetTicks() / 1000.0f);
}
break;
case SDLK_DOWN: {
editor.cursor_row += 1;
-#ifndef TILE_GLYPH_RENDER
cursor_renderer_use(&cr);
- glUniform1f(cr.last_stroke_uniform, (float) SDL_GetTicks() / 1000.0f);
-#endif
+ glUniform1f(cr.uniforms[UNIFORM_SLOT_LAST_STROKE], (float) SDL_GetTicks() / 1000.0f);
}
break;
case SDLK_LEFT: {
if (editor.cursor_col > 0) {
editor.cursor_col -= 1;
-#ifndef TILE_GLYPH_RENDER
cursor_renderer_use(&cr);
- glUniform1f(cr.last_stroke_uniform, (float) SDL_GetTicks() / 1000.0f);
-#endif
+ glUniform1f(cr.uniforms[UNIFORM_SLOT_LAST_STROKE], (float) SDL_GetTicks() / 1000.0f);
}
}
break;
case SDLK_RIGHT: {
editor.cursor_col += 1;
-#ifndef TILE_GLYPH_RENDER
cursor_renderer_use(&cr);
- glUniform1f(cr.last_stroke_uniform, (float) SDL_GetTicks() / 1000.0f);
-#endif
+ glUniform1f(cr.uniforms[UNIFORM_SLOT_LAST_STROKE], (float) SDL_GetTicks() / 1000.0f);
}
break;
@@ -327,10 +314,8 @@ int main(int argc, char **argv)
case SDL_TEXTINPUT: {
editor_insert_text_before_cursor(&editor, event.text.text);
-#ifndef TILE_GLYPH_RENDER
cursor_renderer_use(&cr);
- glUniform1f(cr.last_stroke_uniform, (float) SDL_GetTicks() / 1000.0f);
-#endif
+ glUniform1f(cr.uniforms[UNIFORM_SLOT_LAST_STROKE], (float) SDL_GetTicks() / 1000.0f);
}
break;
}
diff --git a/src/uniforms.c b/src/uniforms.c
@@ -0,0 +1,48 @@
+#include <assert.h>
+#include "./uniforms.h"
+
+static_assert(COUNT_UNIFORM_SLOTS == 7, "The amount of the shader uniforms have change. Please update the definition table accordingly");
+static const Uniform_Def uniform_defs[COUNT_UNIFORM_SLOTS] = {
+ [UNIFORM_SLOT_TIME] = {
+ .slot = UNIFORM_SLOT_TIME,
+ .name = "time",
+ },
+ [UNIFORM_SLOT_RESOLUTION] = {
+ .slot = UNIFORM_SLOT_RESOLUTION,
+ .name = "resolution",
+ },
+ [UNIFORM_SLOT_CAMERA_POS] = {
+ .slot = UNIFORM_SLOT_CAMERA_POS,
+ .name = "camera_pos",
+ },
+ [UNIFORM_SLOT_CAMERA_SCALE] = {
+ .slot = UNIFORM_SLOT_CAMERA_SCALE,
+ .name = "camera_scale",
+ },
+ [UNIFORM_SLOT_CURSOR_POS] = {
+ .slot = UNIFORM_SLOT_CURSOR_POS,
+ .name = "cursor_pos",
+ },
+ [UNIFORM_SLOT_CURSOR_HEIGHT] = {
+ .slot = UNIFORM_SLOT_CURSOR_HEIGHT,
+ .name = "cursor_height",
+ },
+ [UNIFORM_SLOT_LAST_STROKE] = {
+ .slot = UNIFORM_SLOT_LAST_STROKE,
+ .name = "last_stroke",
+ },
+};
+
+const Uniform_Def *get_uniform_def(Uniform_Slot slot)
+{
+ assert(0 <= slot);
+ assert(slot < COUNT_UNIFORM_SLOTS);
+ return &uniform_defs[slot];
+}
+
+void get_uniform_location(GLuint program, GLint locations[COUNT_UNIFORM_SLOTS])
+{
+ for (Uniform_Slot slot = 0; slot < COUNT_UNIFORM_SLOTS; ++slot) {
+ locations[slot] = glGetUniformLocation(program, uniform_defs[slot].name);
+ }
+}
diff --git a/src/uniforms.h b/src/uniforms.h
@@ -0,0 +1,30 @@
+#ifndef UNIFORMS_H_
+#define UNIFORMS_H_
+
+#define GLEW_STATIC
+#include <GL/glew.h>
+
+#define GL_GLEXT_PROTOTYPES
+#include <SDL2/SDL_opengl.h>
+
+typedef enum {
+ UNIFORM_SLOT_TIME = 0,
+ UNIFORM_SLOT_RESOLUTION,
+ UNIFORM_SLOT_CAMERA_POS,
+ UNIFORM_SLOT_CAMERA_SCALE,
+
+ UNIFORM_SLOT_CURSOR_POS,
+ UNIFORM_SLOT_CURSOR_HEIGHT,
+ UNIFORM_SLOT_LAST_STROKE,
+ COUNT_UNIFORM_SLOTS,
+} Uniform_Slot;
+
+typedef struct {
+ Uniform_Slot slot;
+ const char *name;
+} Uniform_Def;
+
+const Uniform_Def *get_uniform_def(Uniform_Slot slot);
+void get_uniform_location(GLuint program, GLint locations[COUNT_UNIFORM_SLOTS]);
+
+#endif // UNIFORMS_H_