Compare commits

..

4 Commits

10 changed files with 122 additions and 39 deletions

View File

@ -1,13 +1,19 @@
#version 430 #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; smooth in vec2 tex_coords;
flat in vec4 color_part; flat in vec4 color_part;
in float type;
out vec4 FragColor; out vec4 FragColor;
void main() 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);
} }

View File

@ -3,7 +3,6 @@
uniform mat4 projection; uniform mat4 projection;
uniform mat4 view; uniform mat4 view;
uniform vec3 camera_pos;
uniform vec3 quad1, quad2; uniform vec3 quad1, quad2;
@ -18,12 +17,15 @@ in float type_pass[];
smooth out vec2 tex_coords; smooth out vec2 tex_coords;
flat out vec4 color_part; flat out vec4 color_part;
out float type;
void main() { void main() {
if(type_pass[0] != 0.0) { if(type_pass[0] != 0.0) {
// This is not a generator particle // This is not a generator particle
vec3 old_pos = gl_in[0].gl_Position.xyz - camera_pos; type = type_pass[0];
vec3 old_pos = gl_in[0].gl_Position.xyz;
float size = size_pass[0]; float size = size_pass[0];
mat4 view_projection_matrix = projection * view; mat4 view_projection_matrix = projection * view;

View File

@ -29,7 +29,7 @@ uniform vec3 spawn_color;
uniform float spawn_size; uniform float spawn_size;
uniform float spawn_lifetime_min, spawn_lifetime_range; // Life of new particle - from min to (min+range) uniform float spawn_lifetime_min, spawn_lifetime_range; // Life of new particle - from min to (min+range)
uniform float time_passed; // Time passed since last frame uniform float delta; // Time passed since last frame
uniform vec3 random_seed; // Seed number for our random number function uniform vec3 random_seed; // Seed number for our random number function
vec3 local_seed; vec3 local_seed;
@ -55,11 +55,11 @@ void main() {
position_out = position_pass[0]; position_out = position_pass[0];
velocity_out = velocity_pass[0]; velocity_out = velocity_pass[0];
if(type_pass[0] != 0.0) position_out += velocity_out * time_passed; if(type_pass[0] != 0.0) position_out += velocity_out * delta;
if(type_pass[0] != 0.0) velocity_out += spawn_gravity * time_passed; if(type_pass[0] != 0.0) velocity_out += spawn_gravity * delta;
color_out = color_pass[0]; color_out = color_pass[0];
lifetime_out = lifetime_pass[0] - time_passed; lifetime_out = lifetime_pass[0] - delta;
size_out = size_pass[0]; size_out = size_pass[0];
type_out = type_pass[0]; type_out = type_pass[0];
@ -91,17 +91,17 @@ void main() {
EndPrimitive(); EndPrimitive();
} else if (type_out == 1.0) { } else if (type_out == 1.0) {
// The lifetime is over -- transform this particle in a new one or discard // 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 // 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.x * random_zero_to_one(),
spawn_velocity_range.y * random_zero_to_one(), spawn_velocity_range.y * random_zero_to_one(),
spawn_velocity_range.z * random_zero_to_one() spawn_velocity_range.z * random_zero_to_one()
); )) * 0.75;
color_out = vec3(1.0, 0, 0); color_out = vec3(1.0, 0, 0);
lifetime_out = spawn_lifetime_min + spawn_lifetime_range * random_zero_to_one(); 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; type_out = 2.0;
EmitVertex(); EmitVertex();
@ -109,17 +109,17 @@ void main() {
} }
} else if (type_out == 2.0) { } else if (type_out == 2.0) {
// The lifetime is over -- transform this particle in a new one or discard // 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 // 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.x * random_zero_to_one(),
spawn_velocity_range.y * random_zero_to_one(), spawn_velocity_range.y * random_zero_to_one(),
spawn_velocity_range.z * random_zero_to_one() spawn_velocity_range.z * random_zero_to_one()
); )) * 0.5;
color_out = vec3(0.0, 1.0, 0); color_out = vec3(0.0, 1.0, 0);
lifetime_out = spawn_lifetime_min + spawn_lifetime_range * random_zero_to_one(); lifetime_out = (spawn_lifetime_min + spawn_lifetime_range * random_zero_to_one()) * 0.3;
size_out = spawn_size; size_out = spawn_size * 0.75;
type_out = 3.0; type_out = 3.0;
EmitVertex(); EmitVertex();

View File

@ -52,7 +52,7 @@ ParticleSystem::ParticleSystem() {
void ParticleSystem::update(float delta) { void ParticleSystem::update(float delta) {
update_shader.use(); update_shader.use();
update_shader.setFloat("time_passed", elapsed_time); update_shader.setFloat("delta", delta);
update_shader.setVec3("spawn_position", spawn_position); update_shader.setVec3("spawn_position", spawn_position);
update_shader.setVec3("spawn_velocity_min", spawn_velocity_min); update_shader.setVec3("spawn_velocity_min", spawn_velocity_min);
update_shader.setVec3("spawn_velocity_range", spawn_velocity_range); update_shader.setVec3("spawn_velocity_range", spawn_velocity_range);
@ -108,8 +108,6 @@ void ParticleSystem::set_camera(const Camera &camera) {
quad2 = glm::cross(camera.forward(), quad1); quad2 = glm::cross(camera.forward(), quad1);
quad2 = glm::normalize(quad2); quad2 = glm::normalize(quad2);
camera_pos = camera.get_translation();
} }
void ParticleSystem::render() { void ParticleSystem::render() {
@ -123,9 +121,10 @@ void ParticleSystem::render() {
render_shader.setMat4("view", view_matrix); render_shader.setMat4("view", view_matrix);
render_shader.setVec3("quad1", quad1); render_shader.setVec3("quad1", quad1);
render_shader.setVec3("quad2", quad2); render_shader.setVec3("quad2", quad2);
render_shader.setVec3("camera_pos", camera_pos);
texture->bind_to(0); texture1->bind_to(0);
texture2->bind_to(1);
texture3->bind_to(2);
glBindVertexArray(vao[current_read_buffer]); glBindVertexArray(vao[current_read_buffer]);
glDisableVertexAttribArray(1); // Disable velocity, because we don't need it for rendering glDisableVertexAttribArray(1); // Disable velocity, because we don't need it for rendering
@ -162,8 +161,10 @@ void ParticleSystem::set_interval(float interval) {
void ParticleSystem::set_number_of_particles(int number) { void ParticleSystem::set_number_of_particles(int number) {
this->target_particle_count = number; this->target_particle_count = number;
} }
void ParticleSystem::set_texture(Texture *texture) { void ParticleSystem::set_textures(Texture *texture1, Texture *texture2, Texture *texture3) {
this->texture = texture; this->texture1 = texture1;
this->texture2 = texture2;
this->texture3 = texture3;
} }
} // namespace Gedeng } // namespace Gedeng

View File

@ -37,8 +37,7 @@ uint Texture::load_texture(const std::string &path, Settings settings) {
else if (nrChannels == 4) else if (nrChannels == 4)
glChannels = GL_RGBA; glChannels = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, glChannels, width, height, 0, glChannels, GL_UNSIGNED_BYTE, glTexImage2D(GL_TEXTURE_2D, 0, glChannels, width, height, 0, glChannels, GL_UNSIGNED_BYTE, data);
data);
if (settings.mipmaps) { if (settings.mipmaps) {
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
@ -57,4 +56,4 @@ void Texture::bind_to(int unit) {
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
} }
} } // namespace Gedeng

View File

@ -7,6 +7,7 @@
#include <glm/gtx/dual_quaternion.hpp> #include <glm/gtx/dual_quaternion.hpp>
#include "Gedeng/Logger.h" #include "Gedeng/Logger.h"
#include "Gedeng/Ray.h"
#include "Input.h" #include "Input.h"
#include "Spatial.h" #include "Spatial.h"
@ -28,6 +29,10 @@ class Camera : public Spatial {
return glm::inverse(get_matrix()); return glm::inverse(get_matrix());
} }
Ray get_view_ray() const {
return Ray(get_translation(), forward());
}
private: private:
glm::mat4 projection; glm::mat4 projection;
}; };

View File

@ -18,6 +18,7 @@ class Input {
static void initialize(GLFWwindow *window) { static void initialize(GLFWwindow *window) {
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, mouse_callback); glfwSetCursorPosCallback(window, mouse_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
is_mouse_active = true; is_mouse_active = true;
@ -28,7 +29,7 @@ class Input {
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE); if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE);
Input::set_key_down(key, action == GLFW_RELEASE ? false : true); set_key_down(key, action == GLFW_RELEASE ? false : true);
} }
static void mouse_callback(GLFWwindow *window, double xpos, double ypos) { static void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
@ -36,6 +37,10 @@ class Input {
mouse_position.y = ypos; mouse_position.y = ypos;
} }
static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) {
set_mouse_down(button, action == GLFW_RELEASE ? false : true);
}
static void poll_input() { static void poll_input() {
glfwPollEvents(); glfwPollEvents();
} }
@ -48,12 +53,22 @@ class Input {
return keys_down[key]; return keys_down[key];
} }
static void set_mouse_down(int button, bool down) {
mouse_down[button] = down;
}
static bool is_mouse_down(int button) {
return mouse_down[button];
}
static glm::vec2 get_mouse_position() { static glm::vec2 get_mouse_position() {
return is_mouse_active ? mouse_position : glm::vec2(0.0, 0.0); return is_mouse_active ? mouse_position : glm::vec2(0.0, 0.0);
} }
private: private:
inline static std::map<int, bool> keys_down; inline static std::map<int, bool> keys_down;
inline static std::map<int, bool> mouse_down;
inline static glm::vec2 mouse_position; inline static glm::vec2 mouse_position;
inline static bool is_mouse_active; inline static bool is_mouse_active;
}; };

View File

@ -30,7 +30,7 @@ class ParticleSystem {
void set_size(float size); void set_size(float size);
void set_interval(float interval); void set_interval(float interval);
void set_number_of_particles(int number); void set_number_of_particles(int number);
void set_texture(Texture *texture); void set_textures(Texture *texture1, Texture *texture2, Texture *texture3);
void clear(); void clear();
void release(); void release();
@ -49,7 +49,9 @@ class ParticleSystem {
GLuint query; GLuint query;
Texture *texture; Texture *texture1;
Texture *texture2;
Texture *texture3;
int current_read_buffer; int current_read_buffer;
int current_particle_count; int current_particle_count;
@ -57,7 +59,6 @@ class ParticleSystem {
glm::mat4 projection_matrix, view_matrix; glm::mat4 projection_matrix, view_matrix;
glm::vec3 quad1, quad2; glm::vec3 quad1, quad2;
glm::vec3 camera_pos;
float elapsed_time; float elapsed_time;
float next_generation_time; float next_generation_time;

36
include/Gedeng/Ray.h Normal file
View File

@ -0,0 +1,36 @@
#include <glm/geometric.hpp>
#include <glm/glm.hpp>
namespace Gedeng {
class CollisionResult {
public:
explicit CollisionResult(bool is_colliding, glm::vec3 collision_position = glm::vec3(0, 0, 0))
: is_colliding(is_colliding), collision_position(collision_position) {
}
bool is_colliding;
glm::vec3 collision_position;
};
class Ray {
public:
glm::vec3 origin;
glm::vec3 direction;
Ray(glm::vec3 origin, glm::vec3 direction) : origin(origin), direction(direction) {
}
CollisionResult get_plane_collision(glm::vec3 normal = glm::vec3(0, 1, 0)) {
float denom = glm::dot(normal, direction);
if (glm::abs(denom) > 0.0001f) {
float t = glm::dot(-origin, normal) / denom;
if (t >= 0) return CollisionResult(true, origin + t * direction);
}
return CollisionResult(false);
}
};
} // namespace Gedeng

View File

@ -1,8 +1,10 @@
#include "Gedeng/Input.h"
#include "Gedeng/Logger.h" #include "Gedeng/Logger.h"
#include "Gedeng/ParticleSystem.h" #include "Gedeng/ParticleSystem.h"
#include "Gedeng/QuadMesh.h" #include "Gedeng/QuadMesh.h"
#include "Gedeng/TextLabel.h" #include "Gedeng/TextLabel.h"
#include "Gedeng/Vector3.h" #include "Gedeng/Vector3.h"
#include <GLFW/glfw3.h>
#define GEDENG_MAIN #define GEDENG_MAIN
#include <Gedeng.h> #include <Gedeng.h>
@ -10,23 +12,26 @@ class ParticleApp : public Gedeng::Application {
public: public:
ParticleApp(unsigned long ms_per_update, unsigned int window_size_x, unsigned int window_size_y, ParticleApp(unsigned long ms_per_update, unsigned int window_size_x, unsigned int window_size_y,
Gedeng::String window_name) Gedeng::String window_name)
: Application(ms_per_update, window_size_x, window_size_y, window_name), number_of_steps(10.0), : Application(ms_per_update, window_size_x, window_size_y, window_name), particle_interval(0.2),
number_of_refinement_steps(10.0), bump_depth(0.1), 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")), render_shader(Gedeng::Shader("Shader/bump.vs", "Shader/bump.fs")),
camera(Gedeng::FPSCamera(90, 1920, 1080, 0.1, 1000.0)), camera(Gedeng::FPSCamera(90, 1920, 1080, 0.1, 1000.0)),
albedo("Resources/Textures/PavingStones/PavingStones070_2K_Color.jpg", Gedeng::Texture::Settings()), albedo("Resources/Textures/PavingStones/PavingStones070_2K_Color.jpg", Gedeng::Texture::Settings()),
bump("Resources/Textures/PavingStones/PavingStones070_2K_Displacement.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()), 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)) { quad_mesh(Gedeng::QuadMesh(10.0)) {
particles.set_position(glm::vec3(0.0f, 0.0f, -5.0f)); particles.set_position(glm::vec3(0.0f, 0.0f, -10.0f));
particles.set_velocity(glm::vec3(-1, 0, -1), glm::vec3(1, 5, 1)); particles.set_velocity(glm::vec3(-2, 4, -2), glm::vec3(2, 6, 2));
particles.set_gravity(glm::vec3(0, -2, 0)); particles.set_gravity(glm::vec3(0, -4, 0));
particles.set_color(glm::vec3(0.0f, 0.5f, 1.0f)); particles.set_color(glm::vec3(0.0f, 0.5f, 1.0f));
particles.set_lifetime(1.0f, 1.5f); particles.set_lifetime(1.0f, 1.5f);
particles.set_size(0.1); particles.set_size(0.1);
particles.set_interval(0.05f); particles.set_interval(particle_interval);
particles.set_number_of_particles(1); 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.translate(glm::vec3(0.0, 2.0, 1.0));
// camera.rotate(30, glm::vec3(1.0, 0.0, 0.0)); // camera.rotate(30, glm::vec3(1.0, 0.0, 0.0));
@ -35,8 +40,15 @@ class ParticleApp : public Gedeng::Application {
~ParticleApp() = default; ~ParticleApp() = default;
void fixed_update(double delta) override { void fixed_update(double delta) override {
// camera.rotate(delta * 180, glm::vec3(0.0, 1.0, 0.0));
camera.update(delta); camera.update(delta);
if (Gedeng::Input::is_mouse_down(GLFW_MOUSE_BUTTON_LEFT))
particles.set_position(camera.get_view_ray().get_plane_collision().collision_position);
if (Gedeng::Input::is_key_down(GLFW_KEY_2)) particle_interval = fmax(particle_interval + 0.1 * delta, 0.02);
if (Gedeng::Input::is_key_down(GLFW_KEY_1)) particle_interval = fmax(particle_interval - 0.1 * delta, 0.02);
particles.set_interval(particle_interval);
} }
void dynamic_update(double delta) override { void dynamic_update(double delta) override {
@ -72,6 +84,8 @@ class ParticleApp : public Gedeng::Application {
} }
private: private:
float particle_interval;
float number_of_steps; float number_of_steps;
float number_of_refinement_steps; float number_of_refinement_steps;
float bump_depth; float bump_depth;
@ -86,6 +100,10 @@ class ParticleApp : public Gedeng::Application {
Gedeng::Texture bump; Gedeng::Texture bump;
Gedeng::Texture normal; Gedeng::Texture normal;
Gedeng::Texture particle_tex1;
Gedeng::Texture particle_tex2;
Gedeng::Texture particle_tex3;
Gedeng::QuadMesh quad_mesh; Gedeng::QuadMesh quad_mesh;
Gedeng::ParticleSystem particles; Gedeng::ParticleSystem particles;