Team Fortress 2 Source Code as on 22/4/2020
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.

112 lines
3.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #include "shaderlib/cshader.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. BEGIN_SHADER( Rift_DX6,
  12. "Help for Rift_DX6" )
  13. BEGIN_SHADER_PARAMS
  14. SHADER_PARAM( TEXTURE2, SHADER_PARAM_TYPE_TEXTURE, "shadertest/BaseTexture", "second texture" )
  15. SHADER_PARAM( FRAME2, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $texture2" )
  16. SHADER_PARAM( TEXTURE2TRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$texture2 texcoord transform" )
  17. END_SHADER_PARAMS
  18. SHADER_INIT
  19. {
  20. if (params[BASETEXTURE]->IsDefined())
  21. {
  22. LoadTexture( BASETEXTURE );
  23. }
  24. if (params[TEXTURE2]->IsDefined())
  25. {
  26. LoadTexture( TEXTURE2 );
  27. }
  28. }
  29. SHADER_DRAW
  30. {
  31. SHADOW_STATE
  32. {
  33. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  34. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  35. SetModulationShadowState();
  36. // Either we've got a constant modulation
  37. bool isTranslucent = IsAlphaModulating();
  38. // Or we've got a texture alpha on either texture
  39. isTranslucent = isTranslucent || TextureIsTranslucent( BASETEXTURE, true ) ||
  40. TextureIsTranslucent( TEXTURE2, true );
  41. if ( isTranslucent )
  42. {
  43. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  44. {
  45. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  46. }
  47. else
  48. {
  49. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  50. }
  51. }
  52. else
  53. {
  54. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  55. {
  56. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  57. }
  58. }
  59. pShaderShadow->EnableTexGen( SHADER_TEXTURE_STAGE0, true );
  60. pShaderShadow->TexGen( SHADER_TEXTURE_STAGE0, SHADER_TEXGENPARAM_EYE_LINEAR );
  61. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD1 );
  62. DefaultFog();
  63. }
  64. DYNAMIC_STATE
  65. {
  66. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  67. // 1) Take a coordinate in camera space
  68. // 2) Flip Y by multiplying by -1
  69. // 3) Transform from [-w,w] to [0,2*w]
  70. // 4) Transform from [0,2*w] to [0,w]
  71. // We'll end up dividing by w in the pixel shader to get to [0,1]
  72. VMatrix matProjection, matYFlip, matHalf, matOffset, matBaseTransform;
  73. s_pShaderAPI->GetMatrix( MATERIAL_PROJECTION, matProjection.m[0] );
  74. MatrixTranspose( matProjection, matProjection );
  75. MatrixBuildScale( matYFlip, 1.0f, -1.0f, 1.0f );
  76. MatrixBuildTranslation( matOffset, 1.0f, 1.0f, 0.0f );
  77. MatrixBuildScale( matHalf, 0.5f, 0.5f, 1.0f );
  78. MatrixMultiply( matYFlip, matProjection, matBaseTransform );
  79. MatrixMultiply( matOffset, matBaseTransform, matBaseTransform );
  80. MatrixMultiply( matHalf, matBaseTransform, matBaseTransform );
  81. // tranpose before going into the shaderapi. . . suck
  82. MatrixTranspose( matBaseTransform, matBaseTransform );
  83. s_pShaderAPI->MatrixMode( MATERIAL_TEXTURE0 );
  84. s_pShaderAPI->LoadMatrix( &matBaseTransform[0][0] );
  85. // NOTE: This *must* be set after LoadMatrix since LoadMatrix slams the texture dimension
  86. pShaderAPI->SetTextureTransformDimension( SHADER_TEXTURE_STAGE0, 3, true );
  87. BindTexture( SHADER_SAMPLER1, TEXTURE2, FRAME2 );
  88. SetFixedFunctionTextureTransform( MATERIAL_TEXTURE1, TEXTURE2TRANSFORM );
  89. SetModulationDynamicState();
  90. }
  91. Draw();
  92. }
  93. END_SHADER