From 4510543aaf1fec7f448f6d52c148f20d6b1422e5 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sun, 26 Feb 2023 14:03:21 +0000 Subject: [PATCH] Text outlining; circle shader radius now in terms of pixels --- source/engine/debug/debugdraw.c | 1 + source/engine/font.c | 59 +++++++++++++++++++++++++++------ source/shaders/circlefrag.glsl | 14 ++------ source/shaders/circlevert.glsl | 2 +- source/shaders/textfrag.glsl | 9 +++-- 5 files changed, 57 insertions(+), 28 deletions(-) diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index 1a678c7..10861f7 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -207,6 +207,7 @@ void draw_circle(int x, int y, float radius, int pixels, float *color, int fill) shader_setint(circleShader, "thickness", pixels); shader_setvec3(circleShader, "dbgColor", color); shader_setbool(circleShader, "fill", fill); + shader_setfloat(circleShader, "zoom", cam_zoom()); glBindVertexArray(circleVAO); glEnableVertexAttribArray(0); diff --git a/source/engine/font.c b/source/engine/font.c index a3640d7..7f24268 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -164,29 +164,66 @@ struct sFont *MakeFont(const char *fontfile, int height) static int curchar = 0; static float *buffdraw; -void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct shader *shader, float color[3]) +void fill_charverts(float *verts, float cursor[2], float scale, struct Character c, float *offset) { - float w = c.Size[0] * scale; - float h = c.Size[1] * scale; + float w = c.Size[0] * scale; + float h = c.Size[1] * scale; - float xpos = cursor[0] + c.Bearing[0] * scale; - float ypos = cursor[1] - c.Bearing[1] * scale; - - float verts[16] = { + float xpos = cursor[0] + (c.Bearing[0]+offset[0]) * scale; + float ypos = cursor[1] - (c.Bearing[1]+offset[1]) * scale; + + float v[16] = { xpos, ypos, c.rect.s0, c.rect.t1, xpos+w, ypos, c.rect.s1, c.rect.t1, xpos, ypos + h, c.rect.s0, c.rect.t0, xpos + w, ypos + h, c.rect.s1, c.rect.t0 }; - /* Check if the vertex is off screen */ + memcpy(verts, v, sizeof(float)*16); +} + +void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct shader *shader, float color[3]) +{ + float shadowcolor[3] = {0.f, 0.f, 0.f}; + float shadowcursor[2]; + + float verts[16]; + float offset[2] = {-1, 1}; + + fill_charverts(verts, cursor, scale, c, offset); + + /* Check if the vertex is off screen */ if (verts[5] < 0 || verts[10] < 0 || verts[0] > window_i(0)->width || verts[1] > window_i(0)->height) return; -// glBufferSubData(GL_ARRAY_BUFFER, curchar*sizeof(verts), sizeof(verts), verts); - + shader_setvec3(shader, "textColor", shadowcolor); glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + offset[0] = 1; + offset[1] = -1; + fill_charverts(verts, cursor, scale, c, offset); + glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + offset[1] = 1; + fill_charverts(verts, cursor, scale, c, offset); + glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + offset[0] = -1; + offset[1] = -1; + fill_charverts(verts, cursor, scale, c, offset); + glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + + offset[0] = offset[1] = 0; + fill_charverts(verts, cursor, scale, c, offset); + shader_setvec3(shader, "textColor", color); + glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + curchar++; } @@ -210,7 +247,7 @@ void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3 glBindTexture(GL_TEXTURE_2D, font->texID); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, len*16*sizeof(float), NULL, GL_STREAM_DRAW); + glBufferData(GL_ARRAY_BUFFER, len*16*sizeof(float)*2, NULL, GL_STREAM_DRAW); /* x2 on the size for the outline pass */ const unsigned char *line, *wordstart; line = (unsigned char*)text; diff --git a/source/shaders/circlefrag.glsl b/source/shaders/circlefrag.glsl index 05fc7d6..05935d9 100644 --- a/source/shaders/circlefrag.glsl +++ b/source/shaders/circlefrag.glsl @@ -7,13 +7,14 @@ uniform float radius; uniform int thickness; uniform vec3 dbgColor; uniform bool fill; +uniform float zoom; void main() { // int tt = thickness + 1; float R1 = 1.f; - float R2 = 1.f - (thickness / radius); + float R2 = 1.f - (thickness*zoom / radius); float dist = sqrt(dot(coords, coords)); if (dist >= R2 && dist <= R1) @@ -22,13 +23,4 @@ void main() color = vec4(dbgColor, 0.1f); else discard; - - -/* - float smoother = 0.01f - (radius * 0.00003f); - float sm = smoothstep(R1, R1-smoother, dist); - float sm2 = smoothstep(R2, R2+smoother, dist); - float alpha = sm*sm2; -*/ - -} \ No newline at end of file +} diff --git a/source/shaders/circlevert.glsl b/source/shaders/circlevert.glsl index 602dc54..986f942 100644 --- a/source/shaders/circlevert.glsl +++ b/source/shaders/circlevert.glsl @@ -15,5 +15,5 @@ layout (std140) uniform Resolution void main() { gl_Position = projection * vec4(vertex.xy, 0.0, 1.0); - coords = vertex.zw*resolution; + coords = vertex.zw;; } diff --git a/source/shaders/textfrag.glsl b/source/shaders/textfrag.glsl index c7e35c0..3519e1f 100644 --- a/source/shaders/textfrag.glsl +++ b/source/shaders/textfrag.glsl @@ -7,9 +7,8 @@ uniform vec3 textColor; void main() { - //vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); - //color = vec4(textColor, 1.0) * sampled; - color = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); -/* if (color.a <= 0.1f) - discard;*/ + vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); + color = vec4(textColor, 1.0) * sampled; + if (color.a <= 0.1f) + discard; }