Skip to content

Commit

Permalink
#5 progess beyond on text rendering
Browse files Browse the repository at this point in the history
- The texture on draw function's parameter is now a reference instead of a shared_ptr
- Erros where detected and listed on issue #5

I create this commit to shove text rendering on the side for now and focus at python scripting
  • Loading branch information
JaoSchmidt committed Dec 8, 2024
1 parent 1e927dd commit a08091d
Show file tree
Hide file tree
Showing 17 changed files with 183 additions and 71 deletions.
File renamed without changes.
36 changes: 18 additions & 18 deletions Example/textRendering/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "imgui.h"
#include <pain.h>

#include <glm/ext/matrix_transform.hpp>
Expand All @@ -10,49 +11,48 @@ class ShapesController : public pain::ImGuiInstance
const void onImGuiUpdate() override
{
ImGui::Begin("Shapes Controller");
// ImGui::Image((ImTextureID)m_font.getAtlasTexture().getRendererId(),
// ImGui::Image((ImTextureID)m_font->getAtlasTexture().getRendererId(),
// {512, 512}, {0, 1}, {1, 0});
ImGui::InputFloat("emShape", &m_emSize);
if (ImGui::Button("Reset texture")) {
m_font.reset(pain::Font::create(
"resources/default/fonts/Epilogue-Black.ttf", m_emSize));
}
ImGui::End();
}
// ShapesController(pain::Font &font) : m_font(font) {}
// pain::Font &m_font;
float m_emSize = 0;
ShapesController(pain::Font *font) : m_font(font) {}
std::unique_ptr<pain::Font> m_font;
};

class MainScene : public pain::Scene
{
public:
MainScene()
: pain::Scene(),
m_font(std::make_shared<pain::Font>("resources/fonts/Arial.ttf"))
{
}
MainScene() : pain::Scene() {}
void init(std::shared_ptr<pain::OrthoCameraEntity> pCamera)
{
m_orthocamera = pCamera;
pain::Renderer2d::init(m_orthocamera);
auto &a = m_orthocamera->addComponent<pain::NativeScriptComponent>();
a.bind<pain::OrthoCameraController>();

ShapesController *sc = new ShapesController();
ShapesController *sc = new ShapesController(
pain::Font::create("resources/default/fonts/Epilogue-Black.ttf", 40.0));
pain::Application::Get().addImGuiInstance(sc);
m_sc = sc;

m_textureAtlas = std::make_shared<pain::Texture>(m_font->getAtlasTexture());
// *m_texture = m_font->getAtlasTexture();
}
void onUpdate(double deltaTimeSec) override {}
void onRender(double currentTime) override
{
// pain::Renderer2d::drawQuad({0.5f, 0.5f}, {0.3f, 0.3f},
// {0.8f, 0.9f, 0.3f, 1.0f},
// m_textureAtlas, 1.f);
pain::Renderer2d::drawString({-0.8f, 0.f}, "Hello World", *m_font.get(),
{1.f, 1.f, 1.f, 1.f}, m_textureAtlas);
pain::Renderer2d::drawQuad({0.5f, 0.5f}, {0.3f, 0.3f}, {1.f, 1.f, 1.f, 1.f},
m_sc->m_font->getAtlasTexture(), 1.f);
// pain::Renderer2d::drawString({-0.8f, 0.f}, "Hello World", *m_font.get(),
// {1.f, 1.f, 1.f, 1.f});
}
void onEvent(const SDL_Event &event) override {}

private:
std::shared_ptr<pain::Texture> m_textureAtlas;
std::shared_ptr<pain::Font> m_font;
ShapesController *m_sc;
std::shared_ptr<pain::OrthoCameraEntity> m_orthocamera;
};
Expand Down
24 changes: 18 additions & 6 deletions Pain/include/CoreRender/Renderer/Renderer2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,33 @@ class EXPORT Renderer2d
/** Draws a quad */
static void drawQuad(const glm::vec2 &position, const glm::vec2 &size,
const glm::vec4 &tintColor,
const std::shared_ptr<Texture> &texture = nullptr,
const Texture *texture = nullptr,
const float tilingFactor = 1.0f,
const std::array<glm::vec2, 4> &textureCoordinate = {
glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f),
glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f)});
static void drawQuad(const glm::vec2 &position, const glm::vec2 &size,
const glm::vec4 &tintColor, const Texture &texture,
const float tilingFactor = 1.0f,
const std::array<glm::vec2, 4> &textureCoordinate = {
glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f),
glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f)});

/** Draws a quad with rotation, new rotationRadians (angle)*/
static void drawQuad(const glm::vec2 &position, const glm::vec2 &size,
const glm::vec4 &tintColor, const float rotationRadians,
const std::shared_ptr<Texture> &texture = nullptr,
const Texture *texture = nullptr,
const float tilingFactor = 1.0f,
const std::array<glm::vec2, 4> &textureCoordinate = {
glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f),
glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f)});

static void drawQuad(const glm::vec2 &position, const glm::vec2 &size,
const glm::vec4 &tintColor, const float rotationRadians,
const Texture &texture, const float tilingFactor = 1.0f,
const std::array<glm::vec2, 4> &textureCoordinate = {
glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f),
glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f)});
// ================================================================= //
// Draw Tri
// ================================================================= //
Expand All @@ -103,8 +116,7 @@ class EXPORT Renderer2d

/** Draws a string of glyphs from a font atlas */
static void drawString(const glm::vec2 &position, const char *string,
const Font &font, const glm::vec4 &color,
std::shared_ptr<Texture> &t);
const Font &font, const glm::vec4 &color);

static const glm::mat4 getTransform(const glm::vec2 &position,
const glm::vec2 &size,
Expand All @@ -119,7 +131,7 @@ class EXPORT Renderer2d
// ================================================================= //
static void initBatches();
static void bindTextures();
static float allocateTextures(const std::shared_ptr<Texture> &texture);
static float allocateTextures(const Texture &texture);

static void allocateQuad(const glm::mat4 &transform,
const glm::vec4 &tintColor, const float tilingFactor,
Expand Down Expand Up @@ -183,7 +195,7 @@ class EXPORT Renderer2d
static const uint32_t MaxTextureSlots = 32;

static std::shared_ptr<Texture> m_whiteTexture;
static std::array<std::shared_ptr<Texture>, MaxTextureSlots> m_textureSlots;
static std::array<const Texture *, MaxTextureSlots> m_textureSlots;

static uint32_t m_textureSlotIndex;
// clang-format off
Expand Down
10 changes: 7 additions & 3 deletions Pain/include/CoreRender/Text/Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ namespace pain
class Font
{
public:
Font(const char *fontFilename);
static Font *create(const char *fontFilename);
static Font *create(const char *fontFilename, double emSize);
const inline Texture &getAtlasTexture() const { return m_atlasTexture; };
inline Texture &getAtlasTexture() { return m_atlasTexture; };
const inline msdf_atlas::FontGeometry &getFontGeometry() const
{
return m_fontGeometry;
Expand All @@ -36,15 +38,17 @@ class Font
static constexpr double maxCornerAngle = 3.0;

// methods
Texture generateAtlas(const char *fontFilename);
Texture generateAtlas(const char *fontFilename, double emSize);
msdf_atlas::Charset getLatinCharset();

template <typename T, typename S, int N,
msdf_atlas::GeneratorFunction<S, N> GenFunc>
Texture
createAtlasTexture(const char *fontName, float fontSize,
createAtlasTexture(const char *fontName,
const std::vector<msdf_atlas::GlyphGeometry> &glyphs,
const msdf_atlas::FontGeometry &fontGeometry,
const float width, const float height);
Font(const char *fontFilename, double emSize);
};

} // namespace pain
11 changes: 11 additions & 0 deletions Pain/include/CoreRender/Text/FontManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

namespace pain
{
class FontManager
{
public:
FontManager(const char *fontFilename);
};

} // namespace pain
7 changes: 5 additions & 2 deletions Pain/include/CoreRender/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ class Texture
void bind() const;
void setData(const void *data, uint32_t size);
bool operator==(const Texture &other) const;
Texture clone();

const uint32_t getRendererId() const { return m_rendererId; }
const uint32_t getWidth() const { return m_width; }
const uint32_t getHeight() const { return m_height; }

MOVABLES(Texture);
// NONCOPYABLE(Texture);
COPIES(Texture);
NONCOPYABLE(Texture);
// COPIES(Texture);

private:
std::string m_path;
Expand Down Expand Up @@ -81,6 +82,8 @@ class Texture
"Missing entry in types array");
return types[static_cast<uint32_t>(format)];
}
Texture(std::string &path, uint32_t width, uint32_t height,
uint32_t dataFormat, uint32_t internalFormat, uint32_t rendererId);
};

} // namespace pain
4 changes: 2 additions & 2 deletions Pain/include/ECS/Components/Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace pain
struct SpriteComponent {
glm::vec2 m_size{0.1f, 0.1f};
glm::vec4 m_color{1.0f, 1.0f, 1.0f, 1.0f};
std::shared_ptr<Texture> m_ptexture;
Texture *m_ptexture;
const float m_tilingFactor;
SpriteComponent(const glm::vec2 &size, const glm::vec4 &color,
float tilingFactor, std::shared_ptr<Texture> ptexture)
float tilingFactor, Texture *ptexture)
: m_size(size), m_color(color), m_ptexture(ptexture),
m_tilingFactor(tilingFactor)
{
Expand Down
4 changes: 2 additions & 2 deletions Pain/include/Misc/BasicShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
namespace pain
{

class EXPORT RectangleSprite : public GameObject
class RectangleSprite : public GameObject
{
public:
RectangleSprite(Scene *scene, const glm::vec2 &position,
const glm::vec2 &size, const glm::vec4 &color,
const std::shared_ptr<Texture> &ptexture, float tilingFactor);
Texture *ptexture, float tilingFactor);

void onUpdate(double dt) {};
void onEvent(const SDL_Event &e) {};
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 8 additions & 6 deletions Pain/src/CoreRender/Renderer/Draw2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ uint32_t Renderer2d::m_sprayIndexCount = 0;

// texture initializer
std::shared_ptr<Texture> Renderer2d::m_whiteTexture = nullptr;
std::array<std::shared_ptr<Texture>, Renderer2d::MaxTextureSlots>
std::array<const Texture *, Renderer2d::MaxTextureSlots>
Renderer2d::m_textureSlots = {nullptr};
uint32_t Renderer2d::m_textureSlotIndex =
1; // at init, there is 1 white texture
Expand Down Expand Up @@ -93,7 +93,7 @@ void Renderer2d::initBatches()
m_quadTextureShader->bind();
m_quadTextureShader->uploadUniformIntArray("u_Textures", samplers,
MaxTextureSlots);
m_textureSlots[0] = m_whiteTexture;
m_textureSlots[0] = m_whiteTexture.get();

// =============================================================== //
// Triangles
Expand Down Expand Up @@ -185,8 +185,10 @@ void Renderer2d::drawBatches(const glm::mat4 &viewProjectionMatrix)
m_quadVertexBuffer->setData((void *)m_quadVertexBufferBase, quadDataSize);

// bind textures
for (uint32_t i = 0; i < m_textureSlotIndex; i++)
for (uint32_t i = 0; i < m_textureSlotIndex; i++) {
PLOG_I("bind texture id = {}", m_textureSlots[i]->getRendererId());
m_textureSlots[i]->bindToSlot(i);
}

m_quadTextureShader->bind();
const uint32_t quadCount =
Expand Down Expand Up @@ -275,21 +277,21 @@ void Renderer2d::goBackToFirstVertex()
m_textVertexBufferPtr = m_textVertexBufferBase;
}

float Renderer2d::allocateTextures(const std::shared_ptr<Texture> &texture)
float Renderer2d::allocateTextures(const Texture &texture)
{
// NOTE: this can be optimized later to avoid searching the texture
float textureIndex = 0.0f;
// tries to get texture from the m_textureSlots
for (uint32_t i = 1; i < m_textureSlotIndex; i++) {
if (*m_textureSlots[i].get() == *texture.get()) {
if (*m_textureSlots[i] == texture) {
textureIndex = (float)i;
break;
}
}
// otherwise use it to allocate new texture
if (textureIndex == 0.0f) {
textureIndex = (float)m_textureSlotIndex;
m_textureSlots[m_textureSlotIndex] = texture;
m_textureSlots[m_textureSlotIndex] = &texture;
m_textureSlotIndex++;
}
P_ASSERT_W(textureIndex != 0.0f,
Expand Down
43 changes: 31 additions & 12 deletions Pain/src/CoreRender/Renderer/Renderer2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,46 @@ void Renderer2d::drawIndexed(const std::shared_ptr<VertexArray> &vertexArray,

void Renderer2d::drawQuad(const glm::vec2 &position, const glm::vec2 &size,
const glm::vec4 &tintColor,
const std::shared_ptr<Texture> &texture,
float tilingFactor, // will prevent fancy tiling
const Texture *texture, // Raw pointer version
float tilingFactor,
const std::array<glm::vec2, 4> &textureCoordinate)
{
const float texIndex =
texture ? allocateTextures(texture) : 0.0f; // White Texture if nullptr
texture ? allocateTextures(*texture) : 0.0f; // White texture if nullptr
const glm::mat4 transform = getTransform(position, size);
allocateQuad(transform, tintColor, tilingFactor, texIndex, textureCoordinate);
}

void Renderer2d::drawQuad(const glm::vec2 &position, const glm::vec2 &size,
const glm::vec4 &tintColor,
const Texture &texture, // Reference version
float tilingFactor,
const std::array<glm::vec2, 4> &textureCoordinate)
{
const float texIndex = allocateTextures(texture);
const glm::mat4 transform = getTransform(position, size);
allocateQuad(transform, tintColor, tilingFactor, texIndex, textureCoordinate);
}

void Renderer2d::drawQuad(const glm::vec2 &position, const glm::vec2 &size,
const glm::vec4 &tintColor,
const float rotationRadians,
const std::shared_ptr<Texture> &texture,
const float rotationRadians, const Texture &texture,
float tilingFactor,
const std::array<glm::vec2, 4> &textureCoordinate)
{
const float texIndex = allocateTextures(texture);
const glm::mat4 transform = getTransform(position, size, rotationRadians);
allocateQuad(transform, tintColor, tilingFactor, texIndex, textureCoordinate);
}

void Renderer2d::drawQuad(const glm::vec2 &position, const glm::vec2 &size,
const glm::vec4 &tintColor,
const float rotationRadians, const Texture *texture,
float tilingFactor,
const std::array<glm::vec2, 4> &textureCoordinate)
{
const float texIndex =
texture ? allocateTextures(texture) : 0.0f; // White Texture if nullptr
texture ? allocateTextures(*texture) : 0.0f; // White Texture if nullptr
const glm::mat4 transform = getTransform(position, size, rotationRadians);
allocateQuad(transform, tintColor, tilingFactor, texIndex, textureCoordinate);
}
Expand Down Expand Up @@ -159,15 +180,13 @@ void Renderer2d::drawSprayParticle(const Particle &p)
// Draw Text
// ================================================================= //
void Renderer2d::drawString(const glm::vec2 &position, const char *string,
const Font &font, const glm::vec4 &color,
std::shared_ptr<Texture> &t)
const Font &font, const glm::vec4 &color)
{

const auto &fontGeometry = font.getFontGeometry();
const auto &metrics = fontGeometry.getMetrics();
const Texture &fontAtlas = font.getAtlasTexture();
const double &spaceGlyphAdvance = fontGeometry.getGlyph(' ')->getAdvance();
m_fontAtlasTexture = t.get();
m_fontAtlasTexture = &font.getAtlasTexture();

double x = 0.0;
double fsScale = 1.0 / (metrics.ascenderY - metrics.descenderY);
Expand Down Expand Up @@ -209,8 +228,8 @@ void Renderer2d::drawString(const glm::vec2 &position, const char *string,
// offset
quadMin += glm::vec2(x, y);
quadMax += glm::vec2(x, y);
float texelWidth = 1.0f / fontAtlas.getWidth();
float texelHeight = 1.0f / fontAtlas.getHeight();
float texelWidth = 1.0f / m_fontAtlasTexture->getWidth();
float texelHeight = 1.0f / m_fontAtlasTexture->getHeight();
texCoordMin *= glm::vec2(texelWidth, texelHeight);
texCoordMax *= glm::vec2(texelWidth, texelHeight);

Expand Down
Loading

0 comments on commit a08091d

Please sign in to comment.