summaryrefslogtreecommitdiff
path: root/shaders/xonotic/46.shader_test
blob: 75de8bde3199e10e522d36bce36be8050d1ba80e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
[require]
GLSL >= 1.20

[vertex shader]
#version 120
#define GLSL120
#define VERTEX_SHADER
#define MODE_LIGHTDIRECTION
#define USEDIFFUSE
































#define USEEXACTSPECULARMATH




#define USEBOTHALPHAS
#define USEOFFSETMAPPING_LOD






// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader
// written by Forest 'LordHavoc' Hale
// shadowmapping enhancements by Lee 'eihrul' Salzman

#if defined(USESKELETAL) || defined(USEOCCLUDE)
#  ifdef GL_ARB_uniform_buffer_object
#    extension GL_ARB_uniform_buffer_object : enable
#  endif
#endif

#ifdef USESHADOWMAP2D
# ifdef GL_EXT_gpu_shader4
#   extension GL_EXT_gpu_shader4 : enable
# endif
# ifdef GL_ARB_texture_gather
#   extension GL_ARB_texture_gather : enable
# else
#   ifdef GL_AMD_texture_texture4
#     extension GL_AMD_texture_texture4 : enable
#   endif
# endif
#endif

#ifdef USECELSHADING
# define SHADEDIFFUSE myhalf diffuse = cast_myhalf(min(max(float(dot(surfacenormal, lightnormal)) * 2.0, 0.0), 1.0));
# ifdef USEEXACTSPECULARMATH
#  define SHADESPECULAR(specpow) myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), eyenormal))*-1.0, 0.0)), 1.0 + specpow);specular = max(0.0, specular * 10.0 - 9.0);
# else
#  define SHADESPECULAR(specpow) myhalf3 specularnormal = normalize(lightnormal + eyenormal);myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + specpow);specular = max(0.0, specular * 10.0 - 9.0);
# endif
#else
# define SHADEDIFFUSE myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));
# ifdef USEEXACTSPECULARMATH
#  define SHADESPECULAR(specpow) myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), eyenormal))*-1.0, 0.0)), 1.0 + specpow);
# else
#  define SHADESPECULAR(specpow) myhalf3 specularnormal = normalize(lightnormal + eyenormal);myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + specpow);
# endif
#endif

#if (defined(GLSL120) || defined(GLSL130) || defined(GLSL140) || defined(GLES)) && defined(VERTEX_SHADER)
invariant gl_Position; // fix for lighting polygons not matching base surface
# endif
#if defined(GLSL130) || defined(GLSL140)
precision highp float;
# ifdef VERTEX_SHADER
#  define dp_varying out
#  define dp_attribute in
# endif
# ifdef FRAGMENT_SHADER
out vec4 dp_FragColor;
#  define dp_varying in
#  define dp_attribute in
# endif
# define dp_offsetmapping_dFdx dFdx
# define dp_offsetmapping_dFdy dFdy
# define dp_textureGrad textureGrad
# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d))
# define dp_texture2D texture
# define dp_texture3D texture
# define dp_textureCube texture
# define dp_shadow2D(a,b) float(texture(a,b))
#else
# ifdef FRAGMENT_SHADER
#  define dp_FragColor gl_FragColor
# endif
# define dp_varying varying
# define dp_attribute attribute
# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0)
# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0)
# define dp_textureGrad(a,b,c,d) texture2D(a,b)
# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d))
# define dp_texture2D texture2D
# define dp_texture3D texture3D
# define dp_textureCube textureCube
# define dp_shadow2D(a,b) float(shadow2D(a,b))
#endif

// GL ES and GLSL130 shaders use precision modifiers, standard GL does not
// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout)
#ifndef GL_ES
#define lowp
#define mediump
#define highp
#endif

#ifdef USEDEPTHRGB
	// for 565 RGB we'd need to use different multipliers
#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0))
#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0)))
#endif

#ifdef VERTEX_SHADER
dp_attribute vec4 Attrib_Position;  // vertex
dp_attribute vec4 Attrib_Color;     // color
dp_attribute vec4 Attrib_TexCoord0; // material texcoords
dp_attribute vec3 Attrib_TexCoord1; // svector
dp_attribute vec3 Attrib_TexCoord2; // tvector
dp_attribute vec3 Attrib_TexCoord3; // normal
dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords
#ifdef USESKELETAL
//uniform mat4 Skeletal_Transform[128];
// this is used with glBindBufferRange to bind a uniform block to the name
// Skeletal_Transform12_UniformBlock, the Skeletal_Transform12 variable is
// directly accessible without a namespace.
// explanation: http://www.opengl.org/wiki/Interface_Block_%28GLSL%29#Syntax
uniform Skeletal_Transform12_UniformBlock
{
	vec4 Skeletal_Transform12[768];
};
dp_attribute vec4 Attrib_SkeletalIndex;
dp_attribute vec4 Attrib_SkeletalWeight;
#endif
#endif
dp_varying mediump vec4 VertexColor;

#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE)
# define USEFOG
#endif
#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP)
# define USELIGHTMAP
#endif
#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG)
# define USEEYEVECTOR
#endif

//#ifdef __GLSL_CG_DATA_TYPES
//# define myhalf half
//# define myhalf2 half2
//# define myhalf3 half3
//# define myhalf4 half4
//# define cast_myhalf half
//# define cast_myhalf2 half2
//# define cast_myhalf3 half3
//# define cast_myhalf4 half4
//#else
# define myhalf mediump float
# define myhalf2 mediump vec2
# define myhalf3 mediump vec3
# define myhalf4 mediump vec4
# define cast_myhalf float
# define cast_myhalf2 vec2
# define cast_myhalf3 vec3
# define cast_myhalf4 vec4
//#endif

#ifdef VERTEX_SHADER
uniform highp mat4 ModelViewProjectionMatrix;
#endif

#ifdef VERTEX_SHADER
#ifdef USETRIPPY
// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE
// tweaked scale
uniform highp float ClientTime;
vec4 TrippyVertex(vec4 position)
{
	float worldTime = ClientTime;
	// tweaked for Quake
	worldTime *= 10.0;
	position *= 0.125;
	//~tweaked for Quake
	float distanceSquared = (position.x * position.x + position.z * position.z);
	position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0);
	float y = position.y;
	float x = position.x;
	float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0);
	position.y = x*sin(om)+y*cos(om);
	position.x = x*cos(om)-y*sin(om);
	return position;
}
#endif
#endif

#ifdef MODE_DEPTH_OR_SHADOW
dp_varying highp float Depth;
#ifdef VERTEX_SHADER
void main(void)
{
#ifdef USESKELETAL
	ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
	ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
	ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
	vec4 sw = Attrib_SkeletalWeight;
	vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
	vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
	vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
	mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
	vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
#define Attrib_Position SkeletalVertex
#endif
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
#ifdef USETRIPPY
	gl_Position = TrippyVertex(gl_Position);
#endif
	Depth = gl_Position.z;
}
#endif

#ifdef FRAGMENT_SHADER
void main(void)
{
#ifdef USEDEPTHRGB
	dp_FragColor = encodedepthmacro(Depth);
#else
	dp_FragColor = vec4(1.0,1.0,1.0,1.0);
#endif
}
#endif
#else // !MODE_DEPTH_ORSHADOW




#ifdef MODE_POSTPROCESS
dp_varying mediump vec2 TexCoord1;
dp_varying mediump vec2 TexCoord2;

#ifdef VERTEX_SHADER
void main(void)
{
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
	TexCoord1 = Attrib_TexCoord0.xy;
#ifdef USEBLOOM
	TexCoord2 = Attrib_TexCoord4.xy;
#endif
}
#endif

#ifdef FRAGMENT_SHADER
uniform sampler2D Texture_First;
#ifdef USEBLOOM
uniform sampler2D Texture_Second;
uniform mediump vec4 BloomColorSubtract;
#endif
#ifdef USEGAMMARAMPS
uniform sampler2D Texture_GammaRamps;
#endif
#ifdef USESATURATION
uniform mediump float Saturation;
#endif
#ifdef USEVIEWTINT
uniform mediump vec4 ViewTintColor;
#endif
//uncomment these if you want to use them:
uniform mediump vec4 UserVec1;
uniform mediump vec4 UserVec2;
// uniform mediump vec4 UserVec3;
// uniform mediump vec4 UserVec4;
// uniform highp float ClientTime;
uniform mediump vec2 PixelSize;

#ifdef USEFXAA
// graphitemaster: based off the white paper by Timothy Lottes
// http://developer.download.nvidia.com/assets/gamedev/files/sdk/11/FXAA_WhitePaper.pdf
vec4 fxaa(vec4 inColor, float maxspan)
{
	vec4 ret = inColor; // preserve old
	float mulreduct = 1.0/maxspan;
	float minreduct = (1.0 / 128.0);

	// directions
	vec3 NW = dp_texture2D(Texture_First, TexCoord1 + (vec2(-1.0, -1.0) * PixelSize)).xyz;
	vec3 NE = dp_texture2D(Texture_First, TexCoord1 + (vec2(+1.0, -1.0) * PixelSize)).xyz;
	vec3 SW = dp_texture2D(Texture_First, TexCoord1 + (vec2(-1.0, +1.0) * PixelSize)).xyz;
	vec3 SE = dp_texture2D(Texture_First, TexCoord1 + (vec2(+1.0, +1.0) * PixelSize)).xyz;
	vec3 M = dp_texture2D(Texture_First, TexCoord1).xyz;

	// luminance directions
	vec3 luma = vec3(0.299, 0.587, 0.114);
	float lNW = dot(NW, luma);
	float lNE = dot(NE, luma);
	float lSW = dot(SW, luma);
	float lSE = dot(SE, luma);
	float lM = dot(M, luma);
	float lMin = min(lM, min(min(lNW, lNE), min(lSW, lSE)));
	float lMax = max(lM, max(max(lNW, lNE), max(lSW, lSE)));

	// direction and reciprocal
	vec2 dir = vec2(-((lNW + lNE) - (lSW + lSE)), ((lNW + lSW) - (lNE + lSE)));
	float rcp = 1.0/(min(abs(dir.x), abs(dir.y)) + max((lNW + lNE + lSW + lSE) * (0.25 * mulreduct), minreduct));

	// span
	dir = min(vec2(maxspan, maxspan), max(vec2(-maxspan, -maxspan), dir * rcp)) * PixelSize;

	vec3 rA = (1.0/2.0) * (
		dp_texture2D(Texture_First, TexCoord1 + dir * (1.0/3.0 - 0.5)).xyz +
		dp_texture2D(Texture_First, TexCoord1 + dir * (2.0/3.0 - 0.5)).xyz);
	vec3 rB = rA * (1.0/2.0) + (1.0/4.0) * (
		dp_texture2D(Texture_First, TexCoord1 + dir * (0.0/3.0 - 0.5)).xyz +
		dp_texture2D(Texture_First, TexCoord1 + dir * (3.0/3.0 - 0.5)).xyz);
	float lB = dot(rB, luma);

	ret.xyz = ((lB < lMin) || (lB > lMax)) ? rA : rB;
	ret.a = 1.0;
	return ret;
}
#endif

void main(void)
{
	dp_FragColor = dp_texture2D(Texture_First, TexCoord1);

#ifdef USEFXAA
	dp_FragColor = fxaa(dp_FragColor, 8.0); // 8.0 can be changed for larger span
#endif

#ifdef USEPOSTPROCESSING
// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want
// 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
#if defined(USERVEC1) || defined(USERVEC2)
	float sobel = 1.0;
	// vec2 ts = textureSize(Texture_First, 0);
	// vec2 px = vec2(1/ts.x, 1/ts.y);
	vec2 px = PixelSize;
	vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb;
	vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,  0.0)).rgb;
	vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb;
	vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb;
	vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,  0.0)).rgb;
	vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb;
	vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb;
	vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2(  0.0,-px.y)).rgb;
	vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb;
	vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb;
	vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2(  0.0, px.y)).rgb;
	vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb;
	float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1);
	float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2);
	float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3);
	float px4 =  1.0 * dot(vec3(0.3, 0.59, 0.11), x4);
	float px5 =  2.0 * dot(vec3(0.3, 0.59, 0.11), x5);
	float px6 =  1.0 * dot(vec3(0.3, 0.59, 0.11), x6);
	float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1);
	float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2);
	float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3);
	float py4 =  1.0 * dot(vec3(0.3, 0.59, 0.11), y4);
	float py5 =  2.0 * dot(vec3(0.3, 0.59, 0.11), y5);
	float py6 =  1.0 * dot(vec3(0.3, 0.59, 0.11), y6);
	sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6);
	dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y;
	dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y;
	dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y;
	dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107,  0.707107)) * UserVec1.y;
	dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990,  0.891007)) * UserVec1.y;
	dp_FragColor /= (1.0 + 5.0 * UserVec1.y);
	dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y;
#endif
#endif

#ifdef USEBLOOM
	dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract);
#endif

#ifdef USEVIEWTINT
	dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a);
#endif

#ifdef USESATURATION
	//apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter
	float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114));
	// 'vampire sight' effect, wheres red is compensated
	#ifdef SATURATION_REDCOMPENSATE
		float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation));
		dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation);
		dp_FragColor.r += rboost;
	#else
		// normal desaturation
		//dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation;
		dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation);
	#endif
#endif

#ifdef USEGAMMARAMPS
	dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r;
	dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g;
	dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b;
#endif
}
#endif
#else // !MODE_POSTPROCESS




#ifdef MODE_GENERIC
#ifdef USEDIFFUSE
dp_varying mediump vec2 TexCoord1;
#endif
#ifdef USESPECULAR
dp_varying mediump vec2 TexCoord2;
#endif
uniform myhalf Alpha;
#ifdef VERTEX_SHADER
void main(void)
{
#ifdef USESKELETAL
	ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
	ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
	ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
	vec4 sw = Attrib_SkeletalWeight;
	vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
	vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
	vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
	mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
	vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
#define Attrib_Position SkeletalVertex
#endif
	VertexColor = Attrib_Color;
#ifdef USEDIFFUSE
	TexCoord1 = Attrib_TexCoord0.xy;
#endif
#ifdef USESPECULAR
	TexCoord2 = Attrib_TexCoord1.xy;
#endif
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
#ifdef USETRIPPY
	gl_Position = TrippyVertex(gl_Position);
#endif
}
#endif

#ifdef FRAGMENT_SHADER
#ifdef USEDIFFUSE
uniform sampler2D Texture_First;
#endif
#ifdef USESPECULAR
uniform sampler2D Texture_Second;
#endif
#ifdef USEGAMMARAMPS
uniform sampler2D Texture_GammaRamps;
#endif

void main(void)
{
#ifdef USEVIEWTINT
	dp_FragColor = VertexColor;
#else
	dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
#endif
#ifdef USEDIFFUSE
# ifdef USEREFLECTCUBE
	// suppress texture alpha
	dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb;
# else
	dp_FragColor *= dp_texture2D(Texture_First, TexCoord1);
# endif
#endif

#ifdef USESPECULAR
	vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2);
# ifdef USECOLORMAPPING
	dp_FragColor *= tex2;
# endif
# ifdef USEGLOW
	dp_FragColor += tex2;
# endif
# ifdef USEVERTEXTEXTUREBLEND
	dp_FragColor = mix(dp_FragColor, tex2, tex2.a);
# endif
#endif
#ifdef USEGAMMARAMPS
	dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r;
	dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g;
	dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b;
#endif
#ifdef USEALPHAKILL
	dp_FragColor.a *= Alpha;
#endif
}
#endif
#else // !MODE_GENERIC




#ifdef MODE_BLOOMBLUR
dp_varying mediump vec2 TexCoord;
#ifdef VERTEX_SHADER
void main(void)
{
	VertexColor = Attrib_Color;
	TexCoord = Attrib_TexCoord0.xy;
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
}
#endif

#ifdef FRAGMENT_SHADER
uniform sampler2D Texture_First;
uniform mediump vec4 BloomBlur_Parameters;

void main(void)
{
	int i;
	vec2 tc = TexCoord;
	vec3 color = dp_texture2D(Texture_First, tc).rgb;
	tc += BloomBlur_Parameters.xy;
	for (i = 1;i < SAMPLES;i++)
	{
		color += dp_texture2D(Texture_First, tc).rgb;
		tc += BloomBlur_Parameters.xy;
	}
	dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1);
}
#endif
#else // !MODE_BLOOMBLUR
#ifdef MODE_REFRACTION
dp_varying mediump vec2 TexCoord;
dp_varying highp vec4 ModelViewProjectionPosition;
uniform highp mat4 TexMatrix;
#ifdef VERTEX_SHADER

void main(void)
{
#ifdef USESKELETAL
	ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
	ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
	ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
	vec4 sw = Attrib_SkeletalWeight;
	vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
	vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
	vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
	mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
	vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
#define Attrib_Position SkeletalVertex
#endif
#ifdef USEALPHAGENVERTEX
	VertexColor = Attrib_Color;
#endif
	TexCoord = vec2(TexMatrix * Attrib_TexCoord0);
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
	ModelViewProjectionPosition = gl_Position;
#ifdef USETRIPPY
	gl_Position = TrippyVertex(gl_Position);
#endif
}
#endif

#ifdef FRAGMENT_SHADER
uniform sampler2D Texture_Normal;
uniform sampler2D Texture_Refraction;

uniform mediump vec4 DistortScaleRefractReflect;
uniform mediump vec4 ScreenScaleRefractReflect;
uniform mediump vec4 ScreenCenterRefractReflect;
uniform mediump vec4 RefractColor;
uniform mediump vec4 ReflectColor;
uniform highp float ClientTime;
#ifdef USENORMALMAPSCROLLBLEND
uniform highp vec2 NormalmapScrollBlend;
#endif

void main(void)
{
	vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);
	//vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;
	vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;
#ifdef USEALPHAGENVERTEX
	vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a;
	vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a);
#else
	vec2 distort = DistortScaleRefractReflect.xy;
	vec4 refractcolor = RefractColor;
#endif
	#ifdef USENORMALMAPSCROLLBLEND
		vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);
		normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;
		vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort;
	#else
		vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort;
	#endif
	// FIXME temporary hack to detect the case that the reflection
	// gets blackened at edges due to leaving the area that contains actual
	// content.
	// Remove this 'ack once we have a better way to stop this thing from
	// 'appening.
	float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);
	f      *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);
	f      *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);
	f      *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);
	ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);
	dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor;
}
#endif
#else // !MODE_REFRACTION




#ifdef MODE_WATER
dp_varying mediump vec2 TexCoord;
dp_varying highp vec3 EyeVector;
dp_varying highp vec4 ModelViewProjectionPosition;
#ifdef VERTEX_SHADER
uniform highp vec3 EyePosition;
uniform highp mat4 TexMatrix;

void main(void)
{
#ifdef USESKELETAL
	ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
	ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
	ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
	vec4 sw = Attrib_SkeletalWeight;
	vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
	vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
	vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
	mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
	mat3 SkeletalNormalMatrix = mat3(cross(SkeletalMatrix[1].xyz, SkeletalMatrix[2].xyz), cross(SkeletalMatrix[2].xyz, SkeletalMatrix[0].xyz), cross(SkeletalMatrix[0].xyz, SkeletalMatrix[1].xyz)); // is actually transpose(inverse(mat3(SkeletalMatrix))) * det(mat3(SkeletalMatrix))
	vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
	vec3 SkeletalSVector = normalize(Attrib_TexCoord1.xyz * SkeletalNormalMatrix);
	vec3 SkeletalTVector = normalize(Attrib_TexCoord2.xyz * SkeletalNormalMatrix);
	vec3 SkeletalNormal  = normalize(Attrib_TexCoord3.xyz * SkeletalNormalMatrix);
#define Attrib_Position SkeletalVertex
#define Attrib_TexCoord1 SkeletalSVector
#define Attrib_TexCoord2 SkeletalTVector
#define Attrib_TexCoord3 SkeletalNormal
#endif
#ifdef USEALPHAGENVERTEX
	VertexColor = Attrib_Color;
#endif
	TexCoord = vec2(TexMatrix * Attrib_TexCoord0);
	vec3 EyeRelative = EyePosition - Attrib_Position.xyz;
	EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz);
	EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz);
	EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz);
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
	ModelViewProjectionPosition = gl_Position;
#ifdef USETRIPPY
	gl_Position = TrippyVertex(gl_Position);
#endif
}
#endif

#ifdef FRAGMENT_SHADER
uniform sampler2D Texture_Normal;
uniform sampler2D Texture_Refraction;
uniform sampler2D Texture_Reflection;

uniform mediump vec4 DistortScaleRefractReflect;
uniform mediump vec4 ScreenScaleRefractReflect;
uniform mediump vec4 ScreenCenterRefractReflect;
uniform mediump vec4 RefractColor;
uniform mediump vec4 ReflectColor;
uniform mediump float ReflectFactor;
uniform mediump float ReflectOffset;
uniform highp float ClientTime;
#ifdef USENORMALMAPSCROLLBLEND
uniform highp vec2 NormalmapScrollBlend;
#endif

void main(void)
{
	vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);
	//vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;
	vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;
	//SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0);
	// slight water animation via 2 layer scrolling (todo: tweak)
#ifdef USEALPHAGENVERTEX
	vec4 distort = DistortScaleRefractReflect * VertexColor.a;
	float reflectoffset = ReflectOffset * VertexColor.a;
	float reflectfactor = ReflectFactor * VertexColor.a;
	vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a);
#else
	vec4 distort = DistortScaleRefractReflect;
	float reflectoffset = ReflectOffset;
	float reflectfactor = ReflectFactor;
	vec4 refractcolor = RefractColor;
#endif
	#ifdef USENORMALMAPSCROLLBLEND
		vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);
		normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;
		vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort;
	#else
		vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort;
	#endif
	// FIXME temporary hack to detect the case that the reflection
	// gets blackened at edges due to leaving the area that contains actual
	// content.
	// Remove this 'ack once we have a better way to stop this thing from
	// 'appening.
	float f  = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002);
	f       *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002);
	f       *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002);
	f       *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002);
	ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);
	f  = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002);
	f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002);
	f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002);
	f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002);
	ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);
	float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset;
	dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);
}
#endif
#else // !MODE_WATER




// common definitions between vertex shader and fragment shader:

dp_varying mediump vec4 TexCoordSurfaceLightmap;
#ifdef USEVERTEXTEXTUREBLEND
dp_varying mediump vec2 TexCoord2;
#endif

#ifdef MODE_LIGHTSOURCE
dp_varying mediump vec3 CubeVector;
#endif

#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE)
dp_varying mediump vec3 LightVector;
#endif

#ifdef USEEYEVECTOR
dp_varying highp vec4 EyeVectorFogDepth;
#endif

#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL)
dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent)
dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal)
dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal)
#else
# ifdef USEFOG
dp_varying highp vec3 EyeVectorModelSpace;
# endif
#endif

#ifdef USEREFLECTION
dp_varying highp vec4 ModelViewProjectionPosition;
#endif
#ifdef MODE_DEFERREDLIGHTSOURCE
uniform highp vec3 LightPosition;
dp_varying highp vec4 ModelViewPosition;
#endif

#ifdef MODE_LIGHTSOURCE
uniform highp vec3 LightPosition;
#endif
uniform highp vec3 EyePosition;
#ifdef MODE_LIGHTDIRECTION
uniform highp vec3 LightDir;
#endif
uniform highp vec4 FogPlane;

#ifdef USESHADOWMAPORTHO
dp_varying highp vec3 ShadowMapTC;
#endif

#ifdef USEBOUNCEGRID
dp_varying highp vec3 BounceGridTexCoord;
#endif

#ifdef MODE_DEFERREDGEOMETRY
dp_varying highp float Depth;
#endif






// 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

// fragment shader specific:
#ifdef FRAGMENT_SHADER

uniform sampler2D Texture_Normal;
uniform sampler2D Texture_Color;
uniform sampler2D Texture_Gloss;
#ifdef USEGLOW
uniform sampler2D Texture_Glow;
#endif
#ifdef USEVERTEXTEXTUREBLEND
uniform sampler2D Texture_SecondaryNormal;
uniform sampler2D Texture_SecondaryColor;
uniform sampler2D Texture_SecondaryGloss;
#ifdef USEGLOW
uniform sampler2D Texture_SecondaryGlow;
#endif
#endif
#ifdef USECOLORMAPPING
uniform sampler2D Texture_Pants;
uniform sampler2D Texture_Shirt;
#endif
#ifdef USEFOG
#ifdef USEFOGHEIGHTTEXTURE
uniform sampler2D Texture_FogHeightTexture;
#endif
uniform sampler2D Texture_FogMask;
#endif
#ifdef USELIGHTMAP
uniform sampler2D Texture_Lightmap;
#endif
#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)
uniform sampler2D Texture_Deluxemap;
#endif
#ifdef USEREFLECTION
uniform sampler2D Texture_Reflection;
#endif

#ifdef MODE_DEFERREDLIGHTSOURCE
uniform sampler2D Texture_ScreenNormalMap;
#endif
#ifdef USEDEFERREDLIGHTMAP
#ifdef USECELOUTLINES
uniform sampler2D Texture_ScreenNormalMap;
#endif
uniform sampler2D Texture_ScreenDiffuse;
uniform sampler2D Texture_ScreenSpecular;
#endif

uniform mediump vec3 Color_Pants;
uniform mediump vec3 Color_Shirt;
uniform mediump vec3 FogColor;

#ifdef USEFOG
uniform highp float FogRangeRecip;
uniform highp float FogPlaneViewDist;
uniform highp float FogHeightFade;
vec3 FogVertex(vec4 surfacecolor)
{
#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL)
	vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w);
#endif
	float FogPlaneVertexDist = EyeVectorFogDepth.w;
	float fogfrac;
       vec3 fc = FogColor;
#ifdef USEFOGALPHAHACK
	fc *= surfacecolor.a;
#endif
#ifdef USEFOGHEIGHTTEXTURE
	vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade));
	fogfrac = fogheightpixel.a;
	return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);
#else
# ifdef USEFOGOUTSIDE
	fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);
# else
	fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
# endif
	return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);
#endif
}
#endif

#ifdef USEOFFSETMAPPING
uniform mediump vec4 OffsetMapping_ScaleSteps;
uniform mediump float OffsetMapping_Bias;
#ifdef USEOFFSETMAPPING_LOD
uniform mediump float OffsetMapping_LodDistance;
#endif
vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy)
{
	float i;
	// distance-based LOD
#ifdef USEOFFSETMAPPING_LOD
	//mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z);
	//mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor);
	mediump float GuessLODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z);
#ifdef USEOFFSETMAPPING_RELIEFMAPPING
	// stupid workaround because 1-step and 2-step reliefmapping is void
	mediump float LODSteps = max(3.0, ceil(GuessLODFactor * OffsetMapping_ScaleSteps.y));
#else
	mediump float LODSteps = ceil(GuessLODFactor * OffsetMapping_ScaleSteps.y);
#endif
	mediump float LODFactor = LODSteps / OffsetMapping_ScaleSteps.y;
	mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, LODSteps, 1.0 / LODSteps, OffsetMapping_ScaleSteps.w * LODFactor);
#else
	#define ScaleSteps OffsetMapping_ScaleSteps
#endif
#ifdef USEOFFSETMAPPING_RELIEFMAPPING
	float f;
	// 14 sample relief mapping: linear search and then binary search
	// this basically steps forward a small amount repeatedly until it finds
	// itself inside solid, then jitters forward and back using decreasing
	// amounts to find the impact
	//vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1);
	//vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1);
	vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1);
	vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1);
	OffsetVector *= ScaleSteps.z;
	for(i = 1.0; i < ScaleSteps.y; ++i)
		RT += OffsetVector *  step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z);
	for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5)
		RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f);
	return RT.xy;
#else
	// 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits)
	//vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1));
	//vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1));
	vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1));
	OffsetVector *= ScaleSteps.z;
	for(i = 0.0; i < ScaleSteps.y; ++i)
		TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a);
	return TexCoord;
#endif
}
#endif // USEOFFSETMAPPING

#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)
uniform sampler2D Texture_Attenuation;
uniform samplerCube Texture_Cube;
#endif

#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO)

#ifdef USESHADOWMAP2D
# ifdef USESHADOWSAMPLER
uniform sampler2DShadow Texture_ShadowMap2D;
# else
uniform sampler2D Texture_ShadowMap2D;
# endif
#endif

#ifdef USESHADOWMAPVSDCT
uniform samplerCube Texture_CubeProjection;
#endif

#if defined(USESHADOWMAP2D)
uniform mediump vec4 ShadowMap_TextureScale;
uniform mediump vec4 ShadowMap_Parameters;
#endif

#if defined(USESHADOWMAP2D)
# ifdef USESHADOWMAPORTHO
#  define GetShadowMapTC2D(dir) (max(vec3(0.0, 0.0, 0.0), min(dir, ShadowMap_Parameters.xyz)))
# else
#  ifdef USESHADOWMAPVSDCT
vec3 GetShadowMapTC2D(vec3 dir)
{
	vec3 adir = abs(dir);
	float m = max(max(adir.x, adir.y), adir.z);
	vec4 proj = dp_textureCube(Texture_CubeProjection, dir);
#ifdef USEDEPTHRGB
	return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) +  proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w);
#else
	vec2 mparams = ShadowMap_Parameters.xy / m;
	return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w);
#endif
}
#  else
vec3 GetShadowMapTC2D(vec3 dir)
{
	vec3 adir = abs(dir);
	float m; vec4 proj;
	if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); }
	if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); }
#ifdef USEDEPTHRGB
	return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w);
#else
	vec2 mparams = ShadowMap_Parameters.xy / m;
	return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w);
#endif
}
#  endif
# endif
#endif // defined(USESHADOWMAP2D)

# ifdef USESHADOWMAP2D
float ShadowMapCompare(vec3 dir)
{
	vec3 shadowmaptc = GetShadowMapTC2D(dir) + vec3(ShadowMap_TextureScale.zw, 0.0f);
	float f;

#  ifdef USEDEPTHRGB
#   ifdef USESHADOWMAPPCF
#    define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale.xy))
#    if USESHADOWMAPPCF > 1
	vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);
	center *= ShadowMap_TextureScale.xy;
	vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));
	vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));
	vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));
	vec4 row4 = step(shadowmaptc.z, vec4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));
	vec4 cols = row2 + row3 + mix(row1, row4, offset.y);
	f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));
#    else
	vec2 center = shadowmaptc.xy*ShadowMap_TextureScale.xy, offset = fract(shadowmaptc.xy);
	vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));
	vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));
	vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));
	vec3 cols = row2 + mix(row1, row3, offset.y);
	f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));
#    endif
#   else
	f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale.xy)));
#   endif
#  else
#   ifdef USESHADOWSAMPLER
#     ifdef USESHADOWMAPPCF
#       define texval(off) dp_shadow2D(Texture_ShadowMap2D, vec3(off, shadowmaptc.z))  
	vec2 offset = fract(shadowmaptc.xy - 0.5);
   vec4 size = vec4(offset + 1.0, 2.0 - offset);
#       if USESHADOWMAPPCF > 1
   vec2 center = (shadowmaptc.xy - offset + 0.5)*ShadowMap_TextureScale.xy;
   vec4 weight = (vec4(-1.5, -1.5, 2.0, 2.0) + (shadowmaptc.xy - 0.5*offset).xyxy)*ShadowMap_TextureScale.xyxy;
	f = (1.0/25.0)*dot(size.zxzx*size.wwyy, vec4(texval(weight.xy), texval(weight.zy), texval(weight.xw), texval(weight.zw))) +
		(2.0/25.0)*dot(size, vec4(texval(vec2(weight.z, center.y)), texval(vec2(center.x, weight.w)), texval(vec2(weight.x, center.y)), texval(vec2(center.x, weight.y)))) +
		(4.0/25.0)*texval(center);
#       else
	vec4 weight = (vec4(1.0, 1.0, -0.5, -0.5) + (shadowmaptc.xy - 0.5*offset).xyxy)*ShadowMap_TextureScale.xyxy;
	f = (1.0/9.0)*dot(size.zxzx*size.wwyy, vec4(texval(weight.zw), texval(weight.xw), texval(weight.zy), texval(weight.xy)));
#       endif        
#     else
	f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale.xy, shadowmaptc.z));
#     endif
#   else
#     ifdef USESHADOWMAPPCF
#      if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)
#       ifdef GL_ARB_texture_gather
#         define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y))
#       else
#         define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale.xy)
#       endif
	vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale.xy;
#       if USESHADOWMAPPCF > 1
   vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0));
   vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0));
   vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0));
   vec4 group4 = step(shadowmaptc.z, texval(-2.0,  0.0));
   vec4 group5 = step(shadowmaptc.z, texval( 0.0,  0.0));
   vec4 group6 = step(shadowmaptc.z, texval( 2.0,  0.0));
   vec4 group7 = step(shadowmaptc.z, texval(-2.0,  2.0));
   vec4 group8 = step(shadowmaptc.z, texval( 0.0,  2.0));
   vec4 group9 = step(shadowmaptc.z, texval( 2.0,  2.0));
	vec4 locols = vec4(group1.ab, group3.ab);
	vec4 hicols = vec4(group7.rg, group9.rg);
	locols.yz += group2.ab;
	hicols.yz += group8.rg;
	vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) +
				vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) +
				mix(locols, hicols, offset.y);
	vec4 cols = group5 + vec4(group2.rg, group8.ab);
	cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x);
	f = dot(cols, vec4(1.0/25.0));
#      else
	vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));
	vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));
	vec4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));
	vec4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));
	vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) +
				mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y);
	f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));
#       endif
#      else
#       ifdef GL_EXT_gpu_shader4
#         define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r
#       else
#         define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale.xy).r  
#       endif
#       if USESHADOWMAPPCF > 1
	vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);
	center *= ShadowMap_TextureScale.xy;
	vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));
	vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));
	vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));
	vec4 row4 = step(shadowmaptc.z, vec4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));
	vec4 cols = row2 + row3 + mix(row1, row4, offset.y);
	f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));
#       else
	vec2 center = shadowmaptc.xy*ShadowMap_TextureScale.xy, offset = fract(shadowmaptc.xy);
	vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));
	vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));
	vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));
	vec3 cols = row2 + mix(row1, row3, offset.y);
	f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));
#       endif
#      endif
#     else
	f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale.xy).r);
#     endif
#   endif
#  endif
#  ifdef USESHADOWMAPORTHO
	return mix(ShadowMap_Parameters.w, 1.0, f);
#  else
	return f;
#  endif
}
# endif
#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO)
#endif // FRAGMENT_SHADER




#ifdef MODE_DEFERREDGEOMETRY
#ifdef VERTEX_SHADER
uniform highp mat4 TexMatrix;
#ifdef USEVERTEXTEXTUREBLEND
uniform highp mat4 BackgroundTexMatrix;
#endif
uniform highp mat4 ModelViewMatrix;
void main(void)
{
#ifdef USESKELETAL
	ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
	ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
	ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
	vec4 sw = Attrib_SkeletalWeight;
	vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
	vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
	vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
	mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
	mat3 SkeletalNormalMatrix = mat3(cross(SkeletalMatrix[1].xyz, SkeletalMatrix[2].xyz), cross(SkeletalMatrix[2].xyz, SkeletalMatrix[0].xyz), cross(SkeletalMatrix[0].xyz, SkeletalMatrix[1].xyz)); // is actually transpose(inverse(mat3(SkeletalMatrix))) * det(mat3(SkeletalMatrix))
	vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
	vec3 SkeletalSVector = normalize(Attrib_TexCoord1.xyz * SkeletalNormalMatrix);
	vec3 SkeletalTVector = normalize(Attrib_TexCoord2.xyz * SkeletalNormalMatrix);
	vec3 SkeletalNormal  = normalize(Attrib_TexCoord3.xyz * SkeletalNormalMatrix);
#define Attrib_Position SkeletalVertex
#define Attrib_TexCoord1 SkeletalSVector
#define Attrib_TexCoord2 SkeletalTVector
#define Attrib_TexCoord3 SkeletalNormal
#endif
	TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0);
#ifdef USEVERTEXTEXTUREBLEND
	VertexColor = Attrib_Color;
	TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0);
#endif

	// transform unnormalized eye direction into tangent space
#ifdef USEOFFSETMAPPING
	vec3 EyeRelative = EyePosition - Attrib_Position.xyz;
	EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz);
	EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz);
	EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz);
	EyeVectorFogDepth.w = 0.0;
#endif

	VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0));
	VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0));
	VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0));
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
#ifdef USETRIPPY
	gl_Position = TrippyVertex(gl_Position);
#endif
	Depth = (ModelViewMatrix * Attrib_Position).z;
}
#endif // VERTEX_SHADER

#ifdef FRAGMENT_SHADER
void main(void)
{
#ifdef USEOFFSETMAPPING
	// apply offsetmapping
	vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy);
	vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy);
	vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy);
# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy)
#else
# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy)
#endif

#ifdef USEALPHAKILL
	if (offsetMappedTexture2D(Texture_Color).a < 0.5)
		discard;
#endif

#ifdef USEVERTEXTEXTUREBLEND
	float alpha = offsetMappedTexture2D(Texture_Color).a;
	float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0));
	//float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0));
	//float terrainblend = float(VertexColor.a) * alpha > 0.5;
#endif

#ifdef USEVERTEXTEXTUREBLEND
	vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5);
	float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend);
#else
	vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5);
	float a = offsetMappedTexture2D(Texture_Gloss).a;
#endif

	vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz);
	dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a);
}
#endif // FRAGMENT_SHADER
#else // !MODE_DEFERREDGEOMETRY




#ifdef MODE_DEFERREDLIGHTSOURCE
#ifdef VERTEX_SHADER
uniform highp mat4 ModelViewMatrix;
void main(void)
{
	ModelViewPosition = ModelViewMatrix * Attrib_Position;
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
}
#endif // VERTEX_SHADER

#ifdef FRAGMENT_SHADER
uniform highp mat4 ViewToLight;
// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far));
uniform highp vec2 ScreenToDepth;
uniform myhalf3 DeferredColor_Ambient;
uniform myhalf3 DeferredColor_Diffuse;
#ifdef USESPECULAR
uniform myhalf3 DeferredColor_Specular;
uniform myhalf SpecularPower;
#endif
uniform myhalf2 PixelToScreenTexCoord;
void main(void)
{
	// calculate viewspace pixel position
	vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;
	vec3 position;
	// get the geometry information (depth, normal, specular exponent)
	myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord);
	// decode viewspace pixel normal
//	myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5));
	myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg)));
	// decode viewspace pixel position
//	position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord));
	position.z = normalmap.b;
//	position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x);
	position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);

	// now do the actual shading
	// surfacenormal = pixel normal in viewspace
	// LightVector = pixel to light in viewspace
	// CubeVector = pixel in lightspace
	// eyenormal = pixel to view direction in viewspace
	vec3 CubeVector = vec3(ViewToLight * vec4(position,1));
	myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));
#ifdef USEDIFFUSE
	// calculate diffuse shading
	myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position));
SHADEDIFFUSE
#endif
#ifdef USESPECULAR
	// calculate directional shading
	myhalf3 eyenormal = -normalize(cast_myhalf3(position));
SHADESPECULAR(SpecularPower * normalmap.a)
#endif

#if defined(USESHADOWMAP2D)
	fade *= ShadowMapCompare(CubeVector);
#endif

#ifdef USESPECULAR
	gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);
	gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0);
# ifdef USECUBEFILTER
	vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb;
	gl_FragData[0].rgb *= cubecolor;
	gl_FragData[1].rgb *= cubecolor;
# endif
#else
# ifdef USEDIFFUSE
	gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);
# else
	gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0);
# endif
# ifdef USECUBEFILTER
	vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb;
	gl_FragColor.rgb *= cubecolor;
# endif
#endif
}
#endif // FRAGMENT_SHADER
#else // !MODE_DEFERREDLIGHTSOURCE




#ifdef VERTEX_SHADER
uniform highp mat4 TexMatrix;
#ifdef USEVERTEXTEXTUREBLEND
uniform highp mat4 BackgroundTexMatrix;
#endif
#ifdef MODE_LIGHTSOURCE
uniform highp mat4 ModelToLight;
#endif
#ifdef USESHADOWMAPORTHO
uniform highp mat4 ShadowMapMatrix;
#endif
#ifdef USEBOUNCEGRID
uniform highp mat4 BounceGridMatrix;
#endif
void main(void)
{
#ifdef USESKELETAL
	ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
	ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
	ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
	vec4 sw = Attrib_SkeletalWeight;
	vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
	vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
	vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
	mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
//	ivec4 si = ivec4(Attrib_SkeletalIndex);
//	mat4 SkeletalMatrix = Skeletal_Transform[si.x] * Attrib_SkeletalWeight.x + Skeletal_Transform[si.y] * Attrib_SkeletalWeight.y + Skeletal_Transform[si.z] * Attrib_SkeletalWeight.z + Skeletal_Transform[si.w] * Attrib_SkeletalWeight.w;
	mat3 SkeletalNormalMatrix = mat3(cross(SkeletalMatrix[1].xyz, SkeletalMatrix[2].xyz), cross(SkeletalMatrix[2].xyz, SkeletalMatrix[0].xyz), cross(SkeletalMatrix[0].xyz, SkeletalMatrix[1].xyz)); // is actually transpose(inverse(mat3(SkeletalMatrix))) * det(mat3(SkeletalMatrix))
	vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
	SkeletalVertex.w = 1.0;
	vec3 SkeletalSVector = normalize(Attrib_TexCoord1.xyz * SkeletalNormalMatrix);
	vec3 SkeletalTVector = normalize(Attrib_TexCoord2.xyz * SkeletalNormalMatrix);
	vec3 SkeletalNormal  = normalize(Attrib_TexCoord3.xyz * SkeletalNormalMatrix);
#define Attrib_Position SkeletalVertex
#define Attrib_TexCoord1 SkeletalSVector
#define Attrib_TexCoord2 SkeletalTVector
#define Attrib_TexCoord3 SkeletalNormal
#endif

#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX)
	VertexColor = Attrib_Color;
#endif
	// copy the surface texcoord
#ifdef USELIGHTMAP
	TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy);
#else
	TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0);
#endif
#ifdef USEVERTEXTEXTUREBLEND
	TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0);
#endif

#ifdef USEBOUNCEGRID
	BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position);
#ifdef USEBOUNCEGRIDDIRECTIONAL
	BounceGridTexCoord.z *= 0.125;
#endif
#endif

#ifdef MODE_LIGHTSOURCE
	// transform vertex position into light attenuation/cubemap space
	// (-1 to +1 across the light box)
	CubeVector = vec3(ModelToLight * Attrib_Position);

# ifdef USEDIFFUSE
	// transform unnormalized light direction into tangent space
	// (we use unnormalized to ensure that it interpolates correctly and then
	//  normalize it per pixel)
	vec3 lightminusvertex = LightPosition - Attrib_Position.xyz;
	LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz);
	LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz);
	LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz);
# endif
#endif

#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE)
	LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz);
	LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz);
	LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz);
#endif

	// transform unnormalized eye direction into tangent space
#ifdef USEEYEVECTOR
	vec3 EyeRelative = EyePosition - Attrib_Position.xyz;
	EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz);
	EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz);
	EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz);
#ifdef USEFOG
	EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position);
#else
	EyeVectorFogDepth.w = 0.0;
#endif
#endif


#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL)
# ifdef USEFOG
	VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x);
	VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y);
	VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z);
# else
	VectorS = vec4(Attrib_TexCoord1, 0);
	VectorT = vec4(Attrib_TexCoord2, 0);
	VectorR = vec4(Attrib_TexCoord3, 0);
# endif
#else
# ifdef USEFOG
	EyeVectorModelSpace = EyePosition - Attrib_Position.xyz;
# endif
#endif

	// transform vertex to clipspace (post-projection, but before perspective divide by W occurs)
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;

#ifdef USESHADOWMAPORTHO
	ShadowMapTC = vec3(ShadowMapMatrix * gl_Position);
#endif

#ifdef USEREFLECTION
	ModelViewProjectionPosition = gl_Position;
#endif
#ifdef USETRIPPY
	gl_Position = TrippyVertex(gl_Position);
#endif
}
#endif // VERTEX_SHADER




#ifdef FRAGMENT_SHADER
#ifdef USEDEFERREDLIGHTMAP
uniform myhalf2 PixelToScreenTexCoord;
uniform myhalf3 DeferredMod_Diffuse;
uniform myhalf3 DeferredMod_Specular;
#endif
uniform myhalf3 Color_Ambient;
uniform myhalf3 Color_Diffuse;
uniform myhalf3 Color_Specular;
uniform myhalf SpecularPower;
#ifdef USEGLOW
uniform myhalf3 Color_Glow;
#endif
uniform myhalf Alpha;
#ifdef USEREFLECTION
uniform mediump vec4 DistortScaleRefractReflect;
uniform mediump vec4 ScreenScaleRefractReflect;
uniform mediump vec4 ScreenCenterRefractReflect;
uniform mediump vec4 ReflectColor;
#endif
#ifdef USEREFLECTCUBE
uniform highp mat4 ModelToReflectCube;
uniform sampler2D Texture_ReflectMask;
uniform samplerCube Texture_ReflectCube;
#endif
#ifdef USEBOUNCEGRID
uniform sampler3D Texture_BounceGrid;
uniform float BounceGridIntensity;
uniform highp mat4 BounceGridMatrix;
#endif
uniform highp float ClientTime;
#ifdef USENORMALMAPSCROLLBLEND
uniform highp vec2 NormalmapScrollBlend;
#endif
#ifdef USEOCCLUDE
uniform occludeQuery {
    uint visiblepixels;
    uint allpixels;
};
#endif
void main(void)
{
#ifdef USEOFFSETMAPPING
	// apply offsetmapping
	vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy);
	vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy);
	vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy);
# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy)
# define TexCoord TexCoordOffset
#else
# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy)
# define TexCoord TexCoordSurfaceLightmap.xy
#endif

	// combine the diffuse textures (base, pants, shirt)
	myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color));
#ifdef USEALPHAKILL
	if (color.a < 0.5)
		discard;
#endif
	color.a *= Alpha;
#ifdef USECOLORMAPPING
	color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt;
#endif
#ifdef USEVERTEXTEXTUREBLEND
#ifdef USEBOTHALPHAS
	myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2));
	myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0));
	color.rgb = mix(color2.rgb, color.rgb, terrainblend);
#else
	myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0));
	//myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0));
	//myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5;
	color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend);
#endif
	color.a = 1.0;
	//color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend);
#endif
#ifdef USEALPHAGENVERTEX
	color.a *= VertexColor.a;
#endif

	// get the surface normal
#ifdef USEVERTEXTEXTUREBLEND
	myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5));
#else
	myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5));
#endif

	// get the material colors
	myhalf3 diffusetex = color.rgb;
#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)
# ifdef USEVERTEXTEXTUREBLEND
	myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend);
# else
	myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss));
# endif
#endif

#ifdef USEREFLECTCUBE
	vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal);
	vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz;
	vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0));
	diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord));
#endif

#ifdef USESPECULAR
	myhalf3 eyenormal = normalize(cast_myhalf3(EyeVectorFogDepth.xyz));
#endif




#ifdef MODE_LIGHTSOURCE
	// light source
#ifdef USEDIFFUSE
	myhalf3 lightnormal = cast_myhalf3(normalize(LightVector));
SHADEDIFFUSE
	color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);
#ifdef USESPECULAR
SHADESPECULAR(SpecularPower * glosstex.a)
	color.rgb += glosstex.rgb * (specular * Color_Specular);
#endif
#else
	color.rgb = diffusetex * Color_Ambient;
#endif
	color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));
#if defined(USESHADOWMAP2D)
	color.rgb *= ShadowMapCompare(CubeVector);
#endif
# ifdef USECUBEFILTER
	color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector));
# endif
#endif // MODE_LIGHTSOURCE




#ifdef MODE_LIGHTDIRECTION
	#define SHADING
	#ifdef USEDIFFUSE
		myhalf3 lightnormal = cast_myhalf3(normalize(LightVector));
	#endif
	#define lightcolor 1
#endif // MODE_LIGHTDIRECTION
#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE
   #define SHADING
	// deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe)
	myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0);
	myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw));
	// convert modelspace light vector to tangentspace
	myhalf3 lightnormal;
	lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS));
	lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT));
	lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR));
	lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this
	// calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)
	// note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar
	// is used (the lightmap and deluxemap coords correspond to virtually random coordinates
	// on that luxel, and NOT to its center, because recursive triangle subdivision is used
	// to map the luxels to coordinates on the draw surfaces), which also causes
	// deluxemaps to be wrong because light contributions from the wrong side of the surface
	// are added up. To prevent divisions by zero or strong exaggerations, a max()
	// nudge is done here at expense of some additional fps. This is ONLY needed for
	// deluxemaps, tangentspace deluxemap avoid this problem by design.
	lightcolor *= 1.0 / max(0.25, lightnormal.z);
#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE
#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE
   #define SHADING
	// deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)
	myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0);
	myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw));
#endif
#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR)
	#define SHADING
	// forced deluxemap on lightmapped/vertexlit surfaces
	myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0);
   #ifdef USELIGHTMAP
		myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw));
   #else
		myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb);
   #endif
#endif
#ifdef MODE_FAKELIGHT
	#define SHADING
	myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz));
	#define lightcolor 1
#endif // MODE_FAKELIGHT




#ifdef MODE_LIGHTMAP
	color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse);
#endif // MODE_LIGHTMAP
#ifdef MODE_VERTEXCOLOR
	color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse);
#endif // MODE_VERTEXCOLOR
#ifdef MODE_FLATCOLOR
	color.rgb = diffusetex * Color_Ambient;
#endif // MODE_FLATCOLOR




#ifdef SHADING
# ifdef USEDIFFUSE
SHADEDIFFUSE
#  ifdef USESPECULAR
SHADESPECULAR(SpecularPower * glosstex.a)
	color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor;
#  else
	color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor);
#  endif
# else
	color.rgb = diffusetex * Color_Ambient;
# endif
#endif

#ifdef USESHADOWMAPORTHO
	color.rgb *= ShadowMapCompare(ShadowMapTC);
#endif

#ifdef USEDEFERREDLIGHTMAP
	vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;
	color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse;
	color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular;
//	color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001);
#endif

#ifdef USEBOUNCEGRID
#ifdef USEBOUNCEGRIDDIRECTIONAL
//	myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord                        ));
//	myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0);
	myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250)));
	myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375)));
	myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500)));
	myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625)));
	myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750)));
	myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875)));
	myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz));
	myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir);
	myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir);
//	bouncegrid_dirp  = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0);
	myhalf3 bouncegrid_light = cast_myhalf3(
		dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn),
		dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn),
		dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn));
	color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity;
//	color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5);
#else
	color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity;
#endif
#endif

#ifdef USEGLOW
#ifdef USEVERTEXTEXTUREBLEND
	color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow;
#else
	color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow;
#endif
#endif

#ifdef USECELOUTLINES
# ifdef USEDEFERREDLIGHTMAP
//	vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;
	vec4 ScreenTexCoordStep = vec4(PixelToScreenTexCoord.x, 0.0, 0.0, PixelToScreenTexCoord.y);
	vec4 DepthNeighbors;

	// enable to test ink on white geometry
//	color.rgb = vec3(1.0, 1.0, 1.0);

	// note: this seems to be negative
	float DepthCenter = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).b;

	// edge detect method
//	DepthNeighbors.x = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord - ScreenTexCoordStep.xy).b;
//	DepthNeighbors.y = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + ScreenTexCoordStep.xy).b;
//	DepthNeighbors.z = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + ScreenTexCoordStep.zw).b;
//	DepthNeighbors.w = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord - ScreenTexCoordStep.zw).b;
//	float DepthAverage = dot(DepthNeighbors, vec4(0.25, 0.25, 0.25, 0.25));
//	float DepthDelta = abs(dot(DepthNeighbors.xy, vec2(-1.0, 1.0))) + abs(dot(DepthNeighbors.zw, vec2(-1.0, 1.0)));
//	color.rgb *= max(0.5, 1.0 - max(0.0, abs(DepthCenter - DepthAverage) - 0.2 * DepthDelta) / (0.01 + 0.2 * DepthDelta));
//	color.rgb *= step(abs(DepthCenter - DepthAverage), 0.2 * DepthDelta); 

	// shadow method
	float DepthScale1 = 4.0 / DepthCenter; // inner ink (shadow on object)
//	float DepthScale1 = -4.0 / DepthCenter; // outer ink (shadow around object)
//	float DepthScale1 = 0.003;
	float DepthScale2 = DepthScale1 / 2.0;
//	float DepthScale3 = DepthScale1 / 4.0;
	float DepthBias1 = -DepthCenter * DepthScale1;
	float DepthBias2 = -DepthCenter * DepthScale2;
//	float DepthBias3 = -DepthCenter * DepthScale3;
	float DepthShadow = max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2(-1.0,  0.0)).b * DepthScale1 + DepthBias1)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 1.0,  0.0)).b * DepthScale1 + DepthBias1)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0, -1.0)).b * DepthScale1 + DepthBias1)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0,  1.0)).b * DepthScale1 + DepthBias1)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2(-2.0,  0.0)).b * DepthScale2 + DepthBias2)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 2.0,  0.0)).b * DepthScale2 + DepthBias2)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0, -2.0)).b * DepthScale2 + DepthBias2)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0,  2.0)).b * DepthScale2 + DepthBias2)
//	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2(-3.0,  0.0)).b * DepthScale3 + DepthBias3)
//	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 3.0,  0.0)).b * DepthScale3 + DepthBias3)
//	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0, -3.0)).b * DepthScale3 + DepthBias3)
//	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0,  3.0)).b * DepthScale3 + DepthBias3)
	                  - 0.0;
	color.rgb *= 1.0 - max(0.0, min(DepthShadow, 1.0));
//	color.r = DepthCenter / -1024.0;
# endif
#endif

#ifdef USEFOG
	color.rgb = FogVertex(color);
#endif

	// 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
#ifdef USEREFLECTION
	vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);
	//vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;
	vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;
	#ifdef USENORMALMAPSCROLLBLEND
# ifdef USEOFFSETMAPPING
		vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0);
# else
		vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);
# endif
		normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;
		vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw;
	#else
		vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw;
	#endif
	// FIXME temporary hack to detect the case that the reflection
	// gets blackened at edges due to leaving the area that contains actual
	// content.
	// Remove this 'ack once we have a better way to stop this thing from
	// 'appening.
	float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);
	f      *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);
	f      *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);
	f      *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);
	ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);
	color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a);
#endif
#ifdef USEOCCLUDE
   color.rgb *= clamp(float(visiblepixels) / float(allpixels), 0.0, 1.0);
#endif

	dp_FragColor = vec4(color);
}
#endif // FRAGMENT_SHADER

#endif // !MODE_DEFERREDLIGHTSOURCE
#endif // !MODE_DEFERREDGEOMETRY
#endif // !MODE_WATER
#endif // !MODE_REFRACTION
#endif // !MODE_BLOOMBLUR
#endif // !MODE_GENERIC
#endif // !MODE_POSTPROCESS
#endif // !MODE_DEPTH_OR_SHADOW

[fragment shader]
#version 120
#define GLSL120
#define FRAGMENT_SHADER
#define MODE_LIGHTDIRECTION
#define USEDIFFUSE
































#define USEEXACTSPECULARMATH




#define USEBOTHALPHAS
#define USEOFFSETMAPPING_LOD






// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader
// written by Forest 'LordHavoc' Hale
// shadowmapping enhancements by Lee 'eihrul' Salzman

#if defined(USESKELETAL) || defined(USEOCCLUDE)
#  ifdef GL_ARB_uniform_buffer_object
#    extension GL_ARB_uniform_buffer_object : enable
#  endif
#endif

#ifdef USESHADOWMAP2D
# ifdef GL_EXT_gpu_shader4
#   extension GL_EXT_gpu_shader4 : enable
# endif
# ifdef GL_ARB_texture_gather
#   extension GL_ARB_texture_gather : enable
# else
#   ifdef GL_AMD_texture_texture4
#     extension GL_AMD_texture_texture4 : enable
#   endif
# endif
#endif

#ifdef USECELSHADING
# define SHADEDIFFUSE myhalf diffuse = cast_myhalf(min(max(float(dot(surfacenormal, lightnormal)) * 2.0, 0.0), 1.0));
# ifdef USEEXACTSPECULARMATH
#  define SHADESPECULAR(specpow) myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), eyenormal))*-1.0, 0.0)), 1.0 + specpow);specular = max(0.0, specular * 10.0 - 9.0);
# else
#  define SHADESPECULAR(specpow) myhalf3 specularnormal = normalize(lightnormal + eyenormal);myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + specpow);specular = max(0.0, specular * 10.0 - 9.0);
# endif
#else
# define SHADEDIFFUSE myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));
# ifdef USEEXACTSPECULARMATH
#  define SHADESPECULAR(specpow) myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), eyenormal))*-1.0, 0.0)), 1.0 + specpow);
# else
#  define SHADESPECULAR(specpow) myhalf3 specularnormal = normalize(lightnormal + eyenormal);myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + specpow);
# endif
#endif

#if (defined(GLSL120) || defined(GLSL130) || defined(GLSL140) || defined(GLES)) && defined(VERTEX_SHADER)
invariant gl_Position; // fix for lighting polygons not matching base surface
# endif
#if defined(GLSL130) || defined(GLSL140)
precision highp float;
# ifdef VERTEX_SHADER
#  define dp_varying out
#  define dp_attribute in
# endif
# ifdef FRAGMENT_SHADER
out vec4 dp_FragColor;
#  define dp_varying in
#  define dp_attribute in
# endif
# define dp_offsetmapping_dFdx dFdx
# define dp_offsetmapping_dFdy dFdy
# define dp_textureGrad textureGrad
# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d))
# define dp_texture2D texture
# define dp_texture3D texture
# define dp_textureCube texture
# define dp_shadow2D(a,b) float(texture(a,b))
#else
# ifdef FRAGMENT_SHADER
#  define dp_FragColor gl_FragColor
# endif
# define dp_varying varying
# define dp_attribute attribute
# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0)
# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0)
# define dp_textureGrad(a,b,c,d) texture2D(a,b)
# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d))
# define dp_texture2D texture2D
# define dp_texture3D texture3D
# define dp_textureCube textureCube
# define dp_shadow2D(a,b) float(shadow2D(a,b))
#endif

// GL ES and GLSL130 shaders use precision modifiers, standard GL does not
// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout)
#ifndef GL_ES
#define lowp
#define mediump
#define highp
#endif

#ifdef USEDEPTHRGB
	// for 565 RGB we'd need to use different multipliers
#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0))
#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0)))
#endif

#ifdef VERTEX_SHADER
dp_attribute vec4 Attrib_Position;  // vertex
dp_attribute vec4 Attrib_Color;     // color
dp_attribute vec4 Attrib_TexCoord0; // material texcoords
dp_attribute vec3 Attrib_TexCoord1; // svector
dp_attribute vec3 Attrib_TexCoord2; // tvector
dp_attribute vec3 Attrib_TexCoord3; // normal
dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords
#ifdef USESKELETAL
//uniform mat4 Skeletal_Transform[128];
// this is used with glBindBufferRange to bind a uniform block to the name
// Skeletal_Transform12_UniformBlock, the Skeletal_Transform12 variable is
// directly accessible without a namespace.
// explanation: http://www.opengl.org/wiki/Interface_Block_%28GLSL%29#Syntax
uniform Skeletal_Transform12_UniformBlock
{
	vec4 Skeletal_Transform12[768];
};
dp_attribute vec4 Attrib_SkeletalIndex;
dp_attribute vec4 Attrib_SkeletalWeight;
#endif
#endif
dp_varying mediump vec4 VertexColor;

#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE)
# define USEFOG
#endif
#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP)
# define USELIGHTMAP
#endif
#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG)
# define USEEYEVECTOR
#endif

//#ifdef __GLSL_CG_DATA_TYPES
//# define myhalf half
//# define myhalf2 half2
//# define myhalf3 half3
//# define myhalf4 half4
//# define cast_myhalf half
//# define cast_myhalf2 half2
//# define cast_myhalf3 half3
//# define cast_myhalf4 half4
//#else
# define myhalf mediump float
# define myhalf2 mediump vec2
# define myhalf3 mediump vec3
# define myhalf4 mediump vec4
# define cast_myhalf float
# define cast_myhalf2 vec2
# define cast_myhalf3 vec3
# define cast_myhalf4 vec4
//#endif

#ifdef VERTEX_SHADER
uniform highp mat4 ModelViewProjectionMatrix;
#endif

#ifdef VERTEX_SHADER
#ifdef USETRIPPY
// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE
// tweaked scale
uniform highp float ClientTime;
vec4 TrippyVertex(vec4 position)
{
	float worldTime = ClientTime;
	// tweaked for Quake
	worldTime *= 10.0;
	position *= 0.125;
	//~tweaked for Quake
	float distanceSquared = (position.x * position.x + position.z * position.z);
	position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0);
	float y = position.y;
	float x = position.x;
	float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0);
	position.y = x*sin(om)+y*cos(om);
	position.x = x*cos(om)-y*sin(om);
	return position;
}
#endif
#endif

#ifdef MODE_DEPTH_OR_SHADOW
dp_varying highp float Depth;
#ifdef VERTEX_SHADER
void main(void)
{
#ifdef USESKELETAL
	ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
	ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
	ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
	vec4 sw = Attrib_SkeletalWeight;
	vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
	vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
	vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
	mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
	vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
#define Attrib_Position SkeletalVertex
#endif
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
#ifdef USETRIPPY
	gl_Position = TrippyVertex(gl_Position);
#endif
	Depth = gl_Position.z;
}
#endif

#ifdef FRAGMENT_SHADER
void main(void)
{
#ifdef USEDEPTHRGB
	dp_FragColor = encodedepthmacro(Depth);
#else
	dp_FragColor = vec4(1.0,1.0,1.0,1.0);
#endif
}
#endif
#else // !MODE_DEPTH_ORSHADOW




#ifdef MODE_POSTPROCESS
dp_varying mediump vec2 TexCoord1;
dp_varying mediump vec2 TexCoord2;

#ifdef VERTEX_SHADER
void main(void)
{
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
	TexCoord1 = Attrib_TexCoord0.xy;
#ifdef USEBLOOM
	TexCoord2 = Attrib_TexCoord4.xy;
#endif
}
#endif

#ifdef FRAGMENT_SHADER
uniform sampler2D Texture_First;
#ifdef USEBLOOM
uniform sampler2D Texture_Second;
uniform mediump vec4 BloomColorSubtract;
#endif
#ifdef USEGAMMARAMPS
uniform sampler2D Texture_GammaRamps;
#endif
#ifdef USESATURATION
uniform mediump float Saturation;
#endif
#ifdef USEVIEWTINT
uniform mediump vec4 ViewTintColor;
#endif
//uncomment these if you want to use them:
uniform mediump vec4 UserVec1;
uniform mediump vec4 UserVec2;
// uniform mediump vec4 UserVec3;
// uniform mediump vec4 UserVec4;
// uniform highp float ClientTime;
uniform mediump vec2 PixelSize;

#ifdef USEFXAA
// graphitemaster: based off the white paper by Timothy Lottes
// http://developer.download.nvidia.com/assets/gamedev/files/sdk/11/FXAA_WhitePaper.pdf
vec4 fxaa(vec4 inColor, float maxspan)
{
	vec4 ret = inColor; // preserve old
	float mulreduct = 1.0/maxspan;
	float minreduct = (1.0 / 128.0);

	// directions
	vec3 NW = dp_texture2D(Texture_First, TexCoord1 + (vec2(-1.0, -1.0) * PixelSize)).xyz;
	vec3 NE = dp_texture2D(Texture_First, TexCoord1 + (vec2(+1.0, -1.0) * PixelSize)).xyz;
	vec3 SW = dp_texture2D(Texture_First, TexCoord1 + (vec2(-1.0, +1.0) * PixelSize)).xyz;
	vec3 SE = dp_texture2D(Texture_First, TexCoord1 + (vec2(+1.0, +1.0) * PixelSize)).xyz;
	vec3 M = dp_texture2D(Texture_First, TexCoord1).xyz;

	// luminance directions
	vec3 luma = vec3(0.299, 0.587, 0.114);
	float lNW = dot(NW, luma);
	float lNE = dot(NE, luma);
	float lSW = dot(SW, luma);
	float lSE = dot(SE, luma);
	float lM = dot(M, luma);
	float lMin = min(lM, min(min(lNW, lNE), min(lSW, lSE)));
	float lMax = max(lM, max(max(lNW, lNE), max(lSW, lSE)));

	// direction and reciprocal
	vec2 dir = vec2(-((lNW + lNE) - (lSW + lSE)), ((lNW + lSW) - (lNE + lSE)));
	float rcp = 1.0/(min(abs(dir.x), abs(dir.y)) + max((lNW + lNE + lSW + lSE) * (0.25 * mulreduct), minreduct));

	// span
	dir = min(vec2(maxspan, maxspan), max(vec2(-maxspan, -maxspan), dir * rcp)) * PixelSize;

	vec3 rA = (1.0/2.0) * (
		dp_texture2D(Texture_First, TexCoord1 + dir * (1.0/3.0 - 0.5)).xyz +
		dp_texture2D(Texture_First, TexCoord1 + dir * (2.0/3.0 - 0.5)).xyz);
	vec3 rB = rA * (1.0/2.0) + (1.0/4.0) * (
		dp_texture2D(Texture_First, TexCoord1 + dir * (0.0/3.0 - 0.5)).xyz +
		dp_texture2D(Texture_First, TexCoord1 + dir * (3.0/3.0 - 0.5)).xyz);
	float lB = dot(rB, luma);

	ret.xyz = ((lB < lMin) || (lB > lMax)) ? rA : rB;
	ret.a = 1.0;
	return ret;
}
#endif

void main(void)
{
	dp_FragColor = dp_texture2D(Texture_First, TexCoord1);

#ifdef USEFXAA
	dp_FragColor = fxaa(dp_FragColor, 8.0); // 8.0 can be changed for larger span
#endif

#ifdef USEPOSTPROCESSING
// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want
// 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
#if defined(USERVEC1) || defined(USERVEC2)
	float sobel = 1.0;
	// vec2 ts = textureSize(Texture_First, 0);
	// vec2 px = vec2(1/ts.x, 1/ts.y);
	vec2 px = PixelSize;
	vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb;
	vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,  0.0)).rgb;
	vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb;
	vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb;
	vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,  0.0)).rgb;
	vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb;
	vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb;
	vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2(  0.0,-px.y)).rgb;
	vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb;
	vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb;
	vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2(  0.0, px.y)).rgb;
	vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb;
	float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1);
	float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2);
	float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3);
	float px4 =  1.0 * dot(vec3(0.3, 0.59, 0.11), x4);
	float px5 =  2.0 * dot(vec3(0.3, 0.59, 0.11), x5);
	float px6 =  1.0 * dot(vec3(0.3, 0.59, 0.11), x6);
	float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1);
	float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2);
	float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3);
	float py4 =  1.0 * dot(vec3(0.3, 0.59, 0.11), y4);
	float py5 =  2.0 * dot(vec3(0.3, 0.59, 0.11), y5);
	float py6 =  1.0 * dot(vec3(0.3, 0.59, 0.11), y6);
	sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6);
	dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y;
	dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y;
	dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y;
	dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107,  0.707107)) * UserVec1.y;
	dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990,  0.891007)) * UserVec1.y;
	dp_FragColor /= (1.0 + 5.0 * UserVec1.y);
	dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y;
#endif
#endif

#ifdef USEBLOOM
	dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract);
#endif

#ifdef USEVIEWTINT
	dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a);
#endif

#ifdef USESATURATION
	//apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter
	float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114));
	// 'vampire sight' effect, wheres red is compensated
	#ifdef SATURATION_REDCOMPENSATE
		float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation));
		dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation);
		dp_FragColor.r += rboost;
	#else
		// normal desaturation
		//dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation;
		dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation);
	#endif
#endif

#ifdef USEGAMMARAMPS
	dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r;
	dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g;
	dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b;
#endif
}
#endif
#else // !MODE_POSTPROCESS




#ifdef MODE_GENERIC
#ifdef USEDIFFUSE
dp_varying mediump vec2 TexCoord1;
#endif
#ifdef USESPECULAR
dp_varying mediump vec2 TexCoord2;
#endif
uniform myhalf Alpha;
#ifdef VERTEX_SHADER
void main(void)
{
#ifdef USESKELETAL
	ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
	ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
	ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
	vec4 sw = Attrib_SkeletalWeight;
	vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
	vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
	vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
	mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
	vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
#define Attrib_Position SkeletalVertex
#endif
	VertexColor = Attrib_Color;
#ifdef USEDIFFUSE
	TexCoord1 = Attrib_TexCoord0.xy;
#endif
#ifdef USESPECULAR
	TexCoord2 = Attrib_TexCoord1.xy;
#endif
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
#ifdef USETRIPPY
	gl_Position = TrippyVertex(gl_Position);
#endif
}
#endif

#ifdef FRAGMENT_SHADER
#ifdef USEDIFFUSE
uniform sampler2D Texture_First;
#endif
#ifdef USESPECULAR
uniform sampler2D Texture_Second;
#endif
#ifdef USEGAMMARAMPS
uniform sampler2D Texture_GammaRamps;
#endif

void main(void)
{
#ifdef USEVIEWTINT
	dp_FragColor = VertexColor;
#else
	dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
#endif
#ifdef USEDIFFUSE
# ifdef USEREFLECTCUBE
	// suppress texture alpha
	dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb;
# else
	dp_FragColor *= dp_texture2D(Texture_First, TexCoord1);
# endif
#endif

#ifdef USESPECULAR
	vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2);
# ifdef USECOLORMAPPING
	dp_FragColor *= tex2;
# endif
# ifdef USEGLOW
	dp_FragColor += tex2;
# endif
# ifdef USEVERTEXTEXTUREBLEND
	dp_FragColor = mix(dp_FragColor, tex2, tex2.a);
# endif
#endif
#ifdef USEGAMMARAMPS
	dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r;
	dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g;
	dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b;
#endif
#ifdef USEALPHAKILL
	dp_FragColor.a *= Alpha;
#endif
}
#endif
#else // !MODE_GENERIC




#ifdef MODE_BLOOMBLUR
dp_varying mediump vec2 TexCoord;
#ifdef VERTEX_SHADER
void main(void)
{
	VertexColor = Attrib_Color;
	TexCoord = Attrib_TexCoord0.xy;
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
}
#endif

#ifdef FRAGMENT_SHADER
uniform sampler2D Texture_First;
uniform mediump vec4 BloomBlur_Parameters;

void main(void)
{
	int i;
	vec2 tc = TexCoord;
	vec3 color = dp_texture2D(Texture_First, tc).rgb;
	tc += BloomBlur_Parameters.xy;
	for (i = 1;i < SAMPLES;i++)
	{
		color += dp_texture2D(Texture_First, tc).rgb;
		tc += BloomBlur_Parameters.xy;
	}
	dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1);
}
#endif
#else // !MODE_BLOOMBLUR
#ifdef MODE_REFRACTION
dp_varying mediump vec2 TexCoord;
dp_varying highp vec4 ModelViewProjectionPosition;
uniform highp mat4 TexMatrix;
#ifdef VERTEX_SHADER

void main(void)
{
#ifdef USESKELETAL
	ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
	ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
	ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
	vec4 sw = Attrib_SkeletalWeight;
	vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
	vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
	vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
	mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
	vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
#define Attrib_Position SkeletalVertex
#endif
#ifdef USEALPHAGENVERTEX
	VertexColor = Attrib_Color;
#endif
	TexCoord = vec2(TexMatrix * Attrib_TexCoord0);
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
	ModelViewProjectionPosition = gl_Position;
#ifdef USETRIPPY
	gl_Position = TrippyVertex(gl_Position);
#endif
}
#endif

#ifdef FRAGMENT_SHADER
uniform sampler2D Texture_Normal;
uniform sampler2D Texture_Refraction;

uniform mediump vec4 DistortScaleRefractReflect;
uniform mediump vec4 ScreenScaleRefractReflect;
uniform mediump vec4 ScreenCenterRefractReflect;
uniform mediump vec4 RefractColor;
uniform mediump vec4 ReflectColor;
uniform highp float ClientTime;
#ifdef USENORMALMAPSCROLLBLEND
uniform highp vec2 NormalmapScrollBlend;
#endif

void main(void)
{
	vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);
	//vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;
	vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;
#ifdef USEALPHAGENVERTEX
	vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a;
	vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a);
#else
	vec2 distort = DistortScaleRefractReflect.xy;
	vec4 refractcolor = RefractColor;
#endif
	#ifdef USENORMALMAPSCROLLBLEND
		vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);
		normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;
		vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort;
	#else
		vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort;
	#endif
	// FIXME temporary hack to detect the case that the reflection
	// gets blackened at edges due to leaving the area that contains actual
	// content.
	// Remove this 'ack once we have a better way to stop this thing from
	// 'appening.
	float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);
	f      *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);
	f      *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);
	f      *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);
	ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);
	dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor;
}
#endif
#else // !MODE_REFRACTION




#ifdef MODE_WATER
dp_varying mediump vec2 TexCoord;
dp_varying highp vec3 EyeVector;
dp_varying highp vec4 ModelViewProjectionPosition;
#ifdef VERTEX_SHADER
uniform highp vec3 EyePosition;
uniform highp mat4 TexMatrix;

void main(void)
{
#ifdef USESKELETAL
	ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
	ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
	ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
	vec4 sw = Attrib_SkeletalWeight;
	vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
	vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
	vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
	mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
	mat3 SkeletalNormalMatrix = mat3(cross(SkeletalMatrix[1].xyz, SkeletalMatrix[2].xyz), cross(SkeletalMatrix[2].xyz, SkeletalMatrix[0].xyz), cross(SkeletalMatrix[0].xyz, SkeletalMatrix[1].xyz)); // is actually transpose(inverse(mat3(SkeletalMatrix))) * det(mat3(SkeletalMatrix))
	vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
	vec3 SkeletalSVector = normalize(Attrib_TexCoord1.xyz * SkeletalNormalMatrix);
	vec3 SkeletalTVector = normalize(Attrib_TexCoord2.xyz * SkeletalNormalMatrix);
	vec3 SkeletalNormal  = normalize(Attrib_TexCoord3.xyz * SkeletalNormalMatrix);
#define Attrib_Position SkeletalVertex
#define Attrib_TexCoord1 SkeletalSVector
#define Attrib_TexCoord2 SkeletalTVector
#define Attrib_TexCoord3 SkeletalNormal
#endif
#ifdef USEALPHAGENVERTEX
	VertexColor = Attrib_Color;
#endif
	TexCoord = vec2(TexMatrix * Attrib_TexCoord0);
	vec3 EyeRelative = EyePosition - Attrib_Position.xyz;
	EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz);
	EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz);
	EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz);
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
	ModelViewProjectionPosition = gl_Position;
#ifdef USETRIPPY
	gl_Position = TrippyVertex(gl_Position);
#endif
}
#endif

#ifdef FRAGMENT_SHADER
uniform sampler2D Texture_Normal;
uniform sampler2D Texture_Refraction;
uniform sampler2D Texture_Reflection;

uniform mediump vec4 DistortScaleRefractReflect;
uniform mediump vec4 ScreenScaleRefractReflect;
uniform mediump vec4 ScreenCenterRefractReflect;
uniform mediump vec4 RefractColor;
uniform mediump vec4 ReflectColor;
uniform mediump float ReflectFactor;
uniform mediump float ReflectOffset;
uniform highp float ClientTime;
#ifdef USENORMALMAPSCROLLBLEND
uniform highp vec2 NormalmapScrollBlend;
#endif

void main(void)
{
	vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);
	//vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;
	vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;
	//SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0);
	// slight water animation via 2 layer scrolling (todo: tweak)
#ifdef USEALPHAGENVERTEX
	vec4 distort = DistortScaleRefractReflect * VertexColor.a;
	float reflectoffset = ReflectOffset * VertexColor.a;
	float reflectfactor = ReflectFactor * VertexColor.a;
	vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a);
#else
	vec4 distort = DistortScaleRefractReflect;
	float reflectoffset = ReflectOffset;
	float reflectfactor = ReflectFactor;
	vec4 refractcolor = RefractColor;
#endif
	#ifdef USENORMALMAPSCROLLBLEND
		vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);
		normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;
		vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort;
	#else
		vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort;
	#endif
	// FIXME temporary hack to detect the case that the reflection
	// gets blackened at edges due to leaving the area that contains actual
	// content.
	// Remove this 'ack once we have a better way to stop this thing from
	// 'appening.
	float f  = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002);
	f       *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002);
	f       *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002);
	f       *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002);
	ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);
	f  = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002);
	f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002);
	f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002);
	f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002);
	ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);
	float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset;
	dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);
}
#endif
#else // !MODE_WATER




// common definitions between vertex shader and fragment shader:

dp_varying mediump vec4 TexCoordSurfaceLightmap;
#ifdef USEVERTEXTEXTUREBLEND
dp_varying mediump vec2 TexCoord2;
#endif

#ifdef MODE_LIGHTSOURCE
dp_varying mediump vec3 CubeVector;
#endif

#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE)
dp_varying mediump vec3 LightVector;
#endif

#ifdef USEEYEVECTOR
dp_varying highp vec4 EyeVectorFogDepth;
#endif

#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL)
dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent)
dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal)
dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal)
#else
# ifdef USEFOG
dp_varying highp vec3 EyeVectorModelSpace;
# endif
#endif

#ifdef USEREFLECTION
dp_varying highp vec4 ModelViewProjectionPosition;
#endif
#ifdef MODE_DEFERREDLIGHTSOURCE
uniform highp vec3 LightPosition;
dp_varying highp vec4 ModelViewPosition;
#endif

#ifdef MODE_LIGHTSOURCE
uniform highp vec3 LightPosition;
#endif
uniform highp vec3 EyePosition;
#ifdef MODE_LIGHTDIRECTION
uniform highp vec3 LightDir;
#endif
uniform highp vec4 FogPlane;

#ifdef USESHADOWMAPORTHO
dp_varying highp vec3 ShadowMapTC;
#endif

#ifdef USEBOUNCEGRID
dp_varying highp vec3 BounceGridTexCoord;
#endif

#ifdef MODE_DEFERREDGEOMETRY
dp_varying highp float Depth;
#endif






// 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

// fragment shader specific:
#ifdef FRAGMENT_SHADER

uniform sampler2D Texture_Normal;
uniform sampler2D Texture_Color;
uniform sampler2D Texture_Gloss;
#ifdef USEGLOW
uniform sampler2D Texture_Glow;
#endif
#ifdef USEVERTEXTEXTUREBLEND
uniform sampler2D Texture_SecondaryNormal;
uniform sampler2D Texture_SecondaryColor;
uniform sampler2D Texture_SecondaryGloss;
#ifdef USEGLOW
uniform sampler2D Texture_SecondaryGlow;
#endif
#endif
#ifdef USECOLORMAPPING
uniform sampler2D Texture_Pants;
uniform sampler2D Texture_Shirt;
#endif
#ifdef USEFOG
#ifdef USEFOGHEIGHTTEXTURE
uniform sampler2D Texture_FogHeightTexture;
#endif
uniform sampler2D Texture_FogMask;
#endif
#ifdef USELIGHTMAP
uniform sampler2D Texture_Lightmap;
#endif
#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)
uniform sampler2D Texture_Deluxemap;
#endif
#ifdef USEREFLECTION
uniform sampler2D Texture_Reflection;
#endif

#ifdef MODE_DEFERREDLIGHTSOURCE
uniform sampler2D Texture_ScreenNormalMap;
#endif
#ifdef USEDEFERREDLIGHTMAP
#ifdef USECELOUTLINES
uniform sampler2D Texture_ScreenNormalMap;
#endif
uniform sampler2D Texture_ScreenDiffuse;
uniform sampler2D Texture_ScreenSpecular;
#endif

uniform mediump vec3 Color_Pants;
uniform mediump vec3 Color_Shirt;
uniform mediump vec3 FogColor;

#ifdef USEFOG
uniform highp float FogRangeRecip;
uniform highp float FogPlaneViewDist;
uniform highp float FogHeightFade;
vec3 FogVertex(vec4 surfacecolor)
{
#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL)
	vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w);
#endif
	float FogPlaneVertexDist = EyeVectorFogDepth.w;
	float fogfrac;
       vec3 fc = FogColor;
#ifdef USEFOGALPHAHACK
	fc *= surfacecolor.a;
#endif
#ifdef USEFOGHEIGHTTEXTURE
	vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade));
	fogfrac = fogheightpixel.a;
	return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);
#else
# ifdef USEFOGOUTSIDE
	fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);
# else
	fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
# endif
	return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);
#endif
}
#endif

#ifdef USEOFFSETMAPPING
uniform mediump vec4 OffsetMapping_ScaleSteps;
uniform mediump float OffsetMapping_Bias;
#ifdef USEOFFSETMAPPING_LOD
uniform mediump float OffsetMapping_LodDistance;
#endif
vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy)
{
	float i;
	// distance-based LOD
#ifdef USEOFFSETMAPPING_LOD
	//mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z);
	//mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor);
	mediump float GuessLODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z);
#ifdef USEOFFSETMAPPING_RELIEFMAPPING
	// stupid workaround because 1-step and 2-step reliefmapping is void
	mediump float LODSteps = max(3.0, ceil(GuessLODFactor * OffsetMapping_ScaleSteps.y));
#else
	mediump float LODSteps = ceil(GuessLODFactor * OffsetMapping_ScaleSteps.y);
#endif
	mediump float LODFactor = LODSteps / OffsetMapping_ScaleSteps.y;
	mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, LODSteps, 1.0 / LODSteps, OffsetMapping_ScaleSteps.w * LODFactor);
#else
	#define ScaleSteps OffsetMapping_ScaleSteps
#endif
#ifdef USEOFFSETMAPPING_RELIEFMAPPING
	float f;
	// 14 sample relief mapping: linear search and then binary search
	// this basically steps forward a small amount repeatedly until it finds
	// itself inside solid, then jitters forward and back using decreasing
	// amounts to find the impact
	//vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1);
	//vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1);
	vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1);
	vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1);
	OffsetVector *= ScaleSteps.z;
	for(i = 1.0; i < ScaleSteps.y; ++i)
		RT += OffsetVector *  step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z);
	for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5)
		RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f);
	return RT.xy;
#else
	// 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits)
	//vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1));
	//vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1));
	vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1));
	OffsetVector *= ScaleSteps.z;
	for(i = 0.0; i < ScaleSteps.y; ++i)
		TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a);
	return TexCoord;
#endif
}
#endif // USEOFFSETMAPPING

#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)
uniform sampler2D Texture_Attenuation;
uniform samplerCube Texture_Cube;
#endif

#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO)

#ifdef USESHADOWMAP2D
# ifdef USESHADOWSAMPLER
uniform sampler2DShadow Texture_ShadowMap2D;
# else
uniform sampler2D Texture_ShadowMap2D;
# endif
#endif

#ifdef USESHADOWMAPVSDCT
uniform samplerCube Texture_CubeProjection;
#endif

#if defined(USESHADOWMAP2D)
uniform mediump vec4 ShadowMap_TextureScale;
uniform mediump vec4 ShadowMap_Parameters;
#endif

#if defined(USESHADOWMAP2D)
# ifdef USESHADOWMAPORTHO
#  define GetShadowMapTC2D(dir) (max(vec3(0.0, 0.0, 0.0), min(dir, ShadowMap_Parameters.xyz)))
# else
#  ifdef USESHADOWMAPVSDCT
vec3 GetShadowMapTC2D(vec3 dir)
{
	vec3 adir = abs(dir);
	float m = max(max(adir.x, adir.y), adir.z);
	vec4 proj = dp_textureCube(Texture_CubeProjection, dir);
#ifdef USEDEPTHRGB
	return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) +  proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w);
#else
	vec2 mparams = ShadowMap_Parameters.xy / m;
	return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w);
#endif
}
#  else
vec3 GetShadowMapTC2D(vec3 dir)
{
	vec3 adir = abs(dir);
	float m; vec4 proj;
	if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); }
	if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); }
#ifdef USEDEPTHRGB
	return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w);
#else
	vec2 mparams = ShadowMap_Parameters.xy / m;
	return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w);
#endif
}
#  endif
# endif
#endif // defined(USESHADOWMAP2D)

# ifdef USESHADOWMAP2D
float ShadowMapCompare(vec3 dir)
{
	vec3 shadowmaptc = GetShadowMapTC2D(dir) + vec3(ShadowMap_TextureScale.zw, 0.0f);
	float f;

#  ifdef USEDEPTHRGB
#   ifdef USESHADOWMAPPCF
#    define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale.xy))
#    if USESHADOWMAPPCF > 1
	vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);
	center *= ShadowMap_TextureScale.xy;
	vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));
	vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));
	vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));
	vec4 row4 = step(shadowmaptc.z, vec4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));
	vec4 cols = row2 + row3 + mix(row1, row4, offset.y);
	f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));
#    else
	vec2 center = shadowmaptc.xy*ShadowMap_TextureScale.xy, offset = fract(shadowmaptc.xy);
	vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));
	vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));
	vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));
	vec3 cols = row2 + mix(row1, row3, offset.y);
	f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));
#    endif
#   else
	f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale.xy)));
#   endif
#  else
#   ifdef USESHADOWSAMPLER
#     ifdef USESHADOWMAPPCF
#       define texval(off) dp_shadow2D(Texture_ShadowMap2D, vec3(off, shadowmaptc.z))  
	vec2 offset = fract(shadowmaptc.xy - 0.5);
   vec4 size = vec4(offset + 1.0, 2.0 - offset);
#       if USESHADOWMAPPCF > 1
   vec2 center = (shadowmaptc.xy - offset + 0.5)*ShadowMap_TextureScale.xy;
   vec4 weight = (vec4(-1.5, -1.5, 2.0, 2.0) + (shadowmaptc.xy - 0.5*offset).xyxy)*ShadowMap_TextureScale.xyxy;
	f = (1.0/25.0)*dot(size.zxzx*size.wwyy, vec4(texval(weight.xy), texval(weight.zy), texval(weight.xw), texval(weight.zw))) +
		(2.0/25.0)*dot(size, vec4(texval(vec2(weight.z, center.y)), texval(vec2(center.x, weight.w)), texval(vec2(weight.x, center.y)), texval(vec2(center.x, weight.y)))) +
		(4.0/25.0)*texval(center);
#       else
	vec4 weight = (vec4(1.0, 1.0, -0.5, -0.5) + (shadowmaptc.xy - 0.5*offset).xyxy)*ShadowMap_TextureScale.xyxy;
	f = (1.0/9.0)*dot(size.zxzx*size.wwyy, vec4(texval(weight.zw), texval(weight.xw), texval(weight.zy), texval(weight.xy)));
#       endif        
#     else
	f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale.xy, shadowmaptc.z));
#     endif
#   else
#     ifdef USESHADOWMAPPCF
#      if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)
#       ifdef GL_ARB_texture_gather
#         define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y))
#       else
#         define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale.xy)
#       endif
	vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale.xy;
#       if USESHADOWMAPPCF > 1
   vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0));
   vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0));
   vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0));
   vec4 group4 = step(shadowmaptc.z, texval(-2.0,  0.0));
   vec4 group5 = step(shadowmaptc.z, texval( 0.0,  0.0));
   vec4 group6 = step(shadowmaptc.z, texval( 2.0,  0.0));
   vec4 group7 = step(shadowmaptc.z, texval(-2.0,  2.0));
   vec4 group8 = step(shadowmaptc.z, texval( 0.0,  2.0));
   vec4 group9 = step(shadowmaptc.z, texval( 2.0,  2.0));
	vec4 locols = vec4(group1.ab, group3.ab);
	vec4 hicols = vec4(group7.rg, group9.rg);
	locols.yz += group2.ab;
	hicols.yz += group8.rg;
	vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) +
				vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) +
				mix(locols, hicols, offset.y);
	vec4 cols = group5 + vec4(group2.rg, group8.ab);
	cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x);
	f = dot(cols, vec4(1.0/25.0));
#      else
	vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));
	vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));
	vec4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));
	vec4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));
	vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) +
				mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y);
	f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));
#       endif
#      else
#       ifdef GL_EXT_gpu_shader4
#         define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r
#       else
#         define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale.xy).r  
#       endif
#       if USESHADOWMAPPCF > 1
	vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);
	center *= ShadowMap_TextureScale.xy;
	vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));
	vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));
	vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));
	vec4 row4 = step(shadowmaptc.z, vec4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));
	vec4 cols = row2 + row3 + mix(row1, row4, offset.y);
	f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));
#       else
	vec2 center = shadowmaptc.xy*ShadowMap_TextureScale.xy, offset = fract(shadowmaptc.xy);
	vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));
	vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));
	vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));
	vec3 cols = row2 + mix(row1, row3, offset.y);
	f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));
#       endif
#      endif
#     else
	f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale.xy).r);
#     endif
#   endif
#  endif
#  ifdef USESHADOWMAPORTHO
	return mix(ShadowMap_Parameters.w, 1.0, f);
#  else
	return f;
#  endif
}
# endif
#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO)
#endif // FRAGMENT_SHADER




#ifdef MODE_DEFERREDGEOMETRY
#ifdef VERTEX_SHADER
uniform highp mat4 TexMatrix;
#ifdef USEVERTEXTEXTUREBLEND
uniform highp mat4 BackgroundTexMatrix;
#endif
uniform highp mat4 ModelViewMatrix;
void main(void)
{
#ifdef USESKELETAL
	ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
	ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
	ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
	vec4 sw = Attrib_SkeletalWeight;
	vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
	vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
	vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
	mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
	mat3 SkeletalNormalMatrix = mat3(cross(SkeletalMatrix[1].xyz, SkeletalMatrix[2].xyz), cross(SkeletalMatrix[2].xyz, SkeletalMatrix[0].xyz), cross(SkeletalMatrix[0].xyz, SkeletalMatrix[1].xyz)); // is actually transpose(inverse(mat3(SkeletalMatrix))) * det(mat3(SkeletalMatrix))
	vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
	vec3 SkeletalSVector = normalize(Attrib_TexCoord1.xyz * SkeletalNormalMatrix);
	vec3 SkeletalTVector = normalize(Attrib_TexCoord2.xyz * SkeletalNormalMatrix);
	vec3 SkeletalNormal  = normalize(Attrib_TexCoord3.xyz * SkeletalNormalMatrix);
#define Attrib_Position SkeletalVertex
#define Attrib_TexCoord1 SkeletalSVector
#define Attrib_TexCoord2 SkeletalTVector
#define Attrib_TexCoord3 SkeletalNormal
#endif
	TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0);
#ifdef USEVERTEXTEXTUREBLEND
	VertexColor = Attrib_Color;
	TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0);
#endif

	// transform unnormalized eye direction into tangent space
#ifdef USEOFFSETMAPPING
	vec3 EyeRelative = EyePosition - Attrib_Position.xyz;
	EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz);
	EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz);
	EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz);
	EyeVectorFogDepth.w = 0.0;
#endif

	VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0));
	VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0));
	VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0));
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
#ifdef USETRIPPY
	gl_Position = TrippyVertex(gl_Position);
#endif
	Depth = (ModelViewMatrix * Attrib_Position).z;
}
#endif // VERTEX_SHADER

#ifdef FRAGMENT_SHADER
void main(void)
{
#ifdef USEOFFSETMAPPING
	// apply offsetmapping
	vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy);
	vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy);
	vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy);
# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy)
#else
# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy)
#endif

#ifdef USEALPHAKILL
	if (offsetMappedTexture2D(Texture_Color).a < 0.5)
		discard;
#endif

#ifdef USEVERTEXTEXTUREBLEND
	float alpha = offsetMappedTexture2D(Texture_Color).a;
	float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0));
	//float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0));
	//float terrainblend = float(VertexColor.a) * alpha > 0.5;
#endif

#ifdef USEVERTEXTEXTUREBLEND
	vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5);
	float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend);
#else
	vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5);
	float a = offsetMappedTexture2D(Texture_Gloss).a;
#endif

	vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz);
	dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a);
}
#endif // FRAGMENT_SHADER
#else // !MODE_DEFERREDGEOMETRY




#ifdef MODE_DEFERREDLIGHTSOURCE
#ifdef VERTEX_SHADER
uniform highp mat4 ModelViewMatrix;
void main(void)
{
	ModelViewPosition = ModelViewMatrix * Attrib_Position;
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;
}
#endif // VERTEX_SHADER

#ifdef FRAGMENT_SHADER
uniform highp mat4 ViewToLight;
// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far));
uniform highp vec2 ScreenToDepth;
uniform myhalf3 DeferredColor_Ambient;
uniform myhalf3 DeferredColor_Diffuse;
#ifdef USESPECULAR
uniform myhalf3 DeferredColor_Specular;
uniform myhalf SpecularPower;
#endif
uniform myhalf2 PixelToScreenTexCoord;
void main(void)
{
	// calculate viewspace pixel position
	vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;
	vec3 position;
	// get the geometry information (depth, normal, specular exponent)
	myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord);
	// decode viewspace pixel normal
//	myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5));
	myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg)));
	// decode viewspace pixel position
//	position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord));
	position.z = normalmap.b;
//	position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x);
	position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);

	// now do the actual shading
	// surfacenormal = pixel normal in viewspace
	// LightVector = pixel to light in viewspace
	// CubeVector = pixel in lightspace
	// eyenormal = pixel to view direction in viewspace
	vec3 CubeVector = vec3(ViewToLight * vec4(position,1));
	myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));
#ifdef USEDIFFUSE
	// calculate diffuse shading
	myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position));
SHADEDIFFUSE
#endif
#ifdef USESPECULAR
	// calculate directional shading
	myhalf3 eyenormal = -normalize(cast_myhalf3(position));
SHADESPECULAR(SpecularPower * normalmap.a)
#endif

#if defined(USESHADOWMAP2D)
	fade *= ShadowMapCompare(CubeVector);
#endif

#ifdef USESPECULAR
	gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);
	gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0);
# ifdef USECUBEFILTER
	vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb;
	gl_FragData[0].rgb *= cubecolor;
	gl_FragData[1].rgb *= cubecolor;
# endif
#else
# ifdef USEDIFFUSE
	gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);
# else
	gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0);
# endif
# ifdef USECUBEFILTER
	vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb;
	gl_FragColor.rgb *= cubecolor;
# endif
#endif
}
#endif // FRAGMENT_SHADER
#else // !MODE_DEFERREDLIGHTSOURCE




#ifdef VERTEX_SHADER
uniform highp mat4 TexMatrix;
#ifdef USEVERTEXTEXTUREBLEND
uniform highp mat4 BackgroundTexMatrix;
#endif
#ifdef MODE_LIGHTSOURCE
uniform highp mat4 ModelToLight;
#endif
#ifdef USESHADOWMAPORTHO
uniform highp mat4 ShadowMapMatrix;
#endif
#ifdef USEBOUNCEGRID
uniform highp mat4 BounceGridMatrix;
#endif
void main(void)
{
#ifdef USESKELETAL
	ivec4 si0 = ivec4(Attrib_SkeletalIndex * 3.0);
	ivec4 si1 = si0 + ivec4(1, 1, 1, 1);
	ivec4 si2 = si0 + ivec4(2, 2, 2, 2);
	vec4 sw = Attrib_SkeletalWeight;
	vec4 SkeletalMatrix1 = Skeletal_Transform12[si0.x] * sw.x + Skeletal_Transform12[si0.y] * sw.y + Skeletal_Transform12[si0.z] * sw.z + Skeletal_Transform12[si0.w] * sw.w;
	vec4 SkeletalMatrix2 = Skeletal_Transform12[si1.x] * sw.x + Skeletal_Transform12[si1.y] * sw.y + Skeletal_Transform12[si1.z] * sw.z + Skeletal_Transform12[si1.w] * sw.w;
	vec4 SkeletalMatrix3 = Skeletal_Transform12[si2.x] * sw.x + Skeletal_Transform12[si2.y] * sw.y + Skeletal_Transform12[si2.z] * sw.z + Skeletal_Transform12[si2.w] * sw.w;
	mat4 SkeletalMatrix = mat4(SkeletalMatrix1, SkeletalMatrix2, SkeletalMatrix3, vec4(0.0, 0.0, 0.0, 1.0));
//	ivec4 si = ivec4(Attrib_SkeletalIndex);
//	mat4 SkeletalMatrix = Skeletal_Transform[si.x] * Attrib_SkeletalWeight.x + Skeletal_Transform[si.y] * Attrib_SkeletalWeight.y + Skeletal_Transform[si.z] * Attrib_SkeletalWeight.z + Skeletal_Transform[si.w] * Attrib_SkeletalWeight.w;
	mat3 SkeletalNormalMatrix = mat3(cross(SkeletalMatrix[1].xyz, SkeletalMatrix[2].xyz), cross(SkeletalMatrix[2].xyz, SkeletalMatrix[0].xyz), cross(SkeletalMatrix[0].xyz, SkeletalMatrix[1].xyz)); // is actually transpose(inverse(mat3(SkeletalMatrix))) * det(mat3(SkeletalMatrix))
	vec4 SkeletalVertex = Attrib_Position * SkeletalMatrix;
	SkeletalVertex.w = 1.0;
	vec3 SkeletalSVector = normalize(Attrib_TexCoord1.xyz * SkeletalNormalMatrix);
	vec3 SkeletalTVector = normalize(Attrib_TexCoord2.xyz * SkeletalNormalMatrix);
	vec3 SkeletalNormal  = normalize(Attrib_TexCoord3.xyz * SkeletalNormalMatrix);
#define Attrib_Position SkeletalVertex
#define Attrib_TexCoord1 SkeletalSVector
#define Attrib_TexCoord2 SkeletalTVector
#define Attrib_TexCoord3 SkeletalNormal
#endif

#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX)
	VertexColor = Attrib_Color;
#endif
	// copy the surface texcoord
#ifdef USELIGHTMAP
	TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy);
#else
	TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0);
#endif
#ifdef USEVERTEXTEXTUREBLEND
	TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0);
#endif

#ifdef USEBOUNCEGRID
	BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position);
#ifdef USEBOUNCEGRIDDIRECTIONAL
	BounceGridTexCoord.z *= 0.125;
#endif
#endif

#ifdef MODE_LIGHTSOURCE
	// transform vertex position into light attenuation/cubemap space
	// (-1 to +1 across the light box)
	CubeVector = vec3(ModelToLight * Attrib_Position);

# ifdef USEDIFFUSE
	// transform unnormalized light direction into tangent space
	// (we use unnormalized to ensure that it interpolates correctly and then
	//  normalize it per pixel)
	vec3 lightminusvertex = LightPosition - Attrib_Position.xyz;
	LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz);
	LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz);
	LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz);
# endif
#endif

#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE)
	LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz);
	LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz);
	LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz);
#endif

	// transform unnormalized eye direction into tangent space
#ifdef USEEYEVECTOR
	vec3 EyeRelative = EyePosition - Attrib_Position.xyz;
	EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz);
	EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz);
	EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz);
#ifdef USEFOG
	EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position);
#else
	EyeVectorFogDepth.w = 0.0;
#endif
#endif


#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL)
# ifdef USEFOG
	VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x);
	VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y);
	VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z);
# else
	VectorS = vec4(Attrib_TexCoord1, 0);
	VectorT = vec4(Attrib_TexCoord2, 0);
	VectorR = vec4(Attrib_TexCoord3, 0);
# endif
#else
# ifdef USEFOG
	EyeVectorModelSpace = EyePosition - Attrib_Position.xyz;
# endif
#endif

	// transform vertex to clipspace (post-projection, but before perspective divide by W occurs)
	gl_Position = ModelViewProjectionMatrix * Attrib_Position;

#ifdef USESHADOWMAPORTHO
	ShadowMapTC = vec3(ShadowMapMatrix * gl_Position);
#endif

#ifdef USEREFLECTION
	ModelViewProjectionPosition = gl_Position;
#endif
#ifdef USETRIPPY
	gl_Position = TrippyVertex(gl_Position);
#endif
}
#endif // VERTEX_SHADER




#ifdef FRAGMENT_SHADER
#ifdef USEDEFERREDLIGHTMAP
uniform myhalf2 PixelToScreenTexCoord;
uniform myhalf3 DeferredMod_Diffuse;
uniform myhalf3 DeferredMod_Specular;
#endif
uniform myhalf3 Color_Ambient;
uniform myhalf3 Color_Diffuse;
uniform myhalf3 Color_Specular;
uniform myhalf SpecularPower;
#ifdef USEGLOW
uniform myhalf3 Color_Glow;
#endif
uniform myhalf Alpha;
#ifdef USEREFLECTION
uniform mediump vec4 DistortScaleRefractReflect;
uniform mediump vec4 ScreenScaleRefractReflect;
uniform mediump vec4 ScreenCenterRefractReflect;
uniform mediump vec4 ReflectColor;
#endif
#ifdef USEREFLECTCUBE
uniform highp mat4 ModelToReflectCube;
uniform sampler2D Texture_ReflectMask;
uniform samplerCube Texture_ReflectCube;
#endif
#ifdef USEBOUNCEGRID
uniform sampler3D Texture_BounceGrid;
uniform float BounceGridIntensity;
uniform highp mat4 BounceGridMatrix;
#endif
uniform highp float ClientTime;
#ifdef USENORMALMAPSCROLLBLEND
uniform highp vec2 NormalmapScrollBlend;
#endif
#ifdef USEOCCLUDE
uniform occludeQuery {
    uint visiblepixels;
    uint allpixels;
};
#endif
void main(void)
{
#ifdef USEOFFSETMAPPING
	// apply offsetmapping
	vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy);
	vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy);
	vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy);
# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy)
# define TexCoord TexCoordOffset
#else
# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy)
# define TexCoord TexCoordSurfaceLightmap.xy
#endif

	// combine the diffuse textures (base, pants, shirt)
	myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color));
#ifdef USEALPHAKILL
	if (color.a < 0.5)
		discard;
#endif
	color.a *= Alpha;
#ifdef USECOLORMAPPING
	color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt;
#endif
#ifdef USEVERTEXTEXTUREBLEND
#ifdef USEBOTHALPHAS
	myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2));
	myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0));
	color.rgb = mix(color2.rgb, color.rgb, terrainblend);
#else
	myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0));
	//myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0));
	//myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5;
	color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend);
#endif
	color.a = 1.0;
	//color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend);
#endif
#ifdef USEALPHAGENVERTEX
	color.a *= VertexColor.a;
#endif

	// get the surface normal
#ifdef USEVERTEXTEXTUREBLEND
	myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5));
#else
	myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5));
#endif

	// get the material colors
	myhalf3 diffusetex = color.rgb;
#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)
# ifdef USEVERTEXTEXTUREBLEND
	myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend);
# else
	myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss));
# endif
#endif

#ifdef USEREFLECTCUBE
	vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal);
	vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz;
	vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0));
	diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord));
#endif

#ifdef USESPECULAR
	myhalf3 eyenormal = normalize(cast_myhalf3(EyeVectorFogDepth.xyz));
#endif




#ifdef MODE_LIGHTSOURCE
	// light source
#ifdef USEDIFFUSE
	myhalf3 lightnormal = cast_myhalf3(normalize(LightVector));
SHADEDIFFUSE
	color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);
#ifdef USESPECULAR
SHADESPECULAR(SpecularPower * glosstex.a)
	color.rgb += glosstex.rgb * (specular * Color_Specular);
#endif
#else
	color.rgb = diffusetex * Color_Ambient;
#endif
	color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));
#if defined(USESHADOWMAP2D)
	color.rgb *= ShadowMapCompare(CubeVector);
#endif
# ifdef USECUBEFILTER
	color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector));
# endif
#endif // MODE_LIGHTSOURCE




#ifdef MODE_LIGHTDIRECTION
	#define SHADING
	#ifdef USEDIFFUSE
		myhalf3 lightnormal = cast_myhalf3(normalize(LightVector));
	#endif
	#define lightcolor 1
#endif // MODE_LIGHTDIRECTION
#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE
   #define SHADING
	// deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe)
	myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0);
	myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw));
	// convert modelspace light vector to tangentspace
	myhalf3 lightnormal;
	lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS));
	lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT));
	lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR));
	lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this
	// calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)
	// note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar
	// is used (the lightmap and deluxemap coords correspond to virtually random coordinates
	// on that luxel, and NOT to its center, because recursive triangle subdivision is used
	// to map the luxels to coordinates on the draw surfaces), which also causes
	// deluxemaps to be wrong because light contributions from the wrong side of the surface
	// are added up. To prevent divisions by zero or strong exaggerations, a max()
	// nudge is done here at expense of some additional fps. This is ONLY needed for
	// deluxemaps, tangentspace deluxemap avoid this problem by design.
	lightcolor *= 1.0 / max(0.25, lightnormal.z);
#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE
#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE
   #define SHADING
	// deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)
	myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0);
	myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw));
#endif
#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR)
	#define SHADING
	// forced deluxemap on lightmapped/vertexlit surfaces
	myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0);
   #ifdef USELIGHTMAP
		myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw));
   #else
		myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb);
   #endif
#endif
#ifdef MODE_FAKELIGHT
	#define SHADING
	myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz));
	#define lightcolor 1
#endif // MODE_FAKELIGHT




#ifdef MODE_LIGHTMAP
	color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse);
#endif // MODE_LIGHTMAP
#ifdef MODE_VERTEXCOLOR
	color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse);
#endif // MODE_VERTEXCOLOR
#ifdef MODE_FLATCOLOR
	color.rgb = diffusetex * Color_Ambient;
#endif // MODE_FLATCOLOR




#ifdef SHADING
# ifdef USEDIFFUSE
SHADEDIFFUSE
#  ifdef USESPECULAR
SHADESPECULAR(SpecularPower * glosstex.a)
	color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor;
#  else
	color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor);
#  endif
# else
	color.rgb = diffusetex * Color_Ambient;
# endif
#endif

#ifdef USESHADOWMAPORTHO
	color.rgb *= ShadowMapCompare(ShadowMapTC);
#endif

#ifdef USEDEFERREDLIGHTMAP
	vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;
	color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse;
	color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular;
//	color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001);
#endif

#ifdef USEBOUNCEGRID
#ifdef USEBOUNCEGRIDDIRECTIONAL
//	myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord                        ));
//	myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0);
	myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250)));
	myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375)));
	myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500)));
	myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625)));
	myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750)));
	myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875)));
	myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz));
	myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir);
	myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir);
//	bouncegrid_dirp  = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0);
	myhalf3 bouncegrid_light = cast_myhalf3(
		dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn),
		dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn),
		dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn));
	color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity;
//	color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5);
#else
	color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity;
#endif
#endif

#ifdef USEGLOW
#ifdef USEVERTEXTEXTUREBLEND
	color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow;
#else
	color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow;
#endif
#endif

#ifdef USECELOUTLINES
# ifdef USEDEFERREDLIGHTMAP
//	vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;
	vec4 ScreenTexCoordStep = vec4(PixelToScreenTexCoord.x, 0.0, 0.0, PixelToScreenTexCoord.y);
	vec4 DepthNeighbors;

	// enable to test ink on white geometry
//	color.rgb = vec3(1.0, 1.0, 1.0);

	// note: this seems to be negative
	float DepthCenter = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).b;

	// edge detect method
//	DepthNeighbors.x = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord - ScreenTexCoordStep.xy).b;
//	DepthNeighbors.y = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + ScreenTexCoordStep.xy).b;
//	DepthNeighbors.z = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + ScreenTexCoordStep.zw).b;
//	DepthNeighbors.w = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord - ScreenTexCoordStep.zw).b;
//	float DepthAverage = dot(DepthNeighbors, vec4(0.25, 0.25, 0.25, 0.25));
//	float DepthDelta = abs(dot(DepthNeighbors.xy, vec2(-1.0, 1.0))) + abs(dot(DepthNeighbors.zw, vec2(-1.0, 1.0)));
//	color.rgb *= max(0.5, 1.0 - max(0.0, abs(DepthCenter - DepthAverage) - 0.2 * DepthDelta) / (0.01 + 0.2 * DepthDelta));
//	color.rgb *= step(abs(DepthCenter - DepthAverage), 0.2 * DepthDelta); 

	// shadow method
	float DepthScale1 = 4.0 / DepthCenter; // inner ink (shadow on object)
//	float DepthScale1 = -4.0 / DepthCenter; // outer ink (shadow around object)
//	float DepthScale1 = 0.003;
	float DepthScale2 = DepthScale1 / 2.0;
//	float DepthScale3 = DepthScale1 / 4.0;
	float DepthBias1 = -DepthCenter * DepthScale1;
	float DepthBias2 = -DepthCenter * DepthScale2;
//	float DepthBias3 = -DepthCenter * DepthScale3;
	float DepthShadow = max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2(-1.0,  0.0)).b * DepthScale1 + DepthBias1)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 1.0,  0.0)).b * DepthScale1 + DepthBias1)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0, -1.0)).b * DepthScale1 + DepthBias1)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0,  1.0)).b * DepthScale1 + DepthBias1)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2(-2.0,  0.0)).b * DepthScale2 + DepthBias2)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 2.0,  0.0)).b * DepthScale2 + DepthBias2)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0, -2.0)).b * DepthScale2 + DepthBias2)
	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0,  2.0)).b * DepthScale2 + DepthBias2)
//	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2(-3.0,  0.0)).b * DepthScale3 + DepthBias3)
//	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 3.0,  0.0)).b * DepthScale3 + DepthBias3)
//	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0, -3.0)).b * DepthScale3 + DepthBias3)
//	                  + max(0.0, dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord + PixelToScreenTexCoord * vec2( 0.0,  3.0)).b * DepthScale3 + DepthBias3)
	                  - 0.0;
	color.rgb *= 1.0 - max(0.0, min(DepthShadow, 1.0));
//	color.r = DepthCenter / -1024.0;
# endif
#endif

#ifdef USEFOG
	color.rgb = FogVertex(color);
#endif

	// 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
#ifdef USEREFLECTION
	vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);
	//vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;
	vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;
	#ifdef USENORMALMAPSCROLLBLEND
# ifdef USEOFFSETMAPPING
		vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0);
# else
		vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);
# endif
		normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;
		vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw;
	#else
		vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw;
	#endif
	// FIXME temporary hack to detect the case that the reflection
	// gets blackened at edges due to leaving the area that contains actual
	// content.
	// Remove this 'ack once we have a better way to stop this thing from
	// 'appening.
	float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);
	f      *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);
	f      *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);
	f      *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);
	ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);
	color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a);
#endif
#ifdef USEOCCLUDE
   color.rgb *= clamp(float(visiblepixels) / float(allpixels), 0.0, 1.0);
#endif

	dp_FragColor = vec4(color);
}
#endif // FRAGMENT_SHADER

#endif // !MODE_DEFERREDLIGHTSOURCE
#endif // !MODE_DEFERREDGEOMETRY
#endif // !MODE_WATER
#endif // !MODE_REFRACTION
#endif // !MODE_BLOOMBLUR
#endif // !MODE_GENERIC
#endif // !MODE_POSTPROCESS
#endif // !MODE_DEPTH_OR_SHADOW