main
Brett 2024-11-12 19:30:28 -05:00
parent b84d29ee9b
commit 04cf8158c4
4 changed files with 34 additions and 21 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
include(FetchContent) include(FetchContent)
set(BLT_GRAPHICS_VERSION 1.0.8) set(BLT_GRAPHICS_VERSION 1.0.9)
set(BLT_GRAPHICS_TEST_VERSION 0.0.1) set(BLT_GRAPHICS_TEST_VERSION 0.0.1)
project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION}) project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION})
@ -39,16 +39,26 @@ else ()
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
endif () endif ()
#set(MSDFGEN_CORE_ONLY ON CACHE BOOL "" FORCE)
#set(MSDFGEN_BUILD_STANDALONE OFF CACHE BOOL "" FORCE)
#set(MSDFGEN_USE_VCPKG OFF CACHE BOOL "" FORCE)
#set(MSDFGEN_USE_CPP11 ON CACHE BOOL "" FORCE)
FetchContent_Declare(freetype FetchContent_Declare(freetype
GIT_REPOSITORY https://github.com/freetype/freetype.git GIT_REPOSITORY https://github.com/freetype/freetype.git
GIT_TAG VER-2-13-3 GIT_TAG VER-2-13-3
FIND_PACKAGE_ARGS) FIND_PACKAGE_ARGS)
#FetchContent_Declare(msdfgen
# GIT_REPOSITORY https://github.com/Chlumsky/msdfgen.git
# GIT_TAG v1.12
# FIND_PACKAGE_ARGS)
FetchContent_Declare(imgui FetchContent_Declare(imgui
GIT_REPOSITORY https://github.com/ocornut/imgui.git GIT_REPOSITORY https://github.com/ocornut/imgui.git
GIT_TAG v1.91.4 GIT_TAG v1.91.4
FIND_PACKAGE_ARGS) FIND_PACKAGE_ARGS)
FetchContent_MakeAvailable(freetype) FetchContent_MakeAvailable(freetype)
#FetchContent_MakeAvailable(msdfgen)
FetchContent_MakeAvailable(imgui) FetchContent_MakeAvailable(imgui)
set(IMGUI_SOURCE_FILES "${imgui_SOURCE_DIR}/imgui.cpp" "${imgui_SOURCE_DIR}/imgui_demo.cpp" set(IMGUI_SOURCE_FILES "${imgui_SOURCE_DIR}/imgui.cpp" "${imgui_SOURCE_DIR}/imgui_demo.cpp"
@ -64,6 +74,7 @@ add_library(BLT_WITH_GRAPHICS ${PROJECT_BUILD_FILES} ${IMGUI_SOURCE_FILES})
target_link_libraries(BLT_WITH_GRAPHICS PUBLIC BLT) target_link_libraries(BLT_WITH_GRAPHICS PUBLIC BLT)
target_link_libraries(BLT_WITH_GRAPHICS PUBLIC freetype) target_link_libraries(BLT_WITH_GRAPHICS PUBLIC freetype)
#target_link_libraries(BLT_WITH_GRAPHICS PUBLIC msdfgen-core)
target_include_directories(BLT_WITH_GRAPHICS PUBLIC include/) target_include_directories(BLT_WITH_GRAPHICS PUBLIC include/)
target_include_directories(BLT_WITH_GRAPHICS PUBLIC "${imgui_SOURCE_DIR}") target_include_directories(BLT_WITH_GRAPHICS PUBLIC "${imgui_SOURCE_DIR}")

0
cloc.sh Normal file → Executable file
View File

View File

@ -47,7 +47,7 @@ namespace blt::gfx
blt::u64 max_index_used; blt::u64 max_index_used;
}; };
public: public:
explicit font_texture_atlas(blt::i32 dimensions); explicit font_texture_atlas(blt::i32 dimensions, blt::u32 padding);
added_font_results_t add_font(const font::font_file_t& file, float size); added_font_results_t add_font(const font::font_file_t& file, float size);
@ -63,6 +63,7 @@ namespace blt::gfx
blt::hashmap_t<float, blt::hashmap_t<blt::u64, bounded_t>> glyphs; blt::hashmap_t<float, blt::hashmap_t<blt::u64, bounded_t>> glyphs;
blt::u32 used_width = 0; blt::u32 used_width = 0;
blt::u32 used_height = 0; blt::u32 used_height = 0;
blt::u32 padding;
}; };
class font_generator_t class font_generator_t
@ -80,7 +81,7 @@ namespace blt::gfx
}; };
public: public:
explicit font_generator_t(blt::i32 dimensions, const std::vector<float>& sizes_to_generate); explicit font_generator_t(blt::i32 dimensions, blt::u32 padding, const std::vector<float>& sizes_to_generate);
void add_font(const font::font_file_t& file); void add_font(const font::font_file_t& file);
@ -101,7 +102,9 @@ namespace blt::gfx
private: private:
blt::i32 dimensions; blt::i32 dimensions;
blt::u32 padding;
std::vector<bounded_font_t> atlases; std::vector<bounded_font_t> atlases;
// doesn't make sense to store this.
std::vector<float> sizes_to_generate; std::vector<float> sizes_to_generate;
}; };

View File

@ -18,18 +18,22 @@
#include <blt/gfx/renderer/font_renderer.h> #include <blt/gfx/renderer/font_renderer.h>
#include <blt/gfx/renderer/shaders/2d_font.vert> #include <blt/gfx/renderer/shaders/2d_font.vert>
#include <blt/gfx/renderer/shaders/2d_font.frag> #include <blt/gfx/renderer/shaders/2d_font.frag>
#include "blt/gfx/shader.h" #include <blt/gfx/shader.h>
// TODO: signed distance fonts
//#include <msdfgen.h>
#include <utility> #include <utility>
namespace blt::gfx namespace blt::gfx
{ {
font_texture_atlas::font_texture_atlas(blt::i32 dimensions): texture_gl2D(dimensions, dimensions, GL_R8)
font_texture_atlas::font_texture_atlas(blt::i32 dimensions, blt::u32 padding): texture_gl2D(dimensions, dimensions, GL_R8), padding(padding)
{ {
bind(); bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 4); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
} }
font_texture_atlas::added_font_results_t font_texture_atlas::add_font(const font::font_file_t& file, float size) font_texture_atlas::added_font_results_t font_texture_atlas::add_font(const font::font_file_t& file, float size)
@ -44,8 +48,6 @@ namespace blt::gfx
blt::u64 max_index_used = start; blt::u64 max_index_used = start;
file.font.set_pixel_sizes(0, static_cast<blt::u32>(size)); file.font.set_pixel_sizes(0, static_cast<blt::u32>(size));
const blt::u32 padding = 2;
for (auto i = start; i < file.character_max_index; i++) for (auto i = start; i < file.character_max_index; i++)
{ {
// returns an empty optional if there is no error code // returns an empty optional if there is no error code
@ -56,15 +58,12 @@ namespace blt::gfx
auto width = face->glyph->bitmap.width; auto width = face->glyph->bitmap.width;
auto height = face->glyph->bitmap.rows; auto height = face->glyph->bitmap.rows;
auto texture_width = width + padding; max_height = std::max(max_height, height);
auto texture_height = height + padding;
max_height = std::max(max_height, texture_height);
// if adding this width overflows, we need to move to the next row. // if adding this width overflows, we need to move to the next row.
if (used_width + texture_width > static_cast<blt::u32>(getWidth())) if (used_width + width > static_cast<blt::u32>(getWidth()))
{ {
used_height += max_height; used_height += max_height + padding;
max_height = 0; max_height = 0;
used_width = 0; used_width = 0;
} }
@ -79,7 +78,7 @@ namespace blt::gfx
auto begin_height = used_height; auto begin_height = used_height;
auto end_width = used_width + width; auto end_width = used_width + width;
auto end_height = used_height + height; auto end_height = used_height + height;
used_width += texture_width; used_width += width + padding;
// BLT_TRACE("%d %d %d %d", begin_width, begin_height, width, height); // BLT_TRACE("%d %d %d %d", begin_width, begin_height, width, height);
// upload the texture // upload the texture
@ -99,8 +98,8 @@ namespace blt::gfx
return {-1ul, min_index_used, max_index_used}; return {-1ul, min_index_used, max_index_used};
} }
font_generator_t::font_generator_t(blt::i32 dimensions, const std::vector<float>& sizes_to_generate): font_generator_t::font_generator_t(blt::i32 dimensions, blt::u32 padding, const std::vector<float>& sizes_to_generate):
dimensions(dimensions), sizes_to_generate(sizes_to_generate) dimensions(dimensions), padding(padding), sizes_to_generate(sizes_to_generate)
{} {}
void font_generator_t::add_font(const font::font_file_t& file) void font_generator_t::add_font(const font::font_file_t& file)
@ -115,7 +114,7 @@ namespace blt::gfx
auto& vec = atlases; auto& vec = atlases;
if (vec.empty()) if (vec.empty())
vec.emplace_back(std::make_unique<font_texture_atlas>(dimensions), min_size, max_size, file.character_min_index, 0); vec.emplace_back(std::make_unique<font_texture_atlas>(dimensions, padding), min_size, max_size, file.character_min_index, 0);
blt::u64 start_index = file.character_min_index; blt::u64 start_index = file.character_min_index;
while (start_index != -1ul) while (start_index != -1ul)
@ -127,7 +126,7 @@ namespace blt::gfx
vec.back().max_size = max_size; vec.back().max_size = max_size;
if (results.last_inserted_index != -1ul) if (results.last_inserted_index != -1ul)
{ {
vec.emplace_back(std::make_unique<font_texture_atlas>(dimensions), min_size, max_size, start_index, 0); vec.emplace_back(std::make_unique<font_texture_atlas>(dimensions, padding), min_size, max_size, start_index, 0);
min_size = size; min_size = size;
max_size = size; max_size = size;
} }
@ -154,10 +153,10 @@ namespace blt::gfx
{ {
int size; int size;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size); glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
dimensions = std::min(size, 4096); dimensions = std::min(size, 2048);
BLT_INFO("Max texture size %d, font renderer will use size %d", size, dimensions); BLT_INFO("Max texture size %d, font renderer will use size %d", size, dimensions);
} }
generator = std::make_unique<font_generator_t>(dimensions, generated_font_sizes); generator = std::make_unique<font_generator_t>(dimensions, 2, generated_font_sizes);
} }
void font_renderer_t::cleanup() void font_renderer_t::cleanup()