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