//-----------------------------------------------------------------------------
// paint it!
//-----------------------------------------------------------------------------
void CVMTPanel::OnPaint3D()
{
	if (!m_pMaterial)
		return;

	// Deal with refraction
	if ( m_pMaterial->NeedsPowerOfTwoFrameBufferTexture() )
	{
		ITexture *pTexture = GetPowerOfTwoFrameBufferTexture();
		if ( pTexture && !pTexture->IsError() )
		{
			CMatRenderContextPtr pRenderContext( MaterialSystem() );
			pRenderContext->CopyRenderTargetToTexture( pTexture );
			pRenderContext->SetFrameBufferCopyTexture( pTexture );
		}
	}

	// Draw a background (translucent objects will appear that way)

	// FIXME: Draw the outline of this panel?

//	pRenderContext->CullMode(MATERIAL_CULLMODE_CCW);

	RenderSphere( vec3_origin, SPHERE_RADIUS, 20, 20 );
	/*
	pRenderContext->MatrixMode( MATERIAL_PROJECTION );
	pRenderContext->LoadIdentity();
	pRenderContext->Ortho( 0, 0, m_iViewableWidth, m_iViewableHeight, 0, 1 );

	pRenderContext->Bind( m_pMaterial );
	IMesh *pMesh = pRenderContext->GetDynamicMesh();
	CMeshBuilder meshBuilder;

	meshBuilder.Begin( pMesh, MATERIAL_QUADS, 1 );

	if (!m_bUseActualSize)
	{
		DrawStretchedToPanel( meshBuilder );
	}
	else
	{
		DrawActualSize( meshBuilder );
	}

	meshBuilder.End();
	pMesh->Draw();
	*/
}
//-----------------------------------------------------------------------------
// Paints the texture
//-----------------------------------------------------------------------------
void CVMTPreviewPanel::Paint( void )
{
	CMatRenderContextPtr pRenderContext( MaterialSystem() );
	int w, h;
	GetSize( w, h );
	vgui::MatSystemSurface()->Begin3DPaint( 0, 0, w, h );

	// Deal with refraction
	if ( m_Material->NeedsPowerOfTwoFrameBufferTexture() )
	{
		ITexture *pTexture = GetPowerOfTwoFrameBufferTexture();
		if ( pTexture && !pTexture->IsError() )
		{
			pRenderContext->CopyRenderTargetToTexture( pTexture );
			pRenderContext->SetFrameBufferCopyTexture( pTexture );
		}
	}

	pRenderContext->ClearColor4ub( 76, 88, 68, 255 ); 
	pRenderContext->ClearBuffers( true, true );
	 			   
	pRenderContext->FogMode( MATERIAL_FOG_NONE );
	pRenderContext->SetNumBoneWeights( 0 );
	pRenderContext->Bind( m_Material );
 	pRenderContext->BindLightmapTexture( m_pLightmapTexture );
	pRenderContext->BindLocalCubemap( m_DefaultEnvCubemap );

	if ( m_bDrawIn3DMode || m_Material->IsSpriteCard() )
	{
		DrawSphere();
	}
	else
	{
		DrawRectangle();
	}

	vgui::MatSystemSurface()->End3DPaint( );
}
void CPlayerTextureProxy::OnBind( C_BaseEntity *pEnt )
{
	// Bail if no base variable
	if ( !m_pBaseTextureVar )
		return;

	CTeamKitInfo *pKitInfo;
	const char *teamFolder;
	const char *kitFolder;
	int skinIndex;
	int shirtNumber;
	const char *shirtName;
	bool isKeeper;

	ITexture *pDetailTexture;
	CProceduralRegenerator **pProcReg;

	if (dynamic_cast<C_SDKPlayer *>(pEnt))
	{
		C_SDKPlayer *pPl = dynamic_cast<C_SDKPlayer *>(pEnt);

		C_Team *pTeam = GetGlobalTeam(g_PR->GetTeam(pPl->index));

		teamFolder = pTeam->GetFolderName();
		kitFolder = pTeam->GetKitFolderName();
		pKitInfo = pTeam->GetKitInfo();
		skinIndex = g_PR->GetSkinIndex(pPl->index);
		shirtNumber = g_PR->GetShirtNumber(pPl->index);
		shirtName = g_PR->GetShirtName(pPl->index);
		isKeeper = g_PR->GetTeamPosType(pPl->index) == POS_GK;

		int teamIndex = pTeam->GetTeamNumber() - TEAM_A;
		int posIndex = g_PR->GetTeamPosIndex(pPl->index);

		pDetailTexture = materials->FindTexture(VarArgs("models/player/default/detail_%d_%d", teamIndex, posIndex), NULL, true);
		pProcReg = &m_pTextureRegen[teamIndex][posIndex];
	}
	else if (dynamic_cast<C_ReplayPlayer *>(pEnt))
	{
		C_ReplayPlayer *pReplayPl = dynamic_cast<C_ReplayPlayer *>(pEnt);

		C_Team *pTeam = GetGlobalTeam(pReplayPl->m_nTeamNumber);

		teamFolder = pTeam->GetFolderName();
		kitFolder = pTeam->GetKitFolderName();
		pKitInfo = pTeam->GetKitInfo();
		skinIndex = pReplayPl->m_nSkinIndex;
		shirtNumber = pReplayPl->m_nShirtNumber;
		shirtName = pReplayPl->m_szShirtName;
		isKeeper = pReplayPl->m_bIsKeeper;

		int teamIndex = pReplayPl->m_nTeamNumber - TEAM_A;
		int posIndex = pReplayPl->m_nTeamPosIndex;

		pDetailTexture = materials->FindTexture(VarArgs("models/player/default/detail_%d_%d", teamIndex, posIndex), NULL, true);
		pProcReg = &m_pTextureRegen[teamIndex][posIndex];
	}
	else if (dynamic_cast<C_BaseAnimatingOverlay *>(pEnt))
	{
		C_BaseAnimatingOverlay *pPlayerModelPreview = dynamic_cast<C_BaseAnimatingOverlay *>(pEnt);

		CAppearanceSettingPanel *pPanel = (CAppearanceSettingPanel *)iosOptionsMenu->GetPanel()->GetSettingPanel(SETTING_PANEL_APPEARANCE);

		pPanel->GetPlayerTeamInfo(&teamFolder, &kitFolder);
		pKitInfo = CTeamInfo::FindTeamByKitName(VarArgs("%s/%s", teamFolder, kitFolder));
		skinIndex = pPanel->GetPlayerSkinIndex();
		shirtNumber = pPanel->GetPlayerOutfieldShirtNumber();
		shirtName = pPanel->GetPlayerShirtName();
		isKeeper = false;

		pDetailTexture = materials->FindTexture("models/player/default/detail_preview", NULL, true);
		pProcReg = &m_pPreviewTextureRegen;
	}
	else
	{
		return;
	}

	if (!Q_strcmp(m_szTextureType, "shirt") || !Q_strcmp(m_szTextureType, "keepershirt"))
	{
		if (!(*pProcReg))
			*pProcReg = new CProceduralRegenerator();

		pDetailTexture->SetTextureRegenerator(*pProcReg);
		m_pDetailTextureVar->SetTextureValue(pDetailTexture);

		bool needsUpdate;

		if (isKeeper)
			needsUpdate = (*pProcReg)->SetPlayerInfo(shirtName, shirtNumber, pKitInfo->m_KeeperShirtNameColor, pKitInfo->m_nKeeperShirtNameOffset, pKitInfo->m_KeeperShirtNumberColor, pKitInfo->m_nKeeperShirtNumberOffset, true, pKitInfo->m_pFontAtlas);
		else
			needsUpdate = (*pProcReg)->SetPlayerInfo(shirtName, shirtNumber, pKitInfo->m_OutfieldShirtNameColor, pKitInfo->m_nOutfieldShirtNameOffset, pKitInfo->m_OutfieldShirtNumberColor, pKitInfo->m_nOutfieldShirtNumberOffset, false, pKitInfo->m_pFontAtlas);
				
		if (needsUpdate)
			pDetailTexture->Download();
	}

	char texture[128];

	if (Q_stricmp(m_szTextureType, "shirt") == 0)
		Q_snprintf(texture, sizeof(texture), "%s/%s/%s/outfield", TEAMKITS_PATH, teamFolder, kitFolder);
	else if (Q_stricmp(m_szTextureType, "keepershirt") == 0)
		Q_snprintf(texture, sizeof(texture), "%s/%s/%s/keeper", TEAMKITS_PATH, teamFolder, kitFolder);
	else if (Q_stricmp(m_szTextureType, "socks") == 0)
		Q_snprintf(texture, sizeof(texture), "%s/%s/%s/socks", TEAMKITS_PATH, teamFolder, kitFolder);
	else if (Q_stricmp(m_szTextureType, "gksocks") == 0)
		Q_snprintf(texture, sizeof(texture), "%s/%s/%s/gksocks", TEAMKITS_PATH, teamFolder, kitFolder);
	else if (Q_stricmp(m_szTextureType, "skin") == 0)
		Q_snprintf(texture, sizeof(texture), "models/player/skins/skin%d", skinIndex + 1);
	else
		Q_snprintf(texture, sizeof(texture), "%s", m_pTexture->GetName());

	ITexture *pNewTex = materials->FindTexture(texture, NULL, false);

	if (!pNewTex->IsError())
		m_pTexture = pNewTex;

	m_pBaseTextureVar->SetTextureValue(m_pTexture);
		
	GetMaterial()->RecomputeStateSnapshots();
}