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