Dung.Dang@gmail.com | My favorites | Profile | Sign out
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
 
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
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
Part of the Processing project - http://processing.org

Copyright (c) 2004-08 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/

package processing.core;

import java.awt.Toolkit;
import java.awt.image.*;
import java.util.*;

/**
* Subclass of PGraphics that handles 3D rendering.
* It can render 3D inside a browser window and requires no plug-ins.
* <p/>
* The renderer is mostly set up based on the structure of the OpenGL API,
* if you have questions about specifics that aren't covered here,
* look for reference on the OpenGL implementation of a similar feature.
* <p/>
* Lighting and camera implementation by Simon Greenwold.
*/
public class PGraphics3D extends PGraphics {

/** The depth buffer. */
public float[] zbuffer;

// ........................................................

/** The modelview matrix. */
public PMatrix3D modelview;

/** Inverse modelview matrix, used for lighting. */
public PMatrix3D modelviewInv;

/**
* Marks when changes to the size have occurred, so that the camera
* will be reset in beginDraw().
*/
protected boolean sizeChanged;

/** The camera matrix, the modelview will be set to this on beginDraw. */
public PMatrix3D camera;

/** Inverse camera matrix */
protected PMatrix3D cameraInv;

/** Camera field of view. */
public float cameraFOV;

/** Position of the camera. */
public float cameraX, cameraY, cameraZ;
public float cameraNear, cameraFar;
/** Aspect ratio of camera's view. */
public float cameraAspect;

/** Current projection matrix. */
public PMatrix3D projection;


//////////////////////////////////////////////////////////////


/**
* Maximum lights by default is 8, which is arbitrary for this renderer,
* but is the minimum defined by OpenGL
*/
public static final int MAX_LIGHTS = 8;

public int lightCount = 0;

/** Light types */
public int[] lightType;

/** Light positions */
//public float[][] lightPosition;
public PVector[] lightPosition;

/** Light direction (normalized vector) */
//public float[][] lightNormal;
public PVector[] lightNormal;

/** Light falloff */
public float[] lightFalloffConstant;
public float[] lightFalloffLinear;
public float[] lightFalloffQuadratic;

/** Light spot angle */
public float[] lightSpotAngle;

/** Cosine of light spot angle */
public float[] lightSpotAngleCos;

/** Light spot concentration */
public float[] lightSpotConcentration;

/** Diffuse colors for lights.
* For an ambient light, this will hold the ambient color.
* Internally these are stored as numbers between 0 and 1. */
public float[][] lightDiffuse;

/** Specular colors for lights.
Internally these are stored as numbers between 0 and 1. */
public float[][] lightSpecular;

/** Current specular color for lighting */
public float[] currentLightSpecular;

/** Current light falloff */
public float currentLightFalloffConstant;
public float currentLightFalloffLinear;
public float currentLightFalloffQuadratic;


//////////////////////////////////////////////////////////////


static public final int TRI_DIFFUSE_R = 0;
static public final int TRI_DIFFUSE_G = 1;
static public final int TRI_DIFFUSE_B = 2;
static public final int TRI_DIFFUSE_A = 3;
static public final int TRI_SPECULAR_R = 4;
static public final int TRI_SPECULAR_G = 5;
static public final int TRI_SPECULAR_B = 6;
static public final int TRI_COLOR_COUNT = 7;

// ........................................................

// Whether or not we have to worry about vertex position for lighting calcs
private boolean lightingDependsOnVertexPosition;

static final int LIGHT_AMBIENT_R = 0;
static final int LIGHT_AMBIENT_G = 1;
static final int LIGHT_AMBIENT_B = 2;
static final int LIGHT_DIFFUSE_R = 3;
static final int LIGHT_DIFFUSE_G = 4;
static final int LIGHT_DIFFUSE_B = 5;
static final int LIGHT_SPECULAR_R = 6;
static final int LIGHT_SPECULAR_G = 7;
static final int LIGHT_SPECULAR_B = 8;
static final int LIGHT_COLOR_COUNT = 9;

// Used to shuttle lighting calcs around
// (no need to re-allocate all the time)
protected float[] tempLightingContribution = new float[LIGHT_COLOR_COUNT];
// protected float[] worldNormal = new float[4];

/// Used in lightTriangle(). Allocated here once to avoid re-allocating
protected PVector lightTriangleNorm = new PVector();

// ........................................................

/**
* This is turned on at beginCamera, and off at endCamera
* Currently we don't support nested begin/end cameras.
* If we wanted to, this variable would have to become a stack.
*/
protected boolean manipulatingCamera;

float[][] matrixStack = new float[MATRIX_STACK_DEPTH][16];
float[][] matrixInvStack = new float[MATRIX_STACK_DEPTH][16];
int matrixStackDepth;

protected int matrixMode = MODELVIEW;
float[][] pmatrixStack = new float[MATRIX_STACK_DEPTH][16];
int pmatrixStackDepth;

// These two matrices always point to either the modelview
// or the modelviewInv, but they are swapped during
// when in camera manipulation mode. That way camera transforms
// are automatically accumulated in inverse on the modelview matrix.
protected PMatrix3D forwardTransform;
protected PMatrix3D reverseTransform;

// Added by ewjordan for accurate texturing purposes. Screen plane is
// not scaled to pixel-size, so these manually keep track of its size
// from frustum() calls. Sorry to add public vars, is there a way
// to compute these from something publicly available without matrix ops?
// (used once per triangle in PTriangle with ENABLE_ACCURATE_TEXTURES)
protected float leftScreen;
protected float rightScreen;
protected float topScreen;
protected float bottomScreen;
protected float nearPlane; //depth of near clipping plane

/** true if frustum has been called to set perspective, false if ortho */
private boolean frustumMode = false;

/**
* Use PSmoothTriangle for rendering instead of PTriangle?
* Usually set by calling smooth() or noSmooth()
*/
static protected boolean s_enableAccurateTextures = false; //maybe just use smooth instead?

/** Used for anti-aliased and perspective corrected rendering. */
public PSmoothTriangle smoothTriangle;


// ........................................................

// pos of first vertex of current shape in vertices array
protected int shapeFirst;

// i think vertex_end is actually the last vertex in the current shape
// and is separate from vertexCount for occasions where drawing happens
// on endDraw() with all the triangles being depth sorted
protected int shapeLast;

// vertices may be added during clipping against the near plane.
protected int shapeLastPlusClipped;

// used for sorting points when triangulating a polygon
// warning - maximum number of vertices for a polygon is DEFAULT_VERTICES
protected int vertexOrder[] = new int[DEFAULT_VERTICES];

// ........................................................

// This is done to keep track of start/stop information for lines in the
// line array, so that lines can be shown as a single path, rather than just
// individual segments. Currently only in use inside PGraphicsOpenGL.
protected int pathCount;
protected int[] pathOffset = new int[64];
protected int[] pathLength = new int[64];

// ........................................................

// line & triangle fields (note that these overlap)
// static protected final int INDEX = 0; // shape index
static protected final int VERTEX1 = 0;
static protected final int VERTEX2 = 1;
static protected final int VERTEX3 = 2; // (triangles only)
/** used to store the strokeColor int for efficient drawing. */
static protected final int STROKE_COLOR = 1; // (points only)
static protected final int TEXTURE_INDEX = 3; // (triangles only)
//static protected final int STROKE_MODE = 2; // (lines only)
//static protected final int STROKE_WEIGHT = 3; // (lines only)

static protected final int POINT_FIELD_COUNT = 2; //4
static protected final int LINE_FIELD_COUNT = 2; //4
static protected final int TRIANGLE_FIELD_COUNT = 4;

// points
static final int DEFAULT_POINTS = 512;
protected int[][] points = new int[DEFAULT_POINTS][POINT_FIELD_COUNT];
protected int pointCount;

// lines
static final int DEFAULT_LINES = 512;
public PLine line; // used for drawing
protected int[][] lines = new int[DEFAULT_LINES][LINE_FIELD_COUNT];
protected int lineCount;

// triangles
static final int DEFAULT_TRIANGLES = 256;
public PTriangle triangle;
protected int[][] triangles =
new int[DEFAULT_TRIANGLES][TRIANGLE_FIELD_COUNT];
protected float triangleColors[][][] =
new float[DEFAULT_TRIANGLES][3][TRI_COLOR_COUNT];
protected int triangleCount; // total number of triangles

// cheap picking someday
//public int shape_index;

// ........................................................

static final int DEFAULT_TEXTURES = 3;
protected PImage[] textures = new PImage[DEFAULT_TEXTURES];
int textureIndex;

// ........................................................

DirectColorModel cm;
MemoryImageSource mis;


//////////////////////////////////////////////////////////////


public PGraphics3D() { }


//public void setParent(PApplet parent)


//public void setPrimary(boolean primary)


//public void setPath(String path)


/**
* Called in response to a resize event, handles setting the
* new width and height internally, as well as re-allocating
* the pixel buffer for the new size.
*
* Note that this will nuke any cameraMode() settings.
*
* No drawing can happen in this function, and no talking to the graphics
* context. That is, no glXxxx() calls, or other things that change state.
*/
public void setSize(int iwidth, int iheight) { // ignore
width = iwidth;
height = iheight;
width1 = width - 1;
height1 = height - 1;

allocate();
reapplySettings();

// init lights (in resize() instead of allocate() b/c needed by opengl)
lightType = new int[MAX_LIGHTS];
lightPosition = new PVector[MAX_LIGHTS];
lightNormal = new PVector[MAX_LIGHTS];
for (int i = 0; i < MAX_LIGHTS; i++) {
lightPosition[i] = new PVector();
lightNormal[i] = new PVector();
}
lightDiffuse = new float[MAX_LIGHTS][3];
lightSpecular = new float[MAX_LIGHTS][3];
lightFalloffConstant = new float[MAX_LIGHTS];
lightFalloffLinear = new float[MAX_LIGHTS];
lightFalloffQuadratic = new float[MAX_LIGHTS];
lightSpotAngle = new float[MAX_LIGHTS];
lightSpotAngleCos = new float[MAX_LIGHTS];
lightSpotConcentration = new float[MAX_LIGHTS];
currentLightSpecular = new float[3];

projection = new PMatrix3D();
modelview = new PMatrix3D();
modelviewInv = new PMatrix3D();

// modelviewStack = new float[MATRIX_STACK_DEPTH][16];
// modelviewInvStack = new float[MATRIX_STACK_DEPTH][16];
// modelviewStackPointer = 0;

forwardTransform = modelview;
reverseTransform = modelviewInv;

// init perspective projection based on new dimensions
cameraFOV = 60 * DEG_TO_RAD; // at least for now
cameraX = width / 2.0f;
cameraY = height / 2.0f;
cameraZ = cameraY / ((float) Math.tan(cameraFOV / 2.0f));
cameraNear = cameraZ / 10.0f;
cameraFar = cameraZ * 10.0f;
cameraAspect = (float)width / (float)height;

camera = new PMatrix3D();
cameraInv = new PMatrix3D();

// set this flag so that beginDraw() will do an update to the camera.
sizeChanged = true;
}


protected void allocate() {
//System.out.println(this + " allocating for " + width + " " + height);
//new Exception().printStackTrace();

pixelCount = width * height;
pixels = new int[pixelCount];
zbuffer = new float[pixelCount];

if (primarySurface) {
cm = new DirectColorModel(32, 0x00ff0000, 0x0000ff00, 0x000000ff);;
mis = new MemoryImageSource(width, height, pixels, 0, width);
mis.setFullBufferUpdates(true);
mis.setAnimated(true);
image = Toolkit.getDefaultToolkit().createImage(mis);

} else {
// when not the main drawing surface, need to set the zbuffer,
// because there's a possibility that background() will not be called
Arrays.fill(zbuffer, Float.MAX_VALUE);
}

line = new PLine(this);
triangle = new PTriangle(this);
smoothTriangle = new PSmoothTriangle(this);
}


//public void dispose()


////////////////////////////////////////////////////////////


//public boolean canDraw()


public void beginDraw() {
// need to call defaults(), but can only be done when it's ok
// to draw (i.e. for opengl, no drawing can be done outside
// beginDraw/endDraw).
if (!settingsInited) defaultSettings();

if (sizeChanged) {
// set up the default camera
camera();

// defaults to perspective, if the user has setup up their
// own projection, they'll need to fix it after resize anyway.
// this helps the people who haven't set up their own projection.
perspective();

// clear the flag
sizeChanged = false;
}

resetMatrix(); // reset model matrix

// reset vertices
vertexCount = 0;

modelview.set(camera);
modelviewInv.set(cameraInv);

// clear out the lights, they'll have to be turned on again
lightCount = 0;
lightingDependsOnVertexPosition = false;
lightFalloff(1, 0, 0);
lightSpecular(0, 0, 0);

/*
// reset lines
lineCount = 0;
if (line != null) line.reset(); // is this necessary?
pathCount = 0;

// reset triangles
triangleCount = 0;
if (triangle != null) triangle.reset(); // necessary?
*/

shapeFirst = 0;

// reset textures
Arrays.fill(textures, null);
textureIndex = 0;

normal(0, 0, 1);
}


/**
* See notes in PGraphics.
* If z-sorting has been turned on, then the triangles will
* all be quicksorted here (to make alpha work more properly)
* and then blit to the screen.
*/
public void endDraw() {
// no need to z order and render
// shapes were already rendered in endShape();
// (but can't return, since needs to update memimgsrc)
if (hints[ENABLE_DEPTH_SORT]) {
flush();
}
if (mis != null) {
mis.newPixels(pixels, cm, 0, width);
}
// mark pixels as having been updated, so that they'll work properly
// when this PGraphics is drawn using image().
updatePixels();
}


////////////////////////////////////////////////////////////


//protected void checkSettings()


protected void defaultSettings() {
super.defaultSettings();

manipulatingCamera = false;
forwardTransform = modelview;
reverseTransform = modelviewInv;

// set up the default camera
camera();

// defaults to perspective, if the user has setup up their
// own projection, they'll need to fix it after resize anyway.
// this helps the people who haven't set up their own projection.
perspective();

// easiest for beginners
textureMode(IMAGE);

emissive(0.0f);
specular(0.5f);
shininess(1.0f);
}


//protected void reapplySettings()


////////////////////////////////////////////////////////////


public void hint(int which) {
if (which == DISABLE_DEPTH_SORT) {
flush();
} else if (which == DISABLE_DEPTH_TEST) {
if (zbuffer != null) { // will be null in OpenGL and others
Arrays.fill(zbuffer, Float.MAX_VALUE);
}
}
super.hint(which);
}


//////////////////////////////////////////////////////////////


//public void beginShape()


public void beginShape(int kind) {
shape = kind;

// shape_index = shape_index + 1;
// if (shape_index == -1) {
// shape_index = 0;
// }

if (hints[ENABLE_DEPTH_SORT]) {
// continue with previous vertex, line and triangle count
// all shapes are rendered at endDraw();
shapeFirst = vertexCount;
shapeLast = 0;

} else {
// reset vertex, line and triangle information
// every shape is rendered at endShape();
vertexCount = 0;
if (line != null) line.reset(); // necessary?
lineCount = 0;
// pathCount = 0;
if (triangle != null) triangle.reset(); // necessary?
triangleCount = 0;
}

textureImage = null;
curveVertexCount = 0;
normalMode = NORMAL_MODE_AUTO;
// normalCount = 0;
}


//public void normal(float nx, float ny, float nz)


//public void textureMode(int mode)


public void texture(PImage image) {
textureImage = image;

if (textureIndex == textures.length - 1) {
textures = (PImage[]) PApplet.expand(textures);
}
if (textures[textureIndex] != null) { // ???
textureIndex++;
}
textures[textureIndex] = image;
}


public void vertex(float x, float y) {
// override so that the default 3D implementation will be used,
// which will pick up all 3D settings (e.g. emissive, ambient)
vertex(x, y, 0);
}


//public void vertex(float x, float y, float z)


public void vertex(float x, float y, float u, float v) {
// see vertex(x, y) for note
vertex(x, y, 0, u, v);
}


//public void vertex(float x, float y, float z, float u, float v)


//public void breakShape()


//public void endShape()


public void endShape(int mode) {
shapeLast = vertexCount;
shapeLastPlusClipped = shapeLast;

// don't try to draw if there are no vertices
// (fixes a bug in LINE_LOOP that re-adds a nonexistent vertex)
if (vertexCount == 0) {
shape = 0;
return;
}

// convert points from model (X/Y/Z) to camera space (VX/VY/VZ).
// Do this now because we will be clipping them on add_triangle.
endShapeModelToCamera(shapeFirst, shapeLast);

if (stroke) {
endShapeStroke(mode);
}

if (fill || textureImage != null) {
endShapeFill();
}

// transform, light, and clip
endShapeLighting(lightCount > 0 && fill);

// convert points from camera space (VX, VY, VZ) to screen space (X, Y, Z)
// (this appears to be wasted time with the OpenGL renderer)
endShapeCameraToScreen(shapeFirst, shapeLastPlusClipped);

// render shape and fill here if not saving the shapes for later
// if true, the shapes will be rendered on endDraw
if (!hints[ENABLE_DEPTH_SORT]) {
if (fill || textureImage != null) {
if (triangleCount > 0) {
renderTriangles(0, triangleCount);
if (raw != null) {
rawTriangles(0, triangleCount);
}
triangleCount = 0;
}
}
if (stroke) {
if (pointCount > 0) {
renderPoints(0, pointCount);
if (raw != null) {
rawPoints(0, pointCount);
}
pointCount = 0;
}

if (lineCount > 0) {
renderLines(0, lineCount);
if (raw != null) {
rawLines(0, lineCount);
}
lineCount = 0;
}
}
pathCount = 0;
}

shape = 0;
}


protected void endShapeModelToCamera(int start, int stop) {
for (int i = start; i < stop; i++) {
float vertex[] = vertices[i];

vertex[VX] =
modelview.m00*vertex[X] + modelview.m01*vertex[Y] +
modelview.m02*vertex[Z] + modelview.m03;
vertex[VY] =
modelview.m10*vertex[X] + modelview.m11*vertex[Y] +
modelview.m12*vertex[Z] + modelview.m13;
vertex[VZ] =
modelview.m20*vertex[X] + modelview.m21*vertex[Y] +
modelview.m22*vertex[Z] + modelview.m23;
vertex[VW] =
modelview.m30*vertex[X] + modelview.m31*vertex[Y] +
modelview.m32*vertex[Z] + modelview.m33;

// normalize
if (vertex[VW] != 0 && vertex[VW] != 1) {
vertex[VX] /= vertex[VW];
vertex[VY] /= vertex[VW];
vertex[VZ] /= vertex[VW];
}
vertex[VW] = 1;
}
}


protected void endShapeStroke(int mode) {
switch (shape) {
case POINTS:
{
int stop = shapeLast;
for (int i = shapeFirst; i < stop; i++) {
// if (strokeWeight == 1) {
addPoint(i);
// } else {
// addLineBreak(); // total overkill for points
// addLine(i, i);
// }
}
}
break;

case LINES:
{
// store index of first vertex
int first = lineCount;
int stop = shapeLast - 1;
//increment = (shape == LINES) ? 2 : 1;

// for LINE_STRIP and LINE_LOOP, make this all one path
if (shape != LINES) addLineBreak();

for (int i = shapeFirst; i < stop; i += 2) {
// for LINES, make a new path for each segment
if (shape == LINES) addLineBreak();
addLine(i, i+1);
}

// for LINE_LOOP, close the loop with a final segment
//if (shape == LINE_LOOP) {
if (mode == CLOSE) {
addLine(stop, lines[first][VERTEX1]);
}
}
break;

case TRIANGLES:
{
for (int i = shapeFirst; i < shapeLast-2; i += 3) {
addLineBreak();
//counter = i - vertex_start;
addLine(i+0, i+1);
addLine(i+1, i+2);
addLine(i+2, i+0);
}
}
break;

case TRIANGLE_STRIP:
{
// first draw all vertices as a line strip
int stop = shapeLast-1;

addLineBreak();
for (int i = shapeFirst; i < stop; i++) {
//counter = i - vertex_start;
addLine(i, i+1);
}

// then draw from vertex (n) to (n+2)
stop = shapeLast-2;
for (int i = shapeFirst; i < stop; i++) {
addLineBreak();
addLine(i, i+2);
}
}
break;

case TRIANGLE_FAN:
{
// this just draws a series of line segments
// from the center to each exterior point
for (int i = shapeFirst + 1; i < shapeLast; i++) {
addLineBreak();
addLine(shapeFirst, i);
}

// then a single line loop around the outside.
addLineBreak();
for (int i = shapeFirst + 1; i < shapeLast-1; i++) {
addLine(i, i+1);
}
// closing the loop
addLine(shapeLast-1, shapeFirst + 1);
}
break;

case QUADS:
{
for (int i = shapeFirst; i < shapeLast; i += 4) {
addLineBreak();
//counter = i - vertex_start;
addLine(i+0, i+1);
addLine(i+1, i+2);
addLine(i+2, i+3);
addLine(i+3, i+0);
}
}
break;

case QUAD_STRIP:
{
for (int i = shapeFirst; i < shapeLast - 3; i += 2) {
addLineBreak();
addLine(i+0, i+2);
addLine(i+2, i+3);
addLine(i+3, i+1);
addLine(i+1, i+0);
}
}
break;

case POLYGON:
{
// store index of first vertex
int stop = shapeLast - 1;

addLineBreak();
for (int i = shapeFirst; i < stop; i++) {
addLine(i, i+1);
}
if (mode == CLOSE) {
// draw the last line connecting back to the first point in poly
addLine(stop, shapeFirst); //lines[first][VERTEX1]);
}
}
break;
}
}


protected void endShapeFill() {
switch (shape) {
case TRIANGLE_FAN:
{
int stop = shapeLast - 1;
for (int i = shapeFirst + 1; i < stop; i++) {
addTriangle(shapeFirst, i, i+1);
}
}
break;

case TRIANGLES:
{
int stop = shapeLast - 2;
for (int i = shapeFirst; i < stop; i += 3) {
// have to switch between clockwise/counter-clockwise
// otherwise the feller is backwards and renderer won't draw
if ((i % 2) == 0) {
addTriangle(i, i+2, i+1);
} else {
addTriangle(i, i+1, i+2);
}
}
}
break;

case TRIANGLE_STRIP:
{
int stop = shapeLast - 2;
for (int i = shapeFirst; i < stop; i++) {
// have to switch between clockwise/counter-clockwise
// otherwise the feller is backwards and renderer won't draw
if ((i % 2) == 0) {
addTriangle(i, i+2, i+1);
} else {
addTriangle(i, i+1, i+2);
}
}
}
break;

case QUADS:
{
int stop = vertexCount-3;
for (int i = shapeFirst; i < stop; i += 4) {
// first triangle
addTriangle(i, i+1, i+2);
// second triangle
addTriangle(i, i+2, i+3);
}
}
break;

case QUAD_STRIP:
{
int stop = vertexCount-3;
for (int i = shapeFirst; i < stop; i += 2) {
// first triangle
addTriangle(i+0, i+2, i+1);
// second triangle
addTriangle(i+2, i+3, i+1);
}
}
break;

case POLYGON:
{
addPolygonTriangles();
}
break;
}
}


protected void endShapeLighting(boolean lights) {
if (lights) {
// If the lighting does not depend on vertex position and there is a single
// normal specified for this shape, go ahead and apply the same lighting
// contribution to every vertex in this shape (one lighting calc!)
if (!lightingDependsOnVertexPosition && normalMode == NORMAL_MODE_SHAPE) {
calcLightingContribution(shapeFirst, tempLightingContribution);
for (int tri = 0; tri < triangleCount; tri++) {
lightTriangle(tri, tempLightingContribution);
}
} else { // Otherwise light each triangle individually...
for (int tri = 0; tri < triangleCount; tri++) {
lightTriangle(tri);
}
}
} else {
for (int tri = 0; tri < triangleCount; tri++) {
int index = triangles[tri][VERTEX1];
copyPrelitVertexColor(tri, index, 0);
index = triangles[tri][VERTEX2];
copyPrelitVertexColor(tri, index, 1);
index = triangles[tri][VERTEX3];
copyPrelitVertexColor(tri, index, 2);
}
}
}


protected void endShapeCameraToScreen(int start, int stop) {
for (int i = start; i < stop; i++) {
float vx[] = vertices[i];

float ox =
projection.m00*vx[VX] + projection.m01*vx[VY] +
projection.m02*vx[VZ] + projection.m03*vx[VW];
float oy =
projection.m10*vx[VX] + projection.m11*vx[VY] +
projection.m12*vx[VZ] + projection.m13*vx[VW];
float oz =
projection.m20*vx[VX] + projection.m21*vx[VY] +
projection.m22*vx[VZ] + projection.m23*vx[VW];
float ow =
projection.m30*vx[VX] + projection.m31*vx[VY] +
projection.m32*vx[VZ] + projection.m33*vx[VW];

if (ow != 0 && ow != 1) {
ox /= ow; oy /= ow; oz /= ow;
}

vx[TX] = width * (1 + ox) / 2.0f;
vx[TY] = height * (1 + oy) / 2.0f;
vx[TZ] = (oz + 1) / 2.0f;
}
}



/////////////////////////////////////////////////////////////////////////////

// POINTS


protected void addPoint(int a) {
if (pointCount == points.length) {
int[][] temp = new int[pointCount << 1][LINE_FIELD_COUNT];
System.arraycopy(points, 0, temp, 0, pointCount);
points = temp;
}
points[pointCount][VERTEX1] = a;
//points[pointCount][STROKE_MODE] = strokeCap | strokeJoin;
points[pointCount][STROKE_COLOR] = strokeColor;
//points[pointCount][STROKE_WEIGHT] = (int) (strokeWeight + 0.5f); // hmm
pointCount++;
}


protected void renderPoints(int start, int stop) {
if (strokeWeight != 1) {
for (int i = start; i < stop; i++) {
float[] a = vertices[points[i][VERTEX1]];
renderLineVertices(a, a);
}
} else {
for (int i = start; i < stop; i++) {
float[] a = vertices[points[i][VERTEX1]];
int sx = (int) (a[TX] + 0.4999f);
int sy = (int) (a[TY] + 0.4999f);
if (sx >= 0 && sx < width && sy >= 0 && sy < height) {
int index = sy*width + sx;
pixels[index] = points[i][STROKE_COLOR];
zbuffer[index] = a[TZ];
}
}
}
}


// alternative implementations of point rendering code...

/*
int sx = (int) (screenX(x, y, z) + 0.5f);
int sy = (int) (screenY(x, y, z) + 0.5f);

int index = sy*width + sx;
pixels[index] = strokeColor;
zbuffer[index] = screenZ(x, y, z);

*/

/*
protected void renderPoints(int start, int stop) {
for (int i = start; i < stop; i++) {
float a[] = vertices[points[i][VERTEX1]];

line.reset();

line.setIntensities(a[SR], a[SG], a[SB], a[SA],
a[SR], a[SG], a[SB], a[SA]);

line.setVertices(a[TX], a[TY], a[TZ],
a[TX] + 0.5f, a[TY] + 0.5f, a[TZ] + 0.5f);

line.draw();
}
}
*/

/*
// handle points with an actual stroke weight (or scaled by renderer)
private void point3(float x, float y, float z, int color) {
// need to get scaled version of the stroke
float x1 = screenX(x - 0.5f, y - 0.5f, z);
float y1 = screenY(x - 0.5f, y - 0.5f, z);
float x2 = screenX(x + 0.5f, y + 0.5f, z);
float y2 = screenY(x + 0.5f, y + 0.5f, z);

float weight = (abs(x2 - x1) + abs(y2 - y1)) / 2f;
if (weight < 1.5f) {
int xx = (int) ((x1 + x2) / 2f);
int yy = (int) ((y1 + y2) / 2f);
//point0(xx, yy, z, color);
zbuffer[yy*width + xx] = screenZ(x, y, z);
//stencil?

} else {
// actually has some weight, need to draw shapes instead
// these will be
}
}
*/


protected void rawPoints(int start, int stop) {
raw.colorMode(RGB, 1);
raw.noFill();
raw.strokeWeight(vertices[lines[start][VERTEX1]][SW]);
raw.beginShape(POINTS);

for (int i = start; i < stop; i++) {
float a[] = vertices[lines[i][VERTEX1]];

if (raw.is3D()) {
if (a[VW] != 0) {
raw.stroke(a[SR], a[SG], a[SB], a[SA]);
raw.vertex(a[VX] / a[VW], a[VY] / a[VW], a[VZ] / a[VW]);
}
} else { // if is2D()
raw.stroke(a[SR], a[SG], a[SB], a[SA]);
raw.vertex(a[TX], a[TY]);
}
}
raw.endShape();
}



/////////////////////////////////////////////////////////////////////////////

// LINES


/**
* Begin a new section of stroked geometry.
*/
protected final void addLineBreak() {
if (pathCount == pathOffset.length) {
pathOffset = PApplet.expand(pathOffset);
pathLength = PApplet.expand(pathLength);
}
pathOffset[pathCount] = lineCount;
pathLength[pathCount] = 0;
pathCount++;
}


protected void addLine(int a, int b) {
addLineWithClip(a, b);
}


protected final void addLineWithClip(int a, int b) {
float az = vertices[a][VZ];
float bz = vertices[b][VZ];
if (az > cameraNear) {
if (bz > cameraNear) {
return;
}
int cb = interpolateClipVertex(a, b);
addLineWithoutClip(cb, b);
return;
}
else {
if (bz <= cameraNear) {
addLineWithoutClip(a, b);
return;
}
int cb = interpolateClipVertex(a, b);
addLineWithoutClip(a, cb);
return;
}
}


protected final void addLineWithoutClip(int a, int b) {
if (lineCount == lines.length) {
int temp[][] = new int[lineCount<<1][LINE_FIELD_COUNT];
System.arraycopy(lines, 0, temp, 0, lineCount);
lines = temp;
}
lines[lineCount][VERTEX1] = a;
lines[lineCount][VERTEX2] = b;

//lines[lineCount][STROKE_MODE] = strokeCap | strokeJoin;
//lines[lineCount][STROKE_WEIGHT] = (int) (strokeWeight + 0.5f); // hmm
lineCount++;

// mark this piece as being part of the current path
pathLength[pathCount-1]++;
}


protected void renderLines(int start, int stop) {
for (int i = start; i < stop; i++) {
renderLineVertices(vertices[lines[i][VERTEX1]],
vertices[lines[i][VERTEX2]]);
}
}


protected void renderLineVertices(float[] a, float[] b) {
// 2D hack added by ewjordan 6/13/07
// Offset coordinates by a little bit if drawing 2D graphics.
// http://dev.processing.org/bugs/show_bug.cgi?id=95

// This hack fixes a bug caused by numerical precision issues when
// applying the 3D transformations to coordinates in the screen plane
// that should actually not be altered under said transformations.
// It will not be applied if any transformations other than translations
// are active, nor should it apply in OpenGL mode (PGraphicsOpenGL
// overrides render_lines(), so this should be fine).
// This fix exposes a last-pixel bug in the lineClipCode() function
// of PLine.java, so that fix must remain in place if this one is used.

// Note: the "true" fix for this bug is to change the pixel coverage
// model so that the threshold for display does not lie on an integer
// boundary. Search "diamond exit rule" for info the OpenGL approach.

/*
// removing for 0149 with the return of P2D
if (drawing2D() && a[Z] == 0) {
a[TX] += 0.01;
a[TY] += 0.01;
a[VX] += 0.01*a[VW];
a[VY] += 0.01*a[VW];
b[TX] += 0.01;
b[TY] += 0.01;
b[VX] += 0.01*b[VW];
b[VY] += 0.01*b[VW];
}
*/
// end 2d-hack

if (a[SW] > 1.25f || a[SW] < 0.75f) {
float ox1 = a[TX];
float oy1 = a[TY];
float ox2 = b[TX];
float oy2 = b[TY];

// TODO strokeWeight should be transformed!
float weight = a[SW] / 2;

// when drawing points with stroke weight, need to extend a bit
if (ox1 == ox2 && oy1 == oy2) {
oy1 -= weight;
oy2 += weight;
}

float dX = ox2 - ox1 + EPSILON;
float dY = oy2 - oy1 + EPSILON;
float len = (float) Math.sqrt(dX*dX + dY*dY);

float rh = weight / len;

float dx0 = rh * dY;
float dy0 = rh * dX;
float dx1 = rh * dY;
float dy1 = rh * dX;

float ax1 = ox1+dx0;
float ay1 = oy1-dy0;

float ax2 = ox1-dx0;
float ay2 = oy1+dy0;

float bx1 = ox2+dx1;
float by1 = oy2-dy1;

float bx2 = ox2-dx1;
float by2 = oy2+dy1;

if (smooth) {
smoothTriangle.reset(3);
smoothTriangle.smooth = true;
smoothTriangle.interpARGB = true; // ?

// render first triangle for thick line
smoothTriangle.setVertices(ax1, ay1, a[TZ],
bx2, by2, b[TZ],
ax2, ay2, a[TZ]);
smoothTriangle.setIntensities(a[SR], a[SG], a[SB], a[SA],
b[SR], b[SG], b[SB], b[SA],
a[SR], a[SG], a[SB], a[SA]);
smoothTriangle.render();

// render second triangle for thick line
smoothTriangle.setVertices(ax1, ay1, a[TZ],
bx2, by2, b[TZ],
bx1, by1, b[TZ]);
smoothTriangle.setIntensities(a[SR], a[SG], a[SB], a[SA],
b[SR], b[SG], b[SB], b[SA],
b[SR], b[SG], b[SB], b[SA]);
smoothTriangle.render();

} else {
triangle.reset();

// render first triangle for thick line
triangle.setVertices(ax1, ay1, a[TZ],
bx2, by2, b[TZ],
ax2, ay2, a[TZ]);
triangle.setIntensities(a[SR], a[SG], a[SB], a[SA],
b[SR], b[SG], b[SB], b[SA],
a[SR], a[SG], a[SB], a[SA]);
triangle.render();

// render second triangle for thick line
triangle.setVertices(ax1, ay1, a[TZ],
bx2, by2, b[TZ],
bx1, by1, b[TZ]);
triangle.setIntensities(a[SR], a[SG], a[SB], a[SA],
b[SR], b[SG], b[SB], b[SA],
b[SR], b[SG], b[SB], b[SA]);
triangle.render();
}

} else {
line.reset();

line.setIntensities(a[SR], a[SG], a[SB], a[SA],
b[SR], b[SG], b[SB], b[SA]);

line.setVertices(a[TX], a[TY], a[TZ],
b[TX], b[TY], b[TZ]);

/*
// Seems okay to remove this because these vertices are not used again,
// but if problems arise, this needs to be uncommented because the above
// change is destructive and may need to be undone before proceeding.
if (drawing2D() && a[MZ] == 0) {
a[X] -= 0.01;
a[Y] -= 0.01;
a[VX] -= 0.01*a[VW];
a[VY] -= 0.01*a[VW];
b[X] -= 0.01;
b[Y] -= 0.01;
b[VX] -= 0.01*b[VW];
b[VY] -= 0.01*b[VW];
}
*/

line.draw();
}
}


/**
* Handle echoing line data to a raw shape recording renderer. This has been
* broken out of the renderLines() procedure so that renderLines() can be
* optimized per-renderer without having to deal with this code. This code,
* for instance, will stay the same when OpenGL is in use, but renderLines()
* can be optimized significantly.
* <br/> <br/>
* Values for start and stop are specified, so that in the future, sorted
* rendering can be implemented, which will require sequences of lines,
* triangles, or points to be rendered in the neighborhood of one another.
* That is, if we're gonna depth sort, we can't just draw all the triangles
* and then draw all the lines, cuz that defeats the purpose.
*/
protected void rawLines(int start, int stop) {
raw.colorMode(RGB, 1);
raw.noFill();
raw.beginShape(LINES);

for (int i = start; i < stop; i++) {
float a[] = vertices[lines[i][VERTEX1]];
float b[] = vertices[lines[i][VERTEX2]];
raw.strokeWeight(vertices[lines[i][VERTEX2]][SW]);

if (raw.is3D()) {
if ((a[VW] != 0) && (b[VW] != 0)) {
raw.stroke(a[SR], a[SG], a[SB], a[SA]);
raw.vertex(a[VX] / a[VW], a[VY] / a[VW], a[VZ] / a[VW]);
raw.stroke(b[SR], b[SG], b[SB], b[SA]);
raw.vertex(b[VX] / b[VW], b[VY] / b[VW], b[VZ] / b[VW]);
}
} else if (raw.is2D()) {
raw.stroke(a[SR], a[SG], a[SB], a[SA]);
raw.vertex(a[TX], a[TY]);
raw.stroke(b[SR], b[SG], b[SB], b[SA]);
raw.vertex(b[TX], b[TY]);
}
}
raw.endShape();
}



/////////////////////////////////////////////////////////////////////////////

// TRIANGLES


protected void addTriangle(int a, int b, int c) {
addTriangleWithClip(a, b, c);
}


protected final void addTriangleWithClip(int a, int b, int c) {
boolean aClipped = false;
boolean bClipped = false;
int clippedCount = 0;

// This is a hack for temporary clipping. Clipping still needs to
// be implemented properly, however. Please help!
// http://dev.processing.org/bugs/show_bug.cgi?id=1393
cameraNear = -8;
if (vertices[a][VZ] > cameraNear) {
aClipped = true;
clippedCount++;
}
if (vertices[b][VZ] > cameraNear) {
bClipped = true;
clippedCount++;
}
if (vertices[c][VZ] > cameraNear) {
//cClipped = true;
clippedCount++;
}
if (clippedCount == 0) {
// if (vertices[a][VZ] < cameraFar &&
// vertices[b][VZ] < cameraFar &&
// vertices[c][VZ] < cameraFar) {
addTriangleWithoutClip(a, b, c);
// }

// } else if (true) {
// return;

} else if (clippedCount == 3) {
// In this case there is only one visible point. |/|
// So we'll have to make two new points on the clip line <| |
// and add that triangle instead. |\|

} else if (clippedCount == 2) {
//System.out.println("Clipped two");

int ca, cb, cc, cd, ce;
if (!aClipped) {
ca = a;
cb = b;
cc = c;
}
else if (!bClipped) {
ca = b;
cb = a;
cc = c;
}
else { //if (!cClipped) {
ca = c;
cb = b;
cc = a;
}

cd = interpolateClipVertex(ca, cb);
ce = interpolateClipVertex(ca, cc);
addTriangleWithoutClip(ca, cd, ce);

} else { // (clippedCount == 1) {
// . |
// In this case there are two visible points. |\|
// So we'll have to make two new points on the clip line | |>
// and then add two new triangles. |/|
// . |
//System.out.println("Clipped one");
int ca, cb, cc, cd, ce;
if (aClipped) {
//System.out.println("aClipped");
ca = c;
cb = b;
cc = a;
}
else if (bClipped) {
//System.out.println("bClipped");
ca = a;
cb = c;
cc = b;
}
else { //if (cClipped) {
//System.out.println("cClipped");
ca = a;
cb = b;
cc = c;
}

cd = interpolateClipVertex(ca, cc);
ce = interpolateClipVertex(cb, cc);
addTriangleWithoutClip(ca, cd, cb);
//System.out.println("ca: " + ca + ", " + vertices[ca][VX] + ", " + vertices[ca][VY] + ", " + vertices[ca][VZ]);
//System.out.println("cd: " + cd + ", " + vertices[cd][VX] + ", " + vertices[cd][VY] + ", " + vertices[cd][VZ]);
//System.out.println("cb: " + cb + ", " + vertices[cb][VX] + ", " + vertices[cb][VY] + ", " + vertices[cb][VZ]);
addTriangleWithoutClip(cb, cd, ce);
}
}


protected final int interpolateClipVertex(int a, int b) {
float[] va;
float[] vb;
// Set up va, vb such that va[VZ] >= vb[VZ]
if (vertices[a][VZ] < vertices[b][VZ]) {
va = vertices[b];
vb = vertices[a];
}
else {
va = vertices[a];
vb = vertices[b];
}
float az = va[VZ];
float bz = vb[VZ];

float dz = az - bz;
// If they have the same z, just use pt. a.
if (dz == 0) {
return a;
}
//float pa = (az - cameraNear) / dz;
//float pb = (cameraNear - bz) / dz;
float pa = (cameraNear - bz) / dz;
float pb = 1 - pa;

vertex(pa * va[X] + pb * vb[X],
pa * va[Y] + pb * vb[Y],
pa * va[Z] + pb * vb[Z]);
int irv = vertexCount - 1;
shapeLastPlusClipped++;

float[] rv = vertices[irv];

rv[TX] = pa * va[TX] + pb * vb[TX];
rv[TY] = pa * va[TY] + pb * vb[TY];
rv[TZ] = pa * va[TZ] + pb * vb[TZ];

rv[VX] = pa * va[VX] + pb * vb[VX];
rv[VY] = pa * va[VY] + pb * vb[VY];
rv[VZ] = pa * va[VZ] + pb * vb[VZ];
rv[VW] = pa * va[VW] + pb * vb[VW];

rv[R] = pa * va[R] + pb * vb[R];
rv[G] = pa * va[G] + pb * vb[G];
rv[B] = pa * va[B] + pb * vb[B];
rv[A] = pa * va[A] + pb * vb[A];

rv[U] = pa * va[U] + pb * vb[U];
rv[V] = pa * va[V] + pb * vb[V];

rv[SR] = pa * va[SR] + pb * vb[SR];
rv[SG] = pa * va[SG] + pb * vb[SG];
rv[SB] = pa * va[SB] + pb * vb[SB];
rv[SA] = pa * va[SA] + pb * vb[SA];

rv[NX] = pa * va[NX] + pb * vb[NX];
rv[NY] = pa * va[NY] + pb * vb[NY];
rv[NZ] = pa * va[NZ] + pb * vb[NZ];

// rv[SW] = pa * va[SW] + pb * vb[SW];

rv[AR] = pa * va[AR] + pb * vb[AR];
rv[AG] = pa * va[AG] + pb * vb[AG];
rv[AB] = pa * va[AB] + pb * vb[AB];

rv[SPR] = pa * va[SPR] + pb * vb[SPR];
rv[SPG] = pa * va[SPG] + pb * vb[SPG];
rv[SPB] = pa * va[SPB] + pb * vb[SPB];
//rv[SPA] = pa * va[SPA] + pb * vb[SPA];

rv[ER] = pa * va[ER] + pb * vb[ER];
rv[EG] = pa * va[EG] + pb * vb[EG];
rv[EB] = pa * va[EB] + pb * vb[EB];

rv[SHINE] = pa * va[SHINE] + pb * vb[SHINE];

rv[BEEN_LIT] = 0;

return irv;
}


protected final void addTriangleWithoutClip(int a, int b, int c) {
if (triangleCount == triangles.length) {
int temp[][] = new int[triangleCount<<1][TRIANGLE_FIELD_COUNT];
System.arraycopy(triangles, 0, temp, 0, triangleCount);
triangles = temp;
//message(CHATTER, "allocating more triangles " + triangles.length);
float ftemp[][][] = new float[triangleCount<<1][3][TRI_COLOR_COUNT];
System.arraycopy(triangleColors, 0, ftemp, 0, triangleCount);
triangleColors = ftemp;
}
triangles[triangleCount][VERTEX1] = a;
triangles[triangleCount][VERTEX2] = b;
triangles[triangleCount][VERTEX3] = c;

if (textureImage == null) {
triangles[triangleCount][TEXTURE_INDEX] = -1;
} else {
triangles[triangleCount][TEXTURE_INDEX] = textureIndex;
}

// triangles[triangleCount][INDEX] = shape_index;
triangleCount++;
}


/**
* Triangulate the current polygon.
* <BR> <BR>
* Simple ear clipping polygon triangulation adapted from code by
* John W. Ratcliff (jratcliff at verant.com). Presumably
* <A HREF="http://www.flipcode.org/cgi-bin/fcarticles.cgi?show=63943">this</A>
* bit of code from the web.
*/
protected void addPolygonTriangles() {
if (vertexOrder.length != vertices.length) {
int[] temp = new int[vertices.length];
// vertex_start may not be zero, might need to keep old stuff around
// also, copy vertexOrder.length, not vertexCount because vertexCount
// may be larger than vertexOrder.length (since this is a post-processing
// step that happens after the vertex arrays are built).
PApplet.arrayCopy(vertexOrder, temp, vertexOrder.length);
vertexOrder = temp;
}

// this clipping algorithm only works in 2D, so in cases where a
// polygon is drawn perpendicular to the z-axis, the area will be zero,
// and triangulation will fail. as such, when the area calculates to
// zero, figure out whether x or y is empty, and calculate based on the
// two dimensions that actually contain information.
// http://dev.processing.org/bugs/show_bug.cgi?id=111
int d1 = X;
int d2 = Y;
// this brings up the nastier point that there may be cases where
// a polygon is irregular in space and will throw off the
// clockwise/counterclockwise calculation. for instance, if clockwise
// relative to x and z, but counter relative to y and z or something
// like that.. will wait to see if this is in fact a problem before
// hurting my head on the math.

/*
// trying to track down bug #774
for (int i = vertex_start; i < vertex_end; i++) {
if (i > vertex_start) {
if (vertices[i-1][MX] == vertices[i][MX] &&
vertices[i-1][MY] == vertices[i][MY]) {
System.out.print("**** " );
}
}
System.out.println(i + " " + vertices[i][MX] + " " + vertices[i][MY]);
}
System.out.println();
*/

// first we check if the polygon goes clockwise or counterclockwise
float area = 0;
for (int p = shapeLast - 1, q = shapeFirst; q < shapeLast; p = q++) {
area += (vertices[q][d1] * vertices[p][d2] -
vertices[p][d1] * vertices[q][d2]);
}
// rather than checking for the perpendicular case first, only do it
// when the area calculates to zero. checking for perpendicular would be
// a needless waste of time for the 99% case.
if (area == 0) {
// figure out which dimension is the perpendicular axis
boolean foundValidX = false;
boolean foundValidY = false;

for (int i = shapeFirst; i < shapeLast; i++) {
for (int j = i; j < shapeLast; j++){
if ( vertices[i][X] != vertices[j][X] ) foundValidX = true;
if ( vertices[i][Y] != vertices[j][Y] ) foundValidY = true;
}
}

if (foundValidX) {
//d1 = MX; // already the case
d2 = Z;
} else if (foundValidY) {
// ermm.. which is the proper order for cw/ccw here?
d1 = Y;
d2 = Z;
} else {
// screw it, this polygon is just f-ed up
return;
}

// re-calculate the area, with what should be good values
for (int p = shapeLast - 1, q = shapeFirst; q < shapeLast; p = q++) {
area += (vertices[q][d1] * vertices[p][d2] -
vertices[p][d1] * vertices[q][d2]);
}
}

// don't allow polygons to come back and meet themselves,
// otherwise it will anger the triangulator
// http://dev.processing.org/bugs/show_bug.cgi?id=97
float vfirst[] = vertices[shapeFirst];
float vlast[] = vertices[shapeLast-1];
if ((abs(vfirst[X] - vlast[X]) < EPSILON) &&
(abs(vfirst[Y] - vlast[Y]) < EPSILON) &&
(abs(vfirst[Z] - vlast[Z]) < EPSILON)) {
shapeLast--;
}

// then sort the vertices so they are always in a counterclockwise order
int j = 0;
if (area > 0) {
for (int i = shapeFirst; i < shapeLast; i++) {
j = i - shapeFirst;
vertexOrder[j] = i;
}
} else {
for (int i = shapeFirst; i < shapeLast; i++) {
j = i - shapeFirst;
vertexOrder[j] = (shapeLast - 1) - j;
}
}

// remove vc-2 Vertices, creating 1 triangle every time
int vc = shapeLast - shapeFirst;
int count = 2*vc; // complex polygon detection

for (int m = 0, v = vc - 1; vc > 2; ) {
boolean snip = true;

// if we start over again, is a complex polygon
if (0 >= (count--)) {
break; // triangulation failed
}

// get 3 consecutive vertices <u,v,w>
int u = v ; if (vc <= u) u = 0; // previous
v = u + 1; if (vc <= v) v = 0; // current
int w = v + 1; if (vc <= w) w = 0; // next

// Upgrade values to doubles, and multiply by 10 so that we can have
// some better accuracy as we tessellate. This seems to have negligible
// speed differences on Windows and Intel Macs, but causes a 50% speed
// drop for PPC Macs with the bug's example code that draws ~200 points
// in a concave polygon. Apple has abandoned PPC so we may as well too.
// http://dev.processing.org/bugs/show_bug.cgi?id=774

// triangle A B C
double Ax = -10 * vertices[vertexOrder[u]][d1];
double Ay = 10 * vertices[vertexOrder[u]][d2];
double Bx = -10 * vertices[vertexOrder[v]][d1];
double By = 10 * vertices[vertexOrder[v]][d2];
double Cx = -10 * vertices[vertexOrder[w]][d1];
double Cy = 10 * vertices[vertexOrder[w]][d2];

// first we check if <u,v,w> continues going ccw
if (EPSILON > (((Bx-Ax) * (Cy-Ay)) - ((By-Ay) * (Cx-Ax)))) {
continue;
}

for (int p = 0; p < vc; p++) {
if ((p == u) || (p == v) || (p == w)) {
continue;
}

double Px = -10 * vertices[vertexOrder[p]][d1];
double Py = 10 * vertices[vertexOrder[p]][d2];

double ax = Cx - Bx; double ay = Cy - By;
double bx = Ax - Cx; double by = Ay - Cy;
double cx = Bx - Ax; double cy = By - Ay;
double apx = Px - Ax; double apy = Py - Ay;
double bpx = Px - Bx; double bpy = Py - By;
double cpx = Px - Cx; double cpy = Py - Cy;

double aCROSSbp = ax * bpy - ay * bpx;
double cCROSSap = cx * apy - cy * apx;
double bCROSScp = bx * cpy - by * cpx;

if ((aCROSSbp >= 0.0) && (bCROSScp >= 0.0) && (cCROSSap >= 0.0)) {
snip = false;
}
}

if (snip) {
addTriangle(vertexOrder[u], vertexOrder[v], vertexOrder[w]);

m++;

// remove v from remaining polygon
for (int s = v, t = v + 1; t < vc; s++, t++) {
vertexOrder[s] = vertexOrder[t];
}
vc--;

// reset error detection counter
count = 2 * vc;
}
}
}


private void toWorldNormal(float nx, float ny, float nz, float[] out) {
out[0] =
modelviewInv.m00*nx + modelviewInv.m10*ny +
modelviewInv.m20*nz + modelviewInv.m30;
out[1] =
modelviewInv.m01*nx + modelviewInv.m11*ny +
modelviewInv.m21*nz + modelviewInv.m31;
out[2] =
modelviewInv.m02*nx + modelviewInv.m12*ny +
modelviewInv.m22*nz + modelviewInv.m32;
out[3] =
modelviewInv.m03*nx + modelviewInv.m13*ny +
modelviewInv.m23*nz + modelviewInv.m33;

if (out[3] != 0 && out[3] != 1) {
// divide by perspective coordinate
out[0] /= out[3]; out[1] /= out[3]; out[2] /= out[3];
}
out[3] = 1;

float nlen = mag(out[0], out[1], out[2]); // normalize
if (nlen != 0 && nlen != 1) {
out[0] /= nlen; out[1] /= nlen; out[2] /= nlen;
}
}


//private PVector calcLightingNorm = new PVector();
//private PVector calcLightingWorldNorm = new PVector();
float[] worldNormal = new float[4];


private void calcLightingContribution(int vIndex,
float[] contribution) {
calcLightingContribution(vIndex, contribution, false);
}


private void calcLightingContribution(int vIndex,
float[] contribution,
boolean normalIsWorld) {
float[] v = vertices[vIndex];

float sr = v[SPR];
float sg = v[SPG];
float sb = v[SPB];

float wx = v[VX];
float wy = v[VY];
float wz = v[VZ];
float shine = v[SHINE];

float nx = v[NX];
float ny = v[NY];
float nz = v[NZ];

if (!normalIsWorld) {
// System.out.println("um, hello?");
// calcLightingNorm.set(nx, ny, nz);
// //modelviewInv.mult(calcLightingNorm, calcLightingWorldNorm);
//
//// PMatrix3D mvi = modelViewInv;
//// float ox = mvi.m00*nx + mvi.m10*ny + mvi*m20+nz +
// modelviewInv.cmult(calcLightingNorm, calcLightingWorldNorm);
//
// calcLightingWorldNorm.normalize();
// nx = calcLightingWorldNorm.x;
// ny = calcLightingWorldNorm.y;
// nz = calcLightingWorldNorm.z;

toWorldNormal(v[NX], v[NY], v[NZ], worldNormal);
nx = worldNormal[X];
ny = worldNormal[Y];
nz = worldNormal[Z];

// float wnx = modelviewInv.multX(nx, ny, nz);
// float wny = modelviewInv.multY(nx, ny, nz);
// float wnz = modelviewInv.multZ(nx, ny, nz);
// float wnw = modelviewInv.multW(nx, ny, nz);

// if (wnw != 0 && wnw != 1) {
// wnx /= wnw;
// wny /= wnw;
// wnz /= wnw;
// }
// float nlen = mag(wnx, wny, wnw);
// if (nlen != 0 && nlen != 1) {
// nx = wnx / nlen;
// ny = wny / nlen;
// nz = wnz / nlen;
// } else {
// nx = wnx;
// ny = wny;
// nz = wnz;
// }
// */
} else {
nx = v[NX];
ny = v[NY];
nz = v[NZ];
}

// Since the camera space == world space,
// we can test for visibility by the dot product of
// the normal with the direction from pt. to eye.
float dir = dot(nx, ny, nz, -wx, -wy, -wz);
// If normal is away from camera, choose its opposite.
// If we add backface culling, this will be backfacing
// (but since this is per vertex, it's more complicated)
if (dir < 0) {
nx = -nx;
ny = -ny;
nz = -nz;
}

// These two terms will sum the contributions from the various lights
contribution[LIGHT_AMBIENT_R] = 0;
contribution[LIGHT_AMBIENT_G] = 0;
contribution[LIGHT_AMBIENT_B] = 0;

contribution[LIGHT_DIFFUSE_R] = 0;
contribution[LIGHT_DIFFUSE_G] = 0;
contribution[LIGHT_DIFFUSE_B] = 0;

contribution[LIGHT_SPECULAR_R] = 0;
contribution[LIGHT_SPECULAR_G] = 0;
contribution[LIGHT_SPECULAR_B] = 0;

// for (int i = 0; i < MAX_LIGHTS; i++) {
// if (!light[i]) continue;
for (int i = 0; i < lightCount; i++) {

float denom = lightFalloffConstant[i];
float spotTerm = 1;

if (lightType[i] == AMBIENT) {
if (lightFalloffQuadratic[i] != 0 || lightFalloffLinear[i] != 0) {
// Falloff depends on distance
float distSq = mag(lightPosition[i].x - wx,
lightPosition[i].y - wy,
lightPosition[i].z - wz);
denom +=
lightFalloffQuadratic[i] * distSq +
lightFalloffLinear[i] * sqrt(distSq);
}
if (denom == 0) denom = 1;

contribution[LIGHT_AMBIENT_R] += lightDiffuse[i][0] / denom;
contribution[LIGHT_AMBIENT_G] += lightDiffuse[i][1] / denom;
contribution[LIGHT_AMBIENT_B] += lightDiffuse[i][2] / denom;

} else {
// If not ambient, we must deal with direction

// li is the vector from the vertex to the light
float lix, liy, liz;
float lightDir_dot_li = 0;
float n_dot_li = 0;

if (lightType[i] == DIRECTIONAL) {
lix = -lightNormal[i].x;
liy = -lightNormal[i].y;
liz = -lightNormal[i].z;
denom = 1;
n_dot_li = (nx * lix + ny * liy + nz * liz);
// If light is lighting the face away from the camera, ditch
if (n_dot_li <= 0) {
continue;
}
} else { // Point or spot light (must deal also with light location)
lix = lightPosition[i].x - wx;
liy = lightPosition[i].y - wy;
liz = lightPosition[i].z - wz;
// normalize
float distSq = mag(lix, liy, liz);
if (distSq != 0) {
lix /= distSq;
liy /= distSq;
liz /= distSq;
}
n_dot_li = (nx * lix + ny * liy + nz * liz);
// If light is lighting the face away from the camera, ditch
if (n_dot_li <= 0) {
continue;
}

if (lightType[i] == SPOT) { // Must deal with spot cone
lightDir_dot_li =
-(lightNormal[i].x * lix +
lightNormal[i].y * liy +
lightNormal[i].z * liz);
// Outside of spot cone
if (lightDir_dot_li <= lightSpotAngleCos[i]) {
continue;
}
spotTerm = (float) Math.pow(lightDir_dot_li, lightSpotConcentration[i]);
}

if (lightFalloffQuadratic[i] != 0 || lightFalloffLinear[i] != 0) {
// Falloff depends on distance
denom +=
lightFalloffQuadratic[i] * distSq +
lightFalloffLinear[i] * (float) sqrt(distSq);
}
}
// Directional, point, or spot light:

// We know n_dot_li > 0 from above "continues"

if (denom == 0)
denom = 1;
float mul = n_dot_li * spotTerm / denom;
contribution[LIGHT_DIFFUSE_R] += lightDiffuse[i][0] * mul;
contribution[LIGHT_DIFFUSE_G] += lightDiffuse[i][1] * mul;
contribution[LIGHT_DIFFUSE_B] += lightDiffuse[i][2] * mul;

// SPECULAR

// If the material and light have a specular component.
if ((sr > 0 || sg > 0 || sb > 0) &&
(lightSpecular[i][0] > 0 ||
lightSpecular[i][1] > 0 ||
lightSpecular[i][2] > 0)) {

float vmag = mag(wx, wy, wz);
if (vmag != 0) {
wx /= vmag;
wy /= vmag;
wz /= vmag;
}
float sx = lix - wx;
float sy = liy - wy;
float sz = liz - wz;
vmag = mag(sx, sy, sz);
if (vmag != 0) {
sx /= vmag;
sy /= vmag;
sz /= vmag;
}
float s_dot_n = (sx * nx + sy * ny + sz * nz);

if (s_dot_n > 0) {
s_dot_n = (float) Math.pow(s_dot_n, shine);
mul = s_dot_n * spotTerm / denom;
contribution[LIGHT_SPECULAR_R] += lightSpecular[i][0] * mul;
contribution[LIGHT_SPECULAR_G] += lightSpecular[i][1] * mul;
contribution[LIGHT_SPECULAR_B] += lightSpecular[i][2] * mul;
}

}
}
}
return;
}


// Multiply the lighting contribution into the vertex's colors.
// Only do this when there is ONE lighting per vertex
// (MANUAL_VERTEX_NORMAL or SHAPE_NORMAL mode).
private void applyLightingContribution(int vIndex, float[] contribution) {
float[] v = vertices[vIndex];

v[R] = clamp(v[ER] + v[AR] * contribution[LIGHT_AMBIENT_R] + v[DR] * contribution[LIGHT_DIFFUSE_R]);
v[G] = clamp(v[EG] + v[AG] * contribution[LIGHT_AMBIENT_G] + v[DG] * contribution[LIGHT_DIFFUSE_G]);
v[B] = clamp(v[EB] + v[AB] * contribution[LIGHT_AMBIENT_B] + v[DB] * contribution[LIGHT_DIFFUSE_B]);
v[A] = clamp(v[DA]);

v[SPR] = clamp(v[SPR] * contribution[LIGHT_SPECULAR_R]);
v[SPG] = clamp(v[SPG] * contribution[LIGHT_SPECULAR_G]);
v[SPB] = clamp(v[SPB] * contribution[LIGHT_SPECULAR_B]);
//v[SPA] = min(1, v[SPA]);

v[BEEN_LIT] = 1;
}


private void lightVertex(int vIndex, float[] contribution) {
calcLightingContribution(vIndex, contribution);
applyLightingContribution(vIndex, contribution);
}


private void lightUnlitVertex(int vIndex, float[] contribution) {
if (vertices[vIndex][BEEN_LIT] == 0) {
lightVertex(vIndex, contribution);
}
}


private void copyPrelitVertexColor(int triIndex, int index, int colorIndex) {
float[] triColor = triangleColors[triIndex][colorIndex];
float[] v = vertices[index];

triColor[TRI_DIFFUSE_R] = v[R];
triColor[TRI_DIFFUSE_G] = v[G];
triColor[TRI_DIFFUSE_B] = v[B];
triColor[TRI_DIFFUSE_A] = v[A];
triColor[TRI_SPECULAR_R] = v[SPR];
triColor[TRI_SPECULAR_G] = v[SPG];
triColor[TRI_SPECULAR_B] = v[SPB];
//triColor[TRI_SPECULAR_A] = v[SPA];
}


private void copyVertexColor(int triIndex, int index, int colorIndex,
float[] contrib) {
float[] triColor = triangleColors[triIndex][colorIndex];
float[] v = vertices[index];

triColor[TRI_DIFFUSE_R] =
clamp(v[ER] + v[AR] * contrib[LIGHT_AMBIENT_R] + v[DR] * contrib[LIGHT_DIFFUSE_R]);
triColor[TRI_DIFFUSE_G] =
clamp(v[EG] + v[AG] * contrib[LIGHT_AMBIENT_G] + v[DG] * contrib[LIGHT_DIFFUSE_G]);
triColor[TRI_DIFFUSE_B] =
clamp(v[EB] + v[AB] * contrib[LIGHT_AMBIENT_B] + v[DB] * contrib[LIGHT_DIFFUSE_B]);
triColor[TRI_DIFFUSE_A] = clamp(v[DA]);

triColor[TRI_SPECULAR_R] = clamp(v[SPR] * contrib[LIGHT_SPECULAR_R]);
triColor[TRI_SPECULAR_G] = clamp(v[SPG] * contrib[LIGHT_SPECULAR_G]);
triColor[TRI_SPECULAR_B] = clamp(v[SPB] * contrib[LIGHT_SPECULAR_B]);
}


private void lightTriangle(int triIndex, float[] lightContribution) {
int vIndex = triangles[triIndex][VERTEX1];
copyVertexColor(triIndex, vIndex, 0, lightContribution);
vIndex = triangles[triIndex][VERTEX2];
copyVertexColor(triIndex, vIndex, 1, lightContribution);
vIndex = triangles[triIndex][VERTEX3];
copyVertexColor(triIndex, vIndex, 2, lightContribution);
}


private void lightTriangle(int triIndex) {
int vIndex;

// Handle lighting on, but no lights (in this case, just use emissive)
// This wont be used currently because lightCount == 0 is don't use
// lighting at all... So. OK. If that ever changes, use the below:
/*
if (lightCount == 0) {
vIndex = triangles[triIndex][VERTEX1];
copy_emissive_vertex_color_to_triangle(triIndex, vIndex, 0);
vIndex = triangles[triIndex][VERTEX2];
copy_emissive_vertex_color_to_triangle(triIndex, vIndex, 1);
vIndex = triangles[triIndex][VERTEX3];
copy_emissive_vertex_color_to_triangle(triIndex, vIndex, 2);
return;
}
*/

// In MANUAL_VERTEX_NORMAL mode, we have a specific normal
// for each vertex. In that case, we light any verts that
// haven't already been lit and copy their colors straight
// into the triangle.
if (normalMode == NORMAL_MODE_VERTEX) {
vIndex = triangles[triIndex][VERTEX1];
lightUnlitVertex(vIndex, tempLightingContribution);
copyPrelitVertexColor(triIndex, vIndex, 0);

vIndex = triangles[triIndex][VERTEX2];
lightUnlitVertex(vIndex, tempLightingContribution);
copyPrelitVertexColor(triIndex, vIndex, 1);

vIndex = triangles[triIndex][VERTEX3];
lightUnlitVertex(vIndex, tempLightingContribution);
copyPrelitVertexColor(triIndex, vIndex, 2);

}

// If the lighting doesn't depend on the vertex position, do the
// following: We've already dealt with NORMAL_MODE_SHAPE mode before
// we got into this function, so here we only have to deal with
// NORMAL_MODE_AUTO. So we calculate the normal for this triangle,
// and use that for the lighting.
else if (!lightingDependsOnVertexPosition) {
vIndex = triangles[triIndex][VERTEX1];
int vIndex2 = triangles[triIndex][VERTEX2];
int vIndex3 = triangles[triIndex][VERTEX3];

/*
dv1[0] = vertices[vIndex2][VX] - vertices[vIndex][VX];
dv1[1] = vertices[vIndex2][VY] - vertices[vIndex][VY];
dv1[2] = vertices[vIndex2][VZ] - vertices[vIndex][VZ];

dv2[0] = vertices[vIndex3][VX] - vertices[vIndex][VX];
dv2[1] = vertices[vIndex3][VY] - vertices[vIndex][VY];
dv2[2] = vertices[vIndex3][VZ] - vertices[vIndex][VZ];

cross(dv1, dv2, norm);
*/

cross(vertices[vIndex2][VX] - vertices[vIndex][VX],
vertices[vIndex2][VY] - vertices[vIndex][VY],
vertices[vIndex2][VZ] - vertices[vIndex][VZ],
vertices[vIndex3][VX] - vertices[vIndex][VX],
vertices[vIndex3][VY] - vertices[vIndex][VY],
vertices[vIndex3][VZ] - vertices[vIndex][VZ], lightTriangleNorm);

lightTriangleNorm.normalize();
vertices[vIndex][NX] = lightTriangleNorm.x;
vertices[vIndex][NY] = lightTriangleNorm.y;
vertices[vIndex][NZ] = lightTriangleNorm.z;

// The true at the end says the normal is already in world coordinates
calcLightingContribution(vIndex, tempLightingContribution, true);
copyVertexColor(triIndex, vIndex, 0, tempLightingContribution);
copyVertexColor(triIndex, vIndex2, 1, tempLightingContribution);
copyVertexColor(triIndex, vIndex3, 2, tempLightingContribution);
}

// If lighting is position-dependent
else {
if (normalMode == NORMAL_MODE_SHAPE) {
vIndex = triangles[triIndex][VERTEX1];
vertices[vIndex][NX] = vertices[shapeFirst][NX];
vertices[vIndex][NY] = vertices[shapeFirst][NY];
vertices[vIndex][NZ] = vertices[shapeFirst][NZ];
calcLightingContribution(vIndex, tempLightingContribution);
copyVertexColor(triIndex, vIndex, 0, tempLightingContribution);

vIndex = triangles[triIndex][VERTEX2];
vertices[vIndex][NX] = vertices[shapeFirst][NX];
vertices[vIndex][NY] = vertices[shapeFirst][NY];
vertices[vIndex][NZ] = vertices[shapeFirst][NZ];
calcLightingContribution(vIndex, tempLightingContribution);
copyVertexColor(triIndex, vIndex, 1, tempLightingContribution);

vIndex = triangles[triIndex][VERTEX3];
vertices[vIndex][NX] = vertices[shapeFirst][NX];
vertices[vIndex][NY] = vertices[shapeFirst][NY];
vertices[vIndex][NZ] = vertices[shapeFirst][NZ];
calcLightingContribution(vIndex, tempLightingContribution);
copyVertexColor(triIndex, vIndex, 2, tempLightingContribution);
}

// lighting mode is AUTO_NORMAL
else {
vIndex = triangles[triIndex][VERTEX1];
int vIndex2 = triangles[triIndex][VERTEX2];
int vIndex3 = triangles[triIndex][VERTEX3];

/*
dv1[0] = vertices[vIndex2][VX] - vertices[vIndex][VX];
dv1[1] = vertices[vIndex2][VY] - vertices[vIndex][VY];
dv1[2] = vertices[vIndex2][VZ] - vertices[vIndex][VZ];

dv2[0] = vertices[vIndex3][VX] - vertices[vIndex][VX];
dv2[1] = vertices[vIndex3][VY] - vertices[vIndex][VY];
dv2[2] = vertices[vIndex3][VZ] - vertices[vIndex][VZ];

cross(dv1, dv2, norm);
*/

cross(vertices[vIndex2][VX] - vertices[vIndex][VX],
vertices[vIndex2][VY] - vertices[vIndex][VY],
vertices[vIndex2][VZ] - vertices[vIndex][VZ],
vertices[vIndex3][VX] - vertices[vIndex][VX],
vertices[vIndex3][VY] - vertices[vIndex][VY],
vertices[vIndex3][VZ] - vertices[vIndex][VZ], lightTriangleNorm);
// float nmag = mag(norm[X], norm[Y], norm[Z]);
// if (nmag != 0 && nmag != 1) {
// norm[X] /= nmag; norm[Y] /= nmag; norm[Z] /= nmag;
// }
lightTriangleNorm.normalize();
vertices[vIndex][NX] = lightTriangleNorm.x;
vertices[vIndex][NY] = lightTriangleNorm.y;
vertices[vIndex][NZ] = lightTriangleNorm.z;
// The true at the end says the normal is already in world coordinates
calcLightingContribution(vIndex, tempLightingContribution, true);
copyVertexColor(triIndex, vIndex, 0, tempLightingContribution);

vertices[vIndex2][NX] = lightTriangleNorm.x;
vertices[vIndex2][NY] = lightTriangleNorm.y;
vertices[vIndex2][NZ] = lightTriangleNorm.z;
// The true at the end says the normal is already in world coordinates
calcLightingContribution(vIndex2, tempLightingContribution, true);
copyVertexColor(triIndex, vIndex2, 1, tempLightingContribution);

vertices[vIndex3][NX] = lightTriangleNorm.x;
vertices[vIndex3][NY] = lightTriangleNorm.y;
vertices[vIndex3][NZ] = lightTriangleNorm.z;
// The true at the end says the normal is already in world coordinates
calcLightingContribution(vIndex3, tempLightingContribution, true);
copyVertexColor(triIndex, vIndex3, 2, tempLightingContribution);
}
}
}


protected void renderTriangles(int start, int stop) {
for (int i = start; i < stop; i++) {
float a[] = vertices[triangles[i][VERTEX1]];
float b[] = vertices[triangles[i][VERTEX2]];
float c[] = vertices[triangles[i][VERTEX3]];
int tex = triangles[i][TEXTURE_INDEX];

/*
// removing for 0149 with the return of P2D
// ewjordan: hack to 'fix' accuracy issues when drawing in 2d
// see also render_lines() where similar hack is employed
float shift = 0.15f;//was 0.49f
boolean shifted = false;
if (drawing2D() && (a[Z] == 0)) {
shifted = true;
a[TX] += shift;
a[TY] += shift;
a[VX] += shift*a[VW];
a[VY] += shift*a[VW];
b[TX] += shift;
b[TY] += shift;
b[VX] += shift*b[VW];
b[VY] += shift*b[VW];
c[TX] += shift;
c[TY] += shift;
c[VX] += shift*c[VW];
c[VY] += shift*c[VW];
}
*/

triangle.reset();

// This is only true when not textured.
// We really should pass specular straight through to triangle rendering.
float ar = clamp(triangleColors[i][0][TRI_DIFFUSE_R] + triangleColors[i][0][TRI_SPECULAR_R]);
float ag = clamp(triangleColors[i][0][TRI_DIFFUSE_G] + triangleColors[i][0][TRI_SPECULAR_G]);
float ab = clamp(triangleColors[i][0][TRI_DIFFUSE_B] + triangleColors[i][0][TRI_SPECULAR_B]);
float br = clamp(triangleColors[i][1][TRI_DIFFUSE_R] + triangleColors[i][1][TRI_SPECULAR_R]);
float bg = clamp(triangleColors[i][1][TRI_DIFFUSE_G] + triangleColors[i][1][TRI_SPECULAR_G]);
float bb = clamp(triangleColors[i][1][TRI_DIFFUSE_B] + triangleColors[i][1][TRI_SPECULAR_B]);
float cr = clamp(triangleColors[i][2][TRI_DIFFUSE_R] + triangleColors[i][2][TRI_SPECULAR_R]);
float cg = clamp(triangleColors[i][2][TRI_DIFFUSE_G] + triangleColors[i][2][TRI_SPECULAR_G]);
float cb = clamp(triangleColors[i][2][TRI_DIFFUSE_B] + triangleColors[i][2][TRI_SPECULAR_B]);

// ACCURATE TEXTURE CODE
boolean failedToPrecalc = false;
if (s_enableAccurateTextures && frustumMode){
boolean textured = true;
smoothTriangle.reset(3);
smoothTriangle.smooth = true;
smoothTriangle.interpARGB = true;
smoothTriangle.setIntensities(ar, ag, ab, a[A],
br, bg, bb, b[A],
cr, cg, cb, c[A]);
if (tex > -1 && textures[tex] != null) {
smoothTriangle.setCamVertices(a[VX], a[VY], a[VZ],
b[VX], b[VY], b[VZ],
c[VX], c[VY], c[VZ]);
smoothTriangle.interpUV = true;
smoothTriangle.texture(textures[tex]);
float umult = textures[tex].width; // apparently no check for textureMode is needed here
float vmult = textures[tex].height;
smoothTriangle.vertices[0][U] = a[U]*umult;
smoothTriangle.vertices[0][V] = a[V]*vmult;
smoothTriangle.vertices[1][U] = b[U]*umult;
smoothTriangle.vertices[1][V] = b[V]*vmult;
smoothTriangle.vertices[2][U] = c[U]*umult;
smoothTriangle.vertices[2][V] = c[V]*vmult;
} else {
smoothTriangle.interpUV = false;
textured = false;
}

smoothTriangle.setVertices(a[TX], a[TY], a[TZ],
b[TX], b[TY], b[TZ],
c[TX], c[TY], c[TZ]);


if (!textured || smoothTriangle.precomputeAccurateTexturing()){
smoothTriangle.render();
} else {
// Something went wrong with the precomputation,
// so we need to fall back on normal PTriangle
// rendering.
failedToPrecalc = true;
}
}

// Normal triangle rendering
// Note: this is not an end-if from the smoothed texturing mode
// because it's possible that the precalculation will fail and we
// need to fall back on normal rendering.
if (!s_enableAccurateTextures || failedToPrecalc || (frustumMode == false)){
if (tex > -1 && textures[tex] != null) {
triangle.setTexture(textures[tex]);
triangle.setUV(a[U], a[V], b[U], b[V], c[U], c[V]);
}

triangle.setIntensities(ar, ag, ab, a[A],
br, bg, bb, b[A],
cr, cg, cb, c[A]);

triangle.setVertices(a[TX], a[TY], a[TZ],
b[TX], b[TY], b[TZ],
c[TX], c[TY], c[TZ]);

triangle.render();
}

/*
// removing for 0149 with the return of P2D
if (drawing2D() && shifted){
a[TX] -= shift;
a[TY] -= shift;
a[VX] -= shift*a[VW];
a[VY] -= shift*a[VW];
b[TX] -= shift;
b[TY] -= shift;
b[VX] -= shift*b[VW];
b[VY] -= shift*b[VW];
c[TX] -= shift;
c[TY] -= shift;
c[VX] -= shift*c[VW];
c[VY] -= shift*c[VW];
}
*/
}
}


protected void rawTriangles(int start, int stop) {
raw.colorMode(RGB, 1);
raw.noStroke();
raw.beginShape(TRIANGLES);

for (int i = start; i < stop; i++) {
float a[] = vertices[triangles[i][VERTEX1]];
float b[] = vertices[triangles[i][VERTEX2]];
float c[] = vertices[triangles[i][VERTEX3]];

float ar = clamp(triangleColors[i][0][TRI_DIFFUSE_R] + triangleColors[i][0][TRI_SPECULAR_R]);
float ag = clamp(triangleColors[i][0][TRI_DIFFUSE_G] + triangleColors[i][0][TRI_SPECULAR_G]);
float ab = clamp(triangleColors[i][0][TRI_DIFFUSE_B] + triangleColors[i][0][TRI_SPECULAR_B]);
float br = clamp(triangleColors[i][1][TRI_DIFFUSE_R] + triangleColors[i][1][TRI_SPECULAR_R]);
float bg = clamp(triangleColors[i][1][TRI_DIFFUSE_G] + triangleColors[i][1][TRI_SPECULAR_G]);
float bb = clamp(triangleColors[i][1][TRI_DIFFUSE_B] + triangleColors[i][1][TRI_SPECULAR_B]);
float cr = clamp(triangleColors[i][2][TRI_DIFFUSE_R] + triangleColors[i][2][TRI_SPECULAR_R]);
float cg = clamp(triangleColors[i][2][TRI_DIFFUSE_G] + triangleColors[i][2][TRI_SPECULAR_G]);
float cb = clamp(triangleColors[i][2][TRI_DIFFUSE_B] + triangleColors[i][2][TRI_SPECULAR_B]);

int tex = triangles[i][TEXTURE_INDEX];
PImage texImage = (tex > -1) ? textures[tex] : null;
if (texImage != null) {
if (raw.is3D()) {
if ((a[VW] != 0) && (b[VW] != 0) && (c[VW] != 0)) {
raw.fill(ar, ag, ab, a[A]);
raw.vertex(a[VX] / a[VW], a[VY] / a[VW], a[VZ] / a[VW], a[U], a[V]);
raw.fill(br, bg, bb, b[A]);
raw.vertex(b[VX] / b[VW], b[VY] / b[VW], b[VZ] / b[VW], b[U], b[V]);
raw.fill(cr, cg, cb, c[A]);
raw.vertex(c[VX] / c[VW], c[VY] / c[VW], c[VZ] / c[VW], c[U], c[V]);
}
} else if (raw.is2D()) {
raw.fill(ar, ag, ab, a[A]);
raw.vertex(a[TX], a[TY], a[U], a[V]);
raw.fill(br, bg, bb, b[A]);
raw.vertex(b[TX], b[TY], b[U], b[V]);
raw.fill(cr, cg, cb, c[A]);
raw.vertex(c[TX], c[TY], c[U], c[V]);
}
} else { // no texture
if (raw.is3D()) {
if ((a[VW] != 0) && (b[VW] != 0) && (c[VW] != 0)) {
raw.fill(ar, ag, ab, a[A]);
raw.vertex(a[VX] / a[VW], a[VY] / a[VW], a[VZ] / a[VW]);
raw.fill(br, bg, bb, b[A]);
raw.vertex(b[VX] / b[VW], b[VY] / b[VW], b[VZ] / b[VW]);
raw.fill(cr, cg, cb, c[A]);
raw.vertex(c[VX] / c[VW], c[VY] / c[VW], c[VZ] / c[VW]);
}
} else if (raw.is2D()) {
raw.fill(ar, ag, ab, a[A]);
raw.vertex(a[TX], a[TY]);
raw.fill(br, bg, bb, b[A]);
raw.vertex(b[TX], b[TY]);
raw.fill(cr, cg, cb, c[A]);
raw.vertex(c[TX], c[TY]);
}
}
}

raw.endShape();
}


//////////////////////////////////////////////////////////////


//public void bezierVertex(float x2, float y2,
// float x3, float y3,
// float x4, float y4)


//public void bezierVertex(float x2, float y2, float z2,
// float x3, float y3, float z3,
// float x4, float y4, float z4)



//////////////////////////////////////////////////////////////


//public void curveVertex(float x, float y)


//public void curveVertex(float x, float y, float z)



////////////////////////////////////////////////////////////


/**
* Emit any sorted geometry that's been collected on this frame.
*/
public void flush() {
if (hints[ENABLE_DEPTH_SORT]) {
sort();
}
render();

/*
if (triangleCount > 0) {
if (hints[ENABLE_DEPTH_SORT]) {
sortTriangles();
}
renderTriangles();
}
if (lineCount > 0) {
if (hints[ENABLE_DEPTH_SORT]) {
sortLines();
}
renderLines();
}
// Clear this out in case flush() is called again.
// For instance, with hint(ENABLE_DEPTH_SORT), it will be called
// once on endRaw(), and once again at endDraw().
triangleCount = 0;
lineCount = 0;
*/
}


protected void render() {
if (pointCount > 0) {
renderPoints(0, pointCount);
if (raw != null) {
rawPoints(0, pointCount);
}
pointCount = 0;
}
if (lineCount > 0) {
renderLines(0, lineCount);
if (raw != null) {
rawLines(0, lineCount);
}
lineCount = 0;
pathCount = 0;
}
if (triangleCount > 0) {
renderTriangles(0, triangleCount);
if (raw != null) {
rawTriangles(0, triangleCount);
}
triangleCount = 0;
}
}


/**
* Handle depth sorting of geometry. Currently this only handles triangles,
* however in the future it will be expanded for points and lines, which
* will also need to be interspersed with one another while rendering.
*/
protected void sort() {
if (triangleCount > 0) {
sortTrianglesInternal(0, triangleCount-1);
}
}


private void sortTrianglesInternal(int i, int j) {
int pivotIndex = (i+j)/2;
sortTrianglesSwap(pivotIndex, j);
int k = sortTrianglesPartition(i-1, j);
sortTrianglesSwap(k, j);
if ((k-i) > 1) sortTrianglesInternal(i, k-1);
if ((j-k) > 1) sortTrianglesInternal(k+1, j);
}


private int sortTrianglesPartition(int left, int right) {
int pivot = right;
do {
while (sortTrianglesCompare(++left, pivot) < 0) { }
while ((right != 0) &&
(sortTrianglesCompare(--right, pivot) > 0)) { }
sortTrianglesSwap(left, right);
} while (left < right);
sortTrianglesSwap(left, right);
return left;
}


private void sortTrianglesSwap(int a, int b) {
int tempi[] = triangles[a];
triangles[a] = triangles[b];
triangles[b] = tempi;
float tempf[][] = triangleColors[a];
triangleColors[a] = triangleColors[b];
triangleColors[b] = tempf;
}


private float sortTrianglesCompare(int a, int b) {
/*
if (Float.isNaN(vertices[triangles[a][VERTEX1]][TZ]) ||
Float.isNaN(vertices[triangles[a][VERTEX2]][TZ]) ||
Float.isNaN(vertices[triangles[a][VERTEX3]][TZ]) ||
Float.isNaN(vertices[triangles[b][VERTEX1]][TZ]) ||
Float.isNaN(vertices[triangles[b][VERTEX2]][TZ]) ||
Float.isNaN(vertices[triangles[b][VERTEX3]][TZ])) {
System.err.println("NaN values in triangle");
}
*/
return ((vertices[triangles[b][VERTEX1]][TZ] +
vertices[triangles[b][VERTEX2]][TZ] +
vertices[triangles[b][VERTEX3]][TZ]) -
(vertices[triangles[a][VERTEX1]][TZ] +
vertices[triangles[a][VERTEX2]][TZ] +
vertices[triangles[a][VERTEX3]][TZ]));
}



//////////////////////////////////////////////////////////////

// POINT, LINE, TRIANGLE, QUAD

// Because vertex(x, y) is mapped to vertex(x, y, 0), none of these commands
// need to be overridden from their default implementation in PGraphics.


//public void point(float x, float y)


//public void point(float x, float y, float z)


//public void line(float x1, float y1, float x2, float y2)


//public void line(float x1, float y1, float z1,
// float x2, float y2, float z2)


//public void triangle(float x1, float y1, float x2, float y2,
// float x3, float y3)


//public void quad(float x1, float y1, float x2, float y2,
// float x3, float y3, float x4, float y4)



//////////////////////////////////////////////////////////////

// RECT


//public void rectMode(int mode)


//public void rect(float a, float b, float c, float d)


//protected void rectImpl(float x1, float y1, float x2, float y2)



//////////////////////////////////////////////////////////////

// ELLIPSE


//public void ellipseMode(int mode)


//public void ellipse(float a, float b, float c, float d)


protected void ellipseImpl(float x, float y, float w, float h) {
float radiusH = w / 2;
float radiusV = h / 2;

float centerX = x + radiusH;
float centerY = y + radiusV;

// float sx1 = screenX(x, y);
// float sy1 = screenY(x, y);
// float sx2 = screenX(x+w, y+h);
// float sy2 = screenY(x+w, y+h);

// returning to pre-1.0 version of algorithm because of problems
int rough = (int)(4+Math.sqrt(w+h)*3);
int accuracy = PApplet.constrain(rough, 6, 100);

if (fill) {
// returning to pre-1.0 version of algorithm because of problems
// int rough = (int)(4+Math.sqrt(w+h)*3);
// int rough = (int) (TWO_PI * PApplet.dist(sx1, sy1, sx2, sy2) / 20);
// int accuracy = PApplet.constrain(rough, 6, 100);

float inc = (float)SINCOS_LENGTH / accuracy;
float val = 0;

boolean strokeSaved = stroke;
stroke = false;
boolean smoothSaved = smooth;
if (smooth && stroke) {
smooth = false;
}

beginShape(TRIANGLE_FAN);
normal(0, 0, 1);
vertex(centerX, centerY);
for (int i = 0; i < accuracy; i++) {
vertex(centerX + cosLUT[(int) val] * radiusH,
centerY + sinLUT[(int) val] * radiusV);
val = (val + inc) % SINCOS_LENGTH;
}
// back to the beginning
vertex(centerX + cosLUT[0] * radiusH,
centerY + sinLUT[0] * radiusV);
endShape();

stroke = strokeSaved;
smooth = smoothSaved;
}

if (stroke) {
// int rough = (int) (TWO_PI * PApplet.dist(sx1, sy1, sx2, sy2) / 8);
// int accuracy = PApplet.constrain(rough, 6, 100);

float inc = (float)SINCOS_LENGTH / accuracy;
float val = 0;

boolean savedFill = fill;
fill = false;

val = 0;
beginShape();
for (int i = 0; i < accuracy; i++) {
vertex(centerX + cosLUT[(int) val] * radiusH,
centerY + sinLUT[(int) val] * radiusV);
val = (val + inc) % SINCOS_LENGTH;
}
endShape(CLOSE);

fill = savedFill;
}
}


//public void arc(float a, float b, float c, float d,
// float start, float stop)


protected void arcImpl(float x, float y, float w, float h,
float start, float stop) {
float hr = w / 2f;
float vr = h / 2f;

float centerX = x + hr;
float centerY = y + vr;

if (fill) {
// shut off stroke for a minute
boolean savedStroke = stroke;
stroke = false;

int startLUT = (int) (0.5f + (start / TWO_PI) * SINCOS_LENGTH);
int stopLUT = (int) (0.5f + (stop / TWO_PI) * SINCOS_LENGTH);

beginShape(TRIANGLE_FAN);
vertex(centerX, centerY);
int increment = 1; // what's a good algorithm? stopLUT - startLUT;
for (int i = startLUT; i < stopLUT; i += increment) {
int ii = i % SINCOS_LENGTH;
// modulo won't make the value positive
if (ii < 0) ii += SINCOS_LENGTH;
vertex(centerX + cosLUT[ii] * hr,
centerY + sinLUT[ii] * vr);
}
// draw last point explicitly for accuracy
vertex(centerX + cosLUT[stopLUT % SINCOS_LENGTH] * hr,
centerY + sinLUT[stopLUT % SINCOS_LENGTH] * vr);
endShape();

stroke = savedStroke;
}

if (stroke) {
// Almost identical to above, but this uses a LINE_STRIP
// and doesn't include the first (center) vertex.

boolean savedFill = fill;
fill = false;

int startLUT = (int) (0.5f + (start / TWO_PI) * SINCOS_LENGTH);
int stopLUT = (int) (0.5f + (stop / TWO_PI) * SINCOS_LENGTH);

beginShape(); //LINE_STRIP);
int increment = 1; // what's a good algorithm? stopLUT - startLUT;
for (int i = startLUT; i < stopLUT; i += increment) {
int ii = i % SINCOS_LENGTH;
if (ii < 0) ii += SINCOS_LENGTH;
vertex(centerX + cosLUT[ii] * hr,
centerY + sinLUT[ii] * vr);
}
// draw last point explicitly for accuracy
vertex(centerX + cosLUT[stopLUT % SINCOS_LENGTH] * hr,
centerY + sinLUT[stopLUT % SINCOS_LENGTH] * vr);
endShape();

fill = savedFill;
}
}



//////////////////////////////////////////////////////////////

// BOX


//public void box(float size)


public void box(float w, float h, float d) {
if (triangle != null) { // triangle is null in gl
triangle.setCulling(true);
}

super.box(w, h, d);

if (triangle != null) { // triangle is null in gl
triangle.setCulling(false);
}
}



//////////////////////////////////////////////////////////////

// SPHERE


//public void sphereDetail(int res)


//public void sphereDetail(int ures, int vres)


public void sphere(float r) {
if (triangle != null) { // triangle is null in gl
triangle.setCulling(true);
}

super.sphere(r);

if (triangle != null) { // triangle is null in gl
triangle.setCulling(false);
}
}



//////////////////////////////////////////////////////////////

// BEZIER


//public float bezierPoint(float a, float b, float c, float d, float t)


//public float bezierTangent(float a, float b, float c, float d, float t)


//public void bezierDetail(int detail)


//public void bezier(float x1, float y1,
// float x2, float y2,
// float x3, float y3,
// float x4, float y4)


//public void bezier(float x1, float y1, float z1,
// float x2, float y2, float z2,
// float x3, float y3, float z3,
// float x4, float y4, float z4)



//////////////////////////////////////////////////////////////

// CATMULL-ROM CURVES


//public float curvePoint(float a, float b, float c, float d, float t)


//public float curveTangent(float a, float b, float c, float d, float t)


//public void curveDetail(int detail)


//public void curveTightness(float tightness)


//public void curve(float x1, float y1,
// float x2, float y2,
// float x3, float y3,
// float x4, float y4)


//public void curve(float x1, float y1, float z1,
// float x2, float y2, float z2,
// float x3, float y3, float z3,
// float x4, float y4, float z4)



//////////////////////////////////////////////////////////////

// SMOOTH


public void smooth() {
//showMethodWarning("smooth");
s_enableAccurateTextures = true;
smooth = true;
}


public void noSmooth() {
s_enableAccurateTextures = false;
smooth = false;
}



//////////////////////////////////////////////////////////////

// IMAGES


//public void imageMode(int mode)


//public void image(PImage image, float x, float y)


//public void image(PImage image, float x, float y, float c, float d)


//public void image(PImage image,
// float a, float b, float c, float d,
// int u1, int v1, int u2, int v2)


//protected void imageImpl(PImage image,
// float x1, float y1, float x2, float y2,
// int u1, int v1, int u2, int v2)



//////////////////////////////////////////////////////////////

// SHAPE


//public void shapeMode(int mode)


//public void shape(PShape shape)


//public void shape(PShape shape, float x, float y)


//public void shape(PShape shape, float x, float y, float c, float d)



//////////////////////////////////////////////////////////////

// TEXT SETTINGS

// Only textModeCheck overridden from PGraphics, no textAlign, textAscent,
// textDescent, textFont, textLeading, textMode, textSize, textWidth


protected boolean textModeCheck(int mode) {
return (textMode == MODEL) || (textMode == SCREEN);
}



//////////////////////////////////////////////////////////////

// TEXT

// None of the variations of text() are overridden from PGraphics.



//////////////////////////////////////////////////////////////

// TEXT IMPL

// Not even the text drawing implementation stuff is overridden.



//////////////////////////////////////////////////////////////

// MATRIX STACK


public void pushMatrix() {
if (matrixMode == PROJECTION) {
if (pmatrixStackDepth == MATRIX_STACK_DEPTH) {
throw new RuntimeException(ERROR_PUSHMATRIX_OVERFLOW);
}
projection.get(pmatrixStack[pmatrixStackDepth]);
pmatrixStackDepth++;
} else {
if (matrixStackDepth == MATRIX_STACK_DEPTH) {
throw new RuntimeException(ERROR_PUSHMATRIX_OVERFLOW);
}
modelview.get(matrixStack[matrixStackDepth]);
modelviewInv.get(matrixInvStack[matrixStackDepth]);
matrixStackDepth++;
}
}


public void popMatrix() {
if (matrixMode == PROJECTION) {
if (pmatrixStackDepth == 0) {
throw new RuntimeException(ERROR_PUSHMATRIX_UNDERFLOW);
}
pmatrixStackDepth--;
projection.set(pmatrixStack[pmatrixStackDepth]);
} else {
if (matrixStackDepth == 0) {
throw new RuntimeException(ERROR_PUSHMATRIX_UNDERFLOW);
}
matrixStackDepth--;
modelview.set(matrixStack[matrixStackDepth]);
modelviewInv.set(matrixInvStack[matrixStackDepth]);
}
}


//////////////////////////////////////////////////////////////

// MATRIX TRANSFORMATIONS


public void translate(float tx, float ty) {
translate(tx, ty, 0);
}


public void translate(float tx, float ty, float tz) {
if (matrixMode == PROJECTION) {
projection.translate(tx, ty, tz);
} else {
forwardTransform.translate(tx, ty, tz);
reverseTransform.invTranslate(tx, ty, tz);
}
}


/**
* Two dimensional rotation. Same as rotateZ (this is identical
* to a 3D rotation along the z-axis) but included for clarity --
* it'd be weird for people drawing 2D graphics to be using rotateZ.
* And they might kick our a-- for the confusion.
*/
public void rotate(float angle) {
rotateZ(angle);
}


public void rotateX(float angle) {
if (matrixMode == PROJECTION) {
projection.rotateX(angle);
} else {
forwardTransform.rotateX(angle);
reverseTransform.invRotateX(angle);
}
}


public void rotateY(float angle) {
if (matrixMode == PROJECTION) {
projection.rotateY(angle);
} else {
forwardTransform.rotateY(angle);
reverseTransform.invRotateY(angle);
}
}


public void rotateZ(float angle) {
if (matrixMode == PROJECTION) {
projection.rotateZ(angle);
} else {
forwardTransform.rotateZ(angle);
reverseTransform.invRotateZ(angle);
}
}


/**
* Rotate around an arbitrary vector, similar to glRotate(),
* except that it takes radians (instead of degrees).
*/
public void rotate(float angle, float v0, float v1, float v2) {
if (matrixMode == PROJECTION) {
projection.rotate(angle, v0, v1, v2);
} else {
forwardTransform.rotate(angle, v0, v1, v2);
reverseTransform.invRotate(angle, v0, v1, v2);
}
}


/**
* Same as scale(s, s, s).
*/
public void scale(float s) {
scale(s, s, s);
}


/**
* Same as scale(sx, sy, 1).
*/
public void scale(float sx, float sy) {
scale(sx, sy, 1);
}


/**
* Scale in three dimensions.
*/
public void scale(float x, float y, float z) {
if (matrixMode == PROJECTION) {
projection.scale(x, y, z);
} else {
forwardTransform.scale(x, y, z);
reverseTransform.invScale(x, y, z);
}
}


public void shearX(float angle) {
float t = (float) Math.tan(angle);
applyMatrix(1, t, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
}


public void shearY(float angle) {
float t = (float) Math.tan(angle);
applyMatrix(1, 0, 0, 0,
t, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
}



//////////////////////////////////////////////////////////////

// MATRIX MORE!


public void resetMatrix() {
if (matrixMode == PROJECTION) {
projection.reset();
} else {
forwardTransform.reset();
reverseTransform.reset();
}
}


public void applyMatrix(PMatrix2D source) {
applyMatrix(source.m00, source.m01, source.m02,
source.m10, source.m11, source.m12);
}


public void applyMatrix(float n00, float n01, float n02,
float n10, float n11, float n12) {
applyMatrix(n00, n01, n02, 0,
n10, n11, n12, 0,
0, 0, 1, 0,
0, 0, 0, 1);
}


public void applyMatrix(PMatrix3D source) {
applyMatrix(source.m00, source.m01, source.m02, source.m03,
source.m10, source.m11, source.m12, source.m13,
source.m20, source.m21, source.m22, source.m23,
source.m30, source.m31, source.m32, source.m33);
}


/**
* Apply a 4x4 transformation matrix. Same as glMultMatrix().
* This call will be slow because it will try to calculate the
* inverse of the transform. So avoid it whenever possible.
*/
public void applyMatrix(float n00, float n01, float n02, float n03,
float n10, float n11, float n12, float n13,
float n20, float n21, float n22, float n23,
float n30, float n31, float n32, float n33) {
if (matrixMode == PROJECTION) {
projection.apply(n00, n01, n02, n03,
n10, n11, n12, n13,
n20, n21, n22, n23,
n30, n31, n32, n33);
} else {
forwardTransform.apply(n00, n01, n02, n03,
n10, n11, n12, n13,
n20, n21, n22, n23,
n30, n31, n32, n33);

reverseTransform.invApply(n00, n01, n02, n03,
n10, n11, n12, n13,
n20, n21, n22, n23,
n30, n31, n32, n33);
}
}



//////////////////////////////////////////////////////////////

// MATRIX GET/SET/PRINT


public PMatrix getMatrix() {
if (matrixMode == PROJECTION) {
return projection.get();
} else {
return modelview.get();
}
}


//public PMatrix2D getMatrix(PMatrix2D target)


public PMatrix3D getMatrix(PMatrix3D target) {
if (target == null) {
target = new PMatrix3D();
}
if (matrixMode == PROJECTION) {
target.set(projection);
} else {
target.set(modelview);
}
return target;
}


//public void setMatrix(PMatrix source)


public void setMatrix(PMatrix2D source) {
// not efficient, but at least handles the inverse stuff.
resetMatrix();
applyMatrix(source);
}


/**
* Set the current transformation to the contents of the specified source.
*/
public void setMatrix(PMatrix3D source) {
// not efficient, but at least handles the inverse stuff.
resetMatrix();
applyMatrix(source);
}


/**
* Print the current model (or "transformation") matrix.
*/
public void printMatrix() {
if (matrixMode == PROJECTION) {
projection.print();
} else {
modelview.print();
}
}


/*
* This function checks if the modelview matrix is set up to likely be
* drawing in 2D. It merely checks if the non-translational piece of the
* matrix is unity. If this is to be used, it should be coupled with a
* check that the raw vertex coordinates lie in the z=0 plane.
* Mainly useful for applying sub-pixel shifts to avoid 2d artifacts
* in the screen plane.
* Added by ewjordan 6/13/07
*
* TODO need to invert the logic here so that we can simply return
* the value, rather than calculating true/false and returning it.
*/
/*
private boolean drawing2D() {
if (modelview.m00 != 1.0f ||
modelview.m11 != 1.0f ||
modelview.m22 != 1.0f || // check scale
modelview.m01 != 0.0f ||
modelview.m02 != 0.0f || // check rotational pieces
modelview.m10 != 0.0f ||
modelview.m12 != 0.0f ||
modelview.m20 != 0.0f ||
modelview.m21 != 0.0f ||
!((camera.m23-modelview.m23) <= EPSILON &&
(camera.m23-modelview.m23) >= -EPSILON)) { // check for z-translation
// Something about the modelview matrix indicates 3d drawing
// (or rotated 2d, in which case 2d subpixel fixes probably aren't needed)
return false;
} else {
//The matrix is mapping z=0 vertices to the screen plane,
// which means it's likely that 2D drawing is happening.
return true;
}
}
*/



//////////////////////////////////////////////////////////////

// CAMERA


/**
* Set matrix mode to the camera matrix (instead of the current
* transformation matrix). This means applyMatrix, resetMatrix, etc.
* will affect the camera.
* <P>
* Note that the camera matrix is *not* the perspective matrix,
* it is in front of the modelview matrix (hence the name "model"
* and "view" for that matrix).
* <P>
* beginCamera() specifies that all coordinate transforms until endCamera()
* should be pre-applied in inverse to the camera transform matrix.
* Note that this is only challenging when a user specifies an arbitrary
* matrix with applyMatrix(). Then that matrix will need to be inverted,
* which may not be possible. But take heart, if a user is applying a
* non-invertible matrix to the camera transform, then he is clearly
* up to no good, and we can wash our hands of those bad intentions.
* <P>
* begin/endCamera clauses do not automatically reset the camera transform
* matrix. That's because we set up a nice default camera transform int
* setup(), and we expect it to hold through draw(). So we don't reset
* the camera transform matrix at the top of draw(). That means that an
* innocuous-looking clause like
* <PRE>
* beginCamera();
* translate(0, 0, 10);
* endCamera();
* </PRE>
* at the top of draw(), will result in a runaway camera that shoots
* infinitely out of the screen over time. In order to prevent this,
* it is necessary to call some function that does a hard reset of the
* camera transform matrix inside of begin/endCamera. Two options are
* <PRE>
* camera(); // sets up the nice default camera transform
* resetMatrix(); // sets up the identity camera transform
* </PRE>
* So to rotate a camera a constant amount, you might try
* <PRE>
* beginCamera();
* camera();
* rotateY(PI/8);
* endCamera();
* </PRE>
*/
public void beginCamera() {
if (manipulatingCamera) {
throw new RuntimeException("beginCamera() cannot be called again " +
"before endCamera()");
} else {
manipulatingCamera = true;
forwardTransform = cameraInv;
reverseTransform = camera;
}
}


/**
* Record the current settings into the camera matrix, and set
* the matrix mode back to the current transformation matrix.
* <P>
* Note that this will destroy any settings to scale(), translate(),
* or whatever, because the final camera matrix will be copied
* (not multiplied) into the modelview.
*/
public void endCamera() {
if (!manipulatingCamera) {
throw new RuntimeException("Cannot call endCamera() " +
"without first calling beginCamera()");
}
// reset the modelview to use this new camera matrix
modelview.set(camera);
modelviewInv.set(cameraInv);

// set matrix mode back to modelview
forwardTransform = modelview;
reverseTransform = modelviewInv;

// all done
manipulatingCamera = false;
}


/**
* Set camera to the default settings.
* <P>
* Processing camera behavior:
* <P>
* Camera behavior can be split into two separate components, camera
* transformation, and projection. The transformation corresponds to the
* physical location, orientation, and scale of the camera. In a physical
* camera metaphor, this is what can manipulated by handling the camera
* body (with the exception of scale, which doesn't really have a physcial
* analog). The projection corresponds to what can be changed by
* manipulating the lens.
* <P>
* We maintain separate matrices to represent the camera transform and
* projection. An important distinction between the two is that the camera
* transform should be invertible, where the projection matrix should not,
* since it serves to map three dimensions to two. It is possible to bake
* the two matrices into a single one just by multiplying them together,
* but it isn't a good idea, since lighting, z-ordering, and z-buffering
* all demand a true camera z coordinate after modelview and camera
* transforms have been applied but before projection. If the camera
* transform and projection are combined there is no way to recover a
* good camera-space z-coordinate from a model coordinate.
* <P>
* Fortunately, there are no functions that manipulate both camera
* transformation and projection.
* <P>
* camera() sets the camera position, orientation, and center of the scene.
* It replaces the camera transform with a new one. This is different from
* gluLookAt(), but I think the only reason that GLU's lookat doesn't fully
* replace the camera matrix with the new one, but instead multiplies it,
* is that GL doesn't enforce the separation of camera transform and
* projection, so it wouldn't be safe (you'd probably stomp your projection).
* <P>
* The transformation functions are the same ones used to manipulate the
* modelview matrix (scale, translate, rotate, etc.). But they are bracketed
* with beginCamera(), endCamera() to indicate that they should apply
* (in inverse), to the camera transformation matrix.
* <P>
* This differs considerably from camera transformation in OpenGL.
* OpenGL only lets you say, apply everything from here out to the
* projection or modelview matrix. This makes it very hard to treat camera
* manipulation as if it were a physical camera. Imagine that you want to
* move your camera 100 units forward. In OpenGL, you need to apply the
* inverse of that transformation or else you'll move your scene 100 units
* forward--whether or not you've specified modelview or projection matrix.
* Remember they're just multiplied by model coods one after another.
* So in order to treat a camera like a physical camera, it is necessary
* to pre-apply inverse transforms to a matrix that will be applied to model
* coordinates. OpenGL provides nothing of this sort, but Processing does!
* This is the camera transform matrix.
*/
public void camera() {
camera(cameraX, cameraY, cameraZ,
cameraX, cameraY, 0,
0, 1, 0);
}


/**
* More flexible method for dealing with camera().
* <P>
* The actual call is like gluLookat. Here's the real skinny on
* what does what:
* <PRE>
* camera(); or
* camera(ex, ey, ez, cx, cy, cz, ux, uy, uz);
* </PRE>
* do not need to be called from with beginCamera();/endCamera();
* That's because they always apply to the camera transformation,
* and they always totally replace it. That means that any coordinate
* transforms done before camera(); in draw() will be wiped out.
* It also means that camera() always operates in untransformed world
* coordinates. Therefore it is always redundant to call resetMatrix();
* before camera(); This isn't technically true of gluLookat, but it's
* pretty much how it's used.
* <P>
* Now, beginCamera(); and endCamera(); are useful if you want to move
* the camera around using transforms like translate(), etc. They will
* wipe out any coordinate system transforms that occur before them in
* draw(), but they will not automatically wipe out the camera transform.
* This means that they should be at the top of draw(). It also means
* that the following:
* <PRE>
* beginCamera();
* rotateY(PI/8);
* endCamera();
* </PRE>
* will result in a camera that spins without stopping. If you want to
* just rotate a small constant amount, try this:
* <PRE>
* beginCamera();
* camera(); // sets up the default view
* rotateY(PI/8);
* endCamera();
* </PRE>
* That will rotate a little off of the default view. Note that this
* is entirely equivalent to
* <PRE>
* camera(); // sets up the default view
* beginCamera();
* rotateY(PI/8);
* endCamera();
* </PRE>
* because camera() doesn't care whether or not it's inside a
* begin/end clause. Basically it's safe to use camera() or
* camera(ex, ey, ez, cx, cy, cz, ux, uy, uz) as naked calls because
* they do all the matrix resetting automatically.
*/
public void camera(float eyeX, float eyeY, float eyeZ,
float centerX, float centerY, float centerZ,
float upX, float upY, float upZ) {
float z0 = eyeX - centerX;
float z1 = eyeY - centerY;
float z2 = eyeZ - centerZ;
float mag = sqrt(z0*z0 + z1*z1 + z2*z2);

if (mag != 0) {
z0 /= mag;
z1 /= mag;
z2 /= mag;
}

float y0 = upX;
float y1 = upY;
float y2 = upZ;

float x0 = y1*z2 - y2*z1;
float x1 = -y0*z2 + y2*z0;
float x2 = y0*z1 - y1*z0;

y0 = z1*x2 - z2*x1;
y1 = -z0*x2 + z2*x0;
y2 = z0*x1 - z1*x0;

mag = sqrt(x0*x0 + x1*x1 + x2*x2);
if (mag != 0) {
x0 /= mag;
x1 /= mag;
x2 /= mag;
}

mag = sqrt(y0*y0 + y1*y1 + y2*y2);
if (mag != 0) {
y0 /= mag;
y1 /= mag;
y2 /= mag;
}

// just does an apply to the main matrix,
// since that'll be copied out on endCamera
camera.set(x0, x1, x2, 0,
y0, y1, y2, 0,
z0, z1, z2, 0,
0, 0, 0, 1);
camera.translate(-eyeX, -eyeY, -eyeZ);

cameraInv.reset();
cameraInv.invApply(x0, x1, x2, 0,
y0, y1, y2, 0,
z0, z1, z2, 0,
0, 0, 0, 1);
cameraInv.translate(eyeX, eyeY, eyeZ);

modelview.set(camera);
modelviewInv.set(cameraInv);
}


/**
* Print the current camera matrix.
*/
public void printCamera() {
camera.print();
}


//////////////////////////////////////////////////////////////

// PROJECTION


/**
* Calls ortho() with the proper parameters for Processing's
* standard orthographic projection.
*/
public void ortho() {
ortho(0, width, 0, height, -10, 10);
}


/**
* Similar to gluOrtho(), but wipes out the current projection matrix.
* <P>
* Implementation partially based on Mesa's matrix.c.
*/
public void ortho(float left, float right,
float bottom, float top,
float near, float far) {
float x = 2.0f / (right - left);
float y = 2.0f / (top - bottom);
float z = -2.0f / (far - near);

float tx = -(right + left) / (right - left);
float ty = -(top + bottom) / (top - bottom);
float tz = -(far + near) / (far - near);

projection.set(x, 0, 0, tx,
0, y, 0, ty,
0, 0, z, tz,
0, 0, 0, 1);
updateProjection();

frustumMode = false;
}


/**
* Calls perspective() with Processing's standard coordinate projection.
* <P>
* Projection functions:
* <UL>
* <LI>frustrum()
* <LI>ortho()
* <LI>perspective()
* </UL>
* Each of these three functions completely replaces the projection
* matrix with a new one. They can be called inside setup(), and their
* effects will be felt inside draw(). At the top of draw(), the projection
* matrix is not reset. Therefore the last projection function to be
* called always dominates. On resize, the default projection is always
* established, which has perspective.
* <P>
* This behavior is pretty much familiar from OpenGL, except where
* functions replace matrices, rather than multiplying against the
* previous.
* <P>
*/
public void perspective() {
perspective(cameraFOV, cameraAspect, cameraNear, cameraFar);
}


/**
* Similar to gluPerspective(). Implementation based on Mesa's glu.c
*/
public void perspective(float fov, float aspect, float zNear, float zFar) {
//float ymax = zNear * tan(fovy * PI / 360.0f);
float ymax = zNear * (float) Math.tan(fov / 2);
float ymin = -ymax;

float xmin = ymin * aspect;
float xmax = ymax * aspect;

frustum(xmin, xmax, ymin, ymax, zNear, zFar);
}


/**
* Same as glFrustum(), except that it wipes out (rather than
* multiplies against) the current perspective matrix.
* <P>
* Implementation based on the explanation in the OpenGL blue book.
*/
public void frustum(float left, float right, float bottom,
float top, float znear, float zfar) {

leftScreen = left;
rightScreen = right;
bottomScreen = bottom;
topScreen = top;
nearPlane = znear;
frustumMode = true;

//System.out.println(projection);
projection.set((2*znear)/(right-left), 0, (right+left)/(right-left), 0,
0, (2*znear)/(top-bottom), (top+bottom)/(top-bottom), 0,
0, 0, -(zfar+znear)/(zfar-znear),-(2*zfar*znear)/(zfar-znear),
0, 0, -1, 0);
updateProjection();
}


/** Called after the 'projection' PMatrix3D has changed. */
protected void updateProjection() {
}


/**
* Print the current projection matrix.
*/
public void printProjection() {
projection.print();
}


public PMatrix getProjection() {
return projection.get();
}


public void matrixMode(int mode) {
if (mode == PROJECTION) {
matrixMode = PROJECTION;
} else if (matrixMode == MODELVIEW) {
matrixMode = MODELVIEW;
} else {
showWarning("Invalid matrix mode. Use PROJECTION or MODELVIEW");
}
}


//////////////////////////////////////////////////////////////

// SCREEN AND MODEL COORDS


public float screenX(float x, float y) {
return screenX(x, y, 0);
}


public float screenY(float x, float y) {
return screenY(x, y, 0);
}


public float screenX(float x, float y, float z) {
float ax =
modelview.m00*x + modelview.m01*y + modelview.m02*z + modelview.m03;
float ay =
modelview.m10*x + modelview.m11*y + modelview.m12*z + modelview.m13;
float az =
modelview.m20*x + modelview.m21*y + modelview.m22*z + modelview.m23;
float aw =
modelview.m30*x + modelview.m31*y + modelview.m32*z + modelview.m33;

float ox =
projection.m00*ax + projection.m01*ay +
projection.m02*az + projection.m03*aw;
float ow =
projection.m30*ax + projection.m31*ay +
projection.m32*az + projection.m33*aw;

if (ow != 0) ox /= ow;
return width * (1 + ox) / 2.0f;
}


public float screenY(float x, float y, float z) {
float ax =
modelview.m00*x + modelview.m01*y + modelview.m02*z + modelview.m03;
float ay =
modelview.m10*x + modelview.m11*y + modelview.m12*z + modelview.m13;
float az =
modelview.m20*x + modelview.m21*y + modelview.m22*z + modelview.m23;
float aw =
modelview.m30*x + modelview.m31*y + modelview.m32*z + modelview.m33;

float oy =
projection.m10*ax + projection.m11*ay +
projection.m12*az + projection.m13*aw;
float ow =
projection.m30*ax + projection.m31*ay +
projection.m32*az + projection.m33*aw;

if (ow != 0) oy /= ow;
return height * (1 + oy) / 2.0f;
}


public float screenZ(float x, float y, float z) {
float ax =
modelview.m00*x + modelview.m01*y + modelview.m02*z + modelview.m03;
float ay =
modelview.m10*x + modelview.m11*y + modelview.m12*z + modelview.m13;
float az =
modelview.m20*x + modelview.m21*y + modelview.m22*z + modelview.m23;
float aw =
modelview.m30*x + modelview.m31*y + modelview.m32*z + modelview.m33;

float oz =
projection.m20*ax + projection.m21*ay +
projection.m22*az + projection.m23*aw;
float ow =
projection.m30*ax + projection.m31*ay +
projection.m32*az + projection.m33*aw;

if (ow != 0) oz /= ow;
return (oz + 1) / 2.0f;
}


public float modelX(float x, float y, float z) {
float ax =
modelview.m00*x + modelview.m01*y + modelview.m02*z + modelview.m03;
float ay =
modelview.m10*x + modelview.m11*y + modelview.m12*z + modelview.m13;
float az =
modelview.m20*x + modelview.m21*y + modelview.m22*z + modelview.m23;
float aw =
modelview.m30*x + modelview.m31*y + modelview.m32*z + modelview.m33;

float ox =
cameraInv.m00*ax + cameraInv.m01*ay +
cameraInv.m02*az + cameraInv.m03*aw;
float ow =
cameraInv.m30*ax + cameraInv.m31*ay +
cameraInv.m32*az + cameraInv.m33*aw;

return (ow != 0) ? ox / ow : ox;
}


public float modelY(float x, float y, float z) {
float ax =
modelview.m00*x + modelview.m01*y + modelview.m02*z + modelview.m03;
float ay =
modelview.m10*x + modelview.m11*y + modelview.m12*z + modelview.m13;
float az =
modelview.m20*x + modelview.m21*y + modelview.m22*z + modelview.m23;
float aw =
modelview.m30*x + modelview.m31*y + modelview.m32*z + modelview.m33;

float oy =
cameraInv.m10*ax + cameraInv.m11*ay +
cameraInv.m12*az + cameraInv.m13*aw;
float ow =
cameraInv.m30*ax + cameraInv.m31*ay +
cameraInv.m32*az + cameraInv.m33*aw;

return (ow != 0) ? oy / ow : oy;
}


public float modelZ(float x, float y, float z) {
float ax =
modelview.m00*x + modelview.m01*y + modelview.m02*z + modelview.m03;
float ay =
modelview.m10*x + modelview.m11*y + modelview.m12*z + modelview.m13;
float az =
modelview.m20*x + modelview.m21*y + modelview.m22*z + modelview.m23;
float aw =
modelview.m30*x + modelview.m31*y + modelview.m32*z + modelview.m33;

float oz =
cameraInv.m20*ax + cameraInv.m21*ay +
cameraInv.m22*az + cameraInv.m23*aw;
float ow =
cameraInv.m30*ax + cameraInv.m31*ay +
cameraInv.m32*az + cameraInv.m33*aw;

return (ow != 0) ? oz / ow : oz;
}



//////////////////////////////////////////////////////////////

// STYLE

// pushStyle(), popStyle(), style() and getStyle() inherited.



//////////////////////////////////////////////////////////////

// STROKE CAP/JOIN/WEIGHT


// public void strokeWeight(float weight) {
// if (weight != DEFAULT_STROKE_WEIGHT) {
// showMethodWarning("strokeWeight");
// }
// }


public void strokeJoin(int join) {
if (join != DEFAULT_STROKE_JOIN) {
showMethodWarning("strokeJoin");
}
}


public void strokeCap(int cap) {
if (cap != DEFAULT_STROKE_CAP) {
showMethodWarning("strokeCap");
}
}



//////////////////////////////////////////////////////////////

// STROKE COLOR

// All methods inherited from PGraphics.



//////////////////////////////////////////////////////////////

// TINT COLOR

// All methods inherited from PGraphics.



//////////////////////////////////////////////////////////////

// FILL COLOR


protected void fillFromCalc() {
super.fillFromCalc();
ambientFromCalc();
}



//////////////////////////////////////////////////////////////

// MATERIAL PROPERTIES

// ambient, specular, shininess, and emissive all inherited.



//////////////////////////////////////////////////////////////

// LIGHTS


PVector lightPositionVec = new PVector();
PVector lightDirectionVec = new PVector();

/**
* Sets up an ambient and directional light.
* <PRE>
* The Lighting Skinny:
*
* The way lighting works is complicated enough that it's worth
* producing a document to describe it. Lighting calculations proceed
* pretty much exactly as described in the OpenGL red book.
*
* Light-affecting material properties:
*
* AMBIENT COLOR
* - multiplies by light's ambient component
* - for believability this should match diffuse color
*
* DIFFUSE COLOR
* - multiplies by light's diffuse component
*
* SPECULAR COLOR
* - multiplies by light's specular component
* - usually less colored than diffuse/ambient
*
* SHININESS
* - the concentration of specular effect
* - this should be set pretty high (20-50) to see really
* noticeable specularity
*
* EMISSIVE COLOR
* - constant additive color effect
*
* Light types:
*
* AMBIENT
* - one color
* - no specular color
* - no direction
* - may have falloff (constant, linear, and quadratic)
* - may have position (which matters in non-constant falloff case)
* - multiplies by a material's ambient reflection
*
* DIRECTIONAL
* - has diffuse color
* - has specular color
* - has direction
* - no position
* - no falloff
* - multiplies by a material's diffuse and specular reflections
*
* POINT
* - has diffuse color
* - has specular color
* - has position
* - no direction
* - may have falloff (constant, linear, and quadratic)
* - multiplies by a material's diffuse and specular reflections
*
* SPOT
* - has diffuse color
* - has specular color
* - has position
* - has direction
* - has cone angle (set to half the total cone angle)
* - has concentration value
* - may have falloff (constant, linear, and quadratic)
* - multiplies by a material's diffuse and specular reflections
*
* Normal modes:
*
* All of the primitives (rect, box, sphere, etc.) have their normals
* set nicely. During beginShape/endShape normals can be set by the user.
*
* AUTO-NORMAL
* - if no normal is set during the shape, we are in auto-normal mode
* - auto-normal calculates one normal per triangle (face-normal mode)
*
* SHAPE-NORMAL
* - if one normal is set during the shape, it will be used for
* all vertices
*
* VERTEX-NORMAL
* - if multiple normals are set, each normal applies to
* subsequent vertices
* - (except for the first one, which applies to previous
* and subsequent vertices)
*
* Efficiency consequences:
*
* There is a major efficiency consequence of position-dependent
* lighting calculations per vertex. (See below for determining
* whether lighting is vertex position-dependent.) If there is no
* position dependency then the only factors that affect the lighting
* contribution per vertex are its colors and its normal.
* There is a major efficiency win if
*
* 1) lighting is not position dependent
* 2) we are in AUTO-NORMAL or SHAPE-NORMAL mode
*
* because then we can calculate one lighting contribution per shape
* (SHAPE-NORMAL) or per triangle (AUTO-NORMAL) and simply multiply it
* into the vertex colors. The converse is our worst-case performance when
*
* 1) lighting is position dependent
* 2) we are in AUTO-NORMAL mode
*
* because then we must calculate lighting per-face * per-vertex.
* Each vertex has a different lighting contribution per face in
* which it appears. Yuck.
*
* Determining vertex position dependency:
*
* If any of the following factors are TRUE then lighting is
* vertex position dependent:
*
* 1) Any lights uses non-constant falloff
* 2) There are any point or spot lights
* 3) There is a light with specular color AND there is a
* material with specular color
*
* So worth noting is that default lighting (a no-falloff ambient
* and a directional without specularity) is not position-dependent.
* We should capitalize.
*
* Simon Greenwold, April 2005
* </PRE>
*/
public void lights() {
// need to make sure colorMode is RGB 255 here
int colorModeSaved = colorMode;
colorMode = RGB;

lightFalloff(1, 0, 0);
lightSpecular(0, 0, 0);

ambientLight(colorModeX * 0.5f,
colorModeY * 0.5f,
colorModeZ * 0.5f);
directionalLight(colorModeX * 0.5f,
colorModeY * 0.5f,
colorModeZ * 0.5f,
0, 0, -1);

colorMode = colorModeSaved;

lightingDependsOnVertexPosition = false;
}


/**
* Turn off all lights.
*/
public void noLights() {
// write any queued geometry, because lighting will be goofed after
flush();
// set the light count back to zero
lightCount = 0;
}


/**
* Add an ambient light based on the current color mode.
*/
public void ambientLight(float r, float g, float b) {
ambientLight(r, g, b, 0, 0, 0);
}


/**
* Add an ambient light based on the current color mode.
* This version includes an (x, y, z) position for situations
* where the falloff distance is used.
*/
public void ambientLight(float r, float g, float b,
float x, float y, float z) {
if (lightCount == MAX_LIGHTS) {
throw new RuntimeException("can only create " + MAX_LIGHTS + " lights");
}
colorCalc(r, g, b);
lightDiffuse[lightCount][0] = calcR;
lightDiffuse[lightCount][1] = calcG;
lightDiffuse[lightCount][2] = calcB;

lightType[lightCount] = AMBIENT;
lightFalloffConstant[lightCount] = currentLightFalloffConstant;
lightFalloffLinear[lightCount] = currentLightFalloffLinear;
lightFalloffQuadratic[lightCount] = currentLightFalloffQuadratic;
lightPosition(lightCount, x, y, z);
lightCount++;
//return lightCount-1;
}


public void directionalLight(float r, float g, float b,
float nx, float ny, float nz) {
if (lightCount == MAX_LIGHTS) {
throw new RuntimeException("can only create " + MAX_LIGHTS + " lights");
}
colorCalc(r, g, b);
lightDiffuse[lightCount][0] = calcR;
lightDiffuse[lightCount][1] = calcG;
lightDiffuse[lightCount][2] = calcB;

lightType[lightCount] = DIRECTIONAL;
lightFalloffConstant[lightCount] = currentLightFalloffConstant;
lightFalloffLinear[lightCount] = currentLightFalloffLinear;
lightFalloffQuadratic[lightCount] = currentLightFalloffQuadratic;
lightSpecular[lightCount][0] = currentLightSpecular[0];
lightSpecular[lightCount][1] = currentLightSpecular[1];
lightSpecular[lightCount][2] = currentLightSpecular[2];
lightDirection(lightCount, nx, ny, nz);
lightCount++;
}


public void pointLight(float r, float g, float b,
float x, float y, float z) {
if (lightCount == MAX_LIGHTS) {
throw new RuntimeException("can only create " + MAX_LIGHTS + " lights");
}
colorCalc(r, g, b);
lightDiffuse[lightCount][0] = calcR;
lightDiffuse[lightCount][1] = calcG;
lightDiffuse[lightCount][2] = calcB;

lightType[lightCount] = POINT;
lightFalloffConstant[lightCount] = currentLightFalloffConstant;
lightFalloffLinear[lightCount] = currentLightFalloffLinear;
lightFalloffQuadratic[lightCount] = currentLightFalloffQuadratic;
lightSpecular[lightCount][0] = currentLightSpecular[0];
lightSpecular[lightCount][1] = currentLightSpecular[1];
lightSpecular[lightCount][2] = currentLightSpecular[2];
lightPosition(lightCount, x, y, z);
lightCount++;

lightingDependsOnVertexPosition = true;
}


public void spotLight(float r, float g, float b,
float x, float y, float z,
float nx, float ny, float nz,
float angle, float concentration) {
if (lightCount == MAX_LIGHTS) {
throw new RuntimeException("can only create " + MAX_LIGHTS + " lights");
}
colorCalc(r, g, b);
lightDiffuse[lightCount][0] = calcR;
lightDiffuse[lightCount][1] = calcG;
lightDiffuse[lightCount][2] = calcB;

lightType[lightCount] = SPOT;
lightFalloffConstant[lightCount] = currentLightFalloffConstant;
lightFalloffLinear[lightCount] = currentLightFalloffLinear;
lightFalloffQuadratic[lightCount] = currentLightFalloffQuadratic;
lightSpecular[lightCount][0] = currentLightSpecular[0];
lightSpecular[lightCount][1] = currentLightSpecular[1];
lightSpecular[lightCount][2] = currentLightSpecular[2];
lightPosition(lightCount, x, y, z);
lightDirection(lightCount, nx, ny, nz);
lightSpotAngle[lightCount] = angle;
lightSpotAngleCos[lightCount] = Math.max(0, (float) Math.cos(angle));
lightSpotConcentration[lightCount] = concentration;
lightCount++;

lightingDependsOnVertexPosition = true;
}


/**
* Set the light falloff rates for the last light that was created.
* Default is lightFalloff(1, 0, 0).
*/
public void lightFalloff(float constant, float linear, float quadratic) {
currentLightFalloffConstant = constant;
currentLightFalloffLinear = linear;
currentLightFalloffQuadratic = quadratic;

lightingDependsOnVertexPosition = true;
}


/**
* Set the specular color of the last light created.
*/
public void lightSpecular(float x, float y, float z) {
colorCalc(x, y, z);
currentLightSpecular[0] = calcR;
currentLightSpecular[1] = calcG;
currentLightSpecular[2] = calcB;

lightingDependsOnVertexPosition = true;
}


/**
* internal function to set the light position
* based on the current modelview matrix.
*/
protected void lightPosition(int num, float x, float y, float z) {
lightPositionVec.set(x, y, z);
modelview.mult(lightPositionVec, lightPosition[num]);
/*
lightPosition[num][0] =
modelview.m00*x + modelview.m01*y + modelview.m02*z + modelview.m03;
lightPosition[num][1] =
modelview.m10*x + modelview.m11*y + modelview.m12*z + modelview.m13;
lightPosition[num][2] =
modelview.m20*x + modelview.m21*y + modelview.m22*z + modelview.m23;
*/
}


/**
* internal function to set the light direction
* based on the current modelview matrix.
*/
protected void lightDirection(int num, float x, float y, float z) {
lightNormal[num].set(modelviewInv.m00*x + modelviewInv.m10*y + modelviewInv.m20*z + modelviewInv.m30,
modelviewInv.m01*x + modelviewInv.m11*y + modelviewInv.m21*z + modelviewInv.m31,
modelviewInv.m02*x + modelviewInv.m12*y + modelviewInv.m22*z + modelviewInv.m32);
lightNormal[num].normalize();

/*
lightDirectionVec.set(x, y, z);
System.out.println("dir vec " + lightDirectionVec);
//modelviewInv.mult(lightDirectionVec, lightNormal[num]);
modelviewInv.cmult(lightDirectionVec, lightNormal[num]);
System.out.println("cmult vec " + lightNormal[num]);
lightNormal[num].normalize();
System.out.println("setting light direction " + lightNormal[num]);
*/

/*
// Multiply by inverse transpose.
lightNormal[num][0] =
modelviewInv.m00*x + modelviewInv.m10*y +
modelviewInv.m20*z + modelviewInv.m30;
lightNormal[num][1] =
modelviewInv.m01*x + modelviewInv.m11*y +
modelviewInv.m21*z + modelviewInv.m31;
lightNormal[num][2] =
modelviewInv.m02*x + modelviewInv.m12*y +
modelviewInv.m22*z + modelviewInv.m32;

float n = mag(lightNormal[num][0], lightNormal[num][1], lightNormal[num][2]);
if (n == 0 || n == 1) return;

lightNormal[num][0] /= n;
lightNormal[num][1] /= n;
lightNormal[num][2] /= n;
*/
}



//////////////////////////////////////////////////////////////

// BACKGROUND

// Base background() variations inherited from PGraphics.


protected void backgroundImpl(PImage image) {
System.arraycopy(image.pixels, 0, pixels, 0, pixels.length);
Arrays.fill(zbuffer, Float.MAX_VALUE);
}


/**
* Clear pixel buffer. With P3D and OPENGL, this also clears the zbuffer.
*/
protected void backgroundImpl() {
Arrays.fill(pixels, backgroundColor);
Arrays.fill(zbuffer, Float.MAX_VALUE);
}



//////////////////////////////////////////////////////////////

// COLOR MODE

// all colorMode() variations inherited from PGraphics.



//////////////////////////////////////////////////////////////

// COLOR CALCULATIONS

// protected colorCalc and colorCalcARGB inherited.



//////////////////////////////////////////////////////////////

// COLOR DATATYPE STUFFING

// final color() variations inherited.



//////////////////////////////////////////////////////////////

// COLOR DATATYPE EXTRACTION

// final methods alpha, red, green, blue,
// hue, saturation, and brightness all inherited.



//////////////////////////////////////////////////////////////

// COLOR DATATYPE INTERPOLATION

// both lerpColor variants inherited.



//////////////////////////////////////////////////////////////

// BEGIN/END RAW

// beginRaw, endRaw() both inherited.



//////////////////////////////////////////////////////////////

// WARNINGS and EXCEPTIONS

// showWarning and showException inherited.



//////////////////////////////////////////////////////////////

// RENDERER SUPPORT QUERIES


//public boolean displayable()


public boolean is2D() {
return false;
}


public boolean is3D() {
return true;
}



//////////////////////////////////////////////////////////////

// PIMAGE METHODS

// All these methods are inherited, because this render has a
// pixels[] array that can be accessed directly.

// getImage
// setCache, getCache, removeCache
// isModified, setModified
// loadPixels, updatePixels
// resize
// get, getImpl, set, setImpl
// mask
// filter
// copy
// blendColor, blend



//////////////////////////////////////////////////////////////

// MATH (internal use only)


private final float sqrt(float a) {
return (float) Math.sqrt(a);
}


private final float mag(float a, float b, float c) {
return (float) Math.sqrt(a*a + b*b + c*c);
}


private final float clamp(float a) {
return (a < 1) ? a : 1;
}


private final float abs(float a) {
return (a < 0) ? -a : a;
}


private float dot(float ax, float ay, float az,
float bx, float by, float bz) {
return ax*bx + ay*by + az*bz;
}


/*
private final void cross(float a0, float a1, float a2,
float b0, float b1, float b2,
float[] out) {
out[0] = a1*b2 - a2*b1;
out[1] = a2*b0 - a0*b2;
out[2] = a0*b1 - a1*b0;
}
*/


private final void cross(float a0, float a1, float a2,
float b0, float b1, float b2,
PVector out) {
out.x = a1*b2 - a2*b1;
out.y = a2*b0 - a0*b2;
out.z = a0*b1 - a1*b0;
}


/*
private final void cross(float[] a, float[] b, float[] out) {
out[0] = a[1]*b[2] - a[2]*b[1];
out[1] = a[2]*b[0] - a[0]*b[2];
out[2] = a[0]*b[1] - a[1]*b[0];
}
*/
}

Change log

r7672 by andres.colubri on Feb 08, 2011   Diff
Matrix returned by getMatrix, printMatrix
depends on selected matrix mode
Go to: 

Older revisions

r7671 by andres.colubri on Feb 07, 2011   Diff
Using matrixMode() in P3D
r7647 by andres.colubri on Jan 31, 2011   Diff
Merged-in the changes in the core
needed for the new opengl renderer
r7133 by f...@processing.org on Jul 18, 2010   Diff
change skewX/Y to shearX/Y
All revisions of this file

File info

Size: 134729 bytes, 4476 lines
Powered by Google Project Hosting