-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathdmematerialoverlayfxclip.cpp
More file actions
323 lines (261 loc) · 9.72 KB
/
dmematerialoverlayfxclip.cpp
File metadata and controls
323 lines (261 loc) · 9.72 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
//====== Copyright © 1996-2004, Valve Corporation, All rights reserved. =======
//
// Purpose:
//
//=============================================================================
#include "movieobjects/dmematerialoverlayfxclip.h"
#include "datamodel/dmelementfactoryhelper.h"
#include "materialsystem/imesh.h"
#include "materialsystem/imaterial.h"
#include "tier1/keyvalues.h"
#include "tier1/convar.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// CDmeClip - common base class for filmclips, soundclips, and channelclips
//-----------------------------------------------------------------------------
IMPLEMENT_FX_CLIP_ELEMENT_FACTORY( DmeMaterialOverlayFXClip, CDmeMaterialOverlayFXClip, "Material Overlay Effect" );
void CDmeMaterialOverlayFXClip::OnConstruction()
{
m_Material.Init( this, "material" );
m_Color.Init( this, "overlaycolor" );
m_nLeft.Init( this, "left" );
m_nTop.Init( this, "top" );
m_nWidth.Init( this, "width" );
m_nHeight.Init( this, "height" );
m_bFullScreen.Init( this, "fullscreen" );
m_bUseSubRect.Init( this, "useSubRect" );
m_flMovementAngle.Init( this, "movementAngle" );
m_flMovementSpeed.Init( this, "movementSpeed" );
m_nSubRectLeft.Init( this, "subRectLeft" );
m_nSubRectTop.Init( this, "subRectTop" );
m_nSubRectWidth.Init( this, "subRectWidth" );
m_nSubRectHeight.Init( this, "subRectHeight" );
m_Color.SetColor( 255, 255, 255, 255 );
m_bFullScreen = true;
m_nLeft = m_nTop = 0;
m_nWidth = m_nHeight = 1;
// Set up the Blt material
KeyValues *pVMTKeyValues = new KeyValues( "accumbuff4sample" );
pVMTKeyValues->SetString( "$INPUT", "Effects/FilmScan256" ); // dummy
KeyValues *pProxiesKV = pVMTKeyValues->FindKey( "proxies", true ); // create a subkey
pProxiesKV->FindKey( "sfm_blt", true );
m_BltMaterial.Init( "accumbuff4sample", pVMTKeyValues );
m_BltMaterial->Refresh();
}
void CDmeMaterialOverlayFXClip::OnDestruction()
{
m_BltMaterial.Shutdown();
}
//-----------------------------------------------------------------------------
// Resolve
//-----------------------------------------------------------------------------
void CDmeMaterialOverlayFXClip::Resolve()
{
if ( m_Material.IsDirty() )
{
m_OverlayMaterial.Shutdown();
const char *pName = m_Material.Get();
if ( pName && pName[0] )
{
m_OverlayMaterial.Init( pName, NULL, false );
}
m_Material.GetAttribute()->RemoveFlag( FATTRIB_DIRTY );
}
}
//-----------------------------------------------------------------------------
// Helper for overlays
//-----------------------------------------------------------------------------
void CDmeMaterialOverlayFXClip::SetOverlayEffect( const char *pMaterialName )
{
m_Material = pMaterialName;
}
void CDmeMaterialOverlayFXClip::SetAlpha( float flAlpha )
{
m_Color.SetAlpha( flAlpha * 255 );
}
float CDmeMaterialOverlayFXClip::GetAlpha( void )
{
return ( (float)m_Color.a() ) / 255.0f;
}
bool CDmeMaterialOverlayFXClip::HasOpaqueOverlay( void )
{
if ( m_OverlayMaterial )
return ( !m_OverlayMaterial->IsTranslucent() && ( m_Color.a() == 255 ) && m_bFullScreen );
// no material overlay set
return false;
}
IMaterial *CDmeMaterialOverlayFXClip::GetMaterial()
{
return m_OverlayMaterial;
}
void CDmeMaterialOverlayFXClip::DrawQuad( int x, int y, int w, int h, float u0, float v0, float u1, float v1 )
{
CMatRenderContextPtr pRenderContext( materials );
IMesh *pMesh = pRenderContext->GetDynamicMesh();
CMeshBuilder meshBuilder;
meshBuilder.Begin( pMesh, MATERIAL_TRIANGLE_STRIP, 2 );
meshBuilder.Position3f( x, y, 0.0f );
meshBuilder.BoneWeight( 0, 1.0f );
meshBuilder.BoneMatrix( 0, 0 );
meshBuilder.Color4ub( 255, 255, 255, 255 );
meshBuilder.TexCoord2f( 0, u0, v0 );
meshBuilder.TexCoord2f( 1, 0.0f, 0.0f );
meshBuilder.TexCoord2f( 2, 0.0f, 0.0f );
meshBuilder.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 3>();
meshBuilder.Position3f( x, y+h, 0.0f );
meshBuilder.BoneWeight( 0, 1.0f );
meshBuilder.BoneMatrix( 0, 0 );
meshBuilder.Color4ub( 255, 255, 255, 255 );
meshBuilder.TexCoord2f( 0, u0, v1 );
meshBuilder.TexCoord2f( 1, 0.0f, 1.0f );
meshBuilder.TexCoord2f( 2, 0.0f, 0.0f );
meshBuilder.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 3>();
meshBuilder.Position3f( x+w, y, 0.0f );
meshBuilder.BoneWeight( 0, 1.0f );
meshBuilder.BoneMatrix( 0, 0 );
meshBuilder.Color4ub( 255, 255, 255, 255 );
meshBuilder.TexCoord2f( 0, u1, v0 );
meshBuilder.TexCoord2f( 1, 1.0f, 0.0f );
meshBuilder.TexCoord2f( 2, 0.0f, 0.0f );
meshBuilder.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 3>();
meshBuilder.Position3f( x+w, y+h, 0.0f );
meshBuilder.BoneWeight( 0, 1.0f );
meshBuilder.BoneMatrix( 0, 0 );
meshBuilder.Color4ub( 255, 255, 255, 255 );
meshBuilder.TexCoord2f( 0, u1, v1 );
meshBuilder.TexCoord2f( 1, 1.0f, 1.0f );
meshBuilder.TexCoord2f( 2, 0.0f, 0.0f );
meshBuilder.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 3>();
meshBuilder.End();
pMesh->Draw();
}
void CDmeMaterialOverlayFXClip::DrawOneToOneQuad( int nWidth, int nHeight )
{
CMatRenderContextPtr pRenderContext( materials );
IMesh *pMesh = pRenderContext->GetDynamicMesh();
CMeshBuilder meshBuilder;
// Epsilons for 1:1 texel to pixel mapping
float fWidthEpsilon = 0.5f / ((float) nWidth);
float fHeightEpsilon = 0.5f / ((float) nHeight);
meshBuilder.Begin( pMesh, MATERIAL_QUADS, 1 );
meshBuilder.Position3f( -1.0f, 1.0f, 0.5f ); // Upper left
meshBuilder.TexCoord2f( 0, 0.0f + fWidthEpsilon, 0.0f + fHeightEpsilon);
meshBuilder.AdvanceVertexF<VTX_HAVEPOS, 1>();
meshBuilder.Position3f( -1.0f, -1.0f, 0.5f ); // Lower left
meshBuilder.TexCoord2f( 0, 0.0f + fWidthEpsilon, 1.0f + fHeightEpsilon);
meshBuilder.AdvanceVertexF<VTX_HAVEPOS, 1>();
meshBuilder.Position3f( 1.0f, -1.0f, 0.5 ); // Lower right
meshBuilder.TexCoord2f( 0, 1.0f + fWidthEpsilon, 1.0f + fHeightEpsilon);
meshBuilder.AdvanceVertexF<VTX_HAVEPOS, 1>();
meshBuilder.Position3f( 1.0f, 1.0f, 0.5 ); // Upper right
meshBuilder.TexCoord2f( 0, 1.0f + fWidthEpsilon, 0.0f + fHeightEpsilon);
meshBuilder.AdvanceVertexF<VTX_HAVEPOS, 1>();
meshBuilder.End();
pMesh->Draw();
}
//-----------------------------------------------------------------------------
// All effects must be able to apply their effect
//-----------------------------------------------------------------------------
void CDmeMaterialOverlayFXClip::ApplyEffect( DmeTime_t time, Rect_t ¤tRect, Rect_t &totalRect, ITexture *pTextures[MAX_FX_INPUT_TEXTURES] )
{
if ( !m_OverlayMaterial || !m_BltMaterial || m_Color.a() == 0 )
return;
time = ToChildMediaTime( time, false );
// Clip the overlay rectangle to the currently drawn one
int x, y, w, h;
int tx, ty, tw, th;
if ( m_bFullScreen )
{
x = currentRect.x;
y = currentRect.y;
w = currentRect.width;
h = currentRect.height;
tx = ty = 0;
tw = totalRect.width;
th = totalRect.height;
}
else
{
x = clamp( m_nLeft.Get(), currentRect.x, currentRect.x + currentRect.width );
y = clamp( m_nTop.Get(), currentRect.y, currentRect.y + currentRect.height );
int x1 = clamp( m_nLeft + m_nWidth, currentRect.x, currentRect.x + currentRect.width );
int y1 = clamp( m_nTop + m_nHeight, currentRect.y, currentRect.y + currentRect.height );
w = x1 - x;
h = y1 - y;
tx = m_nLeft;
ty = m_nTop;
tw = m_nWidth;
th = m_nHeight;
// Clipped...
if ( w <= 0 || h <= 0 )
return;
}
if ( tw == 0 || th == 0 )
return;
// Compute texture coordinate range of the entire texture
int mw = m_OverlayMaterial->GetMappingWidth();
int mh = m_OverlayMaterial->GetMappingHeight();
// Compute the texture coords in texels we want over the entire image
float uMin = 0;
float uMax = mw;
float vMin = 0;
float vMax = mh;
if ( m_bUseSubRect )
{
uMin = m_nSubRectLeft;
vMin = m_nSubRectTop;
uMax = uMin + m_nSubRectWidth;
vMax = vMin + m_nSubRectHeight;
}
if ( m_flMovementSpeed )
{
float flRadians = M_PI * m_flMovementAngle / 180.0f;
float dUdT = -m_flMovementSpeed * cos( flRadians );
float dVdT = m_flMovementSpeed * sin( flRadians );
float dU = time.GetSeconds() * dUdT;
float dV = time.GetSeconds() * dVdT;
uMin += dU; uMax += dU;
vMin += dV; vMax += dV;
}
// This is the range of normalizes (u,v) coordinates over the *total* image
uMin = ( uMin + 0.5f ) / mw;
vMin = ( vMin + 0.5f ) / mh;
uMax = ( uMax - 0.5f ) / mw;
vMax = ( vMax - 0.5f ) / mh;
// Now determine the subrange we should use given we're rendering a portion of the image
float u0, v0, u1, v1, f;
f = ( x - tx ) / tw;
u0 = Lerp( f, uMin, uMax );
f = ( x + w - tx ) / tw;
u1 = Lerp( f, uMin, uMax );
f = ( y - ty ) / th;
v0 = Lerp( f, vMin, vMax );
f = ( y + h - ty ) / th;
v1 = Lerp( f, vMin, vMax );
x -= currentRect.x;
y -= currentRect.y;
CMatRenderContextPtr pRenderContext( materials );
if ( m_OverlayMaterial->NeedsPowerOfTwoFrameBufferTexture() )
{
pRenderContext->Bind( m_BltMaterial, pTextures[0] );
pRenderContext->PushRenderTargetAndViewport( pTextures[1], 0, 0, w, h ); // Blt current input to temp surface
DrawOneToOneQuad( w, h );
pRenderContext->PopRenderTargetAndViewport();
pRenderContext->SetFrameBufferCopyTexture( pTextures[1] );
pRenderContext->PushRenderTargetAndViewport( pTextures[0], 0, 0, w, h ); // Render to result surface
}
float r, g, b, a;
m_OverlayMaterial->GetColorModulation( &r, &g, &b );
a = m_OverlayMaterial->GetAlphaModulation();
m_OverlayMaterial->ColorModulate( m_Color.r() / 255.0f, m_Color.g() / 255.0f, m_Color.b() / 255.0f );
m_OverlayMaterial->AlphaModulate( m_Color.a() / 255.0f );
pRenderContext->Bind( m_OverlayMaterial );
DrawQuad( x, y, w, h, u0, v0, u1, v1 );
if ( m_OverlayMaterial->NeedsPowerOfTwoFrameBufferTexture() )
{
pRenderContext->PopRenderTargetAndViewport();
}
m_OverlayMaterial->ColorModulate( r, g, b );
m_OverlayMaterial->AlphaModulate( a );
}