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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "particle_prototype.h"
#include "particle_util.h"
#include "baseparticleentity.h"
#include "engine/IEngineTrace.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
// ------------------------------------------------------------------------- //
// Defines.
// ------------------------------------------------------------------------- //
#define MAX_FIRE_EMITTERS 128
#define FIRE_PARTICLE_LIFETIME 2
Vector g_FireSpreadDirection(0,0,1);
class FireRamp
{
public:
FireRamp(const Vector &s, const Vector &e)
{
m_Start=s;
m_End=e;
}
Vector m_Start;
Vector m_End;
};
FireRamp g_FireRamps[] =
{
FireRamp(Vector(1,0,0), Vector(1,1,0)),
FireRamp(Vector(0.5,0.5,0), Vector(0,0,0))
};
#define NUM_FIRE_RAMPS (sizeof(g_FireRamps) / sizeof(g_FireRamps[0]))
#define NUM_FIREGRID_OFFSETS 8
Vector g_Offsets[NUM_FIREGRID_OFFSETS] =
{
Vector(-1,-1,-1),
Vector( 1,-1,-1),
Vector(-1, 1,-1),
Vector( 1, 1,-1),
Vector(-1,-1, 1),
Vector( 1,-1, 1),
Vector(-1, 1, 1),
Vector( 1, 1, 1),
};
// If you follow g_Offset[index], you can follow g_Offsets[GetOppositeOffset(index)] to get back.
inline int GetOppositeOffset(int offset)
{
return NUM_FIREGRID_OFFSETS - offset - 1;
}
// ------------------------------------------------------------------------- //
// Classes.
// ------------------------------------------------------------------------- //
class C_ParticleFire : public C_BaseParticleEntity, public IPrototypeAppEffect
{
public:
DECLARE_CLASS( C_ParticleFire, C_BaseParticleEntity );
DECLARE_CLIENTCLASS();
C_ParticleFire();
~C_ParticleFire();
class FireEmitter
{
public:
Vector m_Pos;
TimedEvent m_SpawnEvent;
float m_Lifetime; // How long it's been emitting.
unsigned char m_DirectionsTested; // 1 bit for each of g_Offsets.
};
class FireParticle : public Particle
{
public:
Vector m_StartPos; // The particle moves from m_StartPos to (m_StartPos+m_Direction) over its lifetime.
Vector m_Direction;
float m_Lifetime;
float m_SpinAngle;
unsigned char m_iRamp; // Which fire ramp are we using?
};
// C_BaseEntity.
public:
virtual void OnDataChanged( DataUpdateType_t updateType );
// IPrototypeAppEffect.
public:
virtual void Start(CParticleMgr *pParticleMgr, IPrototypeArgAccess *pArgs);
// IParticleEffect.
public:
virtual void Update(float fTimeDelta);
virtual void RenderParticles( CParticleRenderIterator *pIterator );
virtual void SimulateParticles( CParticleSimulateIterator *pIterator );
public:
CParticleMgr *m_pParticleMgr;
PMaterialHandle m_MaterialHandle;
// Controls where the initial fire goes.
Vector m_vOrigin;
Vector m_vDirection;
TimedEvent m_EmitterSpawn;
FireEmitter m_Emitters[MAX_FIRE_EMITTERS];
int m_nEmitters;
private:
C_ParticleFire( const C_ParticleFire & );
};
// ------------------------------------------------------------------------- //
// Tables.
// ------------------------------------------------------------------------- //
// Expose to the particle app.
EXPOSE_PROTOTYPE_EFFECT(ParticleFire, C_ParticleFire);
// Datatable..
IMPLEMENT_CLIENTCLASS_DT_NOBASE(C_ParticleFire, DT_ParticleFire, CParticleFire)
RecvPropVector(RECVINFO(m_vOrigin)),
RecvPropVector(RECVINFO(m_vDirection)),
END_RECV_TABLE()
// ------------------------------------------------------------------------- //
// C_FireSmoke implementation.
// ------------------------------------------------------------------------- //
C_ParticleFire::C_ParticleFire()
{
m_pParticleMgr = NULL;
m_MaterialHandle = INVALID_MATERIAL_HANDLE;
}
C_ParticleFire::~C_ParticleFire()
{
if(m_pParticleMgr)
m_pParticleMgr->RemoveEffect( &m_ParticleEffect );
}
void C_ParticleFire::OnDataChanged(DataUpdateType_t updateType)
{
C_BaseEntity::OnDataChanged(updateType);
if(updateType == DATA_UPDATE_CREATED)
{
Start(ParticleMgr(), NULL);
}
}
void C_ParticleFire::Start(CParticleMgr *pParticleMgr, IPrototypeArgAccess *pArgs)
{
m_pParticleMgr = pParticleMgr;
m_pParticleMgr->AddEffect( &m_ParticleEffect, this );
m_MaterialHandle = m_ParticleEffect.FindOrAddMaterial("particle/particle_fire");
// Start
m_nEmitters = 0;
m_EmitterSpawn.Init(15);
}
static float fireSpreadDist = 15;
static float size = 20;
void C_ParticleFire::Update(float fTimeDelta)
{
if(!m_pParticleMgr)
{
assert(false);
return;
}
// Add new emitters.
if(m_nEmitters < MAX_FIRE_EMITTERS)
{
float tempDelta = fTimeDelta;
while(m_EmitterSpawn.NextEvent(tempDelta))
{
FireEmitter *pEmitter = NULL;
if(m_nEmitters == 0)
{
// Make the first emitter.
trace_t trace;
UTIL_TraceLine(m_vOrigin, m_vOrigin+m_vDirection*1000, MASK_SOLID_BRUSHONLY, NULL, COLLISION_GROUP_NONE, &trace);
if(trace.fraction < 1)
{
pEmitter = &m_Emitters[m_nEmitters];
pEmitter->m_Pos = trace.endpos + trace.plane.normal * (size - 1);
pEmitter->m_DirectionsTested = 0;
}
}
else
{
static int nTries = 50;
for(int iTry=0; iTry < nTries; iTry++)
{
FireEmitter *pSourceEmitter = &m_Emitters[rand() % m_nEmitters];
int iOffset = rand() % NUM_FIREGRID_OFFSETS;
if(pSourceEmitter->m_DirectionsTested & (1 << iOffset))
continue;
// Test the corners of the new cube. If some points are solid and some are not, then
// we can put fire here.
Vector basePos = pSourceEmitter->m_Pos + g_Offsets[iOffset] * fireSpreadDist;
int nSolidCorners = 0;
for(int iCorner=0; iCorner < NUM_FIREGRID_OFFSETS; iCorner++)
{
Vector vCorner = basePos + g_Offsets[iCorner]*fireSpreadDist;
if ( enginetrace->GetPointContents(vCorner) & CONTENTS_SOLID )
++nSolidCorners;
}
// Don't test this square again.
pSourceEmitter->m_DirectionsTested |= 1 << iOffset;
if(nSolidCorners != 0 && nSolidCorners != NUM_FIREGRID_OFFSETS)
{
pEmitter = &m_Emitters[m_nEmitters];
pEmitter->m_Pos = basePos;
pEmitter->m_DirectionsTested = 1 << GetOppositeOffset(iOffset);
}
}
}
if(pEmitter)
{
pEmitter->m_Lifetime = 0;
pEmitter->m_SpawnEvent.Init(1);
++m_nEmitters;
}
}
}
// Spawn particles out of the emitters.
for(int i=0; i < m_nEmitters; i++)
{
FireEmitter *pEmitter = &m_Emitters[i];
float tempDelta = fTimeDelta;
while(pEmitter->m_SpawnEvent.NextEvent(tempDelta))
{
FireParticle *pParticle = (FireParticle*)m_ParticleEffect.AddParticle(sizeof(FireParticle), m_MaterialHandle);
if(pParticle)
{
static float particleSpeed = 15;
pParticle->m_StartPos = pEmitter->m_Pos;
pParticle->m_Direction = g_FireSpreadDirection * particleSpeed + RandomVector(0, particleSpeed*0.5);
pParticle->m_iRamp = rand() % NUM_FIRE_RAMPS;
pParticle->m_Lifetime = 0;
}
}
}
}
void C_ParticleFire::RenderParticles( CParticleRenderIterator *pIterator )
{
const FireParticle *pParticle = (const FireParticle*)pIterator->GetFirst();
while ( pParticle )
{
float smooth01 = 1 - (cos(pParticle->m_Lifetime * 3.14159 / FIRE_PARTICLE_LIFETIME) * 0.5 + 0.5);
float smooth00 = 1 - (cos(pParticle->m_Lifetime * 3.14159 * 2 / FIRE_PARTICLE_LIFETIME) * 0.5 + 0.5);
FireRamp *pRamp = &g_FireRamps[pParticle->m_iRamp];
Vector curColor = pRamp->m_Start + (pRamp->m_End - pRamp->m_Start) * smooth01;
// Render.
Vector tPos;
TransformParticle(m_pParticleMgr->GetModelView(), pParticle->m_Pos, tPos);
float sortKey = (int)tPos.z;
RenderParticle_ColorSize(
pIterator->GetParticleDraw(),
tPos,
curColor,
smooth00,
size);
pParticle = (const FireParticle*)pIterator->GetNext( sortKey );
}
}
void C_ParticleFire::SimulateParticles( CParticleSimulateIterator *pIterator )
{
FireParticle *pParticle = (FireParticle*)pIterator->GetFirst();
while ( pParticle )
{
// Should this particle die?
pParticle->m_Lifetime += pIterator->GetTimeDelta();
if(pParticle->m_Lifetime > FIRE_PARTICLE_LIFETIME)
{
pIterator->RemoveParticle( pParticle );
}
else
{
float smooth01 = 1 - (cos(pParticle->m_Lifetime * 3.14159 / FIRE_PARTICLE_LIFETIME) * 0.5 + 0.5);
pParticle->m_Pos = pParticle->m_StartPos + pParticle->m_Direction * smooth01;
}
pParticle = (FireParticle*)pIterator->GetNext();
}
}
|