From 57e6e562175f1e778bf65245e9902325ba75daae Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Sun, 2 Oct 2016 17:22:47 +0200 Subject: [PATCH] making code for particle batch modular. --- data/shader/particle.vert | 17 +++-- game/renderer_polygon_3d/particle_batch.cpp | 69 ++++++++++--------- game/renderer_polygon_3d/particle_batch.hpp | 30 +++++--- .../renderer_polygon_3d.cpp | 3 +- 4 files changed, 73 insertions(+), 46 deletions(-) diff --git a/data/shader/particle.vert b/data/shader/particle.vert index 68f698d..b445d97 100644 --- a/data/shader/particle.vert +++ b/data/shader/particle.vert @@ -1,7 +1,8 @@ #version 120 -attribute vec2 in_vertex; -attribute vec3 in_position; -attribute vec3 in_velocity; +attribute vec2 in_geometry; +attribute vec3 in_position; +attribute vec3 in_velocity; +attribute float in_maxDist; uniform float age; uniform float size; @@ -28,11 +29,17 @@ void main() decay = ageMod / halfAge; // faster particles are smaller + // TODO: scale by time too! scale down fast after 3 halfAges float scaleBySpeed = (1.0 - 0.95*length(in_velocity)/maxVelocity); float finalSize = size * scaleBySpeed; - vec2 base = in_vertex; + vec2 base = in_geometry; vec3 p = finalSize*vec3(base, 0.0); - vec3 offset = (0.2*age + log(1.0+age*5.0)) * in_velocity + in_position; + vec3 move = (0.2*age + log(1.0+age*5.0)) * in_velocity; + float md = length(move); + if (md > in_maxDist) { + move *= in_maxDist / md; + } + vec3 offset = move + in_position; p += offset; gl_Position = vec4(p, 1.0); diff --git a/game/renderer_polygon_3d/particle_batch.cpp b/game/renderer_polygon_3d/particle_batch.cpp index 8fa8fec..755c6af 100644 --- a/game/renderer_polygon_3d/particle_batch.cpp +++ b/game/renderer_polygon_3d/particle_batch.cpp @@ -4,7 +4,7 @@ // TODO: use VAO's as soon as this is working -int getDivisorForIndex(int index) +int getDivisorForIndex(size_t index) { // 0 or 1? return (index == 0) ? 0 : 3; @@ -20,42 +20,52 @@ namespace endofthejedi { , m_maxVelocity(1.0) , m_center(glm::vec3(0.0f, 0.0f, 0.0f)) - // 2d quad drawn as a triangle fan - , m_data_quad({ + // 2d quad drawn as a triangle fan. + // + // TODO + // it is transformed before uploading so it looks at the camera + , m_data_geometry({ 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f}) { + m_num_vertex_buffers = 4; + //std::cout<<"[ParticleBatch] create for " << numParticles << " num particles" << std::endl; + m_attr_locations.resize(m_num_vertex_buffers); + m_data_vbos.resize(m_num_vertex_buffers); m_data_position.resize(m_numParticles); m_data_velocity.resize(m_numParticles); - m_data_kind.resize(m_numParticles); - m_data_max_age.resize(m_numParticles); + m_data_max_distance.resize(m_numParticles); + + for (size_t i=0; iprogram(), name); m_attr_locations[i] = loc; - //std::cout<<"attr location " << i << " " << loc << " " << name << std::endl; + //std::cout<<"attr location for " << name << " (#" << i << ") is " << loc << std::endl; } } @@ -69,7 +79,7 @@ namespace endofthejedi { m_maxVelocity = maxVelocity; } - void ParticleBatch::setParticle(size_t index, const glm::vec3 &p, const glm::vec3 &v) + void ParticleBatch::setParticle(size_t index, const glm::vec3 &p, const glm::vec3 &v, float maxDist) { if (index >= m_numParticles) { return; @@ -77,17 +87,16 @@ namespace endofthejedi { //std::cout<<"[ParticleBatch] setParticle " << index << std::endl; - m_data_position[index] = p; - m_data_velocity[index] = v; - m_data_kind[index] = 0.0; - m_data_max_age[index] = 0.0; + m_data_position[index] = p; + m_data_velocity[index] = v; + m_data_max_distance[index] = maxDist; } void ParticleBatch::bind() { //std::cout<<"[ParticleBatch] bind" << std::endl; - for (size_t i=0; i<5; i++) { + for (size_t i=0; i m_data_vbos; + std::vector m_attr_locations; - std::vector m_data_quad; + // vertex attributes + std::vector m_data_geometry; std::vector m_data_position; std::vector m_data_velocity; - std::vector m_data_kind; - std::vector m_data_max_age; - - GLuint m_attr_locations[5]; + std::vector m_data_max_distance; }; } diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.cpp b/game/renderer_polygon_3d/renderer_polygon_3d.cpp index 1c2ad02..47295ac 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.cpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.cpp @@ -124,7 +124,8 @@ namespace endofthejedi { glm::vec3 v = glm::ballRand(maxVelocity); v *= util::randf_0_1() * util::randf_0_1() * util::randf_0_1(); - batch->setParticle(i, glm::vec3(pos, 0.0) + glm::ballRand(explCoreSize), v); + float maxDist = 0.2; + batch->setParticle(i, glm::vec3(pos, 0.0) + glm::ballRand(explCoreSize), v, maxDist); } batch->upload();