Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "mathlib/vmatrix.h"
  9. #include "functionproxy.h"
  10. #include "materialsystem/imaterialvar.h"
  11. #include <keyvalues.h>
  12. #include "materialsystem/imaterial.h"
  13. #include "imaterialproxydict.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. class C_BaseEntity;
  17. //-----------------------------------------------------------------------------
  18. // Texture transform proxy
  19. //-----------------------------------------------------------------------------
  20. class CTextureTransformProxy : public CResultProxy
  21. {
  22. public:
  23. bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
  24. void OnBind( void *pC_BaseEntity );
  25. private:
  26. IMaterialVar *m_pCenterVar;
  27. IMaterialVar *m_pScaleVar;
  28. IMaterialVar *m_pRotateVar;
  29. IMaterialVar *m_pTranslateVar;
  30. };
  31. bool CTextureTransformProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  32. {
  33. // All are optional...
  34. m_pCenterVar = NULL;
  35. m_pScaleVar = NULL;
  36. m_pRotateVar = NULL;
  37. m_pTranslateVar = NULL;
  38. bool bFoundVar;
  39. char const* pVarName = pKeyValues->GetString( "centerVar" );
  40. if( pVarName && pVarName[0] )
  41. {
  42. m_pCenterVar = pMaterial->FindVar( pVarName, &bFoundVar, false );
  43. }
  44. pVarName = pKeyValues->GetString( "scaleVar" );
  45. if( pVarName && pVarName[0] )
  46. {
  47. m_pScaleVar = pMaterial->FindVar( pVarName, &bFoundVar, false );
  48. }
  49. pVarName = pKeyValues->GetString( "rotateVar" );
  50. if( pVarName && pVarName[0] )
  51. {
  52. m_pRotateVar = pMaterial->FindVar( pVarName, &bFoundVar, false );
  53. }
  54. pVarName = pKeyValues->GetString( "translateVar" );
  55. if( pVarName && pVarName[0] )
  56. {
  57. m_pTranslateVar = pMaterial->FindVar( pVarName, &bFoundVar, false );
  58. }
  59. return CResultProxy::Init( pMaterial, pKeyValues );
  60. }
  61. void CTextureTransformProxy::OnBind( void *pC_BaseEntity )
  62. {
  63. Vector2D center( 0.5, 0.5 );
  64. Vector2D translation( 0, 0 );
  65. VMatrix mat, temp;
  66. if (m_pCenterVar)
  67. {
  68. m_pCenterVar->GetVecValue( center.Base(), 2 );
  69. }
  70. MatrixBuildTranslation( mat, -center.x, -center.y, 0.0f );
  71. if (m_pScaleVar)
  72. {
  73. Vector2D scale;
  74. m_pScaleVar->GetVecValue( scale.Base(), 2 );
  75. MatrixBuildScale( temp, scale.x, scale.y, 1.0f );
  76. MatrixMultiply( temp, mat, mat );
  77. }
  78. if (m_pRotateVar)
  79. {
  80. float angle = m_pRotateVar->GetFloatValue( );
  81. MatrixBuildRotateZ( temp, angle );
  82. MatrixMultiply( temp, mat, mat );
  83. }
  84. MatrixBuildTranslation( temp, center.x, center.y, 0.0f );
  85. MatrixMultiply( temp, mat, mat );
  86. if (m_pTranslateVar)
  87. {
  88. m_pTranslateVar->GetVecValue( translation.Base(), 2 );
  89. MatrixBuildTranslation( temp, translation.x, translation.y, 0.0f );
  90. MatrixMultiply( temp, mat, mat );
  91. }
  92. m_pResult->SetMatrixValue( mat );
  93. }
  94. EXPOSE_MATERIAL_PROXY( CTextureTransformProxy, TextureTransform );
  95. //-----------------------------------------------------------------------------
  96. // Rotation proxy
  97. //-----------------------------------------------------------------------------
  98. class CMatrixRotateProxy : public CResultProxy
  99. {
  100. public:
  101. bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
  102. void OnBind( void *pC_BaseEntity );
  103. private:
  104. CFloatInput m_Angle;
  105. IMaterialVar *m_pAxisVar;
  106. };
  107. bool CMatrixRotateProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  108. {
  109. // All are optional...
  110. m_pAxisVar = NULL;
  111. bool bFoundVar;
  112. char const* pVarName = pKeyValues->GetString( "axisVar" );
  113. if( pVarName && pVarName[0] )
  114. {
  115. m_pAxisVar = pMaterial->FindVar( pVarName, &bFoundVar, false );
  116. }
  117. if (!m_Angle.Init( pMaterial, pKeyValues, "angle", 0 ))
  118. return false;
  119. return CResultProxy::Init( pMaterial, pKeyValues );
  120. }
  121. void CMatrixRotateProxy::OnBind( void *pC_BaseEntity )
  122. {
  123. VMatrix mat;
  124. Vector axis( 0, 0, 1 );
  125. if (m_pAxisVar)
  126. {
  127. m_pAxisVar->GetVecValue( axis.Base(), 3 );
  128. if (VectorNormalize( axis ) < 1e-3)
  129. {
  130. axis.Init( 0, 0, 1 );
  131. }
  132. }
  133. MatrixBuildRotationAboutAxis( mat, axis, m_Angle.GetFloat() );
  134. m_pResult->SetMatrixValue( mat );
  135. }
  136. EXPOSE_MATERIAL_PROXY( CMatrixRotateProxy, MatrixRotate );