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
|
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
#include "foundation/PxFoundationVersion.h"
#include "PxPhysXConfig.h"
#include "foundation/PxMemory.h"
#include "SamplePreprocessor.h"
#include "PhysXSampleApplication.h"
#include "PhysXSample.h"
#include "SampleCommandLine.h"
#include "PxTkFile.h"
#include "PsString.h"
#include "PsFPU.h"
#include "PxToolkit.h"
#include "extensions/PxDefaultStreams.h"
#include "RenderBoxActor.h"
#include "RenderSphereActor.h"
#include "RenderCapsuleActor.h"
#include "RenderMeshActor.h"
#include "RenderGridActor.h"
#include "RenderMaterial.h"
#include "RenderTexture.h"
#include "RenderPhysX3Debug.h"
#include "RenderClothActor.h"
#include "RendererClothShape.h"
#include "ParticleSystem.h"
#include <SamplePlatform.h>
#include "SampleBaseInputEventIds.h"
#include <SampleUserInputIds.h>
#include "SampleUserInputDefines.h"
#include <SampleInputAsset.h>
#include "SampleInputMappingAsset.h"
#include <algorithm>
#include <ctype.h>
#include "cloth/PxClothParticleData.h"
#include "TestClothHelpers.h"
#include "extensions/PxClothFabricCooker.h"
#include "Picking.h"
#include "TestGroup.h"
#if PX_WINDOWS
// Starting with Release 302 drivers, application developers can direct the Optimus driver at runtime to use the High Performance
// Graphics to render any application; even those applications for which there is no existing application profile.
// They can do this by exporting a global variable named PxOptimusEnablement.
// A value of 0x00000001 indicates that rendering should be performed using High Performance Graphics.
// A value of 0x00000000 indicates that this method should be ignored.
extern "C"
{
_declspec(dllexport) DWORD PxOptimusEnablement = 0x00000001;
}
#endif
using namespace SampleFramework;
using namespace SampleRenderer;
static bool gRecook = false;
PxDefaultAllocator gDefaultAllocatorCallback;
enum MaterialID
{
MATERIAL_CLOTH = 444,
#ifdef RENDERER_TABLET
MATERIAL_CONTROLS,
MATERIAL_BUTTONS,
#endif
};
#ifdef RENDERER_TABLET
#include "SampleMaterialAsset.h"
static const char* controlMaterialPath = "materials/tablet_sticks.xml";
static const char* buttonMaterialPath = "materials/tablet_buttons.xml";
#endif
///////////////////////////////////////////////////////////////////////////////
PX_FORCE_INLINE PxSimulationFilterShader getSampleFilterShader()
{
return PxDefaultSimulationFilterShader;
}
///////////////////////////////////////////////////////////////////////////////
PX_FORCE_INLINE void SetupDefaultRigidDynamic(PxRigidDynamic& body, bool kinematic=false)
{
body.setActorFlag(PxActorFlag::eVISUALIZATION, true);
body.setAngularDamping(0.5f);
body.setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, kinematic);
}
void PhysXSample::unlink(RenderBaseActor* renderActor, PxShape* shape, PxRigidActor* actor)
{
PhysXShape theShape(actor, shape);
mPhysXShapeToRenderActorMap.erase(theShape);
PX_UNUSED(renderActor);
}
void PhysXSample::link(RenderBaseActor* renderActor, PxShape* shape, PxRigidActor* actor)
{
PhysXShape theShape(actor, shape);
mPhysXShapeToRenderActorMap[theShape] = renderActor;
renderActor->setPhysicsShape(shape, actor);
}
RenderBaseActor* PhysXSample::getRenderActor(PxRigidActor* actor, PxShape* shape)
{
PhysXShape theShape(actor, shape);
PhysXShapeToRenderActorMap::iterator it = mPhysXShapeToRenderActorMap.find(theShape);
if (mPhysXShapeToRenderActorMap.end() != it)
return it->second;
return NULL;
}
SampleDirManager& PhysXSample::getSampleOutputDirManager()
{
static SampleDirManager gSampleOutputDirManager(SAMPLE_OUTPUT_DIR, false);
return gSampleOutputDirManager;
}
PxToolkit::BasicRandom& getSampleRandom()
{
static PxToolkit::BasicRandom gRandom(42);
return gRandom;
}
PxErrorCallback& getSampleErrorCallback()
{
static PxDefaultErrorCallback gDefaultErrorCallback;
return gDefaultErrorCallback;
}
// sschirm: same here: would be good to have a place for platform independent path manipulation
// shared for all apps
const char* getFilenameFromPath(const char* filePath, char* buffer)
{
const char* ptr = strrchr(filePath, '/');
if (!ptr)
ptr = strrchr(filePath, '\\');
if (ptr)
{
strcpy(buffer, ptr + 1);
}
else
{
strcpy(buffer, filePath);
}
return buffer;
}
const char* PhysXSample::getSampleOutputFilePath(const char* inFilePath, const char* outExtension)
{
static char sBuffer[1024];
char tmpBuffer[1024];
const char* inFilename = getFilenameFromPath(inFilePath, sBuffer);
sprintf(tmpBuffer, "cached/%s%s", inFilename, outExtension);
return getSampleOutputDirManager().getFilePath(tmpBuffer, sBuffer, false);
}
static void GenerateCirclePts(unsigned int nb, PxVec3* pts, float scale, float z)
{
for(unsigned int i=0;i<nb;i++)
{
const PxF32 angle = 6.28f*PxF32(i)/PxF32(nb);
pts[i].x = cosf(angle)*scale;
pts[i].y = z;
pts[i].z = sinf(angle)*scale;
}
}
static PxConvexMesh* GenerateConvex(PxPhysics& sdk, PxCooking& cooking, PxU32 nbVerts, const PxVec3* verts, bool recenterVerts=false)
{
PxVec3Alloc* tmp = NULL;
if(recenterVerts)
{
PxVec3 center(0);
for(PxU32 i=0;i<nbVerts;i++)
center += verts[i];
center /= PxReal(nbVerts);
tmp = SAMPLE_NEW(PxVec3Alloc)[nbVerts];
PxVec3* recentered = tmp;
for(PxU32 i=0;i<nbVerts;i++)
recentered[i] = verts[i] - center;
}
PxConvexMesh* convexMesh = PxToolkit::createConvexMesh(sdk, cooking, recenterVerts ? tmp : verts, nbVerts, PxConvexFlag::eCOMPUTE_CONVEX);
DELETEARRAY(tmp);
return convexMesh;
}
static PxConvexMesh* GenerateConvex(PxPhysics& sdk, PxCooking& cooking, float scale, bool large=false, bool randomize=true)
{
const PxI32 minNb = large ? 16 : 3;
const PxI32 maxNb = large ? 32 : 8;
const int nbInsideCirclePts = !randomize ? 3 : getSampleRandom().rand(minNb, maxNb);
const int nbOutsideCirclePts = !randomize ? 8 : getSampleRandom().rand(minNb, maxNb);
const int nbVerts = nbInsideCirclePts + nbOutsideCirclePts;
// Generate random vertices
PxVec3Alloc* verts = SAMPLE_NEW(PxVec3Alloc)[nbVerts];
// Two methods
if(randomize && getSampleRandom().rand(0, 100) > 50)
{
for(int i=0;i<nbVerts;i++)
{
verts[i].x = scale * getSampleRandom().rand(-2.5f, 2.5f);
verts[i].y = scale * getSampleRandom().rand(-2.5f, 2.5f);
verts[i].z = scale * getSampleRandom().rand(-2.5f, 2.5f);
}
}
else
{
GenerateCirclePts(nbInsideCirclePts, verts, scale, 0.0f);
GenerateCirclePts(nbOutsideCirclePts, verts+nbInsideCirclePts, scale*3.0f, 10.0f*scale);
}
PxConvexMesh* convexMesh = GenerateConvex(sdk, cooking, nbVerts, verts);
DELETEARRAY(verts);
return convexMesh;
}
#if 0
static PxConvexMesh* GenerateConvex(PxPhysics& sdk, PxCooking& cooking, int nbInsideCirclePts, int nbOutsideCirclePts, float scale0, float scale1, float z)
{
const int nbVerts = nbInsideCirclePts + nbOutsideCirclePts;
// Generate random vertices
PxVec3Alloc* verts = SAMPLE_NEW(PxVec3Alloc)[nbVerts];
GenerateCirclePts(nbInsideCirclePts, verts, scale0, 0.0f);
GenerateCirclePts(nbOutsideCirclePts, verts+nbInsideCirclePts, scale1, z);
PxConvexMesh* convexMesh = GenerateConvex(sdk, cooking, nbVerts, verts);
DELETEARRAY(verts);
return convexMesh;
}
#endif
static PxRigidDynamic* GenerateCompound(PxPhysics& sdk, PxScene* scene, PxMaterial* defaultMat, const PxVec3& pos, const PxQuat& rot, const std::vector<PxTransform>& poses, const std::vector<const PxGeometry*>& geometries, bool kinematic=false, PxReal density = 1.0f)
{
PxRigidDynamic* actor = sdk.createRigidDynamic(PxTransform(pos, rot));
SetupDefaultRigidDynamic(*actor);
PX_ASSERT(poses.size() == geometries.size());
for(PxU32 i=0;i<poses.size();i++)
{
const PxTransform& currentPose = poses[i];
const PxGeometry* currentGeom = geometries[i];
PxShape* shape = PxRigidActorExt::createExclusiveShape(*actor, *currentGeom, *defaultMat);
shape->setLocalPose(currentPose);
PX_ASSERT(shape);
}
if(actor)
{
PxRigidBodyExt::updateMassAndInertia(*actor, density);
scene->addActor(*actor);
}
return actor;
}
//////////////////////////////////////////////////////////////////////////
void PhysXSample::onRelease(const PxBase* observed, void* userData, PxDeletionEventFlag::Enum deletionEvent)
{
PX_UNUSED(userData);
PX_UNUSED(deletionEvent);
if(observed->is<PxRigidActor>())
{
const PxRigidActor* actor = static_cast<const PxRigidActor*>(observed);
removeRenderActorsFromPhysicsActor(actor);
std::vector<PxRigidActor*>::iterator actorIter = std::find(mPhysicsActors.begin(), mPhysicsActors.end(), actor);
if(actorIter != mPhysicsActors.end())
{
mPhysicsActors.erase(actorIter);
}
}
}
///////////////////////////////////////////////////////////////////////////////
RenderMeshActor* PhysXSample::createRenderMeshFromRawMesh(const RAWMesh& data, PxShape* shape)
{
// Create render mesh from raw mesh
const PxU32 nbTris = data.mNbFaces;
const PxU32* src = data.mIndices;
PxU16* indices = (PxU16*)SAMPLE_ALLOC(sizeof(PxU16)*nbTris*3);
for(PxU32 i=0;i<nbTris;i++)
{
indices[i*3+0] = src[i*3+0];
indices[i*3+1] = src[i*3+2];
indices[i*3+2] = src[i*3+1];
}
RenderMeshActor* meshActor = SAMPLE_NEW(RenderMeshActor)(*getRenderer(), data.mVerts, data.mNbVerts, data.mVertexNormals, data.mUVs, indices, NULL, nbTris);
SAMPLE_FREE(indices);
if(data.mMaterialID!=0xffffffff)
{
size_t nbMaterials = mRenderMaterials.size();
for(PxU32 i=0;i<nbMaterials;i++)
{
if(mRenderMaterials[i]->mID==data.mMaterialID)
{
meshActor->setRenderMaterial(mRenderMaterials[i]);
break;
}
}
}
meshActor->setTransform(data.mTransform);
if(data.mName)
strcpy(meshActor->mName, data.mName);
mRenderActors.push_back(meshActor);
return meshActor;
}
///////////////////////////////////////////////////////////////////////////////
RenderTexture* PhysXSample::createRenderTextureFromRawTexture(const RAWTexture& data)
{
RenderTexture* texture;
if(!data.mPixels)
{
// PT: the pixel data is not included in the RAW file so we use the asset manager to load the texture
SampleAsset* t = getAsset(getSampleMediaFilename(data.mName), SampleAsset::ASSET_TEXTURE);
PX_ASSERT(t->getType()==SampleAsset::ASSET_TEXTURE);
mManagedAssets.push_back(t);
SampleTextureAsset* textureAsset = static_cast<SampleTextureAsset*>(t);
texture = SAMPLE_NEW(RenderTexture)(*getRenderer(), data.mID, textureAsset->getTexture());
}
else
{
// PT: the pixel data is directly included in the RAW file
texture = SAMPLE_NEW(RenderTexture)(*getRenderer(), data.mID, data.mWidth, data.mHeight, data.mPixels);
}
if(data.mName)
strcpy(texture->mName, data.mName);
mRenderTextures.push_back(texture);
return texture;
}
///////////////////////////////////////////////////////////////////////////////
void PhysXSample::newMaterial(const RAWMaterial& data)
{
RenderTexture* diffuse = NULL;
if(data.mDiffuseID!=0xffffffff)
{
size_t nbTextures = mRenderTextures.size();
for(PxU32 i=0;i<nbTextures;i++)
{
if(mRenderTextures[i]->mID==data.mDiffuseID)
{
diffuse = mRenderTextures[i];
break;
}
}
}
RenderMaterial* newRM = SAMPLE_NEW(RenderMaterial)(*getRenderer(), data.mDiffuseColor, data.mOpacity, data.mDoubleSided, data.mID, diffuse);
// strcpy(newRM->mName, data.mName);
mRenderMaterials.push_back(newRM);
}
void PhysXSample::newMesh(const RAWMesh& data)
{
// PT: the mesh name should capture the scale value as well, to make sure different scales give birth to different cooked files
const PxU32 scaleTag = PX_IR(mScale);
PX_ASSERT(mFilename);
char extension[256];
sprintf(extension, "_%d_%x.cooked", mMeshTag, scaleTag);
const char* filePathCooked = getSampleOutputFilePath(mFilename, extension);
PX_ASSERT(NULL != filePathCooked);
bool ok = false;
if(!gRecook)
{
SampleFramework::File* fp = NULL;
PxToolkit::fopen_s(&fp, filePathCooked, "rb");
if(fp)
{
fclose(fp);
ok = true;
}
}
if(!ok)
{
PxTriangleMeshDesc meshDesc;
meshDesc.points.count = data.mNbVerts;
meshDesc.triangles.count = data.mNbFaces;
meshDesc.points.stride = 4*3;
meshDesc.triangles.stride = 4*3;
meshDesc.points.data = data.mVerts;
meshDesc.triangles.data = data.mIndices;
//
shdfnd::printFormatted("Cooking object... %s",filePathCooked);
PxDefaultFileOutputStream stream(filePathCooked);
ok = mCooking->cookTriangleMesh(meshDesc, stream);
shdfnd::printFormatted(" - Done\n");
}
if(ok)
{
PxDefaultFileInputData stream(filePathCooked);
PxTriangleMesh* triangleMesh = mPhysics->createTriangleMesh(stream);
if(triangleMesh)
{
PxTriangleMeshGeometry triGeom;
triGeom.triangleMesh = triangleMesh;
PxRigidStatic* actor = mPhysics->createRigidStatic(data.mTransform);
PxShape* shape = PxRigidActorExt::createExclusiveShape(*actor, triGeom, *mMaterial);
PX_UNUSED(shape);
mScene->addActor(*actor);
addPhysicsActors(actor);
if(0)
{
// Create render mesh from PhysX mesh
PxU32 nbVerts = triangleMesh->getNbVertices();
const PxVec3* verts = triangleMesh->getVertices();
PxU32 nbTris = triangleMesh->getNbTriangles();
const void* tris = triangleMesh->getTriangles();
bool s = triangleMesh->getTriangleMeshFlags() & PxTriangleMeshFlag::e16_BIT_INDICES ? true : false;
PX_ASSERT(s);
PX_UNUSED(s);
const PxU16* src = (const PxU16*)tris;
PxU16* indices = (PxU16*)SAMPLE_ALLOC(sizeof(PxU16)*nbTris*3);
for(PxU32 i=0;i<nbTris;i++)
{
indices[i*3+0] = src[i*3+0];
indices[i*3+1] = src[i*3+2];
indices[i*3+2] = src[i*3+1];
}
RenderMeshActor* meshActor = SAMPLE_NEW(RenderMeshActor)(*getRenderer(), verts, nbVerts, verts, NULL, indices, NULL, nbTris);
if(data.mName)
strcpy(meshActor->mName, data.mName);
PxShape* shape;
actor->getShapes(&shape, 1);
link(meshActor, shape, actor);
mRenderActors.push_back(meshActor);
meshActor->setEnableCameraCull(true);
SAMPLE_FREE(indices);
}
else
{
PxShape* shape;
actor->getShapes(&shape, 1);
RenderMeshActor* meshActor = createRenderMeshFromRawMesh(data, shape);
link(meshActor, shape, actor);
meshActor->setEnableCameraCull(true);
}
mMeshTag++;
}
}
}
void PhysXSample::newShape(const RAWShape&)
{
}
void PhysXSample::newHelper(const RAWHelper&)
{
}
void PhysXSample::newTexture(const RAWTexture& data)
{
createRenderTextureFromRawTexture(data);
}
///////////////////////////////////////////////////////////////////////////////
void PhysXSample::togglePvdConnection()
{
if(mPvd == NULL) return;
if (mPvd->isConnected())
mPvd->disconnect();
else
mPvd->connect(*mTransport,mPvdFlags);
}
void PhysXSample::createPvdConnection()
{
#if PX_SUPPORT_PVD
//Create a pvd connection that writes data straight to the filesystem. This is
//the fastest connection on windows for various reasons. First, the transport is quite fast as
//pvd writes data in blocks and filesystems work well with that abstraction.
//Second, you don't have the PVD application parsing data and using CPU and memory bandwidth
//while your application is running.
//physx::PxPvdTransport* transport = physx::PxDefaultPvdFileTransportCreate( "c:\\mywork\\sample.pxd2" );
//The normal way to connect to pvd. PVD needs to be running at the time this function is called.
//We don't worry about the return value because we are already registered as a listener for connections
//and thus our onPvdConnected call will take care of setting up our basic connection state.
mTransport = physx::PxDefaultPvdSocketTransportCreate(mPvdParams.ip, mPvdParams.port, mPvdParams.timeout);
if(mTransport == NULL)
return;
//The connection flags state overall what data is to be sent to PVD. Currently
//the Debug connection flag requires support from the implementation (don't send
//the data when debug isn't set) but the other two flags, profile and memory
//are taken care of by the PVD SDK.
//Use these flags for a clean profile trace with minimal overhead
mPvdFlags = physx::PxPvdInstrumentationFlag::eALL;
if (!mPvdParams.useFullPvdConnection )
{
mPvdFlags = physx::PxPvdInstrumentationFlag::ePROFILE;
}
mPvd = physx::PxCreatePvd( *mFoundation );
mPvd->connect(*mTransport,mPvdFlags);
#endif
}
///////////////////////////////////////////////////////////////////////////////
//
// default implemententions for PhysXSample interface
//
void PhysXSample::onPointerInputEvent(const InputEvent& ie, physx::PxU32 x, physx::PxU32 y, physx::PxReal dx, physx::PxReal dy, bool pressed)
{
switch (ie.m_Id)
{
case CAMERA_MOUSE_LOOK:
{
if(mPicking)
mPicking->moveCursor(x,y);
}
break;
case PICKUP:
{
if(mPicking)
{
mPicked = !mPicked;;
if(mPicked)
mPicking->lazyPick();
else
mPicking->letGo();
}
}
break;
}
}
void PhysXSample::onResize(PxU32 width, PxU32 height)
{
mApplication.baseResize(width, height);
}
PhysXSample::~PhysXSample()
{
for (size_t i = mDeletedRenderActors.size(); i--;)
{
mDeletedRenderActors[i]->release();
}
mDeletedRenderActors.clear();
PX_ASSERT(!mRenderActors.size());
PX_ASSERT(!mRenderTextures.size());
PX_ASSERT(!mRenderMaterials.size());
PX_ASSERT(!mPhysicsActors.size());
PX_ASSERT(!mManagedAssets.size());
PX_ASSERT(!mBufferedActiveTransforms);
PX_ASSERT(!mScene);
PX_ASSERT(!mCpuDispatcher);
// PX_ASSERT(!mMaterial);
#if PX_SUPPORT_GPU_PHYSX
PX_ASSERT(!mCudaContextManager);
#endif
PX_ASSERT(!mCooking);
PX_ASSERT(!mPhysics);
PX_ASSERT(!mFoundation);
if(SCRATCH_BLOCK_SIZE)
SAMPLE_FREE(mScratchBlock);
delete mPicking;
}
PhysXSample::PhysXSample(PhysXSampleApplication& app, PxU32 maxSubSteps) :
mInitialDebugRender(false),
mCreateCudaCtxManager(true),
mCreateGroundPlane(true),
mStepperType(FIXED_STEPPER),
mMaxNumSubSteps(maxSubSteps),
mNbThreads(1),
mDefaultDensity(20.0f),
mDisplayFPS(true),
mPause(app.mPause),
mOneFrameUpdate(app.mOneFrameUpdate),
mShowHelp(app.mShowHelp),
mShowDescription(app.mShowDescription),
mShowExtendedHelp(app.mShowExtendedHelp),
mHideGraphics(false),
mEnableAutoFlyCamera(false),
mCameraController(app.mCameraController),
mPvdParams(app.mPvdParams),
mApplication(app),
mFoundation(NULL),
mPhysics(NULL),
mCooking(NULL),
mScene(NULL),
mMaterial(NULL),
mCpuDispatcher(NULL),
mPvd(NULL),
mTransport(NULL),
#if PX_SUPPORT_GPU_PHYSX
mCudaContextManager(NULL),
#endif
mManagedMaterials(app.mManagedMaterials),
mSampleInputAsset(NULL),
mBufferedActiveTransforms(NULL),
mActiveTransformCount(0),
mActiveTransformCapacity(0),
mIsFlyCamera(false),
mMeshTag(0),
mFilename(NULL),
mScale(1.0f),
mDebugRenderScale(1.0f),
mWaitForResults(false),
mSavedCameraController(NULL),
mDebugStepper(0.016666660f),
mFixedStepper(0.016666660f, maxSubSteps),
mVariableStepper(1.0f / 80.0f, 1.0f / 40.0f, maxSubSteps),
mWireFrame(false),
mSimulationTime(0.0f),
mPicked(false),
mExtendedHelpPage(0),
mDebugObjectType(DEBUG_OBJECT_BOX)//,
{
mDebugStepper.setSample(this);
mFixedStepper.setSample(this);
mVariableStepper.setSample(this);
mScratchBlock = SCRATCH_BLOCK_SIZE ? SAMPLE_ALLOC(SCRATCH_BLOCK_SIZE) : 0;
mFlyCameraController.init(PxVec3(0.0f, 10.0f, 0.0f), PxVec3(0.0f, 0.0f, 0.0f));
mPicking = new physx::Picking(*this);
mDeletedActors.clear();
}
void PhysXSample::render()
{
PxU32 nbVisible = 0;
if(!mHideGraphics)
{
PX_PROFILE_ZONE("Renderer.CullObjects", 0);
Camera& camera = getCamera();
Renderer* renderer = getRenderer();
for(PxU32 i = 0; i < mRenderActors.size(); ++i)
{
RenderBaseActor* renderActor = mRenderActors[i];
if(camera.mPerformVFC &&
renderActor->getEnableCameraCull() &&
getCamera().cull(renderActor->getWorldBounds())==PLANEAABB_EXCLUSION)
continue;
renderActor->render(*renderer, mManagedMaterials[MATERIAL_GREY], mWireFrame);
++nbVisible;
}
//if(camera.mPerformVFC)
//shdfnd::printFormatted("Nb visible: %d\n", nbVisible);
}
RenderPhysX3Debug* debugRender = getDebugRenderer();
if(debugRender)
debugRender->queueForRenderTriangle();
}
void PhysXSample::displayFPS()
{
if(!mDisplayFPS)
return;
char fpsText[512];
sprintf(fpsText, "%0.2f fps", mFPS.getFPS());
Renderer* renderer = getRenderer();
const PxU32 yInc = 18;
renderer->print(10, getCamera().getScreenHeight() - yInc*2, fpsText);
// sprintf(fpsText, "%d visible objects", mNbVisible);
// renderer->print(10, mCamera.getScreenHeight() - yInc*3, fpsText);
}
void PhysXSample::onShutdown()
{
//mScene->fetchResults(true);
#if defined(RENDERER_TABLET)
getRenderer()->releaseAllButtons();
#endif
releaseAll(mRenderActors);
releaseAll(mRenderTextures);
releaseAll(mRenderMaterials);
{
PxSceneWriteLock scopedLock(*mScene);
releaseAll(mPhysicsActors);
}
SAMPLE_FREE(mBufferedActiveTransforms);
mFixedStepper.shutdown();
mDebugStepper.shutdown();
mVariableStepper.shutdown();
const size_t nbManagedAssets = mManagedAssets.size();
if(nbManagedAssets)
{
SampleAssetManager* assetManager = mApplication.getAssetManager();
for(PxU32 i=0; i<nbManagedAssets; i++)
assetManager->returnAsset(*mManagedAssets[i]);
}
mManagedAssets.clear();
mApplication.getPlatform()->getSampleUserInput()->shutdown();
if(mSampleInputAsset)
{
mApplication.getAssetManager()->returnAsset(*mSampleInputAsset);
mSampleInputAsset = NULL;
}
mPhysics->unregisterDeletionListener(*this);
SAFE_RELEASE(mScene);
SAFE_RELEASE(mCpuDispatcher);
// SAFE_RELEASE(mMaterial);
SAFE_RELEASE(mCooking);
PxCloseExtensions();
SAFE_RELEASE(mPhysics);
#if PX_SUPPORT_GPU_PHYSX
SAFE_RELEASE(mCudaContextManager);
#endif
SAFE_RELEASE(mPvd);
SAFE_RELEASE(mTransport);
SAFE_RELEASE(mFoundation);
}
//#define USE_MBP
#ifdef USE_MBP
static void setupMBP(PxScene& scene)
{
const float range = 1000.0f;
const PxU32 subdiv = 4;
// const PxU32 subdiv = 1;
// const PxU32 subdiv = 2;
// const PxU32 subdiv = 8;
const PxVec3 min(-range);
const PxVec3 max(range);
const PxBounds3 globalBounds(min, max);
PxBounds3 bounds[256];
const PxU32 nbRegions = PxBroadPhaseExt::createRegionsFromWorldBounds(bounds, globalBounds, subdiv);
for(PxU32 i=0;i<nbRegions;i++)
{
PxBroadPhaseRegion region;
region.bounds = bounds[i];
region.userData = (void*)i;
scene.addBroadPhaseRegion(region);
}
}
#endif
void PhysXSample::onInit()
{
//Recording memory allocations is necessary if you want to
//use the memory facilities in PVD effectively. Since PVD isn't necessarily connected
//right away, we add a mechanism that records all outstanding memory allocations and
//forwards them to PVD when it does connect.
//This certainly has a performance and memory profile effect and thus should be used
//only in non-production builds.
bool recordMemoryAllocations = true;
#ifdef RENDERER_ANDROID
const bool useCustomTrackingAllocator = false;
#else
const bool useCustomTrackingAllocator = true;
#endif
PxAllocatorCallback* allocator = &gDefaultAllocatorCallback;
if(useCustomTrackingAllocator)
allocator = getSampleAllocator(); //optional override that will track memory allocations
mFoundation = PxCreateFoundation(PX_FOUNDATION_VERSION, *allocator, getSampleErrorCallback());
if(!mFoundation)
fatalError("PxCreateFoundation failed!");
#if PX_SUPPORT_GPU_PHYSX
if(mCreateCudaCtxManager)
{
PxCudaContextManagerDesc cudaContextManagerDesc;
#if defined(RENDERER_ENABLE_CUDA_INTEROP)
if (!mApplication.getCommandLine().hasSwitch("nointerop"))
{
switch(getRenderer()->getDriverType())
{
case Renderer::DRIVER_DIRECT3D11:
cudaContextManagerDesc.interopMode = PxCudaInteropMode::D3D11_INTEROP;
break;
case Renderer::DRIVER_OPENGL:
cudaContextManagerDesc.interopMode = PxCudaInteropMode::OGL_INTEROP;
break;
}
cudaContextManagerDesc.graphicsDevice = getRenderer()->getDevice();
}
#endif
mCudaContextManager = PxCreateCudaContextManager(*mFoundation, cudaContextManagerDesc);
if( mCudaContextManager )
{
if( !mCudaContextManager->contextIsValid() )
{
mCudaContextManager->release();
mCudaContextManager = NULL;
}
}
}
#endif
createPvdConnection();
PxTolerancesScale scale;
customizeTolerances(scale);
mPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *mFoundation, scale, recordMemoryAllocations, mPvd);
if(!mPhysics)
fatalError("PxCreatePhysics failed!");
if(!PxInitExtensions(*mPhysics, mPvd))
fatalError("PxInitExtensions failed!");
PxCookingParams params(scale);
params.meshWeldTolerance = 0.001f;
params.meshPreprocessParams = PxMeshPreprocessingFlags(PxMeshPreprocessingFlag::eWELD_VERTICES);
params.buildGPUData = true; //Enable GRB data being produced in cooking.
mCooking = PxCreateCooking(PX_PHYSICS_VERSION, *mFoundation, params);
if(!mCooking)
fatalError("PxCreateCooking failed!");
mPhysics->registerDeletionListener(*this, PxDeletionEventFlag::eUSER_RELEASE);
// setup default material...
mMaterial = mPhysics->createMaterial(0.5f, 0.5f, 0.1f);
if(!mMaterial)
fatalError("createMaterial failed!");
#if defined(RENDERER_TABLET)
// load touchscreen control material
{
SampleFramework::SampleAsset* ps_asset = getAsset(controlMaterialPath, SampleFramework::SampleAsset::ASSET_MATERIAL);
mManagedAssets.push_back(ps_asset);
PX_ASSERT(ps_asset->getType() == SampleFramework::SampleAsset::ASSET_MATERIAL);
SampleFramework::SampleMaterialAsset* mat_ps_asset = static_cast<SampleFramework::SampleMaterialAsset*>(ps_asset);
if(mat_ps_asset->getNumVertexShaders() > 0)
{
RenderMaterial* mat = SAMPLE_NEW(RenderMaterial)(*getRenderer(), mat_ps_asset->getMaterial(0), mat_ps_asset->getMaterialInstance(0), MATERIAL_CONTROLS);
mRenderMaterials.push_back(mat);
}
}
// load touchscreen button material
{
SampleFramework::SampleAsset* ps_asset = getAsset(buttonMaterialPath, SampleFramework::SampleAsset::ASSET_MATERIAL);
mManagedAssets.push_back(ps_asset);
PX_ASSERT(ps_asset->getType() == SampleFramework::SampleAsset::ASSET_MATERIAL);
SampleFramework::SampleMaterialAsset* mat_ps_asset = static_cast<SampleFramework::SampleMaterialAsset*>(ps_asset);
if(mat_ps_asset->getNumVertexShaders() > 0)
{
RenderMaterial* mat = SAMPLE_NEW(RenderMaterial)(*getRenderer(), mat_ps_asset->getMaterial(0), mat_ps_asset->getMaterialInstance(0), MATERIAL_BUTTONS);
mRenderMaterials.push_back(mat);
}
}
Renderer* renderer = getRenderer();
RenderMaterial* controlMaterial = getMaterial(MATERIAL_CONTROLS);
renderer->initControls(controlMaterial->mRenderMaterial,
controlMaterial->mRenderMaterialInstance);
RenderMaterial* buttonMaterial = getMaterial(MATERIAL_BUTTONS);
// add buttons for common use
PxReal yInc = -0.12f;
PxVec2 leftBottom(0.58f, 0.90f);
PxVec2 rightTop(0.99f, 0.82f);
renderer->addButton(leftBottom, rightTop, NULL,
buttonMaterial->mRenderMaterial, buttonMaterial->mRenderMaterialInstance);
leftBottom.y += yInc; rightTop.y += yInc;
renderer->addButton(leftBottom, rightTop, NULL,
buttonMaterial->mRenderMaterial, buttonMaterial->mRenderMaterialInstance);
leftBottom.y += yInc; rightTop.y += yInc;
renderer->addButton(leftBottom, rightTop, NULL,
buttonMaterial->mRenderMaterial, buttonMaterial->mRenderMaterialInstance);
leftBottom.y += yInc; rightTop.y += yInc;
renderer->addButton(leftBottom, rightTop, NULL,
buttonMaterial->mRenderMaterial, buttonMaterial->mRenderMaterialInstance);
leftBottom.y += yInc; rightTop.y += yInc;
// add buttons for individual sample
leftBottom.y += yInc; rightTop.y += yInc;
renderer->addButton(leftBottom, rightTop, NULL,
buttonMaterial->mRenderMaterial, buttonMaterial->mRenderMaterialInstance);
leftBottom.y += yInc; rightTop.y += yInc;
renderer->addButton(leftBottom, rightTop, NULL,
buttonMaterial->mRenderMaterial, buttonMaterial->mRenderMaterialInstance);
leftBottom.y += yInc; rightTop.y += yInc;
renderer->addButton(leftBottom, rightTop, NULL,
buttonMaterial->mRenderMaterial, buttonMaterial->mRenderMaterialInstance);
leftBottom.y += yInc; rightTop.y += yInc;
renderer->addButton(leftBottom, rightTop, NULL,
buttonMaterial->mRenderMaterial, buttonMaterial->mRenderMaterialInstance);
leftBottom.y += yInc; rightTop.y += yInc;
renderer->addButton(leftBottom, rightTop, NULL,
buttonMaterial->mRenderMaterial, buttonMaterial->mRenderMaterialInstance);
// quick access button
leftBottom.y += yInc; rightTop.y += yInc;
renderer->addButton(leftBottom, rightTop, NULL,
buttonMaterial->mRenderMaterial, buttonMaterial->mRenderMaterialInstance);
// next, previous buttons
leftBottom.x = -0.4f;
leftBottom.y = 0.9f;
rightTop.x = -0.1f;
rightTop.y = 0.82f;
renderer->addButton(leftBottom, rightTop, NULL,
buttonMaterial->mRenderMaterial, buttonMaterial->mRenderMaterialInstance);
leftBottom.x = 0.1f;
leftBottom.y = 0.9f;
rightTop.x = 0.4f;
rightTop.y = 0.82f;
renderer->addButton(leftBottom, rightTop, NULL,
buttonMaterial->mRenderMaterial, buttonMaterial->mRenderMaterialInstance);
#endif
PX_ASSERT(NULL == mScene);
PxSceneDesc sceneDesc(mPhysics->getTolerancesScale());
sceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
getDefaultSceneDesc(sceneDesc);
if(!sceneDesc.cpuDispatcher)
{
mCpuDispatcher = PxDefaultCpuDispatcherCreate(mNbThreads);
if(!mCpuDispatcher)
fatalError("PxDefaultCpuDispatcherCreate failed!");
sceneDesc.cpuDispatcher = mCpuDispatcher;
}
if(!sceneDesc.filterShader)
sceneDesc.filterShader = getSampleFilterShader();
#if PX_SUPPORT_GPU_PHYSX
if(!sceneDesc.gpuDispatcher && mCudaContextManager)
sceneDesc.gpuDispatcher = mCudaContextManager->getGpuDispatcher();
#endif
//sceneDesc.frictionType = PxFrictionType::eTWO_DIRECTIONAL;
//sceneDesc.frictionType = PxFrictionType::eONE_DIRECTIONAL;
sceneDesc.flags |= PxSceneFlag::eENABLE_GPU_DYNAMICS;
sceneDesc.flags |= PxSceneFlag::eENABLE_PCM;
//sceneDesc.flags |= PxSceneFlag::eENABLE_AVERAGE_POINT;
sceneDesc.flags |= PxSceneFlag::eENABLE_STABILIZATION;
//sceneDesc.flags |= PxSceneFlag::eADAPTIVE_FORCE;
sceneDesc.flags |= PxSceneFlag::eENABLE_ACTIVETRANSFORMS;
sceneDesc.sceneQueryUpdateMode = PxSceneQueryUpdateMode::eBUILD_ENABLED_COMMIT_DISABLED;
//sceneDesc.flags |= PxSceneFlag::eDISABLE_CONTACT_CACHE;
sceneDesc.broadPhaseType = PxBroadPhaseType::eGPU;
sceneDesc.gpuMaxNumPartitions = 8;
#ifdef USE_MBP
sceneDesc.broadPhaseType = PxBroadPhaseType::eMBP;
#endif
customizeSceneDesc(sceneDesc);
mScene = mPhysics->createScene(sceneDesc);
if(!mScene)
fatalError("createScene failed!");
PxSceneWriteLock scopedLock(*mScene);
PxSceneFlags flag = mScene->getFlags();
PX_UNUSED(flag);
mScene->setVisualizationParameter(PxVisualizationParameter::eSCALE, mInitialDebugRender ? mDebugRenderScale : 0.0f);
mScene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_SHAPES, 1.0f);
PxPvdSceneClient* pvdClient = mScene->getScenePvdClient();
if(pvdClient)
{
pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_CONSTRAINTS, true);
pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_CONTACTS, true);
pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_SCENEQUERIES, true);
}
#ifdef USE_MBP
setupMBP(*mScene);
#endif
mApplication.refreshVisualizationMenuState(PxVisualizationParameter::eCOLLISION_SHAPES);
mApplication.applyDefaultVisualizationSettings();
mApplication.setMouseCursorHiding(false);
mApplication.setMouseCursorRecentering(false);
mCameraController.setMouseLookOnMouseButton(false);
mCameraController.setMouseSensitivity(1.0f);
if(mCreateGroundPlane)
createGrid();
LOG_INFO("PhysXSample", "Init sample ok!");
}
RenderMaterial* PhysXSample::getMaterial(PxU32 materialID)
{
for(PxU32 i = 0; i < mRenderMaterials.size(); ++i)
{
if(mRenderMaterials[i]->mID == materialID)
{
return mRenderMaterials[i];
}
}
return NULL;
}
Stepper* PhysXSample::getStepper()
{
switch(mStepperType)
{
case DEFAULT_STEPPER:
return &mDebugStepper;
case FIXED_STEPPER:
return &mFixedStepper;
default:
return &mVariableStepper;
};
}
// ### PT: TODO: refactor this with onTickPreRender
void PhysXSample::freeDeletedActors()
{
getRenderer()->finishRendering();
// delete buffered render actors
for (size_t i = mDeletedRenderActors.size(); i--;)
{
mDeletedRenderActors[i]->release();
}
mDeletedRenderActors.clear();
}
void PhysXSample::onTickPreRender(float dtime)
{
// delete buffered render actors
for (size_t i = mDeletedRenderActors.size(); i--;)
{
mDeletedRenderActors[i]->release();
}
mDeletedRenderActors.clear();
#if PX_PROFILE
{
#endif
PxSceneWriteLock scopedLock(*mScene);
mApplication.baseTickPreRender(dtime);
mFPS.update();
#if PX_PROFILE
}
#endif
if(!isPaused())
{
Stepper* stepper = getStepper();
mWaitForResults = false;
if(mScene)
{
updateRenderObjectsAsync(dtime);
#if !PX_PROFILE
mWaitForResults = stepper->advance(mScene, dtime, mScratchBlock, SCRATCH_BLOCK_SIZE);
// tells the stepper shape data is not going to be accessed until next frame
// (frame ends with stepper->wait(mScene))
stepper->renderDone();
#else
// in profile builds we run the whole frame sequentially
// simulate, wait, update render objects, render
{
mWaitForResults = stepper->advance(mScene, dtime, mScratchBlock, SCRATCH_BLOCK_SIZE);
stepper->renderDone();
if (mWaitForResults)
{
stepper->wait(mScene);
mSimulationTime = stepper->getSimulationTime();
}
}
// update render objects immediately
if (mWaitForResults)
{
bufferActiveTransforms();
updateRenderObjectsSync(dtime);
if (mOneFrameUpdate)
updateRenderObjectsAsync(dtime);
}
#endif
}
}
// profile builds should update render actors
// and debug draw immediately to avoid one frame lag
#if PX_PROFILE
RenderPhysX3Debug* debugRenderer = getDebugRenderer();
if(debugRenderer && mScene)
{
const PxRenderBuffer& debugRenderable = mScene->getRenderBuffer();
debugRenderer->update(debugRenderable);
updateRenderObjectsDebug(dtime);
}
#endif
if(mPicking)
{
mPicking->tick();
}
}
void PhysXSample::onTickPostRender(float dtime)
{
#if !PX_PROFILE
if(!isPaused())
{
if(mScene && mWaitForResults)
{
Stepper* stepper = getStepper();
stepper->wait(mScene);
mSimulationTime = stepper->getSimulationTime();
bufferActiveTransforms();
// make sure that in single step mode, the render objects get updated immediately
if (mOneFrameUpdate)
{
updateRenderObjectsSync(dtime);
updateRenderObjectsAsync(dtime);
}
else
updateRenderObjectsSync(dtime);
}
}
#else
if(!isPaused() && mScene && mWaitForResults )
{
Stepper* stepper = getStepper();
//stepper->wait(mScene);
stepper->postRender(dtime);
}
#endif
RenderPhysX3Debug* debugRenderer = getDebugRenderer();
if(debugRenderer && mScene)
{
const PxRenderBuffer& debugRenderable = mScene->getRenderBuffer();
PX_UNUSED(debugRenderable);
debugRenderer->clear();
#if !PX_PROFILE
debugRenderer->update(debugRenderable);
updateRenderObjectsDebug(dtime);
#endif
renderScene();
}
if(mOneFrameUpdate)
{
mOneFrameUpdate = false;
if (!isPaused()) togglePause();
}
#ifdef PRINT_BLOCK_COUNTERS
static PxU32 refMax = -1;
PxU32 newMax = getActiveScene().getMaxNbContactDataBlocksUsed();
if(refMax != newMax)
shdfnd::printFormatted("max blocks used: %d\n",newMax);
refMax = newMax;
#endif // PRINT_BLOCK_COUNTERS
}
void PhysXSample::saveUserInputs()
{
char name[256];
char sBuffer[512];
const char* sampleName = mApplication.mRunning->getName();
SampleUserInput* sampleUserInput = mApplication.getPlatform()->getSampleUserInput();
if(!sampleUserInput)
return;
strcpy(name,"input");
strcat(name,"/UserInputList.txt");
if(getSampleOutputDirManager().getFilePath(name, sBuffer, false))
{
sprintf(name, "input/%s/%sUserInputList.txt", sampleName,mApplication.getPlatform()->getPlatformName());
if(getSampleOutputDirManager().getFilePath(name, sBuffer, false))
{
SampleFramework::File* file = NULL;
PxToolkit::fopen_s(&file, sBuffer, "w");
if(file)
{
fputs("UserInputList:\n",file);
fputs("----------------------------------------\n",file);
const std::vector<UserInput>& ilist = sampleUserInput->getUserInputList();
for (size_t i = 0; i < ilist.size(); i++)
{
const UserInput& ui = ilist[i];
fputs(ui.m_IdName,file);
fputs("\n",file);
}
fclose(file);
}
}
}
}
void PhysXSample::saveInputEvents(const std::vector<const InputEvent*>& inputEvents)
{
char name[256];
char sBuffer[512];
const char* sampleName = mApplication.mRunning->getName();
SampleUserInput* sampleUserInput = mApplication.getPlatform()->getSampleUserInput();
if(!sampleUserInput)
return;
strcpy(name,"input");
strcat(name,"/InputEventList.txt");
if(getSampleOutputDirManager().getFilePath(name, sBuffer, false))
{
sprintf(name, "input/%s/%sInputEventList.txt", sampleName,mApplication.getPlatform()->getPlatformName());
if(getSampleOutputDirManager().getFilePath(name, sBuffer, false))
{
SampleFramework::File* file = NULL;
PxToolkit::fopen_s(&file, sBuffer, "w");
if(file)
{
fputs("InputEventList:\n",file);
fputs("----------------------------------------\n",file);
for (size_t i = 0; i < inputEvents.size(); i++)
{
const InputEvent* inputEvent = inputEvents[i];
if(!inputEvent)
continue;
const std::vector<size_t>* userInputs = sampleUserInput->getUserInputs(inputEvent->m_Id);
const char* name = sampleUserInput->translateInputEventIdToName(inputEvent->m_Id);
if(userInputs && !userInputs->empty() && name)
{
fputs(name,file);
fputs("\n",file);
}
}
fclose(file);
}
}
}
}
void PhysXSample::parseSampleOutputAsset(const char* sampleName,PxU32 userInputCS, PxU32 inputEventCS)
{
char name[256];
char sBuffer[512];
SampleUserInput* sampleUserInput = mApplication.getPlatform()->getSampleUserInput();
if(!sampleUserInput)
return;
sprintf(name, "input/%s/%sInputMapping.ods", sampleName,mApplication.getPlatform()->getPlatformName());
if(getSampleOutputDirManager().getFilePath(name, sBuffer, false))
{
SampleInputMappingAsset* inputAsset = NULL;
SampleFramework::File* fp = NULL;
PxToolkit::fopen_s(&fp, sBuffer, "r");
if(fp)
{
inputAsset = SAMPLE_NEW(SampleInputMappingAsset)(fp,sBuffer, false, userInputCS, inputEventCS);
if(!inputAsset->isOk())
{
DELETESINGLE(inputAsset);
fp = NULL;
}
}
if(inputAsset)
{
std::vector<SampleInputMapping> inputMappings;
for (size_t i = inputAsset->getSampleInputData().size();i--;)
{
const SampleInputData& data = inputAsset->getSampleInputData()[i];
size_t userInputIndex;
PxI32 userInputId = sampleUserInput->translateUserInputNameToId(data.m_UserInputName,userInputIndex);
size_t inputEventIndex;
PxI32 inputEventId = sampleUserInput->translateInputEventNameToId(data.m_InputEventName, inputEventIndex);
if(userInputId >= 0 && inputEventId >= 0)
{
SampleInputMapping mapping;
mapping.m_InputEventId = inputEventId;
mapping.m_InputEventIndex = inputEventIndex;
mapping.m_UserInputId = userInputId;
mapping.m_UserInputIndex = userInputIndex;
inputMappings.push_back(mapping);
}
else
{
//if we get here we read a command mapping from file that is no longer supported in code ... it should be ignored.
}
}
for (size_t i = inputMappings.size(); i--;)
{
const SampleInputMapping& mapping = inputMappings[i];
sampleUserInput->unregisterInputEvent(mapping.m_InputEventId);
}
//now I have the default keys definition left, save it to the mapping
const std::vector<UserInput>& userInputs = sampleUserInput->getUserInputList();
const std::map<physx::PxU16, std::vector<size_t> >& inputEventUserInputMap = sampleUserInput->getInputEventUserInputMap();
std::map<physx::PxU16, std::vector<size_t> >::const_iterator it = inputEventUserInputMap.begin();
std::map<physx::PxU16, std::vector<size_t> >::const_iterator itEnd = inputEventUserInputMap.end();
while (it != itEnd)
{
PxU16 inputEventId = it->first;
const std::vector<size_t>& uis = it->second;
for (size_t j = 0; j < uis.size(); j++)
{
const UserInput& ui = userInputs[uis[j]];
const char* eventName = sampleUserInput->translateInputEventIdToName(inputEventId);
if(eventName)
{
inputAsset->addMapping(ui.m_IdName, eventName);
}
}
++it;
}
for (size_t i = inputMappings.size(); i--;)
{
const SampleInputMapping& mapping = inputMappings[i];
sampleUserInput->registerInputEvent(mapping);
}
}
else
{
// the file does not exist create one
SampleFramework::File* fp = NULL;
PxToolkit::fopen_s(&fp, sBuffer, "w");
if(fp)
{
inputAsset = SAMPLE_NEW(SampleInputMappingAsset)(fp,sBuffer,true,userInputCS, inputEventCS);
const std::vector<UserInput>& userInputs = sampleUserInput->getUserInputList();
const std::map<physx::PxU16, std::vector<size_t> >& inputEventUserInputMap = sampleUserInput->getInputEventUserInputMap();
std::map<physx::PxU16, std::vector<size_t> >::const_iterator it = inputEventUserInputMap.begin();
std::map<physx::PxU16, std::vector<size_t> >::const_iterator itEnd = inputEventUserInputMap.end();
while (it != itEnd)
{
PxU16 inputEventId = it->first;
const std::vector<size_t>& uis = it->second;
for (size_t j = 0; j < uis.size(); j++)
{
const UserInput& ui = userInputs[uis[j]];
const char* eventName = sampleUserInput->translateInputEventIdToName(inputEventId);
if(eventName)
{
inputAsset->addMapping(ui.m_IdName, eventName);
}
}
++it;
}
}
}
if(inputAsset)
{
inputAsset->saveMappings();
DELETESINGLE(inputAsset);
}
}
}
void PhysXSample::prepareInputEventUserInputInfo(const char* sampleName,PxU32 &userInputCS, PxU32 &inputEventCS)
{
SampleUserInput* sampleUserInput = mApplication.getPlatform()->getSampleUserInput();
if(!sampleUserInput)
return;
saveUserInputs();
std::vector<const InputEvent*> inputEvents;
mApplication.collectInputEvents(inputEvents);
for (size_t i = inputEvents.size(); i--;)
{
const InputEvent* ie = inputEvents[i];
inputEventCS += ie->m_Id;
const std::vector<size_t>* userInputs = sampleUserInput->getUserInputs(ie->m_Id);
if(userInputs)
{
for (size_t j = userInputs->size(); j--;)
{
const UserInput& ui = sampleUserInput->getUserInputList()[ (*userInputs)[j] ];
userInputCS += (ui.m_Id + ie->m_Id);
}
}
}
inputEventCS = inputEventCS + ((PxU16)sampleUserInput->getInputEventList().size() << 16);
userInputCS = userInputCS + ((PxU16)sampleUserInput->getUserInputList().size() << 16);
saveInputEvents(inputEvents);
char name[256];
strcpy(name,"input/");
strcat(name,mApplication.getPlatform()->getPlatformName());
strcat(name,"/");
strcat(name,sampleName);
strcat(name,"InputMapping.ods");
// load the additional mapping file
mSampleInputAsset = (SampleInputAsset*)getAsset(name, SampleAsset::ASSET_INPUT, false);
}
void PhysXSample::unregisterInputEvents()
{
mApplication.getPlatform()->getSampleUserInput()->shutdown();
}
void PhysXSample::registerInputEvents(bool ignoreSaved)
{
const char* sampleName = mApplication.mRunning->getName();
SampleUserInput* sampleUserInput = mApplication.getPlatform()->getSampleUserInput();
if(!sampleUserInput)
return;
PxU32 inputEventCS = 0;
PxU32 userInputCS = 0;
prepareInputEventUserInputInfo(sampleName, inputEventCS, userInputCS);
// register the additional mapping
if(mSampleInputAsset)
{
std::vector<SampleInputMapping> inputMappings;
for (size_t i = mSampleInputAsset->GetSampleInputData().size();i--;)
{
const SampleInputData& data = mSampleInputAsset->GetSampleInputData()[i];
size_t userInputIndex;
PxI32 userInputId = sampleUserInput->translateUserInputNameToId(data.m_UserInputName,userInputIndex);
size_t inputEventIndex;
PxI32 inputEventId = sampleUserInput->translateInputEventNameToId(data.m_InputEventName, inputEventIndex);
if(userInputId >= 0 && inputEventId >= 0)
{
SampleInputMapping mapping;
mapping.m_InputEventId = inputEventId;
mapping.m_InputEventIndex = inputEventIndex;
mapping.m_UserInputId = userInputId;
mapping.m_UserInputIndex = userInputIndex;
inputMappings.push_back(mapping);
}
else
{
PX_ASSERT(0);
}
}
for (size_t i = inputMappings.size(); i--;)
{
const SampleInputMapping& mapping = inputMappings[i];
sampleUserInput->registerInputEvent(mapping);
}
}
#if !defined(RENDERER_TABLET)
if (!ignoreSaved)
parseSampleOutputAsset(sampleName, inputEventCS, userInputCS);
#endif
}
void PhysXSample::onKeyDownEx(SampleFramework::SampleUserInput::KeyCode keyCode, PxU32 param)
{
}
void PhysXSample::collectInputEvents(std::vector<const SampleFramework::InputEvent*>& inputEvents)
{
//digital keyboard events
DIGITAL_INPUT_EVENT_DEF(SPAWN_DEBUG_OBJECT, WKEY_1, XKEY_1, X1KEY_1, PS3KEY_1, PS4KEY_1, AKEY_UNKNOWN, OSXKEY_1, IKEY_UNKNOWN, LINUXKEY_1, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(PAUSE_SAMPLE, WKEY_P, XKEY_P, X1KEY_P, PS3KEY_P, PS4KEY_P, AKEY_UNKNOWN, OSXKEY_P, IKEY_UNKNOWN, LINUXKEY_P, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(STEP_ONE_FRAME, WKEY_O, XKEY_O, X1KEY_O, PS3KEY_O, PS4KEY_O, AKEY_UNKNOWN, OSXKEY_O, IKEY_UNKNOWN, LINUXKEY_O, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(TOGGLE_VISUALIZATION, WKEY_V, XKEY_V, X1KEY_V, PS3KEY_V, PS4KEY_V, AKEY_UNKNOWN, OSXKEY_V, IKEY_UNKNOWN, LINUXKEY_V, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(DECREASE_DEBUG_RENDER_SCALE, WKEY_F7, XKEY_F7, X1KEY_F7, PS3KEY_F7, PS4KEY_F7, AKEY_UNKNOWN, OSXKEY_F7, IKEY_UNKNOWN, LINUXKEY_F7, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(INCREASE_DEBUG_RENDER_SCALE, WKEY_F8, XKEY_F8, X1KEY_F8, PS3KEY_F8, PS4KEY_F8, AKEY_UNKNOWN, OSXKEY_F8, IKEY_UNKNOWN, LINUXKEY_F8, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(HIDE_GRAPHICS, WKEY_G, XKEY_G, X1KEY_G, PS3KEY_G, PS4KEY_G, AKEY_UNKNOWN, OSXKEY_G, IKEY_UNKNOWN, LINUXKEY_G, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(WIREFRAME, WKEY_N, XKEY_N, X1KEY_N, PS3KEY_N, PS4KEY_N, AKEY_UNKNOWN, OSXKEY_N, IKEY_UNKNOWN, LINUXKEY_N, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(TOGGLE_PVD_CONNECTION, WKEY_F5, XKEY_F5, X1KEY_F5, PS3KEY_F5, PS4KEY_F5, AKEY_UNKNOWN, OSXKEY_F5, IKEY_UNKNOWN, LINUXKEY_F5, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(SHOW_HELP, WKEY_H, XKEY_H, X1KEY_H, PS3KEY_H, PS4KEY_H, AKEY_UNKNOWN, OSXKEY_H, IKEY_UNKNOWN, LINUXKEY_H, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(SHOW_DESCRIPTION, WKEY_F, XKEY_F, X1KEY_F, PS3KEY_F, PS4KEY_F, AKEY_UNKNOWN, OSXKEY_F, IKEY_UNKNOWN, LINUXKEY_F, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(VARIABLE_TIMESTEP, WKEY_T, XKEY_T, X1KEY_T, PS3KEY_T, PS4KEY_T, AKEY_UNKNOWN, OSXKEY_T, IKEY_UNKNOWN, LINUXKEY_T, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(NEXT_PAGE, WKEY_NEXT, XKEY_UNKNOWN, X1KEY_UNKNOWN, PS3KEY_NEXT, PS4KEY_NEXT, AKEY_UNKNOWN, OSXKEY_RIGHT, IKEY_UNKNOWN, LINUXKEY_NEXT, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(PREVIOUS_PAGE, WKEY_PRIOR, XKEY_UNKNOWN, X1KEY_UNKNOWN, PS3KEY_PRIOR, PS4KEY_PRIOR, AKEY_UNKNOWN, OSXKEY_LEFT, IKEY_UNKNOWN, LINUXKEY_PRIOR, WIIUKEY_UNKNOWN );
DIGITAL_INPUT_EVENT_DEF(PROFILE_ONLY_PVD, WKEY_9, XKEY_UNKNOWN, X1KEY_UNKNOWN, PS3KEY_UNKNOWN, PS4KEY_UNKNOWN, AKEY_UNKNOWN, OSXKEY_UNKNOWN, IKEY_UNKNOWN, LINUXKEY_UNKNOWN, WIIUKEY_UNKNOWN );
//DIGITAL_INPUT_EVENT_DEF(PAUSE_SAMPLE, WKEY_P, XKEY_UNKNOWN, X1KEY_UNKNOWN, PS3KEY_UNKNOWN, PS4KEY_UNKNOWN, AKEY_UNKNOWN, OSXKEY_UNKNOWN, IKEY_UNKNOWN, LINUXKEY_UNKNOWN);
//DIGITAL_INPUT_EVENT_DEF(STEP_ONE_FRAME, WKEY_O, XKEY_UNKNOWN, X1KEY_UNKNOWN, PS3KEY_UNKNOWN, PS4KEY_UNKNOWN, AKEY_UNKNOWN, OSXKEY_UNKNOWN, IKEY_UNKNOWN, LINUXKEY_UNKNOWN);
//digital gamepad events
DIGITAL_INPUT_EVENT_DEF(SHOW_HELP, GAMEPAD_SELECT, GAMEPAD_SELECT, GAMEPAD_SELECT, GAMEPAD_SELECT, GAMEPAD_SELECT, AKEY_UNKNOWN, GAMEPAD_SELECT, IKEY_UNKNOWN, LINUXKEY_UNKNOWN, GAMEPAD_SELECT);
DIGITAL_INPUT_EVENT_DEF(SPAWN_DEBUG_OBJECT, GAMEPAD_NORTH, GAMEPAD_NORTH, GAMEPAD_NORTH, GAMEPAD_NORTH, GAMEPAD_NORTH, AKEY_UNKNOWN, GAMEPAD_NORTH, IKEY_UNKNOWN, LINUXKEY_UNKNOWN, GAMEPAD_NORTH);
//digital mouse events (are registered in the individual samples as needed)
//touch events (reserve first 4 buttons for common use, individual samples start from 5)
TOUCH_INPUT_EVENT_DEF(PAUSE_SAMPLE, "Pause", ABUTTON_1, IBUTTON_1);
TOUCH_INPUT_EVENT_DEF(STEP_ONE_FRAME, "Single Step", ABUTTON_2, IBUTTON_2);
}
void PhysXSample::onAnalogInputEvent(const SampleFramework::InputEvent& , float val)
{
}
void PhysXSample::onDigitalInputEvent(const SampleFramework::InputEvent& ie, bool val)
{
if (!mScene) return;
if (val)
{
if (mShowExtendedHelp)
{
switch (ie.m_Id)
{
case NEXT_PAGE:
{
mExtendedHelpPage++;
}
break;
case PREVIOUS_PAGE:
{
if(mExtendedHelpPage)
mExtendedHelpPage--;
}
break;
}
return;
}
switch (ie.m_Id)
{
case SPAWN_DEBUG_OBJECT:
spawnDebugObject();
break;
case PAUSE_SAMPLE:
togglePause();
break;
case STEP_ONE_FRAME:
mOneFrameUpdate = !mOneFrameUpdate;
break;
case TOGGLE_VISUALIZATION:
toggleVisualizationParam(*mScene, PxVisualizationParameter::eSCALE);
break;
case DECREASE_DEBUG_RENDER_SCALE:
{
mDebugRenderScale -= 0.1f;
mDebugRenderScale = PxMax(mDebugRenderScale, 0.f);
mScene->setVisualizationParameter(PxVisualizationParameter::eSCALE, mDebugRenderScale);
break;
}
case INCREASE_DEBUG_RENDER_SCALE:
{
mDebugRenderScale += 0.1f;
mScene->setVisualizationParameter(PxVisualizationParameter::eSCALE, mDebugRenderScale);
break;
}
case HIDE_GRAPHICS:
mHideGraphics = !mHideGraphics;
break;
case WIREFRAME:
{
mWireFrame = !mWireFrame;
break;
}
case TOGGLE_PVD_CONNECTION:
{
togglePvdConnection();
break;
}
case SHOW_HELP:
mShowHelp = !mShowHelp;
if(mShowHelp) mShowDescription=false;
break;
case SHOW_DESCRIPTION:
mShowDescription = !mShowDescription;
if(mShowDescription) mShowHelp=false;
break;
case VARIABLE_TIMESTEP:
mStepperType = (mStepperType == VARIABLE_STEPPER) ? FIXED_STEPPER : VARIABLE_STEPPER;
//mUseFixedStepper = !mUseFixedStepper;
mFixedStepper.reset();
mVariableStepper.reset();
break;
case DELETE_PICKED:
if(mPicked && mPicking)
{
PxActor* pickedActor = mPicking->letGo();
if(pickedActor)
pickedActor->release();
}
break;
case PICKUP:
{
if(mPicking)
{
mPicked = true;
PxU32 width;
PxU32 height;
mApplication.getPlatform()->getWindowSize(width, height);
mPicking->moveCursor(width/2,height/2);
mPicking->lazyPick();
}
}
break;
case PROFILE_ONLY_PVD:
if (mPvdParams.useFullPvdConnection)
{
if(mPvd)
{
mPvd->disconnect();
mPvdParams.useFullPvdConnection = false;
mPvd->connect(*mTransport,physx::PxPvdInstrumentationFlag::ePROFILE);
}
}
break;
default: break;
}
}
else
{
if (mShowExtendedHelp)
{
return;
}
if (ie.m_Id == PICKUP)
{
if(mPicking)
{
mPicked = false;
mPicking->letGo();
}
}
}
}
void PhysXSample::toggleFlyCamera()
{
mIsFlyCamera = !mIsFlyCamera;
if (mIsFlyCamera)
{
mSavedCameraController = getCurrentCameraController();
mApplication.saveCameraState();
mFlyCameraController.init(getCamera().getViewMatrix());
mFlyCameraController.setMouseLookOnMouseButton(false);
mFlyCameraController.setMouseSensitivity(0.2f);
setCameraController(&mFlyCameraController);
}
else
{
mApplication.restoreCameraState();
setCameraController(mSavedCameraController);
}
}
void PhysXSample::togglePause()
{
//unregisterInputEvents();
mPause = !mPause;
if (mEnableAutoFlyCamera)
{
if (mPause)
{
mSavedCameraController = getCurrentCameraController();
mApplication.saveCameraState();
mFlyCameraController.init(getCamera().getViewMatrix());
setCameraController(&mFlyCameraController);
}
else
{
mApplication.restoreCameraState();
setCameraController(mSavedCameraController);
}
}
//registerInputEvents(true);
}
void PhysXSample::showExtendedInputEventHelp(PxU32 x, PxU32 y)
{
const PxReal scale = 0.5f;
const PxReal shadowOffset = 6.0f;
const RendererColor textColor(255, 255, 255, 255);
Renderer* renderer = getRenderer();
const PxU32 yInc = 18;
char message[512];
PxU32 width = 0;
PxU32 height = 0;
renderer->getWindowSize(width, height);
y += yInc;
PxU16 numIe = (height - y)/yInc - 8;
const std::vector<InputEvent> inputEventList = getApplication().getPlatform()->getSampleUserInput()->getInputEventList();
const std::vector<InputEventName> inputEventNameList = getApplication().getPlatform()->getSampleUserInput()->getInputEventNameList();
size_t maxHelpPage = inputEventList.size()/numIe;
if(maxHelpPage < mExtendedHelpPage)
mExtendedHelpPage = (PxU8) maxHelpPage;
PxU16 printed = 0;
PxU16 startPrint = 0;
for (size_t i = 0; i < inputEventList.size(); i++)
{
const InputEvent& ie = inputEventList[i];
const char* msg = mApplication.inputInfoMsg("Press "," to ", ie.m_Id, -1);
if(msg)
{
startPrint++;
if(startPrint <= numIe*mExtendedHelpPage)
continue;
strcpy(message,msg);
strcat(message, inputEventNameList[i].m_Name);
renderer->print(x, y, message, scale, shadowOffset, textColor);
y += yInc;
printed++;
if(printed >= numIe)
{
break;
}
}
}
if(printed == 0)
{
if(mExtendedHelpPage)
mExtendedHelpPage--;
}
y += yInc;
const char* msg = mApplication.inputInfoMsg("Press "," to show next/previous page", NEXT_PAGE, PREVIOUS_PAGE);
if(msg)
renderer->print(x, y += yInc, msg, scale, shadowOffset, textColor);
}
///////////////////////////////////////////////////////////////////////////////
RenderBaseActor* PhysXSample::createRenderBoxFromShape(PxRigidActor* actor, PxShape* shape, RenderMaterial* material, const PxReal* uvs)
{
RenderBaseActor* shapeRenderActor = NULL;
Renderer* renderer = getRenderer();
PX_ASSERT(renderer);
PxGeometryType::Enum geomType = shape->getGeometryType();
PX_ASSERT(geomType==PxGeometryType::eBOX);
switch(geomType)
{
case PxGeometryType::eBOX:
{
// Get physics geometry
PxBoxGeometry geometry;
bool status = shape->getBoxGeometry(geometry);
PX_ASSERT(status);
PX_UNUSED(status);
// Create render object
shapeRenderActor = SAMPLE_NEW(RenderBoxActor)(*renderer, geometry.halfExtents, uvs);
}
break;
default: {}
};
if(shapeRenderActor)
{
link(shapeRenderActor, shape, actor);
mRenderActors.push_back(shapeRenderActor);
shapeRenderActor->setRenderMaterial(material);
shapeRenderActor->setEnableCameraCull(true);
}
return shapeRenderActor;
}
RenderBaseActor* PhysXSample::createRenderObjectFromShape(PxRigidActor* actor, PxShape* shape, RenderMaterial* material)
{
PX_ASSERT(getRenderer());
RenderBaseActor* shapeRenderActor = NULL;
Renderer& renderer = *getRenderer();
PxGeometryHolder geom = shape->getGeometry();
switch(geom.getType())
{
case PxGeometryType::eSPHERE:
shapeRenderActor = SAMPLE_NEW(RenderSphereActor)(renderer, geom.sphere().radius);
break;
case PxGeometryType::ePLANE:
shapeRenderActor = SAMPLE_NEW(RenderGridActor)(renderer, 50, 1.f, PxShapeExt::getGlobalPose(*shape, *actor).q);
break;
case PxGeometryType::eCAPSULE:
shapeRenderActor = SAMPLE_NEW(RenderCapsuleActor)(renderer, geom.capsule().radius, geom.capsule().halfHeight);
break;
case PxGeometryType::eBOX:
shapeRenderActor = SAMPLE_NEW(RenderBoxActor)(renderer, geom.box().halfExtents);
break;
case PxGeometryType::eCONVEXMESH:
{
PxConvexMesh* convexMesh = geom.convexMesh().convexMesh;
// ### doesn't support scale
PxU32 nbVerts = convexMesh->getNbVertices();
PX_UNUSED(nbVerts);
const PxVec3* convexVerts = convexMesh->getVertices();
const PxU8* indexBuffer = convexMesh->getIndexBuffer();
PxU32 nbPolygons = convexMesh->getNbPolygons();
PxU32 totalNbTris = 0;
PxU32 totalNbVerts = 0;
for(PxU32 i=0;i<nbPolygons;i++)
{
PxHullPolygon data;
bool status = convexMesh->getPolygonData(i, data);
PX_ASSERT(status);
PX_UNUSED(status);
totalNbVerts += data.mNbVerts;
totalNbTris += data.mNbVerts - 2;
}
PxVec3Alloc* allocVerts = SAMPLE_NEW(PxVec3Alloc)[totalNbVerts];
PxVec3Alloc* allocNormals = SAMPLE_NEW(PxVec3Alloc)[totalNbVerts];
PxReal* UVs = (PxReal*)SAMPLE_ALLOC(sizeof(PxReal)*(totalNbVerts * 2));
PxU16* faces = (PxU16*)SAMPLE_ALLOC(sizeof(PxU16)*totalNbTris*3);
PxU16* triangles = faces;
PxVec3* vertices = allocVerts,
* normals = allocNormals;
PxU32 offset = 0;
for(PxU32 i=0;i<nbPolygons;i++)
{
PxHullPolygon face;
bool status = convexMesh->getPolygonData(i, face);
PX_ASSERT(status);
PX_UNUSED(status);
const PxU8* faceIndices = indexBuffer+face.mIndexBase;
for(PxU32 j=0;j<face.mNbVerts;j++)
{
vertices[offset+j] = convexVerts[faceIndices[j]];
normals[offset+j] = PxVec3(face.mPlane[0], face.mPlane[1], face.mPlane[2]);
}
for(PxU32 j=2;j<face.mNbVerts;j++)
{
*triangles++ = PxU16(offset);
*triangles++ = PxU16(offset+j);
*triangles++ = PxU16(offset+j-1);
}
offset += face.mNbVerts;
}
// prepare UVs for convex:
// filling like this
// vertice #0 - 0,0
// vertice #1 - 0,1
// vertice #2 - 1,0
// vertice #3 - 0,0
// ...
for(PxU32 i = 0; i < totalNbVerts; ++i)
{
PxU32 c = i % 3;
if(c == 0)
{
UVs[2 * i] = 0.0f;
UVs[2 * i + 1] = 0.0f;
}
else if(c == 1)
{
UVs[2 * i] = 0.0f;
UVs[2 * i + 1] = 1.0f;
}
else if(c == 2)
{
UVs[2 * i] = 1.0f;
UVs[2 * i + 1] = 0.0f;
}
}
PX_ASSERT(offset==totalNbVerts);
shapeRenderActor = SAMPLE_NEW(RenderMeshActor)(renderer, vertices, totalNbVerts, normals, UVs, faces, NULL, totalNbTris);
shapeRenderActor->setMeshScale(geom.convexMesh().scale);
SAMPLE_FREE(faces);
SAMPLE_FREE(UVs);
DELETEARRAY(allocVerts);
DELETEARRAY(allocNormals);
}
break;
case PxGeometryType::eTRIANGLEMESH:
{
// Get physics geometry
PxTriangleMesh* tm = geom.triangleMesh().triangleMesh;
const PxU32 nbVerts = tm->getNbVertices();
const PxVec3* verts = tm->getVertices();
const PxU32 nbTris = tm->getNbTriangles();
const void* tris = tm->getTriangles();
const bool has16bitIndices = tm->getTriangleMeshFlags() & PxTriangleMeshFlag::e16_BIT_INDICES ? true : false;
const PxU16* faces16 = has16bitIndices ? (const PxU16*)tris : NULL;
const PxU32* faces32 = has16bitIndices ? NULL : (const PxU32*)tris;
shapeRenderActor = SAMPLE_NEW(RenderMeshActor)(renderer, verts, nbVerts, NULL, NULL, faces16, faces32, nbTris, true);
shapeRenderActor->setMeshScale(geom.triangleMesh().scale);
if (!material)
material = mManagedMaterials[MATERIAL_FLAT];
}
break;
case PxGeometryType::eHEIGHTFIELD:
{
// Get physics geometry
const PxHeightFieldGeometry& geometry = geom.heightField();
const PxReal rs = geometry.rowScale;
const PxReal hs = geometry.heightScale;
const PxReal cs = geometry.columnScale;
// Create render object
PxHeightField* hf = geometry.heightField;
const PxU32 nbCols = hf->getNbColumns();
const PxU32 nbRows = hf->getNbRows();
const PxU32 nbVerts = nbRows * nbCols;
const PxU32 nbFaces = (nbCols - 1) * (nbRows - 1) * 2;
PxHeightFieldSample* sampleBuffer = new PxHeightFieldSample[nbVerts];
hf->saveCells(sampleBuffer, nbVerts * sizeof(PxHeightFieldSample));
PxVec3* vertexes = new PxVec3[nbVerts];
for(PxU32 i = 0; i < nbRows; i++)
{
for(PxU32 j = 0; j < nbCols; j++)
{
vertexes[i * nbCols + j] = PxVec3(PxReal(i) * rs, PxReal(sampleBuffer[j + (i*nbCols)].height) * hs, PxReal(j) * cs);
}
}
PxU32* indices = (PxU32*)SAMPLE_ALLOC(sizeof(PxU32) * nbFaces * 3);
for(PxU32 i = 0; i < (nbCols - 1); ++i)
{
for(PxU32 j = 0; j < (nbRows - 1); ++j)
{
PxU8 tessFlag = sampleBuffer[i+j*nbCols].tessFlag();
PxU32 i0 = j*nbCols + i;
PxU32 i1 = j*nbCols + i + 1;
PxU32 i2 = (j+1) * nbCols + i;
PxU32 i3 = (j+1) * nbCols + i+ 1;
// i2---i3
// | |
// | |
// i0---i1
// this is really a corner vertex index, not triangle index
PxU32 mat0 = hf->getTriangleMaterialIndex((j*nbCols+i)*2);
PxU32 mat1 = hf->getTriangleMaterialIndex((j*nbCols+i)*2+1);
bool hole0 = (mat0 == PxHeightFieldMaterial::eHOLE);
bool hole1 = (mat1 == PxHeightFieldMaterial::eHOLE);
// first triangle
indices[6 * (i * (nbRows - 1) + j) + 0] = hole0 ? i0 : i2; // duplicate i0 to make a hole
indices[6 * (i * (nbRows - 1) + j) + 1] = i0;
indices[6 * (i * (nbRows - 1) + j) + 2] = tessFlag ? i3 : i1;
// second triangle
indices[6 * (i * (nbRows - 1) + j) + 3] = hole1 ? i1 : i3; // duplicate i1 to make a hole
indices[6 * (i * (nbRows - 1) + j) + 4] = tessFlag ? i0 : i2;
indices[6 * (i * (nbRows - 1) + j) + 5] = i1;
}
}
PxU16* indices_16bit = (PxU16*)SAMPLE_ALLOC(sizeof(PxU16) * nbFaces * 3);
for(PxU32 i=0; i< nbFaces; i++)
{
indices_16bit[i*3+0] = indices[i*3+0];
indices_16bit[i*3+1] = indices[i*3+2];
indices_16bit[i*3+2] = indices[i*3+1];
}
shapeRenderActor = SAMPLE_NEW(RenderMeshActor)(renderer, vertexes, nbVerts, NULL, NULL, indices_16bit, NULL, nbFaces);
SAMPLE_FREE(indices_16bit);
SAMPLE_FREE(indices);
DELETEARRAY(sampleBuffer);
DELETEARRAY(vertexes);
if (!material)
material = mManagedMaterials[MATERIAL_FLAT];
}
break;
default: {}
};
if(shapeRenderActor)
{
link(shapeRenderActor, shape, actor);
mRenderActors.push_back(shapeRenderActor);
shapeRenderActor->setRenderMaterial(material);
shapeRenderActor->setEnableCameraCull(true);
}
return shapeRenderActor;
}
void PhysXSample::updateRenderObjectsFromRigidActor(PxRigidActor& actor, RenderMaterial* mat)
{
PxU32 nbShapes = actor.getNbShapes();
if(nbShapes > 0)
{
const PxU32 nbShapesOnStack = 8;
PxShape* shapesOnStack[nbShapesOnStack], **shapes = &shapesOnStack[0];
if(nbShapes > nbShapesOnStack)
shapes = new PxShape*[nbShapes];
actor.getShapes(shapes, nbShapes);
for(PxU32 i = 0; i < nbShapes; ++i)
{
RenderBaseActor* renderActor = getRenderActor(&actor, shapes[i]);
if(renderActor != NULL)
{
renderActor->mActive = true;
if (mat != NULL)
renderActor->setRenderMaterial(mat);
}
else
createRenderObjectFromShape(&actor, shapes[i], mat);
}
if(nbShapes > nbShapesOnStack)
delete[] shapes;
}
}
void PhysXSample::createRenderObjectsFromScene()
{
PxScene& scene = getActiveScene();
PxActorTypeFlags types = PxActorTypeFlag::eRIGID_STATIC | PxActorTypeFlag::eRIGID_DYNAMIC;
#if PX_USE_PARTICLE_SYSTEM_API
types |= PxActorTypeFlag::ePARTICLE_SYSTEM | PxActorTypeFlag::ePARTICLE_FLUID;
#endif
#if PX_USE_CLOTH_API
types |= PxActorTypeFlag::eCLOTH;
#endif
PxU32 nbActors = scene.getNbActors(types);
if(nbActors)
{
PxActor** actors = new PxActor* [nbActors];
scene.getActors(types, actors, nbActors);
for(PxU32 i = 0; i < nbActors; ++i)
{
switch (actors[i]->getType())
{
case PxActorType::eRIGID_STATIC:
case PxActorType::eRIGID_DYNAMIC:
updateRenderObjectsFromRigidActor(*reinterpret_cast<PxRigidActor*>(actors[i]));
break;
#if PX_USE_PARTICLE_SYSTEM_API
case PxActorType::ePARTICLE_SYSTEM:
case PxActorType::ePARTICLE_FLUID:
// updateRenderObjectsFromRigidActor(*reinterpret_cast<PxRigidActor*>(actors[i]));
// break;
#endif
#if PX_USE_CLOTH_API
case PxActorType::eCLOTH:
#endif
default:
break;
}
}
delete[] actors;
}
PxU32 nbArticulations = scene.getNbArticulations();
if(nbArticulations > 0)
{
PxArticulation** articulations = new PxArticulation* [nbArticulations];
scene.getArticulations(articulations, nbArticulations);
for(PxU32 i=0; i < nbArticulations; i++)
{
updateRenderObjectsFromArticulation(*articulations[i]);
}
delete[] articulations;
}
}
void PhysXSample::updateRenderObjectsFromArticulation(PxArticulation& articulation)
{
SampleInlineArray<PxArticulationLink*,20> links;
links.resize(articulation.getNbLinks());
articulation.getLinks(links.begin(), links.size());
for(PxU32 i=0; i < links.size(); i++)
{
updateRenderObjectsFromRigidActor(*links[i]);
}
}
void PhysXSample::createRenderObjectsFromActor(PxRigidActor* rigidActor, RenderMaterial* material)
{
PX_ASSERT(rigidActor);
PxU32 nbShapes = rigidActor->getNbShapes();
if(!nbShapes)
return;
PxShape** shapes = (PxShape**)SAMPLE_ALLOC(sizeof(PxShape*)*nbShapes);
PxU32 nb = rigidActor->getShapes(shapes, nbShapes);
PX_ASSERT(nb==nbShapes);
PX_UNUSED(nb);
for(PxU32 i=0;i<nbShapes;i++)
{
createRenderObjectFromShape(rigidActor, shapes[i], material);
}
SAMPLE_FREE(shapes);
}
void PhysXSample::updateRenderObjectsDebug(float dtime)
{
RenderPhysX3Debug* debugRenderer = getDebugRenderer();
if(debugRenderer && mScene)
{
for(PxU32 i = 0; i < mRenderActors.size(); ++i)
{
if (mRenderActors[i]->getEnableDebugRender())
mRenderActors[i]->drawDebug(debugRenderer);
}
getCamera().drawDebug(debugRenderer);
#ifdef VISUALIZE_PICKING_RAYS
if(mPicking)
{
const std::vector<Picking::Ray>& rays = mPicking->getRays();
PxU32 nbRays = rays.size();
const RendererColor color(255, 0, 0);
for(PxU32 i=0;i<nbRays;i++)
{
debugRenderer->addLine(rays[i].origin, rays[i].origin + rays[i].dir * 1000.0f, color);
}
}
#endif
#ifdef VISUALIZE_PICKING_TRIANGLES
if(mPicking && mPicking->pickedTriangleIsValid())
{
PxTriangle touchedTri = mPicking->getPickedTriangle();
RendererColor color(255, 255, 255);
debugRenderer->addLine(touchedTri.verts[0], touchedTri.verts[1], color);
debugRenderer->addLine(touchedTri.verts[1], touchedTri.verts[2], color);
debugRenderer->addLine(touchedTri.verts[2], touchedTri.verts[0], color);
for(PxU32 i=0;i<3;i++)
{
if(mPicking->pickedTriangleAdjacentIsValid(i))
{
touchedTri = mPicking->getPickedTriangleAdjacent(i);
if(i==0)
color = RendererColor(255, 0, 0);
else if(i==1)
color = RendererColor(0, 255, 0);
else if(i==2)
color = RendererColor(0, 0, 255);
debugRenderer->addLine(touchedTri.verts[0], touchedTri.verts[1], color);
debugRenderer->addLine(touchedTri.verts[1], touchedTri.verts[2], color);
debugRenderer->addLine(touchedTri.verts[2], touchedTri.verts[0], color);
}
}
}
#endif
}
}
void PhysXSample::initRenderObjects()
{
PxSceneWriteLock scopedLock(*mScene);
for (PxU32 i = 0; i < mRenderActors.size(); ++i)
{
mRenderActors[i]->update(0.0f);
}
}
void PhysXSample::updateRenderObjectsSync(float dtime)
{
PxSceneWriteLock scopedLock(*mScene);
#if PX_USE_CLOTH_API
#if defined(RENDERER_ENABLE_CUDA_INTEROP)
// map interop resources
shdfnd::Array<RendererClothShape*> shapes;
if (mCudaContextManager)
{
for(PxU32 i = 0; i < mRenderClothActors.size(); ++i)
{
if (mRenderClothActors[i]->getCloth()->getClothFlags()&PxClothFlag::eGPU &&
mRenderClothActors[i]->getRenderClothShape()->isInteropEnabled())
{
shapes.pushBack(mRenderClothActors[i]->getRenderClothShape());
}
}
if (shapes.size())
{
mCudaContextManager->acquireContext();
RendererClothShape::mapShapes(&shapes[0], shapes.size());
}
}
#endif // RENDERER_ENABLE_CUDA_INTEROP
// update shapes
for(PxU32 i = 0; i < mRenderClothActors.size(); ++i)
mRenderClothActors[i]->update(dtime);
#if defined(RENDERER_ENABLE_CUDA_INTEROP)
// unmap interop resources
if (mCudaContextManager && shapes.size())
{
RendererClothShape::unmapShapes(&shapes[0], shapes.size());
mCudaContextManager->releaseContext();
}
#endif // RENDERER_ENABLE_CUDA_INTEROP
#endif // PX_USE_CLOTH_API
#if PX_USE_PARTICLE_SYSTEM_API
for(PxU32 i = 0; i < mRenderParticleSystemActors.size(); ++i)
mRenderParticleSystemActors[i]->update(dtime);
#endif // PX_USE_PARTICLE_SYSTEM_API
}
void PhysXSample::updateRenderObjectsAsync(float dtime)
{
PX_PROFILE_ZONE("updateRenderObjectsAsync", 0);
if(mActiveTransformCount)
{
PxSceneWriteLock scopedLock(*mScene);
PxU32 nbActiveTransforms = mActiveTransformCount;
PxActiveTransform* currentTransform = mBufferedActiveTransforms;
while(nbActiveTransforms--)
{
const PxActiveTransform& activeTransform = *currentTransform++;
PxActor* actor = activeTransform.actor;
// Check that actor is not a deleted actor
bool isDeleted = false;
for (PxU32 i=0; i < mDeletedActors.size(); i++)
{
if (mDeletedActors[i] == actor)
{
isDeleted = true;
break;
}
}
if (isDeleted) continue;
const PxType actorType = actor->getConcreteType();
if(actorType==PxConcreteType::eRIGID_DYNAMIC || actorType==PxConcreteType::eRIGID_STATIC
|| actorType == PxConcreteType::eARTICULATION_LINK || actorType == PxConcreteType::eARTICULATION_JOINT)
{
PxRigidActor* rigidActor = static_cast<PxRigidActor*>(actor);
PxU32 nbShapes = rigidActor->getNbShapes();
for(PxU32 i=0;i<nbShapes;i++)
{
PxShape* shape;
PxU32 n = rigidActor->getShapes(&shape, 1, i);
PX_ASSERT(n==1);
PX_UNUSED(n);
RenderBaseActor* renderActor = getRenderActor(rigidActor, shape);
if (NULL != renderActor)
renderActor->update(dtime);
}
}
}
mActiveTransformCount = 0;
mDeletedActors.clear();
}
}
void PhysXSample::bufferActiveTransforms()
{
PxSceneReadLock scopedLock(*mScene);
// buffer active transforms to perform render object update parallel to simulation
const PxActiveTransform* activeTransforms = mScene->getActiveTransforms(mActiveTransformCount);
if(mActiveTransformCapacity < mActiveTransformCount)
{
SAMPLE_FREE(mBufferedActiveTransforms);
mActiveTransformCapacity = (PxU32)(mActiveTransformCount * 1.5f);
mBufferedActiveTransforms = (PxActiveTransform*)SAMPLE_ALLOC(sizeof(PxActiveTransform) * mActiveTransformCapacity);
}
if(mActiveTransformCount)
PxMemCopy(mBufferedActiveTransforms, activeTransforms, sizeof(PxActiveTransform) * mActiveTransformCount);
}
///////////////////////////////////////////////////////////////////////////////
#if PX_USE_CLOTH_API
PxCloth* PhysXSample::createClothFromMeshDesc(
const PxClothMeshDesc& meshDesc, const PxTransform& pose, const PxVec3& gravityDir, const PxVec2* uv, const char* textureFile, PxReal scale)
{
PxClothFabric* clothFabric = PxClothFabricCreate(getPhysics(), meshDesc, gravityDir);
PX_ASSERT(meshDesc.points.stride == sizeof(PxVec4));
// create the cloth actor
const PxClothParticle* particles = (const PxClothParticle*)meshDesc.points.data;
PxCloth* cloth = getPhysics().createCloth( pose, *clothFabric, particles, PxClothFlags());
PX_ASSERT(cloth);
cloth->setSolverFrequency(60.0f); // don't know how to get target simulation frequency, just hardcode for now
// add this cloth into the scene
getActiveScene().addActor(*cloth);
// create render material
RenderMaterial* clothMaterial = createRenderMaterialFromTextureFile(textureFile);
// create the render object in sample framework
createRenderObjectsFromCloth(*cloth, meshDesc, clothMaterial, uv, true, scale);
return cloth;
}
///////////////////////////////////////////////////////////////////////////////
// create cloth from obj file
PxCloth* PhysXSample::createClothFromObj(
const char* objFileName, const PxTransform& pose, const char* textureFile)
{
// create a mesh grid
SampleArray<PxVec4> vertices;
SampleArray<PxU32> primitives;
SampleArray<PxVec2> uvs;
PxClothMeshDesc meshDesc = Test::ClothHelpers::createMeshFromObj(objFileName, 1.0f,
PxQuat(PxIdentity), PxVec3(0.0f), vertices, primitives, uvs);
return createClothFromMeshDesc(meshDesc, pose, PxVec3(0,0,-1), uvs.begin(), textureFile);
}
///////////////////////////////////////////////////////////////////////////////
// create cloth grid in XZ plane
PxCloth* PhysXSample::createGridCloth(
PxReal sizeX, PxReal sizeZ, PxU32 numX, PxU32 numZ,
const PxTransform& pose, const char* textureFile)
{
// create a mesh grid
SampleArray<PxVec4> vertices;
SampleArray<PxU32> primitives;
SampleArray<PxVec2> uvs;
PxClothMeshDesc meshDesc = Test::ClothHelpers::createMeshGrid(PxVec3(sizeX, 0, 0),
PxVec3(0, 0, sizeZ), numX, numZ, vertices, primitives, uvs);
return createClothFromMeshDesc(meshDesc, pose, PxVec3(0,0,-1), uvs.begin(), textureFile);
}
#endif // PX_USE_CLOTH_API
///////////////////////////////////////////////////////////////////////////////
RenderMaterial* PhysXSample::createRenderMaterialFromTextureFile(const char* filename)
{
RenderMaterial* material = NULL;
if (!filename)
return NULL;
RAWTexture data;
data.mName = filename;
RenderTexture* texture = createRenderTextureFromRawTexture(data);
material = SAMPLE_NEW(RenderMaterial)(*getRenderer(), PxVec3(0.7f, 0.7f, 0.7f), 1.0f, true, MATERIAL_CLOTH, texture);
mRenderMaterials.push_back(material);
return material;
}
///////////////////////////////////////////////////////////////////////////////
#if PX_USE_CLOTH_API
void PhysXSample::createRenderObjectsFromCloth(const PxCloth& cloth, const PxClothMeshDesc& meshDesc, RenderMaterial* material, const PxVec2* uv, bool enableDebugRender, PxReal scale)
{
RenderClothActor* clothActor = SAMPLE_NEW (RenderClothActor)
(*getRenderer(), cloth, meshDesc,uv, scale );
if(!clothActor)
return;
if (material)
clothActor->setRenderMaterial(material);
mRenderClothActors.push_back(clothActor);
mRenderActors.push_back(clothActor);
}
void PhysXSample::removeRenderClothActor(RenderClothActor& renderActor)
{
std::vector<RenderBaseActor*>::iterator baseActorIter = std::find(mRenderActors.begin(), mRenderActors.end(), (RenderBaseActor*)(&renderActor));
if(baseActorIter != mRenderActors.end())
mRenderActors.erase(baseActorIter);
std::vector<RenderClothActor*>::iterator clothActorIter = std::find(mRenderClothActors.begin(), mRenderClothActors.end(), &renderActor);
if(clothActorIter != mRenderClothActors.end())
mRenderClothActors.erase(clothActorIter);
}
#endif // PX_USE_CLOTH_API
///////////////////////////////////////////////////////////////////////////////
PxRigidActor* PhysXSample::createGrid(RenderMaterial* material)
{
PxSceneWriteLock scopedLock(*mScene);
Renderer* renderer = getRenderer();
PX_ASSERT(renderer);
PxRigidStatic* plane = PxCreatePlane(*mPhysics, PxPlane(PxVec3(0,1,0), 0), *mMaterial);
if(!plane)
fatalError("create plane failed!");
mScene->addActor(*plane);
PxShape* shape;
plane->getShapes(&shape, 1);
RenderGridActor* actor = SAMPLE_NEW(RenderGridActor)(*renderer, 20, 10.0f);
link(actor, shape, plane);
actor->setTransform(PxTransform(PxIdentity));
mRenderActors.push_back(actor);
actor->setRenderMaterial(material);
return plane;
}
///////////////////////////////////////////////////////////////////////////////
void PhysXSample::spawnDebugObject()
{
PxSceneWriteLock scopedLock(*mScene);
PxU32 types = getDebugObjectTypes();
if (!types)
return;
//select legal type
while ((mDebugObjectType & types) == 0)
mDebugObjectType = (mDebugObjectType << 1) ? 0 : 1;
if ((mDebugObjectType & DEBUG_OBJECT_ALL) == 0)
return;
const PxVec3 pos = getCamera().getPos();
const PxVec3 vel = getCamera().getViewDir() * getDebugObjectsVelocity();
PxRigidDynamic* actor = NULL;
switch (mDebugObjectType)
{
case DEBUG_OBJECT_SPHERE:
actor = createSphere(pos, getDebugSphereObjectRadius(), &vel, mManagedMaterials[MATERIAL_GREEN], mDefaultDensity);
break;
case DEBUG_OBJECT_BOX:
actor = createBox(pos, getDebugBoxObjectExtents(), &vel, mManagedMaterials[MATERIAL_RED], mDefaultDensity);
break;
case DEBUG_OBJECT_CAPSULE:
actor = createCapsule(pos, getDebugCapsuleObjectRadius(), getDebugCapsuleObjectHalfHeight(), &vel, mManagedMaterials[MATERIAL_BLUE], mDefaultDensity);
break;
case DEBUG_OBJECT_CONVEX:
actor = createConvex(pos, &vel, mManagedMaterials[MATERIAL_YELLOW], mDefaultDensity);
break;
case DEBUG_OBJECT_COMPOUND:
actor = createTestCompound(pos, 320, 0.1f, 2.0f, &vel, NULL, mDefaultDensity, true);
break;
}
if (actor)
onDebugObjectCreation(actor);
//switch type
mDebugObjectType = mDebugObjectType << 1;
while ((mDebugObjectType & types) == 0)
mDebugObjectType = (mDebugObjectType << 1) ? 0 : 1;
}
///////////////////////////////////////////////////////////////////////////////
PxRigidDynamic* PhysXSample::createBox(const PxVec3& pos, const PxVec3& dims, const PxVec3* linVel, RenderMaterial* material, PxReal density)
{
PxSceneWriteLock scopedLock(*mScene);
PxRigidDynamic* box = PxCreateDynamic(*mPhysics, PxTransform(pos), PxBoxGeometry(dims), *mMaterial, density);
PX_ASSERT(box);
SetupDefaultRigidDynamic(*box);
mScene->addActor(*box);
addPhysicsActors(box);
if(linVel)
box->setLinearVelocity(*linVel);
createRenderObjectsFromActor(box, material);
return box;
}
///////////////////////////////////////////////////////////////////////////////
PxRigidDynamic* PhysXSample::createSphere(const PxVec3& pos, PxReal radius, const PxVec3* linVel, RenderMaterial* material, PxReal density)
{
PxSceneWriteLock scopedLock(*mScene);
PxRigidDynamic* sphere = PxCreateDynamic(*mPhysics, PxTransform(pos), PxSphereGeometry(radius), *mMaterial, density);
PX_ASSERT(sphere);
SetupDefaultRigidDynamic(*sphere);
mScene->addActor(*sphere);
addPhysicsActors(sphere);
if(linVel)
sphere->setLinearVelocity(*linVel);
createRenderObjectsFromActor(sphere, material);
return sphere;
}
///////////////////////////////////////////////////////////////////////////////
PxRigidDynamic* PhysXSample::createCapsule(const PxVec3& pos, PxReal radius, PxReal halfHeight, const PxVec3* linVel, RenderMaterial* material, PxReal density)
{
PxSceneWriteLock scopedLock(*mScene);
const PxQuat rot = PxQuat(PxIdentity);
PX_UNUSED(rot);
PxRigidDynamic* capsule = PxCreateDynamic(*mPhysics, PxTransform(pos), PxCapsuleGeometry(radius, halfHeight), *mMaterial, density);
PX_ASSERT(capsule);
SetupDefaultRigidDynamic(*capsule);
mScene->addActor(*capsule);
addPhysicsActors(capsule);
if(linVel)
capsule->setLinearVelocity(*linVel);
createRenderObjectsFromActor(capsule, material);
return capsule;
}
///////////////////////////////////////////////////////////////////////////////
PxRigidDynamic* PhysXSample::createConvex(const PxVec3& pos, const PxVec3* linVel, RenderMaterial* material, PxReal density)
{
PxSceneWriteLock scopedLock(*mScene);
PxConvexMesh* convexMesh = GenerateConvex(*mPhysics, *mCooking, getDebugConvexObjectScale(), false, true);
PX_ASSERT(convexMesh);
PxRigidDynamic* convex = PxCreateDynamic(*mPhysics, PxTransform(pos), PxConvexMeshGeometry(convexMesh), *mMaterial, density);
PX_ASSERT(convex);
SetupDefaultRigidDynamic(*convex);
mScene->addActor(*convex);
addPhysicsActors(convex);
if(linVel)
convex->setLinearVelocity(*linVel);
createRenderObjectsFromActor(convex, material);
return convex;
}
///////////////////////////////////////////////////////////////////////////////
PxRigidDynamic* PhysXSample::createCompound(const PxVec3& pos, const std::vector<PxTransform>& localPoses, const std::vector<const PxGeometry*>& geometries, const PxVec3* linVel, RenderMaterial* material, PxReal density)
{
PxSceneWriteLock scopedLock(*mScene);
PxRigidDynamic* compound = GenerateCompound(*mPhysics, mScene, mMaterial, pos, PxQuat(PxIdentity), localPoses, geometries, false, density);
addPhysicsActors(compound);
if(linVel)
compound->setLinearVelocity(*linVel);
createRenderObjectsFromActor(compound, material);
return compound;
}
PxRigidDynamic* PhysXSample::createTestCompound(const PxVec3& pos, PxU32 nbBoxes, float boxSize, float amplitude, const PxVec3* vel, RenderMaterial* material, PxReal density, bool makeSureVolumeEmpty)
{
PxSceneWriteLock scopedLock(*mScene);
if (makeSureVolumeEmpty)
{
// Kai: a little bigger than amplitude + boxSize * sqrt(3)
PxSphereGeometry geometry(amplitude + boxSize + boxSize);
PxTransform pose(pos);
PxOverlapBuffer buf;
getActiveScene().overlap(geometry, pose, buf,
PxQueryFilterData(PxQueryFlag::eANY_HIT|PxQueryFlag::eSTATIC|PxQueryFlag::eDYNAMIC));
if (buf.hasBlock) {
// shdfnd::printFormatted("desination volume is not empty!!!\n");
return NULL;
}
}
std::vector<PxTransform> localPoses;
std::vector<const PxGeometry*> geometries;
PxToolkit::BasicRandom rnd(42);
PxBoxGeometryAlloc* geoms = SAMPLE_NEW(PxBoxGeometryAlloc)[nbBoxes];
for(PxU32 i=0;i<nbBoxes;i++)
{
geoms[i].halfExtents = PxVec3(boxSize);
PxTransform localPose;
rnd.unitRandomPt(localPose.p);
localPose.p.normalize();
localPose.p *= amplitude;
rnd.unitRandomQuat(localPose.q);
localPoses.push_back(localPose);
geometries.push_back(&geoms[i]);
}
PxRigidDynamic* actor = createCompound(pos, localPoses, geometries, vel, material, density);
DELETEARRAY(geoms);
return actor;
}
///////////////////////////////////////////////////////////////////////////////
struct FindRenderActor
{
FindRenderActor(PxShape* pxShape): mPxShape(pxShape) {}
bool operator() (const RenderBaseActor* actor) { return actor->getPhysicsShape() == mPxShape; }
PxShape* mPxShape;
};
void PhysXSample::removeRenderObject(RenderBaseActor* renderActor)
{
std::vector<RenderBaseActor*>::iterator renderIter = std::find(mRenderActors.begin(), mRenderActors.end(), renderActor);
if(renderIter != mRenderActors.end())
{
// ######### PT: TODO: unlink call missing here!!!
mDeletedRenderActors.push_back((*renderIter));
mRenderActors.erase(renderIter);
}
}
//////////////////////////////////////////////////////////////////////////
void PhysXSample::removeRenderActorsFromPhysicsActor(const PxRigidActor* actor)
{
const PxU32 numShapes = actor->getNbShapes();
PxShape** shapes = (PxShape**)SAMPLE_ALLOC(sizeof(PxShape*)*numShapes);
actor->getShapes(shapes, numShapes);
for(PxU32 i = 0; i < numShapes; i++)
{
PxShape* shape = shapes[i];
FindRenderActor findRenderActor(shape);
std::vector<RenderBaseActor*>::iterator renderIter = std::find_if(mRenderActors.begin(), mRenderActors.end(), findRenderActor);
if(renderIter != mRenderActors.end())
{
unlink((*renderIter), shape, const_cast<PxRigidActor*>(actor));
mDeletedRenderActors.push_back((*renderIter));
mRenderActors.erase(renderIter);
}
}
// check if the actor is in the active transform list and remove
if(actor->getType() == PxActorType::eRIGID_DYNAMIC)
{
for(PxU32 i=0; i < mActiveTransformCount; i++)
{
if(mBufferedActiveTransforms[i].actor == actor)
{
mBufferedActiveTransforms[i] = mBufferedActiveTransforms[mActiveTransformCount-1];
mActiveTransformCount--;
break;
}
}
mDeletedActors.push_back(const_cast<PxRigidActor*>(actor));
}
SAMPLE_FREE(shapes);
}
//////////////////////////////////////////////////////////////////////////
void PhysXSample::removeActor(PxRigidActor* actor)
{
removeRenderActorsFromPhysicsActor(actor);
std::vector<PxRigidActor*>::iterator actorIter = std::find(mPhysicsActors.begin(), mPhysicsActors.end(), actor);
if(actorIter != mPhysicsActors.end())
{
mPhysicsActors.erase(actorIter);
actor->release();
}
}
///////////////////////////////////////////////////////////////////////////////
SampleFramework::SampleAsset* PhysXSample::getAsset(const char* relativePath, SampleFramework::SampleAsset::Type type, bool abortOnError)
{
SampleFramework::SampleAsset* asset = mApplication.getAssetManager()->getAsset(relativePath, type);
if (NULL == asset && abortOnError)
{
std::string msg = "Error while getting material asset ";
msg += relativePath;
msg += "\n";
fatalError(msg.c_str());
}
return asset;
}
void PhysXSample::importRAWFile(const char* relativePath, PxReal scale, bool recook)
{
mMeshTag = 0;
mFilename = relativePath;
mScale = scale;
const bool saved = gRecook;
if(!gRecook && recook)
gRecook = true;
bool status = loadRAWfile(getSampleMediaFilename(mFilename), *this, scale);
if (!status)
{
std::string msg = "Sample can not load file ";
msg += getSampleMediaFilename(mFilename);
msg += "\n";
fatalError(msg.c_str());
}
gRecook = saved;
}
#if PX_USE_PARTICLE_SYSTEM_API
RenderParticleSystemActor* PhysXSample::createRenderObjectFromParticleSystem(ParticleSystem& ps, RenderMaterial* material, bool useMeshInstancing, bool useFading, PxReal fadingPeriod, PxReal instancingScale)
{
RenderParticleSystemActor* renderActor = SAMPLE_NEW(RenderParticleSystemActor)(*getRenderer(), &ps, useMeshInstancing, useFading, fadingPeriod, instancingScale);
if(material) renderActor->setRenderMaterial(material);
mRenderActors.push_back(renderActor);
mRenderParticleSystemActors.push_back(renderActor);
return renderActor;
}
void PhysXSample::addRenderParticleSystemActor(RenderParticleSystemActor& renderActor)
{
mRenderActors.push_back(&renderActor);
mRenderParticleSystemActors.push_back(&renderActor);
}
void PhysXSample::removeRenderParticleSystemActor(RenderParticleSystemActor& renderActor)
{
std::vector<RenderBaseActor*>::iterator baseActorIter = std::find(mRenderActors.begin(), mRenderActors.end(), (RenderBaseActor*)(&renderActor));
if(baseActorIter != mRenderActors.end())
mRenderActors.erase(baseActorIter);
std::vector<RenderParticleSystemActor*>::iterator psActorIter = std::find(mRenderParticleSystemActors.begin(), mRenderParticleSystemActors.end(), &renderActor);
if(psActorIter != mRenderParticleSystemActors.end())
mRenderParticleSystemActors.erase(psActorIter);
}
void PhysXSample::project(const PxVec3& v, int& x, int& y, float& depth)
{
mPicking->project(v, x, y, depth);
}
///////////////////////////////////////////////////////////////////////////////
#if PX_USE_CLOTH_API
#endif // PX_USE_CLOTH_API
#endif // PX_USE_PARTICLE_SYSTEM_API
|