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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include "cbase.h"
#include "modelimagepanel.h"
#include "iconrenderreceiver.h"
#include "materialsystem/imaterialvar.h"
#include "VGuiMatSurface/IMatSystemSurface.h"
#include "renderparm.h"
using namespace vgui;
const char *g_pszModelImagePanelRTName = "_rt_ModelImagePanel";
static vgui::DHANDLE<CModelImagePanel> s_hModelImageLockPanel;
#ifdef STAGING_ONLY
ConVar tf_modelimagepanel_ignore_cache( "tf_modelimagepanel_ignore_cache", "0" );
#endif
DECLARE_BUILD_FACTORY( CModelImagePanel );
CModelImagePanel::CModelImagePanel( vgui::Panel *pParent, const char *pName )
: BaseClass( pParent, pName )
{
m_pCachedIcon = NULL;
m_pCachedMaterial = NULL;
m_iCachedTextureID = -1;
}
CModelImagePanel::~CModelImagePanel()
{
InvalidateImage();
}
void CModelImagePanel::PerformLayout()
{
BaseClass::PerformLayout();
InvalidateImage();
}
void CModelImagePanel::OnSizeChanged( int wide, int tall )
{
BaseClass::OnSizeChanged( wide, tall );
InvalidateImage();
}
void CModelImagePanel::Paint()
{
// don't do anything for invalid model
if ( m_RootMDL.m_MDL.GetMDL() == MDLHANDLE_INVALID )
{
return;
}
// check lock panel
if ( s_hModelImageLockPanel )
{
// waiting for async copy to finish
if ( s_hModelImageLockPanel->m_pCachedIcon && s_hModelImageLockPanel->m_pCachedIcon->GetTexture() )
{
s_hModelImageLockPanel = NULL;
}
}
if ( m_pCachedIcon )
{
if ( m_pCachedIcon->GetTexture() )
{
if ( !m_pCachedMaterial && g_pMaterialSystem )
{
const char *pszTextureName = m_pCachedIcon->GetTexture()->GetName();
KeyValues *pVMTKeyValues = new KeyValues( "UnlitGeneric" );
pVMTKeyValues->SetString( "$basetexture", pszTextureName );
pVMTKeyValues->SetInt( "$translucent", 1 );
pVMTKeyValues->SetInt( "$vertexcolor", 1 );
IMaterial *pMaterial = g_pMaterialSystem->FindProceduralMaterial( pszTextureName, TEXTURE_GROUP_VGUI, pVMTKeyValues );
SafeAssign( &m_pCachedMaterial, pMaterial );
bool bFound = false;
IMaterialVar *pVar = m_pCachedMaterial->FindVar( "$basetexture", &bFound );
if ( bFound && pVar )
{
pVar->SetTextureValue( m_pCachedIcon->GetTexture() );
m_pCachedMaterial->RefreshPreservingMaterialVars();
}
}
if ( m_iCachedTextureID == -1 )
{
m_iCachedTextureID = g_pMatSystemSurface->DrawGetTextureId( m_pCachedIcon->GetTexture() );
g_pMatSystemSurface->DrawSetTextureMaterial( m_iCachedTextureID, m_pCachedMaterial );
}
}
else
{
// still waiting for texture
BaseClass::Paint();
return;
}
}
// just draw the texture if we got one.
if ( m_iCachedTextureID != -1 )
{
surface()->DrawSetTexture( m_iCachedTextureID );
surface()->DrawSetColor( 255, 255, 255, 255 );
const int iWidth = GetWide();
const int iHeight = GetTall();
const int iMappingWitdh = m_pCachedMaterial->GetMappingWidth();
const int iMappingHeight = m_pCachedMaterial->GetMappingHeight();
float flTexW, flTexH;
if ( iWidth > iMappingWitdh || iHeight > iMappingHeight )
{
float flScale = iWidth > iHeight ? (float)iMappingWitdh / iWidth : (float)iMappingHeight / iHeight;
flTexW = ( flScale * iWidth ) / iMappingWitdh;
flTexH = ( flScale * iHeight ) / iMappingHeight;
}
else
{
flTexW = (float)( iWidth - 1 ) / iMappingWitdh;
flTexH = (float)( iHeight - 1 ) / iMappingHeight;
}
surface()->DrawTexturedSubRect( 0, 0, iWidth, iHeight, 0.f, 0.f, flTexW, flTexH );
return;
}
// can't find available cache render target, don't do anything
if ( s_hModelImageLockPanel != NULL && s_hModelImageLockPanel != this )
{
BaseClass::Paint();
return;
}
CMatRenderContextPtr pRenderContext( materials );
// Turn off depth-write to dest alpha so that we get white there instead. The code that uses
// the render target needs a mask of where stuff was rendered.
pRenderContext->SetIntRenderingParameter( INT_RENDERPARM_WRITE_DEPTH_TO_DESTALPHA, false );
g_pMatSystemSurface->Set3DPaintTempRenderTarget( g_pszModelImagePanelRTName );
BaseClass::Paint();
// copy the rendered weapon skin from the render target
Assert( m_pCachedIcon == NULL );
CStudioHdr studioHdr( g_pMDLCache->GetStudioHdr( m_RootMDL.m_MDL.GetMDL() ), g_pMDLCache );
char buffer[_MAX_PATH];
CUtlString strMDLName = V_GetFileName( studioHdr.pszName() );
V_sprintf_safe( buffer, "proc/icon/mdl_%s_body%d_skin%d_w%d_h%d", strMDLName.StripExtension().Get(), m_RootMDL.m_MDL.m_nBody, m_RootMDL.m_MDL.m_nSkin, GetWide(), GetTall() );
SafeAssign( &m_pCachedIcon, new CIconRenderReceiver() );
// If the icon still exists in the material system, don't bother regenerating it.
if ( materials->IsTextureLoaded( buffer )
#ifdef STAGING_ONLY
&& !tf_modelimagepanel_ignore_cache.GetBool()
#endif
)
{
ITexture* resTexture = materials->FindTexture( buffer, TEXTURE_GROUP_RUNTIME_COMPOSITE, false, 0 );
if ( resTexture && resTexture->IsError() == false )
{
m_pCachedIcon->OnAsyncCreateComplete( resTexture, NULL );
}
}
else
{
// No icon available yet, need to create it.
ITexture *pRenderTarget = g_pMaterialSystem->FindTexture( g_pszModelImagePanelRTName, TEXTURE_GROUP_RENDER_TARGET );
if ( pRenderTarget )
{
pRenderContext->AsyncCreateTextureFromRenderTarget( pRenderTarget, buffer, IMAGE_FORMAT_RGBA8888, false, TEXTUREFLAGS_IMMEDIATE_CLEANUP, m_pCachedIcon, NULL );
// make this panel lock the render target
s_hModelImageLockPanel = this;
}
}
g_pMatSystemSurface->Reset3DPaintTempRenderTarget();
}
void CModelImagePanel::SetMDL( MDLHandle_t handle, void *pProxyData /*= NULL*/ )
{
BaseClass::SetMDL( handle, pProxyData );
InvalidateImage();
}
void CModelImagePanel::SetMDL( const char *pMDLName, void *pProxyData /*= NULL*/ )
{
BaseClass::SetMDL( pMDLName, pProxyData );
}
void CModelImagePanel::SetMDLBody( unsigned int nBody )
{
SetBody( nBody );
InvalidateImage();
}
void CModelImagePanel::SetMDLSkin( int nSkin )
{
SetSkin( nSkin );
InvalidateImage();
}
void CModelImagePanel::InvalidateImage()
{
SafeRelease( &m_pCachedIcon );
SafeRelease( &m_pCachedMaterial );
m_iCachedTextureID = -1;
}
|