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