From eea0c57e7c0355cfad270701114a8c079647bdc2 Mon Sep 17 00:00:00 2001 From: karl Date: Sun, 9 May 2021 23:32:41 +0200 Subject: [PATCH] Add multiple particle textures and more variety to types --- Shader/particle_render.fs | 10 ++++++++-- Shader/particle_render.gs | 3 +++ Shader/particle_update.gs | 18 +++++++++--------- cpp/ParticleSystem.cpp | 10 +++++++--- cpp/Texture.cpp | 5 ++--- include/Gedeng/ParticleSystem.h | 6 ++++-- test/particle-demo/main.cpp | 11 +++++++++-- 7 files changed, 42 insertions(+), 21 deletions(-) diff --git a/Shader/particle_render.fs b/Shader/particle_render.fs index 90754a8..21df2db 100644 --- a/Shader/particle_render.fs +++ b/Shader/particle_render.fs @@ -1,13 +1,19 @@ #version 430 -layout (binding = 0) uniform sampler2D texture; +layout (binding = 0) uniform sampler2D texture1; +layout (binding = 1) uniform sampler2D texture2; +layout (binding = 2) uniform sampler2D texture3; smooth in vec2 tex_coords; flat in vec4 color_part; +in float type; out vec4 FragColor; void main() { - FragColor = texture2D(texture, tex_coords) * color_part; + FragColor = texture2D(texture1, tex_coords) * color_part * float(abs(type - 1.0) < 0.1) + + texture2D(texture2, tex_coords) * color_part * float(abs(type - 2.0) < 0.1) + + texture2D(texture3, tex_coords) * color_part * float(abs(type - 3.0) < 0.1); + } diff --git a/Shader/particle_render.gs b/Shader/particle_render.gs index 2109e5b..48a7e8c 100644 --- a/Shader/particle_render.gs +++ b/Shader/particle_render.gs @@ -17,11 +17,14 @@ in float type_pass[]; smooth out vec2 tex_coords; flat out vec4 color_part; +out float type; void main() { if(type_pass[0] != 0.0) { // This is not a generator particle + type = type_pass[0]; + vec3 old_pos = gl_in[0].gl_Position.xyz; float size = size_pass[0]; mat4 view_projection_matrix = projection * view; diff --git a/Shader/particle_update.gs b/Shader/particle_update.gs index 4976da3..5c28e9a 100644 --- a/Shader/particle_update.gs +++ b/Shader/particle_update.gs @@ -91,17 +91,17 @@ void main() { EndPrimitive(); } else if (type_out == 1.0) { // The lifetime is over -- transform this particle in a new one or discard - for(int i = 0; i < 5; i++) { + for(int i = 0; i < 15; i++) { // Keep the position - velocity_out = spawn_velocity_min + vec3( + velocity_out = (spawn_velocity_min + vec3( spawn_velocity_range.x * random_zero_to_one(), spawn_velocity_range.y * random_zero_to_one(), spawn_velocity_range.z * random_zero_to_one() - ); + )) * 0.75; color_out = vec3(1.0, 0, 0); lifetime_out = spawn_lifetime_min + spawn_lifetime_range * random_zero_to_one(); - size_out = spawn_size; + size_out = spawn_size * 2.0; type_out = 2.0; EmitVertex(); @@ -109,17 +109,17 @@ void main() { } } else if (type_out == 2.0) { // The lifetime is over -- transform this particle in a new one or discard - for(int i = 0; i < 5; i++) { + for(int i = 0; i < 10; i++) { // Keep the position - velocity_out = spawn_velocity_min + vec3( + velocity_out = (spawn_velocity_min + vec3( spawn_velocity_range.x * random_zero_to_one(), spawn_velocity_range.y * random_zero_to_one(), spawn_velocity_range.z * random_zero_to_one() - ); + )) * 0.5; color_out = vec3(0.0, 1.0, 0); - lifetime_out = spawn_lifetime_min + spawn_lifetime_range * random_zero_to_one(); - size_out = spawn_size; + lifetime_out = (spawn_lifetime_min + spawn_lifetime_range * random_zero_to_one()) * 0.3; + size_out = spawn_size * 0.75; type_out = 3.0; EmitVertex(); diff --git a/cpp/ParticleSystem.cpp b/cpp/ParticleSystem.cpp index d3c4612..d227c35 100644 --- a/cpp/ParticleSystem.cpp +++ b/cpp/ParticleSystem.cpp @@ -122,7 +122,9 @@ void ParticleSystem::render() { render_shader.setVec3("quad1", quad1); render_shader.setVec3("quad2", quad2); - texture->bind_to(0); + texture1->bind_to(0); + texture2->bind_to(1); + texture3->bind_to(2); glBindVertexArray(vao[current_read_buffer]); glDisableVertexAttribArray(1); // Disable velocity, because we don't need it for rendering @@ -159,8 +161,10 @@ void ParticleSystem::set_interval(float interval) { void ParticleSystem::set_number_of_particles(int number) { this->target_particle_count = number; } -void ParticleSystem::set_texture(Texture *texture) { - this->texture = texture; +void ParticleSystem::set_textures(Texture *texture1, Texture *texture2, Texture *texture3) { + this->texture1 = texture1; + this->texture2 = texture2; + this->texture3 = texture3; } } // namespace Gedeng \ No newline at end of file diff --git a/cpp/Texture.cpp b/cpp/Texture.cpp index 863ea28..db12d7e 100644 --- a/cpp/Texture.cpp +++ b/cpp/Texture.cpp @@ -37,8 +37,7 @@ uint Texture::load_texture(const std::string &path, Settings settings) { else if (nrChannels == 4) glChannels = GL_RGBA; - glTexImage2D(GL_TEXTURE_2D, 0, glChannels, width, height, 0, glChannels, GL_UNSIGNED_BYTE, - data); + glTexImage2D(GL_TEXTURE_2D, 0, glChannels, width, height, 0, glChannels, GL_UNSIGNED_BYTE, data); if (settings.mipmaps) { glGenerateMipmap(GL_TEXTURE_2D); @@ -57,4 +56,4 @@ void Texture::bind_to(int unit) { glBindTexture(GL_TEXTURE_2D, id); } -} \ No newline at end of file +} // namespace Gedeng \ No newline at end of file diff --git a/include/Gedeng/ParticleSystem.h b/include/Gedeng/ParticleSystem.h index a3fad6d..28d713e 100644 --- a/include/Gedeng/ParticleSystem.h +++ b/include/Gedeng/ParticleSystem.h @@ -30,7 +30,7 @@ class ParticleSystem { void set_size(float size); void set_interval(float interval); void set_number_of_particles(int number); - void set_texture(Texture *texture); + void set_textures(Texture *texture1, Texture *texture2, Texture *texture3); void clear(); void release(); @@ -49,7 +49,9 @@ class ParticleSystem { GLuint query; - Texture *texture; + Texture *texture1; + Texture *texture2; + Texture *texture3; int current_read_buffer; int current_particle_count; diff --git a/test/particle-demo/main.cpp b/test/particle-demo/main.cpp index 0600932..28eea8a 100644 --- a/test/particle-demo/main.cpp +++ b/test/particle-demo/main.cpp @@ -12,13 +12,16 @@ class ParticleApp : public Gedeng::Application { public: ParticleApp(unsigned long ms_per_update, unsigned int window_size_x, unsigned int window_size_y, Gedeng::String window_name) - : Application(ms_per_update, window_size_x, window_size_y, window_name), particle_interval(0.1), + : Application(ms_per_update, window_size_x, window_size_y, window_name), particle_interval(0.2), number_of_steps(10.0), number_of_refinement_steps(10.0), bump_depth(0.1), render_shader(Gedeng::Shader("Shader/bump.vs", "Shader/bump.fs")), camera(Gedeng::FPSCamera(90, 1920, 1080, 0.1, 1000.0)), albedo("Resources/Textures/PavingStones/PavingStones070_2K_Color.jpg", Gedeng::Texture::Settings()), bump("Resources/Textures/PavingStones/PavingStones070_2K_Displacement.jpg", Gedeng::Texture::Settings()), normal("Resources/Textures/PavingStones/PavingStones070_2K_Normal.jpg", Gedeng::Texture::Settings()), + particle_tex1("Resources/Textures/Particles/circle.png", Gedeng::Texture::Settings()), + particle_tex2("Resources/Textures/Particles/magic.png", Gedeng::Texture::Settings()), + particle_tex3("Resources/Textures/Particles/smoke.png", Gedeng::Texture::Settings()), quad_mesh(Gedeng::QuadMesh(10.0)) { particles.set_position(glm::vec3(0.0f, 0.0f, -10.0f)); particles.set_velocity(glm::vec3(-2, 4, -2), glm::vec3(2, 6, 2)); @@ -28,7 +31,7 @@ class ParticleApp : public Gedeng::Application { particles.set_size(0.1); particles.set_interval(particle_interval); particles.set_number_of_particles(1); - particles.set_texture(&albedo); + particles.set_textures(&particle_tex1, &particle_tex2, &particle_tex3); camera.translate(glm::vec3(0.0, 2.0, 1.0)); // camera.rotate(30, glm::vec3(1.0, 0.0, 0.0)); @@ -97,6 +100,10 @@ class ParticleApp : public Gedeng::Application { Gedeng::Texture bump; Gedeng::Texture normal; + Gedeng::Texture particle_tex1; + Gedeng::Texture particle_tex2; + Gedeng::Texture particle_tex3; + Gedeng::QuadMesh quad_mesh; Gedeng::ParticleSystem particles;