From d8911df6193d96ddfd3f3117d1ae99470eb018de Mon Sep 17 00:00:00 2001 From: bailwillharr Date: Mon, 28 Nov 2022 09:39:00 +0000 Subject: [PATCH] Do things --- CMakeLists.txt | 2 +- include/components/text_ui_renderer.hpp | 2 +- include/resources/font.hpp | 10 ++++++++++ src/components/text_ui_renderer.cpp | 19 +++++++++++++++---- src/resources/font.cpp | 22 ++++++++++++++++++++-- src/util/model_loader.cpp | 11 +++++++++-- test/src/game.cpp | 4 ++++ 7 files changed, 60 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2737f37..2d745c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -221,7 +221,7 @@ target_include_directories(${PROJECT_NAME} PRIVATE dependencies/stb) set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "" FORCE) set(ASSIMP_BUILD_TESTS OFF CACHE INTERNAL "" FORCE) set(ASSIMP_BUILD_ZLIB ON CACHE INTERNAL "" FORCE) -set(ASSIMP_NO_EXPORT OFF CACHE INTERNAL "" FORCE) +set(ASSIMP_NO_EXPORT ON CACHE INTERNAL "" FORCE) set(ASSIMP_INSTALL OFF CACHE INTERNAL "" FORCE) add_subdirectory(dependencies/assimp) target_include_directories(${PROJECT_NAME} PRIVATE dependencies/assimp/include) diff --git a/include/components/text_ui_renderer.hpp b/include/components/text_ui_renderer.hpp index c33d87b..f324d44 100644 --- a/include/components/text_ui_renderer.hpp +++ b/include/components/text_ui_renderer.hpp @@ -40,4 +40,4 @@ namespace engine::components { }; -} \ No newline at end of file +} diff --git a/include/resources/font.hpp b/include/resources/font.hpp index 302a818..c0f9102 100644 --- a/include/resources/font.hpp +++ b/include/resources/font.hpp @@ -19,10 +19,20 @@ public: Font(const std::filesystem::path& resPath); ~Font() override; + struct CharData { + glm::vec2 atlas_top_left{}; + glm::vec2 atlas_bottom_right{}; + glm::vec2 offset{}; + float xAdvance{}; + }; + const gfx::Texture* getAtlasTexture(); + CharData getCharData(uint32_t charCode); + private: const gfx::Texture* m_atlas; + std::map m_charData; }; diff --git a/src/components/text_ui_renderer.cpp b/src/components/text_ui_renderer.cpp index 204fe39..253b204 100644 --- a/src/components/text_ui_renderer.cpp +++ b/src/components/text_ui_renderer.cpp @@ -20,7 +20,6 @@ UI::UI(Object* parent) : Component(parent, TypeEnum::UI) { { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f } }, { { 1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f } } }); - } UI::~UI() @@ -33,14 +32,26 @@ void UI::render(glm::mat4 transform, glm::mat4 view) struct { glm::mat4 m; + glm::vec2 atlas_top_left; + glm::vec2 atlas_bottom_right; glm::vec2 offset; glm::vec2 size; } pushConsts{}; - pushConsts.m = glm::mat4{1.0f}; + int advance = 0; - gfxdev->draw(m_shader->getPipeline(), m_atlasMesh->vb, m_atlasMesh->ib, m_atlasMesh->m_indices.size(), &pushConsts, sizeof(pushConsts), m_font->getAtlasTexture()); + for (char c : m_text) { + auto charData = m_font->getCharData(c); + pushConsts.m = glm::mat4{1.0f}; + pushConsts.atlas_top_left = charData.atlas_top_left; + pushConsts.atlas_bottom_right = charData.atlas_bottom_right; + pushConsts.offset = charData.offset; + gfxdev->draw( + m_shader->getPipeline(), m_atlasMesh->vb, m_atlasMesh->ib, m_atlasMesh->m_indices.size(), + &pushConsts, sizeof(pushConsts), m_font->getAtlasTexture()); + advance += charData.xAdvance; + } } -} \ No newline at end of file +} diff --git a/src/resources/font.cpp b/src/resources/font.cpp index 4e3a791..b583b22 100644 --- a/src/resources/font.cpp +++ b/src/resources/font.cpp @@ -16,12 +16,15 @@ Font::Font(const std::filesystem::path& resPath) : Resource(resPath, "font") // TODO: load font auto fontBuffer = util::readBinaryFile(resPath.string()); + constexpr int FIRST_CHAR = 32; + constexpr int NUM_CHARS = 96; + constexpr int BITMAP_WIDTH = 1024; constexpr int BITMAP_HEIGHT = 1024; auto pixels = std::make_unique(BITMAP_WIDTH * BITMAP_HEIGHT); - auto chardata = std::make_unique(96); + auto bakedChars = std::make_unique(NUM_CHARS); - stbtt_BakeFontBitmap(fontBuffer->data(), 0, 128.0f, pixels.get(), BITMAP_WIDTH, BITMAP_HEIGHT, 32, 96, chardata.get()); + stbtt_BakeFontBitmap(fontBuffer->data(), 0, 128.0f, pixels.get(), BITMAP_WIDTH, BITMAP_HEIGHT, FIRST_CHAR, NUM_CHARS, bakedChars.get()); auto textureData = std::make_unique(BITMAP_WIDTH * BITMAP_HEIGHT * 4); @@ -34,6 +37,16 @@ Font::Font(const std::filesystem::path& resPath) : Resource(resPath, "font") m_atlas = gfxdev->createTexture(textureData.get(), BITMAP_WIDTH, BITMAP_HEIGHT, gfx::TextureFilter::LINEAR, gfx::TextureFilter::LINEAR); + for (int i = FIRST_CHAR; i < NUM_CHARS; i++) { + CharData charData{}; + charData.atlas_top_left = { bakedChars[i].x0, bakedChars[i].y0 }; + charData.atlas_bottom_right = { bakedChars[i].x1, bakedChars[i].y1 }; + charData.offset = { bakedChars[i].xoff, bakedChars[i].yoff }; + charData.xAdvance = bakedChars[i].xadvance; + m_charData[i] = charData; + // TODO + } + } Font::~Font() @@ -46,4 +59,9 @@ const gfx::Texture* Font::getAtlasTexture() return m_atlas; } +Font::CharData Font::getCharData(uint32_t charCode) +{ + return m_charData[charCode]; +} + } diff --git a/src/util/model_loader.cpp b/src/util/model_loader.cpp index f094a1c..c7db83a 100644 --- a/src/util/model_loader.cpp +++ b/src/util/model_loader.cpp @@ -7,6 +7,8 @@ #include "components/mesh_renderer.hpp" +#include "resource_manager.hpp" + #include #include #include @@ -17,6 +19,7 @@ #include + #include namespace engine::util { @@ -162,7 +165,11 @@ namespace engine::util { std::filesystem::path absPath = path; absPath = absPath.parent_path(); absPath /= texPath.C_Str(); - textures[i] = std::make_shared(absPath); + try { + textures[i] = std::make_shared(absPath); + } catch (const std::runtime_error& e) { + textures[i] = parent->res.get("textures/white.png"); + } } } @@ -210,4 +217,4 @@ namespace engine::util { return obj; } -} \ No newline at end of file +} diff --git a/test/src/game.cpp b/test/src/game.cpp index f811601..1ddd36a 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -165,8 +165,12 @@ void playGame() plane->transform.position = { -30.0f, 2.0f, 10.0f }; */ + auto van = engine::util::loadAssimpMeshFromFile(app.scene(), app.resources()->getFilePath("models/van/van.dae").string()); + +/* auto lego = engine::util::loadAssimpMeshFromFile(app.scene(), app.resources()->getFilePath("models/lego/lego.dae").string()); lego->transform.position = { -30.0f, -33.0f, -30.0f }; +*/ app.scene()->printTree();