Use triangle fan for text rendering

This commit is contained in:
Phireh 2023-11-01 12:12:09 +01:00
commit 59b08dd01a
3 changed files with 14 additions and 30 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
hexnando
hexnando.exe
imgui.ini
*.pdb
*.ilk

View file

@ -1,20 +0,0 @@
[Window][Debug##Default]
Pos=38,60
Size=400,400
Collapsed=0
[Window][Dear ImGui Demo]
Pos=33,236
Size=550,680
Collapsed=0
[Window][Hi there]
Pos=138,172
Size=422,452
Collapsed=0
[Table][0xC9935533,3]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
Column 2 Weight=1.0000

View file

@ -236,7 +236,6 @@ uint32_t make_gl_program(const char *pathname_vertex, const char *pathname_fragm
void render_text(const char *text, float x, float y, float scale, glm::vec3 color)
{
// TODO: Use triangle strip (4 vertices vs 6)
// TODO: Group draw calls together instead of doing 1 draw call/character. Use an atlas with GL_TEXTURE_ARRAY
// TODO: Do not make draw calls for invisible characters!
@ -258,13 +257,10 @@ void render_text(const char *text, float x, float y, float scale, glm::vec3 colo
float w = charglyph.size.x * scale;
float h = charglyph.size.y * scale;
// update VBO for each character
float vertices[6][4] = {
float vertices[4][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 }
};
// render glyph texture over quad
@ -274,7 +270,9 @@ void render_text(const char *text, float x, float y, float scale, glm::vec3 colo
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// render quad
glDrawArrays(GL_TRIANGLES, 0, 6);
glEnable(GL_CULL_FACE);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisable(GL_CULL_FACE);
// now advance cursors for next glyph (note that advance is number of 1/64 pixels)
x += (charglyph.advance >> 6) * scale; // bitshift by 6 to get value in pixels (2^6 = 64)
}
@ -488,6 +486,7 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
camera_t the_camera;
bool mouse_pressed;
bool mouse_over_gui;
static bool show_hex_numbers = true;
static selection_t selection;
selection.indices = (int32_t*)calloc(1, sizeof(int32_t) * grid.rows * grid.columns);
selection.nselected = 0;
@ -641,7 +640,7 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
stbsp_snprintf(debug_text_buf, 256, "Cursor position %.0f %.0f screen %.2f %.2f world", cursor_x, cursor_y, cursor_world.x, cursor_world.y);
render_text(debug_text_buf, 25, 25, .5f, glm::vec3(1.0f, 1.0f, 1.0f));
for (int i = 0; i < grid.rows * grid.columns; ++i)
for (int i = 0; show_hex_numbers && i < grid.rows * grid.columns; ++i)
{
// Get position of hexes in screen
glm::vec4 v = glm::vec4(grid.hexes[i].position.x, grid.hexes[i].position.y, grid.hexes[i].position.z, 1.0);
@ -650,6 +649,9 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
v.x *= window_width/2.0f;
v.y += 1;
v.y *= window_height/2.0f;
// Do not draw text outside visible window
if (v.x > window_width || v.x < 0 || v.y > window_height || v.y < 0)
continue;
char number_string[11];
stbsp_snprintf(number_string, 11, "%d", i);
render_text(number_string, v.x, v.y, .2f + (0.3f / the_camera.size.x), glm::vec3(0.0f, 0.0f, 0.0f));
@ -682,6 +684,7 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
ImGui::Begin("Hi there");
ImGui::SliderInt("Rows", &grid_rows, 1, 100);
ImGui::SliderInt("Columns", &grid_columns, 1, 100);
ImGui::Checkbox("Show hex numbers", &show_hex_numbers);
if (ImGui::CollapsingHeader("Debug timers"))
{
for (int i = 0; i < ntimers; ++i)