forked from XutaxKamay/css_enhanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmelight.cpp
331 lines (262 loc) · 10.3 KB
/
dmelight.cpp
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include "movieobjects/dmelight.h"
#include "tier0/dbg.h"
#include "datamodel/dmelementfactoryhelper.h"
#include "mathlib/vector.h"
#include "movieobjects/dmetransform.h"
#include "materialsystem/imaterialsystem.h"
#include "movieobjects_interfaces.h"
#include "tier2/tier2.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Expose this class to the scene database
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeLight, CDmeLight );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDmeLight::OnConstruction()
{
m_Color.InitAndSet( this, "color", Color( 255, 255, 255, 255 ), FATTRIB_HAS_CALLBACK );
m_flIntensity.InitAndSet( this, "intensity", 1.0f, FATTRIB_HAS_CALLBACK );
}
void CDmeLight::OnDestruction()
{
}
//-----------------------------------------------------------------------------
// Sets the color and intensity
// NOTE: Color is specified 0-255 floating point.
//-----------------------------------------------------------------------------
void CDmeLight::SetColor( const Color &color )
{
m_Color.Set( color );
}
void CDmeLight::SetIntensity( float flIntensity )
{
m_flIntensity = flIntensity;
}
//-----------------------------------------------------------------------------
// Sets up render state in the material system for rendering
//-----------------------------------------------------------------------------
void CDmeLight::SetupRenderStateInternal( LightDesc_t &desc, float flAtten0, float flAtten1, float flAtten2 )
{
desc.m_Color[0] = m_Color.Get().r();
desc.m_Color[1] = m_Color.Get().g();
desc.m_Color[2] = m_Color.Get().b();
desc.m_Color *= m_flIntensity / 255.0f;
desc.m_Attenuation0 = flAtten0;
desc.m_Attenuation1 = flAtten1;
desc.m_Attenuation2 = flAtten2;
desc.m_Flags = 0;
if ( desc.m_Attenuation0 != 0.0f )
{
desc.m_Flags |= LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION0;
}
if ( desc.m_Attenuation1 != 0.0f )
{
desc.m_Flags |= LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION1;
}
if ( desc.m_Attenuation2 != 0.0f )
{
desc.m_Flags |= LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION2;
}
}
//-----------------------------------------------------------------------------
// Sets lighting state
//-----------------------------------------------------------------------------
void CDmeLight::SetupRenderState( int nLightIndex )
{
LightDesc_t desc;
if ( GetLightDesc( &desc ) )
{
// FIXME: Should we pass the light color in?
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pRenderContext->SetLight( nLightIndex, desc );
}
}
//-----------------------------------------------------------------------------
//
// A directional light
//
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeDirectionalLight, CDmeDirectionalLight );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDmeDirectionalLight::OnConstruction()
{
m_Direction.InitAndSet( this, "direction", Vector( 0.0f, 0.0f, -1.0f ), FATTRIB_HAS_CALLBACK );
}
void CDmeDirectionalLight::OnDestruction()
{
}
//-----------------------------------------------------------------------------
// Sets the light direction
//-----------------------------------------------------------------------------
void CDmeDirectionalLight::SetDirection( const Vector &direction )
{
m_Direction.Set( direction );
}
//-----------------------------------------------------------------------------
// Gets a light desc for the light
//-----------------------------------------------------------------------------
bool CDmeDirectionalLight::GetLightDesc( LightDesc_t *pDesc )
{
memset( pDesc, 0, sizeof(LightDesc_t) );
pDesc->m_Type = MATERIAL_LIGHT_DIRECTIONAL;
SetupRenderStateInternal( *pDesc, 1.0f, 0.0f, 0.0f );
matrix3x4_t m;
GetTransform()->GetTransform( m );
VectorRotate( m_Direction.Get(), m, pDesc->m_Direction );
VectorNormalize( pDesc->m_Direction );
pDesc->m_Theta = 0.0f;
pDesc->m_Phi = 0.0f;
pDesc->m_Falloff = 1.0f;
return true;
}
//-----------------------------------------------------------------------------
//
// A point light
//
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmePointLight, CDmePointLight );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDmePointLight::OnConstruction()
{
m_Position.InitAndSet( this, "position", Vector( 0, 0, 0 ), FATTRIB_HAS_CALLBACK );
m_flAttenuation0.InitAndSet( this, "constantAttenuation", 1.0f, FATTRIB_HAS_CALLBACK );
m_flAttenuation1.InitAndSet( this, "linearAttenuation", 0.0f, FATTRIB_HAS_CALLBACK );
m_flAttenuation2.InitAndSet( this, "quadraticAttenuation", 0.0f, FATTRIB_HAS_CALLBACK );
m_flMaxDistance.InitAndSet( this, "maxDistance", 0.0f, FATTRIB_HAS_CALLBACK );
}
void CDmePointLight::OnDestruction()
{
}
//-----------------------------------------------------------------------------
// Sets the attenuation factors
//-----------------------------------------------------------------------------
void CDmePointLight::SetAttenuation( float flConstant, float flLinear, float flQuadratic )
{
m_flAttenuation0 = flConstant;
m_flAttenuation1 = flLinear;
m_flAttenuation2 = flQuadratic;
}
//-----------------------------------------------------------------------------
// Sets the maximum range
//-----------------------------------------------------------------------------
void CDmePointLight::SetMaxDistance( float flMaxDistance )
{
m_flMaxDistance = flMaxDistance;
}
//-----------------------------------------------------------------------------
// Sets up render state in the material system for rendering
//-----------------------------------------------------------------------------
bool CDmePointLight::GetLightDesc( LightDesc_t *pDesc )
{
memset( pDesc, 0, sizeof(LightDesc_t) );
pDesc->m_Type = MATERIAL_LIGHT_POINT;
SetupRenderStateInternal( *pDesc, m_flAttenuation0, m_flAttenuation1, m_flAttenuation2 );
matrix3x4_t m;
GetTransform()->GetTransform( m );
VectorTransform( m_Position, m, pDesc->m_Position );
pDesc->m_Direction.Init( 0, 0, 1 );
pDesc->m_Range = m_flMaxDistance;
pDesc->m_Theta = 0.0f;
pDesc->m_Phi = 0.0f;
pDesc->m_Falloff = 1.0f;
return true;
}
//-----------------------------------------------------------------------------
//
// A spot light
//
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeSpotLight, CDmeSpotLight );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDmeSpotLight::OnConstruction()
{
m_Direction.InitAndSet( this, "direction", Vector( 0.0f, 0.0f, -1.0f ) );
m_flSpotInnerAngle.InitAndSet( this, "spotInnerAngle", 60.0f );
m_flSpotOuterAngle.InitAndSet( this, "spotOuterAngle", 90.0f );
m_flSpotAngularFalloff.InitAndSet( this, "spotAngularFalloff", 1.0f );
}
void CDmeSpotLight::OnDestruction()
{
}
//-----------------------------------------------------------------------------
// Sets the light direction
//-----------------------------------------------------------------------------
void CDmeSpotLight::SetDirection( const Vector &direction )
{
m_Direction = direction;
}
//-----------------------------------------------------------------------------
// Sets the spotlight angle factors
// Angles are specified in degrees, as full angles (as opposed to half-angles)
//-----------------------------------------------------------------------------
void CDmeSpotLight::SetAngles( float flInnerAngle, float flOuterAngle, float flAngularFalloff )
{
m_flSpotInnerAngle = flInnerAngle;
m_flSpotOuterAngle = flOuterAngle;
m_flSpotAngularFalloff = flAngularFalloff;
}
//-----------------------------------------------------------------------------
// Sets up render state in the material system for rendering
//-----------------------------------------------------------------------------
bool CDmeSpotLight::GetLightDesc( LightDesc_t *pDesc )
{
memset( pDesc, 0, sizeof(LightDesc_t) );
pDesc->m_Type = MATERIAL_LIGHT_SPOT;
SetupRenderStateInternal( *pDesc, m_flAttenuation0, m_flAttenuation1, m_flAttenuation2 );
matrix3x4_t m;
GetTransform()->GetTransform( m );
VectorTransform( m_Position, m, pDesc->m_Position );
VectorRotate( m_Direction.Get(), m, pDesc->m_Direction );
VectorNormalize( pDesc->m_Direction );
pDesc->m_Range = m_flMaxDistance;
// Convert to radians
pDesc->m_Theta = m_flSpotInnerAngle * M_PI / 180.0f;
pDesc->m_Phi = m_flSpotOuterAngle * M_PI / 180.0f;
pDesc->m_Falloff = m_flSpotAngularFalloff;
return true;
}
//-----------------------------------------------------------------------------
//
// An ambient light
//
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeAmbientLight, CDmeAmbientLight );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDmeAmbientLight::OnConstruction()
{
}
void CDmeAmbientLight::OnDestruction()
{
}
//-----------------------------------------------------------------------------
// Sets up render state in the material system for rendering
//-----------------------------------------------------------------------------
void CDmeAmbientLight::SetupRenderState( int nLightIndex )
{
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
Vector4D cube[6];
Vector4D vec4color( m_Color.Get().r(), m_Color.Get().g(), m_Color.Get().b(), m_Color.Get().a() );
Vector4DMultiply( vec4color, m_flIntensity / 255.0f, cube[0] );
cube[1] = cube[0];
cube[2] = cube[0];
cube[3] = cube[0];
cube[4] = cube[0];
cube[5] = cube[0];
pRenderContext->SetAmbientLightCube( cube );
}