From 896a8b5fb71b3fa9566429ee46cbf3f224eb184e Mon Sep 17 00:00:00 2001 From: Seth Long Date: Wed, 30 Oct 2024 14:54:31 -0700 Subject: [PATCH] Added hud --- Makefile | 2 +- aimpoint.h | 36 -------- aimpoint_fragment_shader.glsl | 7 ++ aimpoint_vertex_shader.glsl | 8 ++ helpers.cpp | 2 +- hud.h | 153 ++++++++++++++++++++++++++++++++++ hud_fragment_shader.glsl | 5 +- hud_vertex_shader.glsl | 7 +- main.cpp | 8 +- 9 files changed, 186 insertions(+), 42 deletions(-) delete mode 100644 aimpoint.h create mode 100644 aimpoint_fragment_shader.glsl create mode 100644 aimpoint_vertex_shader.glsl create mode 100644 hud.h diff --git a/Makefile b/Makefile index a9c2bcd..94730d8 100644 --- a/Makefile +++ b/Makefile @@ -16,4 +16,4 @@ test: all ./a.out ${OBJS} : game.h -main.o : projectiles.h activation_area.h turret.h tile_floor.h old_objects.h moving_platform.h geometric_objects.h aimpoint.h +main.o : projectiles.h activation_area.h turret.h tile_floor.h old_objects.h moving_platform.h geometric_objects.h hud.h diff --git a/aimpoint.h b/aimpoint.h deleted file mode 100644 index 5c029b5..0000000 --- a/aimpoint.h +++ /dev/null @@ -1,36 +0,0 @@ -class aimpoint : public gameobject { - public: - int init(){ - float vertices[] { - -.2, .002, 0, - .2, .002, 0, - .2, -.002, 0, - -.2, .002, 0, - .2, -.002, 0, - -.2, -.002, 0, - - -.002, .2, 0, - .002, .2, 0, - .002, -.2, 0, - -.002, .2, 0, - .002, -.2, 0, - -.002, -.2, 0, - }; - glGenBuffers(1, &vbuf); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbuf); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - program = make_program("hud_vertex_shader.glsl",0, 0, 0, "hud_fragment_shader.glsl"); - if (!program) - return 1; - v_attrib = glGetAttribLocation(program, "in_vertex"); - return 0; - } - - void draw(glm::mat4 vp){ - glUseProgram(program); - glEnableVertexAttribArray(v_attrib); - glBindBuffer(GL_ARRAY_BUFFER, vbuf); - glVertexAttribPointer(v_attrib, 3, GL_FLOAT, GL_FALSE, 0, 0); - glDrawArrays(GL_TRIANGLES, 0, 12); - } -}; diff --git a/aimpoint_fragment_shader.glsl b/aimpoint_fragment_shader.glsl new file mode 100644 index 0000000..3cb4cb1 --- /dev/null +++ b/aimpoint_fragment_shader.glsl @@ -0,0 +1,7 @@ +#version 460 + +out vec4 outcolor; + +void main(void) { + outcolor = vec4(1.0f, 0.0f, 0.0f, 1.0f); +} diff --git a/aimpoint_vertex_shader.glsl b/aimpoint_vertex_shader.glsl new file mode 100644 index 0000000..66f119b --- /dev/null +++ b/aimpoint_vertex_shader.glsl @@ -0,0 +1,8 @@ +#version 460 + +in vec3 in_vertex; +out vec4 gl_Position; + +void main(void) { + gl_Position = vec4(in_vertex, 1.0); +} diff --git a/helpers.cpp b/helpers.cpp index ef177f9..85f9a67 100644 --- a/helpers.cpp +++ b/helpers.cpp @@ -109,7 +109,7 @@ unsigned int load_texture(const char* filename){ int width, height, channels; unsigned char *image_data = stbi_load(filename, &width, &height, &channels, 4); if(image_data){ - printf("Loaded image, %d by %d\n", width, height); + printf("Loaded image %s, %d by %d\n", filename, width, height); unsigned int tex; glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D, tex); diff --git a/hud.h b/hud.h new file mode 100644 index 0000000..ad669bc --- /dev/null +++ b/hud.h @@ -0,0 +1,153 @@ +#include + +class aimpoint : public gameobject { + public: + int init(){ + float vertices[] { + -.2, .002, 0, + .2, .002, 0, + .2, -.002, 0, + -.2, .002, 0, + .2, -.002, 0, + -.2, -.002, 0, + + -.002, .2, 0, + .002, .2, 0, + .002, -.2, 0, + -.002, .2, 0, + .002, -.2, 0, + -.002, -.2, 0, + }; + glGenBuffers(1, &vbuf); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbuf); + glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + program = make_program("aimpoint_vertex_shader.glsl",0, 0, 0, "aimpoint_fragment_shader.glsl"); + if (!program) + return 1; + v_attrib = glGetAttribLocation(program, "in_vertex"); + return 0; + } + + void draw(glm::mat4 vp){ + glUseProgram(program); + glEnableVertexAttribArray(v_attrib); + glBindBuffer(GL_ARRAY_BUFFER, vbuf); + glVertexAttribPointer(v_attrib, 3, GL_FLOAT, GL_FALSE, 0, 0); + glDrawArrays(GL_TRIANGLES, 0, 12); + } +}; + +class hud : public gameobject { + public: + float texcoords[3600]; + int char_width = 100; + float y_space = 0.02f; + void set_char(int col, int line, char c){ + float position = c - 32; + position /= 95; + size_t cstart = (col + line * char_width) * 12; + texcoords[cstart + 0] = position; + texcoords[cstart + 1] = 1.0f; + texcoords[cstart + 2] = position + 1.0f/95; + texcoords[cstart + 3] = 1.0f; + texcoords[cstart + 4] = position; + texcoords[cstart + 5] = 0.0f; + texcoords[cstart + 6] = position + 1.0f/95; + texcoords[cstart + 7] = 1.0f; + texcoords[cstart + 8] = position + 1.0f/95; + texcoords[cstart + 9] = 0.0f; + texcoords[cstart + 10] = position; + texcoords[cstart + 11] = 0.0f; + } + + void lprintf(int line, const char *fmt, ...){ + va_list args; + va_start(args, fmt); + char text[100]; + vsprintf(text, fmt, args); + set_text_line(line, text); + } + + void set_text_line(int line, const char* text){ + for(int i = line * 1200; i < (line + 1) * 1200; i++) + texcoords[i] =0; + int col = 0; + for(; *text; text++){ + set_char(col, line, *text); + col++; + } + } + + void set_text(const char* text){ + for(int i = 0; i < 3600; i++) + texcoords[i] = 0; + int col = 0, line = 0; + for(; *text; text++) + if(*text != '\n'){ + set_char(col, line, *text); + col++; + } else { + col = 0; + line++; + } + } + + int init(){ + std::vector vertices; + /* 100 character display, 3 lines */ + for(float y = y_space; y >= -y_space; y -= y_space) + for(int i = 0; i < char_width; i++){ + vertices.push_back(0.01f * i); + vertices.push_back(y); + + vertices.push_back(0.01f * (i + 1)); + vertices.push_back(y); + + vertices.push_back(0.01f * i); + vertices.push_back(y - y_space); + + vertices.push_back(0.01f * (i + 1)); + vertices.push_back(y); + + vertices.push_back(0.01f * (i + 1)); + vertices.push_back(y - y_space); + + vertices.push_back(0.01f * i); + vertices.push_back(y - y_space); + + } + glGenBuffers(1, &vbuf); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbuf); + glBufferData(GL_SHADER_STORAGE_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW); + + for(int i = 0; i < char_width * 3; i++) + set_char(i % char_width, i / char_width, '#'); + glGenBuffers(1, &cbuf); + + tex = load_texture("letters.png"); + + program = make_program("hud_vertex_shader.glsl", 0, 0, 0, "hud_fragment_shader.glsl"); + if(!program) + return 1; + v_attrib = glGetAttribLocation(program, "in_vertex"); + t_attrib = glGetAttribLocation(program, "in_texcoord"); + return 0; + } + + void draw(glm::mat4 vp){ + glUseProgram(program); + glEnableVertexAttribArray(v_attrib); + glBindBuffer(GL_ARRAY_BUFFER, vbuf); + glVertexAttribPointer(v_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0); + + glBindBuffer(GL_SHADER_STORAGE_BUFFER, cbuf); + glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW); + glEnableVertexAttribArray(t_attrib); + glBindBuffer(GL_ARRAY_BUFFER, cbuf); + glVertexAttribPointer(t_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, tex); + + glDrawArrays(GL_TRIANGLES, 0, 1800); + } +}; diff --git a/hud_fragment_shader.glsl b/hud_fragment_shader.glsl index 3cb4cb1..29ab238 100644 --- a/hud_fragment_shader.glsl +++ b/hud_fragment_shader.glsl @@ -1,7 +1,10 @@ #version 460 +in vec2 frag_texcoord; out vec4 outcolor; +uniform sampler2D tex; void main(void) { - outcolor = vec4(1.0f, 0.0f, 0.0f, 1.0f); + outcolor = texture(tex, vec2(frag_texcoord.x, -frag_texcoord.y)); +// outcolor = vec4(frag_texcoord.xy, 0, 1); } diff --git a/hud_vertex_shader.glsl b/hud_vertex_shader.glsl index 66f119b..208d527 100644 --- a/hud_vertex_shader.glsl +++ b/hud_vertex_shader.glsl @@ -1,8 +1,11 @@ #version 460 -in vec3 in_vertex; +in vec2 in_vertex; +in vec2 in_texcoord; out vec4 gl_Position; +out vec2 frag_texcoord; void main(void) { - gl_Position = vec4(in_vertex, 1.0); + gl_Position = vec4(1.8 * in_vertex.x - 0.9, 1.8 * in_vertex.y + 0.8, 0.0, 1.0); + frag_texcoord = in_texcoord; } diff --git a/main.cpp b/main.cpp index 2fc9802..4c9b2ac 100644 --- a/main.cpp +++ b/main.cpp @@ -12,7 +12,7 @@ #include "moving_platform.h" #include "turret.h" #include "geometric_objects.h" -#include "aimpoint.h" +#include "hud.h" std::mutex grand_mutex; @@ -27,6 +27,7 @@ std::vector objects; projectile ice_balls; wall_block spawned_blocks("cube.obj", "projectile.jpg", glm::vec3(2, 2, 2)); fragment brick_fragments; +hud main_hud; class target : public loaded_object, public block_object { public: @@ -174,6 +175,7 @@ void player_movement(){ player_position.y = floor_height + player_height; } } + main_hud.lprintf(2, "Player Position: (%f, %f, %f) %d", player_position.x, player_position.y, player_position.z, 50); // grand_mutex.unlock(); auto end = std::chrono::system_clock::now(); // double difference = std::chrono::duration_cast(start - end).count(); @@ -381,6 +383,8 @@ int main(int argc, char** argv) { aimpoint main_aimpoint; objects.push_back(&main_aimpoint); + objects.push_back(&main_hud); + objects.push_back(&spawned_blocks); /* Initialize game objects */ @@ -390,6 +394,8 @@ int main(int argc, char** argv) { return 1; } } + + main_hud.set_text("Hello World\nThis is line two\nAnd line three!"); /* Start Other Threads */ std::thread player_movement_thread(player_movement); -- 2.39.5