This commit is contained in:
John Alanbrook 2023-05-07 02:16:10 +00:00
parent 0f8a466354
commit 8552d6a1df
5 changed files with 79 additions and 46 deletions

View file

@ -80,15 +80,6 @@ void font_init(struct shader *textshader) {
} }
}, },
.fs.uniform_blocks[0] = {
.size = sizeof(float)*3+sizeof(int),
.layout = SG_UNIFORMLAYOUT_STD140,
.uniforms = {
[0] = { .name = "textColor", .type = SG_UNIFORMTYPE_FLOAT3 },
[1] = { .name = "invert", .type = SG_UNIFORMTYPE_INT }
}
},
.fs.images[0] = { .fs.images[0] = {
.name = "text", .name = "text",
.image_type = SG_IMAGETYPE_2D, .image_type = SG_IMAGETYPE_2D,
@ -99,20 +90,35 @@ void font_init(struct shader *textshader) {
pipe_text = sg_make_pipeline(&(sg_pipeline_desc){ pipe_text = sg_make_pipeline(&(sg_pipeline_desc){
.shader = fontshader, .shader = fontshader,
.layout = { .layout = {
.attrs = { [0].format = SG_VERTEXFORMAT_FLOAT4 } .attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT2,
[0].buffer_index = 0,
[1].format = SG_VERTEXFORMAT_FLOAT2,
[1].buffer_index = 0,
[2].format = SG_VERTEXFORMAT_FLOAT3,
[2].buffer_index = 1},
.buffers[2].step_func = SG_VERTEXSTEP_PER_INSTANCE,
}, },
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP, .primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.label = "text pipeline" .label = "text pipeline"
}); });
bind_text.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ bind_text.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(float)*16, .size = sizeof(float)*16*40000,
.type = SG_BUFFERTYPE_VERTEXBUFFER, .type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM, .usage = SG_USAGE_STREAM,
.label = "text buffer" .label = "text buffer"
}); });
bind_text.vertex_buffers[1] = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(float)*3*40000,
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM,
.label = "text color buffer"
});
font = MakeFont("LessPerfectDOSVGA.ttf", 16); font = MakeFont("LessPerfectDOSVGA.ttf", 16);
bind_text.fs_images[0] = font->texID;
} }
void font_frame(struct window *w) { void font_frame(struct window *w) {
@ -202,6 +208,19 @@ void draw_char_box(struct Character c, float cursor[2], float scale, float color
draw_rect(x,y,w,h,color); draw_rect(x,y,w,h,color);
} }
void text_flush()
{
sg_apply_pipeline(pipe_text);
sg_apply_bindings(&bind_text);
sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection));
YughWarn("Chars: %d", curchar);
sg_draw(0,4*curchar,1);
curchar = 0;
}
void fill_charverts(float *verts, float cursor[2], float scale, struct Character c, float *offset) 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;
@ -231,11 +250,20 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct
float offset[2] = {-1, 1}; float offset[2] = {-1, 1};
fill_charverts(verts, cursor, scale, c, offset); fill_charverts(verts, cursor, scale, c, offset);
curchar++;
/* 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;
curchar++;
/* SET COLOR ? */
sg_append_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts));
sg_append_buffer(bind_text.vertex_buffers[1], SG_RANGE_REF(color));
return;
// fill_charverts(verts, cursor, scale, c, offset);
/* /*
if (drawcaret == curchar) { if (drawcaret == curchar) {
draw_char_box(c, cursor, scale, color); draw_char_box(c, cursor, scale, color);
@ -245,37 +273,30 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct
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);
} }
*/ */
shader_setvec3(shader, "textColor", shadowcolor); // sg_apply_uniforms(SG_SHADERSTAGE_FS, 0, SG_RANGE_REF(shadowcolor));
/*
sg_update_buffer(bind_text.vertex_buffers[0], verts); sg_append_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts));
sg_draw(0,4,1);
offset[0] = 1; offset[0] = 1;
offset[1] = -1; offset[1] = -1;
fill_charverts(verts, cursor, scale, c, offset); fill_charverts(verts, cursor, scale, c, offset);
sg_update_buffer(bind_text.vertex_buffers[0], verts); sg_update_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts));
sg_draw(0,4,1);
offset[1] = 1; offset[1] = 1;
fill_charverts(verts, cursor, scale, c, offset); fill_charverts(verts, cursor, scale, c, offset);
sg_update_buffer(bind_text.vertex_buffers[0], verts); sg_update_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts));
sg_draw(0,4,1);
offset[0] = -1; offset[0] = -1;
offset[1] = -1; offset[1] = -1;
fill_charverts(verts, cursor, scale, c, offset); fill_charverts(verts, cursor, scale, c, offset);
sg_update_buffer(bind_text.vertex_buffers[0], verts); sg_update_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts));
sg_draw(0,4,1); */
offset[0] = offset[1] = 0; offset[0] = offset[1] = 0;
fill_charverts(verts, cursor, scale, c, offset); fill_charverts(verts, cursor, scale, c, offset);
shader_setvec3(shader, "textColor", color); /* SET COLOR ? */
sg_update_buffer(bind_text.vertex_buffers[0], verts); sg_update_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts));
sg_draw(0,4,1);
} }
void text_settype(struct sFont *mfont) void text_settype(struct sFont *mfont)
@ -291,15 +312,10 @@ int renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3]
mfloat_t cursor[2] = { 0.f }; mfloat_t cursor[2] = { 0.f };
cursor[0] = pos[0]; cursor[0] = pos[0];
cursor[1] = pos[1]; cursor[1] = pos[1];
sg_apply_pipeline(pipe_text);
sg_apply_bindings(&bind_text);
sg_apply_uniforms(SG_SHADERSTAGE_FS,0,color);
const unsigned char *line, *wordstart, *drawstart; const unsigned char *line, *wordstart, *drawstart;
line = drawstart = (unsigned char*)text; line = drawstart = (unsigned char*)text;
curchar = 0;
float *usecolor = color; float *usecolor = color;
while (*line != '\0') { while (*line != '\0') {
@ -347,10 +363,10 @@ int renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3]
} }
} }
if (caret > curchar) { /* if (caret > curchar) {
draw_char_box(font->Characters[69], cursor, scale, color); draw_char_box(font->Characters[69], cursor, scale, color);
} }
*/
return cursor[1] - pos[1]; return cursor[1] - pos[1];

View file

@ -31,6 +31,9 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct
void text_settype(struct sFont *font); void text_settype(struct sFont *font);
int renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3], float lw,int caret); int renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3], float lw,int caret);
//void text_frame();
void text_flush();
unsigned char *slurp_file(const char *filename); unsigned char *slurp_file(const char *filename);
char *slurp_text(const char *filename); char *slurp_text(const char *filename);

View file

@ -139,8 +139,13 @@ void openglRender(struct window *window)
window->height, -1.f, 1.f); window->height, -1.f, 1.f);
// sprite_draw_all(); // sprite_draw_all();
gui_draw_img("pill1.png", 200, 200); // gui_draw_img("pill1.png", 200, 200);
// renderText("TEST RENDER", {100,100}, 1.f, {1.f,1.f,1.f}, 0,-1); float a[2] = {100,100};
float w[3] = {1.f,1.f,1.f};
renderText("TEST RENDER", a, 1.f, w, 0,-1);
float b[2] = {50,50};
renderText("TEST 2 RENDER", b, 1.f, w, 0,-1);
/* UI Elements & Debug elements */ /* UI Elements & Debug elements */
// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
@ -154,6 +159,7 @@ void openglRender(struct window *window)
////// TEXT && GUI ////// TEXT && GUI
// glBindBuffer(GL_UNIFORM_BUFFER, projUBO); // glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
// glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, ui_projection); // glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, ui_projection);
text_flush();
// call_gui(); // call_gui();

View file

@ -1,15 +1,17 @@
#version 330 core #version 330 core
in vec2 TexCoords; in vec2 TexCoords;
in vec3 fColor;
out vec4 color; out vec4 color;
uniform sampler2D text; uniform sampler2D text;
uniform vec3 textColor;
uniform bool invert;
void main() void main()
{ {
vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); // color = vec4(fColor.xyz, texture(text, TexCoords).r);
color = vec4(textColor, 1.0) * sampled; color = vec4(1.f,1.f,1.f, texture(text, TexCoords).r);
if (color.a <= 0.1f) if (color.a <= 0.1f)
discard; discard;
} }

View file

@ -1,11 +1,17 @@
#version 330 core #version 330 core
layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex> layout (location = 0) in vec2 vertex;
layout (location = 1) in vec2 rect;
layout (location = 2) in vec3 vColor;
out vec2 TexCoords; out vec2 TexCoords;
out vec3 fColor;
uniform mat4 projection; uniform mat4 projection;
void main() void main()
{ {
gl_Position = projection * vec4(vertex.xy, 0.0, 1.0); gl_Position = projection * vec4(vertex, 0.0, 1.0);
TexCoords = vertex.zw; TexCoords = rect;
fColor = vColor;
} }