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