From: Seth Long Date: Tue, 5 Nov 2024 01:28:12 +0000 (-0800) Subject: Revised panels to use normal+up vectors for orientation X-Git-Url: https://isoptera.lcsc.edu/gitweb/?a=commitdiff_plain;h=488a830c0567e2ae0a2101e9f0e9127d93dc205d;p=example_engine%2F.git Revised panels to use normal+up vectors for orientation --- diff --git a/BarredRock.jpg b/BarredRock.jpg new file mode 100644 index 0000000..1308dd4 Binary files /dev/null and b/BarredRock.jpg differ diff --git a/JerseyGiant.jpg b/JerseyGiant.jpg new file mode 100644 index 0000000..c2fa244 Binary files /dev/null and b/JerseyGiant.jpg differ diff --git a/RhodeIslandRed.jpg b/RhodeIslandRed.jpg new file mode 100644 index 0000000..4b8ef2f Binary files /dev/null and b/RhodeIslandRed.jpg differ diff --git a/geometric_objects.cpp b/geometric_objects.cpp index f89b843..afbe5bd 100644 --- a/geometric_objects.cpp +++ b/geometric_objects.cpp @@ -4,18 +4,18 @@ 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, + -1.0, 1.0, + 1.0, 1.0, + 1.0, -1.0, + -1.0, 1.0, + 1.0, -1.0, + -1.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"); + program = make_program("normal_panel_vertex_shader.glsl",0, 0, 0, "loaded_object_fragment_shader.glsl"); if (!program) return 1; @@ -23,17 +23,33 @@ int flat_panel::init() { glGenBuffers(1, &models_buffer); glGenBuffers(1, &scales_buffer); + glGenBuffers(1, &normals_buffer); + glGenBuffers(1, &ups_buffer); + glGenBuffers(1, &object_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){ +void flat_panel::addpanel(glm::vec3 location, glm::vec2 object_scale, glm::vec2 texture_scale, glm::vec3 normal, glm::vec3 up){ locations.push_back(location); object_scales.push_back(object_scale); texture_scales.push_back(texture_scale); + normals.push_back(glm::vec4(glm::normalize(normal), 1.0f)); + ups.push_back(glm::vec4(glm::normalize(up), 1.0f)); +} + +void flat_panel::rotate(size_t index, float angle, glm::vec3 axis){ + +} +void flat_panel::set_normal(size_t index, glm::vec3 normal){ + normals[index] = glm::vec4(glm::normalize(normal), 1.0f); +} + +void flat_panel::set_up(size_t index, glm::vec3 up){ + ups[index] = glm::vec4(glm::normalize(up), 1.0f); } std::vector flat_panel::create_models(){ @@ -42,7 +58,6 @@ std::vector flat_panel::create_models(){ 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; @@ -51,19 +66,31 @@ std::vector flat_panel::create_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); - + 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); + 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); + + glBindBuffer(GL_SHADER_STORAGE_BUFFER, normals_buffer); + glBufferData(GL_SHADER_STORAGE_BUFFER, normals.size() * sizeof(normals[0]), normals.data(), GL_STATIC_DRAW); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, normals_buffer); + + glBindBuffer(GL_SHADER_STORAGE_BUFFER, ups_buffer); + glBufferData(GL_SHADER_STORAGE_BUFFER, ups.size() * sizeof(ups[0]), ups.data(), GL_STATIC_DRAW); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, ups_buffer); + + glBindBuffer(GL_SHADER_STORAGE_BUFFER, object_scales_buffer); + glBufferData(GL_SHADER_STORAGE_BUFFER, object_scales.size() * sizeof(object_scales[0]), object_scales.data(), GL_STATIC_DRAW); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, object_scales_buffer); data_mutex.unlock(); glEnableVertexAttribArray(v_attrib); glBindBuffer(GL_ARRAY_BUFFER, vbuf); - glVertexAttribPointer(v_attrib, 3, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(v_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex); diff --git a/geometric_objects.h b/geometric_objects.h index a9e2adf..6f729cc 100644 --- a/geometric_objects.h +++ b/geometric_objects.h @@ -1,18 +1,54 @@ #pragma once /* TODO: - * Normal Vector for panel to set orientation * Collision detection (can't move through panel) */ class flat_panel : public gameobject { public: std::vector object_scales, texture_scales; - unsigned scales_buffer; + std::vector normals, ups; + unsigned scales_buffer, normals_buffer, ups_buffer, object_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); + void addpanel(glm::vec3 location, glm::vec2 object_scale, glm::vec2 texture_scale, glm::vec3 normal = glm::vec3(0, 1, 0), glm::vec3 up = glm::vec3(0, 0, 1)); + void rotate(size_t index, float angle, glm::vec3 axis); + void set_normal(size_t index, glm::vec3 normal); + void set_up(size_t index, glm::vec3 normal); }; +class rotating_panel : public flat_panel { +public: + float angle = 0.0f; + rotating_panel(const char* tf) : flat_panel(tf) {} + void move(int elapsed_time){ + angle = elapsed_time * 0.000003f; + for(size_t i = 0; i < normals.size(); i++){ + normals[i] = glm::rotate(glm::mat4(1.0f), angle, glm::vec3(ups[i].x, ups[i].y, ups[i].z)) * normals[i]; + } + } +}; + +class y_rotating_panel : public flat_panel { +public: + float angle = 0.0f; + y_rotating_panel(const char* tf) : flat_panel(tf) {} + void move(int elapsed_time){ + angle = elapsed_time * 0.000003f; + for(size_t i = 0; i < normals.size(); i++){ + normals[i] = glm::rotate(glm::mat4(1.0f), angle, glm::vec3(0, 1, 0)) * normals[i]; + ups[i] = glm::rotate(glm::mat4(1.0f), angle, glm::vec3(0, 1, 0)) * ups[i]; + } + } +}; + +class face_player_panel : public flat_panel { +public: + face_player_panel(const char* tf) : flat_panel(tf) {} + void move(int elapsed_time){ + for(size_t i = 0; i < locations.size(); i++) + set_normal(i, locations[i] - player_position); + } +}; diff --git a/hud.cpp b/hud.cpp index c46d2b8..7c13717 100644 --- a/hud.cpp +++ b/hud.cpp @@ -88,6 +88,7 @@ void hud::set_text(const char* text){ int hud::init(){ std::vector 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++){ diff --git a/main.cpp b/main.cpp index 81e8ad8..26b27f1 100644 --- a/main.cpp +++ b/main.cpp @@ -381,8 +381,26 @@ int main(int argc, char** argv) { p.addpanel(glm::vec3(32, -9.99, 450), glm::vec2(5, 5), glm::vec2(2.0, 2.0)); p.addpanel(glm::vec3(21, -9.99, 450), glm::vec2(5, 5), glm::vec2(0.5, 0.5)); p.addpanel(glm::vec3(10, -9.99, 450), glm::vec2(5, 5), glm::vec2(1, 1)); + p.addpanel(glm::vec3(-20, 0, 410), glm::vec2(10, 10), glm::vec2(1, 1), glm::vec4(0, 0, 1, 1), glm::vec4(0, 1, 0, 1)); objects.push_back(&p); + + rotating_panel rp("RhodeIslandRed.jpg"); + rp.addpanel(glm::vec3(-120, 10, 400), glm::vec2(10, 10), glm::vec2(1, 1), glm::vec3(0, 0, 1), glm::vec3(1, 1, 0)); + rp.addpanel(glm::vec3(-160, 10, 400), glm::vec2(10, 10), glm::vec2(1, 1), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0)); + rp.addpanel(glm::vec3(-200, 10, 400), glm::vec2(10, 10), glm::vec2(1, 1), glm::vec3(0, 0, 1), glm::vec3(-1, 1, 0)); + objects.push_back(&rp); + + y_rotating_panel yrp("JerseyGiant.jpg"); + yrp.addpanel(glm::vec3(-300, -5, 300), glm::vec2(10, 10), glm::vec2(1, 1), glm::vec3(0, 1, -1), glm::vec3(0, 1, 1)); + yrp.addpanel(glm::vec3(-300, -5, 350), glm::vec2(10, 10), glm::vec2(1, 1), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1)); + yrp.addpanel(glm::vec3(-300, -5, 400), glm::vec2(10, 10), glm::vec2(1, 1), glm::vec3(0, 0.5, 1), glm::vec3(0, 0.5, -1)); + objects.push_back(&yrp); + + face_player_panel fpp("BarredRock.jpg"); + fpp.addpanel(glm::vec3(-30, 0, 500), glm::vec2(10, 10), glm::vec2(2.0, 2.0), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0)); + objects.push_back(&fpp); + aimpoint main_aimpoint; objects.push_back(&main_aimpoint); diff --git a/normal_panel_vertex_shader.glsl b/normal_panel_vertex_shader.glsl new file mode 100644 index 0000000..6e9c439 --- /dev/null +++ b/normal_panel_vertex_shader.glsl @@ -0,0 +1,29 @@ +#version 460 + +layout(packed, binding=0) buffer model_list { + mat4 models[]; +}; +layout(packed, binding=1) buffer scale_list { + vec2 scales[]; +}; +layout(packed, binding=2) buffer normal_list { + vec3 normals[]; +}; +layout(packed, binding=3) buffer up_list { + vec3 ups[]; +}; +layout(packed, binding=4) buffer object_scale_list { + vec2 object_scales[]; +}; +in vec2 in_vertex; +uniform mat4 vp; +out vec2 frag_texcoord; +out vec4 gl_Position; + +void main(void) { + vec3 sideways = normalize(cross(ups[gl_InstanceID], normals[gl_InstanceID])); + vec3 pos = in_vertex.x * object_scales[gl_InstanceID].x * ups[gl_InstanceID] + + in_vertex.y * object_scales[gl_InstanceID].y * sideways; + gl_Position = vp * models[gl_InstanceID] * vec4(pos, 1.0); + frag_texcoord = (vec2(in_vertex.y, -in_vertex.x) * 0.5 + vec2(0.5, 0.5)) * scales[gl_InstanceID]; +} diff --git a/turret.h b/turret.h index 64ca880..fd2b831 100644 --- a/turret.h +++ b/turret.h @@ -25,7 +25,7 @@ public: countdown--; return; } - countdown = 314; + countdown = 50; if(!current_target->locations.size()) return; for(auto l : locations){