ADD: Render Text!!! PARTY
This commit is contained in:
parent
917d096d60
commit
be0eb3bae8
@ -15,6 +15,10 @@ target_include_directories(glad PUBLIC
|
|||||||
lib/glad/include
|
lib/glad/include
|
||||||
)
|
)
|
||||||
|
|
||||||
|
find_package(Freetype REQUIRED)
|
||||||
|
include_directories(${FREETYPE_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
|
||||||
add_executable(Dicewars_Siedler src/main.cpp
|
add_executable(Dicewars_Siedler src/main.cpp
|
||||||
src/engine/core/Window.cpp
|
src/engine/core/Window.cpp
|
||||||
src/engine/core/Window.h
|
src/engine/core/Window.h
|
||||||
@ -126,6 +130,18 @@ add_executable(Dicewars_Siedler src/main.cpp
|
|||||||
src/engine/core/gui/uiMain/UiContainer.h
|
src/engine/core/gui/uiMain/UiContainer.h
|
||||||
src/engine/core/gui/uiComponent/UiImage.cpp
|
src/engine/core/gui/uiComponent/UiImage.cpp
|
||||||
src/engine/core/gui/uiComponent/UiImage.h
|
src/engine/core/gui/uiComponent/UiImage.h
|
||||||
|
src/engine/core/gui/uiComponent/UiText.cpp
|
||||||
|
src/engine/core/gui/uiComponent/UiText.h
|
||||||
|
src/engine/core/gui/text/Font.cpp
|
||||||
|
src/engine/core/gui/text/Font.h
|
||||||
|
src/engine/core/gui/uiComponent/UiRenderBundle.cpp
|
||||||
|
src/engine/core/gui/uiComponent/UiRenderBundle.h
|
||||||
|
src/engine/renderer/shaders/TextShader.cpp
|
||||||
|
src/engine/renderer/shaders/TextShader.h
|
||||||
|
src/engine/renderer/TextRenderer.cpp
|
||||||
|
src/engine/renderer/TextRenderer.h
|
||||||
|
src/engine/renderer/model/TextQuadModel.cpp
|
||||||
|
src/engine/renderer/model/TextQuadModel.h
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(Dicewars_Siedler PRIVATE
|
target_include_directories(Dicewars_Siedler PRIVATE
|
||||||
@ -140,4 +156,5 @@ target_link_libraries(Dicewars_Siedler
|
|||||||
glfw
|
glfw
|
||||||
glad
|
glad
|
||||||
OpenGL::GL
|
OpenGL::GL
|
||||||
|
${FREETYPE_LIBRARIES}
|
||||||
)
|
)
|
||||||
13
assets/shaders/textFragmentShader.glsl
Normal file
13
assets/shaders/textFragmentShader.glsl
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#version 400 core
|
||||||
|
|
||||||
|
in vec2 pass_texCoords;
|
||||||
|
|
||||||
|
out vec4 outColor;
|
||||||
|
|
||||||
|
uniform sampler2D text;
|
||||||
|
uniform vec3 textColor;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
float alpha = texture(text, pass_texCoords).r;
|
||||||
|
outColor = vec4(textColor, alpha);
|
||||||
|
}
|
||||||
12
assets/shaders/textVertexShader.glsl
Normal file
12
assets/shaders/textVertexShader.glsl
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#version 400 core
|
||||||
|
|
||||||
|
layout(location = 0) in vec4 vertices;
|
||||||
|
|
||||||
|
out vec2 pass_texCoords;
|
||||||
|
|
||||||
|
uniform mat4 projectionMatrix;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_Position = projectionMatrix * vec4(vertices.x, vertices.y, 0.0, 1.0);
|
||||||
|
pass_texCoords = vec2(vertices.z, vertices.w);
|
||||||
|
}
|
||||||
58
src/engine/core/gui/text/Font.cpp
Normal file
58
src/engine/core/gui/text/Font.cpp
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 10.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Font.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
Font::Font(const std::string &fontPath, unsigned int fontSize) {
|
||||||
|
if (FT_Init_FreeType(&ft)) throw std::runtime_error("Could not init FreeType");
|
||||||
|
if (FT_New_Face(ft, fontPath.c_str(), 0, &face)) throw std::runtime_error("Failed to load font");
|
||||||
|
|
||||||
|
FT_Set_Pixel_Sizes(face, 0, fontSize);
|
||||||
|
|
||||||
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // 1 byte per pixel
|
||||||
|
|
||||||
|
for (unsigned char c = 0; c < 128; c++) {
|
||||||
|
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
|
||||||
|
std::cerr << "Failed to load char: " << (int)c << "\n";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint texture;
|
||||||
|
glGenTextures(1, &texture);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED,
|
||||||
|
face->glyph->bitmap.width,
|
||||||
|
face->glyph->bitmap.rows,
|
||||||
|
0, GL_RED, GL_UNSIGNED_BYTE,
|
||||||
|
face->glyph->bitmap.buffer);
|
||||||
|
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
|
Character character = {
|
||||||
|
texture,
|
||||||
|
{face->glyph->bitmap.width, face->glyph->bitmap.rows},
|
||||||
|
{face->glyph->bitmap_left, face->glyph->bitmap_top},
|
||||||
|
static_cast<unsigned int>(face->glyph->advance.x)
|
||||||
|
};
|
||||||
|
characters.insert({c, character});
|
||||||
|
}
|
||||||
|
|
||||||
|
FT_Done_Face(face);
|
||||||
|
FT_Done_FreeType(ft);
|
||||||
|
}
|
||||||
|
|
||||||
|
Font::Character Font::getCharacter(char c) const {
|
||||||
|
unsigned char uc = static_cast<unsigned char>(c);
|
||||||
|
auto it = characters.find(uc);
|
||||||
|
if (it != characters.end())
|
||||||
|
return it->second;
|
||||||
|
else
|
||||||
|
throw std::runtime_error("Character not loaded: " + std::to_string(uc));
|
||||||
|
}
|
||||||
38
src/engine/core/gui/text/Font.h
Normal file
38
src/engine/core/gui/text/Font.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 10.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef DICEWARS_SIEDLER_FONT_H
|
||||||
|
#define DICEWARS_SIEDLER_FONT_H
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "glad/glad.h"
|
||||||
|
#include "glm/vec2.hpp"
|
||||||
|
|
||||||
|
#include <ft2build.h>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
#include FT_FREETYPE_H
|
||||||
|
|
||||||
|
class Font {
|
||||||
|
public:
|
||||||
|
Font(const std::string& fontPath, unsigned int fontSize);
|
||||||
|
~Font() = default;
|
||||||
|
|
||||||
|
struct Character {
|
||||||
|
GLuint textureID;
|
||||||
|
glm::ivec2 size;
|
||||||
|
glm::ivec2 bearing;
|
||||||
|
unsigned int advance;
|
||||||
|
};
|
||||||
|
|
||||||
|
[[nodiscard]] Character getCharacter(char c) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
FT_Library ft;
|
||||||
|
FT_Face face;
|
||||||
|
std::unordered_map<unsigned char, Character> characters;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //DICEWARS_SIEDLER_FONT_H
|
||||||
@ -28,13 +28,13 @@ void UiComponent::update(float delta) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UiComponent::collectRenderData(std::vector<GUITexture> &guiTextures) {
|
void UiComponent::collectRenderData(UiRenderBundle& uiRenderBundle) {
|
||||||
if (!visible) return;
|
if (!visible) return;
|
||||||
|
|
||||||
onCollectRenderData(guiTextures);
|
onCollectRenderData(uiRenderBundle);
|
||||||
|
|
||||||
for (const auto &child : children) {
|
for (const auto &child : children) {
|
||||||
child->collectRenderData(guiTextures);
|
child->collectRenderData(uiRenderBundle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "UiPositioner.h"
|
#include "UiPositioner.h"
|
||||||
|
#include "UiRenderBundle.h"
|
||||||
#include "../../../renderer/model/GUITexture.h"
|
#include "../../../renderer/model/GUITexture.h"
|
||||||
|
|
||||||
|
|
||||||
@ -19,7 +20,7 @@ public:
|
|||||||
void setVisible(bool visible);
|
void setVisible(bool visible);
|
||||||
[[nodiscard]] bool isVisible() const;
|
[[nodiscard]] bool isVisible() const;
|
||||||
void update(float delta);
|
void update(float delta);
|
||||||
void collectRenderData(std::vector<GUITexture>& guiTextures);
|
void collectRenderData(UiRenderBundle& uiRenderBundle);
|
||||||
virtual bool isMouseOver(float mouseX, float mouseY);
|
virtual bool isMouseOver(float mouseX, float mouseY);
|
||||||
UiComponent* parent = nullptr;
|
UiComponent* parent = nullptr;
|
||||||
UiPositioner uiPositioner;
|
UiPositioner uiPositioner;
|
||||||
@ -30,7 +31,7 @@ protected:
|
|||||||
bool visible = true;
|
bool visible = true;
|
||||||
|
|
||||||
virtual void onUpdate(float /*delta*/) {}
|
virtual void onUpdate(float /*delta*/) {}
|
||||||
virtual void onCollectRenderData(std::vector<GUITexture>&) {}
|
virtual void onCollectRenderData(UiRenderBundle& uiRenderBundle) {}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -4,8 +4,9 @@
|
|||||||
|
|
||||||
#include "UiImage.h"
|
#include "UiImage.h"
|
||||||
|
|
||||||
void UiImage::onCollectRenderData(std::vector<GUITexture> &gui_textures) {
|
void UiImage::onCollectRenderData(UiRenderBundle& renderBundle) {
|
||||||
if (!visible) return;
|
if (!visible) return;
|
||||||
Dimensions dims = uiPositioner.screenSpace;
|
Dimensions dims = uiPositioner.screenSpace;
|
||||||
gui_textures.emplace_back(textureID, glm::vec2(dims.x, dims.y), glm::vec2(dims.width, dims.height));
|
GUITexture texture(textureID, glm::vec2(dims.x, dims.y), glm::vec2(dims.width, dims.height));
|
||||||
|
renderBundle.addGUITexture(std::make_shared<GUITexture>(texture));;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,7 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void onCollectRenderData(std::vector<GUITexture> &) override;
|
void onCollectRenderData(UiRenderBundle& renderBundle) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GLuint textureID;
|
GLuint textureID;
|
||||||
|
|||||||
5
src/engine/core/gui/uiComponent/UiRenderBundle.cpp
Normal file
5
src/engine/core/gui/uiComponent/UiRenderBundle.cpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 10.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "UiRenderBundle.h"
|
||||||
40
src/engine/core/gui/uiComponent/UiRenderBundle.h
Normal file
40
src/engine/core/gui/uiComponent/UiRenderBundle.h
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 10.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef DICEWARS_SIEDLER_UIRENDERBUNDLE_H
|
||||||
|
#define DICEWARS_SIEDLER_UIRENDERBUNDLE_H
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class UiText;
|
||||||
|
#include "../../../renderer/model/GUITexture.h"
|
||||||
|
|
||||||
|
|
||||||
|
class UiRenderBundle {
|
||||||
|
public:
|
||||||
|
void addText(UiText* text) {
|
||||||
|
texts.push_back(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
void addGUITexture(const std::shared_ptr<GUITexture>& guiTexture) {
|
||||||
|
guiImages.push_back(guiTexture);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::shared_ptr<GUITexture>> getGUITextures() {
|
||||||
|
return guiImages;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<UiText*> getTexts() {
|
||||||
|
return texts;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<std::shared_ptr<GUITexture>> guiImages;
|
||||||
|
std::vector<UiText*> texts;
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //DICEWARS_SIEDLER_UIRENDERBUNDLE_H
|
||||||
20
src/engine/core/gui/uiComponent/UiText.cpp
Normal file
20
src/engine/core/gui/uiComponent/UiText.cpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 10.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "UiText.h"
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include "../text/Font.h"
|
||||||
|
|
||||||
|
UiText::UiText(Font &font, std::string text, const glm::vec2 &relativePos, const glm::vec2& relativeSize) : font(font), text(std::move(text)) {
|
||||||
|
uiPositioner.setRelativePos(relativePos);
|
||||||
|
uiPositioner.setRelativeSize(relativeSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UiText::onCollectRenderData(UiRenderBundle& ui_render_bundle) {
|
||||||
|
if (!visible) return;
|
||||||
|
|
||||||
|
ui_render_bundle.addText(this);
|
||||||
|
}
|
||||||
32
src/engine/core/gui/uiComponent/UiText.h
Normal file
32
src/engine/core/gui/uiComponent/UiText.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 10.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef DICEWARS_SIEDLER_UITEXT_H
|
||||||
|
#define DICEWARS_SIEDLER_UITEXT_H
|
||||||
|
#include "UiComponent.h"
|
||||||
|
#include "UiRenderBundle.h"
|
||||||
|
|
||||||
|
class Font;
|
||||||
|
|
||||||
|
|
||||||
|
class UiText : public UiComponent, public std::enable_shared_from_this<UiText> {
|
||||||
|
public:
|
||||||
|
UiText(Font& font, std::string text, const glm::vec2& relativePos, const glm::vec2& relativeSize);
|
||||||
|
void setText(const std::string& text);
|
||||||
|
|
||||||
|
[[nodiscard]] const Font& getFont() const { return font; }
|
||||||
|
|
||||||
|
[[nodiscard]] std::string getText() const {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void onCollectRenderData(UiRenderBundle &uiRenderBundle) override;
|
||||||
|
private:
|
||||||
|
std::string text;
|
||||||
|
Font& font;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //DICEWARS_SIEDLER_UITEXT_H
|
||||||
@ -19,7 +19,7 @@ void UiContainer::onUpdate(float x) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UiContainer::onCollectRenderData(std::vector<GUITexture> &gui_textures) {
|
void UiContainer::onCollectRenderData(UiRenderBundle& render_bundle) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,7 @@ public:
|
|||||||
UiContainer();
|
UiContainer();
|
||||||
bool isMouseOver(float mouseX, float mouseY) override;
|
bool isMouseOver(float mouseX, float mouseY) override;
|
||||||
void onUpdate(float) override;
|
void onUpdate(float) override;
|
||||||
void onCollectRenderData(std::vector<GUITexture> &) override;
|
void onCollectRenderData(UiRenderBundle& renderBundle) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "../core/gui/uiComponent/UiRenderBundle.h"
|
||||||
#include "../toolbox/MathUtils.h"
|
#include "../toolbox/MathUtils.h"
|
||||||
#include "loader/Loader.h"
|
#include "loader/Loader.h"
|
||||||
|
|
||||||
@ -15,20 +16,25 @@ GUIRenderer::GUIRenderer(Loader &loader) {
|
|||||||
rawModel = std::make_unique<RawModel>(model);
|
rawModel = std::make_unique<RawModel>(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GUIRenderer::render(const std::vector<GUITexture>& guiTextures) {
|
void GUIRenderer::render(std::vector<std::shared_ptr<GUITexture>>& gui_textures) {
|
||||||
guiShader.start();
|
guiShader.start();
|
||||||
|
glEnable(GL_BLEND);
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
glDisable(GL_DEPTH_TEST);
|
||||||
glBindVertexArray(rawModel->vaoID);
|
glBindVertexArray(rawModel->vaoID);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
for (GUITexture texture : guiTextures) {
|
for (auto texture : gui_textures) {
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
|
glBindTexture(GL_TEXTURE_2D, texture->getTextureID());
|
||||||
glm::mat4 transformationMatrix = MathUtils::createTransformationMatrix(texture.getPosition(), texture.getScale());
|
glm::mat4 transformationMatrix = MathUtils::createTransformationMatrix(texture->getPosition(), texture->getScale());
|
||||||
guiShader.loadTransformationMatrix(transformationMatrix);
|
guiShader.loadTransformationMatrix(transformationMatrix);
|
||||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, rawModel->vertexCount);
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, rawModel->vertexCount);
|
||||||
}
|
}
|
||||||
//render
|
//render
|
||||||
glDisableVertexAttribArray(0);
|
glDisableVertexAttribArray(0);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glDisable(GL_BLEND);
|
||||||
guiShader.stop();
|
guiShader.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "../core/gui/uiComponent/UiRenderBundle.h"
|
||||||
#include "model/GUITexture.h"
|
#include "model/GUITexture.h"
|
||||||
#include "model/RawModel.h"
|
#include "model/RawModel.h"
|
||||||
#include "shaders/GUIShader.h"
|
#include "shaders/GUIShader.h"
|
||||||
@ -20,7 +21,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
GUIShader guiShader;
|
GUIShader guiShader;
|
||||||
explicit GUIRenderer(Loader& loader);
|
explicit GUIRenderer(Loader& loader);
|
||||||
void render(const std::vector<GUITexture> &guiTextures);
|
void render(std::vector<std::shared_ptr<GUITexture>> &gui_textures);
|
||||||
|
|
||||||
void cleanUp();
|
void cleanUp();
|
||||||
};
|
};
|
||||||
|
|||||||
79
src/engine/renderer/TextRenderer.cpp
Normal file
79
src/engine/renderer/TextRenderer.cpp
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 11.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "TextRenderer.h"
|
||||||
|
|
||||||
|
#include "../core/Application.h"
|
||||||
|
#include "../core/Window.h"
|
||||||
|
#include "../core/gui/text/Font.h"
|
||||||
|
#include "glm/ext/matrix_clip_space.hpp"
|
||||||
|
|
||||||
|
void TextRenderer::renderTexts(const std::vector<UiText*> &textsToRender) {
|
||||||
|
shader.start();
|
||||||
|
shader.loadProjectionMatrix(calculateOrthographicProjectionMatrix());
|
||||||
|
glEnable(GL_BLEND);
|
||||||
|
glDisable(GL_DEPTH_TEST);
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
for (const auto &text : textsToRender) {
|
||||||
|
renderText(*text);
|
||||||
|
}
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glDisable(GL_BLEND);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextRenderer::renderText(const UiText &textToRender) const {
|
||||||
|
const Dimensions& d = textToRender.uiPositioner.screenSpace;
|
||||||
|
|
||||||
|
const Font& font = textToRender.getFont();
|
||||||
|
const std::string& text = textToRender.getText();
|
||||||
|
|
||||||
|
float x = d.x;
|
||||||
|
float y = d.y;
|
||||||
|
|
||||||
|
float scale = 1.0f;
|
||||||
|
|
||||||
|
for (char c : text) {
|
||||||
|
const Font::Character& ch = font.getCharacter(c);
|
||||||
|
|
||||||
|
float xpos = x + static_cast<float>(ch.bearing.x) * scale;
|
||||||
|
float ypos = y - static_cast<float>(ch.size.y - ch.bearing.y) * scale;
|
||||||
|
|
||||||
|
float w = static_cast<float>(ch.size.x) * scale;
|
||||||
|
float h = static_cast<float>(ch.size.y) * scale;
|
||||||
|
|
||||||
|
float vertices[6][4] = {
|
||||||
|
{ xpos, ypos + h, 0.0f, 0.0f },
|
||||||
|
{ xpos, ypos, 0.0f, 1.0f },
|
||||||
|
{ xpos + w, ypos, 1.0f, 1.0f },
|
||||||
|
|
||||||
|
{ xpos, ypos + h, 0.0f, 0.0f },
|
||||||
|
{ xpos + w, ypos, 1.0f, 1.0f },
|
||||||
|
{ xpos + w, ypos + h, 1.0f, 0.0f }
|
||||||
|
};
|
||||||
|
|
||||||
|
glBindVertexArray(textModel.vaoID);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, ch.textureID);
|
||||||
|
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, textModel.vboID);
|
||||||
|
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
|
||||||
|
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
|
||||||
|
x+= static_cast<float>(ch.advance >> 6) * scale;
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
|
glDisableVertexAttribArray(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::mat4 TextRenderer::calculateOrthographicProjectionMatrix() {
|
||||||
|
const auto screenWidth = static_cast<float>(Application::getInstance().getWindow().GetWidth());
|
||||||
|
const auto screenHeight = static_cast<float>(Application::getInstance().getWindow().GetHeight());
|
||||||
|
|
||||||
|
const glm::mat4 projectionMat = glm::ortho(0.f, screenWidth, 0.0f, screenHeight);
|
||||||
|
return projectionMat;
|
||||||
|
}
|
||||||
28
src/engine/renderer/TextRenderer.h
Normal file
28
src/engine/renderer/TextRenderer.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 11.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef DICEWARS_SIEDLER_TEXTRENDERER_H
|
||||||
|
#define DICEWARS_SIEDLER_TEXTRENDERER_H
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "../core/gui/uiComponent/UiText.h"
|
||||||
|
#include "loader/Loader.h"
|
||||||
|
#include "model/RawModel.h"
|
||||||
|
#include "model/TextQuadModel.h"
|
||||||
|
#include "shaders/TextShader.h"
|
||||||
|
|
||||||
|
|
||||||
|
class TextRenderer {
|
||||||
|
public:
|
||||||
|
TextRenderer() : textModel(Loader::instance().loadTextModel()) {};
|
||||||
|
void renderTexts(const std::vector<UiText *> &textsToRender);
|
||||||
|
void renderText(const UiText &textToRender) const;
|
||||||
|
private:
|
||||||
|
TextShader shader;
|
||||||
|
TextQuadModel textModel;
|
||||||
|
static glm::mat4 calculateOrthographicProjectionMatrix();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //DICEWARS_SIEDLER_TEXTRENDERER_H
|
||||||
@ -51,6 +51,24 @@ ModelTexture Loader::loadTextureFromFile(const std::string &path) {
|
|||||||
return ModelTexture(loadTexture(path));
|
return ModelTexture(loadTexture(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextQuadModel Loader::loadTextModel() {
|
||||||
|
GLuint vaoID = createVAO();
|
||||||
|
GLuint vboID;
|
||||||
|
glGenBuffers(1, &vboID);
|
||||||
|
vboIDs.push_back(vboID);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, vboID);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, nullptr, GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer(0, 4 , GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
glBindVertexArray(0);
|
||||||
|
|
||||||
|
return {vaoID, vboID, 4};
|
||||||
|
}
|
||||||
|
|
||||||
GLuint Loader::loadTexture(const std::string &path) {
|
GLuint Loader::loadTexture(const std::string &path) {
|
||||||
Texture2D texture = TextureLoader::loadTexture(path);
|
Texture2D texture = TextureLoader::loadTexture(path);
|
||||||
textureIDs.push_back(texture.id);
|
textureIDs.push_back(texture.id);
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "../model/RawModel.h"
|
#include "../model/RawModel.h"
|
||||||
|
#include "../model/TextQuadModel.h"
|
||||||
#include "../textures/ModelTexture.h"
|
#include "../textures/ModelTexture.h"
|
||||||
#include "glad/glad.h"
|
#include "glad/glad.h"
|
||||||
|
|
||||||
@ -23,6 +24,7 @@ public:
|
|||||||
void cleanUp();
|
void cleanUp();
|
||||||
|
|
||||||
ModelTexture loadTextureFromFile(const std::string& path);
|
ModelTexture loadTextureFromFile(const std::string& path);
|
||||||
|
TextQuadModel loadTextModel();
|
||||||
private:
|
private:
|
||||||
GLuint createVAO();
|
GLuint createVAO();
|
||||||
GLuint loadTexture(const std::string &path);
|
GLuint loadTexture(const std::string &path);
|
||||||
|
|||||||
5
src/engine/renderer/model/TextQuadModel.cpp
Normal file
5
src/engine/renderer/model/TextQuadModel.cpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 11.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "TextQuadModel.h"
|
||||||
19
src/engine/renderer/model/TextQuadModel.h
Normal file
19
src/engine/renderer/model/TextQuadModel.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 11.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef DICEWARS_SIEDLER_TEXTQUADMODEL_H
|
||||||
|
#define DICEWARS_SIEDLER_TEXTQUADMODEL_H
|
||||||
|
#include "glad/glad.h"
|
||||||
|
|
||||||
|
|
||||||
|
class TextQuadModel {
|
||||||
|
public:
|
||||||
|
TextQuadModel(GLuint vaoID, GLuint vboID, int vertexCount) : vaoID(vaoID), vboID(vboID), vertexCount(vertexCount) {};
|
||||||
|
GLuint vaoID;
|
||||||
|
GLuint vboID;
|
||||||
|
int vertexCount;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //DICEWARS_SIEDLER_TEXTQUADMODEL_H
|
||||||
17
src/engine/renderer/shaders/TextShader.cpp
Normal file
17
src/engine/renderer/shaders/TextShader.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 11.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "TextShader.h"
|
||||||
|
|
||||||
|
void TextShader::loadProjectionMatrix(glm::mat4 matrix) {
|
||||||
|
ShaderProgram::loadMatrix(location_projectionMatrix, matrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextShader::getAllUniformLocations() {
|
||||||
|
location_projectionMatrix = ShaderProgram::getUniformLocation("projectionMatrix");
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextShader::bindAttributes() const {
|
||||||
|
ShaderProgram::bindAttribute(0, "vertices");
|
||||||
|
}
|
||||||
29
src/engine/renderer/shaders/TextShader.h
Normal file
29
src/engine/renderer/shaders/TextShader.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 11.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef DICEWARS_SIEDLER_TEXTSHADER_H
|
||||||
|
#define DICEWARS_SIEDLER_TEXTSHADER_H
|
||||||
|
#include "ShaderProgram.h"
|
||||||
|
|
||||||
|
|
||||||
|
class TextShader : public ShaderProgram {
|
||||||
|
public :
|
||||||
|
TextShader() : ShaderProgram(VERTEX_FILE, FRAGMENT_FILE) {
|
||||||
|
TextShader::bindAttributes();
|
||||||
|
TextShader::getAllUniformLocations();
|
||||||
|
}
|
||||||
|
void loadProjectionMatrix(glm::mat4 matrix);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void getAllUniformLocations() override;
|
||||||
|
void bindAttributes() const override;
|
||||||
|
private:
|
||||||
|
inline static const std::string VERTEX_FILE = "assets/shaders/textVertexShader.glsl";
|
||||||
|
inline static const std::string FRAGMENT_FILE = "assets/shaders/textFragmentShader.glsl";
|
||||||
|
|
||||||
|
int location_projectionMatrix;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //DICEWARS_SIEDLER_TEXTSHADER_H
|
||||||
@ -42,6 +42,7 @@ namespace MathUtils {
|
|||||||
glm::vec3 up(0.0f, 1.0f, 0.0f);
|
glm::vec3 up(0.0f, 1.0f, 0.0f);
|
||||||
return glm::lookAt(pos, pos + front, up);
|
return glm::lookAt(pos, pos + front, up);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //MATHUTILS_H
|
#endif //MATHUTILS_H
|
||||||
|
|||||||
@ -4,13 +4,16 @@
|
|||||||
|
|
||||||
#include "UILayer.h"
|
#include "UILayer.h"
|
||||||
|
|
||||||
|
#include "../engine/core/gui/text/Font.h"
|
||||||
#include "../engine/core/gui/uiComponent/UiImage.h"
|
#include "../engine/core/gui/uiComponent/UiImage.h"
|
||||||
|
#include "../engine/core/gui/uiComponent/UiText.h"
|
||||||
#include "../engine/renderer/loader/Loader.h"
|
#include "../engine/renderer/loader/Loader.h"
|
||||||
#include "../engine/renderer/model/GUITexture.h"
|
#include "../engine/renderer/model/GUITexture.h"
|
||||||
|
|
||||||
UILayer::UILayer() {
|
UILayer::UILayer() {
|
||||||
Loader& loader = Loader::instance();
|
Loader& loader = Loader::instance();
|
||||||
guiRenderer = std::make_unique<GUIRenderer>(loader);
|
guiRenderer = std::make_unique<GUIRenderer>(loader);
|
||||||
|
textRenderer = std::make_unique<TextRenderer>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UILayer::onAttach() {
|
void UILayer::onAttach() {
|
||||||
@ -25,17 +28,29 @@ void UILayer::onAttach() {
|
|||||||
glm::vec2(0.5f, 0.5f), glm::vec2(0.5f)
|
glm::vec2(0.5f, 0.5f), glm::vec2(0.5f)
|
||||||
);
|
);
|
||||||
rootContainer->addChild(std::move(image));
|
rootContainer->addChild(std::move(image));
|
||||||
|
|
||||||
|
|
||||||
|
Font myFont("/usr/share/fonts/TTF/DejaVuSans.ttf", 48);
|
||||||
|
font = std::make_unique<Font>(myFont);
|
||||||
|
auto text = std::make_unique<UiText>(*font, "Hello World!", glm::vec2(0.5f, 0.5f), glm::vec2(0.3f, 0.1f));
|
||||||
|
|
||||||
|
rootContainer->addChild(std::move(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
void UILayer::onUpdate() {
|
void UILayer::onUpdate() {
|
||||||
Layer::onUpdate();
|
Layer::onUpdate();
|
||||||
std::vector<GUITexture> guiTextures;
|
UiRenderBundle renderBundle;
|
||||||
if (rootContainer) {
|
if (rootContainer) {
|
||||||
rootContainer->collectRenderData(guiTextures);
|
rootContainer->collectRenderData(renderBundle);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Found UI Textures: %lu\n", guiTextures.size());
|
auto guis = renderBundle.getGUITextures();
|
||||||
guiRenderer->render(guiTextures);
|
guiRenderer->render(guis);
|
||||||
|
|
||||||
|
auto texts = renderBundle.getTexts();
|
||||||
|
|
||||||
|
textRenderer->renderTexts(texts);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UILayer::onDetach() {
|
void UILayer::onDetach() {
|
||||||
|
|||||||
@ -6,15 +6,18 @@
|
|||||||
#define UILAYER_H
|
#define UILAYER_H
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include "../engine/core/gui/text/Font.h"
|
||||||
#include "../engine/core/gui/uiMain/UiContainer.h"
|
#include "../engine/core/gui/uiMain/UiContainer.h"
|
||||||
#include "../engine/layer/Layer.h"
|
#include "../engine/layer/Layer.h"
|
||||||
#include "../engine/renderer/GUIRenderer.h"
|
#include "../engine/renderer/GUIRenderer.h"
|
||||||
|
#include "../engine/renderer/TextRenderer.h"
|
||||||
|
|
||||||
|
|
||||||
class UILayer : public Layer{
|
class UILayer : public Layer{
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<GUIRenderer> guiRenderer;
|
std::unique_ptr<GUIRenderer> guiRenderer;
|
||||||
|
std::unique_ptr<TextRenderer> textRenderer;
|
||||||
|
std::unique_ptr<Font> font;
|
||||||
std::unique_ptr<UiContainer> rootContainer;
|
std::unique_ptr<UiContainer> rootContainer;
|
||||||
public:
|
public:
|
||||||
UILayer();
|
UILayer();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user