]> git.xonotic.org Git - xonotic/darkplaces.git/blob - shader_hlsl.h
make sure gloss never calculates 0^0
[xonotic/darkplaces.git] / shader_hlsl.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 "// not REALLY the smallest value (2^-62 would be correct), but short and any compiler failing at parsing this is broken\n"
6 "#define FLT_MIN 0.000000000000000001\n"
7 "\n"
8 "// FIXME: we need to get rid of ModelViewProjectionPosition to make room for the texcoord for this\n"
9 "#if defined(USEREFLECTION)\n"
10 "#undef USESHADOWMAPORTHO\n"
11 "#endif\n"
12 "\n"
13 "#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE)\n"
14 "# define USEFOG\n"
15 "#endif\n"
16 "#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP)\n"
17 "#define USELIGHTMAP\n"
18 "#endif\n"
19 "#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT)\n"
20 "#define USEEYEVECTOR\n"
21 "#endif\n"
22 "\n"
23 "#ifdef FRAGMENT_SHADER\n"
24 "#ifdef HLSL\n"
25 "//#undef USESHADOWMAPPCF\n"
26 "//#define texDepth2D(tex,texcoord) tex2D(tex,texcoord).r\n"
27 "#define texDepth2D(tex,texcoord) dot(tex2D(tex,texcoord).rgb, float3(1.0, 255.0/65536.0, 255.0/16777216.0))\n"
28 "#else\n"
29 "#define texDepth2D(tex,texcoord) tex2D(tex,texcoord).r\n"
30 "#endif\n"
31 "#endif\n"
32 "\n"
33 "#ifdef VERTEX_SHADER\n"
34 "#ifdef USETRIPPY\n"
35 "// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE\n"
36 "// tweaked scale\n"
37 "float4 TrippyVertex(float4 position)\n"
38 "(\n"
39 "uniform float ClientTime : register(c2)\n"
40 ")\n"
41 "{\n"
42 "       float worldTime = ClientTime;\n"
43 "       // tweaked for Quake\n"
44 "       worldTime *= 10.0;\n"
45 "       position *= 0.125;\n"
46 "       //~tweaked for Quake\n"
47 "       float distanceSquared = (position.x * position.x + position.z * position.z);\n"
48 "       position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0);\n"
49 "       float y = position.y;\n"
50 "       float x = position.x;\n"
51 "       float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0);\n"
52 "       position.y = x*sin(om)+y*cos(om);\n"
53 "       position.x = x*cos(om)-y*sin(om);\n"
54 "       return position;\n"
55 "}\n"
56 "#endif\n"
57 "#endif\n"
58 "\n"
59 "#ifdef MODE_DEPTH_OR_SHADOW\n"
60 "#ifdef VERTEX_SHADER\n"
61 "void main\n"
62 "(\n"
63 "float4 gl_Vertex : POSITION,\n"
64 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
65 "out float4 gl_Position : POSITION,\n"
66 "out float Depth : TEXCOORD0\n"
67 ")\n"
68 "{\n"
69 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
70 "#ifdef USETRIPPY\n"
71 "       gl_Position = TrippyVertex(gl_Position);\n"
72 "#endif\n"
73 "       Depth = gl_Position.z;\n"
74 "}\n"
75 "#endif\n"
76 "\n"
77 "#ifdef FRAGMENT_SHADER\n"
78 "void main\n"
79 "(\n"
80 "float Depth : TEXCOORD0,\n"
81 "out float4 gl_FragColor : COLOR\n"
82 ")\n"
83 "{\n"
84 "//     float4 temp = float4(Depth,Depth*(65536.0/255.0),Depth*(16777216.0/255.0),0.0);\n"
85 "       float4 temp = float4(Depth,Depth*256.0,Depth*65536.0,0.0);\n"
86 "       temp.yz -= floor(temp.yz);\n"
87 "       gl_FragColor = temp;\n"
88 "//     gl_FragColor = float4(Depth,0,0,0);\n"
89 "}\n"
90 "#endif\n"
91 "#else // !MODE_DEPTH_ORSHADOW\n"
92 "\n"
93 "\n"
94 "\n"
95 "\n"
96 "#ifdef MODE_SHOWDEPTH\n"
97 "#ifdef VERTEX_SHADER\n"
98 "void main\n"
99 "(\n"
100 "float4 gl_Vertex : POSITION,\n"
101 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
102 "out float4 gl_Position : POSITION,\n"
103 "out float4 gl_FrontColor : COLOR0\n"
104 ")\n"
105 "{\n"
106 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
107 "#ifdef USETRIPPY\n"
108 "       gl_Position = TrippyVertex(gl_Position);\n"
109 "#endif\n"
110 "       gl_FrontColor = float4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0);\n"
111 "}\n"
112 "#endif\n"
113 "\n"
114 "#ifdef FRAGMENT_SHADER\n"
115 "void main\n"
116 "(\n"
117 "float4 gl_FrontColor : COLOR0,\n"
118 "out float4 gl_FragColor : COLOR\n"
119 ")\n"
120 "{\n"
121 "       gl_FragColor = gl_FrontColor;\n"
122 "}\n"
123 "#endif\n"
124 "#else // !MODE_SHOWDEPTH\n"
125 "\n"
126 "\n"
127 "\n"
128 "\n"
129 "#ifdef MODE_POSTPROCESS\n"
130 "\n"
131 "#ifdef VERTEX_SHADER\n"
132 "void main\n"
133 "(\n"
134 "float4 gl_Vertex : POSITION,\n"
135 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
136 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
137 "float4 gl_MultiTexCoord4 : TEXCOORD4,\n"
138 "out float4 gl_Position : POSITION,\n"
139 "out float2 TexCoord1 : TEXCOORD0,\n"
140 "out float2 TexCoord2 : TEXCOORD1\n"
141 ")\n"
142 "{\n"
143 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
144 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
145 "#ifdef USEBLOOM\n"
146 "       TexCoord2 = gl_MultiTexCoord4.xy;\n"
147 "#endif\n"
148 "}\n"
149 "#endif\n"
150 "\n"
151 "#ifdef FRAGMENT_SHADER\n"
152 "void main\n"
153 "(\n"
154 "float2 TexCoord1 : TEXCOORD0,\n"
155 "float2 TexCoord2 : TEXCOORD1,\n"
156 "uniform sampler Texture_First : register(s0),\n"
157 "#ifdef USEBLOOM\n"
158 "uniform sampler Texture_Second : register(s1),\n"
159 "#endif\n"
160 "#ifdef USEGAMMARAMPS\n"
161 "uniform sampler Texture_GammaRamps : register(s2),\n"
162 "#endif\n"
163 "#ifdef USESATURATION\n"
164 "uniform float Saturation : register(c30),\n"
165 "#endif\n"
166 "#ifdef USEVIEWTINT\n"
167 "uniform float4 ViewTintColor : register(c41),\n"
168 "#endif\n"
169 "uniform float4 UserVec1 : register(c37),\n"
170 "uniform float4 UserVec2 : register(c38),\n"
171 "uniform float4 UserVec3 : register(c39),\n"
172 "uniform float4 UserVec4 : register(c40),\n"
173 "uniform float ClientTime : register(c2),\n"
174 "uniform float2 PixelSize : register(c25),\n"
175 "uniform float4 BloomColorSubtract : register(c43),\n"
176 "out float4 gl_FragColor : COLOR\n"
177 ")\n"
178 "{\n"
179 "       gl_FragColor = tex2D(Texture_First, TexCoord1);\n"
180 "#ifdef USEBLOOM\n"
181 "       gl_FragColor += max(float4(0,0,0,0), tex2D(Texture_Second, TexCoord2) - BloomColorSubtract);\n"
182 "#endif\n"
183 "#ifdef USEVIEWTINT\n"
184 "       gl_FragColor = lerp(gl_FragColor, ViewTintColor, ViewTintColor.a);\n"
185 "#endif\n"
186 "\n"
187 "#ifdef USEPOSTPROCESSING\n"
188 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n"
189 "// 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"
190 "       float sobel = 1.0;\n"
191 "       // float2 ts = textureSize(Texture_First, 0);\n"
192 "       // float2 px = float2(1/ts.x, 1/ts.y);\n"
193 "       float2 px = PixelSize;\n"
194 "       float3 x1 = tex2D(Texture_First, TexCoord1 + float2(-px.x, px.y)).rgb;\n"
195 "       float3 x2 = tex2D(Texture_First, TexCoord1 + float2(-px.x,  0.0)).rgb;\n"
196 "       float3 x3 = tex2D(Texture_First, TexCoord1 + float2(-px.x,-px.y)).rgb;\n"
197 "       float3 x4 = tex2D(Texture_First, TexCoord1 + float2( px.x, px.y)).rgb;\n"
198 "       float3 x5 = tex2D(Texture_First, TexCoord1 + float2( px.x,  0.0)).rgb;\n"
199 "       float3 x6 = tex2D(Texture_First, TexCoord1 + float2( px.x,-px.y)).rgb;\n"
200 "       float3 y1 = tex2D(Texture_First, TexCoord1 + float2( px.x,-px.y)).rgb;\n"
201 "       float3 y2 = tex2D(Texture_First, TexCoord1 + float2(  0.0,-px.y)).rgb;\n"
202 "       float3 y3 = tex2D(Texture_First, TexCoord1 + float2(-px.x,-px.y)).rgb;\n"
203 "       float3 y4 = tex2D(Texture_First, TexCoord1 + float2( px.x, px.y)).rgb;\n"
204 "       float3 y5 = tex2D(Texture_First, TexCoord1 + float2(  0.0, px.y)).rgb;\n"
205 "       float3 y6 = tex2D(Texture_First, TexCoord1 + float2(-px.x, px.y)).rgb;\n"
206 "       float px1 = -1.0 * dot(float3(0.3, 0.59, 0.11), x1);\n"
207 "       float px2 = -2.0 * dot(float3(0.3, 0.59, 0.11), x2);\n"
208 "       float px3 = -1.0 * dot(float3(0.3, 0.59, 0.11), x3);\n"
209 "       float px4 =  1.0 * dot(float3(0.3, 0.59, 0.11), x4);\n"
210 "       float px5 =  2.0 * dot(float3(0.3, 0.59, 0.11), x5);\n"
211 "       float px6 =  1.0 * dot(float3(0.3, 0.59, 0.11), x6);\n"
212 "       float py1 = -1.0 * dot(float3(0.3, 0.59, 0.11), y1);\n"
213 "       float py2 = -2.0 * dot(float3(0.3, 0.59, 0.11), y2);\n"
214 "       float py3 = -1.0 * dot(float3(0.3, 0.59, 0.11), y3);\n"
215 "       float py4 =  1.0 * dot(float3(0.3, 0.59, 0.11), y4);\n"
216 "       float py5 =  2.0 * dot(float3(0.3, 0.59, 0.11), y5);\n"
217 "       float py6 =  1.0 * dot(float3(0.3, 0.59, 0.11), y6);\n"
218 "       sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6);\n"
219 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2(-0.987688, -0.156434)) * UserVec1.y;\n"
220 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2(-0.156434, -0.891007)) * UserVec1.y;\n"
221 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2( 0.891007, -0.453990)) * UserVec1.y;\n"
222 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2( 0.707107,  0.707107)) * UserVec1.y;\n"
223 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2(-0.453990,  0.891007)) * UserVec1.y;\n"
224 "       gl_FragColor /= (1.0 + 5.0 * UserVec1.y);\n"
225 "       gl_FragColor.rgb = gl_FragColor.rgb * (1.0 + UserVec2.x) + float3(1,1,1)*max(0.0, sobel - UserVec2.z)*UserVec2.y;\n"
226 "#endif\n"
227 "\n"
228 "#ifdef USESATURATION\n"
229 "       //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter\n"
230 "       float y = dot(gl_FragColor.rgb, float3(0.299, 0.587, 0.114));\n"
231 "       // 'vampire sight' effect, wheres red is compensated\n"
232 "       #ifdef SATURATION_REDCOMPENSATE\n"
233 "               float rboost = max(0.0, (gl_FragColor.r - max(gl_FragColor.g, gl_FragColor.b))*(1.0 - Saturation));\n"
234 "               gl_FragColor.rgb = lerp(float3(y,y,y), gl_FragColor.rgb, Saturation);\n"
235 "               gl_FragColor.r += r;\n"
236 "       #else\n"
237 "               // normal desaturation\n"
238 "               //gl_FragColor = float3(y,y,y) + (gl_FragColor.rgb - float3(y)) * Saturation;\n"
239 "               gl_FragColor.rgb = lerp(float3(y,y,y), gl_FragColor.rgb, Saturation);\n"
240 "       #endif\n"
241 "#endif\n"
242 "\n"
243 "#ifdef USEGAMMARAMPS\n"
244 "       gl_FragColor.r = tex2D(Texture_GammaRamps, float2(gl_FragColor.r, 0)).r;\n"
245 "       gl_FragColor.g = tex2D(Texture_GammaRamps, float2(gl_FragColor.g, 0)).g;\n"
246 "       gl_FragColor.b = tex2D(Texture_GammaRamps, float2(gl_FragColor.b, 0)).b;\n"
247 "#endif\n"
248 "}\n"
249 "#endif\n"
250 "#else // !MODE_POSTPROCESS\n"
251 "\n"
252 "\n"
253 "\n"
254 "\n"
255 "#ifdef MODE_GENERIC\n"
256 "#ifdef VERTEX_SHADER\n"
257 "void main\n"
258 "(\n"
259 "float4 gl_Vertex : POSITION,\n"
260 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
261 "float4 gl_Color : COLOR0,\n"
262 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
263 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
264 "out float4 gl_Position : POSITION,\n"
265 "#ifdef USEDIFFUSE\n"
266 "out float2 TexCoord1 : TEXCOORD0,\n"
267 "#endif\n"
268 "#ifdef USESPECULAR\n"
269 "out float2 TexCoord2 : TEXCOORD1,\n"
270 "#endif\n"
271 "out float4 gl_FrontColor : COLOR\n"
272 ")\n"
273 "{\n"
274 "       gl_FrontColor = gl_Color;\n"
275 "#ifdef USEDIFFUSE\n"
276 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
277 "#endif\n"
278 "#ifdef USESPECULAR\n"
279 "       TexCoord2 = gl_MultiTexCoord1.xy;\n"
280 "#endif\n"
281 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
282 "#ifdef USETRIPPY\n"
283 "       gl_Position = TrippyVertex(gl_Position);\n"
284 "#endif\n"
285 "}\n"
286 "#endif\n"
287 "\n"
288 "#ifdef FRAGMENT_SHADER\n"
289 "\n"
290 "void main\n"
291 "(\n"
292 "float4 gl_FrontColor : COLOR0,\n"
293 "float2 TexCoord1 : TEXCOORD0,\n"
294 "float2 TexCoord2 : TEXCOORD1,\n"
295 "#ifdef USEDIFFUSE\n"
296 "uniform sampler Texture_First : register(s0),\n"
297 "#endif\n"
298 "#ifdef USESPECULAR\n"
299 "uniform sampler Texture_Second : register(s1),\n"
300 "#endif\n"
301 "#ifdef USEGAMMARAMPS\n"
302 "uniform sampler Texture_GammaRamps : register(s2),\n"
303 "#endif\n"
304 "out float4 gl_FragColor : COLOR\n"
305 ")\n"
306 "{\n"
307 "#ifdef USEVIEWTINT\n"
308 "       gl_FragColor = gl_FrontColor;\n"
309 "#else\n"
310 "       gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
311 "#endif\n"
312 "#ifdef USEDIFFUSE\n"
313 "       gl_FragColor *= tex2D(Texture_First, TexCoord1);\n"
314 "#endif\n"
315 "\n"
316 "#ifdef USESPECULAR\n"
317 "       float4 tex2 = tex2D(Texture_Second, TexCoord2);\n"
318 "# ifdef USECOLORMAPPING\n"
319 "       gl_FragColor *= tex2;\n"
320 "# endif\n"
321 "# ifdef USEGLOW\n"
322 "       gl_FragColor += tex2;\n"
323 "# endif\n"
324 "# ifdef USEVERTEXTEXTUREBLEND\n"
325 "       gl_FragColor = lerp(gl_FragColor, tex2, tex2.a);\n"
326 "# endif\n"
327 "#endif\n"
328 "#ifdef USEGAMMARAMPS\n"
329 "       dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r;\n"
330 "       dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g;\n"
331 "       dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b;\n"
332 "#endif\n"
333 "}\n"
334 "#endif\n"
335 "#else // !MODE_GENERIC\n"
336 "\n"
337 "\n"
338 "\n"
339 "\n"
340 "#ifdef MODE_BLOOMBLUR\n"
341 "#ifdef VERTEX_SHADER\n"
342 "void main\n"
343 "(\n"
344 "float4 gl_Vertex : POSITION,\n"
345 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
346 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
347 "out float4 gl_Position : POSITION,\n"
348 "out float2 TexCoord : TEXCOORD0\n"
349 ")\n"
350 "{\n"
351 "       TexCoord = gl_MultiTexCoord0.xy;\n"
352 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
353 "}\n"
354 "#endif\n"
355 "\n"
356 "#ifdef FRAGMENT_SHADER\n"
357 "\n"
358 "void main\n"
359 "(\n"
360 "float2 TexCoord : TEXCOORD0,\n"
361 "uniform sampler Texture_First : register(s0),\n"
362 "uniform float4 BloomBlur_Parameters : register(c1),\n"
363 "out float4 gl_FragColor : COLOR\n"
364 ")\n"
365 "{\n"
366 "       int i;\n"
367 "       float2 tc = TexCoord;\n"
368 "       float3 color = tex2D(Texture_First, tc).rgb;\n"
369 "       tc += BloomBlur_Parameters.xy;\n"
370 "       for (i = 1;i < SAMPLES;i++)\n"
371 "       {\n"
372 "               color += tex2D(Texture_First, tc).rgb;\n"
373 "               tc += BloomBlur_Parameters.xy;\n"
374 "       }\n"
375 "       gl_FragColor = float4(color * BloomBlur_Parameters.z + float3(BloomBlur_Parameters.w), 1);\n"
376 "}\n"
377 "#endif\n"
378 "#else // !MODE_BLOOMBLUR\n"
379 "#ifdef MODE_REFRACTION\n"
380 "#ifdef VERTEX_SHADER\n"
381 "void main\n"
382 "(\n"
383 "float4 gl_Vertex : POSITION,\n"
384 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
385 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
386 "uniform float4x4 TexMatrix : register(c0),\n"
387 "uniform float3 EyePosition : register(c24),\n"
388 "out float4 gl_Position : POSITION,\n"
389 "out float2 TexCoord : TEXCOORD0,\n"
390 "out float3 EyeVector : TEXCOORD1,\n"
391 "out float4 ModelViewProjectionPosition : TEXCOORD2\n"
392 ")\n"
393 "{\n"
394 "       TexCoord = mul(TexMatrix, gl_MultiTexCoord0).xy;\n"
395 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
396 "       ModelViewProjectionPosition = gl_Position;\n"
397 "#ifdef USETRIPPY\n"
398 "       gl_Position = TrippyVertex(gl_Position);\n"
399 "#endif\n"
400 "}\n"
401 "#endif\n"
402 "\n"
403 "#ifdef FRAGMENT_SHADER\n"
404 "void main\n"
405 "(\n"
406 "float2 TexCoord : TEXCOORD0,\n"
407 "float3 EyeVector : TEXCOORD1,\n"
408 "float4 ModelViewProjectionPosition : TEXCOORD2,\n"
409 "uniform sampler Texture_Normal : register(s0),\n"
410 "uniform sampler Texture_Refraction : register(s3),\n"
411 "uniform sampler Texture_Reflection : register(s7),\n"
412 "uniform float4 DistortScaleRefractReflect : register(c14),\n"
413 "uniform float4 ScreenScaleRefractReflect : register(c32),\n"
414 "uniform float4 ScreenCenterRefractReflect : register(c31),\n"
415 "uniform float4 RefractColor : register(c29),\n"
416 "out float4 gl_FragColor : COLOR\n"
417 ")\n"
418 "{\n"
419 "       float2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
420 "       //float2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
421 "       float2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
422 "       float2 ScreenTexCoord = SafeScreenTexCoord + normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy * DistortScaleRefractReflect.xy;\n"
423 "       // FIXME temporary hack to detect the case that the reflection\n"
424 "       // gets blackened at edges due to leaving the area that contains actual\n"
425 "       // content.\n"
426 "       // Remove this 'ack once we have a better way to stop this thing from\n"
427 "       // 'appening.\n"
428 "       float f = min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(0.01, 0.01)).rgb) / 0.05);\n"
429 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(0.01, -0.01)).rgb) / 0.05);\n"
430 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(-0.01, 0.01)).rgb) / 0.05);\n"
431 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(-0.01, -0.01)).rgb) / 0.05);\n"
432 "       ScreenTexCoord = lerp(SafeScreenTexCoord, ScreenTexCoord, f);\n"
433 "       gl_FragColor = float4(tex2D(Texture_Refraction, ScreenTexCoord).rgb, 1) * RefractColor;\n"
434 "}\n"
435 "#endif\n"
436 "#else // !MODE_REFRACTION\n"
437 "\n"
438 "\n"
439 "\n"
440 "\n"
441 "#ifdef MODE_WATER\n"
442 "#ifdef VERTEX_SHADER\n"
443 "\n"
444 "void main\n"
445 "(\n"
446 "float4 gl_Vertex : POSITION,\n"
447 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
448 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
449 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
450 "float4 gl_MultiTexCoord2 : TEXCOORD2,\n"
451 "float4 gl_MultiTexCoord3 : TEXCOORD3,\n"
452 "uniform float4x4 TexMatrix : register(c0),\n"
453 "uniform float3 EyePosition : register(c24),\n"
454 "out float4 gl_Position : POSITION,\n"
455 "out float2 TexCoord : TEXCOORD0,\n"
456 "out float3 EyeVector : TEXCOORD1,\n"
457 "out float4 ModelViewProjectionPosition : TEXCOORD2\n"
458 ")\n"
459 "{\n"
460 "       TexCoord = mul(TexMatrix, gl_MultiTexCoord0).xy;\n"
461 "       float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
462 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
463 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
464 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
465 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
466 "       ModelViewProjectionPosition = gl_Position;\n"
467 "#ifdef USETRIPPY\n"
468 "       gl_Position = TrippyVertex(gl_Position);\n"
469 "#endif\n"
470 "}\n"
471 "#endif\n"
472 "\n"
473 "#ifdef FRAGMENT_SHADER\n"
474 "void main\n"
475 "(\n"
476 "float2 TexCoord : TEXCOORD0,\n"
477 "float3 EyeVector : TEXCOORD1,\n"
478 "float4 ModelViewProjectionPosition : TEXCOORD2,\n"
479 "uniform sampler Texture_Normal : register(s0),\n"
480 "uniform sampler Texture_Refraction : register(s3),\n"
481 "uniform sampler Texture_Reflection : register(s7),\n"
482 "uniform float4 DistortScaleRefractReflect : register(c14),\n"
483 "uniform float4 ScreenScaleRefractReflect : register(c32),\n"
484 "uniform float4 ScreenCenterRefractReflect : register(c31),\n"
485 "uniform float4 RefractColor : register(c29),\n"
486 "uniform float4 ReflectColor : register(c26),\n"
487 "uniform float ReflectFactor : register(c27),\n"
488 "uniform float ReflectOffset : register(c28),\n"
489 "out float4 gl_FragColor : COLOR\n"
490 ")\n"
491 "{\n"
492 "       float4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
493 "       //float4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
494 "       float4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
495 "       //SafeScreenTexCoord = gl_FragCoord.xyxy * float4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0);\n"
496 "       float4 ScreenTexCoord = SafeScreenTexCoord + float2(normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy).xyxy * DistortScaleRefractReflect;\n"
497 "       // FIXME temporary hack to detect the case that the reflection\n"
498 "       // gets blackened at edges due to leaving the area that contains actual\n"
499 "       // content.\n"
500 "       // Remove this 'ack once we have a better way to stop this thing from\n"
501 "       // 'appening.\n"
502 "       float f = min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(0.01, 0.01)).rgb) / 0.05);\n"
503 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(0.01, -0.01)).rgb) / 0.05);\n"
504 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(-0.01, 0.01)).rgb) / 0.05);\n"
505 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(-0.01, -0.01)).rgb) / 0.05);\n"
506 "       ScreenTexCoord.xy = lerp(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);\n"
507 "       f  = min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(0.01, 0.01)).rgb) / 0.05);\n"
508 "       f *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(0.01, -0.01)).rgb) / 0.05);\n"
509 "       f *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(-0.01, 0.01)).rgb) / 0.05);\n"
510 "       f *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(-0.01, -0.01)).rgb) / 0.05);\n"
511 "       ScreenTexCoord.zw = lerp(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);\n"
512 "       float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n"
513 "       gl_FragColor = lerp(float4(tex2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * RefractColor, float4(tex2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);\n"
514 "}\n"
515 "#endif\n"
516 "#else // !MODE_WATER\n"
517 "\n"
518 "\n"
519 "\n"
520 "\n"
521 "// 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"
522 "\n"
523 "// fragment shader specific:\n"
524 "#ifdef FRAGMENT_SHADER\n"
525 "\n"
526 "#ifdef USEFOG\n"
527 "float3 FogVertex(float4 surfacecolor, float3 FogColor, float3 EyeVectorModelSpace, float FogPlaneVertexDist, float FogRangeRecip, float FogPlaneViewDist, float FogHeightFade, sampler Texture_FogMask, sampler Texture_FogHeightTexture)\n"
528 "{\n"
529 "       float fogfrac;\n"
530 "       float3 fc = FogColor;\n"
531 "#ifdef USEFOGALPHAHACK\n"
532 "       fc *= surfacecolor.a;\n"
533 "#endif\n"
534 "#ifdef USEFOGHEIGHTTEXTURE\n"
535 "       float4 fogheightpixel = tex2D(Texture_FogHeightTexture, float2(1,1) + float2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade));\n"
536 "       fogfrac = fogheightpixel.a;\n"
537 "       return lerp(fogheightpixel.rgb * fc, surfacecolor.rgb, tex2D(Texture_FogMask, float2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);\n"
538 "#else\n"
539 "# ifdef USEFOGOUTSIDE\n"
540 "       fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);\n"
541 "# else\n"
542 "       fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);\n"
543 "# endif\n"
544 "       return lerp(fc, surfacecolor.rgb, tex2D(Texture_FogMask, float2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);\n"
545 "#endif\n"
546 "}\n"
547 "#endif\n"
548 "\n"
549 "#ifdef USEOFFSETMAPPING\n"
550 "float2 OffsetMapping(float2 TexCoord, float4 OffsetMapping_ScaleSteps, float3 EyeVector, sampler Texture_Normal, float2 dPdx, float2 dPdy)\n"
551 "{\n"
552 "       float i;\n"
553 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
554 "       // 14 sample relief mapping: linear search and then binary search\n"
555 "       // this basically steps forward a small amount repeatedly until it finds\n"
556 "       // itself inside solid, then jitters forward and back using decreasing\n"
557 "       // amounts to find the impact\n"
558 "       //float3 OffsetVector = float3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_ScaleSteps.x) * float2(-1, 1), -1);\n"
559 "       //float3 OffsetVector = float3(normalize(EyeVector.xy) * OffsetMapping_ScaleSteps.x * float2(-1, 1), -1);\n"
560 "       float3 OffsetVector = float3(normalize(EyeVector).xy * OffsetMapping_ScaleSteps.x * float2(-1, 1), -1);\n"
561 "       float3 RT = float3(TexCoord, 1);\n"
562 "       OffsetVector *= OffsetMapping_ScaleSteps.z;\n"
563 "       for(i = 1.0; i < OffsetMapping_ScaleSteps.y; ++i)\n"
564 "               RT += OffsetVector *  step(tex2Dgrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z);\n"
565 "       for(i = 0.0, f = 1.0; i < OffsetMapping_ScaleSteps.w; ++i, f *= 0.5)\n"
566 "               RT += OffsetVector * (step(tex2Dgrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f);\n"
567 "       return RT.xy;\n"
568 "#else\n"
569 "       // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits)\n"
570 "       //float2 OffsetVector = float2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_ScaleSteps.x) * float2(-1, 1));\n"
571 "       //float2 OffsetVector = float2(normalize(EyeVector.xy) * OffsetMapping_ScaleSteps.x * float2(-1, 1));\n"
572 "       float2 OffsetVector = float2(normalize(EyeVector).xy * OffsetMapping_ScaleSteps.x * float2(-1, 1));\n"
573 "       OffsetVector *= OffsetMapping_ScaleSteps.z;\n"
574 "       for(i = 0.0; i < OffsetMapping_ScaleSteps.y; ++i)\n"
575 "               TexCoord += OffsetVector * (1.0 - tex2Dgrad(Texture_Normal, TexCoord, dPdx, dPdy).a);\n"
576 "       return TexCoord;\n"
577 "#endif\n"
578 "}\n"
579 "#endif // USEOFFSETMAPPING\n"
580 "\n"
581 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO)\n"
582 "#if defined(USESHADOWMAP2D)\n"
583 "# ifdef USESHADOWMAPORTHO\n"
584 "#  define GetShadowMapTC2D(dir, ShadowMap_Parameters) (min(dir, ShadowMap_Parameters.xyz))\n"
585 "# else\n"
586 "#  ifdef USESHADOWMAPVSDCT\n"
587 "float3 GetShadowMapTC2D(float3 dir, float4 ShadowMap_Parameters, samplerCUBE Texture_CubeProjection)\n"
588 "{\n"
589 "       float3 adir = abs(dir);\n"
590 "       float2 aparams = ShadowMap_Parameters.xy / max(max(adir.x, adir.y), adir.z);\n"
591 "       float4 proj = texCUBE(Texture_CubeProjection, dir);\n"
592 "       return float3(lerp(dir.xy, dir.zz, proj.xy) * aparams.x + proj.zw * ShadowMap_Parameters.z, aparams.y + ShadowMap_Parameters.w);\n"
593 "}\n"
594 "#  else\n"
595 "float3 GetShadowMapTC2D(float3 dir, float4 ShadowMap_Parameters)\n"
596 "{\n"
597 "       float3 adir = abs(dir);\n"
598 "       float ma = adir.z;\n"
599 "       float4 proj = float4(dir, 2.5);\n"
600 "       if (adir.x > ma) { ma = adir.x; proj = float4(dir.zyx, 0.5); }\n"
601 "       if (adir.y > ma) { ma = adir.y; proj = float4(dir.xzy, 1.5); }\n"
602 "#ifdef HLSL\n"
603 "       return float3(proj.xy * ShadowMap_Parameters.x / ma + float2(0.5,0.5) + float2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, ma + 64 * ShadowMap_Parameters.w);\n"
604 "#else\n"
605 "       float2 aparams = ShadowMap_Parameters.xy / ma;\n"
606 "       return float3(proj.xy * aparams.x + float2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, aparams.y + ShadowMap_Parameters.w);\n"
607 "#endif\n"
608 "}\n"
609 "#  endif\n"
610 "# endif\n"
611 "#endif // defined(USESHADOWMAP2D)\n"
612 "\n"
613 "# ifdef USESHADOWMAP2D\n"
614 "#ifdef USESHADOWMAPVSDCT\n"
615 "float ShadowMapCompare(float3 dir, sampler Texture_ShadowMap2D, float4 ShadowMap_Parameters, float2 ShadowMap_TextureScale, samplerCUBE Texture_CubeProjection)\n"
616 "#else\n"
617 "float ShadowMapCompare(float3 dir, sampler Texture_ShadowMap2D, float4 ShadowMap_Parameters, float2 ShadowMap_TextureScale)\n"
618 "#endif\n"
619 "{\n"
620 "#ifdef USESHADOWMAPVSDCT\n"
621 "       float3 shadowmaptc = GetShadowMapTC2D(dir, ShadowMap_Parameters, Texture_CubeProjection);\n"
622 "#else\n"
623 "       float3 shadowmaptc = GetShadowMapTC2D(dir, ShadowMap_Parameters);\n"
624 "#endif\n"
625 "       float f;\n"
626 "\n"
627 "#  ifdef USESHADOWSAMPLER\n"
628 "#    ifdef USESHADOWMAPPCF\n"
629 "#      define texval(x, y) tex2Dproj(Texture_ShadowMap2D, float4(center + float2(x, y)*ShadowMap_TextureScale, shadowmaptc.z, 1.0)).r  \n"
630 "       float2 center = shadowmaptc.xy*ShadowMap_TextureScale;\n"
631 "       f = dot(float4(0.25,0.25,0.25,0.25), float4(texval(-0.4, 1.0), texval(-1.0, -0.4), texval(0.4, -1.0), texval(1.0, 0.4)));\n"
632 "#    else\n"
633 "       f = tex2Dproj(Texture_ShadowMap2D, float4(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z, 1.0)).r;\n"
634 "#    endif\n"
635 "#  else\n"
636 "#    ifdef USESHADOWMAPPCF\n"
637 "#     if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)\n"
638 "#      ifdef GL_ARB_texture_gather\n"
639 "#        define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, int2(x, y))\n"
640 "#      else\n"
641 "#        define texval(x, y) texture4(Texture_ShadowMap2D, center + float2(x, y)*ShadowMap_TextureScale)\n"
642 "#      endif\n"
643 "       float2 offset = frac(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale;\n"
644 "#      if USESHADOWMAPPCF > 1\n"
645 "       float4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0));\n"
646 "       float4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0));\n"
647 "       float4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0));\n"
648 "       float4 group4 = step(shadowmaptc.z, texval(-2.0,  0.0));\n"
649 "       float4 group5 = step(shadowmaptc.z, texval( 0.0,  0.0));\n"
650 "       float4 group6 = step(shadowmaptc.z, texval( 2.0,  0.0));\n"
651 "       float4 group7 = step(shadowmaptc.z, texval(-2.0,  2.0));\n"
652 "       float4 group8 = step(shadowmaptc.z, texval( 0.0,  2.0));\n"
653 "       float4 group9 = step(shadowmaptc.z, texval( 2.0,  2.0));\n"
654 "       float4 locols = float4(group1.ab, group3.ab);\n"
655 "       float4 hicols = float4(group7.rg, group9.rg);\n"
656 "       locols.yz += group2.ab;\n"
657 "       hicols.yz += group8.rg;\n"
658 "       float4 midcols = float4(group1.rg, group3.rg) + float4(group7.ab, group9.ab) +\n"
659 "                               float4(group4.rg, group6.rg) + float4(group4.ab, group6.ab) +\n"
660 "                               lerp(locols, hicols, offset.y);\n"
661 "       float4 cols = group5 + float4(group2.rg, group8.ab);\n"
662 "       cols.xyz += lerp(midcols.xyz, midcols.yzw, offset.x);\n"
663 "       f = dot(cols, float4(1.0/25.0));\n"
664 "#      else\n"
665 "       float4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));\n"
666 "       float4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));\n"
667 "       float4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));\n"
668 "       float4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));\n"
669 "       float4 cols = float4(group1.rg, group2.rg) + float4(group3.ab, group4.ab) +\n"
670 "                               lerp(float4(group1.ab, group2.ab), float4(group3.rg, group4.rg), offset.y);\n"
671 "       f = dot(lerp(cols.xyz, cols.yzw, offset.x), float3(1.0/9.0));\n"
672 "#      endif\n"
673 "#     else\n"
674 "#      ifdef GL_EXT_gpu_shader4\n"
675 "#        define texval(x, y) tex2DOffset(Texture_ShadowMap2D, center, int2(x, y)).r\n"
676 "#      else\n"
677 "#        define texval(x, y) texDepth2D(Texture_ShadowMap2D, center + float2(x, y)*ShadowMap_TextureScale).r  \n"
678 "#      endif\n"
679 "#      if USESHADOWMAPPCF > 1\n"
680 "       float2 center = shadowmaptc.xy - 0.5, offset = frac(center);\n"
681 "       center *= ShadowMap_TextureScale;\n"
682 "       float4 row1 = step(shadowmaptc.z, float4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));\n"
683 "       float4 row2 = step(shadowmaptc.z, float4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));\n"
684 "       float4 row3 = step(shadowmaptc.z, float4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));\n"
685 "       float4 row4 = step(shadowmaptc.z, float4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));\n"
686 "       float4 cols = row2 + row3 + lerp(row1, row4, offset.y);\n"
687 "       f = dot(lerp(cols.xyz, cols.yzw, offset.x), float3(1.0/9.0));\n"
688 "#      else\n"
689 "       float2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = frac(shadowmaptc.xy);\n"
690 "       float3 row1 = step(shadowmaptc.z, float3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
691 "       float3 row2 = step(shadowmaptc.z, float3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
692 "       float3 row3 = step(shadowmaptc.z, float3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
693 "       float3 cols = row2 + lerp(row1, row3, offset.y);\n"
694 "       f = dot(lerp(cols.xy, cols.yz, offset.x), float2(0.25,0.25));\n"
695 "#      endif\n"
696 "#     endif\n"
697 "#    else\n"
698 "       f = step(shadowmaptc.z, tex2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n"
699 "#    endif\n"
700 "#  endif\n"
701 "#  ifdef USESHADOWMAPORTHO\n"
702 "       return lerp(ShadowMap_Parameters.w, 1.0, f);\n"
703 "#  else\n"
704 "       return f;\n"
705 "#  endif\n"
706 "}\n"
707 "# endif\n"
708 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO)\n"
709 "#endif // FRAGMENT_SHADER\n"
710 "\n"
711 "\n"
712 "\n"
713 "\n"
714 "#ifdef MODE_DEFERREDGEOMETRY\n"
715 "#ifdef VERTEX_SHADER\n"
716 "void main\n"
717 "(\n"
718 "float4 gl_Vertex : POSITION,\n"
719 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
720 "#ifdef USEVERTEXTEXTUREBLEND\n"
721 "float4 gl_Color : COLOR0,\n"
722 "#endif\n"
723 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
724 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
725 "float4 gl_MultiTexCoord2 : TEXCOORD2,\n"
726 "float4 gl_MultiTexCoord3 : TEXCOORD3,\n"
727 "uniform float4x4 TexMatrix : register(c0),\n"
728 "#ifdef USEVERTEXTEXTUREBLEND\n"
729 "uniform float4x4 BackgroundTexMatrix : register(c4),\n"
730 "#endif\n"
731 "uniform float4x4 ModelViewMatrix : register(c12),\n"
732 "#ifdef USEOFFSETMAPPING\n"
733 "uniform float3 EyePosition : register(c24),\n"
734 "#endif\n"
735 "out float4 gl_Position : POSITION,\n"
736 "#ifdef USEVERTEXTEXTUREBLEND\n"
737 "out float4 gl_FrontColor : COLOR,\n"
738 "#endif\n"
739 "out float4 TexCoordBoth : TEXCOORD0,\n"
740 "#ifdef USEOFFSETMAPPING\n"
741 "out float3 EyeVector : TEXCOORD2,\n"
742 "#endif\n"
743 "out float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
744 "out float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
745 "out float4 VectorR : TEXCOORD7 // direction of R texcoord (surface normal), Depth value\n"
746 ")\n"
747 "{\n"
748 "       TexCoordBoth = mul(TexMatrix, gl_MultiTexCoord0);\n"
749 "#ifdef USEVERTEXTEXTUREBLEND\n"
750 "       gl_FrontColor = gl_Color;\n"
751 "       TexCoordBoth.zw = float2(Backgroundmul(TexMatrix, gl_MultiTexCoord0));\n"
752 "#endif\n"
753 "\n"
754 "       // transform unnormalized eye direction into tangent space\n"
755 "#ifdef USEOFFSETMAPPING\n"
756 "       float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
757 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
758 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
759 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
760 "#endif\n"
761 "\n"
762 "       VectorS = mul(ModelViewMatrix, float4(gl_MultiTexCoord1.xyz, 0)).xyz;\n"
763 "       VectorT = mul(ModelViewMatrix, float4(gl_MultiTexCoord2.xyz, 0)).xyz;\n"
764 "       VectorR.xyz = mul(ModelViewMatrix, float4(gl_MultiTexCoord3.xyz, 0)).xyz;\n"
765 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
766 "#ifdef USETRIPPY\n"
767 "       gl_Position = TrippyVertex(gl_Position);\n"
768 "#endif\n"
769 "       VectorR.w = gl_Position.z;\n"
770 "}\n"
771 "#endif // VERTEX_SHADER\n"
772 "\n"
773 "#ifdef FRAGMENT_SHADER\n"
774 "void main\n"
775 "(\n"
776 "float4 TexCoordBoth : TEXCOORD0,\n"
777 "float3 EyeVector : TEXCOORD2,\n"
778 "float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
779 "float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
780 "float4 VectorR : TEXCOORD7, // direction of R texcoord (surface normal), Depth value\n"
781 "uniform sampler Texture_Normal : register(s0),\n"
782 "#ifdef USEALPHAKILL\n"
783 "uniform sampler Texture_Color : register(s1),\n"
784 "#endif\n"
785 "uniform sampler Texture_Gloss : register(s2),\n"
786 "#ifdef USEVERTEXTEXTUREBLEND\n"
787 "uniform sampler Texture_SecondaryNormal : register(s4),\n"
788 "uniform sampler Texture_SecondaryGloss : register(s6),\n"
789 "#endif\n"
790 "#ifdef USEOFFSETMAPPING\n"
791 "uniform float4 OffsetMapping_ScaleSteps : register(c24),\n"
792 "#endif\n"
793 "uniform half SpecularPower : register(c36),\n"
794 "#ifdef HLSL\n"
795 "out float4 gl_FragData0 : COLOR0,\n"
796 "out float4 gl_FragData1 : COLOR1\n"
797 "#else\n"
798 "out float4 gl_FragColor : COLOR\n"
799 "#endif\n"
800 ")\n"
801 "{\n"
802 "       float2 TexCoord = TexCoordBoth.xy;\n"
803 "#ifdef USEOFFSETMAPPING\n"
804 "       // apply offsetmapping\n"
805 "       float2 dPdx = ddx(TexCoord);\n"
806 "       float2 dPdy = ddy(TexCoord);\n"
807 "       float2 TexCoordOffset = OffsetMapping(TexCoord, OffsetMapping_ScaleSteps, EyeVector, Texture_Normal, dPdx, dPdy);\n"
808 "# define offsetMappedTexture2D(t) tex2Dgrad(t, TexCoordOffset, dPdx, dPdy)\n"
809 "#else\n"
810 "# define offsetMappedTexture2D(t) tex2D(t, TexCoord)\n"
811 "#endif\n"
812 "\n"
813 "#ifdef USEALPHAKILL\n"
814 "       if (offsetMappedTexture2D(Texture_Color).a < 0.5)\n"
815 "               discard;\n"
816 "#endif\n"
817 "\n"
818 "#ifdef USEVERTEXTEXTUREBLEND\n"
819 "       float alpha = offsetMappedTexture2D(Texture_Color).a;\n"
820 "       float terrainblend = clamp(float(gl_FrontColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0));\n"
821 "       //float terrainblend = min(float(gl_FrontColor.a) * alpha * 2.0, float(1.0));\n"
822 "       //float terrainblend = float(gl_FrontColor.a) * alpha > 0.5;\n"
823 "#endif\n"
824 "\n"
825 "#ifdef USEVERTEXTEXTUREBLEND\n"
826 "       float3 surfacenormal = lerp(tex2D(Texture_SecondaryNormal, TexCoord2).rgb, offsetMappedTexture2D(Texture_Normal).rgb, terrainblend) - float3(0.5, 0.5, 0.5);\n"
827 "       float a = lerp(tex2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend);\n"
828 "#else\n"
829 "       float3 surfacenormal = offsetMappedTexture2D(Texture_Normal).rgb - float3(0.5, 0.5, 0.5);\n"
830 "       float a = offsetMappedTexture2D(Texture_Gloss).a;\n"
831 "#endif\n"
832 "\n"
833 "#ifdef HLSL\n"
834 "       gl_FragData0 = float4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR.xyz) * 0.5 + float3(0.5, 0.5, 0.5), a);\n"
835 "       float Depth = VectorR.w / 256.0;\n"
836 "       float4 depthcolor = float4(Depth,Depth*65536.0/255.0,Depth*16777216.0/255.0,0.0);\n"
837 "//     float4 depthcolor = float4(Depth,Depth*256.0,Depth*65536.0,0.0);\n"
838 "       depthcolor.yz -= floor(depthcolor.yz);\n"
839 "       gl_FragData1 = depthcolor;\n"
840 "#else\n"
841 "       gl_FragColor = float4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR) * 0.5 + float3(0.5, 0.5, 0.5), a);\n"
842 "#endif\n"
843 "}\n"
844 "#endif // FRAGMENT_SHADER\n"
845 "#else // !MODE_DEFERREDGEOMETRY\n"
846 "\n"
847 "\n"
848 "\n"
849 "\n"
850 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
851 "#ifdef VERTEX_SHADER\n"
852 "void main\n"
853 "(\n"
854 "float4 gl_Vertex : POSITION,\n"
855 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
856 "uniform float4x4 ModelViewMatrix : register(c12),\n"
857 "out float4 gl_Position : POSITION,\n"
858 "out float4 ModelViewPosition : TEXCOORD0\n"
859 ")\n"
860 "{\n"
861 "       ModelViewPosition = mul(ModelViewMatrix, gl_Vertex);\n"
862 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
863 "}\n"
864 "#endif // VERTEX_SHADER\n"
865 "\n"
866 "#ifdef FRAGMENT_SHADER\n"
867 "void main\n"
868 "(\n"
869 "#ifdef HLSL\n"
870 "float2 Pixel : VPOS,\n"
871 "#else\n"
872 "float2 Pixel : WPOS,\n"
873 "#endif\n"
874 "float4 ModelViewPosition : TEXCOORD0,\n"
875 "uniform float4x4 ViewToLight : register(c44),\n"
876 "uniform float2 ScreenToDepth : register(c33), // ScreenToDepth = float2(Far / (Far - Near), Far * Near / (Near - Far));\n"
877 "uniform float3 LightPosition : register(c23),\n"
878 "uniform half2 PixelToScreenTexCoord : register(c42),\n"
879 "uniform half3 DeferredColor_Ambient : register(c9),\n"
880 "uniform half3 DeferredColor_Diffuse : register(c10),\n"
881 "#ifdef USESPECULAR\n"
882 "uniform half3 DeferredColor_Specular : register(c11),\n"
883 "uniform half SpecularPower : register(c36),\n"
884 "#endif\n"
885 "uniform sampler Texture_Attenuation : register(s9),\n"
886 "uniform sampler Texture_ScreenDepth : register(s13),\n"
887 "uniform sampler Texture_ScreenNormalMap : register(s14),\n"
888 "\n"
889 "#ifdef USECUBEFILTER\n"
890 "uniform samplerCUBE Texture_Cube : register(s10),\n"
891 "#endif\n"
892 "\n"
893 "#ifdef USESHADOWMAP2D\n"
894 "# ifdef USESHADOWSAMPLER\n"
895 "uniform sampler Texture_ShadowMap2D : register(s15),\n"
896 "# else\n"
897 "uniform sampler Texture_ShadowMap2D : register(s15),\n"
898 "# endif\n"
899 "#endif\n"
900 "\n"
901 "#ifdef USESHADOWMAPVSDCT\n"
902 "uniform samplerCUBE Texture_CubeProjection : register(s12),\n"
903 "#endif\n"
904 "\n"
905 "#if defined(USESHADOWMAP2D)\n"
906 "uniform float2 ShadowMap_TextureScale : register(c35),\n"
907 "uniform float4 ShadowMap_Parameters : register(c34),\n"
908 "#endif\n"
909 "\n"
910 "out float4 gl_FragData0 : COLOR0,\n"
911 "out float4 gl_FragData1 : COLOR1\n"
912 ")\n"
913 "{\n"
914 "       // calculate viewspace pixel position\n"
915 "       float2 ScreenTexCoord = Pixel * PixelToScreenTexCoord;\n"
916 "       //ScreenTexCoord.y = ScreenTexCoord.y * -1 + 1; // Cg is opposite?\n"
917 "       float3 position;\n"
918 "#ifdef HLSL\n"
919 "       position.z = texDepth2D(Texture_ScreenDepth, ScreenTexCoord) * 256.0;\n"
920 "#else\n"
921 "       position.z = ScreenToDepth.y / (texDepth2D(Texture_ScreenDepth, ScreenTexCoord) + ScreenToDepth.x);\n"
922 "#endif\n"
923 "       position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);\n"
924 "       // decode viewspace pixel normal\n"
925 "       half4 normalmap = half4(tex2D(Texture_ScreenNormalMap, ScreenTexCoord));\n"
926 "       half3 surfacenormal = half3(normalize(normalmap.rgb - half3(0.5,0.5,0.5)));\n"
927 "       // surfacenormal = pixel normal in viewspace\n"
928 "       // LightVector = pixel to light in viewspace\n"
929 "       // CubeVector = position in lightspace\n"
930 "       // eyevector = pixel to view in viewspace\n"
931 "       float3 CubeVector = mul(ViewToLight, float4(position,1)).xyz;\n"
932 "       half fade = half(tex2D(Texture_Attenuation, float2(length(CubeVector), 0.0)).r);\n"
933 "#ifdef USEDIFFUSE\n"
934 "       // calculate diffuse shading\n"
935 "       half3 lightnormal = half3(normalize(LightPosition - position));\n"
936 "       half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
937 "#endif\n"
938 "#ifdef USESPECULAR\n"
939 "       // calculate directional shading\n"
940 "       float3 eyevector = position * -1.0;\n"
941 "#  ifdef USEEXACTSPECULARMATH\n"
942 "       half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, FLT_MIN)), SpecularPower * normalmap.a));\n"
943 "#  else\n"
944 "       half3 specularnormal = half3(normalize(lightnormal + half3(normalize(eyevector))));\n"
945 "       half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), FLT_MIN)), SpecularPower * normalmap.a));\n"
946 "#  endif\n"
947 "#endif\n"
948 "\n"
949 "#if defined(USESHADOWMAP2D)\n"
950 "       fade *= half(ShadowMapCompare(CubeVector, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale\n"
951 "#ifdef USESHADOWMAPVSDCT\n"
952 ", Texture_CubeProjection\n"
953 "#endif\n"
954 "       ));\n"
955 "#endif\n"
956 "\n"
957 "#ifdef USEDIFFUSE\n"
958 "       gl_FragData0 = float4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);\n"
959 "#else\n"
960 "       gl_FragData0 = float4(DeferredColor_Ambient * fade, 1.0);\n"
961 "#endif\n"
962 "#ifdef USESPECULAR\n"
963 "       gl_FragData1 = float4(DeferredColor_Specular * (specular * fade), 1.0);\n"
964 "#else\n"
965 "       gl_FragData1 = float4(0.0, 0.0, 0.0, 1.0);\n"
966 "#endif\n"
967 "\n"
968 "# ifdef USECUBEFILTER\n"
969 "       float3 cubecolor = texCUBE(Texture_Cube, CubeVector).rgb;\n"
970 "       gl_FragData0.rgb *= cubecolor;\n"
971 "       gl_FragData1.rgb *= cubecolor;\n"
972 "# endif\n"
973 "}\n"
974 "#endif // FRAGMENT_SHADER\n"
975 "#else // !MODE_DEFERREDLIGHTSOURCE\n"
976 "\n"
977 "\n"
978 "\n"
979 "\n"
980 "#ifdef VERTEX_SHADER\n"
981 "void main\n"
982 "(\n"
983 "float4 gl_Vertex : POSITION,\n"
984 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
985 "#if defined(USEVERTEXTEXTUREBLEND) || defined(MODE_VERTEXCOLOR) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR)\n"
986 "float4 gl_Color : COLOR0,\n"
987 "#endif\n"
988 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
989 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
990 "float4 gl_MultiTexCoord2 : TEXCOORD2,\n"
991 "float4 gl_MultiTexCoord3 : TEXCOORD3,\n"
992 "float4 gl_MultiTexCoord4 : TEXCOORD4,\n"
993 "\n"
994 "uniform float3 EyePosition : register(c24),\n"
995 "uniform float4x4 TexMatrix : register(c0),\n"
996 "#ifdef USEVERTEXTEXTUREBLEND\n"
997 "uniform float4x4 BackgroundTexMatrix : register(c4),\n"
998 "#endif\n"
999 "#ifdef MODE_LIGHTSOURCE\n"
1000 "uniform float4x4 ModelToLight : register(c20),\n"
1001 "#endif\n"
1002 "#ifdef MODE_LIGHTSOURCE\n"
1003 "uniform float3 LightPosition : register(c27),\n"
1004 "#endif\n"
1005 "#ifdef MODE_LIGHTDIRECTION\n"
1006 "uniform float3 LightDir : register(c26),\n"
1007 "#endif\n"
1008 "uniform float4 FogPlane : register(c25),\n"
1009 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
1010 "uniform float3 LightPosition : register(c27),\n"
1011 "#endif\n"
1012 "#ifdef USESHADOWMAPORTHO\n"
1013 "uniform float4x4 ShadowMapMatrix : register(c16),\n"
1014 "#endif\n"
1015 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND)\n"
1016 "out float4 gl_FrontColor : COLOR,\n"
1017 "#endif\n"
1018 "out float4 TexCoordBoth : TEXCOORD0,\n"
1019 "#ifdef USELIGHTMAP\n"
1020 "out float2 TexCoordLightmap : TEXCOORD1,\n"
1021 "#endif\n"
1022 "#ifdef USEEYEVECTOR\n"
1023 "out float3 EyeVector : TEXCOORD2,\n"
1024 "#endif\n"
1025 "#ifdef USEREFLECTION\n"
1026 "out float4 ModelViewProjectionPosition : TEXCOORD3,\n"
1027 "#endif\n"
1028 "#ifdef USEFOG\n"
1029 "out float4 EyeVectorModelSpaceFogPlaneVertexDist : TEXCOORD4,\n"
1030 "#endif\n"
1031 "#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) || defined(USEDIFFUSE)\n"
1032 "out float3 LightVector : TEXCOORD1,\n"
1033 "#endif\n"
1034 "#ifdef MODE_LIGHTSOURCE\n"
1035 "out float3 CubeVector : TEXCOORD3,\n"
1036 "#endif\n"
1037 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE)\n"
1038 "out float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
1039 "out float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
1040 "out float3 VectorR : TEXCOORD7, // direction of R texcoord (surface normal)\n"
1041 "#endif\n"
1042 "#ifdef USESHADOWMAPORTHO\n"
1043 "out float3 ShadowMapTC : TEXCOORD3, // CONFLICTS WITH USEREFLECTION!\n"
1044 "#endif\n"
1045 "out float4 gl_Position : POSITION\n"
1046 ")\n"
1047 "{\n"
1048 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND)\n"
1049 "       gl_FrontColor = gl_Color;\n"
1050 "#endif\n"
1051 "       // copy the surface texcoord\n"
1052 "       TexCoordBoth = mul(TexMatrix, gl_MultiTexCoord0);\n"
1053 "#ifdef USEVERTEXTEXTUREBLEND\n"
1054 "       TexCoordBoth.zw = mul(BackgroundTexMatrix, gl_MultiTexCoord0).xy;\n"
1055 "#endif\n"
1056 "#ifdef USELIGHTMAP\n"
1057 "       TexCoordLightmap = gl_MultiTexCoord4.xy;\n"
1058 "#endif\n"
1059 "\n"
1060 "#ifdef MODE_LIGHTSOURCE\n"
1061 "       // transform vertex position into light attenuation/cubemap space\n"
1062 "       // (-1 to +1 across the light box)\n"
1063 "       CubeVector = mul(ModelToLight, gl_Vertex).xyz;\n"
1064 "\n"
1065 "# ifdef USEDIFFUSE\n"
1066 "       // transform unnormalized light direction into tangent space\n"
1067 "       // (we use unnormalized to ensure that it interpolates correctly and then\n"
1068 "       //  normalize it per pixel)\n"
1069 "       float3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
1070 "       LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
1071 "       LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
1072 "       LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
1073 "# endif\n"
1074 "#endif\n"
1075 "\n"
1076 "#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE)\n"
1077 "       LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
1078 "       LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
1079 "       LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
1080 "#endif\n"
1081 "\n"
1082 "       // transform unnormalized eye direction into tangent space\n"
1083 "#ifdef USEEYEVECTOR\n"
1084 "       float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
1085 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
1086 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
1087 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
1088 "#endif\n"
1089 "\n"
1090 "#ifdef USEFOG\n"
1091 "       EyeVectorModelSpaceFogPlaneVertexDist.xyz = EyePosition - gl_Vertex.xyz;\n"
1092 "       EyeVectorModelSpaceFogPlaneVertexDist.w = dot(FogPlane, gl_Vertex);\n"
1093 "#endif\n"
1094 "\n"
1095 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1096 "       VectorS = gl_MultiTexCoord1.xyz;\n"
1097 "       VectorT = gl_MultiTexCoord2.xyz;\n"
1098 "       VectorR = gl_MultiTexCoord3.xyz;\n"
1099 "#endif\n"
1100 "\n"
1101 "       // transform vertex to camera space, using ftransform to match non-VS rendering\n"
1102 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
1103 "\n"
1104 "#ifdef USESHADOWMAPORTHO\n"
1105 "       ShadowMapTC = mul(ShadowMapMatrix, gl_Position).xyz;\n"
1106 "#endif\n"
1107 "\n"
1108 "#ifdef USEREFLECTION\n"
1109 "       ModelViewProjectionPosition = gl_Position;\n"
1110 "#endif\n"
1111 "#ifdef USETRIPPY\n"
1112 "       gl_Position = TrippyVertex(gl_Position);\n"
1113 "#endif\n"
1114 "}\n"
1115 "#endif // VERTEX_SHADER\n"
1116 "\n"
1117 "\n"
1118 "\n"
1119 "\n"
1120 "#ifdef FRAGMENT_SHADER\n"
1121 "void main\n"
1122 "(\n"
1123 "#ifdef USEDEFERREDLIGHTMAP\n"
1124 "#ifdef HLSL\n"
1125 "float2 Pixel : VPOS,\n"
1126 "#else\n"
1127 "float2 Pixel : WPOS,\n"
1128 "#endif\n"
1129 "#endif\n"
1130 "float4 gl_FrontColor : COLOR,\n"
1131 "float4 TexCoordBoth : TEXCOORD0,\n"
1132 "#ifdef USELIGHTMAP\n"
1133 "float2 TexCoordLightmap : TEXCOORD1,\n"
1134 "#endif\n"
1135 "#ifdef USEEYEVECTOR\n"
1136 "float3 EyeVector : TEXCOORD2,\n"
1137 "#endif\n"
1138 "#ifdef USEREFLECTION\n"
1139 "float4 ModelViewProjectionPosition : TEXCOORD3,\n"
1140 "#endif\n"
1141 "#ifdef USEFOG\n"
1142 "float4 EyeVectorModelSpaceFogPlaneVertexDist : TEXCOORD4,\n"
1143 "#endif\n"
1144 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)\n"
1145 "float3 LightVector : TEXCOORD1,\n"
1146 "#endif\n"
1147 "#ifdef MODE_LIGHTSOURCE\n"
1148 "float3 CubeVector : TEXCOORD3,\n"
1149 "#endif\n"
1150 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
1151 "float4 ModelViewPosition : TEXCOORD0,\n"
1152 "#endif\n"
1153 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE)\n"
1154 "float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
1155 "float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
1156 "float3 VectorR : TEXCOORD7, // direction of R texcoord (surface normal)\n"
1157 "#endif\n"
1158 "#ifdef USESHADOWMAPORTHO\n"
1159 "float3 ShadowMapTC : TEXCOORD3, // CONFLICTS WITH USEREFLECTION!\n"
1160 "#endif\n"
1161 "\n"
1162 "uniform sampler Texture_Normal : register(s0),\n"
1163 "uniform sampler Texture_Color : register(s1),\n"
1164 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
1165 "uniform sampler Texture_Gloss : register(s2),\n"
1166 "#endif\n"
1167 "#ifdef USEGLOW\n"
1168 "uniform sampler Texture_Glow : register(s3),\n"
1169 "#endif\n"
1170 "#ifdef USEVERTEXTEXTUREBLEND\n"
1171 "uniform sampler Texture_SecondaryNormal : register(s4),\n"
1172 "uniform sampler Texture_SecondaryColor : register(s5),\n"
1173 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
1174 "uniform sampler Texture_SecondaryGloss : register(s6),\n"
1175 "#endif\n"
1176 "#ifdef USEGLOW\n"
1177 "uniform sampler Texture_SecondaryGlow : register(s7),\n"
1178 "#endif\n"
1179 "#endif\n"
1180 "#ifdef USECOLORMAPPING\n"
1181 "uniform sampler Texture_Pants : register(s4),\n"
1182 "uniform sampler Texture_Shirt : register(s7),\n"
1183 "#endif\n"
1184 "#ifdef USEFOG\n"
1185 "uniform sampler Texture_FogHeightTexture : register(s14),\n"
1186 "uniform sampler Texture_FogMask : register(s8),\n"
1187 "#endif\n"
1188 "#ifdef USELIGHTMAP\n"
1189 "uniform sampler Texture_Lightmap : register(s9),\n"
1190 "#endif\n"
1191 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
1192 "uniform sampler Texture_Deluxemap : register(s10),\n"
1193 "#endif\n"
1194 "#ifdef USEREFLECTION\n"
1195 "uniform sampler Texture_Reflection : register(s7),\n"
1196 "#endif\n"
1197 "\n"
1198 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
1199 "uniform sampler Texture_ScreenDepth : register(s13),\n"
1200 "uniform sampler Texture_ScreenNormalMap : register(s14),\n"
1201 "#endif\n"
1202 "#ifdef USEDEFERREDLIGHTMAP\n"
1203 "uniform sampler Texture_ScreenDepth : register(s13),\n"
1204 "uniform sampler Texture_ScreenNormalMap : register(s14),\n"
1205 "uniform sampler Texture_ScreenDiffuse : register(s11),\n"
1206 "uniform sampler Texture_ScreenSpecular : register(s12),\n"
1207 "#endif\n"
1208 "\n"
1209 "#ifdef USECOLORMAPPING\n"
1210 "uniform half3 Color_Pants : register(c7),\n"
1211 "uniform half3 Color_Shirt : register(c8),\n"
1212 "#endif\n"
1213 "#ifdef USEFOG\n"
1214 "uniform float3 FogColor : register(c16),\n"
1215 "uniform float FogRangeRecip : register(c20),\n"
1216 "uniform float FogPlaneViewDist : register(c19),\n"
1217 "uniform float FogHeightFade : register(c17),\n"
1218 "#endif\n"
1219 "\n"
1220 "#ifdef USEOFFSETMAPPING\n"
1221 "uniform float4 OffsetMapping_ScaleSteps : register(c24),\n"
1222 "#endif\n"
1223 "\n"
1224 "#ifdef USEDEFERREDLIGHTMAP\n"
1225 "uniform half2 PixelToScreenTexCoord : register(c42),\n"
1226 "uniform half3 DeferredMod_Diffuse : register(c12),\n"
1227 "uniform half3 DeferredMod_Specular : register(c13),\n"
1228 "#endif\n"
1229 "uniform half3 Color_Ambient : register(c3),\n"
1230 "uniform half3 Color_Diffuse : register(c4),\n"
1231 "uniform half3 Color_Specular : register(c5),\n"
1232 "uniform half SpecularPower : register(c36),\n"
1233 "#ifdef USEGLOW\n"
1234 "uniform half3 Color_Glow : register(c6),\n"
1235 "#endif\n"
1236 "uniform half Alpha : register(c0),\n"
1237 "#ifdef USEREFLECTION\n"
1238 "uniform float4 DistortScaleRefractReflect : register(c14),\n"
1239 "uniform float4 ScreenScaleRefractReflect : register(c32),\n"
1240 "uniform float4 ScreenCenterRefractReflect : register(c31),\n"
1241 "uniform half4 ReflectColor : register(c26),\n"
1242 "#endif\n"
1243 "#ifdef USEREFLECTCUBE\n"
1244 "uniform float4x4 ModelToReflectCube : register(c48),\n"
1245 "uniform sampler Texture_ReflectMask : register(s5),\n"
1246 "uniform samplerCUBE Texture_ReflectCube : register(s6),\n"
1247 "#endif\n"
1248 "#ifdef MODE_LIGHTDIRECTION\n"
1249 "uniform half3 LightColor : register(c21),\n"
1250 "#endif\n"
1251 "#ifdef MODE_LIGHTSOURCE\n"
1252 "uniform half3 LightColor : register(c21),\n"
1253 "#endif\n"
1254 "\n"
1255 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)\n"
1256 "uniform sampler Texture_Attenuation : register(s9),\n"
1257 "uniform samplerCUBE Texture_Cube : register(s10),\n"
1258 "#endif\n"
1259 "\n"
1260 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO)\n"
1261 "\n"
1262 "#ifdef USESHADOWMAP2D\n"
1263 "# ifdef USESHADOWSAMPLER\n"
1264 "uniform sampler Texture_ShadowMap2D : register(s15),\n"
1265 "# else\n"
1266 "uniform sampler Texture_ShadowMap2D : register(s15),\n"
1267 "# endif\n"
1268 "#endif\n"
1269 "\n"
1270 "#ifdef USESHADOWMAPVSDCT\n"
1271 "uniform samplerCUBE Texture_CubeProjection : register(s12),\n"
1272 "#endif\n"
1273 "\n"
1274 "#if defined(USESHADOWMAP2D)\n"
1275 "uniform float2 ShadowMap_TextureScale : register(c35),\n"
1276 "uniform float4 ShadowMap_Parameters : register(c34),\n"
1277 "#endif\n"
1278 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO)\n"
1279 "\n"
1280 "out float4 gl_FragColor : COLOR\n"
1281 ")\n"
1282 "{\n"
1283 "       float2 TexCoord = TexCoordBoth.xy;\n"
1284 "#ifdef USEVERTEXTEXTUREBLEND\n"
1285 "       float2 TexCoord2 = TexCoordBoth.zw;\n"
1286 "#endif\n"
1287 "#ifdef USEOFFSETMAPPING\n"
1288 "       // apply offsetmapping\n"
1289 "       float2 dPdx = ddx(TexCoord);\n"
1290 "       float2 dPdy = ddy(TexCoord);\n"
1291 "       float2 TexCoordOffset = OffsetMapping(TexCoord, OffsetMapping_ScaleSteps, EyeVector, Texture_Normal, dPdx, dPdy);\n"
1292 "# define offsetMappedTexture2D(t) tex2Dgrad(t, TexCoordOffset, dPdx, dPdy)\n"
1293 "#else\n"
1294 "# define offsetMappedTexture2D(t) tex2D(t, TexCoord)\n"
1295 "#endif\n"
1296 "\n"
1297 "       // combine the diffuse textures (base, pants, shirt)\n"
1298 "       half4 color = half4(offsetMappedTexture2D(Texture_Color));\n"
1299 "#ifdef USEALPHAKILL\n"
1300 "       if (color.a < 0.5)\n"
1301 "               discard;\n"
1302 "#endif\n"
1303 "       color.a *= Alpha;\n"
1304 "#ifdef USECOLORMAPPING\n"
1305 "       color.rgb += half3(offsetMappedTexture2D(Texture_Pants).rgb) * Color_Pants + half3(offsetMappedTexture2D(Texture_Shirt).rgb) * Color_Shirt;\n"
1306 "#endif\n"
1307 "#ifdef USEVERTEXTEXTUREBLEND\n"
1308 "#ifdef USEBOTHALPHAS\n"
1309 "       half4 color2 = half4(tex2D(Texture_SecondaryColor, TexCoord2));\n"
1310 "       half terrainblend = clamp(half(gl_FrontColor.a) * color.a, half(1.0 - color2.a), half(1.0));\n"
1311 "       color.rgb = lerp(color2.rgb, color.rgb, terrainblend);\n"
1312 "#else\n"
1313 "       half terrainblend = clamp(half(gl_FrontColor.a) * color.a * 2.0 - 0.5, half(0.0), half(1.0));\n"
1314 "       //half terrainblend = min(half(gl_FrontColor.a) * color.a * 2.0, half(1.0));\n"
1315 "       //half terrainblend = half(gl_FrontColor.a) * color.a > 0.5;\n"
1316 "       color.rgb = half3(lerp(tex2D(Texture_SecondaryColor, TexCoord2).rgb, float3(color.rgb), terrainblend));\n"
1317 "       color.a = 1.0;\n"
1318 "       //color = half4(lerp(float4(1, 0, 0, 1), color, terrainblend));\n"
1319 "#endif\n"
1320 "#endif\n"
1321 "\n"
1322 "       // get the surface normal\n"
1323 "#ifdef USEVERTEXTEXTUREBLEND\n"
1324 "       half3 surfacenormal = normalize(half3(lerp(tex2D(Texture_SecondaryNormal, TexCoord2).rgb, offsetMappedTexture2D(Texture_Normal).rgb, terrainblend)) - half3(0.5, 0.5, 0.5));\n"
1325 "#else\n"
1326 "       half3 surfacenormal = half3(normalize(half3(offsetMappedTexture2D(Texture_Normal).rgb) - half3(0.5, 0.5, 0.5)));\n"
1327 "#endif\n"
1328 "\n"
1329 "       // get the material colors\n"
1330 "       half3 diffusetex = color.rgb;\n"
1331 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
1332 "# ifdef USEVERTEXTEXTUREBLEND\n"
1333 "       half4 glosstex = half4(lerp(tex2D(Texture_SecondaryGloss, TexCoord2), offsetMappedTexture2D(Texture_Gloss), terrainblend));\n"
1334 "# else\n"
1335 "       half4 glosstex = half4(offsetMappedTexture2D(Texture_Gloss));\n"
1336 "# endif\n"
1337 "#endif\n"
1338 "\n"
1339 "#ifdef USEREFLECTCUBE\n"
1340 "       float3 TangentReflectVector = reflect(-EyeVector, surfacenormal);\n"
1341 "       float3 ModelReflectVector = TangentReflectVector.x * VectorS + TangentReflectVector.y * VectorT + TangentReflectVector.z * VectorR;\n"
1342 "       float3 ReflectCubeTexCoord = mul(ModelToReflectCube, float4(ModelReflectVector, 0)).xyz;\n"
1343 "       diffusetex += half3(offsetMappedTexture2D(Texture_ReflectMask).rgb) * half3(texCUBE(Texture_ReflectCube, ReflectCubeTexCoord).rgb);\n"
1344 "#endif\n"
1345 "\n"
1346 "\n"
1347 "\n"
1348 "\n"
1349 "#ifdef MODE_LIGHTSOURCE\n"
1350 "       // light source\n"
1351 "#ifdef USEDIFFUSE\n"
1352 "       half3 lightnormal = half3(normalize(LightVector));\n"
1353 "       half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1354 "       color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);\n"
1355 "#ifdef USESPECULAR\n"
1356 "#ifdef USEEXACTSPECULARMATH\n"
1357 "       half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, FLT_MIN)), SpecularPower * glosstex.a));\n"
1358 "#else\n"
1359 "       half3 specularnormal = half3(normalize(lightnormal + half3(normalize(EyeVector))));\n"
1360 "       half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), FLT_MIN)), SpecularPower * glosstex.a));\n"
1361 "#endif\n"
1362 "       color.rgb += glosstex.rgb * (specular * Color_Specular);\n"
1363 "#endif\n"
1364 "#else\n"
1365 "       color.rgb = diffusetex * Color_Ambient;\n"
1366 "#endif\n"
1367 "       color.rgb *= LightColor;\n"
1368 "       color.rgb *= half(tex2D(Texture_Attenuation, float2(length(CubeVector), 0.0)).r);\n"
1369 "#if defined(USESHADOWMAP2D)\n"
1370 "       color.rgb *= half(ShadowMapCompare(CubeVector, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale\n"
1371 "#ifdef USESHADOWMAPVSDCT\n"
1372 ", Texture_CubeProjection\n"
1373 "#endif\n"
1374 "       ));\n"
1375 "\n"
1376 "#endif\n"
1377 "# ifdef USECUBEFILTER\n"
1378 "       color.rgb *= half3(texCUBE(Texture_Cube, CubeVector).rgb);\n"
1379 "# endif\n"
1380 "\n"
1381 "#ifdef USESHADOWMAP2D\n"
1382 "#ifdef USESHADOWMAPVSDCT\n"
1383 "//     float3 shadowmaptc = GetShadowMapTC2D(CubeVector, ShadowMap_Parameters, Texture_CubeProjection);\n"
1384 "#else\n"
1385 "//     float3 shadowmaptc = GetShadowMapTC2D(CubeVector, ShadowMap_Parameters);\n"
1386 "#endif\n"
1387 "//     color.rgb = half3(tex2D(Texture_ShadowMap2D, float2(0.1,0.1)).rgb);\n"
1388 "//     color.rgb = half3(tex2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale).rgb);\n"
1389 "//     color.rgb = half3(shadowmaptc.xyz * float3(ShadowMap_TextureScale,1.0));\n"
1390 "//     color.r = half(texDepth2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale));\n"
1391 "//     color.rgb = half3(tex2D(Texture_ShadowMap2D, float2(0.1,0.1)).rgb);\n"
1392 "//     color.rgb = half3(tex2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale).rgb);\n"
1393 "//     color.rgb = half3(shadowmaptc.xyz * float3(ShadowMap_TextureScale,1.0));\n"
1394 "//     color.r = half(texDepth2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale));\n"
1395 "//     color.r = half(shadowmaptc.z - texDepth2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale));\n"
1396 "//     color.r = half(shadowmaptc.z);\n"
1397 "//     color.r = half(texDepth2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale));\n"
1398 "//     color.r = half(shadowmaptc.z);\n"
1399 "//     color.r = 1;\n"
1400 "//     color.rgb = abs(CubeVector);\n"
1401 "#endif\n"
1402 "//     color.rgb = half3(1,1,1);\n"
1403 "#endif // MODE_LIGHTSOURCE\n"
1404 "\n"
1405 "\n"
1406 "\n"
1407 "\n"
1408 "#ifdef MODE_LIGHTDIRECTION\n"
1409 "       #define SHADING\n"
1410 "       #ifdef USEDIFFUSE\n"
1411 "               half3 lightnormal = half3(normalize(LightVector));\n"
1412 "       #endif\n"
1413 "       #define lightcolor LightColor\n"
1414 "#endif // MODE_LIGHTDIRECTION\n"
1415 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1416 "       #define SHADING\n"
1417 "       // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe)\n"
1418 "       half3 lightnormal_modelspace = half3(tex2D(Texture_Deluxemap, TexCoordLightmap).rgb) * 2.0 + half3(-1.0, -1.0, -1.0);\n"
1419 "       half3 lightcolor = half3(tex2D(Texture_Lightmap, TexCoordLightmap).rgb);\n"
1420 "       // convert modelspace light vector to tangentspace\n"
1421 "       half3 lightnormal;\n"
1422 "       lightnormal.x = dot(lightnormal_modelspace, half3(VectorS));\n"
1423 "       lightnormal.y = dot(lightnormal_modelspace, half3(VectorT));\n"
1424 "       lightnormal.z = dot(lightnormal_modelspace, half3(VectorR));\n"
1425 "       // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
1426 "       // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar\n"
1427 "       // is used (the lightmap and deluxemap coords correspond to virtually random coordinates\n"
1428 "       // on that luxel, and NOT to its center, because recursive triangle subdivision is used\n"
1429 "       // to map the luxels to coordinates on the draw surfaces), which also causes\n"
1430 "       // deluxemaps to be wrong because light contributions from the wrong side of the surface\n"
1431 "       // are added up. To prevent divisions by zero or strong exaggerations, a max()\n"
1432 "       // nudge is done here at expense of some additional fps. This is ONLY needed for\n"
1433 "       // deluxemaps, tangentspace deluxemap avoid this problem by design.\n"
1434 "       lightcolor *= 1.0 / max(0.25, lightnormal.z);\n"
1435 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1436 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
1437 "       #define SHADING\n"
1438 "       // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
1439 "       half3 lightnormal = half3(tex2D(Texture_Deluxemap, TexCoordLightmap).rgb) * 2.0 + half3(-1.0, -1.0, -1.0);\n"
1440 "       half3 lightcolor = half3(tex2D(Texture_Lightmap, TexCoordLightmap).rgb);\n"
1441 "#endif\n"
1442 "#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR)\n"
1443 "       #define SHADING\n"
1444 "       // forced deluxemap on lightmapped/vertexlit surfaces\n"
1445 "       half3 lightnormal = half3(0.0, 0.0, 1.0);\n"
1446 "   #ifdef USELIGHTMAP\n"
1447 "               half3 lightcolor = half3(tex2D(Texture_Lightmap, TexCoordLightmap).rgb);\n"
1448 "   #else\n"
1449 "               half3 lightcolor = half3(gl_FrontColor.rgb);\n"
1450 "   #endif\n"
1451 "#endif\n"
1452 "#ifdef MODE_FAKELIGHT\n"
1453 "       #define SHADING\n"
1454 "       half3 lightnormal = half3(normalize(EyeVector));\n"
1455 "       half3 lightcolor = half3(1.0,1.0,1.0);\n"
1456 "#endif // MODE_FAKELIGHT\n"
1457 "\n"
1458 "\n"
1459 "\n"
1460 "\n"
1461 "#ifdef MODE_LIGHTMAP\n"
1462 "       color.rgb = diffusetex * (Color_Ambient + half3(tex2D(Texture_Lightmap, TexCoordLightmap).rgb) * Color_Diffuse);\n"
1463 "#endif // MODE_LIGHTMAP\n"
1464 "#ifdef MODE_VERTEXCOLOR\n"
1465 "       color.rgb = diffusetex * (Color_Ambient + half3(gl_FrontColor.rgb) * Color_Diffuse);\n"
1466 "#endif // MODE_VERTEXCOLOR\n"
1467 "#ifdef MODE_FLATCOLOR\n"
1468 "       color.rgb = diffusetex * Color_Ambient;\n"
1469 "#endif // MODE_FLATCOLOR\n"
1470 "\n"
1471 "\n"
1472 "\n"
1473 "\n"
1474 "#ifdef SHADING\n"
1475 "# ifdef USEDIFFUSE\n"
1476 "       half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1477 "#  ifdef USESPECULAR\n"
1478 "#   ifdef USEEXACTSPECULARMATH\n"
1479 "       half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, FLT_MIN)), SpecularPower * glosstex.a));\n"
1480 "#   else\n"
1481 "       half3 specularnormal = half3(normalize(lightnormal + half3(normalize(EyeVector))));\n"
1482 "       half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), FLT_MIN)), SpecularPower * glosstex.a));\n"
1483 "#   endif\n"
1484 "       color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor;\n"
1485 "#  else\n"
1486 "       color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor);\n"
1487 "#  endif\n"
1488 "# else\n"
1489 "       color.rgb = diffusetex * Color_Ambient;\n"
1490 "# endif\n"
1491 "#endif\n"
1492 "\n"
1493 "#ifdef USESHADOWMAPORTHO\n"
1494 "       color.rgb *= half(ShadowMapCompare(ShadowMapTC, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale));\n"
1495 "#endif\n"
1496 "\n"
1497 "#ifdef USEDEFERREDLIGHTMAP\n"
1498 "       float2 ScreenTexCoord = Pixel * PixelToScreenTexCoord;\n"
1499 "       color.rgb += diffusetex * half3(tex2D(Texture_ScreenDiffuse, ScreenTexCoord).rgb) * DeferredMod_Diffuse;\n"
1500 "       color.rgb += glosstex.rgb * half3(tex2D(Texture_ScreenSpecular, ScreenTexCoord).rgb) * DeferredMod_Specular;\n"
1501 "//     color.rgb = half3(tex2D(Texture_ScreenDepth, ScreenTexCoord).rgb);\n"
1502 "//     color.r = half(texDepth2D(Texture_ScreenDepth, ScreenTexCoord)) * 1.0;\n"
1503 "#endif\n"
1504 "\n"
1505 "#ifdef USEGLOW\n"
1506 "#ifdef USEVERTEXTEXTUREBLEND\n"
1507 "       color.rgb += half3(lerp(tex2D(Texture_SecondaryGlow, TexCoord2).rgb, offsetMappedTexture2D(Texture_Glow).rgb, terrainblend)) * Color_Glow;\n"
1508 "#else\n"
1509 "       color.rgb += half3(offsetMappedTexture2D(Texture_Glow).rgb) * Color_Glow;\n"
1510 "#endif\n"
1511 "#endif\n"
1512 "\n"
1513 "#ifdef USEFOG\n"
1514 "       color.rgb = half3(FogVertex(color, FogColor, EyeVectorModelSpaceFogPlaneVertexDist.xyz, EyeVectorModelSpaceFogPlaneVertexDist.w, FogRangeRecip, FogPlaneViewDist, FogHeightFade, Texture_FogMask, Texture_FogHeightTexture));\n"
1515 "#endif\n"
1516 "\n"
1517 "       // 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"
1518 "#ifdef USEREFLECTION\n"
1519 "       float4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
1520 "       //float4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(half3(offsetMappedTexture2D(Texture_Normal).rgb) - half3(0.5,0.5,0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
1521 "       float2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;\n"
1522 "       float2 ScreenTexCoord = SafeScreenTexCoord + float3(normalize(half3(offsetMappedTexture2D(Texture_Normal).rgb) - half3(0.5,0.5,0.5))).xy * DistortScaleRefractReflect.zw;\n"
1523 "       // FIXME temporary hack to detect the case that the reflection\n"
1524 "       // gets blackened at edges due to leaving the area that contains actual\n"
1525 "       // content.\n"
1526 "       // Remove this 'ack once we have a better way to stop this thing from\n"
1527 "       // 'appening.\n"
1528 "       float f = min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(0.01, 0.01)).rgb) / 0.05);\n"
1529 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(0.01, -0.01)).rgb) / 0.05);\n"
1530 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(-0.01, 0.01)).rgb) / 0.05);\n"
1531 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(-0.01, -0.01)).rgb) / 0.05);\n"
1532 "       ScreenTexCoord = lerp(SafeScreenTexCoord, ScreenTexCoord, f);\n"
1533 "       color.rgb = lerp(color.rgb, half3(tex2D(Texture_Reflection, ScreenTexCoord).rgb) * ReflectColor.rgb, ReflectColor.a);\n"
1534 "#endif\n"
1535 "\n"
1536 "       gl_FragColor = float4(color);\n"
1537 "}\n"
1538 "#endif // FRAGMENT_SHADER\n"
1539 "\n"
1540 "#endif // !MODE_DEFERREDLIGHTSOURCE\n"
1541 "#endif // !MODE_DEFERREDGEOMETRY\n"
1542 "#endif // !MODE_WATER\n"
1543 "#endif // !MODE_REFRACTION\n"
1544 "#endif // !MODE_BLOOMBLUR\n"
1545 "#endif // !MODE_GENERIC\n"
1546 "#endif // !MODE_POSTPROCESS\n"
1547 "#endif // !MODE_SHOWDEPTH\n"
1548 "#endif // !MODE_DEPTH_OR_SHADOW\n"