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.

194 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Lightmap only shader
  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. DEFINE_FALLBACK_SHADER( ShatteredGlass, ShatteredGlass_DX7 )
  12. BEGIN_SHADER( ShatteredGlass_DX7,
  13. "Help for ShatteredGlass_DX7" )
  14. BEGIN_SHADER_PARAMS
  15. SHADER_PARAM( DETAIL, SHADER_PARAM_TYPE_TEXTURE, "shadertest/Detail", "detail" )
  16. SHADER_PARAM( DETAILSCALE, SHADER_PARAM_TYPE_FLOAT, "1", "detail scale" )
  17. SHADER_PARAM( ENVMAP, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_env", "envmap" )
  18. SHADER_PARAM( ENVMAPFRAME, SHADER_PARAM_TYPE_INTEGER, "", "" )
  19. SHADER_PARAM( ENVMAPTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "envmap tint" )
  20. SHADER_PARAM( ENVMAPMASKSCALE, SHADER_PARAM_TYPE_FLOAT, "1", "envmap mask scale" )
  21. SHADER_PARAM( LIGHTMAPTINT, SHADER_PARAM_TYPE_FLOAT, "1.0", "lightmap tint" )
  22. END_SHADER_PARAMS
  23. SHADER_INIT_PARAMS()
  24. {
  25. if( !params[ENVMAPMASKSCALE]->IsDefined() )
  26. {
  27. params[ENVMAPMASKSCALE]->SetFloatValue( 1.0f );
  28. }
  29. if( !params[DETAILSCALE]->IsDefined() )
  30. {
  31. params[DETAILSCALE]->SetFloatValue( 1.0f );
  32. }
  33. if( !params[LIGHTMAPTINT]->IsDefined() )
  34. {
  35. params[LIGHTMAPTINT]->SetFloatValue( 1.0f );
  36. }
  37. if( !params[ENVMAPTINT]->IsDefined() )
  38. {
  39. params[ENVMAPTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  40. }
  41. // No texture means no self-illum or env mask in base alpha
  42. if ( !params[BASETEXTURE]->IsDefined() )
  43. {
  44. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  45. }
  46. // If in decal mode, no debug override...
  47. if (IS_FLAG_SET(MATERIAL_VAR_DECAL))
  48. {
  49. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  50. }
  51. }
  52. SHADER_INIT
  53. {
  54. if (params[BASETEXTURE]->IsDefined())
  55. {
  56. LoadTexture( BASETEXTURE );
  57. if (!params[BASETEXTURE]->GetTextureValue()->IsTranslucent())
  58. {
  59. if (IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK))
  60. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  61. }
  62. }
  63. if (params[DETAIL]->IsDefined())
  64. {
  65. LoadTexture( DETAIL );
  66. }
  67. // Don't alpha test if the alpha channel is used for other purposes
  68. if (IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) )
  69. CLEAR_FLAGS( MATERIAL_VAR_ALPHATEST );
  70. if (params[ENVMAP]->IsDefined())
  71. {
  72. if( !IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE) )
  73. LoadCubeMap( ENVMAP );
  74. else
  75. LoadTexture( ENVMAP );
  76. if( !g_pHardwareConfig->SupportsCubeMaps() )
  77. {
  78. SET_FLAGS( MATERIAL_VAR_ENVMAPSPHERE );
  79. }
  80. }
  81. }
  82. SHADER_FALLBACK
  83. {
  84. // The only thing we can't do here is masked env-mapped
  85. if (g_pHardwareConfig->GetSamplerCount() < 2)
  86. {
  87. return "LightmappedTwoTexture_DX5";
  88. }
  89. return 0;
  90. }
  91. // Can't use the normal detail pass since we're modulating by the detail texture
  92. // and because we're using the secondary texture coordinate
  93. void DrawBaseTimesDetail( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow )
  94. {
  95. // Base
  96. SHADOW_STATE
  97. {
  98. // alpha test
  99. pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  100. // Alpha blending, enable alpha blending if the detail texture is translucent
  101. bool detailIsTranslucent = TextureIsTranslucent( DETAIL, false );
  102. if ( detailIsTranslucent )
  103. {
  104. if ( IS_FLAG_SET( MATERIAL_VAR_ADDITIVE ) )
  105. {
  106. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  107. }
  108. else
  109. {
  110. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  111. }
  112. }
  113. else
  114. {
  115. SetDefaultBlendingShadowState( BASETEXTURE, true );
  116. }
  117. // independently configure alpha and color
  118. pShaderShadow->EnableAlphaPipe( true );
  119. // Here's the color states (NOTE: SHADER_DRAW_COLOR == use Vertex Color)
  120. pShaderShadow->EnableConstantColor( IsColorModulating() );
  121. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  122. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  123. // Here's the alpha states
  124. pShaderShadow->EnableConstantAlpha( IsAlphaModulating() );
  125. pShaderShadow->EnableVertexAlpha( IS_FLAG_SET(MATERIAL_VAR_VERTEXALPHA) );
  126. pShaderShadow->EnableTextureAlpha( SHADER_TEXTURE_STAGE0, TextureIsTranslucent(BASETEXTURE, true) );
  127. pShaderShadow->EnableTextureAlpha( SHADER_TEXTURE_STAGE1, detailIsTranslucent );
  128. int flags = SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_SECONDARY_TEXCOORD1;
  129. if (IS_FLAG_SET(MATERIAL_VAR_VERTEXCOLOR))
  130. flags |= SHADER_DRAW_COLOR;
  131. pShaderShadow->DrawFlags( flags );
  132. DefaultFog();
  133. }
  134. DYNAMIC_STATE
  135. {
  136. SetFixedFunctionTextureTransform( MATERIAL_TEXTURE0, BASETEXTURETRANSFORM );
  137. SetFixedFunctionTextureScale( MATERIAL_TEXTURE1, DETAILSCALE );
  138. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  139. BindTexture( SHADER_SAMPLER1, DETAIL );
  140. SetModulationDynamicState();
  141. }
  142. Draw();
  143. SHADOW_STATE
  144. {
  145. pShaderShadow->EnableAlphaPipe( false );
  146. }
  147. }
  148. SHADER_DRAW
  149. {
  150. // Pass 1 : Base + env
  151. DrawBaseTimesDetail( params, pShaderAPI, pShaderShadow );
  152. // Pass 2 : * lightmap
  153. FixedFunctionMultiplyByLightmapPass( BASETEXTURE, FRAME, BASETEXTURETRANSFORM, params[LIGHTMAPTINT]->GetFloatValue() );
  154. // Pass 3 : + env
  155. bool envDefined = params[ENVMAP]->IsTexture();
  156. if (envDefined)
  157. {
  158. FixedFunctionAdditiveMaskedEnvmapPass(
  159. ENVMAP, BASETEXTURE, BASETEXTURE,
  160. ENVMAPFRAME, FRAME, FRAME,
  161. BASETEXTURETRANSFORM, ENVMAPMASKSCALE, ENVMAPTINT );
  162. }
  163. }
  164. END_SHADER