From 102e86013d3a48e488fc6bfe657d0cc4e2d4253b Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 22 Oct 2024 10:18:11 -0700 Subject: [PATCH] Started geometric objects --- Makefile | 4 +-- baseclass.cpp | 28 ++++++++-------- game.h | 2 +- geometric_objects.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++ geometric_objects.h | 14 ++++++++ main.cpp | 21 ++++++++---- turret.h | 3 +- 7 files changed, 120 insertions(+), 26 deletions(-) create mode 100644 geometric_objects.cpp create mode 100644 geometric_objects.h diff --git a/Makefile b/Makefile index a962bbf..ca9d8c7 100644 --- 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 diff --git a/baseclass.cpp b/baseclass.cpp index 67a3973..2f0c5ed 100644 --- a/baseclass.cpp +++ b/baseclass.cpp @@ -1,5 +1,18 @@ #include "game.h" +std::vector gameobject::create_models(){ + std::vector 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 vertices; @@ -30,22 +43,9 @@ int loaded_object::init() { return 0; } -std::vector loaded_object::create_models(glm::mat4 vp){ - std::vector 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 models = create_models(vp); + std::vector 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 05c3e36..a2d6244 100644 --- 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 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 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 index 0000000..f89b843 --- /dev/null +++ b/geometric_objects.cpp @@ -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 flat_panel::create_models(){ + std::vector 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 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 index 0000000..8ed5a03 --- /dev/null +++ b/geometric_objects.h @@ -0,0 +1,14 @@ +#pragma once + +class flat_panel : public gameobject { +public: + std::vector 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 create_models(); + void addpanel(glm::vec3 location, glm::vec2 object_scale, glm::vec2 texture_scale); +}; + diff --git a/main.cpp b/main.cpp index e95fa3d..031841a 100644 --- 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(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){ diff --git a/turret.h b/turret.h index 53e7c42..0ad4f37 100644 --- a/turret.h +++ b/turret.h @@ -42,12 +42,11 @@ public: rotation += 0.01; } - std::vector create_models(glm::mat4 vp) override { + std::vector create_models() override { std::vector 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)); -- 2.39.5