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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#ifndef VIEW_SCENE_H
#define VIEW_SCENE_H
#ifdef _WIN32
#pragma once
#endif
#include "convar.h"
#include "iviewrender.h"
#include "view_shared.h"
#include "rendertexture.h"
#include "materialsystem/itexture.h"
extern ConVar mat_wireframe;
extern ConVar building_cubemaps;
// Transform into view space (translate and rotate the camera into the origin).
void ViewTransform( const Vector &worldSpace, Vector &viewSpace );
// Transform a world point into normalized screen space (X and Y from -1 to 1).
// Returns 0 if the point is behind the viewer.
int ScreenTransform( const Vector& point, Vector& screen );
int HudTransform( const Vector& point, Vector& screen );
extern ConVar r_updaterefracttexture;
extern int g_viewscene_refractUpdateFrame;
extern bool g_bAllowMultipleRefractUpdatesPerScenePerFrame;
bool DrawingShadowDepthView( void );
bool DrawingMainView();
inline void UpdateRefractTexture( int x, int y, int w, int h, bool bForceUpdate = false )
{
Assert( !DrawingShadowDepthView() );
if ( !IsRetail() && !r_updaterefracttexture.GetBool() )
return;
CMatRenderContextPtr pRenderContext( materials );
ITexture *pTexture = GetPowerOfTwoFrameBufferTexture();
if ( IsPC() || bForceUpdate || g_bAllowMultipleRefractUpdatesPerScenePerFrame || (gpGlobals->framecount != g_viewscene_refractUpdateFrame) )
{
// forced or only once per frame
Rect_t rect;
rect.x = x;
rect.y = y;
rect.width = w;
rect.height = h;
pRenderContext->CopyRenderTargetToTextureEx( pTexture, 0, &rect, NULL );
g_viewscene_refractUpdateFrame = gpGlobals->framecount;
}
pRenderContext->SetFrameBufferCopyTexture( pTexture );
}
inline void UpdateRefractTexture( bool bForceUpdate = false )
{
Assert( !DrawingShadowDepthView() );
CMatRenderContextPtr pRenderContext( materials );
int x,y,w,h;
pRenderContext->GetViewport( x, y, w, h );
UpdateRefractTexture( x, y, w, h, bForceUpdate );
}
inline void UpdateScreenEffectTexture( int textureIndex, int x, int y, int w, int h, bool bDestFullScreen = false, Rect_t *pActualRect = NULL )
{
Rect_t srcRect;
srcRect.x = x;
srcRect.y = y;
srcRect.width = w;
srcRect.height = h;
CMatRenderContextPtr pRenderContext( materials );
ITexture *pTexture = GetFullFrameFrameBufferTexture( textureIndex );
int nSrcWidth, nSrcHeight;
pRenderContext->GetRenderTargetDimensions( nSrcWidth, nSrcHeight );
int nDestWidth = pTexture->GetActualWidth();
int nDestHeight = pTexture->GetActualHeight();
Rect_t destRect = srcRect;
if( !bDestFullScreen && ( nSrcWidth > nDestWidth || nSrcHeight > nDestHeight ) )
{
// the source and target sizes aren't necessarily the same (specifically in dx7 where
// nonpow2 rendertargets aren't supported), so lets figure it out here.
float scaleX = ( float )nDestWidth / ( float )nSrcWidth;
float scaleY = ( float )nDestHeight / ( float )nSrcHeight;
destRect.x = srcRect.x * scaleX;
destRect.y = srcRect.y * scaleY;
destRect.width = srcRect.width * scaleX;
destRect.height = srcRect.height * scaleY;
destRect.x = clamp( destRect.x, 0, nDestWidth );
destRect.y = clamp( destRect.y, 0, nDestHeight );
destRect.width = clamp( destRect.width, 0, nDestWidth - destRect.x );
destRect.height = clamp( destRect.height, 0, nDestHeight - destRect.y );
}
pRenderContext->CopyRenderTargetToTextureEx( pTexture, 0, &srcRect, bDestFullScreen ? NULL : &destRect );
pRenderContext->SetFrameBufferCopyTexture( pTexture, textureIndex );
if ( pActualRect )
{
pActualRect->x = destRect.x;
pActualRect->y = destRect.y;
pActualRect->width = destRect.width;
pActualRect->height = destRect.height;
}
}
//-----------------------------------------------------------------------------
// Draws the screen effect
//-----------------------------------------------------------------------------
inline void DrawScreenEffectMaterial( IMaterial *pMaterial, int x, int y, int w, int h )
{
Rect_t actualRect;
UpdateScreenEffectTexture( 0, x, y, w, h, false, &actualRect );
ITexture *pTexture = GetFullFrameFrameBufferTexture( 0 );
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->DrawScreenSpaceRectangle( pMaterial, x, y, w, h,
actualRect.x, actualRect.y, actualRect.x+actualRect.width-1, actualRect.y+actualRect.height-1,
pTexture->GetActualWidth(), pTexture->GetActualHeight() );
}
//intended for use by dynamic meshes to naively update front buffer textures needed by a material
inline void UpdateFrontBufferTexturesForMaterial( IMaterial *pMaterial, bool bForce = false )
{
Assert( !DrawingShadowDepthView() );
if( pMaterial->NeedsPowerOfTwoFrameBufferTexture() )
{
UpdateRefractTexture( bForce );
}
else if( pMaterial->NeedsFullFrameBufferTexture() )
{
const CViewSetup *pView = view->GetViewSetup();
UpdateScreenEffectTexture( 0, pView->x, pView->y, pView->width, pView->height );
}
}
inline void UpdateScreenEffectTexture( void )
{
Assert( !DrawingShadowDepthView() );
const CViewSetup *pViewSetup = view->GetViewSetup();
UpdateScreenEffectTexture( 0, pViewSetup->x, pViewSetup->y, pViewSetup->width, pViewSetup->height);
}
// reset the tonem apping to a constant value, and clear the filter bank
void ResetToneMapping(float value);
void UpdateFullScreenDepthTexture( void );
#endif // VIEW_SCENE_H
|