]> git.xonotic.org Git - xonotic/darkplaces.git/blob - shader_glsl.h
add a hack to support fog on premultiplied alpha surfaces too
[xonotic/darkplaces.git] / shader_glsl.h
1 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
2 "// written by Forest 'LordHavoc' Hale\n"
3 "// shadowmapping enhancements by Lee 'eihrul' Salzman\n"
4 "\n"
5 "// GL ES shaders use precision modifiers, standard GL does not\n"
6 "#ifndef GL_ES\n"
7 "#define lowp\n"
8 "#define mediump\n"
9 "#define highp\n"
10 "#endif\n"
11 "\n"
12 "#ifdef VERTEX_SHADER\n"
13 "attribute vec4 Attrib_Position;  // vertex\n"
14 "attribute vec4 Attrib_Color;     // color\n"
15 "attribute vec4 Attrib_TexCoord0; // material texcoords\n"
16 "attribute vec3 Attrib_TexCoord1; // svector\n"
17 "attribute vec3 Attrib_TexCoord2; // tvector\n"
18 "attribute vec3 Attrib_TexCoord3; // normal\n"
19 "attribute vec4 Attrib_TexCoord4; // lightmap texcoords\n"
20 "#endif\n"
21 "varying lowp vec4 VertexColor;\n"
22 "\n"
23 "#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE)\n"
24 "# define USEFOG\n"
25 "#endif\n"
26 "#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
27 "#define USELIGHTMAP\n"
28 "#endif\n"
29 "#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT)\n"
30 "#define USEEYEVECTOR\n"
31 "#endif\n"
32 "\n"
33 "#ifdef USESHADOWMAP2D\n"
34 "# ifdef GL_EXT_gpu_shader4\n"
35 "#   extension GL_EXT_gpu_shader4 : enable\n"
36 "# endif\n"
37 "# ifdef GL_ARB_texture_gather\n"
38 "#   extension GL_ARB_texture_gather : enable\n"
39 "# else\n"
40 "#   ifdef GL_AMD_texture_texture4\n"
41 "#     extension GL_AMD_texture_texture4 : enable\n"
42 "#   endif\n"
43 "# endif\n"
44 "#endif\n"
45 "\n"
46 "//#ifdef USESHADOWSAMPLER\n"
47 "//# extension GL_ARB_shadow : enable\n"
48 "//#endif\n"
49 "\n"
50 "//#ifdef __GLSL_CG_DATA_TYPES\n"
51 "//# define myhalf half\n"
52 "//# define myhalf2 half2\n"
53 "//# define myhalf3 half3\n"
54 "//# define myhalf4 half4\n"
55 "//#else\n"
56 "# define myhalf mediump float\n"
57 "# define myhalf2 mediump vec2\n"
58 "# define myhalf3 mediump vec3\n"
59 "# define myhalf4 mediump vec4\n"
60 "//#endif\n"
61 "\n"
62 "#ifdef VERTEX_SHADER\n"
63 "uniform highp mat4 ModelViewProjectionMatrix;\n"
64 "#endif\n"
65 "\n"
66 "#ifdef MODE_DEPTH_OR_SHADOW\n"
67 "#ifdef VERTEX_SHADER\n"
68 "void main(void)\n"
69 "{\n"
70 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
71 "}\n"
72 "#endif\n"
73 "#else // !MODE_DEPTH_ORSHADOW\n"
74 "\n"
75 "\n"
76 "\n"
77 "\n"
78 "#ifdef MODE_SHOWDEPTH\n"
79 "#ifdef VERTEX_SHADER\n"
80 "void main(void)\n"
81 "{\n"
82 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
83 "       VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0);\n"
84 "}\n"
85 "#endif\n"
86 "\n"
87 "#ifdef FRAGMENT_SHADER\n"
88 "void main(void)\n"
89 "{\n"
90 "       gl_FragColor = VertexColor;\n"
91 "}\n"
92 "#endif\n"
93 "#else // !MODE_SHOWDEPTH\n"
94 "\n"
95 "\n"
96 "\n"
97 "\n"
98 "#ifdef MODE_POSTPROCESS\n"
99 "varying mediump vec2 TexCoord1;\n"
100 "varying mediump vec2 TexCoord2;\n"
101 "\n"
102 "#ifdef VERTEX_SHADER\n"
103 "void main(void)\n"
104 "{\n"
105 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
106 "       TexCoord1 = Attrib_TexCoord0.xy;\n"
107 "#ifdef USEBLOOM\n"
108 "       TexCoord2 = Attrib_TexCoord4.xy;\n"
109 "#endif\n"
110 "}\n"
111 "#endif\n"
112 "\n"
113 "#ifdef FRAGMENT_SHADER\n"
114 "uniform sampler2D Texture_First;\n"
115 "#ifdef USEBLOOM\n"
116 "uniform sampler2D Texture_Second;\n"
117 "uniform lowp vec4 BloomColorSubtract;\n"
118 "#endif\n"
119 "#ifdef USEGAMMARAMPS\n"
120 "uniform sampler2D Texture_GammaRamps;\n"
121 "#endif\n"
122 "#ifdef USESATURATION\n"
123 "uniform lowp float Saturation;\n"
124 "#endif\n"
125 "#ifdef USEVIEWTINT\n"
126 "uniform lowp vec4 ViewTintColor;\n"
127 "#endif\n"
128 "//uncomment these if you want to use them:\n"
129 "uniform mediump vec4 UserVec1;\n"
130 "uniform mediump vec4 UserVec2;\n"
131 "// uniform mediump vec4 UserVec3;\n"
132 "// uniform mediump vec4 UserVec4;\n"
133 "// uniform highp float ClientTime;\n"
134 "uniform mediump vec2 PixelSize;\n"
135 "void main(void)\n"
136 "{\n"
137 "       gl_FragColor = texture2D(Texture_First, TexCoord1);\n"
138 "#ifdef USEBLOOM\n"
139 "       gl_FragColor += max(vec4(0,0,0,0), texture2D(Texture_Second, TexCoord2) - BloomColorSubtract);\n"
140 "#endif\n"
141 "#ifdef USEVIEWTINT\n"
142 "       gl_FragColor = mix(gl_FragColor, ViewTintColor, ViewTintColor.a);\n"
143 "#endif\n"
144 "\n"
145 "#ifdef USEPOSTPROCESSING\n"
146 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n"
147 "// 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"
148 "       float sobel = 1.0;\n"
149 "       // vec2 ts = textureSize(Texture_First, 0);\n"
150 "       // vec2 px = vec2(1/ts.x, 1/ts.y);\n"
151 "       vec2 px = PixelSize;\n"
152 "       vec3 x1 = texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb;\n"
153 "       vec3 x2 = texture2D(Texture_First, TexCoord1 + vec2(-px.x,  0.0)).rgb;\n"
154 "       vec3 x3 = texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb;\n"
155 "       vec3 x4 = texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb;\n"
156 "       vec3 x5 = texture2D(Texture_First, TexCoord1 + vec2( px.x,  0.0)).rgb;\n"
157 "       vec3 x6 = texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb;\n"
158 "       vec3 y1 = texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb;\n"
159 "       vec3 y2 = texture2D(Texture_First, TexCoord1 + vec2(  0.0,-px.y)).rgb;\n"
160 "       vec3 y3 = texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb;\n"
161 "       vec3 y4 = texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb;\n"
162 "       vec3 y5 = texture2D(Texture_First, TexCoord1 + vec2(  0.0, px.y)).rgb;\n"
163 "       vec3 y6 = texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb;\n"
164 "       float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1);\n"
165 "       float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2);\n"
166 "       float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3);\n"
167 "       float px4 =  1.0 * dot(vec3(0.3, 0.59, 0.11), x4);\n"
168 "       float px5 =  2.0 * dot(vec3(0.3, 0.59, 0.11), x5);\n"
169 "       float px6 =  1.0 * dot(vec3(0.3, 0.59, 0.11), x6);\n"
170 "       float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1);\n"
171 "       float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2);\n"
172 "       float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3);\n"
173 "       float py4 =  1.0 * dot(vec3(0.3, 0.59, 0.11), y4);\n"
174 "       float py5 =  2.0 * dot(vec3(0.3, 0.59, 0.11), y5);\n"
175 "       float py6 =  1.0 * dot(vec3(0.3, 0.59, 0.11), y6);\n"
176 "       sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6);\n"
177 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y;\n"
178 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y;\n"
179 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y;\n"
180 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107,  0.707107)) * UserVec1.y;\n"
181 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990,  0.891007)) * UserVec1.y;\n"
182 "       gl_FragColor /= (1.0 + 5.0 * UserVec1.y);\n"
183 "       gl_FragColor.rgb = gl_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y;\n"
184 "#endif\n"
185 "\n"
186 "#ifdef USESATURATION\n"
187 "       //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter\n"
188 "       float y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114));\n"
189 "       // 'vampire sight' effect, wheres red is compensated\n"
190 "       #ifdef SATURATION_REDCOMPENSATE\n"
191 "               float rboost = max(0.0, (gl_FragColor.r - max(gl_FragColor.g, gl_FragColor.b))*(1.0 - Saturation));\n"
192 "               gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation);\n"
193 "               gl_FragColor.r += rboost;\n"
194 "       #else\n"
195 "               // normal desaturation\n"
196 "               //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation;\n"
197 "               gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation);\n"
198 "       #endif\n"
199 "#endif\n"
200 "\n"
201 "#ifdef USEGAMMARAMPS\n"
202 "       gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r;\n"
203 "       gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g;\n"
204 "       gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b;\n"
205 "#endif\n"
206 "}\n"
207 "#endif\n"
208 "#else // !MODE_POSTPROCESS\n"
209 "\n"
210 "\n"
211 "\n"
212 "\n"
213 "#ifdef MODE_GENERIC\n"
214 "#ifdef USEDIFFUSE\n"
215 "varying mediump vec2 TexCoord1;\n"
216 "#endif\n"
217 "#ifdef USESPECULAR\n"
218 "varying mediump vec2 TexCoord2;\n"
219 "#endif\n"
220 "#ifdef VERTEX_SHADER\n"
221 "void main(void)\n"
222 "{\n"
223 "       VertexColor = Attrib_Color;\n"
224 "#ifdef USEDIFFUSE\n"
225 "       TexCoord1 = Attrib_TexCoord0.xy;\n"
226 "#endif\n"
227 "#ifdef USESPECULAR\n"
228 "       TexCoord2 = Attrib_TexCoord1.xy;\n"
229 "#endif\n"
230 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
231 "}\n"
232 "#endif\n"
233 "\n"
234 "#ifdef FRAGMENT_SHADER\n"
235 "#ifdef USEDIFFUSE\n"
236 "uniform sampler2D Texture_First;\n"
237 "#endif\n"
238 "#ifdef USESPECULAR\n"
239 "uniform sampler2D Texture_Second;\n"
240 "#endif\n"
241 "\n"
242 "void main(void)\n"
243 "{\n"
244 "#ifdef USEVIEWTINT\n"
245 "       gl_FragColor = VertexColor;\n"
246 "#else\n"
247 "       gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
248 "#endif\n"
249 "#ifdef USEDIFFUSE\n"
250 "       gl_FragColor *= texture2D(Texture_First, TexCoord1);\n"
251 "#endif\n"
252 "\n"
253 "#ifdef USESPECULAR\n"
254 "       vec4 tex2 = texture2D(Texture_Second, TexCoord2);\n"
255 "# ifdef USECOLORMAPPING\n"
256 "       gl_FragColor *= tex2;\n"
257 "# endif\n"
258 "# ifdef USEGLOW\n"
259 "       gl_FragColor += tex2;\n"
260 "# endif\n"
261 "# ifdef USEVERTEXTEXTUREBLEND\n"
262 "       gl_FragColor = mix(gl_FragColor, tex2, tex2.a);\n"
263 "# endif\n"
264 "#endif\n"
265 "}\n"
266 "#endif\n"
267 "#else // !MODE_GENERIC\n"
268 "\n"
269 "\n"
270 "\n"
271 "\n"
272 "#ifdef MODE_BLOOMBLUR\n"
273 "varying mediump vec2 TexCoord;\n"
274 "#ifdef VERTEX_SHADER\n"
275 "void main(void)\n"
276 "{\n"
277 "       VertexColor = Attrib_Color;\n"
278 "       TexCoord = Attrib_TexCoord0.xy;\n"
279 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
280 "}\n"
281 "#endif\n"
282 "\n"
283 "#ifdef FRAGMENT_SHADER\n"
284 "uniform sampler2D Texture_First;\n"
285 "uniform mediump vec4 BloomBlur_Parameters;\n"
286 "\n"
287 "void main(void)\n"
288 "{\n"
289 "       int i;\n"
290 "       vec2 tc = TexCoord;\n"
291 "       vec3 color = texture2D(Texture_First, tc).rgb;\n"
292 "       tc += BloomBlur_Parameters.xy;\n"
293 "       for (i = 1;i < SAMPLES;i++)\n"
294 "       {\n"
295 "               color += texture2D(Texture_First, tc).rgb;\n"
296 "               tc += BloomBlur_Parameters.xy;\n"
297 "       }\n"
298 "       gl_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1);\n"
299 "}\n"
300 "#endif\n"
301 "#else // !MODE_BLOOMBLUR\n"
302 "#ifdef MODE_REFRACTION\n"
303 "varying mediump vec2 TexCoord;\n"
304 "varying highp vec4 ModelViewProjectionPosition;\n"
305 "uniform highp mat4 TexMatrix;\n"
306 "#ifdef VERTEX_SHADER\n"
307 "\n"
308 "void main(void)\n"
309 "{\n"
310 "       TexCoord = vec2(TexMatrix * Attrib_TexCoord0);\n"
311 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
312 "       ModelViewProjectionPosition = gl_Position;\n"
313 "}\n"
314 "#endif\n"
315 "\n"
316 "#ifdef FRAGMENT_SHADER\n"
317 "uniform sampler2D Texture_Normal;\n"
318 "uniform sampler2D Texture_Refraction;\n"
319 "uniform sampler2D Texture_Reflection;\n"
320 "\n"
321 "uniform mediump vec4 DistortScaleRefractReflect;\n"
322 "uniform mediump vec4 ScreenScaleRefractReflect;\n"
323 "uniform mediump vec4 ScreenCenterRefractReflect;\n"
324 "uniform lowp vec4 RefractColor;\n"
325 "uniform lowp vec4 ReflectColor;\n"
326 "uniform mediump float ReflectFactor;\n"
327 "uniform mediump float ReflectOffset;\n"
328 "\n"
329 "void main(void)\n"
330 "{\n"
331 "       vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
332 "       //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
333 "       vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
334 "       vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xy * DistortScaleRefractReflect.xy;\n"
335 "       // FIXME temporary hack to detect the case that the reflection\n"
336 "       // gets blackened at edges due to leaving the area that contains actual\n"
337 "       // content.\n"
338 "       // Remove this 'ack once we have a better way to stop this thing from\n"
339 "       // 'appening.\n"
340 "       float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
341 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
342 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
343 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
344 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
345 "       gl_FragColor = vec4(texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * RefractColor;\n"
346 "}\n"
347 "#endif\n"
348 "#else // !MODE_REFRACTION\n"
349 "\n"
350 "\n"
351 "\n"
352 "\n"
353 "#ifdef MODE_WATER\n"
354 "varying mediump vec2 TexCoord;\n"
355 "varying highp vec3 EyeVector;\n"
356 "varying highp vec4 ModelViewProjectionPosition;\n"
357 "#ifdef VERTEX_SHADER\n"
358 "uniform highp vec3 EyePosition;\n"
359 "uniform highp mat4 TexMatrix;\n"
360 "\n"
361 "void main(void)\n"
362 "{\n"
363 "       TexCoord = vec2(TexMatrix * Attrib_TexCoord0);\n"
364 "       vec3 EyeVectorModelSpace = EyePosition - Attrib_Position.xyz;\n"
365 "       EyeVector.x = dot(EyeVectorModelSpace, Attrib_TexCoord1.xyz);\n"
366 "       EyeVector.y = dot(EyeVectorModelSpace, Attrib_TexCoord2.xyz);\n"
367 "       EyeVector.z = dot(EyeVectorModelSpace, Attrib_TexCoord3.xyz);\n"
368 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
369 "       ModelViewProjectionPosition = gl_Position;\n"
370 "}\n"
371 "#endif\n"
372 "\n"
373 "#ifdef FRAGMENT_SHADER\n"
374 "uniform sampler2D Texture_Normal;\n"
375 "uniform sampler2D Texture_Refraction;\n"
376 "uniform sampler2D Texture_Reflection;\n"
377 "\n"
378 "uniform mediump vec4 DistortScaleRefractReflect;\n"
379 "uniform mediump vec4 ScreenScaleRefractReflect;\n"
380 "uniform mediump vec4 ScreenCenterRefractReflect;\n"
381 "uniform lowp vec4 RefractColor;\n"
382 "uniform lowp vec4 ReflectColor;\n"
383 "uniform mediump float ReflectFactor;\n"
384 "uniform mediump float ReflectOffset;\n"
385 "uniform highp float ClientTime;\n"
386 "#ifdef USENORMALMAPSCROLLBLEND\n"
387 "uniform highp vec2 NormalmapScrollBlend;\n"
388 "#endif\n"
389 "\n"
390 "void main(void)\n"
391 "{\n"
392 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
393 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
394 "       vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
395 "       //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0);\n"
396 "       // slight water animation via 2 layer scrolling (todo: tweak)\n"
397 "       #ifdef USENORMALMAPSCROLLBLEND\n"
398 "               vec3 normal = texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);\n"
399 "               normal += texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;\n"
400 "               vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * DistortScaleRefractReflect;\n"
401 "       #else\n"
402 "               vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
403 "       #endif\n"
404 "       // FIXME temporary hack to detect the case that the reflection\n"
405 "       // gets blackened at edges due to leaving the area that contains actual\n"
406 "       // content.\n"
407 "       // Remove this 'ack once we have a better way to stop this thing from\n"
408 "       // 'appening.\n"
409 "       float f  = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002);\n"
410 "       f       *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002);\n"
411 "       f       *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002);\n"
412 "       f       *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002);\n"
413 "       ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);\n"
414 "       f  = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002);\n"
415 "       f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002);\n"
416 "       f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002);\n"
417 "       f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002);\n"
418 "       ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);\n"
419 "       float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n"
420 "       gl_FragColor = mix(vec4(texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * RefractColor, vec4(texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);\n"
421 "}\n"
422 "#endif\n"
423 "#else // !MODE_WATER\n"
424 "\n"
425 "\n"
426 "\n"
427 "\n"
428 "// common definitions between vertex shader and fragment shader:\n"
429 "\n"
430 "varying mediump vec2 TexCoord;\n"
431 "#ifdef USEVERTEXTEXTUREBLEND\n"
432 "varying mediump vec2 TexCoord2;\n"
433 "#endif\n"
434 "#ifdef USELIGHTMAP\n"
435 "varying mediump vec2 TexCoordLightmap;\n"
436 "#endif\n"
437 "\n"
438 "#ifdef MODE_LIGHTSOURCE\n"
439 "varying mediump vec3 CubeVector;\n"
440 "#endif\n"
441 "\n"
442 "#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE)\n"
443 "varying mediump vec3 LightVector;\n"
444 "#endif\n"
445 "\n"
446 "#ifdef USEEYEVECTOR\n"
447 "varying highp vec3 EyeVector;\n"
448 "#endif\n"
449 "#ifdef USEFOG\n"
450 "varying highp vec4 EyeVectorModelSpaceFogPlaneVertexDist;\n"
451 "#endif\n"
452 "\n"
453 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE)\n"
454 "varying lowp vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent)\n"
455 "varying lowp vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal)\n"
456 "varying lowp vec3 VectorR; // direction of R texcoord (surface normal)\n"
457 "#endif\n"
458 "\n"
459 "#ifdef USEREFLECTION\n"
460 "varying highp vec4 ModelViewProjectionPosition;\n"
461 "#endif\n"
462 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
463 "uniform highp vec3 LightPosition;\n"
464 "varying highp vec4 ModelViewPosition;\n"
465 "#endif\n"
466 "\n"
467 "#ifdef MODE_LIGHTSOURCE\n"
468 "uniform highp vec3 LightPosition;\n"
469 "#endif\n"
470 "uniform highp vec3 EyePosition;\n"
471 "#ifdef MODE_LIGHTDIRECTION\n"
472 "uniform highp vec3 LightDir;\n"
473 "#endif\n"
474 "uniform highp vec4 FogPlane;\n"
475 "\n"
476 "#ifdef USESHADOWMAPORTHO\n"
477 "varying mediump vec3 ShadowMapTC;\n"
478 "#endif\n"
479 "\n"
480 "#ifdef USEBOUNCEGRID\n"
481 "varying mediump vec3 BounceGridTexCoord;\n"
482 "#endif\n"
483 "\n"
484 "\n"
485 "\n"
486 "\n"
487 "\n"
488 "\n"
489 "// 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"
490 "\n"
491 "// fragment shader specific:\n"
492 "#ifdef FRAGMENT_SHADER\n"
493 "\n"
494 "uniform sampler2D Texture_Normal;\n"
495 "uniform sampler2D Texture_Color;\n"
496 "uniform sampler2D Texture_Gloss;\n"
497 "#ifdef USEGLOW\n"
498 "uniform sampler2D Texture_Glow;\n"
499 "#endif\n"
500 "#ifdef USEVERTEXTEXTUREBLEND\n"
501 "uniform sampler2D Texture_SecondaryNormal;\n"
502 "uniform sampler2D Texture_SecondaryColor;\n"
503 "uniform sampler2D Texture_SecondaryGloss;\n"
504 "#ifdef USEGLOW\n"
505 "uniform sampler2D Texture_SecondaryGlow;\n"
506 "#endif\n"
507 "#endif\n"
508 "#ifdef USECOLORMAPPING\n"
509 "uniform sampler2D Texture_Pants;\n"
510 "uniform sampler2D Texture_Shirt;\n"
511 "#endif\n"
512 "#ifdef USEFOG\n"
513 "#ifdef USEFOGHEIGHTTEXTURE\n"
514 "uniform sampler2D Texture_FogHeightTexture;\n"
515 "#endif\n"
516 "uniform sampler2D Texture_FogMask;\n"
517 "#endif\n"
518 "#ifdef USELIGHTMAP\n"
519 "uniform sampler2D Texture_Lightmap;\n"
520 "#endif\n"
521 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
522 "uniform sampler2D Texture_Deluxemap;\n"
523 "#endif\n"
524 "#ifdef USEREFLECTION\n"
525 "uniform sampler2D Texture_Reflection;\n"
526 "#endif\n"
527 "\n"
528 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
529 "uniform sampler2D Texture_ScreenDepth;\n"
530 "uniform sampler2D Texture_ScreenNormalMap;\n"
531 "#endif\n"
532 "#ifdef USEDEFERREDLIGHTMAP\n"
533 "uniform sampler2D Texture_ScreenDiffuse;\n"
534 "uniform sampler2D Texture_ScreenSpecular;\n"
535 "#endif\n"
536 "\n"
537 "uniform lowp vec3 Color_Pants;\n"
538 "uniform lowp vec3 Color_Shirt;\n"
539 "uniform lowp vec3 FogColor;\n"
540 "\n"
541 "#ifdef USEFOG\n"
542 "uniform highp float FogRangeRecip;\n"
543 "uniform highp float FogPlaneViewDist;\n"
544 "uniform highp float FogHeightFade;\n"
545 "vec3 FogVertex(vec4 surfacecolor)\n"
546 "{\n"
547 "       vec3 EyeVectorModelSpace = EyeVectorModelSpaceFogPlaneVertexDist.xyz;\n"
548 "       float FogPlaneVertexDist = EyeVectorModelSpaceFogPlaneVertexDist.w;\n"
549 "       float fogfrac;\n"
550 "       vec3 fc = FogColor;\n"
551 "#ifdef USEFOGALPHAHACK\n"
552 "       fc *= surfacecolor.a;\n"
553 "#endif\n"
554 "#ifdef USEFOGHEIGHTTEXTURE\n"
555 "       vec4 fogheightpixel = texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade));\n"
556 "       fogfrac = fogheightpixel.a;\n"
557 "       return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);\n"
558 "#else\n"
559 "# ifdef USEFOGOUTSIDE\n"
560 "       fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);\n"
561 "# else\n"
562 "       fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);\n"
563 "# endif\n"
564 "       return mix(fc, surfacecolor.rgb, texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);\n"
565 "#endif\n"
566 "}\n"
567 "#endif\n"
568 "\n"
569 "#ifdef USEOFFSETMAPPING\n"
570 "uniform mediump float OffsetMapping_Scale;\n"
571 "vec2 OffsetMapping(vec2 TexCoord)\n"
572 "{\n"
573 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
574 "       // 14 sample relief mapping: linear search and then binary search\n"
575 "       // this basically steps forward a small amount repeatedly until it finds\n"
576 "       // itself inside solid, then jitters forward and back using decreasing\n"
577 "       // amounts to find the impact\n"
578 "       //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1);\n"
579 "       //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
580 "       vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
581 "       vec3 RT = vec3(TexCoord, 1);\n"
582 "       OffsetVector *= 0.1;\n"
583 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
584 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
585 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
586 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
587 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
588 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
589 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
590 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
591 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
592 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z)          - 0.5);\n"
593 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5    - 0.25);\n"
594 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25   - 0.125);\n"
595 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125  - 0.0625);\n"
596 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
597 "       return RT.xy;\n"
598 "#else\n"
599 "       // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits)\n"
600 "       //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1));\n"
601 "       //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1));\n"
602 "       vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1));\n"
603 "       OffsetVector *= 0.5;\n"
604 "       TexCoord += OffsetVector * (1.0 - texture2D(Texture_Normal, TexCoord).a);\n"
605 "       TexCoord += OffsetVector * (1.0 - texture2D(Texture_Normal, TexCoord).a);\n"
606 "       return TexCoord;\n"
607 "#endif\n"
608 "}\n"
609 "#endif // USEOFFSETMAPPING\n"
610 "\n"
611 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)\n"
612 "uniform sampler2D Texture_Attenuation;\n"
613 "uniform samplerCube Texture_Cube;\n"
614 "#endif\n"
615 "\n"
616 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO)\n"
617 "\n"
618 "#ifdef USESHADOWMAP2D\n"
619 "# ifdef USESHADOWSAMPLER\n"
620 "uniform sampler2DShadow Texture_ShadowMap2D;\n"
621 "# else\n"
622 "uniform sampler2D Texture_ShadowMap2D;\n"
623 "# endif\n"
624 "#endif\n"
625 "\n"
626 "#ifdef USESHADOWMAPVSDCT\n"
627 "uniform samplerCube Texture_CubeProjection;\n"
628 "#endif\n"
629 "\n"
630 "#if defined(USESHADOWMAP2D)\n"
631 "uniform mediump vec2 ShadowMap_TextureScale;\n"
632 "uniform mediump vec4 ShadowMap_Parameters;\n"
633 "#endif\n"
634 "\n"
635 "#if defined(USESHADOWMAP2D)\n"
636 "# ifdef USESHADOWMAPORTHO\n"
637 "#  define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz))\n"
638 "# else\n"
639 "#  ifdef USESHADOWMAPVSDCT\n"
640 "vec3 GetShadowMapTC2D(vec3 dir)\n"
641 "{\n"
642 "       vec3 adir = abs(dir);\n"
643 "       vec2 aparams = ShadowMap_Parameters.xy / max(max(adir.x, adir.y), adir.z);\n"
644 "       vec4 proj = textureCube(Texture_CubeProjection, dir);\n"
645 "       return vec3(mix(dir.xy, dir.zz, proj.xy) * aparams.x + proj.zw * ShadowMap_Parameters.z, aparams.y + ShadowMap_Parameters.w);\n"
646 "}\n"
647 "#  else\n"
648 "vec3 GetShadowMapTC2D(vec3 dir)\n"
649 "{\n"
650 "       vec3 adir = abs(dir);\n"
651 "       float ma = adir.z;\n"
652 "       vec4 proj = vec4(dir, 2.5);\n"
653 "       if (adir.x > ma) { ma = adir.x; proj = vec4(dir.zyx, 0.5); }\n"
654 "       if (adir.y > ma) { ma = adir.y; proj = vec4(dir.xzy, 1.5); }\n"
655 "       vec2 aparams = ShadowMap_Parameters.xy / ma;\n"
656 "       return vec3(proj.xy * aparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, aparams.y + ShadowMap_Parameters.w);\n"
657 "}\n"
658 "#  endif\n"
659 "# endif\n"
660 "#endif // defined(USESHADOWMAP2D)\n"
661 "\n"
662 "# ifdef USESHADOWMAP2D\n"
663 "float ShadowMapCompare(vec3 dir)\n"
664 "{\n"
665 "       vec3 shadowmaptc = GetShadowMapTC2D(dir);\n"
666 "       float f;\n"
667 "\n"
668 "#  ifdef USESHADOWSAMPLER\n"
669 "#    ifdef USESHADOWMAPPCF\n"
670 "#      define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r  \n"
671 "       vec2 center = shadowmaptc.xy*ShadowMap_TextureScale;\n"
672 "       f = dot(vec4(0.25), vec4(texval(-0.4, 1.0), texval(-1.0, -0.4), texval(0.4, -1.0), texval(1.0, 0.4)));\n"
673 "#    else\n"
674 "       f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r;\n"
675 "#    endif\n"
676 "#  else\n"
677 "#    ifdef USESHADOWMAPPCF\n"
678 "#     if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)\n"
679 "#      ifdef GL_ARB_texture_gather\n"
680 "#        define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y))\n"
681 "#      else\n"
682 "#        define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)\n"
683 "#      endif\n"
684 "       vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale;\n"
685 "#      if USESHADOWMAPPCF > 1\n"
686 "   vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0));\n"
687 "   vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0));\n"
688 "   vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0));\n"
689 "   vec4 group4 = step(shadowmaptc.z, texval(-2.0,  0.0));\n"
690 "   vec4 group5 = step(shadowmaptc.z, texval( 0.0,  0.0));\n"
691 "   vec4 group6 = step(shadowmaptc.z, texval( 2.0,  0.0));\n"
692 "   vec4 group7 = step(shadowmaptc.z, texval(-2.0,  2.0));\n"
693 "   vec4 group8 = step(shadowmaptc.z, texval( 0.0,  2.0));\n"
694 "   vec4 group9 = step(shadowmaptc.z, texval( 2.0,  2.0));\n"
695 "       vec4 locols = vec4(group1.ab, group3.ab);\n"
696 "       vec4 hicols = vec4(group7.rg, group9.rg);\n"
697 "       locols.yz += group2.ab;\n"
698 "       hicols.yz += group8.rg;\n"
699 "       vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) +\n"
700 "                               vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) +\n"
701 "                               mix(locols, hicols, offset.y);\n"
702 "       vec4 cols = group5 + vec4(group2.rg, group8.ab);\n"
703 "       cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x);\n"
704 "       f = dot(cols, vec4(1.0/25.0));\n"
705 "#      else\n"
706 "       vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));\n"
707 "       vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));\n"
708 "       vec4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));\n"
709 "       vec4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));\n"
710 "       vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) +\n"
711 "                               mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y);\n"
712 "       f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
713 "#      endif\n"
714 "#     else\n"
715 "#      ifdef GL_EXT_gpu_shader4\n"
716 "#        define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r\n"
717 "#      else\n"
718 "#        define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r  \n"
719 "#      endif\n"
720 "#      if USESHADOWMAPPCF > 1\n"
721 "       vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
722 "       center *= ShadowMap_TextureScale;\n"
723 "       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"
724 "       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"
725 "       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"
726 "       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"
727 "       vec4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
728 "       f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
729 "#      else\n"
730 "       vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy);\n"
731 "       vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
732 "       vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
733 "       vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
734 "       vec3 cols = row2 + mix(row1, row3, offset.y);\n"
735 "       f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));\n"
736 "#      endif\n"
737 "#     endif\n"
738 "#    else\n"
739 "       f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n"
740 "#    endif\n"
741 "#  endif\n"
742 "#  ifdef USESHADOWMAPORTHO\n"
743 "       return mix(ShadowMap_Parameters.w, 1.0, f);\n"
744 "#  else\n"
745 "       return f;\n"
746 "#  endif\n"
747 "}\n"
748 "# endif\n"
749 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO)\n"
750 "#endif // FRAGMENT_SHADER\n"
751 "\n"
752 "\n"
753 "\n"
754 "\n"
755 "#ifdef MODE_DEFERREDGEOMETRY\n"
756 "#ifdef VERTEX_SHADER\n"
757 "uniform highp mat4 TexMatrix;\n"
758 "#ifdef USEVERTEXTEXTUREBLEND\n"
759 "uniform highp mat4 BackgroundTexMatrix;\n"
760 "#endif\n"
761 "uniform highp mat4 ModelViewMatrix;\n"
762 "void main(void)\n"
763 "{\n"
764 "       TexCoord = vec2(TexMatrix * Attrib_TexCoord0);\n"
765 "#ifdef USEVERTEXTEXTUREBLEND\n"
766 "       VertexColor = Attrib_Color;\n"
767 "       TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0);\n"
768 "#endif\n"
769 "\n"
770 "       // transform unnormalized eye direction into tangent space\n"
771 "#ifdef USEOFFSETMAPPING\n"
772 "       vec3 EyeVectorModelSpace = EyePosition - Attrib_Position.xyz;\n"
773 "       EyeVector.x = dot(EyeVectorModelSpace, Attrib_TexCoord1.xyz);\n"
774 "       EyeVector.y = dot(EyeVectorModelSpace, Attrib_TexCoord2.xyz);\n"
775 "       EyeVector.z = dot(EyeVectorModelSpace, Attrib_TexCoord3.xyz);\n"
776 "#endif\n"
777 "\n"
778 "       VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)).xyz;\n"
779 "       VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)).xyz;\n"
780 "       VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)).xyz;\n"
781 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
782 "}\n"
783 "#endif // VERTEX_SHADER\n"
784 "\n"
785 "#ifdef FRAGMENT_SHADER\n"
786 "void main(void)\n"
787 "{\n"
788 "#ifdef USEOFFSETMAPPING\n"
789 "       // apply offsetmapping\n"
790 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
791 "#define TexCoord TexCoordOffset\n"
792 "#endif\n"
793 "\n"
794 "#ifdef USEALPHAKILL\n"
795 "       if (texture2D(Texture_Color, TexCoord).a < 0.5)\n"
796 "               discard;\n"
797 "#endif\n"
798 "\n"
799 "#ifdef USEVERTEXTEXTUREBLEND\n"
800 "       float alpha = texture2D(Texture_Color, TexCoord).a;\n"
801 "       float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0));\n"
802 "       //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0));\n"
803 "       //float terrainblend = float(VertexColor.a) * alpha > 0.5;\n"
804 "#endif\n"
805 "\n"
806 "#ifdef USEVERTEXTEXTUREBLEND\n"
807 "       vec3 surfacenormal = mix(vec3(texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(texture2D(Texture_Normal, TexCoord)), terrainblend) - vec3(0.5, 0.5, 0.5);\n"
808 "       float a = mix(texture2D(Texture_SecondaryGloss, TexCoord2).a, texture2D(Texture_Gloss, TexCoord).a, terrainblend);\n"
809 "#else\n"
810 "       vec3 surfacenormal = vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5, 0.5, 0.5);\n"
811 "       float a = texture2D(Texture_Gloss, TexCoord).a;\n"
812 "#endif\n"
813 "\n"
814 "       gl_FragColor = vec4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR) * 0.5 + vec3(0.5, 0.5, 0.5), a);\n"
815 "}\n"
816 "#endif // FRAGMENT_SHADER\n"
817 "#else // !MODE_DEFERREDGEOMETRY\n"
818 "\n"
819 "\n"
820 "\n"
821 "\n"
822 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
823 "#ifdef VERTEX_SHADER\n"
824 "uniform highp mat4 ModelViewMatrix;\n"
825 "void main(void)\n"
826 "{\n"
827 "       ModelViewPosition = ModelViewMatrix * Attrib_Position;\n"
828 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
829 "}\n"
830 "#endif // VERTEX_SHADER\n"
831 "\n"
832 "#ifdef FRAGMENT_SHADER\n"
833 "uniform highp mat4 ViewToLight;\n"
834 "// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far));\n"
835 "uniform highp vec2 ScreenToDepth;\n"
836 "uniform myhalf3 DeferredColor_Ambient;\n"
837 "uniform myhalf3 DeferredColor_Diffuse;\n"
838 "#ifdef USESPECULAR\n"
839 "uniform myhalf3 DeferredColor_Specular;\n"
840 "uniform myhalf SpecularPower;\n"
841 "#endif\n"
842 "uniform myhalf2 PixelToScreenTexCoord;\n"
843 "void main(void)\n"
844 "{\n"
845 "       // calculate viewspace pixel position\n"
846 "       vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;\n"
847 "       vec3 position;\n"
848 "       position.z = ScreenToDepth.y / (texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x);\n"
849 "       position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);\n"
850 "       // decode viewspace pixel normal\n"
851 "       myhalf4 normalmap = texture2D(Texture_ScreenNormalMap, ScreenTexCoord);\n"
852 "       myhalf3 surfacenormal = normalize(normalmap.rgb - myhalf3(0.5,0.5,0.5));\n"
853 "       // surfacenormal = pixel normal in viewspace\n"
854 "       // LightVector = pixel to light in viewspace\n"
855 "       // CubeVector = position in lightspace\n"
856 "       // eyevector = pixel to view in viewspace\n"
857 "       vec3 CubeVector = vec3(ViewToLight * vec4(position,1));\n"
858 "       myhalf fade = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
859 "#ifdef USEDIFFUSE\n"
860 "       // calculate diffuse shading\n"
861 "       myhalf3 lightnormal = myhalf3(normalize(LightPosition - position));\n"
862 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
863 "#endif\n"
864 "#ifdef USESPECULAR\n"
865 "       // calculate directional shading\n"
866 "       vec3 eyevector = position * -1.0;\n"
867 "#  ifdef USEEXACTSPECULARMATH\n"
868 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), SpecularPower * normalmap.a);\n"
869 "#  else\n"
870 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(eyevector)));\n"
871 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * normalmap.a);\n"
872 "#  endif\n"
873 "#endif\n"
874 "\n"
875 "#if defined(USESHADOWMAP2D)\n"
876 "       fade *= ShadowMapCompare(CubeVector);\n"
877 "#endif\n"
878 "\n"
879 "#ifdef USEDIFFUSE\n"
880 "       gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);\n"
881 "#else\n"
882 "       gl_FragData[0] = vec4(DeferredColor_Ambient * fade, 1.0);\n"
883 "#endif\n"
884 "#ifdef USESPECULAR\n"
885 "       gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0);\n"
886 "#else\n"
887 "       gl_FragData[1] = vec4(0.0, 0.0, 0.0, 1.0);\n"
888 "#endif\n"
889 "\n"
890 "# ifdef USECUBEFILTER\n"
891 "       vec3 cubecolor = textureCube(Texture_Cube, CubeVector).rgb;\n"
892 "       gl_FragData[0].rgb *= cubecolor;\n"
893 "       gl_FragData[1].rgb *= cubecolor;\n"
894 "# endif\n"
895 "}\n"
896 "#endif // FRAGMENT_SHADER\n"
897 "#else // !MODE_DEFERREDLIGHTSOURCE\n"
898 "\n"
899 "\n"
900 "\n"
901 "\n"
902 "#ifdef VERTEX_SHADER\n"
903 "uniform highp mat4 TexMatrix;\n"
904 "#ifdef USEVERTEXTEXTUREBLEND\n"
905 "uniform highp mat4 BackgroundTexMatrix;\n"
906 "#endif\n"
907 "#ifdef MODE_LIGHTSOURCE\n"
908 "uniform highp mat4 ModelToLight;\n"
909 "#endif\n"
910 "#ifdef USESHADOWMAPORTHO\n"
911 "uniform highp mat4 ShadowMapMatrix;\n"
912 "#endif\n"
913 "#ifdef USEBOUNCEGRID\n"
914 "uniform highp mat4 BounceGridMatrix;\n"
915 "#endif\n"
916 "void main(void)\n"
917 "{\n"
918 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND)\n"
919 "       VertexColor = Attrib_Color;\n"
920 "#endif\n"
921 "       // copy the surface texcoord\n"
922 "       TexCoord = vec2(TexMatrix * Attrib_TexCoord0);\n"
923 "#ifdef USEVERTEXTEXTUREBLEND\n"
924 "       TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0);\n"
925 "#endif\n"
926 "#ifdef USELIGHTMAP\n"
927 "       TexCoordLightmap = vec2(Attrib_TexCoord4);\n"
928 "#endif\n"
929 "\n"
930 "#ifdef USEBOUNCEGRID\n"
931 "       BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position);\n"
932 "#endif\n"
933 "\n"
934 "#ifdef MODE_LIGHTSOURCE\n"
935 "       // transform vertex position into light attenuation/cubemap space\n"
936 "       // (-1 to +1 across the light box)\n"
937 "       CubeVector = vec3(ModelToLight * Attrib_Position);\n"
938 "\n"
939 "# ifdef USEDIFFUSE\n"
940 "       // transform unnormalized light direction into tangent space\n"
941 "       // (we use unnormalized to ensure that it interpolates correctly and then\n"
942 "       //  normalize it per pixel)\n"
943 "       vec3 lightminusvertex = LightPosition - Attrib_Position.xyz;\n"
944 "       LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz);\n"
945 "       LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz);\n"
946 "       LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz);\n"
947 "# endif\n"
948 "#endif\n"
949 "\n"
950 "#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE)\n"
951 "       LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz);\n"
952 "       LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz);\n"
953 "       LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz);\n"
954 "#endif\n"
955 "\n"
956 "       // transform unnormalized eye direction into tangent space\n"
957 "#ifdef USEEYEVECTOR\n"
958 "       vec3 EyeVectorModelSpace = EyePosition - Attrib_Position.xyz;\n"
959 "       EyeVector.x = dot(EyeVectorModelSpace, Attrib_TexCoord1.xyz);\n"
960 "       EyeVector.y = dot(EyeVectorModelSpace, Attrib_TexCoord2.xyz);\n"
961 "       EyeVector.z = dot(EyeVectorModelSpace, Attrib_TexCoord3.xyz);\n"
962 "#endif\n"
963 "\n"
964 "#ifdef USEFOG\n"
965 "       EyeVectorModelSpaceFogPlaneVertexDist.xyz = EyePosition - Attrib_Position.xyz;\n"
966 "       EyeVectorModelSpaceFogPlaneVertexDist.w = dot(FogPlane, Attrib_Position);\n"
967 "#endif\n"
968 "\n"
969 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE)\n"
970 "       VectorS = Attrib_TexCoord1.xyz;\n"
971 "       VectorT = Attrib_TexCoord2.xyz;\n"
972 "       VectorR = Attrib_TexCoord3.xyz;\n"
973 "#endif\n"
974 "\n"
975 "       // transform vertex to camera space, using ftransform to match non-VS rendering\n"
976 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
977 "\n"
978 "#ifdef USESHADOWMAPORTHO\n"
979 "       ShadowMapTC = vec3(ShadowMapMatrix * gl_Position);\n"
980 "#endif\n"
981 "\n"
982 "#ifdef USEREFLECTION\n"
983 "       ModelViewProjectionPosition = gl_Position;\n"
984 "#endif\n"
985 "}\n"
986 "#endif // VERTEX_SHADER\n"
987 "\n"
988 "\n"
989 "\n"
990 "\n"
991 "#ifdef FRAGMENT_SHADER\n"
992 "#ifdef USEDEFERREDLIGHTMAP\n"
993 "uniform myhalf2 PixelToScreenTexCoord;\n"
994 "uniform myhalf3 DeferredMod_Diffuse;\n"
995 "uniform myhalf3 DeferredMod_Specular;\n"
996 "#endif\n"
997 "uniform myhalf3 Color_Ambient;\n"
998 "uniform myhalf3 Color_Diffuse;\n"
999 "uniform myhalf3 Color_Specular;\n"
1000 "uniform myhalf SpecularPower;\n"
1001 "#ifdef USEGLOW\n"
1002 "uniform myhalf3 Color_Glow;\n"
1003 "#endif\n"
1004 "uniform myhalf Alpha;\n"
1005 "#ifdef USEREFLECTION\n"
1006 "uniform mediump vec4 DistortScaleRefractReflect;\n"
1007 "uniform mediump vec4 ScreenScaleRefractReflect;\n"
1008 "uniform mediump vec4 ScreenCenterRefractReflect;\n"
1009 "uniform lowp vec4 ReflectColor;\n"
1010 "#endif\n"
1011 "#ifdef USEREFLECTCUBE\n"
1012 "uniform highp mat4 ModelToReflectCube;\n"
1013 "uniform sampler2D Texture_ReflectMask;\n"
1014 "uniform samplerCube Texture_ReflectCube;\n"
1015 "#endif\n"
1016 "#ifdef MODE_LIGHTDIRECTION\n"
1017 "uniform myhalf3 LightColor;\n"
1018 "#endif\n"
1019 "#ifdef MODE_LIGHTSOURCE\n"
1020 "uniform myhalf3 LightColor;\n"
1021 "#endif\n"
1022 "#ifdef USEBOUNCEGRID\n"
1023 "uniform sampler3D Texture_BounceGrid;\n"
1024 "uniform float BounceGridIntensity;\n"
1025 "#endif\n"
1026 "void main(void)\n"
1027 "{\n"
1028 "#ifdef USEOFFSETMAPPING\n"
1029 "       // apply offsetmapping\n"
1030 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1031 "#define TexCoord TexCoordOffset\n"
1032 "#endif\n"
1033 "\n"
1034 "       // combine the diffuse textures (base, pants, shirt)\n"
1035 "       myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord));\n"
1036 "#ifdef USEALPHAKILL\n"
1037 "       if (color.a < 0.5)\n"
1038 "               discard;\n"
1039 "#endif\n"
1040 "       color.a *= Alpha;\n"
1041 "#ifdef USECOLORMAPPING\n"
1042 "       color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
1043 "#endif\n"
1044 "#ifdef USEVERTEXTEXTUREBLEND\n"
1045 "       myhalf terrainblend = clamp(myhalf(VertexColor.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0));\n"
1046 "       //myhalf terrainblend = min(myhalf(VertexColor.a) * color.a * 2.0, myhalf(1.0));\n"
1047 "       //myhalf terrainblend = myhalf(VertexColor.a) * color.a > 0.5;\n"
1048 "       color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend);\n"
1049 "       color.a = 1.0;\n"
1050 "       //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend);\n"
1051 "#endif\n"
1052 "\n"
1053 "       // get the surface normal\n"
1054 "#ifdef USEVERTEXTEXTUREBLEND\n"
1055 "       myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5));\n"
1056 "#else\n"
1057 "       myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5));\n"
1058 "#endif\n"
1059 "\n"
1060 "       // get the material colors\n"
1061 "       myhalf3 diffusetex = color.rgb;\n"
1062 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
1063 "# ifdef USEVERTEXTEXTUREBLEND\n"
1064 "       myhalf4 glosstex = mix(myhalf4(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf4(texture2D(Texture_Gloss, TexCoord)), terrainblend);\n"
1065 "# else\n"
1066 "       myhalf4 glosstex = myhalf4(texture2D(Texture_Gloss, TexCoord));\n"
1067 "# endif\n"
1068 "#endif\n"
1069 "\n"
1070 "#ifdef USEREFLECTCUBE\n"
1071 "       vec3 TangentReflectVector = reflect(-EyeVector, surfacenormal);\n"
1072 "       vec3 ModelReflectVector = TangentReflectVector.x * VectorS + TangentReflectVector.y * VectorT + TangentReflectVector.z * VectorR;\n"
1073 "       vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0));\n"
1074 "       diffusetex += myhalf3(texture2D(Texture_ReflectMask, TexCoord)) * myhalf3(textureCube(Texture_ReflectCube, ReflectCubeTexCoord));\n"
1075 "#endif\n"
1076 "\n"
1077 "\n"
1078 "\n"
1079 "\n"
1080 "#ifdef MODE_LIGHTSOURCE\n"
1081 "       // light source\n"
1082 "#ifdef USEDIFFUSE\n"
1083 "       myhalf3 lightnormal = myhalf3(normalize(LightVector));\n"
1084 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1085 "       color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);\n"
1086 "#ifdef USESPECULAR\n"
1087 "#ifdef USEEXACTSPECULARMATH\n"
1088 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower * glosstex.a);\n"
1089 "#else\n"
1090 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(EyeVector)));\n"
1091 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a);\n"
1092 "#endif\n"
1093 "       color.rgb += glosstex.rgb * (specular * Color_Specular);\n"
1094 "#endif\n"
1095 "#else\n"
1096 "       color.rgb = diffusetex * Color_Ambient;\n"
1097 "#endif\n"
1098 "       color.rgb *= LightColor;\n"
1099 "       color.rgb *= myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
1100 "#if defined(USESHADOWMAP2D)\n"
1101 "       color.rgb *= ShadowMapCompare(CubeVector);\n"
1102 "#endif\n"
1103 "# ifdef USECUBEFILTER\n"
1104 "       color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector));\n"
1105 "# endif\n"
1106 "#endif // MODE_LIGHTSOURCE\n"
1107 "\n"
1108 "\n"
1109 "\n"
1110 "\n"
1111 "#ifdef MODE_LIGHTDIRECTION\n"
1112 "#define SHADING\n"
1113 "#ifdef USEDIFFUSE\n"
1114 "       myhalf3 lightnormal = myhalf3(normalize(LightVector));\n"
1115 "#endif\n"
1116 "#define lightcolor LightColor\n"
1117 "#endif // MODE_LIGHTDIRECTION\n"
1118 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1119 "#define SHADING\n"
1120 "       // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe)\n"
1121 "       myhalf3 lightnormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1122 "       myhalf3 lightcolor = myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1123 "       // convert modelspace light vector to tangentspace\n"
1124 "       myhalf3 lightnormal;\n"
1125 "       lightnormal.x = dot(lightnormal_modelspace, myhalf3(VectorS));\n"
1126 "       lightnormal.y = dot(lightnormal_modelspace, myhalf3(VectorT));\n"
1127 "       lightnormal.z = dot(lightnormal_modelspace, myhalf3(VectorR));\n"
1128 "       lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this\n"
1129 "       // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
1130 "       // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar\n"
1131 "       // is used (the lightmap and deluxemap coords correspond to virtually random coordinates\n"
1132 "       // on that luxel, and NOT to its center, because recursive triangle subdivision is used\n"
1133 "       // to map the luxels to coordinates on the draw surfaces), which also causes\n"
1134 "       // deluxemaps to be wrong because light contributions from the wrong side of the surface\n"
1135 "       // are added up. To prevent divisions by zero or strong exaggerations, a max()\n"
1136 "       // nudge is done here at expense of some additional fps. This is ONLY needed for\n"
1137 "       // deluxemaps, tangentspace deluxemap avoid this problem by design.\n"
1138 "       lightcolor *= 1.0 / max(0.25, lightnormal.z);\n"
1139 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1140 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
1141 "#define SHADING\n"
1142 "       // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
1143 "       myhalf3 lightnormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1144 "       myhalf3 lightcolor = myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1145 "#endif\n"
1146 "\n"
1147 "\n"
1148 "\n"
1149 "\n"
1150 "#ifdef MODE_FAKELIGHT\n"
1151 "#define SHADING\n"
1152 "myhalf3 lightnormal = myhalf3(normalize(EyeVector));\n"
1153 "myhalf3 lightcolor = myhalf3(1.0);\n"
1154 "#endif // MODE_FAKELIGHT\n"
1155 "\n"
1156 "\n"
1157 "\n"
1158 "\n"
1159 "#ifdef MODE_LIGHTMAP\n"
1160 "       color.rgb = diffusetex * (Color_Ambient + myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * Color_Diffuse);\n"
1161 "#endif // MODE_LIGHTMAP\n"
1162 "#ifdef MODE_VERTEXCOLOR\n"
1163 "       color.rgb = diffusetex * (Color_Ambient + myhalf3(VertexColor.rgb) * Color_Diffuse);\n"
1164 "#endif // MODE_VERTEXCOLOR\n"
1165 "#ifdef MODE_FLATCOLOR\n"
1166 "       color.rgb = diffusetex * Color_Ambient;\n"
1167 "#endif // MODE_FLATCOLOR\n"
1168 "\n"
1169 "\n"
1170 "\n"
1171 "\n"
1172 "#ifdef SHADING\n"
1173 "# ifdef USEDIFFUSE\n"
1174 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1175 "#  ifdef USESPECULAR\n"
1176 "#   ifdef USEEXACTSPECULARMATH\n"
1177 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower * glosstex.a);\n"
1178 "#   else\n"
1179 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(EyeVector)));\n"
1180 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a);\n"
1181 "#   endif\n"
1182 "       color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor;\n"
1183 "#  else\n"
1184 "       color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor);\n"
1185 "#  endif\n"
1186 "# else\n"
1187 "       color.rgb = diffusetex * Color_Ambient;\n"
1188 "# endif\n"
1189 "#endif\n"
1190 "\n"
1191 "#ifdef USESHADOWMAPORTHO\n"
1192 "       color.rgb *= ShadowMapCompare(ShadowMapTC);\n"
1193 "#endif\n"
1194 "\n"
1195 "#ifdef USEDEFERREDLIGHTMAP\n"
1196 "       vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;\n"
1197 "       color.rgb += diffusetex * myhalf3(texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse;\n"
1198 "       color.rgb += glosstex.rgb * myhalf3(texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular;\n"
1199 "#endif\n"
1200 "\n"
1201 "#ifdef USEBOUNCEGRID\n"
1202 "       color.rgb += diffusetex * myhalf3(texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity;\n"
1203 "#endif\n"
1204 "\n"
1205 "#ifdef USEGLOW\n"
1206 "#ifdef USEVERTEXTEXTUREBLEND\n"
1207 "       color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend) * Color_Glow;\n"
1208 "#else\n"
1209 "       color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * Color_Glow;\n"
1210 "#endif\n"
1211 "#endif\n"
1212 "\n"
1213 "#ifdef USEFOG\n"
1214 "       color.rgb = FogVertex(color);\n"
1215 "#endif\n"
1216 "\n"
1217 "       // 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"
1218 "#ifdef USEREFLECTION\n"
1219 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
1220 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
1221 "       vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;\n"
1222 "       vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw;\n"
1223 "       // FIXME temporary hack to detect the case that the reflection\n"
1224 "       // gets blackened at edges due to leaving the area that contains actual\n"
1225 "       // content.\n"
1226 "       // Remove this 'ack once we have a better way to stop this thing from\n"
1227 "       // 'appening.\n"
1228 "       float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1229 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1230 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1231 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1232 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
1233 "       color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a);\n"
1234 "#endif\n"
1235 "\n"
1236 "       gl_FragColor = vec4(color);\n"
1237 "}\n"
1238 "#endif // FRAGMENT_SHADER\n"
1239 "\n"
1240 "#endif // !MODE_DEFERREDLIGHTSOURCE\n"
1241 "#endif // !MODE_DEFERREDGEOMETRY\n"
1242 "#endif // !MODE_WATER\n"
1243 "#endif // !MODE_REFRACTION\n"
1244 "#endif // !MODE_BLOOMBLUR\n"
1245 "#endif // !MODE_GENERIC\n"
1246 "#endif // !MODE_POSTPROCESS\n"
1247 "#endif // !MODE_SHOWDEPTH\n"
1248 "#endif // !MODE_DEPTH_OR_SHADOW\n"