Text outlining; circle shader radius now in terms of pixels

This commit is contained in:
John Alanbrook 2023-02-26 14:03:21 +00:00
parent b2b54ddce9
commit 4510543aaf
5 changed files with 57 additions and 28 deletions

View file

@ -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_setint(circleShader, "thickness", pixels);
shader_setvec3(circleShader, "dbgColor", color); shader_setvec3(circleShader, "dbgColor", color);
shader_setbool(circleShader, "fill", fill); shader_setbool(circleShader, "fill", fill);
shader_setfloat(circleShader, "zoom", cam_zoom());
glBindVertexArray(circleVAO); glBindVertexArray(circleVAO);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);

View file

@ -164,29 +164,66 @@ struct sFont *MakeFont(const char *fontfile, int height)
static int curchar = 0; static int curchar = 0;
static float *buffdraw; 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 w = c.Size[0] * scale;
float h = c.Size[1] * scale; float h = c.Size[1] * scale;
float xpos = cursor[0] + c.Bearing[0] * scale; float xpos = cursor[0] + (c.Bearing[0]+offset[0]) * scale;
float ypos = cursor[1] - c.Bearing[1] * scale; float ypos = cursor[1] - (c.Bearing[1]+offset[1]) * scale;
float verts[16] = { float v[16] = {
xpos, ypos, c.rect.s0, c.rect.t1, xpos, ypos, c.rect.s0, c.rect.t1,
xpos+w, ypos, c.rect.s1, c.rect.t1, xpos+w, ypos, c.rect.s1, c.rect.t1,
xpos, ypos + h, c.rect.s0, c.rect.t0, xpos, ypos + h, c.rect.s0, c.rect.t0,
xpos + w, ypos + h, c.rect.s1, c.rect.t0 xpos + w, ypos + h, c.rect.s1, c.rect.t0
}; };
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 */ /* 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) if (verts[5] < 0 || verts[10] < 0 || verts[0] > window_i(0)->width || verts[1] > window_i(0)->height)
return; 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); glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 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++; 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); glBindTexture(GL_TEXTURE_2D, font->texID);
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); 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; const unsigned char *line, *wordstart;
line = (unsigned char*)text; line = (unsigned char*)text;

View file

@ -7,13 +7,14 @@ uniform float radius;
uniform int thickness; uniform int thickness;
uniform vec3 dbgColor; uniform vec3 dbgColor;
uniform bool fill; uniform bool fill;
uniform float zoom;
void main() void main()
{ {
// int tt = thickness + 1; // int tt = thickness + 1;
float R1 = 1.f; float R1 = 1.f;
float R2 = 1.f - (thickness / radius); float R2 = 1.f - (thickness*zoom / radius);
float dist = sqrt(dot(coords, coords)); float dist = sqrt(dot(coords, coords));
if (dist >= R2 && dist <= R1) if (dist >= R2 && dist <= R1)
@ -22,13 +23,4 @@ void main()
color = vec4(dbgColor, 0.1f); color = vec4(dbgColor, 0.1f);
else else
discard; 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;
*/
} }

View file

@ -15,5 +15,5 @@ layout (std140) uniform Resolution
void main() void main()
{ {
gl_Position = projection * vec4(vertex.xy, 0.0, 1.0); gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);
coords = vertex.zw*resolution; coords = vertex.zw;;
} }

View file

@ -7,9 +7,8 @@ uniform vec3 textColor;
void main() void main()
{ {
//vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);
//color = vec4(textColor, 1.0) * sampled; color = vec4(textColor, 1.0) * sampled;
color = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); if (color.a <= 0.1f)
/* if (color.a <= 0.1f) discard;
discard;*/
} }