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
|
// Shave and a Haircut
// (c) 2019 Epic Games
// US Patent 6720962
//Qt headers must be included before any others !!!
#include <QtCore/QEvent>
#include <QtCore/QElapsedTimer>
#include <QtGui/QMouseEvent>
#include <QtGui/QTabletEvent>
#if QT_VERSION < 0x050000
# include <QtGui/QApplication>
# include <QtGui/QWidget>
#else
# include <QtWidgets/QApplication>
# include <QtWidgets/QWidget>
#endif
#include "shaveIO.h"
#include <map>
#include <vector>
#include <maya/MArgDatabase.h>
#include <maya/MArgList.h>
#include <maya/MDataBlock.h>
#include <maya/MFloatMatrix.h>
#include <maya/MFloatPoint.h>
#include <maya/MFloatPointArray.h>
#include <maya/MFnSet.h>
#include <maya/MFnSubd.h>
#include <maya/MFnSubdNames.h>
#include <maya/MGlobal.h>
#include <maya/MIntArray.h>
#include <maya/MPlugArray.h>
#include <maya/MRenderUtil.h>
#include <maya/MSyntax.h>
#include <maya/MUint64Array.h>
#include <maya/MItMeshVertex.h>
#include "shaveHairShape.h"
#include "ShavePerVertTexInfo.h"
#include "shaveRender.h"
#include "shaveSDK.h"
#include "shaveTextureStore.h"
#include "shaveUtil.h"
#include "shaveHairUI.h"
namespace
{
typedef struct
{
int pntid;
float u;
float v;
} VertUVInfo;
struct compMString
{
bool operator()(const MString s1, const MString s2) const
{
return (strcmp(s1.asChar(), s2.asChar()) < 0);
}
};
typedef std::vector<VertUVInfo> FaceUVInfo;
typedef std::vector<FaceUVInfo> SurfaceUVInfo;
typedef std::vector<SurfaceUVInfo*> UVSetUVInfo;
typedef std::map<MString, UVSetUVInfo*, compMString> UVInfoCache;
};
static SurfaceUVInfo* cacheUVs(
shaveHairShape* sNode,
unsigned int surfIdx,
const MString& uvSet,
UVInfoCache& uvInfoCache
);
static unsigned int* createFaceIDMap(const MIntArray& startIndices);
static void makeTempColourConnections(
const MObject& shavenode, MDGModifier& dgMod
);
#define McheckErr(stat,m) \
if ( MS::kSuccess != stat ) { \
cerr << "ERROR: " << m << endl; \
}
static bool buildingLookups = false;
static int nodeCount = 0;
static int* texIDMap = NULL;
static unsigned texIDMapSize = 0;
static NODETEXINFO* texInfoLookup = NULL;
static unsigned texInfoLookupSize = 0;
static std::map<int, ShavePerVertTexInfo*> vertTexInfoMap;
bool IsBuildingLookups()
{
return buildingLookups;
}
// Some of the Shave parameters apply to growth vertices (i.e. for use by
// guides) while all the others apply to hairs.
static unsigned int vertParams[] = { 8, 21, 40 };
static const unsigned int numVertParams = sizeof(vertParams) / sizeof(unsigned int);
MObjectArray standinMeshes;
MDagPathArray standinMeshOrigSurfaces;
static inline void getHairUV(
const SurfaceUVInfo& surfUVInfo,
const unsigned int polyIdx,
const int baryPointIDs[3],
const float baryWeights[3],
float& u,
float& v
)
{
u = v = 0.0f;
if (polyIdx < surfUVInfo.size())
{
const FaceUVInfo& faceUVInfo = surfUVInfo[polyIdx];
size_t numVerts = faceUVInfo.size();
for (unsigned int i = 0; i < 3; ++i)
{
// faceUVInfo is a sparse array of VertUVInfo, with one for
// each vertex of this poly. So we have to run through them to
// find the one which has the same point ID as the bary coord.
for (size_t j = 0; j < numVerts; ++j)
{
if (faceUVInfo[j].pntid == baryPointIDs[i])
{
u += faceUVInfo[j].u * baryWeights[i];
v += faceUVInfo[j].v * baryWeights[i];
break;
}
}
}
}
}
// This evaluates the hair-root textures. These are needed during
// rendering and export, but not during Live Mode.
void initTexInfoLookup2(
const MObjectArray& shaveHairShapes,
MString meshUVSet,
bool verbose,
bool displayHairsOnly,
MObject onlyThis
)
{
//if (verbose) cerr << endl << "Beginning texture pre-process." << endl;
#ifdef DO_PROFILE
if(!Profile::GetDiagFile())
Profile::ProfileStart(NULL);
Profile::ProfileDump("initTexInfoLookup2", NULL);
#endif
//
// There is a bit of recursion here which we need to be careful with.
// To build the texture lookups, we'll need to get the positions of
// each hair from Shave using either SHAVEmake_a_curve() or
// SHAVEmake_a_curveROOT().
//
// Both of those functions will call the SHAVEapply_texture() callback
// to try and fill in any textured attributes for the hair.
//
// SHAVEapply_texture() will in turn call getTexInfoLookup() to
// retrieve the shaveHairShape's texture information -- which is exactly
// what we're in the process of building.
//
// To break the cycle, we set a flag which tells getTexInfoLookup() and
// getVertInfoLookup() to return null pointers until we're done.
//
buildingLookups = true;
MStatus status;
shaveHairShape* onlyThisShape = NULL;
if (onlyThis != MObject::kNullObj)
{
MFnDependencyNode shaveDependNode(onlyThis);
onlyThisShape = (shaveHairShape*) shaveDependNode.userNode (&status);
}
nodeCount = (int)shaveHairShapes.length();
unsigned int numShaveIDs = (unsigned)(shaveHairShape::getMaxShaveID() + 1);
// We can only reuse the existing texInfoLookup and texIDMap arrays if
// we're initializing a single hair node (i.e. 'onlyThisShape' is not
// NULL) and the sizes of the arrays haven't changed. Otherwise we have to
// blow the old arrays away and make new ones.
//
bool needNewArrays = ( (onlyThisShape == NULL)
|| (texIDMapSize != numShaveIDs)
|| (nodeCount != texInfoLookupSize)
|| (texInfoLookup == NULL) );
if (needNewArrays) {
freeTextureLookup();
}
// if we have nodes (we always should if we get here) then initialize
// the lookup structures.
if (nodeCount > 0)
{
//
// Allocate the top (node) level of the texture tables.
//
if (needNewArrays)
{
texInfoLookupSize = nodeCount;
texInfoLookup = (NODETEXINFO*)malloc(texInfoLookupSize*sizeof(NODETEXINFO));
memset(texInfoLookup, 0, texInfoLookupSize*sizeof(NODETEXINFO));
// We need a lookup table to translate Shave node IDs into indices
// into our texture tables.
//
texIDMapSize = numShaveIDs;
texIDMap = new int[texIDMapSize];
for (int i = 0; i < (int)texIDMapSize; i++)
texIDMap[i] = -1;
}
if (texInfoLookup == NULL)
{
cerr << "ERROR- COULD NOT ALLOCATE MEMORY FOR TEXTURE LOOKUP"
<< " STRUCTURES, PROCEEDING W/O TEXTURES." << endl;
}
else
{
for (int node = 0; node < nodeCount; node++)
{
// get the shaveHairShape;
MFnDependencyNode shaveDependNode(shaveHairShapes[node]);
shaveHairShape* sNode = (shaveHairShape*) shaveDependNode.userNode (&status);
//printf("02 - hasPendingEvents: %s\n",QCoreApplication::hasPendingEvents()?"yes":"no");fflush(stdout);
if (needNewArrays || (onlyThisShape == sNode))
{
NODETEXINFO* nodeInfo = &texInfoLookup[node];
if (onlyThisShape != NULL)
freeTextureLookup(nodeInfo);
else
{
nodeInfo->displacement = NULL;
nodeInfo->textureLookup = NULL;
nodeInfo->u = NULL;
nodeInfo->v = NULL;
}
nodeInfo->node = sNode;
//
// Get Shave's internal SHAVENODE structure. Note that
// getting it this way will force the guides to update if
// the geometry has changed, e.g. because we're on a new
// frame.
//
SHAVENODE* hairNode = sNode->getHairNode();
int hairGroup = sNode->getHairGroup();
//
// Make sure that our parameters are all up to date with
// any changes the user may have made.
//
// %%% Shouldn't the 'getHairNode' call above already have
// taken care of that?
//
//sNode->updateParams();
//
// Add the node's Shave ID to our map.
//
texIDMap[sNode->getShaveID()] = node;
//
// Let Shave know which node our calls will be referring
// to.
//
sNode->makeCurrent();
int numPasses = hairNode->shavep.passes[hairGroup];
nodeInfo->maxPasses = numPasses;
// our lookup will return a float, indexed by
// [pass][parm][hairid], here we allocate the [pass]
// level, now that we know what that number is.
nodeInfo->textureLookup = (float***)
// malloc(1*sizeof(float**));
malloc(numPasses*sizeof(float**));
// nodeInfo->u = new float*[1];
// nodeInfo->v = new float*[1];
nodeInfo->u = new float*[numPasses];
nodeInfo->v = new float*[numPasses];
// get the haircount for this node
if (displayHairsOnly)
nodeInfo->count = sNode->getNumDisplayHairs(false);
else
nodeInfo->count = hairNode->shavep.haircount[hairGroup]*numPasses;
// TODO: Why do we set these to 1 here?
numPasses=1;
nodeInfo->maxPasses=1;
// init the textured boolean to false;
for(int l = 0; l < SHAVE_NUM_PARAMS; l++)
{
nodeInfo->textured[l] = false;
nodeInfo->maxIndex[l] = -1;
}
// we've got a bit of work to do to find the uv we want.
// We'll need to get the selection list for this node, then
// we'll need to be able to determing which mesh the
// faceIndex belongs to, and then we'll need to correlate
// the pointids to a local face vertid, in order to get the
// UVs. Yuck.
// get the texture node plug. We'll use this array plug in
// the next loop to figure out where we need to do our
// lookups from, and which parms are carrying textures.
MDGModifier dgMod;
MIntArray connectedIndexes;
MPlug texPlug(shaveHairShapes[node], shaveHairShape::shaveTextureAttr);
MPlugArray texPlugArray;
MString currentNodeName = sNode->name();
unsigned int numGrowthSurfaces = 0;
//Only need to do this if the growth type is not Spline.
bool splineNode = (hairGroup == 4);
if (!splineNode)
{
short uTess;
short vTess;
MPlug plug(shaveHairShapes[node], shaveHairShape::surfTessU);
plug.getValue(uTess);
plug.setAttribute(shaveHairShape::surfTessV);
plug.getValue(vTess);
short sDept;
short sSamp;
MPlug plug2(shaveHairShapes[node], shaveHairShape::subdTessDept);
plug2.getValue(sDept);
plug2.setAttribute(shaveHairShape::subdTessSamp);
plug2.getValue(sSamp);
tesselateGrowthSurfaces(
sNode->exportData.meshes, uTess, vTess, sDept, sSamp
);
numGrowthSurfaces = sNode->exportData.meshes.length();
}
//printf("03 - hasPendingEvetns: %s\n",QCoreApplication::hasPendingEvents()?"yes":"no");fflush(stdout);
//
// Most texture connections are made directly to elements
// of the shaveHairShape's 'shaveTex' array attribute.
//
// However, a colour attribute is a compound, which can
// be connected either as a single compound plug, or as
// individual component plugs. Since the 'shaveTex' array
// doesn't allow for compounds, the shaveHairShape's colour
// attributes have their own separate connections.
//
// To make life easier on ourselves, we'd like everything
// to be in the 'shaveTex' array, so let's break the
// compounds up into their constituent parts and make
// temporary connections to the appropriate 'shaveTex'
// array elements.
//
makeTempColourConnections(shaveHairShapes[node], dgMod);
status = dgMod.doIt();
//printf("04 - hasPendingEvetns: %s\n",QCoreApplication::hasPendingEvents()?"yes":"no");fflush(stdout);
// now we need to make sure that the plug is actually
// connected to something.
// Thanks, Maya.
MPlug checkPlug;
unsigned int pi = 0;
for (pi = 0; pi < 60; pi++) // was 50
{
// Filter out those parameters which apply to growth
// vertices, not hairs.
unsigned int j;
for (j = 0; j < numVertParams; ++j)
if (pi == vertParams[j]) break;
if (j == numVertParams)
{
checkPlug = texPlug.elementByLogicalIndex(pi);
if(checkPlug.isConnected())
connectedIndexes.append(pi);
}
}
bool haveTextures = (connectedIndexes.length() > 0);
//printf("05 - hasPendingEvetns: %s\n",QCoreApplication::hasPendingEvents()?"yes":"no");fflush(stdout);
//
// This structure defines those few items of
// information from each hair's CURVEINFO which we need
// to keep track of.
//
struct HairBaryInfo
{
int UTpid;
int pntid[3];
float wgt[3];
} **hairInfo = NULL;
//
// If we have textures, then we'll be needing the hairInfo
// array.
//
if (haveTextures)
hairInfo = new struct HairBaryInfo*[numPasses];
UVInfoCache uvInfoCache;
nodeInfo->displacement = NULL;
// Cache the vertex UVs so that we can later generate
// hair-root UVs for use in SHAVEapply_texture().
unsigned int i;
SurfaceUVInfo** uvInfo = NULL;
if (!splineNode)
{
uvInfo = new SurfaceUVInfo*[numGrowthSurfaces];
for (i = 0; i < numGrowthSurfaces; i++)
{
uvInfo[i] = NULL;
MDagPath surf = sNode->exportData.meshes[i];
surf.extendToShape();
#ifdef EVALUATE_POSITION_FIXED
if (!surf.hasFn(MFn::kMesh))
#else
if (!surf.hasFn(MFn::kMesh)
&& !surf.hasFn(MFn::kSubdiv))
#endif
{
// For non-mesh surfaces Shave uses a mesh to
// approximate the surface. To get the hairs
// rooted at the correct point we calculate
// the 'displacement' (i.e. the difference
// between the approximate mesh and the real
// surface) for every hair root and cache it.
if (nodeInfo->displacement == NULL)
nodeInfo->displacement = new VERT*[numPasses];
// Non-meshes only have a single, default UV
// parameterization.
uvInfo[i] = cacheUVs(sNode, i, "", uvInfoCache);
}
else
{
uvInfo[i] = cacheUVs(sNode, i, meshUVSet, uvInfoCache);
}
}
}
//printf("06 - hasPendingEvetns: %s\n",QCoreApplication::hasPendingEvents()?"yes":"no");fflush(stdout);
// Cache the displacements and barycentric info for each hair.
CURVEINFO curveInfo;
unsigned int* faceToMesh = NULL;
int hairnum;
unsigned int meshIndex;
MDagPath meshNode;
int pass;
float u;
float v;
WFTYPE wt;
init_geomWF(&wt);
for (pass = 0; pass < numPasses; pass++)
{
if (nodeInfo->displacement)
nodeInfo->displacement[pass] = new VERT[nodeInfo->count];
nodeInfo->u[pass] = new float[nodeInfo->count];
nodeInfo->v[pass] = new float[nodeInfo->count];
if (haveTextures)
{
hairInfo[pass] =
new struct HairBaryInfo[nodeInfo->count];
}
for (hairnum = 0; hairnum < nodeInfo->count; hairnum++)
{
SHAVEmake_a_curveROOT(
pass,
hairGroup,
hairnum,
&wt,
&curveInfo
);
if (haveTextures)
{
// Save the barycentric info from curveInfo,
// which we'll need later on.
hairInfo[pass][hairnum].UTpid = curveInfo.UTpid;
for (int i = 0; i < 3; i++)
{
hairInfo[pass][hairnum].pntid[i] =
curveInfo.pntid[i];
hairInfo[pass][hairnum].wgt[i] =
curveInfo.wgt[i];
}
}
//
// Shave's hair number indices include killed
// hairs, so we have to leave room for them in our
// arrays. But we don't need to calculate their
// uvs.
//
if (wt.totalfaces > 0)
{
if (splineNode)
{
//
// Spline hair is a sheet, so there's a U
// direction but no V. (Keep in mind that
// we're talking about texture coords for
// the *base* of the hair here, not along
// the length of the hair itself.)
//
// Annoyingly, Maya never seems to
// consider a V value of 0.0 to actually be
// on the texture, so we have to nudge it
// in a bit.
//
nodeInfo->u[pass][hairnum] = curveInfo.wgt[0];
nodeInfo->v[pass][hairnum] = 0.0001f;
}
else
{
// Create a map from Shave's UTpid to the
// the mesh index, if it doesn't already
// exist.
if (faceToMesh == NULL)
faceToMesh = createFaceIDMap(sNode->exportData.startFaces);
// Get the index of the mesh that this
// hair is growing on.
meshIndex = faceToMesh[curveInfo.UTpid];
meshNode = sNode->exportData.meshes[meshIndex];
int normalizedPolyIdx = curveInfo.UTpid -
sNode->exportData.startFaces[meshIndex];
float u = 0.0f;
float v = 0.0f;
getHairUV(
*uvInfo[meshIndex],
normalizedPolyIdx,
curveInfo.pntid,
curveInfo.wgt,
u,
v
);
nodeInfo->u[pass][hairnum] = u;
nodeInfo->v[pass][hairnum] = v;
// Get this hair's displacement.
if (nodeInfo->displacement)
{
getDisplacement(
meshNode,
curveInfo,
wt.v[0],
u,
v,
sNode->exportData.startVerts[meshIndex],
nodeInfo->displacement[pass][hairnum]
);
}
}
}
else
{
if (nodeInfo->displacement)
{
nodeInfo->displacement[pass][hairnum].x = 0.0f;
nodeInfo->displacement[pass][hairnum].y = 0.0f;
nodeInfo->displacement[pass][hairnum].z = 0.0f;
}
}
}
}
//printf("07 - hasPendingEvetns: %s\n",QCoreApplication::hasPendingEvents()?"yes":"no");fflush(stdout);
//
// SHAVEmake_a_curveROOT will free up the old WFTYPE's info
// on each call, but we have to free up the the final one
// that it left us with.
//
free_geomWF(&wt);
//
// If we have any textured parameters then we need to
// iterate through all the passes and determine the texture
// information.
//
if (haveTextures)
{
if (verbose) cerr << "doing uv map preprocess... ";
MObjectArray textures;
unsigned int ti; // which texture?
unsigned int numTextures = 0;
//
// Get an array of all the textures being used by this
// shaveHairShape.
//
unsigned int i;
for(i = 0; i < connectedIndexes.length(); i++)
{
int parmIndex;
parmIndex = (int)connectedIndexes[i];
//
// Get the plug which is driving this parameter.
//
texPlug
.elementByLogicalIndex(parmIndex)
.connectedTo(texPlugArray, true, false);
//
// Grab its node.
//
MObject texture = texPlugArray[0].node();
//{
// MFnDependencyNode dFn(texture);
// printf("texture node name %s\n",dFn.name().asChar() );fflush(stdout);
//}
//
// If we haven't yet seen this one, add it to the
// array.
//
for (ti = 0; ti < numTextures; ti++)
if (texture == textures[ti]) break;
if (ti == numTextures)
{
textures.append(texture);
numTextures++;
}
}
//
// Build a UV map for each texture. Each map will have
// a single entry per hair, per pass.
//
HAIRUVS** textureUVMaps = new HAIRUVS*[numTextures];
struct HairBaryInfo* hair;
for (ti = 0; ti < numTextures; ti++)
{
textureUVMaps[ti] = new HAIRUVS[nodeInfo->maxPasses];
//
// Find the UV set used by each growth mesh for
// this texture.
//
MStringArray uvSets;
if (splineNode)
{
//
// UV mapping for spline hair is handled
// specially.
//
uvSets.append("");
}
else
{
MString uvSet;
for (unsigned int m = 0; m < numGrowthSurfaces; m++)
{
MDagPath objectPath =
sNode->exportData.meshes[m];
if (!objectPath.hasFn(MFn::kMesh))
{
//
// This growth object is not a mesh, so
// it doesn't support UV sets.
//
// We store a blank UV set name which
// will be an indicator later on to
// perform the default mapping for this
// growth object.
//
uvSet = "";
}
else
{
//
// We have a mesh.
//
// Get the UV set used by this mesh for
// this texture.
//
uvSet = sNode->getUVSet(
objectPath.node(),
textures[ti]
);
}
uvSets.append(uvSet);
// Cache the uvs for this UV set.
uvInfo[m] = cacheUVs(sNode, m, uvSet, uvInfoCache);
}
}
for (pass = 0;
pass < nodeInfo->maxPasses;
pass++)
{
textureUVMaps[ti][pass].us = new float[nodeInfo->count];
textureUVMaps[ti][pass].vs = new float[nodeInfo->count];
for (hairnum = 0; hairnum < nodeInfo->count; hairnum++)
{
hair = &hairInfo[pass][hairnum];
if (splineNode)
{
//
// Spline hair is a sheet, so there's a U
// direction but no V. (Keep in mind that
// we're talking about texture coords for
// the *base* of the hair here, not along
// the length of the hair itself.)
//
// Annoyingly, Maya never seems to
// consider a V value of 0.0 to
// actually be on the texture, so we
// have to nudge it in a bit.
//
u = hair->wgt[0];
v = 0.0001f;
}
else
{
// Create a map from Shave's UTpid to the
// the mesh index, if it doesn't already
// exist.
if (faceToMesh == NULL)
faceToMesh = createFaceIDMap(sNode->exportData.startFaces);
meshIndex = faceToMesh[hair->UTpid];
meshNode = sNode->exportData.meshes[meshIndex];
meshNode.extendToShape();
// If this growth surface is a not
// really a mesh (i.e. it's a NURBS or
// subd) then it will be using the
// default UV parameterization, which
// we have already stored in nodeInfo
// so we can grab the value from there
// rather than recalculating it here.
//
// Similarly, if it *is* a mesh and we're
// using the same uv set as was cached
// in nodeInfo, then just grab the
// values from there.
if (!meshNode.hasFn(MFn::kMesh)
|| (uvSets[meshIndex] == meshUVSet))
{
u = nodeInfo->u[pass][hairnum];
v = nodeInfo->v[pass][hairnum];
}
else
{
int normalizedPolyIdx = hair->UTpid -
sNode->exportData.startFaces[meshIndex];
u = 0.0f;
v = 0.0f;
getHairUV(
*uvInfo[meshIndex],
normalizedPolyIdx,
hair->pntid,
hair->wgt,
u,
v
);
}
}
textureUVMaps[ti][pass].us[hairnum] = u;
textureUVMaps[ti][pass].vs[hairnum] = v;
}
}
}
//printf("08 - hasPendingEvetns: %s\n",QCoreApplication::hasPendingEvents()?"yes":"no");fflush(stdout);
if (verbose)
{
cerr << "done." << endl;
cerr << "getting texture values from maya... " << endl;
}
int maxtex=100000;
for (pass = 0; pass < nodeInfo->maxPasses; pass++)
{
// malloc the top level of our lookup;
nodeInfo->textureLookup[pass] = (float**)
calloc(SHAVE_NUM_PARAMS, sizeof(float*));
MFloatArray uArray;
MFloatArray vArray;
MFloatPointArray pointArray;
MFloatPointArray pa;
VERT pos[3];
struct HairBaryInfo* hair;
// we need to to figure out the uv coords for each
// of the roots for this pass, and also the point
// locations for 3D texture evaluation.
MFloatMatrix camMatrix;
// if we store the lookups we have done then we
// don't need to do them again. This will save both
// time and memory for high density scenes.
MStringArray doneLookups;
MIntArray doneLookupsIndex;
for (pi = 0; pi < connectedIndexes.length(); pi++)
{
int ind;
int total;
total=nodeInfo->count;
int parmIndex = (int)connectedIndexes[pi];
texPlug
.elementByLogicalIndex(parmIndex)
.connectedTo(texPlugArray, true, false);
MObject texture = texPlugArray[0].node();
for (ti = 0; ti < numTextures; ti++)
if (textures[ti] == texture) break;
nodeInfo->textureLookup[pass][parmIndex]
= (float*) calloc(
total, sizeof(float)
);
for (ind=0;ind<nodeInfo->count;ind+=maxtex)
{
uArray.clear();
vArray.clear();
pointArray.clear();
int szz=total;
if (total>=maxtex) szz=total-ind;
if (szz>maxtex) szz=maxtex;
//fprintf (stdout,"ind = %d szz= %d\n",ind,szz);fflush(stdout);
for (hairnum = ind; hairnum < ind+szz; hairnum++)
{
hair = &hairInfo[pass][hairnum];
for (int k = 0; k < 3; k++)
{
pos[k] = sNode->memShaveObj
.v[hair->pntid[k]];
pos[k].x *= hair->wgt[k];
pos[k].y *= hair->wgt[k];
pos[k].z *= hair->wgt[k];
}
uArray.append(
textureUVMaps[ti][pass].us[hairnum]
);
vArray.append(
textureUVMaps[ti][pass].vs[hairnum]
);
pointArray.append(
pos[0].x+pos[1].x+pos[2].x,
pos[0].y+pos[1].y+pos[2].y,
pos[0].z+pos[1].z+pos[2].z
);
}
// set aside a little memory for our lookup results.
{
{
MFloatVectorArray vertColorArray;
MFloatVectorArray vertTranspArray;
//////debug//////
//MFloatVectorArray uTang;
//MFloatVectorArray vTang;
//MFloatArray filter;
//uTang.setLength(szz);
//vTang.setLength(szz);
//filter.setLength(szz);
//for(unsigned int k = 0; k < szz; k++)
//{
// uTang[k] = MFloatVector(1.0f,0.0f);
// vTang[k] = MFloatVector(0.0f,1.0f);
// filter[k] = 0.1f;
//}
/////////////////////////
//printf("texture name %s\n",texPlugArray[0].name().asChar() );fflush(stdout);
// lets get the lookup values from the maya
// shading engine.
MStatus shRes =
MRenderUtil::sampleShadingNetwork(
texPlugArray[0].name(), //shade node name
szz, //samples
false, //shadows
false, //reuse shad maps
camMatrix, //camMatrix
&pointArray, //points
&uArray, //u coords
&vArray, //v coords
NULL, //normals
&pointArray, //ref points
NULL /*&uTang*/, //u tang
NULL /*&vTang*/, //v tang
NULL /*&filter*/, //filter size
vertColorArray, //out color
vertTranspArray //out transp
);
//fprintf (stdout,"done with lookup\n");fflush(stdout);
// we need to copy the returned values into the
// lookup array our pointer references. also,
// we'll store the name and parm index of this
// lookup so we can reuse it if the opportunity
// presents itself.
nodeInfo->maxIndex[parmIndex]
= (int)total;
///////// debug ///////////////
//if(shRes != MStatus::kSuccess) printf(" MRenderUtil::sampleShadingNetwork failed with result %i\n",shRes);
//for(int k=0; k < (int)szz; k++)
//{
// printf("uv %f %f rgb %f %f %f t %f\n",uArray[k],vArray[k],vertColorArray[k].x,vertColorArray[k].y,vertColorArray[k].z,vertTranspArray[k]);
//}
//fflush(stdout);
///////////////////////////////
for(int k=0; k < (int)szz; k++)
{
nodeInfo->textureLookup[pass][parmIndex][k+ind]
= (float)vertColorArray[k].x;
// nodeInfo->textureLookup[pass][parmIndex][k]
// = (float)vertColorArray[k].x;
}
}
}
}
}
}
//printf("09 - hasPendingEvetns: %s\n",QCoreApplication::hasPendingEvents()?"yes":"no");fflush(stdout);
if (verbose)
{
cerr << "done." << endl;
cerr << "freeing cached curveinfos and uvlists..." << endl;
}
for (ti = 0; ti < numTextures; ti++)
{
for (pass = 0; pass < nodeInfo->maxPasses; pass++)
{
delete [] textureUVMaps[ti][pass].us;
delete [] textureUVMaps[ti][pass].vs;
}
delete [] textureUVMaps[ti];
}
delete [] textureUVMaps;
for (pass = 0; pass < nodeInfo->maxPasses; pass++)
delete [] hairInfo[pass];
delete [] hairInfo;
}
delete [] faceToMesh;
UVInfoCache::iterator setIter;
for (setIter = uvInfoCache.begin();
setIter != uvInfoCache.end();
++setIter)
{
UVSetUVInfo* uvSetUVInfo = (*setIter).second;
for (unsigned int ui = 0; ui < uvSetUVInfo->size(); ++ui)
delete (*uvSetUVInfo)[ui];
delete uvSetUVInfo;
}
//printf("10 - hasPendingEvetns: %s\n",QCoreApplication::hasPendingEvents()?"yes":"no");fflush(stdout);
uvInfoCache.clear();
// we can't set the textured member true until we've taken
// care of all the passes. Otherwise make a curve will fail
// trying to do a lookup.
for (pi = 0; pi < connectedIndexes.length(); pi++)
{
unsigned int pIndex = connectedIndexes[pi];
nodeInfo->textured[pIndex] = true;
}
// remove temporary connections that were made in the
// hypergraph. not strictly necesary, but keeps thing
// looking cleaner for the user.
dgMod.undoIt();
//
// Let the node know that its texture cache has changed.
//
MPlug plug(
shaveHairShapes[node],
shaveHairShape::textureCacheUpdatedAttr
);
plug.setValue(true);
} //end if(onlyThisShape != NULL && onlyThisShape == sNode)
} //end for node
}
}
//
// It's now safe to serve up the texture lookup tables.
//
buildingLookups = false;
#ifdef DO_PROFILE
Profile::ProfileDump("initTexInfoLookup2 - done", NULL);
#endif
}
// This evaluates the hair-root textures. These are needed during
// rendering and export, but not during Live Mode.
void initTexInfoLookup(
const MObjectArray& shaveHairShapes,
MString meshUVSet,
bool verbose,
bool displayHairsOnly
)
{
//if (verbose) cerr << endl << "Beginning texture pre-process." << endl;
//
// There is a bit of recursion here which we need to be careful with.
// To build the texture lookups, we'll need to get the positions of
// each hair from Shave using either SHAVEmake_a_curve() or
// SHAVEmake_a_curveROOT().
//
// Both of those functions will call the SHAVEapply_texture() callback
// to try and fill in any textured attributes for the hair.
//
// SHAVEapply_texture() will in turn call getTexInfoLookup() to
// retrieve the shaveHairShape's texture information -- which is exactly
// what we're in the process of building.
//
// To break the cycle, we set a flag which tells getTexInfoLookup() and
// getVertInfoLookup() to return null pointers until we're done.
//
buildingLookups = true;
freeTextureLookup();
shaveHairShape* sNode;
bool splineNode;
MStringArray result;
MStatus status;
// get our shaveHairShapes in a selection list
nodeCount = (int)shaveHairShapes.length();
// if we have nodes (we always should if we get here) then allocate
// space for the lookup structures.
if (nodeCount > 0)
{
//
// We need a lookup table to translate Shave node ID's into indices
// into our texture tables.
//
int i;
texIDMapSize = (unsigned)(shaveHairShape::getMaxShaveID() + 1);
texIDMap = new int[texIDMapSize];
for (i = 0; i < (int)texIDMapSize; i++)
texIDMap[i] = -1;
//
// Allocate the top (node) level of the texture tables.
//
texInfoLookup = (NODETEXINFO*)malloc(nodeCount*sizeof(NODETEXINFO));
if (texInfoLookup == NULL)
{
cerr << "ERROR- COULD NOT ALLOCATE MEMORY FOR TEXTURE LOOKUP"
<< " STRUCTURES, PROCEEDING W/O TEXTURES." << endl;
}
else
{
for (int node = 0; node < nodeCount; node++)
{
NODETEXINFO* nodeInfo = &texInfoLookup[node];
nodeInfo->displacement = NULL;
nodeInfo->textureLookup = NULL;
nodeInfo->u = NULL;
nodeInfo->v = NULL;
// get the shaveHairShape;
MFnDependencyNode shaveDependNode(shaveHairShapes[node]);
sNode = (shaveHairShape*) shaveDependNode.userNode (&status);
nodeInfo->node = sNode;
//
// Get Shave's internal SHAVENODE structure. Note that
// getting it this way will force the guides to update if
// the geometry has changed, e.g. because we're on a new
// frame.
//
SHAVENODE* hairNode = sNode->getHairNode();
int hairGroup = sNode->getHairGroup();
//
// Make sure that our parameters are all up to date with
// any changes the user may have made.
//
// %%% Shouldn't the 'getHairNode' call above already have
// taken care of that?
//
sNode->updateParams();
//
// Add the node's Shave ID to our map.
//
texIDMap[sNode->getShaveID()] = node;
//
// Let Shave know which node our calls will be referring
// to.
//
sNode->makeCurrent();
int numPasses = hairNode->shavep.passes[hairGroup];
nodeInfo->maxPasses = numPasses;
// our lookup will return a float, indexed by
// [pass][parm][hairid], here we allocate the [pass]
// level, now that we know what that number is.
nodeInfo->textureLookup = (float***)
// malloc(1*sizeof(float**));
malloc(numPasses*sizeof(float**));
// nodeInfo->u = new float*[1];
// nodeInfo->v = new float*[1];
nodeInfo->u = new float*[numPasses];
nodeInfo->v = new float*[numPasses];
// get the haircount for this node
if (displayHairsOnly)
nodeInfo->count = sNode->getNumDisplayHairs(false);
else
nodeInfo->count = hairNode->shavep.haircount[hairGroup]*numPasses;
numPasses=1;
nodeInfo->maxPasses=1;
// init the textured boolean to false;
for(int l = 0; l < SHAVE_NUM_PARAMS; l++)
{
nodeInfo->textured[l] = false;
nodeInfo->maxIndex[l] = -1;
}
// we've got a bit of work to do to find the uv we want.
// We'll need to get the selection list for this node, then
// we'll need to be able to determing which mesh the
// faceIndex belongs to, and then we'll need to correlate
// the pointids to a local face vertid, in order to get the
// UVs. Yuck.
// get the texture node plug. We'll use this array plug in
// the next loop to figure out where we need to do our
// lookups from, and which parms are carrying textures.
MDGModifier dgMod;
MIntArray connectedIndexes;
MPlug texPlug(shaveHairShapes[node], shaveHairShape::shaveTextureAttr);
MPlugArray texPlugArray;
MString currentNodeName = sNode->name();
unsigned int numGrowthSurfaces = 0;
//Only need to do this if the growth type is not Spline.
splineNode = (hairGroup == 4);
if (!splineNode)
{
short uTess;
short vTess;
MPlug plug(shaveHairShapes[node], shaveHairShape::surfTessU);
plug.getValue(uTess);
plug.setAttribute(shaveHairShape::surfTessV);
plug.getValue(vTess);
short sDept;
short sSamp;
MPlug plug2(shaveHairShapes[node], shaveHairShape::subdTessDept);
plug2.getValue(sDept);
plug2.setAttribute(shaveHairShape::subdTessSamp);
plug2.getValue(sSamp);
tesselateGrowthSurfaces(
sNode->exportData.meshes, uTess, vTess, sDept, sSamp
);
numGrowthSurfaces = sNode->exportData.meshes.length();
}
//
// Most texture connections are made directly to elements
// of the shaveHairShape's 'shaveTex' array attribute.
//
// However, a colour attribute is a compound, which can
// be connected either as a single compound plug, or as
// individual component plugs. Since the 'shaveTex' array
// doesn't allow for compounds, the shaveHairShape's colour
// attributes have their own separate connections.
//
// To make life easier on ourselves, we'd like everything
// to be in the 'shaveTex' array, so let's break the
// compounds up into their constituent parts and make
// temporary connections to the appropriate 'shaveTex'
// array elements.
//
makeTempColourConnections(shaveHairShapes[node], dgMod);
status = dgMod.doIt();
// now we need to make sure that the plug is actually
// connected to something.
// Thanks, Maya.
MPlug checkPlug;
unsigned int pi = 0;
for (pi = 0; pi < 60; pi++) // was 50
{
// Filter out those parameters which apply to growth
// vertices, not hairs.
unsigned int j;
for (j = 0; j < numVertParams; ++j)
if (pi == vertParams[j]) break;
if (j == numVertParams)
{
checkPlug = texPlug.elementByLogicalIndex(pi);
if(checkPlug.isConnected())
connectedIndexes.append(pi);
}
}
bool haveTextures = (connectedIndexes.length() > 0);
//
// This structure defines those few items of
// information from each hair's CURVEINFO which we need
// to keep track of.
//
struct HairBaryInfo
{
int UTpid;
int pntid[3];
float wgt[3];
} **hairInfo = NULL;
//
// If we have textures, then we'll be needing the hairInfo
// array.
//
if (haveTextures)
hairInfo = new struct HairBaryInfo*[numPasses];
UVInfoCache uvInfoCache;
nodeInfo->displacement = NULL;
// Cache the vertex UVs so that we can later generate
// hair-root UVs for use in SHAVEapply_texture().
unsigned int i;
SurfaceUVInfo** uvInfo = NULL;
if (!splineNode)
{
uvInfo = new SurfaceUVInfo*[numGrowthSurfaces];
for (i = 0; i < numGrowthSurfaces; i++)
{
uvInfo[i] = NULL;
MDagPath surf = sNode->exportData.meshes[i];
surf.extendToShape();
#ifdef EVALUATE_POSITION_FIXED
if (!surf.hasFn(MFn::kMesh))
#else
if (!surf.hasFn(MFn::kMesh)
&& !surf.hasFn(MFn::kSubdiv))
#endif
{
// For non-mesh surfaces Shave uses a mesh to
// approximate the surface. To get the hairs
// rooted at the correct point we calculate
// the 'displacement' (i.e. the difference
// between the approximate mesh and the real
// surface) for every hair root and cache it.
if (nodeInfo->displacement == NULL)
nodeInfo->displacement = new VERT*[numPasses];
// Non-meshes only have a single, default UV
// parameterization.
uvInfo[i] = cacheUVs(sNode, i, "", uvInfoCache);
}
else
{
uvInfo[i] = cacheUVs(sNode, i, meshUVSet, uvInfoCache);
}
}
}
// Cache the displacements and barycentric info for each hair.
CURVEINFO curveInfo;
unsigned int* faceToMesh = NULL;
int hairnum;
unsigned int meshIndex;
MDagPath meshNode;
int pass;
float u;
float v;
WFTYPE wt;
init_geomWF(&wt);
for (pass = 0; pass < numPasses; pass++)
{
if (nodeInfo->displacement)
nodeInfo->displacement[pass] = new VERT[nodeInfo->count];
nodeInfo->u[pass] = new float[nodeInfo->count];
nodeInfo->v[pass] = new float[nodeInfo->count];
if (haveTextures)
{
hairInfo[pass] =
new struct HairBaryInfo[nodeInfo->count];
}
for (hairnum = 0; hairnum < nodeInfo->count; hairnum++)
{
SHAVEmake_a_curveROOT(
pass,
hairGroup,
hairnum,
&wt,
&curveInfo
);
if (haveTextures)
{
// Save the barycentric info from curveInfo,
// which we'll need later on.
hairInfo[pass][hairnum].UTpid = curveInfo.UTpid;
for (int i = 0; i < 3; i++)
{
hairInfo[pass][hairnum].pntid[i] =
curveInfo.pntid[i];
hairInfo[pass][hairnum].wgt[i] =
curveInfo.wgt[i];
}
}
//
// Shave's hair number indices include killed
// hairs, so we have to leave room for them in our
// arrays. But we don't need to calculate their
// uvs.
//
if (wt.totalfaces > 0)
{
if (splineNode)
{
//
// Spline hair is a sheet, so there's a U
// direction but no V. (Keep in mind that
// we're talking about texture coords for
// the *base* of the hair here, not along
// the length of the hair itself.)
//
// Annoyingly, Maya never seems to
// consider a V value of 0.0 to actually be
// on the texture, so we have to nudge it
// in a bit.
//
nodeInfo->u[pass][hairnum] = curveInfo.wgt[0];
nodeInfo->v[pass][hairnum] = 0.0001f;
}
else
{
// Create a map from Shave's UTpid to the
// the mesh index, if it doesn't already
// exist.
if (faceToMesh == NULL)
faceToMesh = createFaceIDMap(sNode->exportData.startFaces);
// Get the index of the mesh that this
// hair is growing on.
meshIndex = faceToMesh[curveInfo.UTpid];
meshNode = sNode->exportData.meshes[meshIndex];
int normalizedPolyIdx = curveInfo.UTpid -
sNode->exportData.startFaces[meshIndex];
float u = 0.0f;
float v = 0.0f;
getHairUV(
*uvInfo[meshIndex],
normalizedPolyIdx,
curveInfo.pntid,
curveInfo.wgt,
u,
v
);
nodeInfo->u[pass][hairnum] = u;
nodeInfo->v[pass][hairnum] = v;
// Get this hair's displacement.
if (nodeInfo->displacement)
{
getDisplacement(
meshNode,
curveInfo,
wt.v[0],
u,
v,
sNode->exportData.startVerts[meshIndex],
nodeInfo->displacement[pass][hairnum]
);
}
}
}
else
{
if (nodeInfo->displacement)
{
nodeInfo->displacement[pass][hairnum].x = 0.0f;
nodeInfo->displacement[pass][hairnum].y = 0.0f;
nodeInfo->displacement[pass][hairnum].z = 0.0f;
}
}
}
}
//
// SHAVEmake_a_curveROOT will free up the old WFTYPE's info
// on each call, but we have to free up the the final one
// that it left us with.
//
free_geomWF(&wt);
//
// If we have any textured parameters then we need to
// iterate through all the passes and determine the texture
// information.
//
if (haveTextures)
{
if (verbose) cerr << "doing uv map preprocess... ";
MObjectArray textures;
unsigned int ti; // which texture?
unsigned int numTextures = 0;
//
// Get an array of all the textures being used by this
// shaveHairShape.
//
unsigned int i;
for(i = 0; i < connectedIndexes.length(); i++)
{
int parmIndex;
parmIndex = (int)connectedIndexes[i];
//
// Get the plug which is driving this parameter.
//
texPlug
.elementByLogicalIndex(parmIndex)
.connectedTo(texPlugArray, true, false);
//
// Grab its node.
//
MObject texture = texPlugArray[0].node();
//{
// MFnDependencyNode dFn(texture);
// printf("texture node name %s\n",dFn.name().asChar() );fflush(stdout);
//}
//
// If we haven't yet seen this one, add it to the
// array.
//
for (ti = 0; ti < numTextures; ti++)
if (texture == textures[ti]) break;
if (ti == numTextures)
{
textures.append(texture);
numTextures++;
}
}
//
// Build a UV map for each texture. Each map will have
// a single entry per hair, per pass.
//
HAIRUVS** textureUVMaps = new HAIRUVS*[numTextures];
struct HairBaryInfo* hair;
for (ti = 0; ti < numTextures; ti++)
{
textureUVMaps[ti] = new HAIRUVS[nodeInfo->maxPasses];
//
// Find the UV set used by each growth mesh for
// this texture.
//
MStringArray uvSets;
if (splineNode)
{
//
// UV mapping for spline hair is handled
// specially.
//
uvSets.append("");
}
else
{
MString uvSet;
for (unsigned int m = 0; m < numGrowthSurfaces; m++)
{
MDagPath objectPath =
sNode->exportData.meshes[m];
if (!objectPath.hasFn(MFn::kMesh))
{
//
// This growth object is not a mesh, so
// it doesn't support UV sets.
//
// We store a blank UV set name which
// will be an indicator later on to
// perform the default mapping for this
// growth object.
//
uvSet = "";
}
else
{
//
// We have a mesh.
//
// Get the UV set used by this mesh for
// this texture.
//
uvSet = sNode->getUVSet(
objectPath.node(),
textures[ti]
);
}
uvSets.append(uvSet);
// Cache the uvs for this UV set.
uvInfo[m] = cacheUVs(sNode, m, uvSet, uvInfoCache);
}
}
for (pass = 0;
pass < nodeInfo->maxPasses;
pass++)
{
textureUVMaps[ti][pass].us = new float[nodeInfo->count];
textureUVMaps[ti][pass].vs = new float[nodeInfo->count];
for (hairnum = 0; hairnum < nodeInfo->count; hairnum++)
{
hair = &hairInfo[pass][hairnum];
if (splineNode)
{
//
// Spline hair is a sheet, so there's a U
// direction but no V. (Keep in mind that
// we're talking about texture coords for
// the *base* of the hair here, not along
// the length of the hair itself.)
//
// Annoyingly, Maya never seems to
// consider a V value of 0.0 to
// actually be on the texture, so we
// have to nudge it in a bit.
//
u = hair->wgt[0];
v = 0.0001f;
}
else
{
// Create a map from Shave's UTpid to the
// the mesh index, if it doesn't already
// exist.
if (faceToMesh == NULL)
faceToMesh = createFaceIDMap(sNode->exportData.startFaces);
meshIndex = faceToMesh[hair->UTpid];
meshNode = sNode->exportData.meshes[meshIndex];
meshNode.extendToShape();
// If this growth surface is a not
// really a mesh (i.e. it's a NURBS or
// subd) then it will be using the
// default UV parameterization, which
// we have already stored in nodeInfo
// so we can grab the value from there
// rather than recalculating it here.
//
// Similarly, if it *is* a mesh and we're
// using the same uv set as was cached
// in nodeInfo, then just grab the
// values from there.
if (!meshNode.hasFn(MFn::kMesh)
|| (uvSets[meshIndex] == meshUVSet))
{
u = nodeInfo->u[pass][hairnum];
v = nodeInfo->v[pass][hairnum];
}
else
{
int normalizedPolyIdx = hair->UTpid -
sNode->exportData.startFaces[meshIndex];
u = 0.0f;
v = 0.0f;
getHairUV(
*uvInfo[meshIndex],
normalizedPolyIdx,
hair->pntid,
hair->wgt,
u,
v
);
}
}
textureUVMaps[ti][pass].us[hairnum] = u;
textureUVMaps[ti][pass].vs[hairnum] = v;
}
}
}
if (verbose)
{
cerr << "done." << endl;
cerr << "getting texture values from maya... " << endl;
}
int maxtex=100000;
for (pass = 0; pass < nodeInfo->maxPasses; pass++)
{
// malloc the top level of our lookup;
nodeInfo->textureLookup[pass] = (float**)
calloc(SHAVE_NUM_PARAMS, sizeof(float*));
MFloatArray uArray;
MFloatArray vArray;
MFloatPointArray pointArray;
MFloatPointArray pa;
VERT pos[3];
struct HairBaryInfo* hair;
// we need to to figure out the uv coords for each
// of the roots for this pass, and also the point
// locations for 3D texture evaluation.
MFloatMatrix camMatrix;
// if we store the lookups we have done then we
// don't need to do them again. This will save both
// time and memory for high density scenes.
MStringArray doneLookups;
MIntArray doneLookupsIndex;
for (pi = 0; pi < connectedIndexes.length(); pi++)
{
int ind;
int total;
total=nodeInfo->count;
int parmIndex = (int)connectedIndexes[pi];
texPlug
.elementByLogicalIndex(parmIndex)
.connectedTo(texPlugArray, true, false);
MObject texture = texPlugArray[0].node();
for (ti = 0; ti < numTextures; ti++)
if (textures[ti] == texture) break;
nodeInfo->textureLookup[pass][parmIndex]
= (float*) calloc(
total, sizeof(float)
);
for (ind=0;ind<nodeInfo->count;ind+=maxtex)
{
uArray.clear();
vArray.clear();
pointArray.clear();
int szz=total;
if (total>=maxtex) szz=total-ind;
if (szz>maxtex) szz=maxtex;
//fprintf (stdout,"ind = %d szz= %d\n",ind,szz);fflush(stdout);
for (hairnum = ind; hairnum < ind+szz; hairnum++)
{
hair = &hairInfo[pass][hairnum];
for (int k = 0; k < 3; k++)
{
pos[k] = sNode->memShaveObj
.v[hair->pntid[k]];
pos[k].x *= hair->wgt[k];
pos[k].y *= hair->wgt[k];
pos[k].z *= hair->wgt[k];
}
uArray.append(
textureUVMaps[ti][pass].us[hairnum]
);
vArray.append(
textureUVMaps[ti][pass].vs[hairnum]
);
pointArray.append(
pos[0].x+pos[1].x+pos[2].x,
pos[0].y+pos[1].y+pos[2].y,
pos[0].z+pos[1].z+pos[2].z
);
}
// set aside a little memory for our lookup results.
{
{
MFloatVectorArray vertColorArray;
MFloatVectorArray vertTranspArray;
//////debug//////
//MFloatVectorArray uTang;
//MFloatVectorArray vTang;
//MFloatArray filter;
//uTang.setLength(szz);
//vTang.setLength(szz);
//filter.setLength(szz);
//for(unsigned int k = 0; k < szz; k++)
//{
// uTang[k] = MFloatVector(1.0f,0.0f);
// vTang[k] = MFloatVector(0.0f,1.0f);
// filter[k] = 0.1f;
//}
/////////////////////////
//printf("texture name %s\n",texPlugArray[0].name().asChar() );fflush(stdout);
// lets get the lookup values from the maya
// shading engine.
MStatus shRes =
MRenderUtil::sampleShadingNetwork(
texPlugArray[0].name(), //shade node name
szz, //samples
false, //shadows
false, //reuse shad maps
camMatrix, //camMatrix
&pointArray, //points
&uArray, //u coords
&vArray, //v coords
NULL, //normals
&pointArray, //ref points
NULL /*&uTang*/, //u tang
NULL /*&vTang*/, //v tang
NULL /*&filter*/, //filter size
vertColorArray, //out color
vertTranspArray //out transp
);
//fprintf (stdout,"done with lookup\n");fflush(stdout);
// we need to copy the returned values into the
// lookup array our pointer references. also,
// we'll store the name and parm index of this
// lookup so we can reuse it if the opportunity
// presents itself.
nodeInfo->maxIndex[parmIndex]
= (int)total;
///////// debug ///////////////
//if(shRes != MStatus::kSuccess) printf(" MRenderUtil::sampleShadingNetwork failed with result %i\n",shRes);
//for(int k=0; k < (int)szz; k++)
//{
// printf("uv %f %f rgb %f %f %f t %f\n",uArray[k],vArray[k],vertColorArray[k].x,vertColorArray[k].y,vertColorArray[k].z,vertTranspArray[k]);
//}
//fflush(stdout);
///////////////////////////////
for(int k=0; k < (int)szz; k++)
{
nodeInfo->textureLookup[pass][parmIndex][k+ind]
= (float)vertColorArray[k].x;
// nodeInfo->textureLookup[pass][parmIndex][k]
// = (float)vertColorArray[k].x;
}
}
}
}
}
}
if (verbose)
{
cerr << "done." << endl;
cerr << "freeing cached curveinfos and uvlists..." << endl;
}
for (ti = 0; ti < numTextures; ti++)
{
for (pass = 0; pass < nodeInfo->maxPasses; pass++)
{
delete [] textureUVMaps[ti][pass].us;
delete [] textureUVMaps[ti][pass].vs;
}
delete [] textureUVMaps[ti];
}
delete [] textureUVMaps;
for (pass = 0; pass < nodeInfo->maxPasses; pass++)
delete [] hairInfo[pass];
delete [] hairInfo;
}
delete [] faceToMesh;
UVInfoCache::iterator setIter;
for (setIter = uvInfoCache.begin();
setIter != uvInfoCache.end();
++setIter)
{
UVSetUVInfo* uvSetUVInfo = (*setIter).second;
for (unsigned int ui = 0; ui < uvSetUVInfo->size(); ++ui)
delete (*uvSetUVInfo)[ui];
delete uvSetUVInfo;
}
uvInfoCache.clear();
// we can't set the textured member true until we've taken
// care of all the passes. Otherwise make a curve will fail
// trying to do a lookup.
for (pi = 0; pi < connectedIndexes.length(); pi++)
{
unsigned int pIndex = connectedIndexes[pi];
nodeInfo->textured[pIndex] = true;
}
// remove temporary connections that were made in the
// hypergraph. not strictly necesary, but keeps thing
// looking cleaner for the user.
dgMod.undoIt();
//
// Let the node know that its texture cache has changed.
//
MPlug plug(
shaveHairShapes[node],
shaveHairShape::textureCacheUpdatedAttr
);
plug.setValue(true);
}
}
}
//
// It's now safe to serve up the texture lookup tables.
//
buildingLookups = false;
//if (verbose) cerr << "done with lookups." << endl << endl;
}
MStatus freeTextureLookup(void)
{
if (texInfoLookup)
{
for (unsigned int node = 0; node < texInfoLookupSize; node++) {
freeTextureLookup(&texInfoLookup[node]);
}
free(texInfoLookup);
texInfoLookup = NULL;
texInfoLookupSize = 0;
}
if (texIDMap)
{
delete [] texIDMap;
texIDMap = NULL;
texIDMapSize = 0;
}
return MS::kSuccess;
}
MStatus freeTextureLookup(NODETEXINFO* lookup)
{
if (lookup)
{
bool istextured = false;
int clearPasses = lookup->maxPasses;
for(int pass = 0; pass < clearPasses; pass++)
{
if (lookup->displacement)
{
delete [] lookup->displacement[pass];
lookup->displacement[pass] = NULL;
}
delete [] lookup->u[pass];
lookup->u[pass] = NULL;
delete [] lookup->v[pass];
lookup->v[pass] = NULL;
for(int parm = 0; parm < SHAVE_NUM_PARAMS; parm++)
{
if(lookup->textured[parm])
{
free(lookup->textureLookup[pass][parm]);
lookup->textureLookup[pass][parm] = NULL;
istextured = true;
}
}
if(istextured)
{
free(lookup->textureLookup[pass]);
lookup->textureLookup[pass] = NULL;
}
}
if (lookup->displacement)
{
delete [] lookup->displacement;
lookup->displacement = NULL;
}
delete [] lookup->u;
lookup->u = NULL;
delete [] lookup->v;
lookup->v = NULL;
if (lookup->textureLookup)
{
free(lookup->textureLookup);
lookup->textureLookup = NULL;
}
}
return MS::kSuccess;
}
void getDisplacement(
MDagPath& surface,
CURVEINFO& ci,
VERT& rootPos,
float rootU,
float rootV,
int surfaceStartFaceIndex,
VERT& displacement
)
{
displacement.x = 0;
displacement.y = 0;
displacement.z = 0;
if (surface.hasFn(MFn::kNurbsSurface))
{
MPoint actualPoint;
MFnNurbsSurface nurbsFn(surface);
MPoint shavePt(rootPos.x, rootPos.y, rootPos.z);
actualPoint = nurbsFn.closestPoint(
shavePt, NULL, NULL, true, MPoint_kTol, MSpace::kWorld
);
displacement.x = (float)(actualPoint.x - shavePt.x);
displacement.y = (float)(actualPoint.y - shavePt.y);
displacement.z = (float)(actualPoint.z - shavePt.z);
}
#ifdef EVALUATE_POSITION_FIXED
else if (surface.hasFn(MFn::kSubdiv))
{
int i;
int level1FaceIdx = (ci->UTpid - surfaceStartFaceIndex) / 2;
MFnSubd subdFn(surface);
int numLevel0Faces = subdFn.polygonCount(0);
for (i = 0; i < numLevel0Faces; i++)
{
MUint64 level0FaceID;
MUint64Array level1Faces;
MFnSubdNames::toMUint64(level0FaceID, i, 0, 0, 0, 0);
subdFn.polygonChildren(level0FaceID, level1Faces);
if ((unsigned int)level1FaceIdx >= level1Faces.length())
{
level1FaceIdx -= level1Faces.length();
}
else
{
MPoint actualPoint;
subdFn.evaluatePosition(
level1Faces[level1FaceIdx],
rootU,
rootV,
false,
actualPoint
);
//
// 'actualPoint' is in object space. Convert it to world.
//
MMatrix mat = surface.inclusiveMatrix();
actualPoint = actualPoint * mat;
displacement.x = actualPoint.x - rootPos.x;
displacement.y = actualPoint.y - rootPos.y;
displacement.z = actualPoint.z - rootPos.z;
break;
}
}
}
else if (!surface.hasFn(MFn::kMesh) && !surface.hasFn(MFn::kNurbsCurve))
#else
else if (!surface.hasFn(MFn::kMesh)
&& !surface.hasFn(MFn::kSubdiv)
&& !surface.hasFn(MFn::kNurbsCurve))
#endif
{
MGlobal::displayWarning(
MString("shave: getDisplacement: got request for invalid")
+ " growth surface '" + surface.fullPathName() + "'."
);
}
}
NODETEXINFO* getTexInfoLookup(unsigned shaveID)
{
if (buildingLookups
|| (texIDMap == NULL)
|| (shaveID < 0)
|| (shaveID >= texIDMapSize))
{
return NULL;
}
int lookupID = texIDMap[shaveID];
if (lookupID == -1) return NULL;
return &texInfoLookup[lookupID];
}
//
// Connect the source plug to the specified element of the given
// shaveHairShape's texture array plug, using the supplied DGModifier.
//
static MStatus makeTextureConnection(
MPlug sourcePlug, MObject shavenode, int texPlugIndex, MDGModifier& dgMod
)
{
MPlug texPlugArray(shavenode, shaveHairShape::shaveTextureAttr);
dgMod.connect(sourcePlug, texPlugArray.elementByLogicalIndex(texPlugIndex));
return MS::kSuccess;
}
//
// If a colour shaveHairShape parameter has an incoming connection, create
// separate temporary connections to the 'tex' array for each of its RGB
// components.
//
static void makeTempCompoundConnection(
const MObject& shavenode,
MObject& colourAttr,
int startIndex,
MDGModifier& dgMod
)
{
MPlug colourPlug(shavenode, colourAttr);
if (colourPlug.isConnected())
{
//
// Find the node which is feeding this connection.
//
MPlugArray connections;
colourPlug.connectedTo(connections, true, false);
if(connections.length() > 0)
{
MFnDependencyNode texNode(connections[0].node());
//
// Connect the component plugs to the shaveHairShape's 'shaveTex'
// array.
//
colourPlug = texNode.findPlug("outColorR");
makeTextureConnection(colourPlug, shavenode, startIndex, dgMod);
colourPlug = texNode.findPlug("outColorG");
makeTextureConnection(colourPlug, shavenode, startIndex+1, dgMod);
colourPlug = texNode.findPlug("outColorB");
makeTextureConnection(colourPlug, shavenode, startIndex+2, dgMod);
}
}
}
static void makeTempColourConnections(
const MObject& shavenode, MDGModifier& dgMod
)
{
// first we need to hook up the 'special' nodes- the color
// ones. These nodes are handled differently than the rest
// because they hook one tex up to a vector value, rather
// than one float.
makeTempCompoundConnection(
shavenode, shaveHairShape::hairColorTexture, 9, dgMod
);
makeTempCompoundConnection(
shavenode, shaveHairShape::mutantHairColorTexture, 13, dgMod
);
makeTempCompoundConnection(
shavenode, shaveHairShape::rootHairColorTexture, 17, dgMod
);
MObject colourAttrs[] =
{
shaveHairShape::hairColorTextureR,
shaveHairShape::hairColorTextureG,
shaveHairShape::hairColorTextureB,
shaveHairShape::mutantHairColorTextureR,
shaveHairShape::mutantHairColorTextureG,
shaveHairShape::mutantHairColorTextureB,
shaveHairShape::rootHairColorTextureR,
shaveHairShape::rootHairColorTextureG,
shaveHairShape::rootHairColorTextureB
};
int attrIndices[] = { 9, 10, 11, 13, 14, 15, 17, 18, 19 };
int numAttrs = sizeof(attrIndices) / sizeof(int);
int i;
MPlugArray texturePlug;
for (i = 0; i < numAttrs; i++)
{
MPlug colourPlug(shavenode, colourAttrs[i]);
if (colourPlug.isConnected())
{
colourPlug.connectedTo(texturePlug, true, false);
makeTextureConnection(
texturePlug[0], shavenode, attrIndices[i], dgMod
);
}
}
}
MStatus tesselateGrowthSurfaces(
const MDagPathArray& growthSurfaces,
short uTess, short vTess,
short sDept, short sSamp
)
{
MDagPath surface;
unsigned int i;
standinMeshes.clear();
standinMeshOrigSurfaces.clear();
for (i = 0; i < growthSurfaces.length(); i++)
{
surface = growthSurfaces[i];
//
// Make sure that we've got the shape and not its transform.
//
surface.extendToShape();
#ifdef EVALUATE_POSITION_FIXED
//
// If the shape is a NURBS surface, then we have to generate a
// temporary mesh for it.
//
if (surface.hasFn(MFn::kNurbsSurface))
#else
//
// If the shape is a NURBS or subdivision surface, then we have to
// generate a temporary mesh for it.
//
if (surface.hasFn(MFn::kNurbsSurface) || surface.hasFn(MFn::kSubdiv))
#endif
{
MObject tempMesh = shaveUtil::getMesh(surface, (int)uTess, (int)vTess, (int)sDept, (int)sSamp);
standinMeshes.append(tempMesh);
standinMeshOrigSurfaces.append(surface);
}
}
return MS::kSuccess;
}
unsigned int* createFaceIDMap(const MIntArray& startIndices)
{
if (startIndices.length() == 0) return new unsigned int[1];
unsigned int numFaces = startIndices[startIndices.length()-1];
unsigned int* map = new unsigned int[numFaces];
unsigned int i;
unsigned int j = 0;
for (i = 1; i < startIndices.length(); i++)
{
while (j < (unsigned int)startIndices[i])
map[j++] = i-1;
}
return map;
}
const MString ShavePreprocTex::commandName("shaveUpdateTextures");
static const char* flVertexParams = "-vertexParams";
static const char* fsVertexParams = "-vp";
ShavePreprocTex::~ShavePreprocTex() {}
void* ShavePreprocTex::createCmd()
{
return new ShavePreprocTex();
}
MSyntax ShavePreprocTex::createSyntax()
{
MSyntax syntax;
syntax.addFlag(fsVertexParams, flVertexParams);
return syntax;
}
MStatus ShavePreprocTex::doIt( const MArgList& args )
{
MStatus res = MS::kSuccess;
MArgDatabase argdb(syntax(), args, &res);
if (!res) return res;
MObjectArray shaveHairShapes;
shaveUtil::getShaveNodes(shaveHairShapes);
if (shaveHairShapes.length() > 0)
{
MGlobal::displayInfo("command shaveUpdateTextures");
shaveGlobals::Globals globals;
shaveGlobals::getGlobals(globals);
if (argdb.isFlagSet(fsVertexParams))
initVertTexInfoLookup(shaveHairShapes);
else
#ifdef PER_NODE_TEXLOOKUP
initTexInfoLookup2(shaveHairShapes, "", globals.verbose, true);
#else
initTexInfoLookup(shaveHairShapes, "", globals.verbose, true);
#endif
MGlobal::executeCommand("currentTime `currentTime -q`");
}
return res;
}
// Initialize the per-vertex texture lookup for a single shaveHairShape.
//
// If this method is called during shaveHairShape::compute() then 'block' must
// be provided and must point to the same datablock as was passed to
// shaveHairShape::compute().
//
void initVertTexInfoLookup(shaveHairShape* sNode, MObject node, MDataBlock* block)
{
MPlug checkPlug;
MObject nodeObj = sNode->thisMObject();
MPlug texPlug(nodeObj, shaveHairShape::shaveTextureAttr);
MPlugArray texPlugArray;
unsigned int vertCount = sNode->memShaveObj.totalverts;
// Get get the hair shape's per-vertex texture data.
ShavePerVertTexInfo* vertInfo = &sNode->vertTexInfo;
// Prepare it to accept new data.
vertInfo->init(vertCount);
// Store a pointer to the node's info in a map where it can be
// quickly indexed by Shave ID.
vertTexInfoMap[sNode->getShaveID()] = vertInfo;
// Step through each of the per-vertex parameters and gather texture
// data for any which are textured.
unsigned int pi = 0;
for (pi = 0; pi < ShavePerVertTexInfo::numParams(); pi++)
{
int paramID = ShavePerVertTexInfo::getParamNumber(pi);
checkPlug = texPlug.elementByLogicalIndex(paramID);
if (checkPlug.isConnected())
{
//
// Get the texture plug so we can determine what
// node to sample.
//
texPlugArray.clear();
texPlug
.elementByLogicalIndex(paramID)
.connectedTo(texPlugArray, true, false);
MFloatArray uArray;
MFloatArray vArray;
MFloatPointArray pointArray;
MFloatVectorArray vertColorArray;
MFloatVectorArray vertTranspArray;
MFloatMatrix camMatrix;
VERT wfpos;
for (unsigned int vi = 0; vi < vertCount; ++vi)
{
//uArray.append(sNode->memShaveObj.uv[vi].x);
//vArray.append(sNode->memShaveObj.uv[vi].y);
wfpos = sNode->memShaveObj.v[vi];
pointArray.append(
MFloatPoint(wfpos.x, wfpos.y, wfpos.z)
);
//printf("x %f y %f z %f\n", (float)wfpos.x,(float)wfpos.y,(float)wfpos.z);
}
/// by some reason memShaveObj contains some weird UVs
/// so vertex maps are not mappad same as others
/// so we are trying to grab UVs from export mesh via Maya api
/// if something is wrong with it, the uvs from memShaveObj are used
unsigned int n = 0;
//printf("num exp meshes %i\n",sNode->exportData.meshes.length());fflush(stdout);
if(sNode->exportData.meshes.length() > 0)
{
short uTess;
short vTess;
short sDept;
short sSamp;
if (block != NULL)
{
uTess = block->inputValue(shaveHairShape::surfTessU).asShort();
vTess = block->inputValue(shaveHairShape::surfTessV).asShort();
sDept = block->inputValue(shaveHairShape::subdTessDept).asShort();
sSamp = block->inputValue(shaveHairShape::subdTessSamp).asShort();
}
else
{
MPlug plug(node, shaveHairShape::surfTessU);
plug.getValue(uTess);
plug.setAttribute(shaveHairShape::surfTessV);
plug.getValue(vTess);
plug.setAttribute(shaveHairShape::subdTessDept);
plug.getValue(sDept);
plug.setAttribute(shaveHairShape::subdTessSamp);
plug.getValue(sSamp);
}
//MItMeshVertex vIter(sNode->exportData.meshes[0]);
for(unsigned int oo = 0; oo < sNode->exportData.meshes.length(); oo++)
{
MDagPath surface = sNode->exportData.meshes[oo];
surface.extendToShape();
MObject mesh = shaveUtil::getMesh(surface, (int)uTess, (int)vTess, (int)sDept, (int)sSamp);
MItMeshVertex vIter(mesh);
for (vIter.reset(); !vIter.isDone(); vIter.next())
{
int numUVs;
vIter.numUVs(numUVs);
if (numUVs > 0)
{
float2 vertUV;
vIter.getUV(vertUV);
uArray.append(vertUV[0]);
vArray.append(vertUV[1]);
//printf("u %f v %f\n", vertUV[0], vertUV[1]);
}
else
{
uArray.append(0.0f);
vArray.append(0.0f);
}
n++;
}
}
}
if(n != vertCount)
{
uArray.clear();
vArray.clear();
for (unsigned int vi = 0; vi < vertCount; ++vi)
{
uArray.append(sNode->memShaveObj.uv[vi].x);
vArray.append(sNode->memShaveObj.uv[vi].y);
}
}
//printf("texture %s\n",texPlugArray[0].name().asChar());
//printf("uvs stored %i vertices %i\n", n, vertCount);fflush(stdout);
////////////////////////////
// Sample the texture at all vertices.
MRenderUtil::sampleShadingNetwork(
texPlugArray[0].name(), //shade node name
uArray.length(), //samples
false, //shadows
false, //reuse shad maps
camMatrix, //camMatrix
&pointArray, //points
&uArray, //u coords
&vArray, //v coords
NULL, //normals
&pointArray, //ref points
NULL, //u tang
NULL, //v tang
NULL, //filter size
vertColorArray, //out color
vertTranspArray //out transp
);
// Store the sampled texture values.
for (unsigned int k = 0; k < vertColorArray.length(); ++k)
{
vertInfo->setValue(pi, k, (float)vertColorArray[k].x);
//vertInfo->setValue(pi, k, k < vertColorArray.length()/2 ? 0.0f : 1.0f); //test - OK
//printf("r %f g %f b %f\n", (float)vertColorArray[k].x,(float)vertColorArray[k].y,(float)vertColorArray[k].z);fflush(stdout);
}
}
}
}
// Vertex-level textures are used for dynamics runup and Live Mode.
// They're not used in rendering or export.
void initVertTexInfoLookup(MObjectArray& shaveHairShapes)
{
buildingLookups = true;
unsigned int numNodes = shaveHairShapes.length();
for (unsigned int i = 0; i < numNodes; ++i)
{
// Get the hair shape.
MFnDependencyNode shaveDependNode(shaveHairShapes[i]);
shaveHairShape* node = (shaveHairShape*) shaveDependNode.userNode();
// Force the guides to update if the geometry has changed,
// e.g. because we're on a new frame.
node->getHairNode();
// Make sure that our parameters are all up to date with
// any changes the user may have made.
//
// %%% Shouldn't the 'getHairNode' call above already have
// taken care of that?
node->updateParams();
// Let Shave know which node our calls will be referring to.
node->makeCurrent();
// Update the node's per-vertex texture info.
initVertTexInfoLookup(node,shaveHairShapes[i]);
//
// Let the node know that its texture cache has changed.
//
MPlug plug(shaveHairShapes[i], shaveHairShape::textureCacheUpdatedAttr);
plug.setValue(true);
}
// It's now safe to serve up the texture lookup data.
buildingLookups = false;
}
float applyVertTexValue(long shaveID, int vertID, int paramID, float baseValue)
{
if (!buildingLookups)
{
ShavePerVertTexInfo* vertTexInfo = vertTexInfoMap[shaveID];
if (vertTexInfo)
{
// Find the index of this parameter.
unsigned int paramIdx;
if (ShavePerVertTexInfo::getParamIdx(paramID, paramIdx))
return (baseValue * vertTexInfo->getValue(paramIdx, vertID));
}
}
return baseValue;
}
void cacheSurfaceUVs(
MItMeshPolygon& polyIter,
unsigned int pntidOffset,
const MString& uvSet,
SurfaceUVInfo* surfUVInfo
)
{
FaceUVInfo faceUVInfo;
VertUVInfo vertUVInfo;
surfUVInfo->reserve(polyIter.count ());
for (polyIter.reset(); !polyIter.isDone(); polyIter.next())
{
unsigned int numFaceVerts = polyIter.polygonVertexCount();
unsigned int i;
faceUVInfo.clear();
faceUVInfo.reserve(numFaceVerts);
for (i = 0; i < numFaceVerts; ++i)
{
float2 uv = { 0.0f, 0.0f };
// If no UV set is specified, use the mesh's default set.
if (uvSet.length() == 0)
{
polyIter.getUV(i, uv);
}
else
{
MStatus st = polyIter.getUV(i, uv, &uvSet);
// If we couldn't find a UV it's probably because we have
// an uninitialized UV set. So fall back to the default
// mapping.
if (!st)
{
polyIter.getUV(i, uv);
}
}
vertUVInfo.u = uv[0];
vertUVInfo.v = uv[1];
vertUVInfo.pntid = pntidOffset + polyIter.vertexIndex(i);
faceUVInfo.push_back(vertUVInfo);
}
surfUVInfo->push_back(faceUVInfo);
}
}
SurfaceUVInfo* cacheUVs(
shaveHairShape* sNode,
unsigned int surfIdx,
const MString& uvSet,
UVInfoCache& uvInfoCache
)
{
UVSetUVInfo* uvSetUVInfo = NULL;
// Do we have any data cached for this uv set?
UVInfoCache::iterator it = uvInfoCache.find(uvSet);
if (it == uvInfoCache.end())
{
// No data for this uv set yet, so create an entry with empty
// slots for each growth surface.
uvSetUVInfo = new UVSetUVInfo;
unsigned int numSurfaces = sNode->exportData.meshes.length();
uvSetUVInfo->reserve(numSurfaces);
uvInfoCache.insert(UVInfoCache::value_type(uvSet, uvSetUVInfo));
for (unsigned int i = 0; i < numSurfaces; ++i)
uvSetUVInfo->push_back(NULL);
}
else
{
uvSetUVInfo = (*it).second;
// If we've already cached the UVs for this growth mesh, return
// them.
if ((*uvSetUVInfo)[surfIdx] != NULL) return (*uvSetUVInfo)[surfIdx];
}
SurfaceUVInfo* surfUVInfo = new SurfaceUVInfo;
MDagPath surfPath = sNode->exportData.meshes[surfIdx];
// If hair we are dealing with is growing from a nurbs surface,
// then we cannot initialize the poly iterator with its path because
// the path doesn't point to a mesh. Instead we must initialize it
// with the tesselated mesh object that we created earlier.
#ifdef EVALUATE_POSITION_FIXED
if (surfPath.hasFn(MFn::kNurbsSurface))
#else
if (surfPath.hasFn(MFn::kNurbsSurface) || surfPath.hasFn(MFn::kSubdiv))
#endif
{
unsigned int i;
for (i = 0; i < standinMeshOrigSurfaces.length(); i++)
{
if (standinMeshOrigSurfaces[i] == surfPath) break;
}
if (i < standinMeshes.length())
{
MItMeshPolygon polyIter(standinMeshes[i]);
cacheSurfaceUVs(
polyIter,
sNode->exportData.startVerts[surfIdx],
uvSet,
surfUVInfo
);
}
}
else
{
MItMeshPolygon polyIter(surfPath);
// Meshes with history can sometimes get themselves into a state
// where an MFnMesh created from the dag path (or node) reports
// vertices but no faces. If that happens then we need to pull
// the mesh from its output plug.
if (polyIter.count() == 0)
{
MFnDagNode nodeFn(surfPath);
MPlug plug = nodeFn.findPlug("outMesh");
MObject meshObj;
plug.getValue(meshObj);
MItMeshPolygon polyIter(meshObj);
cacheSurfaceUVs(
polyIter,
sNode->exportData.startVerts[surfIdx],
uvSet,
surfUVInfo
);
}
else
{
cacheSurfaceUVs(
polyIter,
sNode->exportData.startVerts[surfIdx],
uvSet,
surfUVInfo
);
}
}
(*uvSetUVInfo)[surfIdx] = surfUVInfo;
return surfUVInfo;
}
|