]> isoptera.lcsc.edu Git - engine26/.git/commitdiff
Added hud from last term, but it's broken somehow
authorSeth Long <sslong@lcsc.edu>
Tue, 10 Nov 2026 18:13:22 +0000 (10:13 -0800)
committerSeth Long <sslong@lcsc.edu>
Tue, 10 Nov 2026 18:13:22 +0000 (10:13 -0800)
13 files changed:
Makefile
game.h
gameobject.cpp
hud.cpp [new file with mode: 0644]
hud.h [new file with mode: 0644]
main.cpp
shaders/aimpoint_fragment_shader.glsl [new file with mode: 0644]
shaders/aimpoint_fragment_shader.glsl~ [new file with mode: 0644]
shaders/aimpoint_vertex_shader.glsl [new file with mode: 0644]
shaders/hud_fragment_shader.glsl [new file with mode: 0644]
shaders/hud_vertex_shader.glsl [new file with mode: 0644]
shaders/hud_vertex_shader.glsl~ [new file with mode: 0644]
textures/letters.png [new file with mode: 0644]

index bafbdf9c9319b2fa8b955afb19824e350a36fe50..450f6f7921928ea041348a8479dcc4f82720f4f7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-OBJS = main.o implementations.o gameobject.o solidobject.o misc_objects.o helpers.o load_level.o water_tank.o
+OBJS = main.o implementations.o gameobject.o solidobject.o misc_objects.o helpers.o load_level.o water_tank.o hud.o
 CXXFLAGS = -g  -std=c++17 -Wall 
 LDFLAGS = -lGL -lglfw -lGLEW -pthread -g 
 
@@ -15,4 +15,4 @@ clean:
 test:  all
        ./game
 
-${OBJS} : game.h misc_objects.h water_tank.h projectile.h explodingtarget.h cat.h
+${OBJS} : game.h misc_objects.h water_tank.h projectile.h explodingtarget.h cat.h hud.h
diff --git a/game.h b/game.h
index 15b6e089c927c85975162608d09cca0fca642dca..89f1b4c130e05aa23065ef8cec113ec35d77f987 100644 (file)
--- a/game.h
+++ b/game.h
@@ -140,16 +140,22 @@ public:
 
 };
 
+#include "hud.h"
+
 #ifdef MAIN
 globals g;
 vector<GameObject*> game_objects;
 vector<SolidObject*> solid_objects;
+vector<Overlay*> overlays;
 map<string, unsigned int> gl_resources;
+Hud *main_hud;
 #else
 extern globals g;
 extern vector<GameObject*> game_objects;
 extern vector<SolidObject*> solid_objects;
+extern vector<Overlay*> overlays;
 extern map<string, unsigned int> gl_resources;
+extern Hud *main_hud;
 #endif
 
 struct vertex {
@@ -163,6 +169,8 @@ GameObject* add_object(GameObject *new_object, string name, bool solid = false);
 void load_level();
 GameObject* find_object(string name);
 bool can_see(vec3 A, vec3 B, float start_distance = 0.0f);
+unsigned int make_program(const char* v_file, const char* tcs_file, const char* tes_file, const char* g_file, const char* f_file);
+unsigned int load_texture(string texture_file);
 
 #define PLAYER_ON_OBJECT 1
 #define ACTIVATE 2
index 5a5a20563b71d7b64f3cdbbc742f518c849c81dc..623e9b8160e9c519b529cc1a70f82686602989ce 100644 (file)
@@ -53,6 +53,7 @@ unsigned int load_texture(string texture_file){
                glGenTextures(1, &tex);
                glBindTexture(GL_TEXTURE_2D, tex);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);           
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img_width, img_height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
                free(image);
                gl_resources[texture_file] = tex;
@@ -85,6 +86,47 @@ unsigned int load_shader(string shader_filename, unsigned int type){
        }
 }
 
+unsigned int make_program(const char* v_file, const char* tcs_file, const char* tes_file, const char* g_file, const char* f_file) {
+       unsigned int vs_reference = load_shader(v_file, GL_VERTEX_SHADER);
+       unsigned int tcs_reference = 0, tes_reference = 0;
+       if (tcs_file)
+               if (!(tcs_reference = load_shader(tcs_file, GL_TESS_CONTROL_SHADER)))
+                       return 0;
+       if (tes_file)
+               if (!(tes_reference = load_shader(tes_file, GL_TESS_EVALUATION_SHADER)))
+                       return 0;
+       unsigned int gs_reference = 0;
+       if (g_file)
+               gs_reference = load_shader(g_file, GL_GEOMETRY_SHADER);
+       unsigned int fs_reference = load_shader(f_file, GL_FRAGMENT_SHADER);
+       if (!(vs_reference && fs_reference))
+               return 0;
+       if (g_file && !gs_reference)
+               return 0;
+
+
+       unsigned int program = glCreateProgram();
+       glAttachShader(program, vs_reference);
+       if (g_file)
+               glAttachShader(program, gs_reference);
+       if (tcs_file)
+               glAttachShader(program, tcs_reference);
+       if (tes_file)
+               glAttachShader(program, tes_reference);
+       glAttachShader(program, fs_reference);
+       glLinkProgram(program);
+       GLint link_ok;
+       glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
+       if (!link_ok) {
+               glGetProgramInfoLog(program, 2048, NULL, log_buffer);
+               puts(log_buffer);
+               puts("Link Failed");
+               return 0;
+       }
+
+       return program;
+}
+
 bool GameObject::init(){
        tinyobj::attrib_t attrib;
        vector<tinyobj::shape_t> shapes;
@@ -135,19 +177,8 @@ bool GameObject::init(){
        glGenBuffers(1, &models_buffer);
        glGenBuffers(1, &specials_buffer);
 
-       unsigned int vs_reference = load_shader("shaders/vertex_shader.glsl", GL_VERTEX_SHADER);
-       unsigned int fs_reference = load_shader("shaders/fragment_shader.glsl", GL_FRAGMENT_SHADER);
-
-       program = glCreateProgram();
-       glAttachShader(program, vs_reference);
-       glAttachShader(program, fs_reference);
-       glLinkProgram(program);
-
-       int link_ok;
-       glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
-       glGetProgramInfoLog(program, 2048, NULL, log_buffer);
-       puts(log_buffer);
-       if(!link_ok){
+       program = make_program("shaders/vertex_shader.glsl", 0, 0, 0, "shaders/fragment_shader.glsl");
+       if(!program){
                printf("Link failure\n");
                return false;
        }
diff --git a/hud.cpp b/hud.cpp
new file mode 100644 (file)
index 0000000..7d476a1
--- /dev/null
+++ b/hud.cpp
@@ -0,0 +1,148 @@
+#include "game.h"
+#include "hud.h"
+#include<stdarg.h>
+
+int Aimpoint::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("shaders/aimpoint_vertex_shader.glsl",0, 0, 0, "shaders/aimpoint_fragment_shader.glsl");
+       if (!program)
+               return 1;
+       v_attrib = glGetAttribLocation(program, "in_vertex");
+       return 0;
+}
+
+void Aimpoint::draw(){
+       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);
+}
+
+void Hud::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 Hud::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 Hud::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 Hud::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 Hud::init(){
+       std::vector<float> vertices;    
+       vertices.reserve(3600);
+       /* 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("textures/letters.png");
+
+       program = make_program("shaders/hud_vertex_shader.glsl", 0, 0, 0, "shaders/hud_fragment_shader.glsl");
+       if(!program)
+               return 1;
+       v_attrib = glGetAttribLocation(program, "in_vertex");
+       t_attrib = glGetAttribLocation(program, "in_texcoord");
+       return 0;
+}
+
+void Hud::draw(){
+       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.h b/hud.h
new file mode 100644 (file)
index 0000000..892fae5
--- /dev/null
+++ b/hud.h
@@ -0,0 +1,34 @@
+#pragma once
+
+class Overlay {
+       public:
+               string name;
+               Overlay(string n) : name(n) {};
+               virtual int init() = 0;
+               virtual void draw() = 0;
+
+};
+
+class Aimpoint : public Overlay {
+       public:
+               Aimpoint(string n) : Overlay(n) {}
+               unsigned int vbuf, program, v_attrib;
+               int init() override;
+               void draw() override;
+};
+
+class Hud : public Overlay {
+       public:
+               Hud(string n) : Overlay(n) {}
+               unsigned int vbuf, program, v_attrib, tbuf, t_attrib, tex, cbuf;
+               float texcoords[3600];
+               int char_width = 100;
+               float y_space = 0.02f;
+
+               int init() override;
+               void draw() override;
+               void set_char(int col, int line, char c);
+               void lprintf(int line, const char *fmt, ...);
+               void set_text_line(int line, const char* text);
+               void set_text(const char* text);
+};
index 05d515f0b23dd3d7dbcd272283535a4873b31d7d..1ea8243a4abf6e341001d72fa3e0fd204d04d266 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -387,6 +387,16 @@ int main(int argc, char ** argv){
        glfwSetCursorPosCallback(window, cursor_pos_callback);
        glfwSetMouseButtonCallback(window, mouse_button_callback);
        glfwSetFramebufferSizeCallback(window, window_size_callback);
+   glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
+
+       // Set up hud and aimpoint
+       Overlay *aimpoint = new Aimpoint("Aimpoint");
+       aimpoint->init();
+       overlays.push_back(aimpoint);
+       main_hud = new Hud("Main Hud");
+       main_hud->init();
+       overlays.push_back(main_hud);
+       main_hud->lprintf(1, "Hello World!");
 
        load_level();
        for(auto o : game_objects)
@@ -418,6 +428,9 @@ int main(int argc, char ** argv){
                for(auto o : game_objects)
                        o->draw(vp);
 
+               for(auto o : overlays)
+                       o->draw();
+
                /* Not for a particular object */
                glfwSwapBuffers(window);
                framecount++;
diff --git a/shaders/aimpoint_fragment_shader.glsl b/shaders/aimpoint_fragment_shader.glsl
new file mode 100644 (file)
index 0000000..ec69f02
--- /dev/null
@@ -0,0 +1,7 @@
+#version 460
+
+out vec4 outcolor;
+
+void main(void) {
+  outcolor = vec4(0.0f, 1.0f, 0.0f, 1.0f);
+}
diff --git a/shaders/aimpoint_fragment_shader.glsl~ b/shaders/aimpoint_fragment_shader.glsl~
new file mode 100644 (file)
index 0000000..3cb4cb1
--- /dev/null
@@ -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/shaders/aimpoint_vertex_shader.glsl b/shaders/aimpoint_vertex_shader.glsl
new file mode 100644 (file)
index 0000000..66f119b
--- /dev/null
@@ -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/shaders/hud_fragment_shader.glsl b/shaders/hud_fragment_shader.glsl
new file mode 100644 (file)
index 0000000..e2c93a7
--- /dev/null
@@ -0,0 +1,11 @@
+#version 460
+
+in vec2 frag_texcoord;
+out vec4 outcolor;
+uniform sampler2D tex;
+
+void main(void) {
+       outcolor = texture(tex, vec2(frag_texcoord.x, -frag_texcoord.y));
+       if(outcolor.r < 0.1)
+               discard;
+}
diff --git a/shaders/hud_vertex_shader.glsl b/shaders/hud_vertex_shader.glsl
new file mode 100644 (file)
index 0000000..208d527
--- /dev/null
@@ -0,0 +1,11 @@
+#version 460
+
+in vec2 in_vertex;
+in vec2 in_texcoord;
+out vec4 gl_Position;
+out vec2 frag_texcoord;
+
+void main(void) {      
+       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/shaders/hud_vertex_shader.glsl~ b/shaders/hud_vertex_shader.glsl~
new file mode 100644 (file)
index 0000000..208d527
--- /dev/null
@@ -0,0 +1,11 @@
+#version 460
+
+in vec2 in_vertex;
+in vec2 in_texcoord;
+out vec4 gl_Position;
+out vec2 frag_texcoord;
+
+void main(void) {      
+       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/textures/letters.png b/textures/letters.png
new file mode 100644 (file)
index 0000000..552310c
Binary files /dev/null and b/textures/letters.png differ