forked from ValveSoftware/source-sdk-2013
-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathc_beamspotlight.cpp
More file actions
354 lines (295 loc) · 9.36 KB
/
Copy pathc_beamspotlight.cpp
File metadata and controls
354 lines (295 loc) · 9.36 KB
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
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose:
//
//===========================================================================//
#include "cbase.h"
#include "dlight.h"
#include "iefx.h"
#include "beam_shared.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CSpotlightTraceCacheEntry
{
public:
CSpotlightTraceCacheEntry()
{
m_origin.Init();
m_radius = -1.0f;
}
bool IsValidFor( const Vector &origin )
{
if ( m_radius > 0 && m_origin.DistToSqr(origin) < 1.0f )
return true;
return false;
}
void Cache( const Vector &origin, const trace_t &tr )
{
m_radius = (tr.endpos - origin).Length();
m_origin = origin;
}
Vector m_origin;
float m_radius;
};
static const int NUM_CACHE_ENTRIES = 64;
class C_BeamSpotLight : public C_BaseEntity
{
public:
DECLARE_CLASS( C_BeamSpotLight, C_BaseEntity );
DECLARE_CLIENTCLASS();
C_BeamSpotLight();
~C_BeamSpotLight();
bool ShouldDraw();
void ClientThink( void );
void OnDataChanged( DataUpdateType_t updateType );
void Release( void );
private:
Vector SpotlightCurrentPos(void);
void SpotlightCreate(void);
void SpotlightDestroy(void);
// Computes render info for a spotlight
void ComputeRenderInfo();
private:
int m_nHaloIndex;
int m_nRotationAxis;
float m_flRotationSpeed;
bool m_bSpotlightOn;
bool m_bHasDynamicLight;
float m_flSpotlightMaxLength;
float m_flSpotlightGoalWidth;
float m_flHDRColorScale;
Vector m_vSpotlightTargetPos;
Vector m_vSpotlightCurrentPos;
Vector m_vSpotlightDir;
CHandle<C_Beam> m_hSpotlight;
float m_flSpotlightCurLength;
float m_flLightScale;
dlight_t* m_pDynamicLight;
float m_lastTime;
CSpotlightTraceCacheEntry *m_pCache;
};
LINK_ENTITY_TO_CLASS( beam_spotlight, C_BeamSpotLight );
IMPLEMENT_CLIENTCLASS_DT( C_BeamSpotLight, DT_BeamSpotlight, CBeamSpotlight )
RecvPropInt( RECVINFO(m_nHaloIndex) ),
RecvPropBool( RECVINFO(m_bSpotlightOn) ),
RecvPropBool( RECVINFO(m_bHasDynamicLight) ),
RecvPropFloat( RECVINFO(m_flSpotlightMaxLength) ),
RecvPropFloat( RECVINFO(m_flSpotlightGoalWidth) ),
RecvPropFloat( RECVINFO(m_flHDRColorScale) ),
RecvPropInt( RECVINFO(m_nRotationAxis) ),
RecvPropFloat( RECVINFO(m_flRotationSpeed) ),
END_RECV_TABLE()
//-----------------------------------------------------------------------------
C_BeamSpotLight::C_BeamSpotLight()
: m_vSpotlightTargetPos( vec3_origin )
, m_vSpotlightCurrentPos( vec3_origin )
, m_vSpotlightDir( vec3_origin )
, m_flSpotlightCurLength( 0.0f )
, m_flLightScale( 100.0f )
, m_pDynamicLight( NULL )
, m_lastTime( 0.0f )
, m_pCache(NULL)
{
}
C_BeamSpotLight::~C_BeamSpotLight()
{
delete[] m_pCache;
}
//-----------------------------------------------------------------------------
bool C_BeamSpotLight::ShouldDraw()
{
return false;
}
//-----------------------------------------------------------------------------
void C_BeamSpotLight::ClientThink( void )
{
float dt = gpGlobals->curtime - m_lastTime;
if ( !m_lastTime )
{
dt = 0.0f;
}
m_lastTime = gpGlobals->curtime;
// ---------------------------------------------------
// If I don't have a spotlight attempt to create one
// ---------------------------------------------------
if ( !m_hSpotlight )
{
if ( m_bSpotlightOn )
{
// Make the spotlight
SpotlightCreate();
}
else
{
SetNextClientThink( CLIENT_THINK_NEVER );
return;
}
}
else if ( !m_bSpotlightOn )
{
SpotlightDestroy();
SetNextClientThink( CLIENT_THINK_NEVER );
return;
}
// update rotation
if ( m_flRotationSpeed != 0.0f )
{
QAngle angles = GetAbsAngles();
angles[m_nRotationAxis] += m_flRotationSpeed * dt;
angles[m_nRotationAxis] = anglemod(angles[m_nRotationAxis]);
if ( !m_pCache )
{
m_pCache = new CSpotlightTraceCacheEntry[NUM_CACHE_ENTRIES];
}
SetAbsAngles( angles );
}
m_vSpotlightCurrentPos = SpotlightCurrentPos();
Assert( m_hSpotlight );
m_hSpotlight->SetStartPos( GetAbsOrigin() );
m_hSpotlight->SetEndPos( m_vSpotlightCurrentPos );
// Avoid sudden change in where beam fades out when cross disconinuities
Vector dir = m_vSpotlightCurrentPos - GetAbsOrigin();
float flBeamLength = VectorNormalize( dir );
m_flSpotlightCurLength = (0.60*m_flSpotlightCurLength) + (0.4*flBeamLength);
ComputeRenderInfo();
m_hSpotlight->RelinkBeam();
//NDebugOverlay::Cross3D(GetAbsOrigin(),Vector(-5,-5,-5),Vector(5,5,5),0,255,0,true,0.1);
//NDebugOverlay::Cross3D(m_vSpotlightCurrentPos,Vector(-5,-5,-5),Vector(5,5,5),0,255,0,true,0.1);
//NDebugOverlay::Cross3D(m_vSpotlightTargetPos,Vector(-5,-5,-5),Vector(5,5,5),255,0,0,true,0.1);
// Do we need to keep updating?
if ( !GetMoveParent() && m_flRotationSpeed == 0 )
{
// No reason to think again, we're not going to move unless there's a data change
SetNextClientThink( CLIENT_THINK_NEVER );
}
}
//-----------------------------------------------------------------------------
void C_BeamSpotLight::OnDataChanged( DataUpdateType_t updateType )
{
BaseClass::OnDataChanged( updateType );
if ( updateType == DATA_UPDATE_CREATED )
{
m_flSpotlightCurLength = m_flSpotlightMaxLength;
}
// On a data change always think again
SetNextClientThink( CLIENT_THINK_ALWAYS );
}
//------------------------------------------------------------------------------
void C_BeamSpotLight::Release()
{
SpotlightDestroy();
BaseClass::Release();
}
//------------------------------------------------------------------------------
void C_BeamSpotLight::SpotlightCreate(void)
{
m_vSpotlightTargetPos = SpotlightCurrentPos();
{
//C_Beam *beam = CBeam::BeamCreate( "sprites/spotlight.vmt", m_flSpotlightGoalWidth );
C_Beam *beam = C_Beam::BeamCreate( "sprites/glow_test02.vmt", m_flSpotlightGoalWidth );
// Beam only exists client side
ClientEntityList().AddNonNetworkableEntity( beam );
m_hSpotlight = beam;
}
// Set the temporary spawnflag on the beam so it doesn't save (we'll recreate it on restore)
m_hSpotlight->SetHDRColorScale( m_flHDRColorScale );
const color32 c = GetRenderColor();
m_hSpotlight->SetColor( c.r, c.g, c.b );
m_hSpotlight->SetHaloTexture(m_nHaloIndex);
m_hSpotlight->SetHaloScale(60);
m_hSpotlight->SetEndWidth(m_flSpotlightGoalWidth);
m_hSpotlight->SetBeamFlags( (FBEAM_SHADEOUT|FBEAM_NOTILE) );
m_hSpotlight->SetBrightness( 64 );
m_hSpotlight->SetNoise( 0 );
m_hSpotlight->PointsInit( GetAbsOrigin(), m_vSpotlightTargetPos );
}
//------------------------------------------------------------------------------
void C_BeamSpotLight::SpotlightDestroy(void)
{
if ( m_hSpotlight )
{
m_hSpotlight->Release();
m_hSpotlight.Term();
}
}
//------------------------------------------------------------------------------
Vector C_BeamSpotLight::SpotlightCurrentPos(void)
{
QAngle angles = GetAbsAngles();
GetVectors( &m_vSpotlightDir, NULL, NULL );
Vector position = GetAbsOrigin();
int cacheIndex = -1;
if ( m_pCache )
{
cacheIndex = int( angles[m_nRotationAxis] * float(NUM_CACHE_ENTRIES) * (1.0f / 360.0f)) & (NUM_CACHE_ENTRIES - 1);
if ( m_pCache[cacheIndex].IsValidFor(GetAbsOrigin()) )
{
return position + m_vSpotlightDir * m_pCache[cacheIndex].m_radius;
}
}
// Get beam end point. Only collide with solid objects, not npcs
trace_t tr;
UTIL_TraceLine( position, position + (m_vSpotlightDir * 2 * m_flSpotlightMaxLength), MASK_SOLID_BRUSHONLY, this, COLLISION_GROUP_NONE, &tr );
if ( cacheIndex >= 0 )
{
m_pCache[cacheIndex].Cache(position, tr);
}
return tr.endpos;
}
//-----------------------------------------------------------------------------
// Computes render info for a spotlight
//-----------------------------------------------------------------------------
void C_BeamSpotLight::ComputeRenderInfo()
{
// Fade out spotlight end if past max length.
if ( m_flSpotlightCurLength > 2*m_flSpotlightMaxLength )
{
SetRenderColorA( 0 );
m_hSpotlight->SetFadeLength( m_flSpotlightMaxLength );
}
else if ( m_flSpotlightCurLength > m_flSpotlightMaxLength )
{
SetRenderColorA( (1-((m_flSpotlightCurLength-m_flSpotlightMaxLength)/m_flSpotlightMaxLength)) );
m_hSpotlight->SetFadeLength( m_flSpotlightMaxLength );
}
else
{
SetRenderColorA( 1.0 );
m_hSpotlight->SetFadeLength( m_flSpotlightCurLength );
}
// Adjust end width to keep beam width constant
float flNewWidth = m_flSpotlightGoalWidth * (m_flSpotlightCurLength / m_flSpotlightMaxLength);
flNewWidth = clamp(flNewWidth, 0, MAX_BEAM_WIDTH );
m_hSpotlight->SetEndWidth(flNewWidth);
if ( m_bHasDynamicLight )
{
// <<TODO>> - magic number 1.8 depends on sprite size
m_flLightScale = 1.8*flNewWidth;
if ( m_flLightScale > 0 )
{
const color32 c = GetRenderColor();
float a = GetRenderColor().a / 255.0f;
ColorRGBExp32 color;
color.r = c.r * a;
color.g = c.g * a;
color.b = c.b * a;
color.exponent = 0;
if ( color.r == 0 && color.g == 0 && color.b == 0 )
return;
// Deal with the environment light
if ( !m_pDynamicLight || (m_pDynamicLight->key != index) )
{
m_pDynamicLight = effects->CL_AllocDlight( index );
assert (m_pDynamicLight);
}
//m_pDynamicLight->flags = DLIGHT_NO_MODEL_ILLUMINATION;
m_pDynamicLight->radius = m_flLightScale*3.0f;
m_pDynamicLight->origin = GetAbsOrigin() + Vector(0,0,5);
m_pDynamicLight->die = gpGlobals->curtime + 0.05f;
m_pDynamicLight->color = color;
}
}
}