Use triangle fan for text rendering
This commit is contained in:
parent
a125c864cb
commit
59b08dd01a
3 changed files with 14 additions and 30 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,4 +1,5 @@
|
||||||
hexnando
|
hexnando
|
||||||
hexnando.exe
|
hexnando.exe
|
||||||
|
imgui.ini
|
||||||
*.pdb
|
*.pdb
|
||||||
*.ilk
|
*.ilk
|
||||||
|
|
|
||||||
20
imgui.ini
20
imgui.ini
|
|
@ -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
|
|
||||||
|
|
||||||
23
main.cpp
23
main.cpp
|
|
@ -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)
|
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: 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!
|
// TODO: Do not make draw calls for invisible characters!
|
||||||
|
|
||||||
|
|
@ -258,14 +257,11 @@ void render_text(const char *text, float x, float y, float scale, glm::vec3 colo
|
||||||
float w = charglyph.size.x * scale;
|
float w = charglyph.size.x * scale;
|
||||||
float h = charglyph.size.y * scale;
|
float h = charglyph.size.y * scale;
|
||||||
// update VBO for each character
|
// update VBO for each character
|
||||||
float vertices[6][4] = {
|
float vertices[4][4] = {
|
||||||
{ xpos, ypos + h, 0.0f, 0.0f },
|
{ xpos, ypos + h, 0.0f, 0.0f },
|
||||||
{ xpos, ypos, 0.0f, 1.0f },
|
{ xpos, ypos, 0.0f, 1.0f },
|
||||||
{ xpos + w, ypos, 1.0f, 1.0f },
|
{ xpos + w, ypos, 1.0f, 1.0f },
|
||||||
|
{ xpos + w, ypos + h, 1.0f, 0.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
|
// render glyph texture over quad
|
||||||
glBindTexture(GL_TEXTURE_2D, charglyph.tex);
|
glBindTexture(GL_TEXTURE_2D, charglyph.tex);
|
||||||
|
|
@ -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);
|
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
// render quad
|
// 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)
|
// 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)
|
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;
|
camera_t the_camera;
|
||||||
bool mouse_pressed;
|
bool mouse_pressed;
|
||||||
bool mouse_over_gui;
|
bool mouse_over_gui;
|
||||||
|
static bool show_hex_numbers = true;
|
||||||
static selection_t selection;
|
static selection_t selection;
|
||||||
selection.indices = (int32_t*)calloc(1, sizeof(int32_t) * grid.rows * grid.columns);
|
selection.indices = (int32_t*)calloc(1, sizeof(int32_t) * grid.rows * grid.columns);
|
||||||
selection.nselected = 0;
|
selection.nselected = 0;
|
||||||
|
|
@ -640,8 +639,8 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
|
||||||
TIME_BLOCK(text_render);
|
TIME_BLOCK(text_render);
|
||||||
stbsp_snprintf(debug_text_buf, 256, "Cursor position %.0f %.0f screen %.2f %.2f world", cursor_x, cursor_y, cursor_world.x, cursor_world.y);
|
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));
|
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
|
// 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);
|
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.x *= window_width/2.0f;
|
||||||
v.y += 1;
|
v.y += 1;
|
||||||
v.y *= window_height/2.0f;
|
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];
|
char number_string[11];
|
||||||
stbsp_snprintf(number_string, 11, "%d", i);
|
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));
|
render_text(number_string, v.x, v.y, .2f + (0.3f / the_camera.size.x), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||||
|
|
@ -667,7 +669,7 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
|
||||||
|
|
||||||
static int grid_rows = grid.rows;
|
static int grid_rows = grid.rows;
|
||||||
static int grid_columns = grid.columns;
|
static int grid_columns = grid.columns;
|
||||||
|
|
||||||
if (grid_rows != grid.rows || grid_columns != grid.columns)
|
if (grid_rows != grid.rows || grid_columns != grid.columns)
|
||||||
{
|
{
|
||||||
create_grid(&grid, grid_rows, grid_columns);
|
create_grid(&grid, grid_rows, grid_columns);
|
||||||
|
|
@ -682,6 +684,7 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
|
||||||
ImGui::Begin("Hi there");
|
ImGui::Begin("Hi there");
|
||||||
ImGui::SliderInt("Rows", &grid_rows, 1, 100);
|
ImGui::SliderInt("Rows", &grid_rows, 1, 100);
|
||||||
ImGui::SliderInt("Columns", &grid_columns, 1, 100);
|
ImGui::SliderInt("Columns", &grid_columns, 1, 100);
|
||||||
|
ImGui::Checkbox("Show hex numbers", &show_hex_numbers);
|
||||||
if (ImGui::CollapsingHeader("Debug timers"))
|
if (ImGui::CollapsingHeader("Debug timers"))
|
||||||
{
|
{
|
||||||
for (int i = 0; i < ntimers; ++i)
|
for (int i = 0; i < ntimers; ++i)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue