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.

289 lines
8.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. // Implementation of the sprite shader
  8. //=============================================================================//
  9. #include "shaderlib/cshader.h"
  10. #include <string.h>
  11. #include "const.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. // WARNING! Change these in engine/SpriteGn.h if you change them here!
  15. #define SPR_VP_PARALLEL_UPRIGHT 0
  16. #define SPR_FACING_UPRIGHT 1
  17. #define SPR_VP_PARALLEL 2
  18. #define SPR_ORIENTED 3
  19. #define SPR_VP_PARALLEL_ORIENTED 4
  20. DEFINE_FALLBACK_SHADER( Sprite, Sprite_DX6 )
  21. BEGIN_SHADER( Sprite_DX6,
  22. "Help for Sprite_DX6" )
  23. BEGIN_SHADER_PARAMS
  24. SHADER_PARAM( SPRITEORIGIN, SHADER_PARAM_TYPE_VEC3, "[0 0 0]", "sprite origin" )
  25. SHADER_PARAM( SPRITEORIENTATION, SHADER_PARAM_TYPE_INTEGER, "0", "sprite orientation" )
  26. SHADER_PARAM( SPRITERENDERMODE, SHADER_PARAM_TYPE_INTEGER, "0", "sprite rendermode" )
  27. SHADER_PARAM( IGNOREVERTEXCOLORS, SHADER_PARAM_TYPE_BOOL, "1", "ignore vertex colors" )
  28. END_SHADER_PARAMS
  29. SHADER_INIT_PARAMS()
  30. {
  31. // FIXME: This can share code with sprite.cpp
  32. // FIXME: Not sure if this is the best solution, but it's a very]
  33. // easy one. When graphics aren't enabled, we oftentimes need to get
  34. // at the parameters of a shader. Therefore, we must set the default
  35. // values in a separate phase from when we load resources.
  36. if (!params[ALPHA]->IsDefined())
  37. params[ ALPHA ]->SetFloatValue( 1.0f );
  38. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  39. SET_FLAGS( MATERIAL_VAR_VERTEXCOLOR );
  40. SET_FLAGS( MATERIAL_VAR_VERTEXALPHA );
  41. // translate from a string orientation to an enumeration
  42. if (params[SPRITEORIENTATION]->IsDefined())
  43. {
  44. const char *orientationString = params[SPRITEORIENTATION]->GetStringValue();
  45. if( stricmp( orientationString, "parallel_upright" ) == 0 )
  46. {
  47. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL_UPRIGHT );
  48. }
  49. else if( stricmp( orientationString, "facing_upright" ) == 0 )
  50. {
  51. params[SPRITEORIENTATION]->SetIntValue( SPR_FACING_UPRIGHT );
  52. }
  53. else if( stricmp( orientationString, "vp_parallel" ) == 0 )
  54. {
  55. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL );
  56. }
  57. else if( stricmp( orientationString, "oriented" ) == 0 )
  58. {
  59. params[SPRITEORIENTATION]->SetIntValue( SPR_ORIENTED );
  60. }
  61. else if( stricmp( orientationString, "vp_parallel_oriented" ) == 0 )
  62. {
  63. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL_ORIENTED );
  64. }
  65. else
  66. {
  67. Warning( "error with $spriteOrientation\n" );
  68. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL_UPRIGHT );
  69. }
  70. }
  71. else
  72. {
  73. // default case
  74. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL_UPRIGHT );
  75. }
  76. }
  77. SHADER_INIT
  78. {
  79. LoadTexture( BASETEXTURE );
  80. }
  81. SHADER_DRAW
  82. {
  83. SHADOW_STATE
  84. {
  85. pShaderShadow->EnableCulling( false );
  86. }
  87. switch( params[SPRITERENDERMODE]->GetIntValue() )
  88. {
  89. case kRenderNormal:
  90. SHADOW_STATE
  91. {
  92. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  93. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 );
  94. FogToFogColor();
  95. }
  96. DYNAMIC_STATE
  97. {
  98. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  99. }
  100. Draw();
  101. break;
  102. case kRenderTransColor:
  103. SHADOW_STATE
  104. {
  105. pShaderShadow->EnableDepthWrites( false );
  106. pShaderShadow->EnableBlending( true );
  107. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  108. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  109. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_COLOR );
  110. FogToFogColor();
  111. }
  112. DYNAMIC_STATE
  113. {
  114. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  115. }
  116. Draw();
  117. break;
  118. case kRenderTransTexture:
  119. SHADOW_STATE
  120. {
  121. pShaderShadow->EnableDepthWrites( false );
  122. pShaderShadow->EnableBlending( true );
  123. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  124. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  125. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_COLOR );
  126. FogToFogColor();
  127. }
  128. DYNAMIC_STATE
  129. {
  130. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  131. }
  132. Draw();
  133. break;
  134. case kRenderGlow:
  135. case kRenderWorldGlow:
  136. SHADOW_STATE
  137. {
  138. pShaderShadow->EnableDepthWrites( false );
  139. pShaderShadow->EnableDepthTest( false );
  140. pShaderShadow->EnableBlending( true );
  141. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  142. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  143. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_COLOR );
  144. FogToBlack();
  145. }
  146. DYNAMIC_STATE
  147. {
  148. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  149. }
  150. Draw();
  151. break;
  152. case kRenderTransAlpha:
  153. SHADOW_STATE
  154. {
  155. pShaderShadow->EnableDepthWrites( false );
  156. pShaderShadow->EnableBlending( true );
  157. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  158. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  159. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_COLOR );
  160. FogToFogColor();
  161. }
  162. DYNAMIC_STATE
  163. {
  164. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  165. }
  166. Draw();
  167. break;
  168. case kRenderTransAlphaAdd:
  169. SHADOW_STATE
  170. {
  171. pShaderShadow->EnableDepthWrites( false );
  172. pShaderShadow->EnableBlending( true );
  173. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  174. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  175. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_COLOR );
  176. FogToFogColor();
  177. }
  178. DYNAMIC_STATE
  179. {
  180. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  181. }
  182. Draw();
  183. SHADOW_STATE
  184. {
  185. pShaderShadow->EnableDepthWrites( false );
  186. pShaderShadow->EnableBlending( true );
  187. pShaderShadow->BlendFunc( SHADER_BLEND_ONE_MINUS_SRC_ALPHA, SHADER_BLEND_ONE );
  188. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  189. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_COLOR );
  190. FogToBlack();
  191. }
  192. DYNAMIC_STATE
  193. {
  194. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  195. }
  196. Draw();
  197. break;
  198. case kRenderTransAdd:
  199. SHADOW_STATE
  200. {
  201. if( params[ IGNOREVERTEXCOLORS ]->GetIntValue() )
  202. {
  203. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 );
  204. }
  205. else
  206. {
  207. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_COLOR );
  208. }
  209. pShaderShadow->EnableConstantColor( true );
  210. pShaderShadow->EnableDepthWrites( false );
  211. pShaderShadow->EnableBlending( true );
  212. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  213. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  214. FogToBlack();
  215. }
  216. DYNAMIC_STATE
  217. {
  218. SetColorState( COLOR, ALPHA );
  219. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  220. }
  221. Draw();
  222. break;
  223. case kRenderTransAddFrameBlend:
  224. {
  225. float flFrame = params[FRAME]->GetFloatValue();
  226. float flFade = params[ALPHA]->GetFloatValue();
  227. SHADOW_STATE
  228. {
  229. if( params[ IGNOREVERTEXCOLORS ]->GetIntValue() )
  230. {
  231. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 );
  232. }
  233. else
  234. {
  235. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_COLOR );
  236. }
  237. pShaderShadow->EnableConstantColor( true );
  238. pShaderShadow->EnableDepthWrites( false );
  239. pShaderShadow->EnableBlending( true );
  240. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  241. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  242. FogToBlack();
  243. }
  244. DYNAMIC_STATE
  245. {
  246. float frameBlendAlpha = 1.0f - ( flFrame - ( int )flFrame );
  247. pShaderAPI->Color3f( flFade * frameBlendAlpha, flFade * frameBlendAlpha, flFade * frameBlendAlpha );
  248. ITexture *pTexture = params[BASETEXTURE]->GetTextureValue();
  249. BindTexture( SHADER_SAMPLER0, pTexture, ( int )flFrame );
  250. }
  251. Draw();
  252. SHADOW_STATE
  253. {
  254. FogToBlack();
  255. }
  256. DYNAMIC_STATE
  257. {
  258. float frameBlendAlpha = ( flFrame - ( int )flFrame );
  259. pShaderAPI->Color3f( flFade * frameBlendAlpha, flFade * frameBlendAlpha, flFade * frameBlendAlpha );
  260. ITexture *pTexture = params[BASETEXTURE]->GetTextureValue();
  261. int numAnimationFrames = pTexture->GetNumAnimationFrames();
  262. BindTexture( SHADER_SAMPLER0, pTexture, ( ( int )flFrame + 1 ) % numAnimationFrames );
  263. }
  264. Draw();
  265. }
  266. break;
  267. default:
  268. ShaderWarning( "shader Sprite: Unknown sprite render mode\n" );
  269. break;
  270. }
  271. }
  272. END_SHADER