]> isoptera.lcsc.edu Git - example_engine/.git/commitdiff
Started geometric objects
author= <=>
Tue, 22 Oct 2024 17:18:11 +0000 (10:18 -0700)
committer= <=>
Tue, 22 Oct 2024 17:18:11 +0000 (10:18 -0700)
Makefile
baseclass.cpp
game.h
geometric_objects.cpp [new file with mode: 0644]
geometric_objects.h [new file with mode: 0644]
main.cpp
turret.h

index a962bbf071e3f22271539f3e0a4675c3662edeb5..ca9d8c777cca3402efecf4c88095997074ce0271 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-OBJS = main.o stb_image.o helpers.o tiny_obj_loader.o baseclass.o old_objects.o 
+OBJS = main.o stb_image.o helpers.o tiny_obj_loader.o baseclass.o old_objects.o geometric_objects.o
 CXXFLAGS = -g -Wall 
 LDFLAGS = -lGL -lglfw -lGLEW -pthread -g 
 
@@ -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
+main.o : projectiles.h activation_area.h turret.h tile_floor.h old_objects.h moving_platform.h geometric_objects.h
index 67a3973404c967a3ecad03d513e1e7deed842779..2f0c5edb953373229897c0a5d79d36ff10ed2c04 100644 (file)
@@ -1,5 +1,18 @@
 #include "game.h"
 
+std::vector<glm::mat4> gameobject::create_models(){
+       std::vector<glm::mat4> models;
+       data_mutex.lock();
+       models.reserve(locations.size());
+       for(glm::vec3 l : locations){
+               glm::mat4 new_model = glm::mat4(1.0f);
+               new_model = translate(new_model, l);
+               models.push_back(new_model);
+       }
+       data_mutex.unlock();
+       return models;
+}
+
 int loaded_object::init() {
        // Initialization part
        std::vector<vertex> vertices;
@@ -30,22 +43,9 @@ int loaded_object::init() {
        return 0;
 }
 
-std::vector<glm::mat4> loaded_object::create_models(glm::mat4 vp){
-       std::vector<glm::mat4> models;
-       data_mutex.lock();
-       models.reserve(locations.size());
-       for(glm::vec3 l : locations){
-               glm::mat4 new_model = glm::mat4(1.0f);
-               new_model = translate(new_model, l);
-               models.push_back(new_model);
-       }
-       data_mutex.unlock();
-       return models;
-}
-
 void loaded_object::draw(glm::mat4 vp) {
        glUseProgram(program);
-       std::vector<glm::mat4> models = create_models(vp);
+       std::vector<glm::mat4> models = create_models();
        glBindBuffer(GL_SHADER_STORAGE_BUFFER, models_buffer);
        glBufferData(GL_SHADER_STORAGE_BUFFER, models.size() * sizeof(glm::mat4), models.data(), GL_STATIC_DRAW);
        glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, models_buffer);
diff --git a/game.h b/game.h
index 05c3e36bbdd232b698f02669aed4f6b20babef02..a2d62441513492eaca8f18aa9721f262a01d0103 100644 (file)
--- a/game.h
+++ b/game.h
@@ -44,6 +44,7 @@ class gameobject {
                virtual int init() { return 0; }
                virtual void deinit() {};
                virtual void draw(glm::mat4) {}
+               virtual std::vector<glm::mat4> create_models();
                virtual void move() {}
                virtual void animate() {}
                virtual bool is_on_idx(glm::vec3 position, size_t index, float height) {return false;}
@@ -67,7 +68,6 @@ public:
                loaded_object(const char* of, const char* tf, glm::vec3 s) : objectfile(of), texturefile(tf) { size = s;        }
                
                int init() override ;
-               virtual std::vector<glm::mat4> create_models(glm::mat4 vp);
                void draw(glm::mat4 vp) override;
 };
 
diff --git a/geometric_objects.cpp b/geometric_objects.cpp
new file mode 100644 (file)
index 0000000..f89b843
--- /dev/null
@@ -0,0 +1,74 @@
+#include "game.h"
+#include "geometric_objects.h"
+
+int flat_panel::init()  {
+       // Initialization part
+       float vertices[] = {    
+               -1.0, 0,  1.0,
+               1.0, 0,  1.0,
+               1.0,  0,  -1.0,
+               -1.0, 0,  1.0,
+               1.0, 0,  -1.0,
+               -1.0, 0,  -1.0,
+       };
+       glGenBuffers(1, &vbuf);
+       glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbuf);
+       glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
+
+       program = make_program("panel_vertex_shader.glsl",0, 0, 0, "loaded_object_fragment_shader.glsl");
+       if (!program)
+               return 1;
+
+       tex = load_texture(texturefile);
+
+       glGenBuffers(1, &models_buffer);
+       glGenBuffers(1, &scales_buffer);
+
+       v_attrib = glGetAttribLocation(program, "in_vertex");
+       mvp_uniform = glGetUniformLocation(program, "vp");
+       return 0;
+}
+
+void flat_panel::addpanel(glm::vec3 location, glm::vec2 object_scale, glm::vec2 texture_scale){
+       locations.push_back(location);
+       object_scales.push_back(object_scale);
+       texture_scales.push_back(texture_scale);
+
+}
+
+std::vector<glm::mat4> flat_panel::create_models(){
+       std::vector<glm::mat4> models;
+       models.reserve(locations.size());
+       for(size_t i = 0; i < locations.size(); i++){
+               glm::mat4 new_model = glm::mat4(1.0f);
+               new_model = glm::translate(new_model, locations[i]);
+               new_model = glm::scale(new_model, glm::vec3(object_scales[i].x, 1.0f, object_scales[i].y));
+               models.push_back(new_model);
+       }
+       return models;
+}
+
+void flat_panel::draw(glm::mat4 vp)  {
+       glUseProgram(program);
+       data_mutex.lock();
+        std::vector<glm::mat4> models = create_models();
+        glBindBuffer(GL_SHADER_STORAGE_BUFFER, models_buffer);
+        glBufferData(GL_SHADER_STORAGE_BUFFER, models.size() * sizeof(glm::mat4), models.data(), GL_STATIC_DRAW);
+        glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, models_buffer);
+        
+       glBindBuffer(GL_SHADER_STORAGE_BUFFER, scales_buffer);
+        glBufferData(GL_SHADER_STORAGE_BUFFER, texture_scales.size() * sizeof(glm::vec2), texture_scales.data(), GL_STATIC_DRAW);
+        glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, scales_buffer);
+       data_mutex.unlock();
+
+       glEnableVertexAttribArray(v_attrib);
+       glBindBuffer(GL_ARRAY_BUFFER, vbuf);
+       glVertexAttribPointer(v_attrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
+
+       glActiveTexture(GL_TEXTURE0);
+       glBindTexture(GL_TEXTURE_2D, tex);
+
+       glUniformMatrix4fv(mvp_uniform, 1, 0, glm::value_ptr(vp));
+
+       glDrawArraysInstanced(GL_TRIANGLES, 0, 6, models.size());
+}
diff --git a/geometric_objects.h b/geometric_objects.h
new file mode 100644 (file)
index 0000000..8ed5a03
--- /dev/null
@@ -0,0 +1,14 @@
+#pragma once
+
+class flat_panel : public gameobject {
+public:
+       std::vector<glm::vec2> object_scales, texture_scales;
+       unsigned scales_buffer;
+       const char *texturefile;
+       flat_panel(const char* tf) : texturefile(tf) {}
+       int init() override;
+       void draw(glm::mat4 vp) override;
+       std::vector<glm::mat4> create_models();
+       void addpanel(glm::vec3 location, glm::vec2 object_scale, glm::vec2 texture_scale);
+};
+
index e95fa3da51e417ab5c71d357e2be92d0b661014e..031841a412e54bff5175088d45febb0934ca79ee 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -11,6 +11,7 @@
 #include "tile_floor.h"
 #include "moving_platform.h"
 #include "turret.h"
+#include "geometric_objects.h"
 
 std::mutex grand_mutex;
 
@@ -185,7 +186,7 @@ void object_movement(){
                int cpu_time = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();
                last_call = start_time;
                int sleep_time = 1000 - cpu_time;
-               printf("Loop time:  %d          Sleep time:  %d CPU time:  %d\n", loop_time, sleep_time, cpu_time);
+//             printf("Loop time:  %d          Sleep time:  %d CPU time:  %d\n", loop_time, sleep_time, cpu_time);
                if(sleep_time > 100 )
                        std::this_thread::sleep_for(std::chrono::microseconds(sleep_time));
        }
@@ -304,6 +305,7 @@ int main(int argc, char** argv) {
 
 
        /* Level Loading (hardcoded at the moment) */
+       /*
        tile_floor fl;
        objects.push_back(&ice_balls);
        objects.push_back(&fl);
@@ -333,14 +335,14 @@ int main(int argc, char** argv) {
        target_spawning.add_area(glm::vec3(10, 0, 10), bob, 1);
        target_spawning.add_area(glm::vec3(100, 5, 300), activate_turret, 2);
        objects.push_back(&target_spawning);
-
-       /*
+*/
+       
        wall_block monster_box("cube.obj", "monster1.png", glm::vec3(10, 10, 10));
        monster_box.scale = 5.0;
-       monster_box.locations.push_back(glm::vec3(90, -5, 300 ));
+       monster_box.locations.push_back(glm::vec3(53, -10, 50));
        objects.push_back(&monster_box);
-       */
-
+       
+/*
        // What if it wasn't a cube?
        wall_block bwb("cube.obj", "brick.jpg", glm::vec3(20, 20, 20));
        bwb.scale = 10;
@@ -353,7 +355,12 @@ int main(int argc, char** argv) {
        t.current_target = &targets;
        t.current_projectile = &ice_balls;
        objects.push_back(&t);
-
+*/
+       flat_panel p("nutsandbolts.jpg");
+       p.addpanel(glm::vec3(63, 0, 50), glm::vec2(2, 2), glm::vec2(2.0, 2.0));
+       p.addpanel(glm::vec3(58, 0, 50), glm::vec2(2, 2), glm::vec2(0.5, 0.5));
+       p.addpanel(glm::vec3(53, 0, 50), glm::vec2(2, 2), glm::vec2(1, 1));
+       objects.push_back(&p);  
 
        /* Initialize game objects */
        for(gameobject* o : objects){
index 53e7c423ce58c6ac736d22b792c2813522cfdfdc..0ad4f37dba430168bca1785a6ea2091a8f1eb4da 100644 (file)
--- a/turret.h
+++ b/turret.h
@@ -42,12 +42,11 @@ public:
                        rotation += 0.01;
        }
 
-       std::vector<glm::mat4> create_models(glm::mat4 vp) override {
+       std::vector<glm::mat4> create_models() override {
                std::vector<glm::mat4> models;
                data_mutex.lock();
                models.reserve(locations.size());
                for(glm::vec3 l : locations){
-                       printf("create_models loop in turret\n");
                        glm::mat4 new_model = glm::mat4(1.0f);
                        new_model = translate(new_model, l);
                        new_model = rotate(new_model, rotation, glm::vec3(0, 1, 0));