Skip to content
Snippets Groups Projects
Commit 61116010 authored by Micah Elizabeth Scott's avatar Micah Elizabeth Scott
Browse files

Switch to particle kernel function with compact support

This gives us opportunities to improve performance, since particles have a finite distance over which they
provide a contribution to the scene. This also includes kernel function and its derivative for use by subclasses.
parent ded78371
No related branches found
No related tags found
No related merge requests found
......@@ -39,8 +39,8 @@ public:
struct ParticleAppearance {
Vec3 point;
Vec3 color;
float radius;
float intensity;
float falloff;
};
virtual void calculatePixel(Vec3& rgb, const PixelInfo& p);
......@@ -51,6 +51,20 @@ protected:
* or keep it persistent across frames and update the parts you're changing.
*/
std::vector<ParticleAppearance> displayList;
/*
* Kernel function; determines particle shape
* Poly6 kernel, Müller, Charypar, & Gross (2003)
* q normalized in range [0, 1].
* Has compact support; kernel forced to zero outside this range.
*/
float kernel(float q);
// Variant of kernel function called with q^2
float kernel2(float q2);
// First derivative of kernel()
float kernelDerivative(float q);
};
......@@ -59,6 +73,25 @@ protected:
*****************************************************************************************/
inline float ParticleEffect::kernel(float q)
{
float a = 1 - q * q;
return a * a * a;
}
inline float ParticleEffect::kernel2(float q2)
{
float a = 1 - q2;
return a * a * a;
}
inline float ParticleEffect::kernelDerivative(float q)
{
float a = 1 - q * q;
return -6.0f * q * a * a;
}
inline void ParticleEffect::calculatePixel(Vec3& rgb, const PixelInfo& p)
{
Vec3 accumulator(0, 0, 0);
......@@ -69,8 +102,12 @@ inline void ParticleEffect::calculatePixel(Vec3& rgb, const PixelInfo& p)
for (; i != e; ++i) {
ParticleAppearance &particle = *i;
float dist2 = sqrlen(particle.point - point);
float intensity = particle.intensity / (1.0f + particle.falloff * dist2);
accumulator += particle.color * intensity;
// Normalized distance
float q2 = dist2 / sq(particle.radius);
if (q2 < 1.0f) {
accumulator += particle.color * (particle.intensity * kernel2(q2));
}
}
rgb = accumulator;
......
// Simple particle system example for C++. Ported from the "particle_trail" Node.js example.
// Simple particle system example for C++.
#include <math.h>
#include "lib/color.h"
......@@ -26,7 +26,7 @@ public:
ParticleAppearance& p = displayList[i];
p.point = Vec3(x, 0, y);
p.intensity = 0.2f * s;
p.falloff = 60.0f;
p.radius = 0.1 + 0.4f * s;
hsv2rgb(p.color, hue, 0.5, 0.8);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment