From 5f422f1452162bc279570cf7dd4463bc4ddc706d Mon Sep 17 00:00:00 2001 From: lisyarus Date: Mon, 21 Sep 2020 21:53:56 +0300 Subject: [PATCH] Use non-normalized texture coordinates for text & add more space between font glyphs --- libs/gfx/resources/font_9x12.pbm | 4 ++-- libs/gfx/source/painter.cpp | 21 +++++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/libs/gfx/resources/font_9x12.pbm b/libs/gfx/resources/font_9x12.pbm index 6c65e962..dc215b32 100644 --- a/libs/gfx/resources/font_9x12.pbm +++ b/libs/gfx/resources/font_9x12.pbm @@ -1,3 +1,3 @@ P4 -144 72 -џџџџџџџџџџџџџџџџџџџџџџџџџџџћїџџџџџџџџћђ~Пџяћї§џџџџџћџћіўО>Пџяїћ№~џџџџћџћі§}џЯяїћ§ўџџџџїџћџј=џЯПяїћњўџџџџїџћџ§~>?Пџїћњ№ўџяџџџј=ПпWџїћџўџџџџяџџџ§|Џoџїћџўџ?џŸпџћџњџп—џћїџўџџŸпџџџњџџџџћїџџўџџПџџџџџџџџџџџџџўџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџуѓјќŸЧƒуёџџџџџџџнћї{П_ПЛнюџџџŸўчнћџП_ћнюљўџПлнћўў~пїуюљў|ўЯћнћ§џНпяwїн№џџћџџїїнћћџМяwїнўџџќўЯянћї{Пояwян§љўџПџур№|яууљќџŸўЯџџџџџџџџџџџџ§џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџуџџџџџџџџџџџџџџџџџнѓрќ8|Уˆрјq|F#Чнћї{НОяwЛнћўћОўO7Лйѕї{§оП_Пнћўћ~ўO7Леѕ№ћ§о?ПСћўњўўЏWЛеѕї{§оП_БнћіјўўЏWЛйрї{§оџЛнћіћ~ояWЛпюї{НОяЛнћіћОояgЛнФ`ќx|?ЧˆрљёœF'ЧуџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџќџџџУёрќИ F#œФp}§џппџэюї{;nяwЛнюї}ўџппџэюї{џ~яwЛыѕў§ўџпЏџэюї|~яЏЋїѕ§§ўџпwџую№џП~яЏЋїћ§§џпџџяюіџП~яЏЋыћћ§џпџџяюїyП~япЋнћї}џПпџџЧёуК~?пзœё№}џПпџџџјџџџџџџџџџџќПџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџїџџџџџџџџџџџџџџџџџћџчџџŸџЧџŸћ§ѓўџџџџџїџџпџПџпџџћџџџџџёєќ>_Щгу№њ?|^OЧџюѓ{НžяПГЭћўћ~Џ7Лџ№ї{§оПЛнћўјџ~ЏwЛџюї{§оџПЛнћўњџ~ЏwЛџюї{НоџПЛнћўћ~ЏwЛџ№`ќ~Уˆрўђ<#ЧџџџџџџџџћџџўџџџџџџџџџџџџџџЧџџёџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџўџПЗџџџџџ§џџџџџџџўџПOџ“ђr|8<Ю#ЬФp~џПџџЭьљћНўяwЛэюіўџПџџнюћќ}ўяwЋѓі§§џпџџнюћџНўяЏЋѓѕћўџПџџнюћћНоЯЏЋэљї~џПџџУ№№x~?'пзЬћ№~џПџџпўџџџџџџџџћџџџџџќџџџџџџџсџџџџџџџ \ No newline at end of file +176 84 +џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ§ўџџџџџџџџџџџ“§Пяџџп§ўџяџџџџџџїџџЗ§зџџпћџƒ§џџџџџїџџЗњўџяўпћџя§џџџџџяџџџ№~џљ§џпћџз§џџџџџяџџџњџЧ§џџћџзр?џСџџпџџџџ№~пћњПџћџџ§џџџџџпџџџџњў?ѕћџћџџ§џŸџќџПџџџѕџПћќПџ§ўџџ§џПџќџПџџџџѕџПџџџџ§ўџџџџ?џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџёўЧјџЯсў?ёў?џџџџџџџџюџЛїЏя§џwю§пџџџЯџѓџЯюџћџЏяћџїю§пЯќџПџ§џЗюџїќџoујяё§пЯќўСўїюџяџ~я§ћПяюўџџ§џџџПяюџпџ~§ћПяюџпџџўСўпюџЛїянћПпюџПЯќџПџ§џџёќƒјџЧуќпёќЯљџЯџѓџŸџџџџџџџџџџџџџџџћџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџёџџџџџџџџџџџџџџџџџџџџџюўј|?№?‡Ф|Ут>?ˆёюџЛї~пнћПwюџїїЩљПwьўПЛїўязњџюџїіџЩљПwъўП‡їўяЧјџрџїѕџењПwъўПЛїўязњџcюџЗёџењПwьќЛїўяпћџwюџЗіџoнњПwя§пЛї~пнћџwюџЗїoнћ?wюјјќ?ёџФ|Яу>ˆё?ёџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџјџџуџџџсў?љ|ˆё#Юxƒћўџћўџџі§пЛі}ЗнћПwю§пЛћџћўџџі§пЛїџПнћПwѕўПїћџћ§џі§пЛјџПн§WћўПяћџћћПџё§п‡џПн§WћџяћџПћџџџї§пЗџПн§WѕџпћџПћџџџї§пЛѓПнўџWюџЛћџпћџџџуў?єџуўџЏЮ~?ƒћџпћџџџџџџџџџџџџџџџџџјџпуџџџџџџџџџџџџџџџџџџџџџџџўџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџћџџџџџџџџџџџџџџџџџџџџџ§џџ?џџЯџў?џЯџячџ?џџџџџџџПџџяџ§џџяџџџїџПџџџџџў?Їј/уј?“щќ‡єП‹ђџ§п›ї~Ян§џgцџїіџПељПwџўЛїўяС§џwюџїёџПећПwџ§пЛїўяп§џwюџїѕџПећПwџ§пЛї~яп§џwюџїіџПећПwџўјџсј?‡Ф|їф~€ёџџџџџџџџџџїџџџїџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџўџПяџџџџџџџџџџџџџџџџџџ§џПї§Пџџџџџџўџџџџџџџџџ§џПїњџЩўO“ј|™ё#цxƒ§џПїџџџц§ŸЯї~џнћПwі§пЗ§џПїџџџю§ппјўџнћПWљўпяћџПћџџџю§ппџ~џн§WљўПп§џПїџџџю§ппї~яй§Wіџ?Л§џПїџџџсўƒ№џфўџЏцƒ§џПїџџџяџпџџџџџџџџџџџўџџяџџџЧџџџџџџџџџџќ?џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ \ No newline at end of file diff --git a/libs/gfx/source/painter.cpp b/libs/gfx/source/painter.cpp index 79639647..c317ec63 100644 --- a/libs/gfx/source/painter.cpp +++ b/libs/gfx/source/painter.cpp @@ -5,6 +5,7 @@ #include #include #include +#include static const char vertex_source[] = R"(#version 330 @@ -40,6 +41,7 @@ static const char text_vertex_source[] = R"(#version 330 uniform mat4 u_transform; +uniform ivec2 u_texture_size; layout (location = 0) in vec4 in_position; layout (location = 1) in vec4 in_color; @@ -52,7 +54,7 @@ void main() { gl_Position = u_transform * in_position; color = in_color; - texcoord = in_texcoord; + texcoord = in_texcoord / vec2(u_texture_size); } )"; @@ -106,7 +108,7 @@ namespace psemek::gfx impl() { mesh.setup, gfx::normalized>(); - text_mesh.setup, gfx::normalized, gfx::normalized>>(); + text_mesh.setup, gfx::normalized, geom::point>(); util::memory_istream font_data(resource::font_9x12); font_texture.load(gfx::read_pbm(font_data)); @@ -117,6 +119,8 @@ namespace psemek::gfx gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_SWIZZLE_A, gl::RED); gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST); gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST); + gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE); + gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE); } }; @@ -259,9 +263,9 @@ namespace psemek::gfx geom::vector const sx = {9.f * opts.scale, 0.f}; geom::vector const sy = {0.f, 12.f * opts.scale}; - auto to_texcoord = [](int tx, int ty) + auto to_texcoord = [](int tx, int ty, int ix, int iy) { - return geom::point{ (tx * 65535) / 16, (ty * 65535) / 6 }; + return geom::point{ tx * 11 + (ix == 0 ? 1 : 10), ty * 14 + (iy == 0 ? 1 : 13) }; }; for (char c : str) @@ -277,10 +281,10 @@ namespace psemek::gfx std::uint32_t const base = impl().text_vertices.size(); - impl().text_vertices.push_back({pen, opts.c, to_texcoord(tx, ty)}); - impl().text_vertices.push_back({pen + sx, opts.c, to_texcoord(tx + 1, ty)}); - impl().text_vertices.push_back({pen + sy, opts.c, to_texcoord(tx, ty + 1)}); - impl().text_vertices.push_back({pen + sx + sy, opts.c, to_texcoord(tx + 1, ty + 1)}); + impl().text_vertices.push_back({pen, opts.c, to_texcoord(tx, ty, 0, 0)}); + impl().text_vertices.push_back({pen + sx, opts.c, to_texcoord(tx, ty, 1, 0)}); + impl().text_vertices.push_back({pen + sy, opts.c, to_texcoord(tx, ty, 0, 1)}); + impl().text_vertices.push_back({pen + sx + sy, opts.c, to_texcoord(tx, ty, 1, 1)}); impl().text_indices.push_back(base + 0); impl().text_indices.push_back(base + 1); @@ -435,6 +439,7 @@ namespace psemek::gfx impl().text_program.bind(); impl().text_program["u_transform"] = transform; impl().text_program["u_texture"] = 0; + impl().text_program["u_texture_size"] = impl().font_texture.size(); impl().font_texture.bind(); impl().text_mesh.draw(gl::TRIANGLES); }