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
|
#include "StdAfx.h"
#include "mmgr.h"
#include "Game/Camera.h"
#include "Sim/Misc/GlobalConstants.h"
#include "GenericParticleProjectile.h"
#include "GlobalUnsynced.h"
#include "Rendering/GL/VertexArray.h"
#include "Rendering/Textures/ColorMap.h"
#include "SimpleParticleSystem.h"
#include "Sim/Projectiles/ProjectileHandler.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(void)
: CProjectile()
{
checkCol=false;
useAirLos=true;
particles=0;
emitMul = float3(1,1,1);
}
CSimpleParticleSystem::~CSimpleParticleSystem(void)
{
if(particles)
delete [] particles;
}
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) {
float3 dif(p->pos - camera->pos);
dif.SafeANormalize();
float3 dir1 =
(p->speed.SqLength() > 0.001f)?
(dif.cross(p->speed)):
ZeroVector;
dir1.SafeANormalize();
const float3 dir2(dif.cross(dir1));
const float3 interPos = p->pos + p->speed * gu->timeOffset;
const float size = p->size;
unsigned char color[4];
colorMap->GetColor(color, p->life);
va->AddVertexQTC(interPos - dir1 * size - dir2 * size, texture->xstart, texture->ystart, color);
va->AddVertexQTC(interPos - dir1 * size + dir2 * size, texture->xend, texture->ystart, color);
va->AddVertexQTC(interPos + dir1 * size + dir2 * size, texture->xend, texture->yend, color);
va->AddVertexQTC(interPos + dir1 * size - dir2 * 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 * gu->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].life += particles[i].decayrate;
particles[i].speed += gravity;
particles[i].speed *= airdrag;
particles[i].size = particles[i].size * sizeMod + sizeGrowth;
deleteMe = false;
}
}
}
void CSimpleParticleSystem::Init(const float3& explosionPos, CUnit *owner GML_PARG_C)
{
CProjectile::Init(explosionPos, owner GML_PARG_P);
particles = new Particle[numParticles];
float3 up = emitVector;
float3 right = up.cross(float3(up.y,up.z,-up.x));
float3 forward = up.cross(right);
for(int i=0; i<numParticles; i++)
{
float az = gu->usRandFloat()*2*PI;
float ay = (emitRot + emitRotSpread*gu->usRandFloat())*(PI/180.0);
particles[i].decayrate = 1.0f/(particleLife + gu->usRandFloat()*particleLifeSpread);
particles[i].life = 0;
particles[i].size = particleSize + gu->usRandFloat()*particleSizeSpread;
particles[i].pos = pos;
particles[i].speed = ((up*emitMul.y)*cos(ay)-((right*emitMul.x)*cos(az)-(forward*emitMul.z)*sin(az))*sin(ay)) * (particleSpeed + gu->usRandFloat()*particleSpeedSpread);
}
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()
{
}
CSphereParticleSpawner::~CSphereParticleSpawner()
{
}
void CSphereParticleSpawner::Init(const float3& explosionPos, CUnit* owner GML_PARG_C)
{
float3 up = emitVector;
float3 right = up.cross(float3(up.y, up.z, -up.x));
float3 forward = up.cross(right);
for (int i = 0; i < numParticles; i++) {
float az = gu->usRandFloat() * 2 * PI;
float ay = (emitRot + emitRotSpread*gu->usRandFloat()) * (PI / 180.0);
float3 pspeed = ((up*emitMul.y)*cos(ay)-((right*emitMul.x)*cos(az)-(forward*emitMul.z)*sin(az))*sin(ay)) * (particleSpeed + gu->usRandFloat()*particleSpeedSpread);
CGenericParticleProjectile* particle = new CGenericParticleProjectile(pos + explosionPos, pspeed, owner);
particle->decayrate = 1.0f / (particleLife + gu->usRandFloat() * particleLifeSpread);
particle->life = 0;
particle->size = particleSize + gu->usRandFloat() * particleSizeSpread;
particle->texture = texture;
particle->colorMap = colorMap;
particle->airdrag = airdrag;
particle->sizeGrowth = sizeGrowth;
particle->sizeMod = sizeMod;
particle->gravity = gravity;
particle->directional = directional;
particle->SetRadius(particle->size + sizeGrowth * particleLife);
}
deleteMe = true;
}
|