illwieckz

darkplaces glsl (from shader_glsl.h)

Feb 7th, 2019
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader
  2. // written by Forest 'LordHavoc' Hale
  3. // shadowmapping enhancements by Lee 'eihrul' Salzman
  4.  
  5. #if defined(USESKELETAL) || defined(USEOCCLUDE)
  6. #  ifdef GL_ARB_uniform_buffer_object
  7. #    extension GL_ARB_uniform_buffer_object : enable
  8. #  endif
  9. #endif
  10.  
  11. #ifdef USESHADOWMAP2D
  12. # ifdef GL_EXT_gpu_shader4
  13. #   extension GL_EXT_gpu_shader4 : enable
  14. # endif
  15. # ifdef GL_ARB_texture_gather
  16. #   extension GL_ARB_texture_gather : enable
  17. # else
  18. #   ifdef GL_AMD_texture_texture4
  19. #     extension GL_AMD_texture_texture4 : enable
  20. #   endif
  21. # endif
  22. #endif
  23.  
  24. #ifdef USECELSHADING
  25. # define SHADEDIFFUSE myhalf diffuse = cast_myhalf(min(max(float(dot(surfacenormal, lightnormal)) * 2.0, 0.0), 1.0));
  26. # ifdef USEEXACTSPECULARMATH
  27. #  define SHADESPECULAR(specpow) myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), eyenormal))*-1.0, 0.0)), 1.0 + specpow);specular = max(0.0, specular * 10.0 - 9.0);
  28. # else
  29. #  define SHADESPECULAR(specpow) myhalf3 specularnormal = normalize(lightnormal + eyenormal);myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + specpow);specular = max(0.0, specular * 10.0 - 9.0);
  30. # endif
  31. #else
  32. # define SHADEDIFFUSE myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));
  33. # ifdef USEEXACTSPECULARMATH
  34. #  define SHADESPECULAR(specpow) myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), eyenormal))*-1.0, 0.0)), 1.0 + specpow);
  35. # else
  36. #  define SHADESPECULAR(specpow) myhalf3 specularnormal = normalize(lightnormal + eyenormal);myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + specpow);
  37. # endif
  38. #endif
  39.  
  40. #if (defined(GLSL120) || defined(GLSL130) || defined(GLSL140) || defined(GLES)) && defined(VERTEX_SHADER)
  41. invariant gl_Position; // fix for lighting polygons not matching base surface
  42. # endif
  43. #if defined(GLSL130) || defined(GLSL140)
  44. precision highp float;
  45. # ifdef VERTEX_SHADER
  46. #  define dp_varying out
  47. #  define dp_attribute in
  48. # endif
  49. # ifdef FRAGMENT_SHADER
  50. out vec4 dp_FragColor;
  51. #  define dp_varying in
  52. #  define dp_attribute in
  53. # endif
  54. # define dp_offsetmapping_dFdx dFdx
  55. # define dp_offsetmapping_dFdy dFdy
  56. # define dp_textureGrad textureGrad
  57. # define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d))
  58. # define dp_texture2D texture
  59. # define dp_texture3D texture
  60. # define dp_textureCube texture
  61. # define dp_shadow2D(a,b) float(texture(a,b))
  62. #else
  63. # ifdef FRAGMENT_SHADER
  64. #  define dp_FragColor gl_FragColor
  65. # endif
  66. # define dp_varying varying
  67. # define dp_attribute attribute
  68. # define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0)
  69. # define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0)
  70. # define dp_textureGrad(a,b,c,d) texture2D(a,b)
  71. # define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d))
  72. # define dp_texture2D texture2D
  73. # define dp_texture3D texture3D
  74. # define dp_textureCube textureCube
  75. # define dp_shadow2D(a,b) float(shadow2D(a,b))
  76. #endif
  77.  
  78. // GL ES and GLSL130 shaders use precision modifiers, standard GL does not
  79. // in GLSL130 we don't use them though because of syntax differences (can't use precision with inout)
  80. #ifndef GL_ES
  81. #define lowp
  82. #define mediump
  83. #define highp
  84. #endif
  85.  
  86. #ifdef USEDEPTHRGB
  87.     // for 565 RGB we'd need to use different multipliers
  88. #define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0))
  89. #define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0)))
  90. #endif
  91.  
  92. #ifdef VERTEX_SHADER
  93. dp_attribute vec4 Attrib_Position;  // vertex
  94. dp_attribute vec4 Attrib_Color;     // color
  95. dp_attribute vec4 Attrib_TexCoord0; // material texcoords
  96. dp_attribute vec3 Attrib_TexCoord1; // svector
  97. dp_attribute vec3 Attrib_TexCoord2; // tvector
  98. dp_attribute vec3 Attrib_TexCoord3; // normal
  99. dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords
  100. #ifdef USESKELETAL
  101. //uniform mat4 Skeletal_Transform[128];
  102. // this is used with glBindBufferRange to bind a uniform block to the name
  103. // Skeletal_Transform12_UniformBlock, the Skeletal_Transform12 variable is
  104. // directly accessible without a namespace.
  105. // explanation: http://www.opengl.org/wiki/Interface_Block_%28GLSL%29#Syntax
  106. uniform Skeletal_Transform12_UniformBlock
  107. {
  108.     vec4 Skeletal_Transform12[768];
  109. };
  110. dp_attribute vec4 Attrib_SkeletalIndex;
  111. dp_attribute vec4 Attrib_SkeletalWeight;
  112. #endif
  113. #endif
  114. dp_varying mediump vec4 VertexColor;
  115.  
  116. #if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE)
  117. # define USEFOG
  118. #endif
  119. #if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP)
  120. # define USELIGHTMAP
  121. #endif
  122. #if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG)
  123. # define USEEYEVECTOR
  124. #endif
  125.  
  126. //#ifdef __GLSL_CG_DATA_TYPES
  127. //# define myhalf half
  128. //# define myhalf2 half2
  129. //# define myhalf3 half3
  130. //# define myhalf4 half4
  131. //# define cast_myhalf half
  132. //# define cast_myhalf2 half2
  133. //# define cast_myhalf3 half3
  134. //# define cast_myhalf4 half4
  135. //#else
  136. # define myhalf mediump float
  137. # define myhalf2 mediump vec2
  138. # define myhalf3 mediump vec3
  139. # define myhalf4 mediump vec4
  140. # define cast_myhalf float
  141. # define cast_myhalf2 vec2
  142. # define cast_myhalf3 vec3
  143. # define cast_myhalf4 vec4
  144. //#endif
  145.  
  146. #ifdef VERTEX_SHADER
  147. uniform highp mat4 ModelViewProjectionMatrix;
  148. #endif
  149.  
  150. #ifdef VERTEX_SHADER
  151. #ifdef USETRIPPY
  152. // LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE
  153. // tweaked scale
  154. uniform highp float ClientTime;
  155. vec4 TrippyVertex(vec4 position)
  156. {
  157.     float worldTime = ClientTime;
  158.     // tweaked for Quake
  159.     worldTime *= 10.0;
  160.     position *= 0.125;
  161.     //~tweaked for Quake
  162.     float distanceSquared = (position.x * position.x + position.z * position.z);
  163.     position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0);
  164.     float y = position.y;
  165.     float x = position.x;
  166.     float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0);
  167.     position.y = x*sin(om)+y*cos(om);
  168.     position.x = x*cos(om)-y*sin(om);
  169.     return position;
  170. }
  171. #endif
  172. #endif
  173.  
  174. #ifdef MODE_DEPTH_OR_SHADOW
  175. dp_varying highp float Depth;
  176. #ifdef VERTEX_SHADER
  177. void main(void)
  178. {
  179. #ifdef USESKELETAL
  180.     ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
  181.     ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
  182.     ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
  183.     vec4 sw = Attrib_SkeletalWeight;
  184.     vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
  185.     vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
  186.     vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
  187.     mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
  188.     vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
  189. #define Attrib_Position SkeletalVertex
  190. #endif
  191.     gl_Position = ModelViewProjectionMatrix * Attrib_Position;
  192. #ifdef USETRIPPY
  193.     gl_Position = TrippyVertex(gl_Position);
  194. #endif
  195.     Depth = gl_Position.z;
  196. }
  197. #endif
  198.  
  199. #ifdef FRAGMENT_SHADER
  200. void main(void)
  201. {
  202. #ifdef USEDEPTHRGB
  203.     dp_FragColor = encodedepthmacro(Depth);
  204. #else
  205.     dp_FragColor = vec4(1.0,1.0,1.0,1.0);
  206. #endif
  207. }
  208. #endif
  209. #else // !MODE_DEPTH_ORSHADOW
  210.  
  211.  
  212.  
  213.  
  214. #ifdef MODE_POSTPROCESS
  215. dp_varying mediump vec2 TexCoord1;
  216. dp_varying mediump vec2 TexCoord2;
  217.  
  218. #ifdef VERTEX_SHADER
  219. void main(void)
  220. {
  221.     gl_Position = ModelViewProjectionMatrix * Attrib_Position;
  222.     TexCoord1 = Attrib_TexCoord0.xy;
  223. #ifdef USEBLOOM
  224.     TexCoord2 = Attrib_TexCoord4.xy;
  225. #endif
  226. }
  227. #endif
  228.  
  229. #ifdef FRAGMENT_SHADER
  230. uniform sampler2D Texture_First;
  231. #ifdef USEBLOOM
  232. uniform sampler2D Texture_Second;
  233. uniform mediump vec4 BloomColorSubtract;
  234. #endif
  235. #ifdef USEGAMMARAMPS
  236. uniform sampler2D Texture_GammaRamps;
  237. #endif
  238. #ifdef USESATURATION
  239. uniform mediump float Saturation;
  240. #endif
  241. #ifdef USEVIEWTINT
  242. uniform mediump vec4 ViewTintColor;
  243. #endif
  244. //uncomment these if you want to use them:
  245. uniform mediump vec4 UserVec1;
  246. uniform mediump vec4 UserVec2;
  247. // uniform mediump vec4 UserVec3;
  248. // uniform mediump vec4 UserVec4;
  249. // uniform highp float ClientTime;
  250. uniform mediump vec2 PixelSize;
  251.  
  252. #ifdef USEFXAA
  253. // graphitemaster: based off the white paper by Timothy Lottes
  254. // http://developer.download.nvidia.com/assets/gamedev/files/sdk/11/FXAA_WhitePaper.pdf
  255. vec4 fxaa(vec4 inColor, float maxspan)
  256. {
  257.     vec4 ret = inColor; // preserve old
  258.     float mulreduct = 1.0/maxspan;
  259.     float minreduct = (1.0 / 128.0);
  260.  
  261.     // directions
  262.     vec3 NW = dp_texture2D(Texture_First, TexCoord1 + (vec2(-1.0, -1.0) * PixelSize)).xyz;
  263.     vec3 NE = dp_texture2D(Texture_First, TexCoord1 + (vec2(+1.0, -1.0) * PixelSize)).xyz;
  264.     vec3 SW = dp_texture2D(Texture_First, TexCoord1 + (vec2(-1.0, +1.0) * PixelSize)).xyz;
  265.     vec3 SE = dp_texture2D(Texture_First, TexCoord1 + (vec2(+1.0, +1.0) * PixelSize)).xyz;
  266.     vec3 M = dp_texture2D(Texture_First, TexCoord1).xyz;
  267.  
  268.     // luminance directions
  269.     vec3 luma = vec3(0.299, 0.587, 0.114);
  270.     float lNW = dot(NW, luma);
  271.     float lNE = dot(NE, luma);
  272.     float lSW = dot(SW, luma);
  273.     float lSE = dot(SE, luma);
  274.     float lM = dot(M, luma);
  275.     float lMin = min(lM, min(min(lNW, lNE), min(lSW, lSE)));
  276.     float lMax = max(lM, max(max(lNW, lNE), max(lSW, lSE)));
  277.  
  278.     // direction and reciprocal
  279.     vec2 dir = vec2(-((lNW + lNE) - (lSW + lSE)), ((lNW + lSW) - (lNE + lSE)));
  280.     float rcp = 1.0/(min(abs(dir.x), abs(dir.y)) + max((lNW + lNE + lSW + lSE) * (0.25 * mulreduct), minreduct));
  281.  
  282.     // span
  283.     dir = min(vec2(maxspan, maxspan), max(vec2(-maxspan, -maxspan), dir * rcp)) * PixelSize;
  284.  
  285.     vec3 rA = (1.0/2.0) * (
  286.         dp_texture2D(Texture_First, TexCoord1 + dir * (1.0/3.0 - 0.5)).xyz +
  287.         dp_texture2D(Texture_First, TexCoord1 + dir * (2.0/3.0 - 0.5)).xyz);
  288.     vec3 rB = rA * (1.0/2.0) + (1.0/4.0) * (
  289.         dp_texture2D(Texture_First, TexCoord1 + dir * (0.0/3.0 - 0.5)).xyz +
  290.         dp_texture2D(Texture_First, TexCoord1 + dir * (3.0/3.0 - 0.5)).xyz);
  291.     float lB = dot(rB, luma);
  292.  
  293.     ret.xyz = ((lB < lMin) || (lB > lMax)) ? rA : rB;
  294.     ret.a = 1.0;
  295.     return ret;
  296. }
  297. #endif
  298.  
  299. void main(void)
  300. {
  301.     dp_FragColor = dp_texture2D(Texture_First, TexCoord1);
  302.  
  303. #ifdef USEFXAA
  304.     dp_FragColor = fxaa(dp_FragColor, 8.0); // 8.0 can be changed for larger span
  305. #endif
  306.  
  307. #ifdef USEPOSTPROCESSING
  308. // do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want
  309. // this code does a blur with the radius specified in the first component of r_glsl_postprocess_uservec1 and blends it using the second component
  310. #if defined(USERVEC1) || defined(USERVEC2)
  311.     float sobel = 1.0;
  312.     // vec2 ts = textureSize(Texture_First, 0);
  313.     // vec2 px = vec2(1/ts.x, 1/ts.y);
  314.     vec2 px = PixelSize;
  315.     vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb;
  316.     vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,  0.0)).rgb;
  317.     vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb;
  318.     vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb;
  319.     vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,  0.0)).rgb;
  320.     vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb;
  321.     vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb;
  322.     vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2(  0.0,-px.y)).rgb;
  323.     vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb;
  324.     vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb;
  325.     vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2(  0.0, px.y)).rgb;
  326.     vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb;
  327.     float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1);
  328.     float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2);
  329.     float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3);
  330.     float px4 =  1.0 * dot(vec3(0.3, 0.59, 0.11), x4);
  331.     float px5 =  2.0 * dot(vec3(0.3, 0.59, 0.11), x5);
  332.     float px6 =  1.0 * dot(vec3(0.3, 0.59, 0.11), x6);
  333.     float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1);
  334.     float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2);
  335.     float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3);
  336.     float py4 =  1.0 * dot(vec3(0.3, 0.59, 0.11), y4);
  337.     float py5 =  2.0 * dot(vec3(0.3, 0.59, 0.11), y5);
  338.     float py6 =  1.0 * dot(vec3(0.3, 0.59, 0.11), y6);
  339.     sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6);
  340.     dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y;
  341.     dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y;
  342.     dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y;
  343.     dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107,  0.707107)) * UserVec1.y;
  344.     dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990,  0.891007)) * UserVec1.y;
  345.     dp_FragColor /= (1.0 + 5.0 * UserVec1.y);
  346.     dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y;
  347. #endif
  348. #endif
  349.  
  350. #ifdef USEBLOOM
  351.     dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract);
  352. #endif
  353.  
  354. #ifdef USEVIEWTINT
  355.     dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a);
  356. #endif
  357.  
  358. #ifdef USESATURATION
  359.     //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter
  360.     float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114));
  361.     // 'vampire sight' effect, wheres red is compensated
  362.     #ifdef SATURATION_REDCOMPENSATE
  363.         float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation));
  364.         dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation);
  365.         dp_FragColor.r += rboost;
  366.     #else
  367.         // normal desaturation
  368.         //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation;
  369.         dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation);
  370.     #endif
  371. #endif
  372.  
  373. #ifdef USEGAMMARAMPS
  374.     dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r;
  375.     dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g;
  376.     dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b;
  377. #endif
  378. }
  379. #endif
  380. #else // !MODE_POSTPROCESS
  381.  
  382.  
  383.  
  384.  
  385. #ifdef MODE_GENERIC
  386. #ifdef USEDIFFUSE
  387. dp_varying mediump vec2 TexCoord1;
  388. #endif
  389. #ifdef USESPECULAR
  390. dp_varying mediump vec2 TexCoord2;
  391. #endif
  392. uniform myhalf Alpha;
  393. #ifdef VERTEX_SHADER
  394. void main(void)
  395. {
  396. #ifdef USESKELETAL
  397.     ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
  398.     ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
  399.     ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
  400.     vec4 sw = Attrib_SkeletalWeight;
  401.     vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
  402.     vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
  403.     vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
  404.     mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
  405.     vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
  406. #define Attrib_Position SkeletalVertex
  407. #endif
  408.     VertexColor = Attrib_Color;
  409. #ifdef USEDIFFUSE
  410.     TexCoord1 = Attrib_TexCoord0.xy;
  411. #endif
  412. #ifdef USESPECULAR
  413.     TexCoord2 = Attrib_TexCoord1.xy;
  414. #endif
  415.     gl_Position = ModelViewProjectionMatrix * Attrib_Position;
  416. #ifdef USETRIPPY
  417.     gl_Position = TrippyVertex(gl_Position);
  418. #endif
  419. }
  420. #endif
  421.  
  422. #ifdef FRAGMENT_SHADER
  423. #ifdef USEDIFFUSE
  424. uniform sampler2D Texture_First;
  425. #endif
  426. #ifdef USESPECULAR
  427. uniform sampler2D Texture_Second;
  428. #endif
  429. #ifdef USEGAMMARAMPS
  430. uniform sampler2D Texture_GammaRamps;
  431. #endif
  432.  
  433. void main(void)
  434. {
  435. #ifdef USEVIEWTINT
  436.     dp_FragColor = VertexColor;
  437. #else
  438.     dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
  439. #endif
  440. #ifdef USEDIFFUSE
  441. # ifdef USEREFLECTCUBE
  442.     // suppress texture alpha
  443.     dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb;
  444. # else
  445.     dp_FragColor *= dp_texture2D(Texture_First, TexCoord1);
  446. # endif
  447. #endif
  448.  
  449. #ifdef USESPECULAR
  450.     vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2);
  451. # ifdef USECOLORMAPPING
  452.     dp_FragColor *= tex2;
  453. # endif
  454. # ifdef USEGLOW
  455.     dp_FragColor += tex2;
  456. # endif
  457. # ifdef USEVERTEXTEXTUREBLEND
  458.     dp_FragColor = mix(dp_FragColor, tex2, tex2.a);
  459. # endif
  460. #endif
  461. #ifdef USEGAMMARAMPS
  462.     dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r;
  463.     dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g;
  464.     dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b;
  465. #endif
  466. #ifdef USEALPHAKILL
  467.     dp_FragColor.a *= Alpha;
  468. #endif
  469. }
  470. #endif
  471. #else // !MODE_GENERIC
  472.  
  473.  
  474.  
  475.  
  476. #ifdef MODE_BLOOMBLUR
  477. dp_varying mediump vec2 TexCoord;
  478. #ifdef VERTEX_SHADER
  479. void main(void)
  480. {
  481.     VertexColor = Attrib_Color;
  482.     TexCoord = Attrib_TexCoord0.xy;
  483.     gl_Position = ModelViewProjectionMatrix * Attrib_Position;
  484. }
  485. #endif
  486.  
  487. #ifdef FRAGMENT_SHADER
  488. uniform sampler2D Texture_First;
  489. uniform mediump vec4 BloomBlur_Parameters;
  490.  
  491. void main(void)
  492. {
  493.     int i;
  494.     vec2 tc = TexCoord;
  495.     vec3 color = dp_texture2D(Texture_First, tc).rgb;
  496.     tc += BloomBlur_Parameters.xy;
  497.     for (i = 1;i < SAMPLES;i++)
  498.     {
  499.         color += dp_texture2D(Texture_First, tc).rgb;
  500.         tc += BloomBlur_Parameters.xy;
  501.     }
  502.     dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1);
  503. }
  504. #endif
  505. #else // !MODE_BLOOMBLUR
  506. #ifdef MODE_REFRACTION
  507. dp_varying mediump vec2 TexCoord;
  508. dp_varying highp vec4 ModelViewProjectionPosition;
  509. uniform highp mat4 TexMatrix;
  510. #ifdef VERTEX_SHADER
  511.  
  512. void main(void)
  513. {
  514. #ifdef USESKELETAL
  515.     ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
  516.     ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
  517.     ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
  518.     vec4 sw = Attrib_SkeletalWeight;
  519.     vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
  520.     vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
  521.     vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
  522.     mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
  523.     vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
  524. #define Attrib_Position SkeletalVertex
  525. #endif
  526. #ifdef USEALPHAGENVERTEX
  527.     VertexColor = Attrib_Color;
  528. #endif
  529.     TexCoord = vec2(TexMatrix * Attrib_TexCoord0);
  530.     gl_Position = ModelViewProjectionMatrix * Attrib_Position;
  531.     ModelViewProjectionPosition = gl_Position;
  532. #ifdef USETRIPPY
  533.     gl_Position = TrippyVertex(gl_Position);
  534. #endif
  535. }
  536. #endif
  537.  
  538. #ifdef FRAGMENT_SHADER
  539. uniform sampler2D Texture_Normal;
  540. uniform sampler2D Texture_Refraction;
  541.  
  542. uniform mediump vec4 DistortScaleRefractReflect;
  543. uniform mediump vec4 ScreenScaleRefractReflect;
  544. uniform mediump vec4 ScreenCenterRefractReflect;
  545. uniform mediump vec4 RefractColor;
  546. uniform mediump vec4 ReflectColor;
  547. uniform highp float ClientTime;
  548. #ifdef USENORMALMAPSCROLLBLEND
  549. uniform highp vec2 NormalmapScrollBlend;
  550. #endif
  551.  
  552. void main(void)
  553. {
  554.     vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);
  555.     //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;
  556.     vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;
  557. #ifdef USEALPHAGENVERTEX
  558.     vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a;
  559.     vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a);
  560. #else
  561.     vec2 distort = DistortScaleRefractReflect.xy;
  562.     vec4 refractcolor = RefractColor;
  563. #endif
  564.     #ifdef USENORMALMAPSCROLLBLEND
  565.         vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);
  566.         normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;
  567.         vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort;
  568.     #else
  569.         vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort;
  570.     #endif
  571.     // FIXME temporary hack to detect the case that the reflection
  572.     // gets blackened at edges due to leaving the area that contains actual
  573.     // content.
  574.     // Remove this 'ack once we have a better way to stop this thing from
  575.     // 'appening.
  576.     float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);
  577.     f      *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);
  578.     f      *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);
  579.     f      *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);
  580.     ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);
  581.     dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor;
  582. }
  583. #endif
  584. #else // !MODE_REFRACTION
  585.  
  586.  
  587.  
  588.  
  589. #ifdef MODE_WATER
  590. dp_varying mediump vec2 TexCoord;
  591. dp_varying highp vec3 EyeVector;
  592. dp_varying highp vec4 ModelViewProjectionPosition;
  593. #ifdef VERTEX_SHADER
  594. uniform highp vec3 EyePosition;
  595. uniform highp mat4 TexMatrix;
  596.  
  597. void main(void)
  598. {
  599. #ifdef USESKELETAL
  600.     ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
  601.     ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
  602.     ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
  603.     vec4 sw = Attrib_SkeletalWeight;
  604.     vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
  605.     vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
  606.     vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
  607.     mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
  608.     mat3 SkeletalNormalMatrix = mat3(cross(SkeletalMatrix[1].xyz, SkeletalMatrix[2].xyz), cross(SkeletalMatrix[2].xyz, SkeletalMatrix[0].xyz), cross(SkeletalMatrix[0].xyz, SkeletalMatrix[1].xyz)); // is actually transpose(inverse(mat3(SkeletalMatrix))) * det(mat3(SkeletalMatrix))
  609.     vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
  610.     vec3 SkeletalSVector = normalize(Attrib_TexCoord1.xyz * SkeletalNormalMatrix);
  611.     vec3 SkeletalTVector = normalize(Attrib_TexCoord2.xyz * SkeletalNormalMatrix);
  612.     vec3 SkeletalNormal  = normalize(Attrib_TexCoord3.xyz * SkeletalNormalMatrix);
  613. #define Attrib_Position SkeletalVertex
  614. #define Attrib_TexCoord1 SkeletalSVector
  615. #define Attrib_TexCoord2 SkeletalTVector
  616. #define Attrib_TexCoord3 SkeletalNormal
  617. #endif
  618. #ifdef USEALPHAGENVERTEX
  619.     VertexColor = Attrib_Color;
  620. #endif
  621.     TexCoord = vec2(TexMatrix * Attrib_TexCoord0);
  622.     vec3 EyeRelative = EyePosition - Attrib_Position.xyz;
  623.     EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz);
  624.     EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz);
  625.     EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz);
  626.     gl_Position = ModelViewProjectionMatrix * Attrib_Position;
  627.     ModelViewProjectionPosition = gl_Position;
  628. #ifdef USETRIPPY
  629.     gl_Position = TrippyVertex(gl_Position);
  630. #endif
  631. }
  632. #endif
  633.  
  634. #ifdef FRAGMENT_SHADER
  635. uniform sampler2D Texture_Normal;
  636. uniform sampler2D Texture_Refraction;
  637. uniform sampler2D Texture_Reflection;
  638.  
  639. uniform mediump vec4 DistortScaleRefractReflect;
  640. uniform mediump vec4 ScreenScaleRefractReflect;
  641. uniform mediump vec4 ScreenCenterRefractReflect;
  642. uniform mediump vec4 RefractColor;
  643. uniform mediump vec4 ReflectColor;
  644. uniform mediump float ReflectFactor;
  645. uniform mediump float ReflectOffset;
  646. uniform highp float ClientTime;
  647. #ifdef USENORMALMAPSCROLLBLEND
  648. uniform highp vec2 NormalmapScrollBlend;
  649. #endif
  650.  
  651. void main(void)
  652. {
  653.     vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);
  654.     //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;
  655.     vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;
  656.     //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0);
  657.     // slight water animation via 2 layer scrolling (todo: tweak)
  658. #ifdef USEALPHAGENVERTEX
  659.     vec4 distort = DistortScaleRefractReflect * VertexColor.a;
  660.     float reflectoffset = ReflectOffset * VertexColor.a;
  661.     float reflectfactor = ReflectFactor * VertexColor.a;
  662.     vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a);
  663. #else
  664.     vec4 distort = DistortScaleRefractReflect;
  665.     float reflectoffset = ReflectOffset;
  666.     float reflectfactor = ReflectFactor;
  667.     vec4 refractcolor = RefractColor;
  668. #endif
  669.     #ifdef USENORMALMAPSCROLLBLEND
  670.         vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);
  671.         normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;
  672.         vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort;
  673.     #else
  674.         vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort;
  675.     #endif
  676.     // FIXME temporary hack to detect the case that the reflection
  677.     // gets blackened at edges due to leaving the area that contains actual
  678.     // content.
  679.     // Remove this 'ack once we have a better way to stop this thing from
  680.     // 'appening.
  681.     float f  = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002);
  682.     f       *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002);
  683.     f       *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002);
  684.     f       *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002);
  685.     ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);
  686.     f  = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002);
  687.     f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002);
  688.     f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002);
  689.     f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002);
  690.     ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);
  691.     float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset;
  692.     dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);
  693. }
  694. #endif
  695. #else // !MODE_WATER
  696.  
  697.  
  698.  
  699.  
  700. // common definitions between vertex shader and fragment shader:
  701.  
  702. dp_varying mediump vec4 TexCoordSurfaceLightmap;
  703. #ifdef USEVERTEXTEXTUREBLEND
  704. dp_varying mediump vec2 TexCoord2;
  705. #endif
  706.  
  707. #ifdef MODE_LIGHTSOURCE
  708. dp_varying mediump vec3 CubeVector;
  709. #endif
  710.  
  711. #if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE)
  712. dp_varying mediump vec3 LightVector;
  713. #endif
  714.  
  715. #ifdef USEEYEVECTOR
  716. dp_varying highp vec4 EyeVectorFogDepth;
  717. #endif
  718.  
  719. #if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL)
  720. dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent)
  721. dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal)
  722. dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal)
  723. #else
  724. # ifdef USEFOG
  725. dp_varying highp vec3 EyeVectorModelSpace;
  726. # endif
  727. #endif
  728.  
  729. #ifdef USEREFLECTION
  730. dp_varying highp vec4 ModelViewProjectionPosition;
  731. #endif
  732. #ifdef MODE_DEFERREDLIGHTSOURCE
  733. uniform highp vec3 LightPosition;
  734. dp_varying highp vec4 ModelViewPosition;
  735. #endif
  736.  
  737. #ifdef MODE_LIGHTSOURCE
  738. uniform highp vec3 LightPosition;
  739. #endif
  740. uniform highp vec3 EyePosition;
  741. #ifdef MODE_LIGHTDIRECTION
  742. uniform highp vec3 LightDir;
  743. #endif
  744. uniform highp vec4 FogPlane;
  745.  
  746. #ifdef USESHADOWMAPORTHO
  747. dp_varying highp vec3 ShadowMapTC;
  748. #endif
  749.  
  750. #ifdef USEBOUNCEGRID
  751. dp_varying highp vec3 BounceGridTexCoord;
  752. #endif
  753.  
  754. #ifdef MODE_DEFERREDGEOMETRY
  755. dp_varying highp float Depth;
  756. #endif
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763. // TODO: get rid of tangentt (texcoord2) and use a crossproduct to regenerate it from tangents (texcoord1) and normal (texcoord3), this would require sending a 4 component texcoord1 with W as 1 or -1 according to which side the texcoord2 should be on
  764.  
  765. // fragment shader specific:
  766. #ifdef FRAGMENT_SHADER
  767.  
  768. uniform sampler2D Texture_Normal;
  769. uniform sampler2D Texture_Color;
  770. uniform sampler2D Texture_Gloss;
  771. #ifdef USEGLOW
  772. uniform sampler2D Texture_Glow;
  773. #endif
  774. #ifdef USEVERTEXTEXTUREBLEND
  775. uniform sampler2D Texture_SecondaryNormal;
  776. uniform sampler2D Texture_SecondaryColor;
  777. uniform sampler2D Texture_SecondaryGloss;
  778. #ifdef USEGLOW
  779. uniform sampler2D Texture_SecondaryGlow;
  780. #endif
  781. #endif
  782. #ifdef USECOLORMAPPING
  783. uniform sampler2D Texture_Pants;
  784. uniform sampler2D Texture_Shirt;
  785. #endif
  786. #ifdef USEFOG
  787. #ifdef USEFOGHEIGHTTEXTURE
  788. uniform sampler2D Texture_FogHeightTexture;
  789. #endif
  790. uniform sampler2D Texture_FogMask;
  791. #endif
  792. #ifdef USELIGHTMAP
  793. uniform sampler2D Texture_Lightmap;
  794. #endif
  795. #if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)
  796. uniform sampler2D Texture_Deluxemap;
  797. #endif
  798. #ifdef USEREFLECTION
  799. uniform sampler2D Texture_Reflection;
  800. #endif
  801.  
  802. #ifdef MODE_DEFERREDLIGHTSOURCE
  803. uniform sampler2D Texture_ScreenNormalMap;
  804. #endif
  805. #ifdef USEDEFERREDLIGHTMAP
  806. #ifdef USECELOUTLINES
  807. uniform sampler2D Texture_ScreenNormalMap;
  808. #endif
  809. uniform sampler2D Texture_ScreenDiffuse;
  810. uniform sampler2D Texture_ScreenSpecular;
  811. #endif
  812.  
  813. uniform mediump vec3 Color_Pants;
  814. uniform mediump vec3 Color_Shirt;
  815. uniform mediump vec3 FogColor;
  816.  
  817. #ifdef USEFOG
  818. uniform highp float FogRangeRecip;
  819. uniform highp float FogPlaneViewDist;
  820. uniform highp float FogHeightFade;
  821. vec3 FogVertex(vec4 surfacecolor)
  822. {
  823. #if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL)
  824.     vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w);
  825. #endif
  826.     float FogPlaneVertexDist = EyeVectorFogDepth.w;
  827.     float fogfrac;
  828.        vec3 fc = FogColor;
  829. #ifdef USEFOGALPHAHACK
  830.     fc *= surfacecolor.a;
  831. #endif
  832. #ifdef USEFOGHEIGHTTEXTURE
  833.     vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade));
  834.     fogfrac = fogheightpixel.a;
  835.     return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);
  836. #else
  837. # ifdef USEFOGOUTSIDE
  838.     fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);
  839. # else
  840.     fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
  841. # endif
  842.     return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);
  843. #endif
  844. }
  845. #endif
  846.  
  847. #ifdef USEOFFSETMAPPING
  848. uniform mediump vec4 OffsetMapping_ScaleSteps;
  849. uniform mediump float OffsetMapping_Bias;
  850. #ifdef USEOFFSETMAPPING_LOD
  851. uniform mediump float OffsetMapping_LodDistance;
  852. #endif
  853. vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy)
  854. {
  855.     float i;
  856.     // distance-based LOD
  857. #ifdef USEOFFSETMAPPING_LOD
  858.     //mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z);
  859.     //mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor);
  860.     mediump float GuessLODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z);
  861. #ifdef USEOFFSETMAPPING_RELIEFMAPPING
  862.     // stupid workaround because 1-step and 2-step reliefmapping is void
  863.     mediump float LODSteps = max(3.0, ceil(GuessLODFactor * OffsetMapping_ScaleSteps.y));
  864. #else
  865.     mediump float LODSteps = ceil(GuessLODFactor * OffsetMapping_ScaleSteps.y);
  866. #endif
  867.     mediump float LODFactor = LODSteps / OffsetMapping_ScaleSteps.y;
  868.     mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, LODSteps, 1.0 / LODSteps, OffsetMapping_ScaleSteps.w * LODFactor);
  869. #else
  870.     #define ScaleSteps OffsetMapping_ScaleSteps
  871. #endif
  872. #ifdef USEOFFSETMAPPING_RELIEFMAPPING
  873.     float f;
  874.     // 14 sample relief mapping: linear search and then binary search
  875.     // this basically steps forward a small amount repeatedly until it finds
  876.     // itself inside solid, then jitters forward and back using decreasing
  877.     // amounts to find the impact
  878.     //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1);
  879.     //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1);
  880.     vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1);
  881.     vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1);
  882.     OffsetVector *= ScaleSteps.z;
  883.     for(i = 1.0; i < ScaleSteps.y; ++i)
  884.         RT += OffsetVector *  step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z);
  885.     for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5)
  886.         RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f);
  887.     return RT.xy;
  888. #else
  889.     // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits)
  890.     //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1));
  891.     //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1));
  892.     vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1));
  893.     OffsetVector *= ScaleSteps.z;
  894.     for(i = 0.0; i < ScaleSteps.y; ++i)
  895.         TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a);
  896.     return TexCoord;
  897. #endif
  898. }
  899. #endif // USEOFFSETMAPPING
  900.  
  901. #if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)
  902. uniform sampler2D Texture_Attenuation;
  903. uniform samplerCube Texture_Cube;
  904. #endif
  905.  
  906. #if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO)
  907.  
  908. #ifdef USESHADOWMAP2D
  909. # ifdef USESHADOWSAMPLER
  910. uniform sampler2DShadow Texture_ShadowMap2D;
  911. # else
  912. uniform sampler2D Texture_ShadowMap2D;
  913. # endif
  914. #endif
  915.  
  916. #ifdef USESHADOWMAPVSDCT
  917. uniform samplerCube Texture_CubeProjection;
  918. #endif
  919.  
  920. #if defined(USESHADOWMAP2D)
  921. uniform mediump vec4 ShadowMap_TextureScale;
  922. uniform mediump vec4 ShadowMap_Parameters;
  923. #endif
  924.  
  925. #if defined(USESHADOWMAP2D)
  926. # ifdef USESHADOWMAPORTHO
  927. #  define GetShadowMapTC2D(dir) (max(vec3(0.0, 0.0, 0.0), min(dir, ShadowMap_Parameters.xyz)))
  928. # else
  929. #  ifdef USESHADOWMAPVSDCT
  930. vec3 GetShadowMapTC2D(vec3 dir)
  931. {
  932.     vec3 adir = abs(dir);
  933.     float m = max(max(adir.x, adir.y), adir.z);
  934.     vec4 proj = dp_textureCube(Texture_CubeProjection, dir);
  935. #ifdef USEDEPTHRGB
  936.     return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) +  proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w);
  937. #else
  938.     vec2 mparams = ShadowMap_Parameters.xy / m;
  939.     return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w);
  940. #endif
  941. }
  942. #  else
  943. vec3 GetShadowMapTC2D(vec3 dir)
  944. {
  945.     vec3 adir = abs(dir);
  946.     float m; vec4 proj;
  947.     if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); }
  948.     if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); }
  949. #ifdef USEDEPTHRGB
  950.     return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w);
  951. #else
  952.     vec2 mparams = ShadowMap_Parameters.xy / m;
  953.     return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w);
  954. #endif
  955. }
  956. #  endif
  957. # endif
  958. #endif // defined(USESHADOWMAP2D)
  959.  
  960. # ifdef USESHADOWMAP2D
  961. float ShadowMapCompare(vec3 dir)
  962. {
  963.     vec3 shadowmaptc = GetShadowMapTC2D(dir) + vec3(ShadowMap_TextureScale.zw, 0.0f);
  964.     float f;
  965.  
  966. #  ifdef USEDEPTHRGB
  967. #   ifdef USESHADOWMAPPCF
  968. #    define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale.xy))
  969. #    if USESHADOWMAPPCF > 1
  970.     vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);
  971.     center *= ShadowMap_TextureScale.xy;
  972.     vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));
  973.     vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));
  974.     vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));
  975.     vec4 row4 = step(shadowmaptc.z, vec4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));
  976.     vec4 cols = row2 + row3 + mix(row1, row4, offset.y);
  977.     f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));
  978. #    else
  979.     vec2 center = shadowmaptc.xy*ShadowMap_TextureScale.xy, offset = fract(shadowmaptc.xy);
  980.     vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));
  981.     vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));
  982.     vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));
  983.     vec3 cols = row2 + mix(row1, row3, offset.y);
  984.     f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));
  985. #    endif
  986. #   else
  987.     f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale.xy)));
  988. #   endif
  989. #  else
  990. #   ifdef USESHADOWSAMPLER
  991. #     ifdef USESHADOWMAPPCF
  992. #       define texval(off) dp_shadow2D(Texture_ShadowMap2D, vec3(off, shadowmaptc.z))  
  993.     vec2 offset = fract(shadowmaptc.xy - 0.5);
  994.    vec4 size = vec4(offset + 1.0, 2.0 - offset);
  995. #       if USESHADOWMAPPCF > 1
  996.    vec2 center = (shadowmaptc.xy - offset + 0.5)*ShadowMap_TextureScale.xy;
  997.    vec4 weight = (vec4(-1.5, -1.5, 2.0, 2.0) + (shadowmaptc.xy - 0.5*offset).xyxy)*ShadowMap_TextureScale.xyxy;
  998.     f = (1.0/25.0)*dot(size.zxzx*size.wwyy, vec4(texval(weight.xy), texval(weight.zy), texval(weight.xw), texval(weight.zw))) +
  999.         (2.0/25.0)*dot(size, vec4(texval(vec2(weight.z, center.y)), texval(vec2(center.x, weight.w)), texval(vec2(weight.x, center.y)), texval(vec2(center.x, weight.y)))) +
  1000.         (4.0/25.0)*texval(center);
  1001. #       else
  1002.     vec4 weight = (vec4(1.0, 1.0, -0.5, -0.5) + (shadowmaptc.xy - 0.5*offset).xyxy)*ShadowMap_TextureScale.xyxy;
  1003.     f = (1.0/9.0)*dot(size.zxzx*size.wwyy, vec4(texval(weight.zw), texval(weight.xw), texval(weight.zy), texval(weight.xy)));
  1004. #       endif        
  1005. #     else
  1006.     f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale.xy, shadowmaptc.z));
  1007. #     endif
  1008. #   else
  1009. #     ifdef USESHADOWMAPPCF
  1010. #      if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)
  1011. #       ifdef GL_ARB_texture_gather
  1012. #         define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y))
  1013. #       else
  1014. #         define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale.xy)
  1015. #       endif
  1016.     vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale.xy;
  1017. #       if USESHADOWMAPPCF > 1
  1018.    vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0));
  1019.    vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0));
  1020.    vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0));
  1021.    vec4 group4 = step(shadowmaptc.z, texval(-2.0,  0.0));
  1022.    vec4 group5 = step(shadowmaptc.z, texval( 0.0,  0.0));
  1023.    vec4 group6 = step(shadowmaptc.z, texval( 2.0,  0.0));
  1024.    vec4 group7 = step(shadowmaptc.z, texval(-2.0,  2.0));
  1025.    vec4 group8 = step(shadowmaptc.z, texval( 0.0,  2.0));
  1026.    vec4 group9 = step(shadowmaptc.z, texval( 2.0,  2.0));
  1027.     vec4 locols = vec4(group1.ab, group3.ab);
  1028.     vec4 hicols = vec4(group7.rg, group9.rg);
  1029.     locols.yz += group2.ab;
  1030.     hicols.yz += group8.rg;
  1031.     vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) +
  1032.                 vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) +
  1033.                 mix(locols, hicols, offset.y);
  1034.     vec4 cols = group5 + vec4(group2.rg, group8.ab);
  1035.     cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x);
  1036.     f = dot(cols, vec4(1.0/25.0));
  1037. #      else
  1038.     vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));
  1039.     vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));
  1040.     vec4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));
  1041.     vec4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));
  1042.     vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) +
  1043.                 mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y);
  1044.     f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));
  1045. #       endif
  1046. #      else
  1047. #       ifdef GL_EXT_gpu_shader4
  1048. #         define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r
  1049. #       else
  1050. #         define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale.xy).r  
  1051. #       endif
  1052. #       if USESHADOWMAPPCF > 1
  1053.     vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);
  1054.     center *= ShadowMap_TextureScale.xy;
  1055.     vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));
  1056.     vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));
  1057.     vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));
  1058.     vec4 row4 = step(shadowmaptc.z, vec4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));
  1059.     vec4 cols = row2 + row3 + mix(row1, row4, offset.y);
  1060.     f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));
  1061. #       else
  1062.     vec2 center = shadowmaptc.xy*ShadowMap_TextureScale.xy, offset = fract(shadowmaptc.xy);
  1063.     vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));
  1064.     vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));
  1065.     vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));
  1066.     vec3 cols = row2 + mix(row1, row3, offset.y);
  1067.     f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));
  1068. #       endif
  1069. #      endif
  1070. #     else
  1071.     f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale.xy).r);
  1072. #     endif
  1073. #   endif
  1074. #  endif
  1075. #  ifdef USESHADOWMAPORTHO
  1076.     return mix(ShadowMap_Parameters.w, 1.0, f);
  1077. #  else
  1078.     return f;
  1079. #  endif
  1080. }
  1081. # endif
  1082. #endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO)
  1083. #endif // FRAGMENT_SHADER
  1084.  
  1085.  
  1086.  
  1087.  
  1088. #ifdef MODE_DEFERREDGEOMETRY
  1089. #ifdef VERTEX_SHADER
  1090. uniform highp mat4 TexMatrix;
  1091. #ifdef USEVERTEXTEXTUREBLEND
  1092. uniform highp mat4 BackgroundTexMatrix;
  1093. #endif
  1094. uniform highp mat4 ModelViewMatrix;
  1095. void main(void)
  1096. {
  1097. #ifdef USESKELETAL
  1098.     ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
  1099.     ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
  1100.     ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
  1101.     vec4 sw = Attrib_SkeletalWeight;
  1102.     vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
  1103.     vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
  1104.     vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
  1105.     mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
  1106.     mat3 SkeletalNormalMatrix = mat3(cross(SkeletalMatrix[1].xyz, SkeletalMatrix[2].xyz), cross(SkeletalMatrix[2].xyz, SkeletalMatrix[0].xyz), cross(SkeletalMatrix[0].xyz, SkeletalMatrix[1].xyz)); // is actually transpose(inverse(mat3(SkeletalMatrix))) * det(mat3(SkeletalMatrix))
  1107.     vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
  1108.     vec3 SkeletalSVector = normalize(Attrib_TexCoord1.xyz * SkeletalNormalMatrix);
  1109.     vec3 SkeletalTVector = normalize(Attrib_TexCoord2.xyz * SkeletalNormalMatrix);
  1110.     vec3 SkeletalNormal  = normalize(Attrib_TexCoord3.xyz * SkeletalNormalMatrix);
  1111. #define Attrib_Position SkeletalVertex
  1112. #define Attrib_TexCoord1 SkeletalSVector
  1113. #define Attrib_TexCoord2 SkeletalTVector
  1114. #define Attrib_TexCoord3 SkeletalNormal
  1115. #endif
  1116.     TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0);
  1117. #ifdef USEVERTEXTEXTUREBLEND
  1118.     VertexColor = Attrib_Color;
  1119.     TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0);
  1120. #endif
  1121.  
  1122.     // transform unnormalized eye direction into tangent space
  1123. #ifdef USEOFFSETMAPPING
  1124.     vec3 EyeRelative = EyePosition - Attrib_Position.xyz;
  1125.     EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz);
  1126.     EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz);
  1127.     EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz);
  1128.     EyeVectorFogDepth.w = 0.0;
  1129. #endif
  1130.  
  1131.     VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0));
  1132.     VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0));
  1133.     VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0));
  1134.     gl_Position = ModelViewProjectionMatrix * Attrib_Position;
  1135. #ifdef USETRIPPY
  1136.     gl_Position = TrippyVertex(gl_Position);
  1137. #endif
  1138.     Depth = (ModelViewMatrix * Attrib_Position).z;
  1139. }
  1140. #endif // VERTEX_SHADER
  1141.  
  1142. #ifdef FRAGMENT_SHADER
  1143. void main(void)
  1144. {
  1145. #ifdef USEOFFSETMAPPING
  1146.     // apply offsetmapping
  1147.     vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy);
  1148.     vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy);
  1149.     vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy);
  1150. # define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy)
  1151. #else
  1152. # define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy)
  1153. #endif
  1154.  
  1155. #ifdef USEALPHAKILL
  1156.     if (offsetMappedTexture2D(Texture_Color).a < 0.5)
  1157.         discard;
  1158. #endif
  1159.  
  1160. #ifdef USEVERTEXTEXTUREBLEND
  1161.     float alpha = offsetMappedTexture2D(Texture_Color).a;
  1162.     float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0));
  1163.     //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0));
  1164.     //float terrainblend = float(VertexColor.a) * alpha > 0.5;
  1165. #endif
  1166.  
  1167. #ifdef USEVERTEXTEXTUREBLEND
  1168.     vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5);
  1169.     float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend);
  1170. #else
  1171.     vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5);
  1172.     float a = offsetMappedTexture2D(Texture_Gloss).a;
  1173. #endif
  1174.  
  1175.     vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz);
  1176.     dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a);
  1177. }
  1178. #endif // FRAGMENT_SHADER
  1179. #else // !MODE_DEFERREDGEOMETRY
  1180.  
  1181.  
  1182.  
  1183.  
  1184. #ifdef MODE_DEFERREDLIGHTSOURCE
  1185. #ifdef VERTEX_SHADER
  1186. uniform highp mat4 ModelViewMatrix;
  1187. void main(void)
  1188. {
  1189.     ModelViewPosition = ModelViewMatrix * Attrib_Position;
  1190.     gl_Position = ModelViewProjectionMatrix * Attrib_Position;
  1191. }
  1192. #endif // VERTEX_SHADER
  1193.  
  1194. #ifdef FRAGMENT_SHADER
  1195. uniform highp mat4 ViewToLight;
  1196. // ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far));
  1197. uniform highp vec2 ScreenToDepth;
  1198. uniform myhalf3 DeferredColor_Ambient;
  1199. uniform myhalf3 DeferredColor_Diffuse;
  1200. #ifdef USESPECULAR
  1201. uniform myhalf3 DeferredColor_Specular;
  1202. uniform myhalf SpecularPower;
  1203. #endif
  1204. uniform myhalf2 PixelToScreenTexCoord;
  1205. void main(void)
  1206. {
  1207.     // calculate viewspace pixel position
  1208.     vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;
  1209.     vec3 position;
  1210.     // get the geometry information (depth, normal, specular exponent)
  1211.     myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord);
  1212.     // decode viewspace pixel normal
  1213. //  myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5));
  1214.     myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg)));
  1215.     // decode viewspace pixel position
  1216. //  position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord));
  1217.     position.z = normalmap.b;
  1218. //  position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x);
  1219.     position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);
  1220.  
  1221.     // now do the actual shading
  1222.     // surfacenormal = pixel normal in viewspace
  1223.     // LightVector = pixel to light in viewspace
  1224.     // CubeVector = pixel in lightspace
  1225.     // eyenormal = pixel to view direction in viewspace
  1226.     vec3 CubeVector = vec3(ViewToLight * vec4(position,1));
  1227.     myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));
  1228. #ifdef USEDIFFUSE
  1229.     // calculate diffuse shading
  1230.     myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position));
  1231. SHADEDIFFUSE
  1232. #endif
  1233. #ifdef USESPECULAR
  1234.     // calculate directional shading
  1235.     myhalf3 eyenormal = -normalize(cast_myhalf3(position));
  1236. SHADESPECULAR(SpecularPower * normalmap.a)
  1237. #endif
  1238.  
  1239. #if defined(USESHADOWMAP2D)
  1240.     fade *= ShadowMapCompare(CubeVector);
  1241. #endif
  1242.  
  1243. #ifdef USESPECULAR
  1244.     gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);
  1245.     gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0);
  1246. # ifdef USECUBEFILTER
  1247.     vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb;
  1248.     gl_FragData[0].rgb *= cubecolor;
  1249.     gl_FragData[1].rgb *= cubecolor;
  1250. # endif
  1251. #else
  1252. # ifdef USEDIFFUSE
  1253.     gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);
  1254. # else
  1255.     gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0);
  1256. # endif
  1257. # ifdef USECUBEFILTER
  1258.     vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb;
  1259.     gl_FragColor.rgb *= cubecolor;
  1260. # endif
  1261. #endif
  1262. }
  1263. #endif // FRAGMENT_SHADER
  1264. #else // !MODE_DEFERREDLIGHTSOURCE
  1265.  
  1266.  
  1267.  
  1268.  
  1269. #ifdef VERTEX_SHADER
  1270. uniform highp mat4 TexMatrix;
  1271. #ifdef USEVERTEXTEXTUREBLEND
  1272. uniform highp mat4 BackgroundTexMatrix;
  1273. #endif
  1274. #ifdef MODE_LIGHTSOURCE
  1275. uniform highp mat4 ModelToLight;
  1276. #endif
  1277. #ifdef USESHADOWMAPORTHO
  1278. uniform highp mat4 ShadowMapMatrix;
  1279. #endif
  1280. #ifdef USEBOUNCEGRID
  1281. uniform highp mat4 BounceGridMatrix;
  1282. #endif
  1283. void main(void)
  1284. {
  1285. #ifdef USESKELETAL
  1286.     ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
  1287.     ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
  1288.     ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
  1289.     vec4 sw = Attrib_SkeletalWeight;
  1290.     vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
  1291.     vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
  1292.     vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
  1293.     mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
  1294. //  ivec4 si = ivec4(Attrib_SkeletalIndex);
  1295. //  mat4 SkeletalMatrix = Skeletal_Transform[si.x] * Attrib_SkeletalWeight.x + Skeletal_Transform[si.y] * Attrib_SkeletalWeight.y + Skeletal_Transform[si.z] * Attrib_SkeletalWeight.z + Skeletal_Transform[si.w] * Attrib_SkeletalWeight.w;
  1296.     mat3 SkeletalNormalMatrix = mat3(cross(SkeletalMatrix[1].xyz, SkeletalMatrix[2].xyz), cross(SkeletalMatrix[2].xyz, SkeletalMatrix[0].xyz), cross(SkeletalMatrix[0].xyz, SkeletalMatrix[1].xyz)); // is actually transpose(inverse(mat3(SkeletalMatrix))) * det(mat3(SkeletalMatrix))
  1297.     vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
  1298.     SkeletalVertex.w = 1.0;
  1299.     vec3 SkeletalSVector = normalize(Attrib_TexCoord1.xyz * SkeletalNormalMatrix);
  1300.     vec3 SkeletalTVector = normalize(Attrib_TexCoord2.xyz * SkeletalNormalMatrix);
  1301.     vec3 SkeletalNormal  = normalize(Attrib_TexCoord3.xyz * SkeletalNormalMatrix);
  1302. #define Attrib_Position SkeletalVertex
  1303. #define Attrib_TexCoord1 SkeletalSVector
  1304. #define Attrib_TexCoord2 SkeletalTVector
  1305. #define Attrib_TexCoord3 SkeletalNormal
  1306. #endif
  1307.  
  1308. #if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX)
  1309.     VertexColor = Attrib_Color;
  1310. #endif
  1311.     // copy the surface texcoord
  1312. #ifdef USELIGHTMAP
  1313.     TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy);
  1314. #else
  1315.     TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0);
  1316. #endif
  1317. #ifdef USEVERTEXTEXTUREBLEND
  1318.     TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0);
  1319. #endif
  1320.  
  1321. #ifdef USEBOUNCEGRID
  1322.     BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position);
  1323. #ifdef USEBOUNCEGRIDDIRECTIONAL
  1324.     BounceGridTexCoord.z *= 0.125;
  1325. #endif
  1326. #endif
  1327.  
  1328. #ifdef MODE_LIGHTSOURCE
  1329.     // transform vertex position into light attenuation/cubemap space
  1330.     // (-1 to +1 across the light box)
  1331.     CubeVector = vec3(ModelToLight * Attrib_Position);
  1332.  
  1333. # ifdef USEDIFFUSE
  1334.     // transform unnormalized light direction into tangent space
  1335.     // (we use unnormalized to ensure that it interpolates correctly and then
  1336.     //  normalize it per pixel)
  1337.     vec3 lightminusvertex = LightPosition - Attrib_Position.xyz;
  1338.     LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz);
  1339.     LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz);
  1340.     LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz);
  1341. # endif
  1342. #endif
  1343.  
  1344. #if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE)
  1345.     LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz);
  1346.     LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz);
  1347.     LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz);
  1348. #endif
  1349.  
  1350.     // transform unnormalized eye direction into tangent space
  1351. #ifdef USEEYEVECTOR
  1352.     vec3 EyeRelative = EyePosition - Attrib_Position.xyz;
  1353.     EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz);
  1354.     EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz);
  1355.     EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz);
  1356. #ifdef USEFOG
  1357.     EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position);
  1358. #else
  1359.     EyeVectorFogDepth.w = 0.0;
  1360. #endif
  1361. #endif
  1362.  
  1363.  
  1364. #if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL)
  1365. # ifdef USEFOG
  1366.     VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x);
  1367.     VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y);
  1368.     VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z);
  1369. # else
  1370.     VectorS = vec4(Attrib_TexCoord1, 0);
  1371.     VectorT = vec4(Attrib_TexCoord2, 0);
  1372.     VectorR = vec4(Attrib_TexCoord3, 0);
  1373. # endif
  1374. #else
  1375. # ifdef USEFOG
  1376.     EyeVectorModelSpace = EyePosition - Attrib_Position.xyz;
  1377. # endif
  1378. #endif
  1379.  
  1380.     // transform vertex to clipspace (post-projection, but before perspective divide by W occurs)
  1381.     gl_Position = ModelViewProjectionMatrix * Attrib_Position;
  1382.  
  1383. #ifdef USESHADOWMAPORTHO
  1384.     ShadowMapTC = vec3(ShadowMapMatrix * gl_Position);
  1385. #endif
  1386.  
  1387. #ifdef USEREFLECTION
  1388.     ModelViewProjectionPosition = gl_Position;
  1389. #endif
  1390. #ifdef USETRIPPY
  1391.     gl_Position = TrippyVertex(gl_Position);
  1392. #endif
  1393. }
  1394. #endif // VERTEX_SHADER
  1395.  
  1396.  
  1397.  
  1398.  
  1399. #ifdef FRAGMENT_SHADER
  1400. #ifdef USEDEFERREDLIGHTMAP
  1401. uniform myhalf2 PixelToScreenTexCoord;
  1402. uniform myhalf3 DeferredMod_Diffuse;
  1403. uniform myhalf3 DeferredMod_Specular;
  1404. #endif
  1405. uniform myhalf3 Color_Ambient;
  1406. uniform myhalf3 Color_Diffuse;
  1407. uniform myhalf3 Color_Specular;
  1408. uniform myhalf SpecularPower;
  1409. #ifdef USEGLOW
  1410. uniform myhalf3 Color_Glow;
  1411. #endif
  1412. uniform myhalf Alpha;
  1413. #ifdef USEREFLECTION
  1414. uniform mediump vec4 DistortScaleRefractReflect;
  1415. uniform mediump vec4 ScreenScaleRefractReflect;
  1416. uniform mediump vec4 ScreenCenterRefractReflect;
  1417. uniform mediump vec4 ReflectColor;
  1418. #endif
  1419. #ifdef USEREFLECTCUBE
  1420. uniform highp mat4 ModelToReflectCube;
  1421. uniform sampler2D Texture_ReflectMask;
  1422. uniform samplerCube Texture_ReflectCube;
  1423. #endif
  1424. #ifdef MODE_LIGHTDIRECTION
  1425. uniform myhalf3 LightColor;
  1426. #endif
  1427. #ifdef MODE_LIGHTSOURCE
  1428. uniform myhalf3 LightColor;
  1429. #endif
  1430. #ifdef USEBOUNCEGRID
  1431. uniform sampler3D Texture_BounceGrid;
  1432. uniform float BounceGridIntensity;
  1433. uniform highp mat4 BounceGridMatrix;
  1434. #endif
  1435. uniform highp float ClientTime;
  1436. #ifdef USENORMALMAPSCROLLBLEND
  1437. uniform highp vec2 NormalmapScrollBlend;
  1438. #endif
  1439. #ifdef USEOCCLUDE
  1440. uniform occludeQuery {
  1441.     uint visiblepixels;
  1442.     uint allpixels;
  1443. };
  1444. #endif
  1445. void main(void)
  1446. {
  1447. #ifdef USEOFFSETMAPPING
  1448.     // apply offsetmapping
  1449.     vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy);
  1450.     vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy);
  1451.     vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy);
  1452. # define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy)
  1453. # define TexCoord TexCoordOffset
  1454. #else
  1455. # define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy)
  1456. # define TexCoord TexCoordSurfaceLightmap.xy
  1457. #endif
  1458.  
  1459.     // combine the diffuse textures (base, pants, shirt)
  1460.     myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color));
  1461. #ifdef USEALPHAKILL
  1462.     if (color.a < 0.5)
  1463.         discard;
  1464. #endif
  1465.     color.a *= Alpha;
  1466. #ifdef USECOLORMAPPING
  1467.     color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt;
  1468. #endif
  1469. #ifdef USEVERTEXTEXTUREBLEND
  1470. #ifdef USEBOTHALPHAS
  1471.     myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2));
  1472.     myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0));
  1473.     color.rgb = mix(color2.rgb, color.rgb, terrainblend);
  1474. #else
  1475.     myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0));
  1476.     //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0));
  1477.     //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5;
  1478.     color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend);
  1479. #endif
  1480.     color.a = 1.0;
  1481.     //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend);
  1482. #endif
  1483. #ifdef USEALPHAGENVERTEX
  1484.     color.a *= VertexColor.a;
  1485. #endif
  1486.  
  1487.     // get the surface normal
  1488. #ifdef USEVERTEXTEXTUREBLEND
  1489.     myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5));
  1490. #else
  1491.     myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5));
  1492. #endif
  1493.  
  1494.     // get the material colors
  1495.     myhalf3 diffusetex = color.rgb;
  1496. #if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)
  1497. # ifdef USEVERTEXTEXTUREBLEND
  1498.     myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend);
  1499. # else
  1500.     myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss));
  1501. # endif
  1502. #endif
  1503.  
  1504. #ifdef USEREFLECTCUBE
  1505.     vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal);
  1506.     vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz;
  1507.     vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0));
  1508.     diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord));
  1509. #endif
  1510.  
  1511. #ifdef USESPECULAR
  1512.     myhalf3 eyenormal = normalize(cast_myhalf3(EyeVectorFogDepth.xyz));
  1513. #endif
  1514.  
  1515.  
  1516.  
  1517.  
  1518. #ifdef MODE_LIGHTSOURCE
  1519.     // light source
  1520. #ifdef USEDIFFUSE
  1521.     myhalf3 lightnormal = cast_myhalf3(normalize(LightVector));
  1522. SHADEDIFFUSE
  1523.     color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);
  1524. #ifdef USESPECULAR
  1525. SHADESPECULAR(SpecularPower * glosstex.a)
  1526.     color.rgb += glosstex.rgb * (specular * Color_Specular);
  1527. #endif
  1528. #else
  1529.     color.rgb = diffusetex * Color_Ambient;
  1530. #endif
  1531.     color.rgb *= LightColor;
  1532.     color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));
  1533. #if defined(USESHADOWMAP2D)
  1534.     color.rgb *= ShadowMapCompare(CubeVector);
  1535. #endif
  1536. # ifdef USECUBEFILTER
  1537.     color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector));
  1538. # endif
  1539. #endif // MODE_LIGHTSOURCE
  1540.  
  1541.  
  1542.  
  1543.  
  1544. #ifdef MODE_LIGHTDIRECTION
  1545.     #define SHADING
  1546.     #ifdef USEDIFFUSE
  1547.         myhalf3 lightnormal = cast_myhalf3(normalize(LightVector));
  1548.     #endif
  1549.     #define lightcolor LightColor
  1550. #endif // MODE_LIGHTDIRECTION
  1551. #ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE
  1552.    #define SHADING
  1553.     // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe)
  1554.     myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0);
  1555.     myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw));
  1556.     // convert modelspace light vector to tangentspace
  1557.     myhalf3 lightnormal;
  1558.     lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS));
  1559.     lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT));
  1560.     lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR));
  1561.     lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this
  1562.     // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)
  1563.     // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar
  1564.     // is used (the lightmap and deluxemap coords correspond to virtually random coordinates
  1565.     // on that luxel, and NOT to its center, because recursive triangle subdivision is used
  1566.     // to map the luxels to coordinates on the draw surfaces), which also causes
  1567.     // deluxemaps to be wrong because light contributions from the wrong side of the surface
  1568.     // are added up. To prevent divisions by zero or strong exaggerations, a max()
  1569.     // nudge is done here at expense of some additional fps. This is ONLY needed for
  1570.     // deluxemaps, tangentspace deluxemap avoid this problem by design.
  1571.     lightcolor *= 1.0 / max(0.25, lightnormal.z);
  1572. #endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE
  1573. #ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE
  1574.    #define SHADING
  1575.     // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)
  1576.     myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0);
  1577.     myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw));
  1578. #endif
  1579. #if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR)
  1580.     #define SHADING
  1581.     // forced deluxemap on lightmapped/vertexlit surfaces
  1582.     myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0);
  1583.    #ifdef USELIGHTMAP
  1584.         myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw));
  1585.    #else
  1586.         myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb);
  1587.    #endif
  1588. #endif
  1589. #ifdef MODE_FAKELIGHT
  1590.     #define SHADING
  1591.     myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz));
  1592.     myhalf3 lightcolor = cast_myhalf3(1.0);
  1593. #endif // MODE_FAKELIGHT
  1594.  
  1595.  
  1596.  
  1597.  
  1598. #ifdef MODE_LIGHTMAP
  1599.     color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse);
  1600. #endif // MODE_LIGHTMAP
  1601. #ifdef MODE_VERTEXCOLOR
  1602.     color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse);
  1603. #endif // MODE_VERTEXCOLOR
  1604. #ifdef MODE_FLATCOLOR
  1605.     color.rgb = diffusetex * Color_Ambient;
  1606. #endif // MODE_FLATCOLOR
  1607.  
  1608.  
  1609.  
  1610.  
  1611. #ifdef SHADING
  1612. # ifdef USEDIFFUSE
  1613. SHADEDIFFUSE
  1614. #  ifdef USESPECULAR
  1615. SHADESPECULAR(SpecularPower * glosstex.a)
  1616.     color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor;
  1617. #  else
  1618.     color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor);
  1619. #  endif
  1620. # else
  1621.     color.rgb = diffusetex * Color_Ambient;
  1622. # endif
  1623. #endif
  1624.  
  1625. #ifdef USESHADOWMAPORTHO
  1626.     color.rgb *= ShadowMapCompare(ShadowMapTC);
  1627. #endif
  1628.  
  1629. #ifdef USEDEFERREDLIGHTMAP
  1630.     vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;
  1631.     color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse;
  1632.     color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular;
  1633. //  color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001);
  1634. #endif
  1635.  
  1636. #ifdef USEBOUNCEGRID
  1637. #ifdef USEBOUNCEGRIDDIRECTIONAL
  1638. //  myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord                        ));
  1639. //  myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0);
  1640.     myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250)));
  1641.     myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375)));
  1642.     myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500)));
  1643.     myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625)));
  1644.     myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750)));
  1645.     myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875)));
  1646.     myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz));
  1647.     myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir);
  1648.     myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir);
  1649. //  bouncegrid_dirp  = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0);
  1650.     myhalf3 bouncegrid_light = cast_myhalf3(
  1651.         dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn),
  1652.         dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn),
  1653.         dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn));
  1654.     color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity;
  1655. //  color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5);
  1656. #else
  1657.     color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity;
  1658. #endif
  1659. #endif
  1660.  
  1661. #ifdef USEGLOW
  1662. #ifdef USEVERTEXTEXTUREBLEND
  1663.     color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow;
  1664. #else
  1665.     color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow;
  1666. #endif
  1667. #endif
  1668.  
  1669. #ifdef USECELOUTLINES
  1670. # ifdef USEDEFERREDLIGHTMAP
  1671. //  vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;
  1672.     vec4 ScreenTexCoordStep = vec4(PixelToScreenTexCoord.x, 0.0, 0.0, PixelToScreenTexCoord.y);
  1673.     vec4 DepthNeighbors;
  1674.  
  1675.     // enable to test ink on white geometry
  1676. //  color.rgb = vec3(1.0, 1.0, 1.0);
  1677.  
  1678.     // note: this seems to be negative
  1679.     float DepthCenter = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).b;
  1680.  
  1681.     // edge detect method
  1682. //  DepthNeighbors.x = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord - ScreenTexCoordStep.xy).b;
  1683. //  DepthNeighbors.y = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + ScreenTexCoordStep.xy).b;
  1684. //  DepthNeighbors.z = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + ScreenTexCoordStep.zw).b;
  1685. //  DepthNeighbors.w = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord - ScreenTexCoordStep.zw).b;
  1686. //  float DepthAverage = dot(DepthNeighbors, vec4(0.25, 0.25, 0.25, 0.25));
  1687. //  float DepthDelta = abs(dot(DepthNeighbors.xy, vec2(-1.0, 1.0))) + abs(dot(DepthNeighbors.zw, vec2(-1.0, 1.0)));
  1688. //  color.rgb *= max(0.5, 1.0 - max(0.0, abs(DepthCenter - DepthAverage) - 0.2 * DepthDelta) / (0.01 + 0.2 * DepthDelta));
  1689. //  color.rgb *= step(abs(DepthCenter - DepthAverage), 0.2 * DepthDelta);
  1690.  
  1691.     // shadow method
  1692.     float DepthScale1 = 4.0 / DepthCenter; // inner ink (shadow on object)
  1693. //  float DepthScale1 = -4.0 / DepthCenter; // outer ink (shadow around object)
  1694. //  float DepthScale1 = 0.003;
  1695.     float DepthScale2 = DepthScale1 / 2.0;
  1696. //  float DepthScale3 = DepthScale1 / 4.0;
  1697.     float DepthBias1 = -DepthCenter * DepthScale1;
  1698.     float DepthBias2 = -DepthCenter * DepthScale2;
  1699. //  float DepthBias3 = -DepthCenter * DepthScale3;
  1700.     float DepthShadow = max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2(-1.0,  0.0)).b * DepthScale1 + DepthBias1)
  1701.                       + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 1.0,  0.0)).b * DepthScale1 + DepthBias1)
  1702.                       + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0, -1.0)).b * DepthScale1 + DepthBias1)
  1703.                       + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0,  1.0)).b * DepthScale1 + DepthBias1)
  1704.                       + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2(-2.0,  0.0)).b * DepthScale2 + DepthBias2)
  1705.                       + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 2.0,  0.0)).b * DepthScale2 + DepthBias2)
  1706.                       + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0, -2.0)).b * DepthScale2 + DepthBias2)
  1707.                       + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0,  2.0)).b * DepthScale2 + DepthBias2)
  1708. //                    + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2(-3.0,  0.0)).b * DepthScale3 + DepthBias3)
  1709. //                    + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 3.0,  0.0)).b * DepthScale3 + DepthBias3)
  1710. //                    + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0, -3.0)).b * DepthScale3 + DepthBias3)
  1711. //                    + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0,  3.0)).b * DepthScale3 + DepthBias3)
  1712.                       - 0.0;
  1713.     color.rgb *= 1.0 - max(0.0, min(DepthShadow, 1.0));
  1714. //  color.r = DepthCenter / -1024.0;
  1715. # endif
  1716. #endif
  1717.  
  1718. #ifdef USEFOG
  1719.     color.rgb = FogVertex(color);
  1720. #endif
  1721.  
  1722.     // reflection must come last because it already contains exactly the correct fog (the reflection render preserves camera distance from the plane, it only flips the side) and ContrastBoost/SceneBrightness
  1723. #ifdef USEREFLECTION
  1724.     vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);
  1725.     //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;
  1726.     vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;
  1727.     #ifdef USENORMALMAPSCROLLBLEND
  1728. # ifdef USEOFFSETMAPPING
  1729.         vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0);
  1730. # else
  1731.         vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);
  1732. # endif
  1733.         normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;
  1734.         vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw;
  1735.     #else
  1736.         vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw;
  1737.     #endif
  1738.     // FIXME temporary hack to detect the case that the reflection
  1739.     // gets blackened at edges due to leaving the area that contains actual
  1740.     // content.
  1741.     // Remove this 'ack once we have a better way to stop this thing from
  1742.     // 'appening.
  1743.     float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);
  1744.     f      *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);
  1745.     f      *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);
  1746.     f      *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);
  1747.     ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);
  1748.     color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a);
  1749. #endif
  1750. #ifdef USEOCCLUDE
  1751.    color.rgb *= clamp(float(visiblepixels) / float(allpixels), 0.0, 1.0);
  1752. #endif
  1753.  
  1754.     dp_FragColor = vec4(color);
  1755. }
  1756. #endif // FRAGMENT_SHADER
  1757.  
  1758. #endif // !MODE_DEFERREDLIGHTSOURCE
  1759. #endif // !MODE_DEFERREDGEOMETRY
  1760. #endif // !MODE_WATER
  1761. #endif // !MODE_REFRACTION
  1762. #endif // !MODE_BLOOMBLUR
  1763. #endif // !MODE_GENERIC
  1764. #endif // !MODE_POSTPROCESS
  1765. #endif // !MODE_DEPTH_OR_SHADOW
Add Comment
Please, Sign In to add comment