]> isoptera.lcsc.edu Git - engine26/.git/commitdiff
Added imgui support (add imgui from github, #define IMGUI at the top of main, build... master origin/HEAD origin/master
authorSeth Long <sslong@lcsc.edu>
Thu, 12 Nov 2026 18:17:05 +0000 (10:17 -0800)
committerSeth Long <sslong@lcsc.edu>
Thu, 12 Nov 2026 18:17:05 +0000 (10:17 -0800)
Makefile
game.h
load_level.cpp
main.cpp
projectile.h
shaders/hud_fragment_shader.glsl
shaders/vertex_shader.glsl

index 450f6f7921928ea041348a8479dcc4f82720f4f7..980ee8a3ee4aed3768a09eee80363481e04b70e0 100644 (file)
--- 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 89f1b4c130e05aa23065ef8cec113ec35d77f987..46586486eb5b9345852062e21f8a7a05bfb2d3e3 100644 (file)
--- 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<GameObject*> game_objects;
 vector<SolidObject*> solid_objects;
-vector<Overlay*> overlays;
 map<string, unsigned int> gl_resources;
 Hud *main_hud;
+Aimpoint *aimpoint;
 #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;
+extern Aimpoint *aimpoint;
 #endif
 
 struct vertex {
index d88e354e30320033ff3f57c5b90b4324a881543f..282b0ddca6795537c595febf7cf19acb50cb7b20 100644 (file)
@@ -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)
index 910d916e82625ff8e29d9aafa565387daf7a2057..5df1f2d7bd2529af73083a475451299d0ecb51f6 100644 (file)
--- 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);
index 341740023ae20216f27fb59d99e5099aa2fa2105..79643aadeab99024bb2df7953930b219fb26accd 100644 (file)
@@ -7,12 +7,14 @@ public:
        constexpr static float proj_size = .3;
        vector<vec3> velocities;
        vector<float> 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);
+               }
        };
 };
index e2c93a7654965e2fe886c679bcec402df84ac144..a12072b9dd88cc09bb4275ad783dedd12929269d 100644 (file)
@@ -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;
 }
index 8dbd07a4debd1fe00e06bca74cf90d7363575305..ed7f9c485ecedde0a066f5483c9b03da7586753a 100644 (file)
@@ -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];
 }