1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "SimpleParticleSystem.h"
#include "GenericParticleProjectile.h"
#include "Game/Camera.h"
#include "Game/GlobalUnsynced.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/ProjectileDrawer.h"
#include "Rendering/GL/VertexArray.h"
#include "Rendering/Textures/ColorMap.h"
#include "System/float3.h"
#include "System/Log/ILog.h"
CR_BIND_DERIVED(CSimpleParticleSystem, CProjectile, )
CR_REG_METADATA(CSimpleParticleSystem,
(
CR_MEMBER_BEGINFLAG(CM_Config),
CR_MEMBER(emitVector),
CR_MEMBER(emitMul),
CR_MEMBER(gravity),
CR_MEMBER(colorMap),
CR_MEMBER(texture),
CR_MEMBER(airdrag),
CR_MEMBER(particleLife),
CR_MEMBER(particleLifeSpread),
CR_MEMBER(numParticles),
CR_MEMBER(particleSpeed),
CR_MEMBER(particleSpeedSpread),
CR_MEMBER(particleSize),
CR_MEMBER(particleSizeSpread),
CR_MEMBER(emitRot),
CR_MEMBER(emitRotSpread),
CR_MEMBER(directional),
CR_MEMBER(sizeGrowth),
CR_MEMBER(sizeMod),
CR_MEMBER_ENDFLAG(CM_Config),
CR_MEMBER(particles),
CR_RESERVED(16)
))
CR_BIND(CSimpleParticleSystem::Particle, )
CR_REG_METADATA_SUB(CSimpleParticleSystem, Particle,
(
CR_MEMBER(pos),
CR_MEMBER(life),
CR_MEMBER(speed),
CR_MEMBER(decayrate),
CR_MEMBER(size),
CR_MEMBER(sizeGrowth),
CR_MEMBER(sizeMod),
CR_RESERVED(8)
))
CSimpleParticleSystem::CSimpleParticleSystem()
: CProjectile()
, emitVector(ZeroVector)
, emitMul(1.0f, 1.0f, 1.0f)
, gravity(ZeroVector)
, particleSpeed(0.0f)
, particleSpeedSpread(0.0f)
, emitRot(0.0f)
, emitRotSpread(0.0f)
, texture(NULL)
, colorMap(NULL)
, directional(false)
, particleLife(0.0f)
, particleLifeSpread(0.0f)
, particleSize(0.0f)
, particleSizeSpread(0.0f)
, airdrag(0.0f)
, sizeGrowth(0.0f)
, sizeMod(0.0f)
, numParticles(0)
{
checkCol = false;
useAirLos = true;
}
void CSimpleParticleSystem::Draw()
{
inArray = true;
va->EnlargeArrays(numParticles * 4, 0, VA_SIZE_TC);
if (directional) {
for (int i = 0; i < numParticles; i++) {
const Particle* p = &particles[i];
if (p->life < 1.0f) {
const float3 zdir = (p->pos - camera->GetPos()).SafeANormalize();
const float3 ydir = (zdir.cross(p->speed)).SafeANormalize();
const float3 xdir = (zdir.cross(ydir));
const float3 interPos = p->pos + p->speed * globalRendering->timeOffset;
const float size = p->size;
unsigned char color[4];
colorMap->GetColor(color, p->life);
if (p->speed.SqLength() > 0.001f) {
va->AddVertexQTC(interPos - ydir * size - xdir * size, texture->xstart, texture->ystart, color);
va->AddVertexQTC(interPos - ydir * size + xdir * size, texture->xend, texture->ystart, color);
va->AddVertexQTC(interPos + ydir * size + xdir * size, texture->xend, texture->yend, color);
va->AddVertexQTC(interPos + ydir * size - xdir * size, texture->xstart, texture->yend, color);
} else {
// in this case the particle's coor-system is degenerate
va->AddVertexQTC(interPos - camera->up * size - camera->right * size, texture->xstart, texture->ystart, color);
va->AddVertexQTC(interPos - camera->up * size + camera->right * size, texture->xend, texture->ystart, color);
va->AddVertexQTC(interPos + camera->up * size + camera->right * size, texture->xend, texture->yend, color);
va->AddVertexQTC(interPos + camera->up * size - camera->right * size, texture->xstart, texture->yend, color);
}
}
}
} else {
for (int i = 0; i < numParticles; i++) {
const Particle* p = &particles[i];
if (p->life < 1.0f) {
unsigned char color[4];
colorMap->GetColor(color, p->life);
const float3 interPos = p->pos + p->speed * globalRendering->timeOffset;
const float3 cameraRight = camera->right * p->size;
const float3 cameraUp = camera->up * p->size;
va->AddVertexQTC(interPos - cameraRight - cameraUp, texture->xstart, texture->ystart, color);
va->AddVertexQTC(interPos + cameraRight - cameraUp, texture->xend, texture->ystart, color);
va->AddVertexQTC(interPos + cameraRight + cameraUp, texture->xend, texture->yend, color);
va->AddVertexQTC(interPos - cameraRight + cameraUp, texture->xstart, texture->yend, color);
}
}
}
}
void CSimpleParticleSystem::Update()
{
deleteMe = true;
for (int i = 0; i < numParticles; i++) {
if (particles[i].life < 1.0f) {
particles[i].pos += particles[i].speed;
particles[i].speed += gravity;
particles[i].speed *= airdrag;
particles[i].life += particles[i].decayrate;
particles[i].size = particles[i].size * sizeMod + sizeGrowth;
deleteMe = false;
}
}
}
void CSimpleParticleSystem::Init(const CUnit* owner, const float3& offset)
{
CProjectile::Init(owner, offset);
particles.resize(numParticles);
const float3 up = emitVector;
const float3 right = up.cross(float3(up.y, up.z, -up.x));
const float3 forward = up.cross(right);
// FIXME: should catch these earlier and for more projectile-types
if (colorMap == NULL) {
colorMap = CColorMap::LoadFromFloatVector(std::vector<float>(8, 1.0f));
LOG_L(L_WARNING, "[CSimpleParticleSystem::%s] no color-map specified", __FUNCTION__);
}
if (texture == NULL) {
texture = &projectileDrawer->textureAtlas->GetTexture("simpleparticle");
LOG_L(L_WARNING, "[CSimpleParticleSystem::%s] no texture specified", __FUNCTION__);
}
for (int i = 0; i < numParticles; i++) {
float az = gu->RandFloat() * 2 * PI;
float ay = (emitRot + (emitRotSpread * gu->RandFloat())) * (PI / 180.0);
particles[i].pos = offset;
particles[i].speed = ((up * emitMul.y) * math::cos(ay) - ((right * emitMul.x) * math::cos(az) - (forward * emitMul.z) * math::sin(az)) * math::sin(ay)) * (particleSpeed + (gu->RandFloat() * particleSpeedSpread));
particles[i].life = 0;
particles[i].decayrate = 1.0f / (particleLife + (gu->RandFloat() * particleLifeSpread));
particles[i].size = particleSize + gu->RandFloat()*particleSizeSpread;
}
drawRadius = (particleSpeed + particleSpeedSpread) * (particleLife * particleLifeSpread);
}
CR_BIND_DERIVED(CSphereParticleSpawner, CSimpleParticleSystem, )
CR_REG_METADATA(CSphereParticleSpawner,
(
CR_MEMBER_BEGINFLAG(CM_Config),
CR_MEMBER_ENDFLAG(CM_Config)
))
CSphereParticleSpawner::CSphereParticleSpawner(): CSimpleParticleSystem()
{
}
void CSphereParticleSpawner::Init(const CUnit* owner, const float3& offset)
{
const float3 up = emitVector;
const float3 right = up.cross(float3(up.y, up.z, -up.x));
const float3 forward = up.cross(right);
// FIXME: should catch these earlier and for more projectile-types
if (colorMap == NULL) {
colorMap = CColorMap::LoadFromFloatVector(std::vector<float>(8, 1.0f));
LOG_L(L_WARNING, "[CSphereParticleSpawner::%s] no color-map specified", __FUNCTION__);
}
if (texture == NULL) {
texture = &projectileDrawer->textureAtlas->GetTexture("sphereparticle");
LOG_L(L_WARNING, "[CSphereParticleSpawner::%s] no texture specified", __FUNCTION__);
}
for (int i = 0; i < numParticles; i++) {
const float az = gu->RandFloat() * 2 * PI;
const float ay = (emitRot + emitRotSpread*gu->RandFloat()) * (PI / 180.0);
const float3 pspeed = ((up * emitMul.y) * math::cos(ay) - ((right * emitMul.x) * math::cos(az) - (forward * emitMul.z) * math::sin(az)) * math::sin(ay)) * (particleSpeed + (gu->RandFloat() * particleSpeedSpread));
CGenericParticleProjectile* particle = new CGenericParticleProjectile(owner, pos + offset, pspeed);
particle->decayrate = 1.0f / (particleLife + gu->RandFloat() * particleLifeSpread);
particle->life = 0;
particle->size = particleSize + gu->RandFloat() * particleSizeSpread;
particle->texture = texture;
particle->colorMap = colorMap;
particle->airdrag = airdrag;
particle->sizeGrowth = sizeGrowth;
particle->sizeMod = sizeMod;
particle->gravity = gravity;
particle->directional = directional;
particle->SetRadiusAndHeight(particle->size + sizeGrowth * particleLife, 0.0f);
}
deleteMe = true;
}
|