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