Refactor draw to be separate and handle ortho draw

This commit is contained in:
2026-03-23 13:32:46 +05:45
parent 157f04060e
commit 739a806493
6 changed files with 637 additions and 267 deletions
+30 -13
View File
@@ -1,5 +1,6 @@
package e2glyph
import "core:fmt"
/*
Provides text rendering using glfw, stb_truetype for the engine
@@ -52,11 +53,13 @@ GlyphState :: struct {
}
glyph_init :: proc(glyph: ^GlyphState, filepath : string) {
glyph_init :: proc(glyph: ^GlyphState, font_path : string = "") {
ok : bool
glyph.program_id, ok = gl.load_shaders_source(#load("./shaders/font_vert.glsl"), #load("./shaders/font_frag.glsl"), )
gl.UseProgram(glyph.program_id)
gl.CreateBuffers(1, &glyph.rect_instances_vbo)
gl.CreateVertexArrays(1, &glyph.vao)
@@ -82,9 +85,18 @@ glyph_init :: proc(glyph: ^GlyphState, filepath : string) {
gl.CreateTextures(gl.TEXTURE_RECTANGLE, 1, &glyph.atlas_texture)
gl.TextureStorage2D(glyph.atlas_texture, 1, gl.RGB8, glyph.atlas_width, glyph.atlas_height)
font_data, _ := os.read_entire_file_from_path(filepath, context.allocator)
stbtt.InitFont(&glyph.font_info, &font_data[0], 0)
if !os.exists(font_path)
{
fmt.eprintln("Font not provided or not exist using default font")
font_data := #load("./mononoki-Regular.ttf", []byte)
stbtt.InitFont(&glyph.font_info, &font_data[0], 0)
}else
{
font_data, _ := os.read_entire_file_from_path(font_path, context.allocator)
stbtt.InitFont(&glyph.font_info, &font_data[0], 0)
}
gl.UseProgram(0)
}
@@ -98,6 +110,7 @@ glyph_draw_font :: proc(glyph_state: ^GlyphState, text: string, pos : [2]f32, co
coverage_adjustment: f32 = 0.0
text_color: [4]f32 = {f32( color.r)/ 255.0 ,f32(color.g)/255.0, f32(color.b)/255.0, f32(color.a)/255.0}
glyph_state.rect_buffer = make([dynamic]GlyphRectInstance, 0, 1000)
{
//put every glyph in text into rect_buffer
@@ -138,13 +151,15 @@ glyph_draw_font :: proc(glyph_state: ^GlyphState, text: string, pos : [2]f32, co
glyph_state.curr.y += math.round(line_height)
} else if c == '\t' {
glyph_state.curr.x += 2 * glyph_state.font_size_pt
} else {
horizontal_filter_padding, subpixel_positioning_left_padding: i32 = 1, 1
assert(codepoint <= 127)
if codepoint >= 127{
return
}
assert(codepoint < 127)
glyph_atlas := glyph_state.atlas_items[codepoint]
if !glyph_atlas.filled
@@ -283,17 +298,19 @@ glyph_draw_font :: proc(glyph_state: ^GlyphState, text: string, pos : [2]f32, co
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC1_COLOR)
gl.BindVertexArray(glyph_state.vao)
gl.UseProgram(glyph_state.program_id)
gl.ProgramUniform2f(glyph_state.program_id, 0, f32(glyph_state.width) / 2.0, f32(glyph_state.height) / 2.0)
gl.ProgramUniform1ui(glyph_state.program_id, 1, u32(coverage_adjustment))
gl.ProgramUniform1ui(glyph_state.program_id, 2, 0)
gl.BindTextureUnit(0, glyph_state.atlas_texture)
gl.UseProgram( glyph_state.program_id)
gl.BindVertexArray( glyph_state.vao)
gl.ProgramUniform2f( glyph_state.program_id, 0, f32(glyph_state.width) / 2.0, f32(glyph_state.height) / 2.0)
gl.ProgramUniform1ui( glyph_state.program_id, 1, u32(coverage_adjustment))
gl.ProgramUniform1ui( glyph_state.program_id, 2, 0)
gl.BindTextureUnit(0, glyph_state.atlas_texture)
gl.DrawArraysInstanced(gl.TRIANGLES, 0, 6, i32(len(glyph_state.rect_buffer)))
gl.Disable(gl.BLEND)
gl.UseProgram(0)
gl.BindVertexArray(0)
//gl.UseProgram(0)
//gl.BindVertexArray(0)
gl.InvalidateBufferData(glyph_state.rect_instances_vbo)
}
Binary file not shown.