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