From: Seth Long Date: Thu, 12 Nov 2026 18:17:05 +0000 (-0800) Subject: Added imgui support (add imgui from github, #define IMGUI at the top of main, build... X-Git-Url: https://isoptera.lcsc.edu/gitweb/?a=commitdiff_plain;ds=inline;p=engine26%2F.git Added imgui support (add imgui from github, #define IMGUI at the top of main, build relevant imgui .cpp files individually or add them to the visual studio project) --- diff --git a/Makefile b/Makefile index 450f6f7..980ee8a 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,10 @@ LDFLAGS = -lGL -lglfw -lGLEW -pthread -g CXXFLAGS += -I /usr/local/include LDFLAGS += -L /usr/local/lib +# For ImGui +CXXFLAGS += -I imgui +LDFLAGS += imgui/*.o imgui/backends/*.o + all: ${OBJS} ${CXX} ${OBJS} ${LDFLAGS} -o game diff --git a/game.h b/game.h index 89f1b4c..4658648 100644 --- a/game.h +++ b/game.h @@ -70,6 +70,8 @@ struct globals { double cctime = 0; double movement_time = 0; double gmtime = 0; + bool show_hud = 0; + bool show_menu = 0; }; vec3 get_lookat_point(int direction = 0, float r = 0.0); @@ -146,16 +148,16 @@ public: globals g; vector game_objects; vector solid_objects; -vector overlays; map gl_resources; Hud *main_hud; +Aimpoint *aimpoint; #else extern globals g; extern vector game_objects; extern vector solid_objects; -extern vector overlays; extern map gl_resources; extern Hud *main_hud; +extern Aimpoint *aimpoint; #endif struct vertex { diff --git a/load_level.cpp b/load_level.cpp index d88e354..282b0dd 100644 --- a/load_level.cpp +++ b/load_level.cpp @@ -1,6 +1,15 @@ #include "game.h" -void load_level(){ +void empty_level(){ + g.player_position = vec3(0, 1, 0); + + FloorPanel *floor = (FloorPanel*)add_object(new FloorPanel, "The Floor", true); + floor->texture_file = "textures/stones.png"; + floor->add_location_size(vec3(0, 0, 0), vec3(20, 1, 20)); + floor->add_location_size(vec3(12, 0, 0), vec3(4, 1, 4)); +} + +void demo_level(){ /* Level Loading Area */ g.player_position = vec3(0, 1, 10); @@ -64,6 +73,11 @@ void load_level(){ mouse->texture_file = "textures/mouse.jpg"; } +void load_level(){ + /* Choose Here. We could make a UI for this... */ + demo_level(); +} + GameObject* add_object(GameObject *new_object, string name, bool solid){ game_objects.push_back(new_object); if(solid) diff --git a/main.cpp b/main.cpp index 910d916..5df1f2d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,12 @@ #define MAIN -#include "game.h" +//#define IMGUI +#include "game.h" +#ifdef IMGUI +#include "imgui/imgui.h" +#include "imgui/backends/imgui_impl_glfw.h" +#include "imgui/backends/imgui_impl_opengl3.h" +#endif int movement_count = 0; void fire(); @@ -32,6 +38,8 @@ bool clear_check(vec3 point, float margin = 0.0f, SolidObject **blocker = 0, siz } /* For each object: + for(auto o : overlays) + o->draw(); * if object->moving: * for each instance of that object: * for each solid object: @@ -211,6 +219,7 @@ void general_management_function(){ g.on_count++; } + main_hud->lprintf(1, "Location: %.2f %.2f %.2f", g.player_position.x, g.player_position.y, g.player_position.z); double end_time = glfwGetTime(); double sleep_seconds = 0.01 - (end_time - start_time); unsigned long sleep_microseconds = sleep_seconds * 1000000.0; @@ -247,7 +256,7 @@ void stats_function() { framecount = 0; g.movement_time = 1.0 / (double)movement_count; movement_count = 0; - printf("FPS: %u cctime = %lf movement_time = %lf gmtime = %lf\n", g.last_fps, g.cctime, g.movement_time, g.gmtime); + main_hud->lprintf(2, "FPS: %u cctime = %0.5lf movement_time = %0.5lf gmtime = %0.5lf\n", g.last_fps, g.cctime, g.movement_time, g.gmtime); /* for(auto proj : game_objects) { if(proj->name == "Projectiles") { @@ -303,8 +312,18 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod if(key == GLFW_KEY_SPACE) if(!clear_check(g.player_position - vec3(0, g.player_height, 0), g.move_margin)) g.vertical_speed = g.jump_speed; - if(key == GLFW_KEY_ESCAPE) - g.release_cursor = true; + if(key == GLFW_KEY_ESCAPE) { + if(g.release_cursor){ + g.release_cursor = false; + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + } else { + g.release_cursor = true; + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + } + g.show_menu = !g.show_menu; + } + if(key == GLFW_KEY_F3) + g.show_hud = !g.show_hud; } if(action == GLFW_RELEASE) { // UP if(key == GLFW_KEY_W) @@ -348,8 +367,8 @@ void fire(){ void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) { if(g.release_cursor) - g.release_cursor = false; - else if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { + return; + if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { fire(); } else if(button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) { vec3 B; @@ -389,14 +408,23 @@ int main(int argc, char ** argv){ glfwSetFramebufferSizeCallback(window, window_size_callback); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + // Set up imgui (if we want to use it) +#ifdef IMGUI + IMGUI_CHECKVERSION(); + using namespace ImGui; + CreateContext(); + ImGuiIO& io = GetIO();// (void)io; // Why? + StyleColorsDark(); + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init("#version 450"); +#endif + // Set up hud and aimpoint - Overlay *aimpoint = new Aimpoint("Aimpoint"); + Aimpoint *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!"); + main_hud->lprintf(0, "Hello World!"); load_level(); for(auto o : game_objects) @@ -428,8 +456,30 @@ int main(int argc, char ** argv){ for(auto o : game_objects) o->draw(vp); - for(auto o : overlays) - o->draw(); + if(g.show_hud) + main_hud->draw(); + aimpoint->draw(); + +#ifdef IMGUI + if(g.show_menu){ + StyleColorsDark(); + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + NewFrame(); + Begin("OurWindow"); + Text("FPS: %.1f FPS", GetIO().Framerate); + Text("X = %f, Y = %f, Z = %f", g.player_position.x, g.player_position.y, g.player_position.z); + Projectile* p = (Projectile*)find_object("Projectiles"); + SliderFloat("Speed", &(p->speed), 0.0, 1.0); + RadioButton("Projectiles Bounce", &(p->bounce), 1); + RadioButton("Projectiles Stop", &(p->bounce), 0); + + // Add some option to try it out + End(); + Render(); + ImGui_ImplOpenGL3_RenderDrawData(GetDrawData()); + } +#endif /* Not for a particular object */ glfwSwapBuffers(window); diff --git a/projectile.h b/projectile.h index 3417400..79643aa 100644 --- a/projectile.h +++ b/projectile.h @@ -7,12 +7,14 @@ public: constexpr static float proj_size = .3; vector velocities; vector times_to_live; + float speed = 0.1f; + int bounce = 1; Projectile() { moving = true; }; void add_location_size(vec3 start, vec3 vel){ - velocities.push_back(vel * 0.1f); + velocities.push_back(vel * speed); times_to_live.push_back(2000); specials.push_back(0); models.push_back(translate(mat4(1.0), start)); @@ -67,27 +69,32 @@ public: void report_collision(GameObject *other, size_t other_instance, size_t our_instance) { if(our_instance >= models.size()) throw runtime_error("Attempt to report collision with nonexistent instance"); - vec3 our_center = get_center(our_instance); - vec3 other_cp = other->closest_point(our_center, other_instance); - /* Now figure out what shape the other object is! */ - /* If we could get a normal vector for the surface we hit... */ - bool rx = true, ry = true, rz = true; - if(fabs(other_cp.x - our_center.x) < 0.01) - rx = false; - if(fabs(other_cp.y - our_center.y) < 0.01) - ry = false; - if(fabs(other_cp.z - our_center.z) < 0.01) - rz = false; - - if(rx) - velocities[our_instance] *= vec3(-1, 1, 1); - if(ry) - velocities[our_instance] *= vec3(1, -1, 1); - if(rz) - velocities[our_instance] *= vec3(1, 1, -1); - - /* Might not need this - applies one timestep of movement to make sure this projectile - * isn't in contact with the wall anymore so it won't bounce right back */ - models[our_instance] = translate(models[our_instance], velocities[our_instance]); + if(bounce){ + vec3 our_center = get_center(our_instance); + vec3 other_cp = other->closest_point(our_center, other_instance); + /* Now figure out what shape the other object is! */ + /* If we could get a normal vector for the surface we hit... */ + bool rx = true, ry = true, rz = true; + if(fabs(other_cp.x - our_center.x) < 0.01) + rx = false; + if(fabs(other_cp.y - our_center.y) < 0.01) + ry = false; + if(fabs(other_cp.z - our_center.z) < 0.01) + rz = false; + + if(rx) + velocities[our_instance] *= vec3(-1, 1, 1); + if(ry) + velocities[our_instance] *= vec3(1, -1, 1); + if(rz) + velocities[our_instance] *= vec3(1, 1, -1); + + /* Might not need this - applies one timestep of movement to make sure this projectile + * isn't in contact with the wall anymore so it won't bounce right back */ + models[our_instance] = translate(models[our_instance], velocities[our_instance]); + } else { + /* TODO: To properly stick, we should make sure we're not kinda partly inside */ + velocities[our_instance] = vec3(0, 0, 0); + } }; }; diff --git a/shaders/hud_fragment_shader.glsl b/shaders/hud_fragment_shader.glsl index e2c93a7..a12072b 100644 --- a/shaders/hud_fragment_shader.glsl +++ b/shaders/hud_fragment_shader.glsl @@ -6,6 +6,9 @@ uniform sampler2D tex; void main(void) { outcolor = texture(tex, vec2(frag_texcoord.x, -frag_texcoord.y)); + outcolor.g = 0; + outcolor.b = 0; if(outcolor.r < 0.1) - discard; + outcolor = vec4(1.0, 1.0, 1.0, 1.0); +// discard; } diff --git a/shaders/vertex_shader.glsl b/shaders/vertex_shader.glsl index 8dbd07a..ed7f9c4 100644 --- a/shaders/vertex_shader.glsl +++ b/shaders/vertex_shader.glsl @@ -14,6 +14,8 @@ out flat int special; void main(void) { gl_Position = vp * models[gl_InstanceID] * vec4(in_vertex, 1.0); - out_texcoord = in_texcoord; + out_texcoord = in_texcoord; +// We'll come back and solve this with normal vectors +// out_texcoord = in_texcoord * vec2(models[gl_InstanceID][0][0], models[gl_InstanceID][2][2]); special = instance_special[gl_InstanceID]; }