//----------------------------------------------------------------------------- // Helpers for dealing with envmap tint //----------------------------------------------------------------------------- // set alphaVar to -1 to ignore it. void CBaseVSShader::SetEnvMapTintPixelShaderDynamicState( int pixelReg, int tintVar, int alphaVar, bool bConvertFromGammaToLinear ) { float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; if( g_pConfig->bShowSpecular && mat_fullbright.GetInt() != 2 ) { IMaterialVar* pAlphaVar = NULL; if( alphaVar >= 0 ) { pAlphaVar = s_ppParams[alphaVar]; } if( pAlphaVar ) { color[3] = pAlphaVar->GetFloatValue(); } IMaterialVar* pTintVar = s_ppParams[tintVar]; #ifdef _DEBUG pTintVar->GetVecValue( color, 3 ); float envmapTintOverride = mat_envmaptintoverride.GetFloat(); float envmapTintScaleOverride = mat_envmaptintscale.GetFloat(); if( envmapTintOverride != -1.0f ) { color[0] = color[1] = color[2] = envmapTintOverride; } if( envmapTintScaleOverride != -1.0f ) { color[0] *= envmapTintScaleOverride; color[1] *= envmapTintScaleOverride; color[2] *= envmapTintScaleOverride; } if( bConvertFromGammaToLinear ) { color[0] = color[0] > 1.0f ? color[0] : GammaToLinear( color[0] ); color[1] = color[1] > 1.0f ? color[1] : GammaToLinear( color[1] ); color[2] = color[2] > 1.0f ? color[2] : GammaToLinear( color[2] ); } #else if( bConvertFromGammaToLinear ) { pTintVar->GetLinearVecValue( color, 3 ); } else { pTintVar->GetVecValue( color, 3 ); } #endif } else { color[0] = color[1] = color[2] = color[3] = 0.0f; } s_pShaderAPI->SetPixelShaderConstant( pixelReg, color, 1 ); }
//----------------------------------------------------------------------------- // Returns the translucency... //----------------------------------------------------------------------------- void CBaseShader::ApplyColor2Factor( float *pColorOut ) const // (*pColorOut) *= COLOR2 { if ( !g_pConfig->bShowDiffuse ) { pColorOut[0] = pColorOut[1] = pColorOut[2] = 0.0f; return; } IMaterialVar* pColor2Var = s_ppParams[COLOR2]; if ( pColor2Var->GetType() == MATERIAL_VAR_TYPE_VECTOR ) { float flColor2[3]; pColor2Var->GetVecValue( flColor2, 3 ); pColorOut[0] *= flColor2[0]; pColorOut[1] *= flColor2[1]; pColorOut[2] *= flColor2[2]; } if ( g_pHardwareConfig->UsesSRGBCorrectBlending() ) { IMaterialVar* pSRGBVar = s_ppParams[SRGBTINT]; if (pSRGBVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) { float flSRGB[3]; pSRGBVar->GetVecValue( flSRGB, 3 ); pColorOut[0] *= flSRGB[0]; pColorOut[1] *= flSRGB[1]; pColorOut[2] *= flSRGB[2]; } } }
void CBaseOES2Shader::SetTextureScaledTransform(int reg, int transformVar, int scaleVar) { Vector4D transformation[2]; IMaterialVar *pTransformationVar = s_ppParams[transformVar]; if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX)) { const VMatrix &mat = pTransformationVar->GetMatrixValue(); transformation[0].Init(mat[0][0], mat[0][1], mat[0][2], mat[0][3]); transformation[1].Init(mat[1][0], mat[1][1], mat[1][2], mat[1][3]); } else { transformation[0].Init(1.0f, 0.0f, 0.0f, 0.0f); transformation[1].Init(0.0f, 1.0f, 0.0f, 0.0f); } float scale[2] = {1.0f, 1.0f}; IMaterialVar *pScaleVar = s_ppParams[scaleVar]; if (pScaleVar) { if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) pScaleVar->GetVecValue(scale, 2); else if (pScaleVar->IsDefined()) scale[0] = scale[1] = pScaleVar->GetFloatValue(); } transformation[0][0] *= scale[0]; transformation[0][1] *= scale[1]; transformation[1][0] *= scale[0]; transformation[1][1] *= scale[1]; transformation[0][3] *= scale[0]; transformation[1][3] *= scale[1]; s_pShaderAPI->Uniform4fv(reg, 2, transformation[0].Base()); }
void CBaseVSShader::SetPixelShaderTextureScaledTransform( int pixelReg, int transformVar, int scaleVar ) { Vector4D transformation[2]; IMaterialVar* pTransformationVar = s_ppParams[transformVar]; if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX)) { const VMatrix &mat = pTransformationVar->GetMatrixValue(); transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] ); transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] ); } else { transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f ); transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f ); } Vector2D scale( 1, 1 ); IMaterialVar* pScaleVar = s_ppParams[scaleVar]; if (pScaleVar) { if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) pScaleVar->GetVecValue( scale.Base(), 2 ); else if (pScaleVar->IsDefined()) scale[0] = scale[1] = pScaleVar->GetFloatValue(); } // Apply the scaling transformation[0][0] *= scale[0]; transformation[0][1] *= scale[1]; transformation[1][0] *= scale[0]; transformation[1][1] *= scale[1]; transformation[0][3] *= scale[0]; transformation[1][3] *= scale[1]; s_pShaderAPI->SetPixelShaderConstant( pixelReg, transformation[0].Base(), 2 ); }
//----------------------------------------------------------------------------- // Sets the color + transparency //----------------------------------------------------------------------------- void CBaseShader::SetColorState( int colorVar, bool setAlpha ) { Assert( !IsSnapshotting() ); if ( !s_ppParams ) return; // Use tint instead of color if it was specified... IMaterialVar* pColorVar = (colorVar != -1) ? s_ppParams[colorVar] : 0; float color[4] = { 1.0, 1.0, 1.0, 1.0 }; if (pColorVar) { if (pColorVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) { pColorVar->GetVecValue( color, 3 ); } else { color[0] = color[1] = color[2] = pColorVar->GetFloatValue(); } // Clamp... color[0] = clamp( color[0], 0.0f, 1.0f ); color[1] = clamp( color[1], 0.0f, 1.0f ); color[2] = clamp( color[2], 0.0f, 1.0f ); } color[3] = setAlpha ? GetAlpha() : 1.0f; s_pShaderAPI->Color4fv( color ); }
void CBaseVSShader::SetPixelShaderConstantGammaToLinear( int pixelReg, int constantVar, int constantVar2 ) { Assert( !IsSnapshotting() ); if ((!s_ppParams) || (constantVar == -1) || (constantVar2 == -1)) return; IMaterialVar* pPixelVar = s_ppParams[constantVar]; Assert( pPixelVar ); IMaterialVar* pPixelVar2 = s_ppParams[constantVar2]; Assert( pPixelVar2 ); float val[4]; if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) { pPixelVar->GetVecValue( val, 3 ); } else { val[0] = val[1] = val[2] = pPixelVar->GetFloatValue(); } val[3] = pPixelVar2->GetFloatValue(); val[0] = val[0] > 1.0f ? val[0] : GammaToLinear( val[0] ); val[1] = val[1] > 1.0f ? val[1] : GammaToLinear( val[1] ); val[2] = val[2] > 1.0f ? val[2] : GammaToLinear( val[2] ); s_pShaderAPI->SetPixelShaderConstant( pixelReg, val ); }
//------------------------------------------------------------------------------ // Purpose : Create leak effect if material requests it // Input : // Output : //------------------------------------------------------------------------------ void LeakEffect( trace_t &tr ) { Vector diffuseColor, baseColor; Vector vTraceDir = (tr.endpos - tr.startpos); VectorNormalize(vTraceDir); Vector vTraceStart = tr.endpos - 0.1*vTraceDir; Vector vTraceEnd = tr.endpos + 0.1*vTraceDir; IMaterial* pTraceMaterial = engine->TraceLineMaterialAndLighting( vTraceStart, vTraceEnd, diffuseColor, baseColor ); if (!pTraceMaterial) return; bool found; IMaterialVar *pLeakVar = pTraceMaterial->FindVar( "$leakamount", &found, false ); if( !found ) return; C_Splash* pLeak = new C_Splash(); if (!pLeak) return; ClientEntityList().AddNonNetworkableEntity( pLeak->GetIClientUnknown() ); IMaterialVar* pLeakColorVar = pTraceMaterial->FindVar( "$leakcolor", &found ); if (found) { Vector color; pLeakColorVar->GetVecValue(color.Base(),3); pLeak->m_vStartColor = pLeak->m_vEndColor = color; } IMaterialVar* pLeakNoiseVar = pTraceMaterial->FindVar( "$leaknoise", &found ); if (found) { pLeak->m_flNoise = pLeakNoiseVar->GetFloatValue(); } IMaterialVar* pLeakForceVar = pTraceMaterial->FindVar( "$leakforce", &found ); if (found) { float flForce = pLeakForceVar->GetFloatValue(); pLeak->m_flSpeed = flForce; pLeak->m_flSpeedRange = pLeak->m_flNoise * flForce; } pLeak->m_flSpawnRate = pLeakVar->GetFloatValue();; pLeak->m_flParticleLifetime = 10; pLeak->m_flWidthMin = 1; pLeak->m_flWidthMax = 5; pLeak->SetLocalOrigin( tr.endpos ); QAngle angles; VectorAngles( tr.plane.normal, angles ); pLeak->SetLocalAngles( angles ); pLeak->Start(&g_ParticleMgr, NULL); pLeak->m_flStopEmitTime = gpGlobals->curtime+5.0; pLeak->SetNextClientThink(gpGlobals->curtime+20.0); }
void CTextureTransformProxy::OnBind( void *pC_BaseEntity ) { Vector2D center( 0.5, 0.5 ); Vector2D translation( 0, 0 ); VMatrix mat, temp; if (m_pCenterVar) { m_pCenterVar->GetVecValue( center.Base(), 2 ); } MatrixBuildTranslation( mat, -center.x, -center.y, 0.0f ); if (m_pScaleVar) { Vector2D scale; m_pScaleVar->GetVecValue( scale.Base(), 2 ); MatrixBuildScale( temp, scale.x, scale.y, 1.0f ); MatrixMultiply( temp, mat, mat ); } if (m_pRotateVar) { float angle = m_pRotateVar->GetFloatValue( ); MatrixBuildRotateZ( temp, angle ); MatrixMultiply( temp, mat, mat ); } MatrixBuildTranslation( temp, center.x, center.y, 0.0f ); MatrixMultiply( temp, mat, mat ); if (m_pTranslateVar) { m_pTranslateVar->GetVecValue( translation.Base(), 2 ); MatrixBuildTranslation( temp, translation.x, translation.y, 0.0f ); MatrixMultiply( temp, mat, mat ); } m_pResult->SetMatrixValue( mat ); if ( ToolsEnabled() ) { ToolFramework_RecordMaterialParams( GetMaterial() ); } }
void CLessOrEqualProxy::OnBind( void *pC_BaseEntity ) { Assert( m_pSrc1 && m_pSrc2 && m_pLessVar && m_pGreaterVar && m_pResult ); IMaterialVar *pSourceVar; if (m_pSrc1->GetFloatValue() <= m_pSrc2->GetFloatValue()) { pSourceVar = m_pLessVar; } else { pSourceVar = m_pGreaterVar; } int vecSize = 0; MaterialVarType_t resultType = m_pResult->GetType(); if (resultType == MATERIAL_VAR_TYPE_VECTOR) { if (m_ResultVecComp >= 0) resultType = MATERIAL_VAR_TYPE_FLOAT; vecSize = m_pResult->VectorSize(); } else if (resultType == MATERIAL_VAR_TYPE_UNDEFINED) { resultType = pSourceVar->GetType(); if (resultType == MATERIAL_VAR_TYPE_VECTOR) { vecSize = pSourceVar->VectorSize(); } } switch( resultType ) { case MATERIAL_VAR_TYPE_VECTOR: { Vector src; pSourceVar->GetVecValue( src.Base(), vecSize ); m_pResult->SetVecValue( src.Base(), vecSize ); } break; case MATERIAL_VAR_TYPE_FLOAT: SetFloatResult( pSourceVar->GetFloatValue() ); break; case MATERIAL_VAR_TYPE_INT: m_pResult->SetFloatValue( pSourceVar->GetIntValue() ); break; } if ( ToolsEnabled() ) { ToolFramework_RecordMaterialParams( GetMaterial() ); } }
bool CEngineSprite::Init( const char *pName ) { m_hAVIMaterial = AVIMATERIAL_INVALID; m_width = m_height = m_numFrames = 1; const char *pExt = Q_GetFileExtension( pName ); bool bIsAVI = pExt && !Q_stricmp( pExt, "avi" ); if ( bIsAVI ) { m_hAVIMaterial = avi->CreateAVIMaterial( pName, pName, "GAME" ); if ( m_hAVIMaterial == AVIMATERIAL_INVALID ) return false; m_material = avi->GetMaterial( m_hAVIMaterial ); avi->GetFrameSize( m_hAVIMaterial, &m_width, &m_height ); m_numFrames = avi->GetFrameCount( m_hAVIMaterial ); } else { m_material = materials->FindMaterial( pName, TEXTURE_GROUP_CLIENT_EFFECTS ); m_width = m_material->GetMappingWidth(); m_height = m_material->GetMappingHeight(); m_numFrames = (!bIsAVI) ? m_material->GetNumAnimationFrames() : avi->GetFrameCount( m_hAVIMaterial ); } if ( !m_material ) return false; m_material->IncrementReferenceCount(); IMaterialVar *orientationVar = m_material->FindVarFast( "$spriteorientation", &spriteOrientationCache ); m_orientation = orientationVar ? orientationVar->GetIntValue() : C_SpriteRenderer::SPR_VP_PARALLEL_UPRIGHT; IMaterialVar *originVar = m_material->FindVarFast( "$spriteorigin", &spriteOriginCache ); Vector origin, originVarValue; if( !originVar || ( originVar->GetType() != MATERIAL_VAR_TYPE_VECTOR ) ) { origin[0] = -m_width * 0.5f; origin[1] = m_height * 0.5f; } else { originVar->GetVecValue( &originVarValue[0], 3 ); origin[0] = -m_width * originVarValue[0]; origin[1] = m_height * originVarValue[1]; } up = origin[1]; down = origin[1] - m_height; left = origin[0]; right = m_width + origin[0]; return true; }
inline void GetVecParam( int constantVar, float *val ) { if( constantVar == -1 ) return; IMaterialVar* pVar = s_ppParams[constantVar]; Assert( pVar ); if (pVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) pVar->GetVecValue( val, 4 ); else val[0] = val[1] = val[2] = val[3] = pVar->GetFloatValue(); }
void CBaseVSShader::SetVertexShaderConstant( int vertexReg, int constantVar ) { Assert( !IsSnapshotting() ); if ((!s_ppParams) || (constantVar == -1)) return; IMaterialVar* pVertexVar = s_ppParams[constantVar]; Assert( pVertexVar ); float val[4]; if (pVertexVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) pVertexVar->GetVecValue( val, 4 ); else val[0] = val[1] = val[2] = val[3] = pVertexVar->GetFloatValue(); s_pShaderAPI->SetVertexShaderConstant( vertexReg, val ); }
void CBaseOES2Shader::SetTextureScale(int reg, int scaleVar) { float scale[2] = {1.0f, 1.0f}; IMaterialVar *pScaleVar = s_ppParams[scaleVar]; if (pScaleVar) { if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) pScaleVar->GetVecValue(scale, 2); else if (pScaleVar->IsDefined()) scale[0] = scale[1] = pScaleVar->GetFloatValue(); } Vector4D scaleMatrix[2]; scaleMatrix[0].Init(scale[0], 0.0f, 0.0f, 0.0f); scaleMatrix[1].Init(0.0f, scale[1], 0.0f, 0.0f); s_pShaderAPI->Uniform4fv(reg, 2, scaleMatrix[0].Base()); }
void CBaseVSShader::SetPixelShaderConstant_W( int pixelReg, int constantVar, float fWValue ) { Assert( !IsSnapshotting() ); if ((!s_ppParams) || (constantVar == -1)) return; IMaterialVar* pPixelVar = s_ppParams[constantVar]; Assert( pPixelVar ); float val[4]; if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) pPixelVar->GetVecValue( val, 4 ); else val[0] = val[1] = val[2] = val[3] = pPixelVar->GetFloatValue(); val[3]=fWValue; s_pShaderAPI->SetPixelShaderConstant( pixelReg, val ); }
//----------------------------------------------------------------------------- // Sets pixel shader texture transforms //----------------------------------------------------------------------------- void CBaseVSShader::SetPixelShaderTextureTranslation( int pixelReg, int translationVar ) { float offset[2] = {0, 0}; IMaterialVar* pTranslationVar = s_ppParams[translationVar]; if (pTranslationVar) { if (pTranslationVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) pTranslationVar->GetVecValue( offset, 2 ); else offset[0] = offset[1] = pTranslationVar->GetFloatValue(); } Vector4D translation[2]; translation[0].Init( 1.0f, 0.0f, 0.0f, offset[0] ); translation[1].Init( 0.0f, 1.0f, 0.0f, offset[1] ); s_pShaderAPI->SetPixelShaderConstant( pixelReg, translation[0].Base(), 2 ); }
void CBaseVSShader::SetPixelShaderTextureScale( int pixelReg, int scaleVar ) { float scale[2] = {1, 1}; IMaterialVar* pScaleVar = s_ppParams[scaleVar]; if (pScaleVar) { if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) pScaleVar->GetVecValue( scale, 2 ); else if (pScaleVar->IsDefined()) scale[0] = scale[1] = pScaleVar->GetFloatValue(); } Vector4D scaleMatrix[2]; scaleMatrix[0].Init( scale[0], 0.0f, 0.0f, 0.0f ); scaleMatrix[1].Init( 0.0f, scale[1], 0.0f, 0.0f ); s_pShaderAPI->SetPixelShaderConstant( pixelReg, scaleMatrix[0].Base(), 2 ); }
void CMatrixRotateProxy::OnBind( void *pC_BaseEntity ) { VMatrix mat; Vector axis( 0, 0, 1 ); if (m_pAxisVar) { m_pAxisVar->GetVecValue( axis.Base(), 3 ); if (VectorNormalize( axis ) < 1e-3) { axis.Init( 0, 0, 1 ); } } MatrixBuildRotationAboutAxis( mat, axis, m_Angle.GetFloat() ); m_pResult->SetMatrixValue( mat ); if ( ToolsEnabled() ) { ToolFramework_RecordMaterialParams( GetMaterial() ); } }
//----------------------------------------------------------------------------- // Converts a color + alpha into a vector4 //----------------------------------------------------------------------------- void CBaseVSShader::ColorVarsToVector( int colorVar, int alphaVar, Vector4D &color ) { color.Init( 1.0, 1.0, 1.0, 1.0 ); if ( colorVar != -1 ) { IMaterialVar* pColorVar = s_ppParams[colorVar]; if ( pColorVar->GetType() == MATERIAL_VAR_TYPE_VECTOR ) { pColorVar->GetVecValue( color.Base(), 3 ); } else { color[0] = color[1] = color[2] = pColorVar->GetFloatValue(); } } if ( alphaVar != -1 ) { float flAlpha = s_ppParams[alphaVar]->GetFloatValue(); color[3] = clamp( flAlpha, 0.0f, 1.0f ); } }
void CBaseShader::SetFixedFunctionTextureScaledTransform( MaterialMatrixMode_t textureTransform, int transformVar, int scaleVar ) { Assert( !IsSnapshotting() ); float mat[16]; IMaterialVar* pTransformationVar = s_ppParams[transformVar]; if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX)) { Vector2D scale( 1, 1 ); IMaterialVar* pScaleVar = s_ppParams[scaleVar]; if (pScaleVar) { if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) pScaleVar->GetVecValue( scale.Base(), 2 ); else if (pScaleVar->IsDefined()) scale[0] = scale[1] = pScaleVar->GetFloatValue(); } s_pShaderAPI->MatrixMode( textureTransform ); const VMatrix &transformation = pTransformationVar->GetMatrixValue(); // only do the upper 3x3 since this is a 2D matrix mat[0] = transformation[0][0] * scale[0]; mat[1] = transformation[1][0] * scale[0]; mat[2] = transformation[3][0] * scale[0]; mat[4] = transformation[0][1] * scale[1]; mat[5] = transformation[1][1] * scale[1]; mat[6] = transformation[3][1] * scale[1]; mat[8] = transformation[0][3]; mat[9] = transformation[1][3]; mat[10] = transformation[3][3]; // Better set the stuff we don't set with some sort of value! mat[3] = mat[7] = mat[11] = 0; mat[12] = mat[13] = mat[14] = 0; mat[15] = 1; s_pShaderAPI->LoadMatrix( mat ); } else { SetFixedFunctionTextureScale( textureTransform, scaleVar ); } }
void CBaseShader::ComputeModulationColor( float* color ) { Assert( !IsSnapshotting() ); if (!s_ppParams) return; IMaterialVar* pColorVar = s_ppParams[COLOR]; if (pColorVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) { pColorVar->GetVecValue( color, 3 ); } else { color[0] = color[1] = color[2] = pColorVar->GetFloatValue(); } if( !g_pConfig->bShowDiffuse ) { color[0] = color[1] = color[2] = 0.0f; } color[3] = GetAlpha(); }
bool CEngineSprite::Init( const char *pName ) { m_VideoMaterial = NULL; for ( int i = 0; i < kRenderModeCount; ++i ) { m_material[ i ] = NULL; } m_width = m_height = m_numFrames = 1; Assert( g_pVideo != NULL ); if ( g_pVideo != NULL && g_pVideo->LocateVideoSystemForPlayingFile( pName ) != VideoSystem::NONE ) { m_VideoMaterial = g_pVideo->CreateVideoMaterial( pName, pName, "GAME", VideoPlaybackFlags::DEFAULT_MATERIAL_OPTIONS, VideoSystem::DETERMINE_FROM_FILE_EXTENSION, false ); if ( m_VideoMaterial == NULL ) return false; IMaterial *pMaterial = m_VideoMaterial->GetMaterial(); m_VideoMaterial->GetVideoImageSize( &m_width, &m_height ); m_numFrames = m_VideoMaterial->GetFrameCount(); for ( int i = 0; i < kRenderModeCount; ++i ) { m_material[i] = pMaterial; pMaterial->IncrementReferenceCount(); } } else { char pTemp[MAX_PATH]; char pMaterialName[MAX_PATH]; char pMaterialPath[MAX_PATH]; Q_StripExtension( pName, pTemp, sizeof(pTemp) ); Q_strlower( pTemp ); Q_FixSlashes( pTemp, '/' ); // Check to see if this is a UNC-specified material name bool bIsUNC = pTemp[0] == '/' && pTemp[1] == '/' && pTemp[2] != '/'; if ( !bIsUNC ) { Q_strncpy( pMaterialName, "materials/", sizeof(pMaterialName) ); Q_strncat( pMaterialName, pTemp, sizeof(pMaterialName), COPY_ALL_CHARACTERS ); } else { Q_strncpy( pMaterialName, pTemp, sizeof(pMaterialName) ); } Q_strncpy( pMaterialPath, pMaterialName, sizeof(pMaterialPath) ); Q_SetExtension( pMaterialPath, ".vmt", sizeof(pMaterialPath) ); KeyValues *kv = new KeyValues( "vmt" ); if ( !kv->LoadFromFile( g_pFullFileSystem, pMaterialPath, "GAME" ) ) { Warning( "Unable to load sprite material %s!\n", pMaterialPath ); return false; } for ( int i = 0; i < kRenderModeCount; ++i ) { if ( i == kRenderNone || i == kRenderEnvironmental ) { m_material[i] = NULL; continue; } Q_snprintf( pMaterialPath, sizeof(pMaterialPath), "%s_rendermode_%d", pMaterialName, i ); KeyValues *pMaterialKV = kv->MakeCopy(); pMaterialKV->SetInt( "$spriteRenderMode", i ); m_material[i] = g_pMaterialSystem->FindProceduralMaterial( pMaterialPath, TEXTURE_GROUP_CLIENT_EFFECTS, pMaterialKV ); m_material[ i ]->IncrementReferenceCount(); } kv->deleteThis(); m_width = m_material[0]->GetMappingWidth(); m_height = m_material[0]->GetMappingHeight(); m_numFrames = m_material[0]->GetNumAnimationFrames(); } for ( int i = 0; i < kRenderModeCount; ++i ) { if ( i == kRenderNone || i == kRenderEnvironmental ) continue; if ( !m_material[i] ) return false; } IMaterialVar *orientationVar = m_material[0]->FindVarFast( "$spriteorientation", &spriteOrientationCache ); m_orientation = orientationVar ? orientationVar->GetIntValue() : C_SpriteRenderer::SPR_VP_PARALLEL_UPRIGHT; IMaterialVar *originVar = m_material[0]->FindVarFast( "$spriteorigin", &spriteOriginCache ); Vector origin, originVarValue; if( !originVar || ( originVar->GetType() != MATERIAL_VAR_TYPE_VECTOR ) ) { origin[0] = -m_width * 0.5f; origin[1] = m_height * 0.5f; } else { originVar->GetVecValue( &originVarValue[0], 3 ); origin[0] = -m_width * originVarValue[0]; origin[1] = m_height * originVarValue[1]; } up = origin[1]; down = origin[1] - m_height; left = origin[0]; right = m_width + origin[0]; return true; }
bool CEngineSprite::Init( const char *pName ) { m_hAVIMaterial = AVIMATERIAL_INVALID; m_hBIKMaterial = BIKMATERIAL_INVALID; m_width = m_height = m_numFrames = 1; const char *pExt = Q_GetFileExtension( pName ); bool bIsAVI = pExt && !Q_stricmp( pExt, "avi" ); #if !defined( _X360 ) || defined( BINK_ENABLED_FOR_X360 ) bool bIsBIK = pExt && !Q_stricmp( pExt, "bik" ); #endif if ( bIsAVI && IsPC() ) { m_hAVIMaterial = avi->CreateAVIMaterial( pName, pName, "GAME" ); if ( m_hAVIMaterial == AVIMATERIAL_INVALID ) return false; IMaterial *pMaterial = avi->GetMaterial( m_hAVIMaterial ); avi->GetFrameSize( m_hAVIMaterial, &m_width, &m_height ); m_numFrames = avi->GetFrameCount( m_hAVIMaterial ); for ( int i = 0; i < kRenderModeCount; ++i ) { m_material[i] = pMaterial; pMaterial->IncrementReferenceCount(); } } #if !defined( _X360 ) || defined( BINK_ENABLED_FOR_X360 ) else if ( bIsBIK ) { m_hBIKMaterial = bik->CreateMaterial( pName, pName, "GAME" ); if ( m_hBIKMaterial == BIKMATERIAL_INVALID ) return false; IMaterial *pMaterial = bik->GetMaterial( m_hBIKMaterial ); bik->GetFrameSize( m_hBIKMaterial, &m_width, &m_height ); m_numFrames = bik->GetFrameCount( m_hBIKMaterial ); for ( int i = 0; i < kRenderModeCount; ++i ) { m_material[i] = pMaterial; pMaterial->IncrementReferenceCount(); } } #endif else { char pTemp[MAX_PATH]; char pMaterialName[MAX_PATH]; char pMaterialPath[MAX_PATH]; Q_StripExtension( pName, pTemp, sizeof(pTemp) ); Q_strlower( pTemp ); Q_FixSlashes( pTemp, '/' ); // Check to see if this is a UNC-specified material name bool bIsUNC = pTemp[0] == '/' && pTemp[1] == '/' && pTemp[2] != '/'; if ( !bIsUNC ) { Q_strncpy( pMaterialName, "materials/", sizeof(pMaterialName) ); Q_strncat( pMaterialName, pTemp, sizeof(pMaterialName), COPY_ALL_CHARACTERS ); } else { Q_strncpy( pMaterialName, pTemp, sizeof(pMaterialName) ); } Q_strncpy( pMaterialPath, pMaterialName, sizeof(pMaterialPath) ); Q_SetExtension( pMaterialPath, ".vmt", sizeof(pMaterialPath) ); for ( int i = 0; i < kRenderModeCount; ++i ) { m_material[i] = NULL; } KeyValues *kv = new KeyValues( "vmt" ); if ( !kv->LoadFromFile( g_pFullFileSystem, pMaterialPath, "GAME" ) ) { Warning( "Unable to load sprite material %s!\n", pMaterialPath ); return false; } for ( int i = 0; i < kRenderModeCount; ++i ) { if ( i == kRenderNone || i == kRenderEnvironmental ) { continue; } // strip possible materials/ Q_snprintf( pMaterialPath, sizeof(pMaterialPath), "%s_rendermode_%d", pMaterialName + ( bIsUNC ? 0 : 10 ), i ); KeyValues *pMaterialKV = kv->MakeCopy(); pMaterialKV->SetInt( "$spriteRenderMode", i ); m_material[i] = g_pMaterialSystem->FindProceduralMaterial( pMaterialPath, TEXTURE_GROUP_CLIENT_EFFECTS, pMaterialKV ); m_material[i]->IncrementReferenceCount(); } kv->deleteThis(); m_width = m_material[0]->GetMappingWidth(); m_height = m_material[0]->GetMappingHeight(); m_numFrames = m_material[0]->GetNumAnimationFrames(); } for ( int i = 0; i < kRenderModeCount; ++i ) { if ( i == kRenderNone || i == kRenderEnvironmental ) continue; if ( !m_material[i] ) return false; } IMaterialVar *orientationVar = m_material[0]->FindVarFast( "$spriteorientation", &spriteOrientationCache ); m_orientation = orientationVar ? orientationVar->GetIntValue() : C_SpriteRenderer::SPR_VP_PARALLEL_UPRIGHT; IMaterialVar *originVar = m_material[0]->FindVarFast( "$spriteorigin", &spriteOriginCache ); Vector origin, originVarValue; if( !originVar || ( originVar->GetType() != MATERIAL_VAR_TYPE_VECTOR ) ) { origin[0] = -m_width * 0.5f; origin[1] = m_height * 0.5f; } else { originVar->GetVecValue( &originVarValue[0], 3 ); origin[0] = -m_width * originVarValue[0]; origin[1] = m_height * originVarValue[1]; } up = origin[1]; down = origin[1] - m_height; left = origin[0]; right = m_width + origin[0]; return true; }