| 943 | } |
| 944 | |
| 945 | static void SpawnParticle(dmArray<Particle>& particles, uint32_t* seed, dmParticleDDF::Emitter* ddf, const dmTransform::TransformS1& emitter_transform, Vector3 emitter_velocity, float emitter_properties[EMITTER_KEY_COUNT], float dt) |
| 946 | { |
| 947 | DM_PROFILE(__FUNCTION__); |
| 948 | |
| 949 | uint32_t particle_count = particles.Size(); |
| 950 | particles.SetSize(particle_count + 1); |
| 951 | Particle *particle = &particles[particle_count]; |
| 952 | memset(particle, 0, sizeof(Particle)); |
| 953 | |
| 954 | // TODO Handle birth-action |
| 955 | |
| 956 | particle->SetMaxLifeTime(emitter_properties[EMITTER_KEY_PARTICLE_LIFE_TIME]); |
| 957 | particle->SetooMaxLifeTime(1.0f / particle->GetMaxLifeTime()); |
| 958 | // Include dt since already existing particles have already been advanced |
| 959 | particle->SetTimeLeft(particle->GetMaxLifeTime() - dt); |
| 960 | particle->SetSpreadFactor(dmMath::Rand11(seed)); |
| 961 | particle->SetSourceSize(emitter_properties[EMITTER_KEY_PARTICLE_SIZE] * emitter_transform.GetScale()); |
| 962 | particle->SetSourceColor(Vector4( |
| 963 | emitter_properties[EMITTER_KEY_PARTICLE_RED], |
| 964 | emitter_properties[EMITTER_KEY_PARTICLE_GREEN], |
| 965 | emitter_properties[EMITTER_KEY_PARTICLE_BLUE], |
| 966 | emitter_properties[EMITTER_KEY_PARTICLE_ALPHA])); |
| 967 | |
| 968 | dmTransform::TransformS1 transform; |
| 969 | transform.SetIdentity(); |
| 970 | Vector3 dir(0.0f, 0.0f, 0.0f); |
| 971 | |
| 972 | switch (ddf->m_Type) |
| 973 | { |
| 974 | case EMITTER_TYPE_SPHERE: |
| 975 | { |
| 976 | // Direction is sampled uniformly over the unit-sphere surface |
| 977 | // http://www.altdevblogaday.com/2012/05/03/generating-uniformly-distributed-points-on-sphere/ |
| 978 | float z = dmMath::Rand11(seed); |
| 979 | float angle = 2.0f * ((float) M_PI) * dmMath::RandOpen01(seed); |
| 980 | float r = sqrtf(1.0f - z * z); |
| 981 | dir = Vector3(r * cosf(angle), r * sinf(angle), z); |
| 982 | // Pick radius to give uniform dist. over volume, surface area of sub-spheres grows quadratic wrt radius |
| 983 | float radius = sqrtf(dmMath::RandOpen01(seed)); |
| 984 | radius *= 0.5f * emitter_properties[EMITTER_KEY_SIZE_X]; |
| 985 | transform.SetTranslation(dir * radius); |
| 986 | |
| 987 | break; |
| 988 | } |
| 989 | |
| 990 | case EMITTER_TYPE_CIRCLE: |
| 991 | { |
| 992 | // Direction is sampled uniformly over the unit-sphere surface |
| 993 | // http://www.altdevblogaday.com/2012/05/03/generating-uniformly-distributed-points-on-sphere/ |
| 994 | float angle = 2.0f * ((float) M_PI) * dmMath::RandOpen01(seed); |
| 995 | dir = Vector3(cosf(angle), sinf(angle), 0); |
| 996 | // Pick radius to give uniform dist. over volume, surface area of sub-spheres grows quadratic wrt radius |
| 997 | float radius = sqrtf(dmMath::RandOpen01(seed)); |
| 998 | radius *= 0.5f * emitter_properties[EMITTER_KEY_SIZE_X]; |
| 999 | transform.SetTranslation(dir * radius); |
| 1000 | |
| 1001 | break; |
| 1002 | } |
no test coverage detected