Applied Fixes from the Valve Developer Wiki for detail props projected textures (including shadows and the flashlight).

This commit is contained in:
ZombieRoxtar 2015-05-19 13:24:02 -04:00
parent 7b2662f8ac
commit 16448139fb
8 changed files with 168 additions and 13 deletions

View file

@ -438,6 +438,12 @@ C_BasePlayer::C_BasePlayer() : m_iv_vecViewOffset( "C_BasePlayer::m_iv_vecViewOf
m_nForceVisionFilterFlags = 0;
ListenForGameEvent( "base_player_teleported" );
//*/
// At this point, the bugs that are being fixed are due to the bug-fixes...
// https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes#Fixing_cuts_in_projected_texture
//
ConVarRef scissor( "r_flashlightscissor" );
scissor.SetValue( "0" );
}
//-----------------------------------------------------------------------------

View file

@ -17,8 +17,13 @@
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
// https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes#Decreasing_shadow_bleeding_and_.22ghosting.22
static ConVar mat_slopescaledepthbias_shadowmap( "mat_slopescaledepthbias_shadowmap", "4", FCVAR_CHEAT );
static ConVar mat_depthbias_shadowmap( "mat_depthbias_shadowmap", "0.00001", FCVAR_CHEAT );
/*
static ConVar mat_slopescaledepthbias_shadowmap( "mat_slopescaledepthbias_shadowmap", "16", FCVAR_CHEAT );
static ConVar mat_depthbias_shadowmap( "mat_depthbias_shadowmap", "0.0005", FCVAR_CHEAT );
//*/
//-----------------------------------------------------------------------------
// Purpose:
@ -151,6 +156,9 @@ void C_EnvProjectedTexture::UpdateLight( bool bForceUpdate )
VectorNormalize( vUp );
}
}
/*
//Swapping this out for the code from https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes
//
else
{
vForward = m_hTargetEntity->GetAbsOrigin() - GetAbsOrigin();
@ -169,6 +177,24 @@ void C_EnvProjectedTexture::UpdateLight( bool bForceUpdate )
// VectorNormalize( vRight );
// VectorNormalize( vUp );
}
//*/
else
{
// VXP: Fixing targeting
Vector vecToTarget;
QAngle vecAngles;
if ( m_hTargetEntity == NULL )
{
vecAngles = GetAbsAngles();
}
else
{
vecToTarget = m_hTargetEntity->GetAbsOrigin() - GetAbsOrigin();
VectorAngles( vecToTarget, vecAngles );
}
AngleVectors( vecAngles, &vForward, &vRight, &vUp );
}
//*/
}
else
{
@ -221,15 +247,19 @@ void C_EnvProjectedTexture::UpdateLight( bool bForceUpdate )
g_pClientShadowMgr->SetFlashlightLightWorld( m_LightHandle, m_bLightWorld );
if ( bForceUpdate == false )
{
//*/ https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes#Fixing_Parenting
//if ( bForceUpdate == false )
//{
g_pClientShadowMgr->UpdateProjectedTexture( m_LightHandle, true );
}
//}
//*/
}
void C_EnvProjectedTexture::Simulate( void )
{
UpdateLight( false );
// Same Fix
//UpdateLight( false );
UpdateLight( GetMoveParent() != NULL );
BaseClass::Simulate();
}

View file

@ -1289,9 +1289,15 @@ bool CClientShadowMgr::Init()
SetShadowDistance( 50 );
SetShadowBlobbyCutoffArea( 0.005 );
/*
// https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes
//
bool bTools = CommandLine()->CheckParm( "-tools" ) != NULL;
m_nMaxDepthTextureShadows = bTools ? 4 : 1; // Just one shadow depth texture in games, more in tools
//*/
// I just made that number up, but the wiki suggests staying < 10
// as a new render texture is created for each shadow map.
m_nMaxDepthTextureShadows = 8;
bool bLowEnd = ( g_pMaterialSystemHardwareConfig->GetDXSupportLevel() < 80 );
@ -1328,7 +1334,7 @@ void CClientShadowMgr::Shutdown()
materials->RemoveRestoreFunc( ShadowRestoreFunc );
}
/* Code was added to this function from https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes#Create_a_shadow_map_depth_texture_greater_than_the_framebuffer_size */
//-----------------------------------------------------------------------------
// Initialize, shutdown depth-texture shadows
//-----------------------------------------------------------------------------
@ -1336,6 +1342,13 @@ void CClientShadowMgr::InitDepthTextureShadows()
{
VPROF_BUDGET( "CClientShadowMgr::InitDepthTextureShadows", VPROF_BUDGETGROUP_SHADOW_DEPTH_TEXTURING );
// SAUL: start benchmark timer
CFastTimer timer;
timer.Start();
// SAUL: set m_nDepthTextureResolution to the depth resolution we want
m_nDepthTextureResolution = r_flashlightdepthres.GetInt();
if( !m_bDepthTextureActive )
{
m_bDepthTextureActive = true;
@ -1352,7 +1365,9 @@ void CClientShadowMgr::InitDepthTextureShadows()
m_DummyColorTexture.InitRenderTargetTexture( r_flashlightdepthres.GetInt(), r_flashlightdepthres.GetInt(), RT_SIZE_OFFSCREEN, IMAGE_FORMAT_BGR565, MATERIAL_RT_DEPTH_SHARED, false, "_rt_ShadowDummy" );
m_DummyColorTexture.InitRenderTargetSurface( r_flashlightdepthres.GetInt(), r_flashlightdepthres.GetInt(), IMAGE_FORMAT_BGR565, true );
#else
m_DummyColorTexture.InitRenderTarget( r_flashlightdepthres.GetInt(), r_flashlightdepthres.GetInt(), RT_SIZE_OFFSCREEN, nullFormat, MATERIAL_RT_DEPTH_NONE, false, "_rt_ShadowDummy" );
// SAUL: we want to create a render target of specific size, so use RT_SIZE_NO_CHANGE
m_DummyColorTexture.InitRenderTarget( m_nDepthTextureResolution, m_nDepthTextureResolution, RT_SIZE_NO_CHANGE, nullFormat, MATERIAL_RT_DEPTH_NONE, false, "_rt_ShadowDummy" );
//m_DummyColorTexture.InitRenderTarget( r_flashlightdepthres.GetInt(), r_flashlightdepthres.GetInt(), RT_SIZE_OFFSCREEN, nullFormat, MATERIAL_RT_DEPTH_NONE, false, "_rt_ShadowDummy" );
#endif
// Create some number of depth-stencil textures
@ -1372,7 +1387,9 @@ void CClientShadowMgr::InitDepthTextureShadows()
depthTex.InitRenderTargetTexture( m_nDepthTextureResolution, m_nDepthTextureResolution, RT_SIZE_OFFSCREEN, dstFormat, MATERIAL_RT_DEPTH_NONE, false, strRTName );
depthTex.InitRenderTargetSurface( 1, 1, dstFormat, false );
#else
depthTex.InitRenderTarget( m_nDepthTextureResolution, m_nDepthTextureResolution, RT_SIZE_OFFSCREEN, dstFormat, MATERIAL_RT_DEPTH_NONE, false, strRTName );
// SAUL: we want to create a *DEPTH TEXTURE* of specific size, so use RT_SIZE_NO_CHANGE and MATERIAL_RT_DEPTH_ONLY
depthTex.InitRenderTarget( m_nDepthTextureResolution, m_nDepthTextureResolution, RT_SIZE_NO_CHANGE, dstFormat, MATERIAL_RT_DEPTH_ONLY, false, strRTName );
//depthTex.InitRenderTarget( m_nDepthTextureResolution, m_nDepthTextureResolution, RT_SIZE_OFFSCREEN, dstFormat, MATERIAL_RT_DEPTH_NONE, false, strRTName );
#endif
if ( i == 0 )
@ -1382,12 +1399,18 @@ void CClientShadowMgr::InitDepthTextureShadows()
r_flashlightdepthres.SetValue( m_nDepthTextureResolution );
}
// SAUL: ensure the depth texture size wasn't changed
Assert(depthTex->GetActualWidth() == m_nDepthTextureResolution);
m_DepthTextureCache.AddToTail( depthTex );
m_DepthTextureCacheLocks.AddToTail( bFalse );
}
materials->EndRenderTargetAllocation();
}
timer.End();
DevMsg("InitDepthTextureShadows took %.2f msec\n", timer.GetDuration().GetMillisecondsF());
}
void CClientShadowMgr::ShutdownDepthTextureShadows()
@ -2623,6 +2646,17 @@ void CClientShadowMgr::BuildFlashlight( ClientShadowHandle_t handle )
VPROF_BUDGET( "CClientShadowMgr::BuildFlashlight", VPROF_BUDGETGROUP_SHADOW_DEPTH_TEXTURING );
/*
// https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes#Enabling_visibility_tests
//
// Don't project the flashlight if the frustum AABB is not in our view
Vector mins, maxs;
CalculateAABBFromProjectionMatrix(shadow.m_WorldToShadow, &mins, &maxs);
if(engine->CullBox(mins, maxs))
return;
//*/
bool bLightModels = r_flashlightmodels.GetBool();
bool bLightSpecificEntity = shadow.m_hTargetEntity.Get() != NULL;
bool bLightWorld = ( shadow.m_Flags & SHADOW_FLAGS_LIGHT_WORLD ) != 0;
@ -3847,7 +3881,9 @@ int CClientShadowMgr::BuildActiveShadowDepthList( const CViewSetup &viewSetup, i
// Bail if this flashlight doesn't want shadows
if ( !flashlightState.m_bEnableShadows )
continue;
/*
// https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes#Removing_incorrect_culling
//
// Calculate an AABB around the shadow frustum
Vector vecAbsMins, vecAbsMaxs;
CalculateAABBFromProjectionMatrix( shadow.m_WorldToShadow, &vecAbsMins, &vecAbsMaxs );
@ -3862,6 +3898,7 @@ int CClientShadowMgr::BuildActiveShadowDepthList( const CViewSetup &viewSetup, i
shadowmgr->SetFlashlightDepthTexture( shadow.m_ShadowHandle, NULL, 0 );
continue;
}
//*/
if ( nActiveDepthShadowCount >= nMaxDepthShadows )
{

View file

@ -1470,7 +1470,9 @@ void CDetailObjectSystem::LevelInitPreEntity()
break;
}
}
/*
// Dumping this code as according to https://developer.valvesoftware.com/wiki/Detail_props/Aspect_ratio_fix
//
if ( m_DetailObjects.Count() || m_DetailSpriteDict.Count() )
{
// There are detail objects in the level, so precache the material
@ -1489,6 +1491,7 @@ void CDetailObjectSystem::LevelInitPreEntity()
}
}
}
//*/
int detailPropLightingLump;
if( g_pMaterialSystemHardwareConfig->GetHDRType() != HDR_TYPE_NONE )
@ -1512,6 +1515,9 @@ void CDetailObjectSystem::LevelInitPreEntity()
void CDetailObjectSystem::LevelInitPostEntity()
{
/*
// Replacing code as according to same fix mentioned above
//
const char *pDetailSpriteMaterial = DETAIL_SPRITE_MATERIAL;
C_World *pWorld = GetClientWorldEntity();
if ( pWorld && pWorld->GetDetailSpriteMaterial() && *(pWorld->GetDetailSpriteMaterial()) )
@ -1519,6 +1525,32 @@ void CDetailObjectSystem::LevelInitPostEntity()
pDetailSpriteMaterial = pWorld->GetDetailSpriteMaterial();
}
m_DetailSpriteMaterial.Init( pDetailSpriteMaterial, TEXTURE_GROUP_OTHER );
//*/
if ( m_DetailObjects.Count() || m_DetailSpriteDict.Count() )
{
const char *pDetailSpriteMaterial = DETAIL_SPRITE_MATERIAL;
C_World *pWorld = GetClientWorldEntity();
if ( pWorld && pWorld->GetDetailSpriteMaterial() && *(pWorld->GetDetailSpriteMaterial()) )
pDetailSpriteMaterial = pWorld->GetDetailSpriteMaterial();
m_DetailSpriteMaterial.Init( pDetailSpriteMaterial, TEXTURE_GROUP_OTHER );
PrecacheMaterial( pDetailSpriteMaterial );
IMaterial *pMat = m_DetailSpriteMaterial;
// adjust for non-square textures (cropped)
float flRatio = pMat->GetMappingWidth() / pMat->GetMappingHeight();
if ( flRatio > 1.0 )
{
for( int i = 0; i<m_DetailSpriteDict.Count(); i++ )
{
m_DetailSpriteDict[i].m_TexUL.y *= flRatio;
m_DetailSpriteDict[i].m_TexLR.y *= flRatio;
m_DetailSpriteDictFlipped[i].m_TexUL.y *= flRatio;
m_DetailSpriteDictFlipped[i].m_TexLR.y *= flRatio;
}
}
}
//*/
if ( GetDetailController() )
{

View file

@ -4983,6 +4983,13 @@ void CShadowDepthView::Draw()
render->Push3DView( (*this), VIEW_CLEAR_DEPTH, m_pRenderTarget, GetFrustum() );
}
//https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes#Create_a_shadow_map_depth_texture_greater_than_the_framebuffer_size
//*/
pRenderContext.GetFrom(materials);
pRenderContext->PushRenderTargetAndViewport(m_pRenderTarget, m_pDepthTexture, 0, 0, m_pDepthTexture->GetMappingWidth(), m_pDepthTexture->GetMappingWidth());
pRenderContext.SafeRelease();
//*/
SetupCurrentView( origin, angles, VIEW_SHADOW_DEPTH_TEXTURE );
MDLCACHE_CRITICAL_SECTION();
@ -5027,6 +5034,10 @@ void CShadowDepthView::Draw()
pRenderContext->CopyRenderTargetToTextureEx( m_pDepthTexture, -1, NULL, NULL );
}
// Same fix -^
pRenderContext->PopRenderTargetAndViewport();
//
render->PopView( GetFrustum() );
#if defined( _X360 )

View file

@ -73,7 +73,8 @@ BEGIN_DATADESC( CEnvProjectedTexture )
DEFINE_KEYFIELD( m_bLightWorld, FIELD_BOOLEAN, "lightworld" ),
DEFINE_KEYFIELD( m_bCameraSpace, FIELD_BOOLEAN, "cameraspace" ),
DEFINE_KEYFIELD( m_flAmbient, FIELD_FLOAT, "ambient" ),
DEFINE_AUTO_ARRAY_KEYFIELD( m_SpotlightTextureName, FIELD_CHARACTER, "texturename" ),
DEFINE_AUTO_ARRAY( m_SpotlightTextureName, FIELD_CHARACTER ), // https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes#Fix_configurable_texture_value_in_Hammer
//DEFINE_AUTO_ARRAY_KEYFIELD( m_SpotlightTextureName, FIELD_CHARACTER, "texturename" ),
DEFINE_KEYFIELD( m_nSpotlightTextureFrame, FIELD_INTEGER, "textureframe" ),
DEFINE_KEYFIELD( m_flNearZ, FIELD_FLOAT, "nearz" ),
DEFINE_KEYFIELD( m_flFarZ, FIELD_FLOAT, "farz" ),
@ -155,6 +156,25 @@ void UTIL_ColorStringToLinearFloatColor( Vector &color, const char *pString )
bool CEnvProjectedTexture::KeyValue( const char *szKeyName, const char *szValue )
{
if ( FStrEq( szKeyName, "lightcolor" ) )
{
Vector tmp;
UTIL_ColorStringToLinearFloatColor( tmp, szValue );
m_LinearFloatLightColor = tmp;
}
else if ( FStrEq(szKeyName, "texturename" ) )
{
Q_strcpy( m_SpotlightTextureName.GetForModify(), szValue );
}
else
{
return BaseClass::KeyValue( szKeyName, szValue );
}
return true;
//
// https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes#Fix_configurable_texture_value_in_Hammer
/*
//
if ( FStrEq( szKeyName, "lightcolor" ) )
{
Vector tmp;
@ -167,6 +187,7 @@ bool CEnvProjectedTexture::KeyValue( const char *szKeyName, const char *szValue
}
return true;
//*/
}
void CEnvProjectedTexture::InputTurnOn( inputdata_t &inputdata )
@ -239,7 +260,23 @@ void CEnvProjectedTexture::Activate( void )
void CEnvProjectedTexture::InitialThink( void )
{
/*
// https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes#Fixing_targeting
//
m_hTargetEntity = gEntList.FindEntityByName( NULL, m_target );
//*/
if ( m_hTargetEntity == NULL && m_target != NULL_STRING )
m_hTargetEntity = gEntList.FindEntityByName( NULL, m_target );
if ( m_hTargetEntity == NULL )
return;
Vector vecToTarget = (m_hTargetEntity->GetAbsOrigin() - GetAbsOrigin());
QAngle vecAngles;
VectorAngles( vecToTarget, vecAngles );
SetAbsAngles( vecAngles );
SetNextThink( gpGlobals->curtime + 0.1 );
//*/
}
int CEnvProjectedTexture::UpdateTransmitState()

View file

@ -145,7 +145,9 @@ public:
// Should this object receive shadows?
virtual bool ShouldReceiveProjectedTextures( int flags )
{
return false;
// https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes#Enabling_shadow_receiving_on_the_view_model
//return false;
return true;
}
// Add entity to visible view models list?

View file

@ -423,7 +423,7 @@ struct FlashlightState_t
m_bEnableShadows = false; // Provide reasonable defaults for shadow depth mapping parameters
m_bDrawShadowFrustum = false;
m_flShadowMapResolution = 1024.0f;
m_flShadowFilterSize = 3.0f;
m_flShadowFilterSize = 1.0f; //3.0f; //https://developer.valvesoftware.com/wiki/Env_projectedtexture/fixes#Lowering_the_amount_of_.22grain.22_on_shadows
m_flShadowSlopeScaleDepthBias = 16.0f;
m_flShadowDepthBias = 0.0005f;
m_flShadowJitterSeed = 0.0f;