Fixed font rectangles

This commit is contained in:
Kevin Trogant 2022-12-16 11:46:17 +01:00
parent d8be6fc06f
commit ffe2764d29
3 changed files with 58 additions and 34 deletions

View File

@ -115,11 +115,11 @@ Renderer::Renderer()
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)(2 * sizeof(float)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)(4 * sizeof(float)));
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)(6 * sizeof(float)));
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)(8 * sizeof(float)));
glVertexAttribPointer(5, 1, GL_INT, GL_FALSE, sizeof(Rect), (GLvoid*)(12 * sizeof(float)));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)offsetof(Rect, dst_p1_x));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)offsetof(Rect, src_p0_x));
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)offsetof(Rect, src_p1_x));
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Rect), (GLvoid*)offsetof(Rect, r));
glVertexAttribIPointer(5, 1, GL_INT, sizeof(Rect), (GLvoid*)offsetof(Rect, expand_r));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
@ -225,35 +225,35 @@ void Renderer::addRect(float x,
}
void Renderer::addFontRect(float x,
float y,
float w,
float h,
float char_height,
StringHandle font,
StringHandle id,
float src_x0,
float src_y0,
float src_x1,
float src_y1)
float y,
float w,
float h,
float char_height,
StringHandle font,
StringHandle id,
float src_x0,
float src_y0,
float src_x1,
float src_y1)
{
addFontRect(x, y, w, h, 0.f, 0.f, 0.f, 1.f, char_height, font, id, src_x0, src_y0, src_x1, src_y1);
}
void Renderer::addFontRect(float x,
float y,
float w,
float h,
float r,
float g,
float b,
float a,
float char_height,
StringHandle font,
StringHandle id,
float src_x0,
float src_y0,
float src_x1,
float src_y1)
float y,
float w,
float h,
float r,
float g,
float b,
float a,
float char_height,
StringHandle font,
StringHandle id,
float src_x0,
float src_y0,
float src_x1,
float src_y1)
{
if (m_textures.find(id) == m_textures.end()) {
Texture tex;
@ -287,7 +287,7 @@ void Renderer::renderFrame(float width, float height)
assert(m_rects.size() == m_draw_textures.size());
glViewport(0, 0, static_cast<int>(width), static_cast<int>(height));
//glClearColor(0.8f, 0.3f, 0.3f, 1.f);
// glClearColor(0.8f, 0.3f, 0.3f, 1.f);
glClearColor(0.0f, 0.0f, 0.0f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -346,10 +346,7 @@ void Renderer::renderFrame(float width, float height)
m_draw_textures.clear();
}
void Renderer::getTextureSize(
StringHandle texture,
unsigned int* w,
unsigned int* h)
void Renderer::getTextureSize(StringHandle texture, unsigned int* w, unsigned int* h)
{
if (m_textures.find(texture) == m_textures.end()) {
Texture tex;

View File

@ -38,6 +38,7 @@ Texture::Texture(unsigned int width, unsigned int height, const void* data, GLin
GL_RED,
GL_UNSIGNED_BYTE,
data);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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);

View File

@ -90,6 +90,9 @@ int main()
// BEISPIELCODE
Position smiley_pos = {512, 384};
StringHandle smiley = StringRepository::global->internString("smiley_PNG42.png");
StringHandle ttf = StringRepository::global->internString("Milky Honey.ttf");
FontData fd;
AssetManager::ptr->loadFontData("Milky Honey.ttf", 24.f, &fd);
// ENDE BEISPIELCODE
glfwSwapInterval(1);
@ -178,6 +181,29 @@ int main()
Renderer::ptr->addRect(100, 100, 500, 500, 0.3f, 0.3f, 0.3f, 1.f);
Renderer::ptr->addRect(smiley_pos.x, smiley_pos.y, 500, 500, 0.f, x * x, 1.f - x * x, 1.f, smiley);
const char* text = "Hello, Sailor!";
float tx = 100, ty = 200;
while (*text) {
if (*text >= 32 && *text < 128) {
stbtt_aligned_quad q;
stbtt_GetBakedQuad(fd.char_data, 1024, 1024, *text - 32, &tx, &ty, &q, 1);
float w = q.x1 - q.x0, h = q.y1 - q.y0;
Renderer::ptr->addFontRect(q.x0,
q.y0,
w,
h,
fd.char_height,
ttf,
StringRepository::global->internString("1"),
q.s0,
q.t0,
q.s1,
q.t1);
}
++text;
}
// ENDE BEISPIELCODE
Renderer::ptr->renderFrame(static_cast<float>(w), static_cast<float>(h));