Skip to content

Instantly share code, notes, and snippets.

@fenbf
Created May 12, 2014 15:48
Show Gist options
  • Save fenbf/80f0dae815167950ce42 to your computer and use it in GitHub Desktop.
Save fenbf/80f0dae815167950ce42 to your computer and use it in GitHub Desktop.
Basic Particle Generators modules
#include "particleGenerators.h"
#include <assert.h>
#include <algorithm>
#include <glm/common.hpp>
#include <glm/gtc/random.hpp>
namespace particles
{
namespace generators
{
void BoxPosGen::generate(double dt, ParticleData *p, size_t startId, size_t endId)
{
glm::vec4 posMin{ m_pos.x - m_maxStartPosOffset.x, m_pos.y - m_maxStartPosOffset.y, m_pos.z - m_maxStartPosOffset.z, 1.0 };
glm::vec4 posMax{ m_pos.x + m_maxStartPosOffset.x, m_pos.y + m_maxStartPosOffset.y, m_pos.z + m_maxStartPosOffset.z, 1.0 };
for (size_t i = startId; i < endId; ++i)
{
p->m_pos[i] = glm::linearRand(posMin, posMax);
}
}
void RoundPosGen::generate(double dt, ParticleData *p, size_t startId, size_t endId)
{
for (size_t i = startId; i < endId; ++i)
{
double ang = glm::linearRand(0.0, M_PI*2.0);
p->m_pos[i] = m_center + glm::vec4(m_radX*sin(ang), m_radY*cos(ang), 0.0, 1.0);
}
}
void BasicColorGen::generate(double dt, ParticleData *p, size_t startId, size_t endId)
{
for (size_t i = startId; i < endId; ++i)
{
p->m_startCol[i] = glm::linearRand(m_minStartCol, m_maxStartCol);
p->m_endCol[i] = glm::linearRand(m_minEndCol, m_maxEndCol);
}
}
void BasicVelGen::generate(double dt, ParticleData *p, size_t startId, size_t endId)
{
for (size_t i = startId; i < endId; ++i)
{
p->m_vel[i] = glm::linearRand(m_minStartVel, m_maxStartVel);
}
}
void SphereVelGen::generate(double dt, ParticleData *p, size_t startId, size_t endId)
{
float phi, theta, v, r;
for (size_t i = startId; i < endId; ++i)
{
phi = glm::linearRand(-M_PI, M_PI);
theta = glm::linearRand(-M_PI, M_PI);
v = glm::linearRand(m_minVel, m_maxVel);
r = v*sinf(phi);
p->m_vel[i].z = v*cosf(phi);
p->m_vel[i].x = r*cosf(theta);
p->m_vel[i].y = r*sinf(theta);
}
}
void BasicTimeGen::generate(double dt, ParticleData *p, size_t startId, size_t endId)
{
for (size_t i = startId; i < endId; ++i)
{
p->m_time[i].x = p->m_time[i].y = glm::linearRand(m_minTime, m_maxTime);
p->m_time[i].z = (float)0.0;
p->m_time[i].w = (float)1.0 / p->m_time[i].x;
}
}
}
}
#pragma once
#include <vector>
#include "commonMath.h"
#include "particles.h"
namespace particles
{
namespace generators
{
class BoxPosGen : public ParticleGenerator
{
public:
glm::vec4 m_pos{ 0.0 };
glm::vec4 m_maxStartPosOffset{ 0.0 };
public:
BoxPosGen() { }
virtual void generate(double dt, ParticleData *p, size_t startId, size_t endId) override;
};
class RoundPosGen : public ParticleGenerator
{
public:
glm::vec4 m_center{ 0.0 };
float m_radX{ 0.0 };
float m_radY{ 0.0 };
public:
RoundPosGen() { }
RoundPosGen(const glm::vec4 &center, double radX, double radY)
: m_center(center)
, m_radX((float)radX)
, m_radY((float)radY)
{ }
virtual void generate(double dt, ParticleData *p, size_t startId, size_t endId) override;
};
class BasicColorGen : public ParticleGenerator
{
public:
glm::vec4 m_minStartCol{ 0.0 };
glm::vec4 m_maxStartCol{ 0.0 };
glm::vec4 m_minEndCol{ 0.0 };
glm::vec4 m_maxEndCol{ 0.0 };
public:
BasicColorGen() { }
virtual void generate(double dt, ParticleData *p, size_t startId, size_t endId) override;
};
class BasicVelGen : public ParticleGenerator
{
public:
glm::vec4 m_minStartVel{ 0.0 };
glm::vec4 m_maxStartVel{ 0.0 };
public:
BasicVelGen() { }
virtual void generate(double dt, ParticleData *p, size_t startId, size_t endId) override;
};
class SphereVelGen : public ParticleGenerator
{
public:
float m_minVel{ 0.0f };
float m_maxVel{ 0.0f };
public:
SphereVelGen() { }
virtual void generate(double dt, ParticleData *p, size_t startId, size_t endId) override;
};
class BasicTimeGen : public ParticleGenerator
{
public:
float m_minTime{ 0.0 };
float m_maxTime{ 0.0 };
public:
BasicTimeGen() { }
virtual void generate(double dt, ParticleData *p, size_t startId, size_t endId) override;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment