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.

251 lines
8.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Teeth renderer
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include "mathlib/vmatrix.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. DEFINE_FALLBACK_SHADER( Eyes, Eyes_dx6 )
  13. BEGIN_VS_SHADER( Eyes_dx6,
  14. "Help for Eyes" )
  15. BEGIN_SHADER_PARAMS
  16. SHADER_PARAM( IRIS, SHADER_PARAM_TYPE_TEXTURE, "shadertest/BaseTexture", "iris texture" )
  17. SHADER_PARAM( IRISFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame for the iris texture" )
  18. SHADER_PARAM( GLINT, SHADER_PARAM_TYPE_TEXTURE, "shadertest/BaseTexture", "glint texture" )
  19. SHADER_PARAM( EYEORIGIN, SHADER_PARAM_TYPE_VEC3, "[0 0 0]", "origin for the eyes" )
  20. SHADER_PARAM( EYEUP, SHADER_PARAM_TYPE_VEC3, "[0 0 1]", "up vector for the eyes" )
  21. SHADER_PARAM( IRISU, SHADER_PARAM_TYPE_VEC4, "[0 1 0 0 ]", "U projection vector for the iris" )
  22. SHADER_PARAM( IRISV, SHADER_PARAM_TYPE_VEC4, "[0 0 1 0]", "V projection vector for the iris" )
  23. SHADER_PARAM( GLINTU, SHADER_PARAM_TYPE_VEC4, "[0 1 0 0]", "U projection vector for the glint" )
  24. SHADER_PARAM( GLINTV, SHADER_PARAM_TYPE_VEC4, "[0 0 1 0]", "V projection vector for the glint" )
  25. END_SHADER_PARAMS
  26. SHADER_INIT_PARAMS()
  27. {
  28. // FLASHLIGHTFIXME
  29. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight001" );
  30. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  31. }
  32. SHADER_INIT
  33. {
  34. LoadTexture( FLASHLIGHTTEXTURE );
  35. LoadTexture( BASETEXTURE );
  36. LoadTexture( IRIS );
  37. }
  38. void SetTextureTransform( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  39. MaterialMatrixMode_t textureTransform, int uparam, int vparam )
  40. {
  41. Vector4D u, v;
  42. params[uparam]->GetVecValue( u.Base(), 4 );
  43. params[vparam]->GetVecValue( v.Base(), 4 );
  44. // Need to transform these puppies into camera space
  45. // they are defined in world space
  46. VMatrix mat, invTrans;
  47. pShaderAPI->GetMatrix( MATERIAL_VIEW, mat.m[0] );
  48. mat = mat.Transpose();
  49. // Compute the inverse transpose of the matrix
  50. // NOTE: I only have to invert it here because VMatrix is transposed
  51. // with respect to what gets returned from GetMatrix.
  52. mat.InverseGeneral( invTrans );
  53. invTrans = invTrans.Transpose();
  54. // Transform the u and v planes into view space
  55. Vector4D uview, vview;
  56. uview.AsVector3D() = invTrans.VMul3x3( u.AsVector3D() );
  57. vview.AsVector3D() = invTrans.VMul3x3( v.AsVector3D() );
  58. uview[3] = u[3] - DotProduct( mat.GetTranslation(), uview.AsVector3D() );
  59. vview[3] = v[3] - DotProduct( mat.GetTranslation(), vview.AsVector3D() );
  60. float m[16];
  61. m[0] = uview[0]; m[1] = vview[0]; m[2] = 0.0f; m[3] = 0.0f;
  62. m[4] = uview[1]; m[5] = vview[1]; m[6] = 0.0f; m[7] = 0.0f;
  63. m[8] = uview[2]; m[9] = vview[2]; m[10] = 1.0f; m[11] = 0.0f;
  64. m[12] = uview[3]; m[13] = vview[3]; m[14] = 0.0f; m[15] = 1.0f;
  65. pShaderAPI->MatrixMode( textureTransform );
  66. pShaderAPI->LoadMatrix( m );
  67. }
  68. void DrawFlashlight_Iris( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow )
  69. {
  70. SHADOW_STATE
  71. {
  72. SET_FLAGS2( MATERIAL_VAR2_NEEDS_FIXED_FUNCTION_FLASHLIGHT );
  73. // Alpha blend
  74. pShaderShadow->EnableBlending( true );
  75. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  76. pShaderShadow->EnableDepthWrites( false );
  77. pShaderShadow->EnableAlphaWrites( false );
  78. int flags = SHADER_DRAW_POSITION | SHADER_DRAW_COLOR | SHADER_DRAW_NORMAL;
  79. pShaderShadow->DrawFlags( flags );
  80. FogToBlack();
  81. pShaderShadow->EnableLighting( true );
  82. pShaderShadow->EnableCustomPixelPipe( true );
  83. pShaderShadow->CustomTextureStages( 2 );
  84. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE0,
  85. SHADER_TEXCHANNEL_COLOR,
  86. SHADER_TEXOP_MODULATE,
  87. SHADER_TEXARG_TEXTURE,
  88. SHADER_TEXARG_VERTEXCOLOR );
  89. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE1,
  90. SHADER_TEXCHANNEL_COLOR,
  91. SHADER_TEXOP_MODULATE,
  92. SHADER_TEXARG_TEXTURE, SHADER_TEXARG_PREVIOUSSTAGE );
  93. // alpha stage 0
  94. // get alpha from constant alpha
  95. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE0,
  96. SHADER_TEXCHANNEL_ALPHA,
  97. SHADER_TEXOP_SELECTARG1,
  98. SHADER_TEXARG_CONSTANTCOLOR, SHADER_TEXARG_NONE );
  99. // alpha stage 1
  100. // get alpha from $basetexture
  101. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE1,
  102. SHADER_TEXCHANNEL_ALPHA,
  103. SHADER_TEXOP_MODULATE,
  104. SHADER_TEXARG_TEXTURE, SHADER_TEXARG_PREVIOUSSTAGE );
  105. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  106. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  107. // Shove the view position into texcoord 0 before the texture matrix.
  108. pShaderShadow->TexGen( SHADER_TEXTURE_STAGE0, SHADER_TEXGENPARAM_EYE_LINEAR );
  109. pShaderShadow->EnableTexGen( SHADER_TEXTURE_STAGE0, true );
  110. // iris transform
  111. pShaderShadow->EnableTexGen( SHADER_TEXTURE_STAGE1, true );
  112. pShaderShadow->TexGen( SHADER_TEXTURE_STAGE1, SHADER_TEXGENPARAM_EYE_LINEAR );
  113. }
  114. DYNAMIC_STATE
  115. {
  116. SetFlashlightFixedFunctionTextureTransform( MATERIAL_TEXTURE0 );
  117. // NOTE: This has to come after the loadmatrix since the loadmatrix screws with the
  118. // transform flags!!!!!!
  119. // Specify that we have XYZ texcoords that need to be divided by W before the pixel shader.
  120. // NOTE Tried to divide XY by Z, but doesn't work.
  121. pShaderAPI->SetTextureTransformDimension( SHADER_TEXTURE_STAGE0, 3, true );
  122. BindTexture( SHADER_SAMPLER0, FLASHLIGHTTEXTURE, FLASHLIGHTTEXTUREFRAME );
  123. BindTexture( SHADER_SAMPLER1, IRIS, IRISFRAME );
  124. SetTextureTransform( params, pShaderAPI, MATERIAL_TEXTURE1, IRISU, IRISV );
  125. }
  126. Draw();
  127. }
  128. void DrawFlashlight( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow )
  129. {
  130. // whites
  131. DrawFlashlight_dx70( params, pShaderAPI, pShaderShadow,
  132. FLASHLIGHTTEXTURE, FLASHLIGHTTEXTUREFRAME, true );
  133. // iris
  134. DrawFlashlight_Iris( params, pShaderAPI, pShaderShadow );
  135. }
  136. void DrawUsingSoftwareLighting( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow )
  137. {
  138. // whites
  139. SHADOW_STATE
  140. {
  141. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  142. pShaderShadow->OverbrightValue( SHADER_TEXTURE_STAGE0, OVERBRIGHT );
  143. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_COLOR | SHADER_DRAW_TEXCOORD0 );
  144. FogToFogColor();
  145. }
  146. DYNAMIC_STATE
  147. {
  148. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  149. }
  150. Draw();
  151. // iris
  152. SHADOW_STATE
  153. {
  154. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  155. pShaderShadow->OverbrightValue( SHADER_TEXTURE_STAGE0, OVERBRIGHT );
  156. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_COLOR );
  157. pShaderShadow->EnableTexGen( SHADER_TEXTURE_STAGE0, true );
  158. pShaderShadow->TexGen( SHADER_TEXTURE_STAGE0, SHADER_TEXGENPARAM_EYE_LINEAR );
  159. pShaderShadow->EnableBlending( true );
  160. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  161. FogToFogColor();
  162. }
  163. DYNAMIC_STATE
  164. {
  165. BindTexture( SHADER_SAMPLER0, IRIS, IRISFRAME );
  166. SetTextureTransform( params, pShaderAPI, MATERIAL_TEXTURE0, IRISU, IRISV );
  167. }
  168. Draw();
  169. // Glint
  170. SHADOW_STATE
  171. {
  172. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  173. pShaderShadow->EnableTexture( SHADER_SAMPLER1, false );
  174. pShaderShadow->OverbrightValue( SHADER_TEXTURE_STAGE0, 1.0f );
  175. pShaderShadow->OverbrightValue( SHADER_TEXTURE_STAGE1, 1.0f );
  176. pShaderShadow->EnableConstantColor( true );
  177. pShaderShadow->EnableDepthWrites( false );
  178. pShaderShadow->EnableBlending( true );
  179. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  180. pShaderShadow->EnableTexGen( SHADER_TEXTURE_STAGE0, true );
  181. pShaderShadow->TexGen( SHADER_TEXTURE_STAGE0, SHADER_TEXGENPARAM_EYE_LINEAR );
  182. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION );
  183. FogToBlack();
  184. }
  185. DYNAMIC_STATE
  186. {
  187. BindTexture( SHADER_SAMPLER0, GLINT );
  188. SetTextureTransform( params, pShaderAPI, MATERIAL_TEXTURE0, GLINTU, GLINTV );
  189. }
  190. Draw( );
  191. }
  192. SHADER_DRAW
  193. {
  194. SHADOW_STATE
  195. {
  196. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  197. }
  198. bool hasFlashlight = UsingFlashlight( params );
  199. if( hasFlashlight )
  200. {
  201. DrawFlashlight( params, pShaderAPI, pShaderShadow );
  202. }
  203. else
  204. {
  205. DrawUsingSoftwareLighting( params, pShaderAPI, pShaderShadow );
  206. }
  207. }
  208. END_SHADER