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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Client's energy wave
//
// $Workfile: $
// $Date: $
//
//-----------------------------------------------------------------------------
// $Log: $
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "materialsystem/imaterialsystem.h"
#include "materialsystem/imesh.h"
#include "energy_wave_effect.h"
#include "mathlib/vmatrix.h"
#include "clienteffectprecachesystem.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
CLIENTEFFECT_REGISTER_BEGIN( PrecacheEnergyWave )
CLIENTEFFECT_MATERIAL( "effects/energywave/energywave" )
CLIENTEFFECT_REGISTER_END()
//-----------------------------------------------------------------------------
// Energy Wave:
//-----------------------------------------------------------------------------
class C_EnergyWave : public C_BaseEntity
{
public:
DECLARE_CLASS( C_EnergyWave, C_BaseEntity );
DECLARE_CLIENTCLASS();
C_EnergyWave();
~C_EnergyWave();
void PostDataUpdate( DataUpdateType_t updateType );
int DrawModel( int flags );
void ComputePoint( float s, float t, Vector& pt, Vector& normal, float& opacity );
void DrawWireframeModel( );
CEnergyWaveEffect m_EWaveEffect;
IMaterial* m_pWireframe;
IMaterial* m_pEWaveMat;
private:
C_EnergyWave( const C_EnergyWave & ); // not defined, not accessible
void ComputeEWavePoints( Vector* pt, Vector* normal, float* opacity );
void DrawEWavePoints(Vector* pt, Vector* normal, float* opacity);
};
EXTERN_RECV_TABLE(DT_BaseEntity);
IMPLEMENT_CLIENTCLASS_DT(C_EnergyWave, DT_EWaveEffect, CEnergyWave)
END_RECV_TABLE()
// ----------------------------------------------------------------------------
// Functions.
// ----------------------------------------------------------------------------
C_EnergyWave::C_EnergyWave() : m_EWaveEffect(NULL, NULL)
{
m_pWireframe = materials->FindMaterial("shadertest/wireframevertexcolor", TEXTURE_GROUP_OTHER);
m_pEWaveMat = materials->FindMaterial("effects/energywave/energywave", TEXTURE_GROUP_CLIENT_EFFECTS);
m_EWaveEffect.Spawn();
}
C_EnergyWave::~C_EnergyWave()
{
}
void C_EnergyWave::PostDataUpdate( DataUpdateType_t updateType )
{
MarkMessageReceived();
// Make sure that origin points to current origin, at least
MoveToLastReceivedPosition();
}
enum
{
NUM_SUBDIVISIONS = 21,
};
static void ComputeIndices( int is, int it, int* idx )
{
int is0 = (is > 0) ? (is - 1) : is;
int it0 = (it > 0) ? (it - 1) : it;
int is1 = (is < EWAVE_NUM_HORIZONTAL_POINTS - 1) ? is + 1 : is;
int it1 = (it < EWAVE_NUM_HORIZONTAL_POINTS - 1) ? it + 1 : it;
int is2 = is + 2;
int it2 = it + 2;
if (is2 >= EWAVE_NUM_HORIZONTAL_POINTS)
is2 = EWAVE_NUM_HORIZONTAL_POINTS - 1;
if (it2 >= EWAVE_NUM_HORIZONTAL_POINTS)
it2 = EWAVE_NUM_HORIZONTAL_POINTS - 1;
idx[0] = is0 + it0 * EWAVE_NUM_HORIZONTAL_POINTS;
idx[1] = is + it0 * EWAVE_NUM_HORIZONTAL_POINTS;
idx[2] = is1 + it0 * EWAVE_NUM_HORIZONTAL_POINTS;
idx[3] = is2 + it0 * EWAVE_NUM_HORIZONTAL_POINTS;
idx[4] = is0 + it * EWAVE_NUM_HORIZONTAL_POINTS;
idx[5] = is + it * EWAVE_NUM_HORIZONTAL_POINTS;
idx[6] = is1 + it * EWAVE_NUM_HORIZONTAL_POINTS;
idx[7] = is2 + it * EWAVE_NUM_HORIZONTAL_POINTS;
idx[8] = is0 + it1 * EWAVE_NUM_HORIZONTAL_POINTS;
idx[9] = is + it1 * EWAVE_NUM_HORIZONTAL_POINTS;
idx[10] = is1 + it1 * EWAVE_NUM_HORIZONTAL_POINTS;
idx[11] = is2 + it1 * EWAVE_NUM_HORIZONTAL_POINTS;
idx[12] = is0 + it2 * EWAVE_NUM_HORIZONTAL_POINTS;
idx[13] = is + it2 * EWAVE_NUM_HORIZONTAL_POINTS;
idx[14] = is1 + it2 * EWAVE_NUM_HORIZONTAL_POINTS;
idx[15] = is2 + it2 * EWAVE_NUM_HORIZONTAL_POINTS;
}
void C_EnergyWave::ComputePoint( float s, float t, Vector& pt, Vector& normal, float& opacity )
{
int is = (int)s;
int it = (int)t;
if( is >= EWAVE_NUM_HORIZONTAL_POINTS )
is -= 1;
if( it >= EWAVE_NUM_VERTICAL_POINTS )
it -= 1;
int idx[16];
ComputeIndices( is, it, idx );
// The patch equation is:
// px = S * M * Gx * M^T * T^T
// py = S * M * Gy * M^T * T^T
// pz = S * M * Gz * M^T * T^T
// where S = [s^3 s^2 s 1], T = [t^3 t^2 t 1]
// M is the patch type matrix, in my case I'm using a catmull-rom
// G is the array of control points. rows have constant t
static VMatrix catmullRom( -0.5, 1.5, -1.5, 0.5,
1, -2.5, 2, -0.5,
-0.5, 0, 0.5, 0,
0, 1, 0, 0 );
VMatrix controlPointsX, controlPointsY, controlPointsZ, controlPointsO;
Vector pos;
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
const Vector& v = m_EWaveEffect.GetPoint( idx[i * 4 + j] );
controlPointsX[j][i] = v.x;
controlPointsY[j][i] = v.y;
controlPointsZ[j][i] = v.z;
controlPointsO[j][i] = m_EWaveEffect.ComputeOpacity( v, GetAbsOrigin() );
}
}
float fs = s - is;
float ft = t - it;
VMatrix temp, mgm[4];
MatrixTranspose( catmullRom, temp );
MatrixMultiply( controlPointsX, temp, mgm[0] );
MatrixMultiply( controlPointsY, temp, mgm[1] );
MatrixMultiply( controlPointsZ, temp, mgm[2] );
MatrixMultiply( controlPointsO, temp, mgm[3] );
MatrixMultiply( catmullRom, mgm[0], mgm[0] );
MatrixMultiply( catmullRom, mgm[1], mgm[1] );
MatrixMultiply( catmullRom, mgm[2], mgm[2] );
MatrixMultiply( catmullRom, mgm[3], mgm[3] );
Vector4D svec, tvec;
float ft2 = ft * ft;
tvec[0] = ft2 * ft; tvec[1] = ft2; tvec[2] = ft; tvec[3] = 1.0f;
float fs2 = fs * fs;
svec[0] = fs2 * fs; svec[1] = fs2; svec[2] = fs; svec[3] = 1.0f;
Vector4D tmp;
Vector4DMultiply( mgm[0], tvec, tmp );
pt[0] = DotProduct4D( tmp, svec );
Vector4DMultiply( mgm[1], tvec, tmp );
pt[1] = DotProduct4D( tmp, svec );
Vector4DMultiply( mgm[2], tvec, tmp );
pt[2] = DotProduct4D( tmp, svec );
Vector4DMultiply( mgm[3], tvec, tmp );
opacity = DotProduct4D( tmp, svec );
if ((s == 0.0f) || (t == 0.0f) ||
(s == (EWAVE_NUM_HORIZONTAL_POINTS-1.0f)) || (t == (EWAVE_NUM_VERTICAL_POINTS-1.0f)) )
{
opacity = 0.0f;
}
if ((s <= 0.3) || (t < 0.3))
{
opacity *= 0.35f;
}
if ((s == (EWAVE_NUM_HORIZONTAL_POINTS-0.7f)) || (t == (EWAVE_NUM_VERTICAL_POINTS-0.7f)) )
{
opacity *= 0.35f;
}
if (opacity < 0.0f)
opacity = 0.0f;
else if (opacity > 255.0f)
opacity = 255.0f;
// Normal computation
Vector4D dsvec, dtvec;
dsvec[0] = 3.0f * fs2; dsvec[1] = 2.0f * fs; dsvec[2] = 1.0f; dsvec[3] = 0.0f;
dtvec[0] = 3.0f * ft2; dtvec[1] = 2.0f * ft; dtvec[2] = 1.0f; dtvec[3] = 0.0f;
Vector ds, dt;
Vector4DMultiply( mgm[0], tvec, tmp );
ds[0] = DotProduct4D( tmp, dsvec );
Vector4DMultiply( mgm[1], tvec, tmp );
ds[1] = DotProduct4D( tmp, dsvec );
Vector4DMultiply( mgm[2], tvec, tmp );
ds[2] = DotProduct4D( tmp, dsvec );
Vector4DMultiply( mgm[0], dtvec, tmp );
dt[0] = DotProduct4D( tmp, svec );
Vector4DMultiply( mgm[1], dtvec, tmp );
dt[1] = DotProduct4D( tmp, svec );
Vector4DMultiply( mgm[2], dtvec, tmp );
dt[2] = DotProduct4D( tmp, svec );
CrossProduct( ds, dt, normal );
VectorNormalize( normal );
}
void C_EnergyWave::DrawWireframeModel( )
{
IMesh* pMesh = materials->GetDynamicMesh( true, NULL, NULL, m_pWireframe );
int numLines = (EWAVE_NUM_VERTICAL_POINTS - 1) * EWAVE_NUM_HORIZONTAL_POINTS +
EWAVE_NUM_VERTICAL_POINTS * (EWAVE_NUM_HORIZONTAL_POINTS - 1);
CMeshBuilder meshBuilder;
meshBuilder.Begin( pMesh, MATERIAL_LINES, numLines );
Vector tmp;
for (int i = 0; i < EWAVE_NUM_VERTICAL_POINTS; ++i)
{
for (int j = 0; j < EWAVE_NUM_HORIZONTAL_POINTS; ++j)
{
if ( i > 0 )
{
meshBuilder.Position3fv( m_EWaveEffect.GetPoint( j, i ).Base() );
meshBuilder.Color4ub( 255, 255, 255, 128 );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( m_EWaveEffect.GetPoint( j, i - 1 ).Base() );
meshBuilder.Color4ub( 255, 255, 255, 128 );
meshBuilder.AdvanceVertex();
}
if (j > 0)
{
meshBuilder.Position3fv( m_EWaveEffect.GetPoint( j, i ).Base() );
meshBuilder.Color4ub( 255, 255, 255, 128 );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( m_EWaveEffect.GetPoint( j - 1, i ).Base() );
meshBuilder.Color4ub( 255, 255, 255, 128 );
meshBuilder.AdvanceVertex();
}
}
}
meshBuilder.End();
pMesh->Draw();
}
//-----------------------------------------------------------------------------
// Compute the ewave points using catmull-rom
//-----------------------------------------------------------------------------
void C_EnergyWave::ComputeEWavePoints( Vector* pt, Vector* normal, float* opacity )
{
int i;
for ( i = 0; i < NUM_SUBDIVISIONS; ++i)
{
float t = (EWAVE_NUM_VERTICAL_POINTS -1 ) * (float)i / (float)(NUM_SUBDIVISIONS - 1);
for (int j = 0; j < NUM_SUBDIVISIONS; ++j)
{
float s = (EWAVE_NUM_HORIZONTAL_POINTS-1) * (float)j / (float)(NUM_SUBDIVISIONS - 1);
int idx = i * NUM_SUBDIVISIONS + j;
ComputePoint( s, t, pt[idx], normal[idx], opacity[idx] );
}
}
}
//-----------------------------------------------------------------------------
// Draws the base ewave
//-----------------------------------------------------------------------------
#define TRANSITION_REGION_WIDTH 0.5f
void C_EnergyWave::DrawEWavePoints(Vector* pt, Vector* normal, float* opacity)
{
IMesh* pMesh = materials->GetDynamicMesh( true, NULL, NULL, m_pEWaveMat );
int numTriangles = (NUM_SUBDIVISIONS - 1) * (NUM_SUBDIVISIONS - 1) * 2;
CMeshBuilder meshBuilder;
meshBuilder.Begin( pMesh, MATERIAL_TRIANGLES, numTriangles );
float du = 1.0f / (float)(NUM_SUBDIVISIONS - 1);
float dv = du;
unsigned char color[3];
color[0] = 255;
color[1] = 255;
color[2] = 255;
for ( int i = 0; i < NUM_SUBDIVISIONS - 1; ++i)
{
float v = i * dv;
for (int j = 0; j < NUM_SUBDIVISIONS - 1; ++j)
{
int idx = i * NUM_SUBDIVISIONS + j;
float u = j * du;
meshBuilder.Position3fv( pt[idx].Base() );
meshBuilder.Color4ub( color[0], color[1], color[2], opacity[idx] );
meshBuilder.Normal3fv( normal[idx].Base() );
meshBuilder.TexCoord2f( 0, u, v );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( pt[idx + NUM_SUBDIVISIONS].Base() );
meshBuilder.Color4ub( color[0], color[1], color[2], opacity[idx+NUM_SUBDIVISIONS] );
meshBuilder.Normal3fv( normal[idx + NUM_SUBDIVISIONS].Base() );
meshBuilder.TexCoord2f( 0, u, v + dv );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( pt[idx + 1].Base() );
meshBuilder.Color4ub( color[0], color[1], color[2], opacity[idx+1] );
meshBuilder.Normal3fv( normal[idx+1].Base() );
meshBuilder.TexCoord2f( 0, u + du, v );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( pt[idx + 1].Base() );
meshBuilder.Color4ub( color[0], color[1], color[2], opacity[idx+1] );
meshBuilder.Normal3fv( normal[idx+1].Base() );
meshBuilder.TexCoord2f( 0, u + du, v );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( pt[idx + NUM_SUBDIVISIONS].Base() );
meshBuilder.Color4ub( color[0], color[1], color[2], opacity[idx+NUM_SUBDIVISIONS] );
meshBuilder.Normal3fv( normal[idx + NUM_SUBDIVISIONS].Base() );
meshBuilder.TexCoord2f( 0, u, v + dv );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( pt[idx + NUM_SUBDIVISIONS + 1].Base() );
meshBuilder.Color4ub( color[0], color[1], color[2], opacity[idx+NUM_SUBDIVISIONS+1] );
meshBuilder.Normal3fv( normal[idx + NUM_SUBDIVISIONS + 1].Base() );
meshBuilder.TexCoord2f( 0, u + du, v + dv );
meshBuilder.AdvanceVertex();
}
}
meshBuilder.End();
pMesh->Draw();
}
//-----------------------------------------------------------------------------
// Main draw entry point
//-----------------------------------------------------------------------------
int C_EnergyWave::DrawModel( int flags )
{
if ( !m_bReadyToDraw )
return 0;
// NOTE: We've got a stiff spring case here, we need to simulate at
// a fairly fast timestep. A better solution would be to use an
// implicit method, which I'm going to not implement for the moment
float dt = gpGlobals->frametime;
m_EWaveEffect.SetPosition( GetAbsOrigin(), GetAbsAngles() );
m_EWaveEffect.Simulate(dt);
Vector pt[NUM_SUBDIVISIONS * NUM_SUBDIVISIONS];
Vector normal[NUM_SUBDIVISIONS * NUM_SUBDIVISIONS];
float opacity[NUM_SUBDIVISIONS * NUM_SUBDIVISIONS];
ComputeEWavePoints( pt, normal, opacity );
DrawEWavePoints( pt, normal, opacity );
return 1;
}
|