Skip to content

Commit

Permalink
A proper Hello World text! Majority of work related to text is now done
Browse files Browse the repository at this point in the history
- a drawing fuction inside renderer2d can now render a string
- a allocate function can now send proper data from said draw function to the gpu
- text buffers are created, shaders are created using msdf instructions
- more work needs to be done to clean up, more test need also to be done (more fonts, different texts and symbols, etc)
  • Loading branch information
JaoSchmidt committed Nov 19, 2024
1 parent 1ed9845 commit 1e927dd
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 32 deletions.
38 changes: 23 additions & 15 deletions Example/textRendering/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,49 @@ class ShapesController : public pain::ImGuiInstance
const void onImGuiUpdate() override
{
ImGui::Begin("Shapes Controller");
ImGui::Image((ImTextureID)m_font.getAtlasTexture().getRendererId(),
{512, 512}, {0, 1}, {1, 0});
// ImGui::Image((ImTextureID)m_font.getAtlasTexture().getRendererId(),
// {512, 512}, {0, 1}, {1, 0});
ImGui::End();
}
ShapesController(pain::Font &font) : m_font(font) {}
pain::Font &m_font;
// ShapesController(pain::Font &font) : m_font(font) {}
// pain::Font &m_font;
};

class MainScene : public pain::Scene
{
public:
MainScene() : pain::Scene(), m_font{"resources/fonts/Arial.ttf"} {}
MainScene()
: pain::Scene(),
m_font(std::make_shared<pain::Font>("resources/fonts/Arial.ttf"))
{
}
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(m_font);
ShapesController *sc = new ShapesController();
pain::Application::Get().addImGuiInstance(sc);
m_sc = sc;

m_textureAtlas = std::make_shared<pain::Texture>(m_font->getAtlasTexture());
}
void onUpdate(double deltaTimeSec) override {}
void onRender(double currentTime) override
{
pain::Renderer2d::drawQuad({-0.5f, 0.0f}, {0.3f, 0.3f},
{0.8f, 0.9f, 0.3f, 1.0f});
pain::Renderer2d::drawString({-0.5, -0.5}, "Hello World", m_font,
{0.7f, 0.2f, 0.8f, 1.f});
// 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);
}
void onEvent(const SDL_Event &event) override {}

private:
pain::Font m_font;
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 All @@ -71,10 +79,10 @@ pain::Application *pain::CreateApplication()
{
LOG_T("Creating app");
const char *title = "Developing Pain - Example 2d";
const int width = 800;
const int height = 600;
// const int width = 1280;
// const int height = 720;
// const int width = 800;
// const int height = 600;
const int width = 1280;
const int height = 768;
return new Sandbox(title, width, height);
}

Expand Down
4 changes: 3 additions & 1 deletion Pain/include/CoreRender/Renderer/Renderer2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ 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);
const Font &font, const glm::vec4 &color,
std::shared_ptr<Texture> &t);

static const glm::mat4 getTransform(const glm::vec2 &position,
const glm::vec2 &size,
Expand Down Expand Up @@ -176,6 +177,7 @@ class EXPORT Renderer2d
static ParticleVertex *m_sprayVertexBufferBase;
static ParticleVertex *m_sprayVertexBufferPtr;
static uint32_t m_sprayIndexCount;
const static Texture *m_fontAtlasTexture;

// TODO:(jao) search MaxTextureSlots dinamically (i.e TMU value on gpu)
static const uint32_t MaxTextureSlots = 32;
Expand Down
2 changes: 2 additions & 0 deletions Pain/include/CoreRender/Text/Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ class Font
{
return m_glyphs;
};
static Font *getDefault();

private:
// variables
msdf_atlas::FontGeometry m_fontGeometry;
std::vector<msdf_atlas::GlyphGeometry> m_glyphs;
Texture m_atlasTexture;
static Font *m_defaultFont;

// consts
const char *textureKey = "fontAltas";
Expand Down
6 changes: 4 additions & 2 deletions Pain/include/CoreRender/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Texture
ImageFormat format = ImageFormat::RGBA8);
Texture(Texture &t);
~Texture();
void bind(uint32_t slot = 0) const;
void bindToSlot(uint32_t slot = 0) const;
void bind() const;
void setData(const void *data, uint32_t size);
bool operator==(const Texture &other) const;

Expand All @@ -24,7 +25,8 @@ class Texture
const uint32_t getHeight() const { return m_height; }

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

private:
std::string m_path;
Expand Down
4 changes: 3 additions & 1 deletion Pain/resources/shaders/Renderer2dText.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ void main()

if (opacity == 0.0)
discard;
vec4 bgColor = vec4(0.0);
vec4 bgColor = vec4(0.2,0.2,0.2,1.0);
o_Color = mix(bgColor, v_Color, opacity);
if (o_Color.a == 0.0)
discard;

// o_Color = texture(u_FontAtlas, v_TexCoord) * v_Color;
}
14 changes: 7 additions & 7 deletions Pain/src/CoreRender/Renderer/Draw2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ std::shared_ptr<Shader> Renderer2d::m_textTextureShader = nullptr;
TextQuadVertex *Renderer2d::m_textVertexBufferBase = nullptr;
TextQuadVertex *Renderer2d::m_textVertexBufferPtr = nullptr;
uint32_t Renderer2d::m_textIndexCount = 0; // at init, there are 0 texts
const Texture *Renderer2d::m_fontAtlasTexture = nullptr;

// tri initializer
std::shared_ptr<VertexArray> Renderer2d::m_triVertexArray = nullptr;
Expand Down Expand Up @@ -185,7 +186,7 @@ void Renderer2d::drawBatches(const glm::mat4 &viewProjectionMatrix)

// bind textures
for (uint32_t i = 0; i < m_textureSlotIndex; i++)
m_textureSlots[i]->bind(i);
m_textureSlots[i]->bindToSlot(i);

m_quadTextureShader->bind();
const uint32_t quadCount =
Expand All @@ -203,11 +204,10 @@ void Renderer2d::drawBatches(const glm::mat4 &viewProjectionMatrix)
(uint8_t *)m_textVertexBufferPtr - (uint8_t *)m_textVertexBufferBase;
m_textVertexBuffer->setData((void *)m_textVertexBufferBase, textDataSize);

// bind textures
for (uint32_t i = 0; i < m_textureSlotIndex; i++)
m_textureSlots[i]->bind(i);

m_fontAtlasTexture->bind();
m_textTextureShader->bind();
m_textTextureShader->uploadUniformInt("u_FontAtlas", 0);

const uint32_t textCount =
m_textIndexCount ? m_textIndexCount
: m_textVertexArray->getIndexBuffer()->getCount();
Expand All @@ -227,7 +227,7 @@ void Renderer2d::drawBatches(const glm::mat4 &viewProjectionMatrix)

// bind textures
for (uint32_t i = 0; i < m_textureSlotIndex; i++)
m_textureSlots[i]->bind(i);
m_textureSlots[i]->bindToSlot(i);

m_sprayShader->bind();
const uint32_t sprayCount =
Expand Down Expand Up @@ -257,7 +257,7 @@ void Renderer2d::drawBatches(const glm::mat4 &viewProjectionMatrix)
void Renderer2d::bindTextures()
{
for (uint32_t i = 0; i < m_textureSlotIndex; i++)
m_textureSlots[i]->bind(i);
m_textureSlots[i]->bindToSlot(i);
}

void Renderer2d::goBackToFirstVertex()
Expand Down
20 changes: 18 additions & 2 deletions Pain/src/CoreRender/Renderer/Renderer2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ void Renderer2d::beginScene(float globalTime, const glm::mat4 &transform)
m_sprayShader->uploadUniformMat4("u_Transform", transform);
m_sprayShader->uploadUniformFloat("u_Time", globalTime);

m_textTextureShader->bind();
m_textTextureShader->uploadUniformMat4(
"u_ViewProjection", m_cameraEntity->getComponent<OrthoCameraComponent>()
.m_camera->getViewProjectionMatrix());
m_textTextureShader->uploadUniformMat4("u_Transform", transform);

goBackToFirstVertex();
}

Expand Down Expand Up @@ -153,14 +159,15 @@ 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)
const Font &font, const glm::vec4 &color,
std::shared_ptr<Texture> &t)
{

PLOG_I("Hello");
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();

double x = 0.0;
double fsScale = 1.0 / (metrics.ascenderY - metrics.descenderY);
Expand Down Expand Up @@ -206,6 +213,7 @@ void Renderer2d::drawString(const glm::vec2 &position, const char *string,
float texelHeight = 1.0f / fontAtlas.getHeight();
texCoordMin *= glm::vec2(texelWidth, texelHeight);
texCoordMax *= glm::vec2(texelWidth, texelHeight);

allocateCharacter(glm::translate(glm::mat4(1.f), {position, 0.f}), color,
// textureCoordinate
{texCoordMin, glm::vec2(texCoordMin.x, texCoordMax.y),
Expand All @@ -215,6 +223,14 @@ void Renderer2d::drawString(const glm::vec2 &position, const char *string,
glm::vec4{quadMin.x, quadMax.y, 0.f, 1.f},
glm::vec4{quadMax, 0.f, 1.f},
glm::vec4{quadMax.x, quadMin.y, 0.f, 1.f}});

if (*t != '\0') {
double advance = glyph->getAdvance();
char nextCharacter = *(t + 1);
fontGeometry.getAdvance(advance, *t, nextCharacter);
float kerningOffset = 0.0f;
x += fsScale * advance + kerningOffset;
}
break;
}
}
Expand Down
9 changes: 7 additions & 2 deletions Pain/src/CoreRender/Text/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ Font::createAtlasTexture(const char *fontName, float fontSize,
generator.setAttributes(attributes);
generator.setThreadCount(Application::getProcessorCount() / 2);
generator.generate(glyphs.data(), (int)glyphs.size());
msdfgen::savePng(generator.atlasStorage(), "output.png");
// msdfgen::savePng(generator.atlasStorage(), "output.png");
msdfgen::BitmapConstRef<T, N> bitmap =
(msdfgen::BitmapConstRef<T, N>)generator.atlasStorage();

Texture texture = Texture(bitmap.width, bitmap.height, ImageFormat::RGB8);
PLOG_I("size ({},{})", bitmap.width, bitmap.height);
texture.setData((void *)bitmap.pixels, bitmap.width * bitmap.height * 3);
return texture;
}
Expand All @@ -112,4 +111,10 @@ msdf_atlas::Charset Font::getLatinCharset()
return charset;
}

Font *Font::getDefault()
{
static Font m_defaultFont("resources/fonts/Arial.ttf");
return &m_defaultFont;
}

} // namespace pain
8 changes: 6 additions & 2 deletions Pain/src/CoreRender/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ Texture::Texture(uint32_t width, uint32_t height, ImageFormat format)
m_dataFormat = getGLDataFormat(format);

glCreateTextures(GL_TEXTURE_2D, 1, &m_rendererId);
glBindTexture(GL_TEXTURE_2D, m_rendererId);
glTextureStorage2D(m_rendererId, 1, m_internalFormat, m_width, m_height);

glTextureParameteri(m_rendererId, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(m_rendererId, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTextureParameteri(m_rendererId, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTextureParameteri(m_rendererId, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(m_rendererId, GL_TEXTURE_WRAP_T, GL_REPEAT);
Expand Down Expand Up @@ -68,10 +69,13 @@ void Texture::setData(const void *data, uint32_t size)
glTextureSubImage2D(m_rendererId, 0, 0, 0, m_width, m_height, m_dataFormat,
GL_UNSIGNED_BYTE, data);
}
void Texture::bind(uint32_t slot) const

// bind texture unit i.e. has a slot in a sample array
void Texture::bindToSlot(uint32_t slot) const
{
glBindTextureUnit(slot, m_rendererId);
}
void Texture::bind() const { glBindTexture(GL_TEXTURE_2D, m_rendererId); }
Texture::~Texture() { glDeleteTextures(1, &m_rendererId); }

} // namespace pain

0 comments on commit 1e927dd

Please sign in to comment.