1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
|
// This code contains NVIDIA Confidential Information and is disclosed
// under the Mutual Non-Disclosure Agreement.
//
// Notice
// ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES
// NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
//
// NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. No third party distribution is allowed unless
// expressly authorized by NVIDIA. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright � 2008- 2013 NVIDIA Corporation. All rights reserved.
//
// NVIDIA Corporation and its licensors retain all intellectual property and proprietary
// rights in and to this software and related documentation and any modifications thereto.
// Any use, reproduction, disclosure or distribution of this software and related
// documentation without an express license agreement from NVIDIA Corporation is
// strictly prohibited.
//
//--------------------------------------------------------------------------------------
// File: DXUTMisc.cpp
//
// Shortcut macros and functions for using DX objects
//
// Copyright (c) Microsoft Corporation. All rights reserved
//--------------------------------------------------------------------------------------
#include "dxstdafx.h"
#define DXUT_GAMEPAD_TRIGGER_THRESHOLD 30
#undef min // use __min instead
#undef max // use __max instead
//--------------------------------------------------------------------------------------
// Global/Static Members
//--------------------------------------------------------------------------------------
CDXUTResourceCache& DXUTGetGlobalResourceCache()
{
// Using an accessor function gives control of the construction order
static CDXUTResourceCache cache;
return cache;
}
CDXUTTimer* DXUTGetGlobalTimer()
{
// Using an accessor function gives control of the construction order
static CDXUTTimer timer;
return &timer;
}
//--------------------------------------------------------------------------------------
// Internal functions forward declarations
//--------------------------------------------------------------------------------------
bool DXUTFindMediaSearchTypicalDirs( WCHAR* strSearchPath, int cchSearch, LPCWSTR strLeaf, WCHAR* strExePath, WCHAR* strExeName );
bool DXUTFindMediaSearchParentDirs( WCHAR* strSearchPath, int cchSearch, WCHAR* strStartAt, WCHAR* strLeafName );
INT_PTR CALLBACK DisplaySwitchToREFWarningProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
//--------------------------------------------------------------------------------------
// Shared code for samples to ask user if they want to use a REF device or quit
//--------------------------------------------------------------------------------------
void DXUTDisplaySwitchingToREFWarning()
{
if( DXUTGetShowMsgBoxOnError() )
{
// Open the appropriate registry key
DWORD dwSkipWarning = 0;
HKEY hKey;
LONG lResult = RegOpenKeyEx( HKEY_CURRENT_USER, L"Software\\Microsoft\\DirectX 9.0 SDK", 0, KEY_READ, &hKey );
if( ERROR_SUCCESS == lResult )
{
DWORD dwType;
DWORD dwSize = sizeof(DWORD);
lResult = RegQueryValueEx( hKey, L"Skip Warning On REF", NULL, &dwType, (BYTE*)&dwSkipWarning, &dwSize );
RegCloseKey( hKey );
}
if( dwSkipWarning == 0 )
{
// Compact code to create a custom dialog box without using a template in a resource file.
// If this dialog were in a .rc file, this would be a lot simpler but every sample calling this function would
// need a copy of the dialog in its own .rc file. Also MessageBox API could be used here instead, but
// the MessageBox API is simpler to call but it can't provide a "Don't show again" checkbox
typedef struct { DLGITEMTEMPLATE a; WORD b; WORD c; WORD d; WORD e; WORD f; } DXUT_DLG_ITEM;
typedef struct { DLGTEMPLATE a; WORD b; WORD c; WCHAR d[2]; WORD e; WCHAR f[14]; DXUT_DLG_ITEM i1; DXUT_DLG_ITEM i2; DXUT_DLG_ITEM i3; DXUT_DLG_ITEM i4; DXUT_DLG_ITEM i5; } DXUT_DLG_DATA;
DXUT_DLG_DATA dtp =
{
{WS_CAPTION|WS_POPUP|WS_VISIBLE|WS_SYSMENU|DS_ABSALIGN|DS_3DLOOK|DS_SETFONT|DS_MODALFRAME|DS_CENTER,0,5,0,0,269,82},0,0,L" ",8,L"MS Sans Serif",
{{WS_CHILD|WS_VISIBLE|SS_ICON|SS_CENTERIMAGE,0,7,7,24,24,0x100},0xFFFF,0x0082,0,0,0}, // icon
{{WS_CHILD|WS_VISIBLE,0,40,7,230,25,0x101},0xFFFF,0x0082,0,0,0}, // static text
{{WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,0,80,39,50,14,IDYES},0xFFFF,0x0080,0,0,0}, // Yes button
{{WS_CHILD|WS_VISIBLE,0,133,39,50,14,IDNO},0xFFFF,0x0080,0,0,0}, // No button
{{WS_CHILD|WS_VISIBLE|BS_CHECKBOX,0,7,59,70,16,IDIGNORE},0xFFFF,0x0080,0,0,0}, // checkbox
};
int nResult = (int) DialogBoxIndirect( DXUTGetHINSTANCE(), (DLGTEMPLATE*)&dtp, DXUTGetHWND(), DisplaySwitchToREFWarningProc );
if( (nResult & 0x80) == 0x80 ) // "Don't show again" checkbox was checked
{
lResult = RegOpenKeyEx( HKEY_CURRENT_USER, L"Software\\Microsoft\\DirectX 9.0 SDK", 0, KEY_WRITE, &hKey );
if( ERROR_SUCCESS == lResult )
{
dwSkipWarning = 1;
RegSetValueEx( hKey, L"Skip Warning On REF", 0, REG_DWORD, (BYTE*)&dwSkipWarning, sizeof(DWORD) );
RegCloseKey( hKey );
}
}
// User choose not to continue
if( (nResult & 0x0F) == IDNO )
DXUTShutdown(1);
}
}
}
//--------------------------------------------------------------------------------------
// MsgProc for DXUTDisplaySwitchingToREFWarning() dialog box
//--------------------------------------------------------------------------------------
INT_PTR CALLBACK DisplaySwitchToREFWarningProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
// Easier to set text here than in the DLGITEMTEMPLATE
SetWindowText( hDlg, DXUTGetWindowTitle() );
SendMessage( GetDlgItem(hDlg, 0x100), STM_SETIMAGE, IMAGE_ICON, (LPARAM)LoadIcon(0, IDI_QUESTION));
SetDlgItemText( hDlg, 0x101, L"Switching to the Direct3D reference rasterizer, a software device\nthat implements the entire Direct3D feature set, but runs very slowly.\nDo you wish to continue?" );
SetDlgItemText( hDlg, IDYES, L"&Yes" );
SetDlgItemText( hDlg, IDNO, L"&No" );
SetDlgItemText( hDlg, IDIGNORE, L"&Don't show again" );
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDIGNORE: CheckDlgButton( hDlg, IDIGNORE, (IsDlgButtonChecked( hDlg, IDIGNORE ) == BST_CHECKED) ? BST_UNCHECKED : BST_CHECKED ); EnableWindow( GetDlgItem( hDlg, IDNO ), (IsDlgButtonChecked( hDlg, IDIGNORE ) != BST_CHECKED) ); break;
case IDNO: EndDialog(hDlg, (IsDlgButtonChecked( hDlg, IDIGNORE ) == BST_CHECKED) ? IDNO|0x80 : IDNO|0x00 ); return TRUE;
case IDCANCEL:
case IDYES: EndDialog(hDlg, (IsDlgButtonChecked( hDlg, IDIGNORE ) == BST_CHECKED) ? IDYES|0x80 : IDYES|0x00 ); return TRUE;
}
break;
}
return FALSE;
}
//--------------------------------------------------------------------------------------
CDXUTTimer::CDXUTTimer()
{
m_bTimerStopped = true;
m_llQPFTicksPerSec = 0;
m_llStopTime = 0;
m_llLastElapsedTime = 0;
m_llBaseTime = 0;
// Use QueryPerformanceFrequency to get the frequency of the counter
LARGE_INTEGER qwTicksPerSec;
QueryPerformanceFrequency( &qwTicksPerSec );
m_llQPFTicksPerSec = qwTicksPerSec.QuadPart;
}
//--------------------------------------------------------------------------------------
void CDXUTTimer::Reset()
{
LARGE_INTEGER qwTime = GetAdjustedCurrentTime();
m_llBaseTime = qwTime.QuadPart;
m_llLastElapsedTime = qwTime.QuadPart;
m_llStopTime = 0;
m_bTimerStopped = FALSE;
}
//--------------------------------------------------------------------------------------
void CDXUTTimer::Start()
{
// Get the current time
LARGE_INTEGER qwTime;
QueryPerformanceCounter( &qwTime );
if( m_bTimerStopped )
m_llBaseTime += qwTime.QuadPart - m_llStopTime;
m_llStopTime = 0;
m_llLastElapsedTime = qwTime.QuadPart;
m_bTimerStopped = FALSE;
}
//--------------------------------------------------------------------------------------
void CDXUTTimer::Stop()
{
if( !m_bTimerStopped )
{
LARGE_INTEGER qwTime;
QueryPerformanceCounter( &qwTime );
m_llStopTime = qwTime.QuadPart;
m_llLastElapsedTime = qwTime.QuadPart;
m_bTimerStopped = TRUE;
}
}
//--------------------------------------------------------------------------------------
void CDXUTTimer::Advance()
{
m_llStopTime += m_llQPFTicksPerSec/10;
}
//--------------------------------------------------------------------------------------
double CDXUTTimer::GetAbsoluteTime()
{
LARGE_INTEGER qwTime;
QueryPerformanceCounter( &qwTime );
double fTime = qwTime.QuadPart / (double) m_llQPFTicksPerSec;
return fTime;
}
//--------------------------------------------------------------------------------------
double CDXUTTimer::GetTime()
{
LARGE_INTEGER qwTime = GetAdjustedCurrentTime();
double fAppTime = (double) ( qwTime.QuadPart - m_llBaseTime ) / (double) m_llQPFTicksPerSec;
return fAppTime;
}
//--------------------------------------------------------------------------------------
void CDXUTTimer::GetTimeValues( double* pfTime, double* pfAbsoluteTime, float* pfElapsedTime )
{
assert( pfTime && pfAbsoluteTime && pfElapsedTime );
LARGE_INTEGER qwTime = GetAdjustedCurrentTime();
float fElapsedTime = (float) ((double) ( qwTime.QuadPart - m_llLastElapsedTime ) / (double) m_llQPFTicksPerSec);
m_llLastElapsedTime = qwTime.QuadPart;
// Clamp the timer to non-negative values to ensure the timer is accurate.
// fElapsedTime can be outside this range if processor goes into a
// power save mode or we somehow get shuffled to another processor.
// However, the main thread should call SetThreadAffinityMask to ensure that
// we don't get shuffled to another processor. Other worker threads should NOT call
// SetThreadAffinityMask, but use a shared copy of the timer data gathered from
// the main thread.
if( fElapsedTime < 0.0f )
fElapsedTime = 0.0f;
*pfAbsoluteTime = qwTime.QuadPart / (double) m_llQPFTicksPerSec;
*pfTime = ( qwTime.QuadPart - m_llBaseTime ) / (double) m_llQPFTicksPerSec;
*pfElapsedTime = fElapsedTime;
}
//--------------------------------------------------------------------------------------
double CDXUTTimer::GetElapsedTime()
{
LARGE_INTEGER qwTime = GetAdjustedCurrentTime();
double fElapsedTime = (double) ( qwTime.QuadPart - m_llLastElapsedTime ) / (double) m_llQPFTicksPerSec;
m_llLastElapsedTime = qwTime.QuadPart;
// See the explanation about clamping in CDXUTTimer::GetTimeValues()
if( fElapsedTime < 0.0f )
fElapsedTime = 0.0f;
return fElapsedTime;
}
//--------------------------------------------------------------------------------------
// If stopped, returns time when stopped otherwise returns current time
//--------------------------------------------------------------------------------------
LARGE_INTEGER CDXUTTimer::GetAdjustedCurrentTime()
{
LARGE_INTEGER qwTime;
if( m_llStopTime != 0 )
qwTime.QuadPart = m_llStopTime;
else
QueryPerformanceCounter( &qwTime );
return qwTime;
}
//--------------------------------------------------------------------------------------
bool CDXUTTimer::IsStopped()
{
return m_bTimerStopped;
}
//--------------------------------------------------------------------------------------
// Limit the current thread to one processor (the current one). This ensures that timing code
// runs on only one processor, and will not suffer any ill effects from power management.
// See "Game Timing and Multicore Processors" for more details
//--------------------------------------------------------------------------------------
void CDXUTTimer::LimitThreadAffinityToCurrentProc()
{
HANDLE hCurrentProcess = GetCurrentProcess();
// Get the processor affinity mask for this process
DWORD_PTR dwProcessAffinityMask = 0;
DWORD_PTR dwSystemAffinityMask = 0;
if( GetProcessAffinityMask( hCurrentProcess, &dwProcessAffinityMask, &dwSystemAffinityMask ) != 0 && dwProcessAffinityMask )
{
// Find the lowest processor that our process is allows to run against
DWORD_PTR dwAffinityMask = ( dwProcessAffinityMask & ((~dwProcessAffinityMask) + 1 ) );
// Set this as the processor that our thread must always run against
// This must be a subset of the process affinity mask
HANDLE hCurrentThread = GetCurrentThread();
if( INVALID_HANDLE_VALUE != hCurrentThread )
{
SetThreadAffinityMask( hCurrentThread, dwAffinityMask );
CloseHandle( hCurrentThread );
}
}
CloseHandle( hCurrentProcess );
}
//--------------------------------------------------------------------------------------
// Returns pointer to static media search buffer
//--------------------------------------------------------------------------------------
WCHAR* DXUTMediaSearchPath()
{
static WCHAR s_strMediaSearchPath[MAX_PATH] = {0};
return s_strMediaSearchPath;
}
//--------------------------------------------------------------------------------------
LPCWSTR DXUTGetMediaSearchPath()
{
return DXUTMediaSearchPath();
}
//--------------------------------------------------------------------------------------
HRESULT DXUTSetMediaSearchPath( LPCWSTR strPath )
{
HRESULT hr;
WCHAR* s_strSearchPath = DXUTMediaSearchPath();
hr = StringCchCopy( s_strSearchPath, MAX_PATH, strPath );
if( SUCCEEDED(hr) )
{
// append slash if needed
size_t ch;
hr = StringCchLength( s_strSearchPath, MAX_PATH, &ch );
if( SUCCEEDED(hr) && s_strSearchPath[ch-1] != L'\\')
{
hr = StringCchCat( s_strSearchPath, MAX_PATH, L"\\" );
}
}
return hr;
}
//--------------------------------------------------------------------------------------
// Tries to find the location of a SDK media file
// cchDest is the size in WCHARs of strDestPath. Be careful not to
// pass in sizeof(strDest) on UNICODE builds.
//--------------------------------------------------------------------------------------
HRESULT DXUTFindDXSDKMediaFileCch( WCHAR* strDestPath, int cchDest, LPCWSTR strFilename )
{
bool bFound;
WCHAR strSearchFor[MAX_PATH];
if( NULL==strFilename || strFilename[0] == 0 || NULL==strDestPath || cchDest < 10 )
return E_INVALIDARG;
// Get the exe name, and exe path
WCHAR strExePath[MAX_PATH] = {0};
WCHAR strExeName[MAX_PATH] = {0};
WCHAR* strLastSlash = NULL;
GetModuleFileName( NULL, strExePath, MAX_PATH );
strExePath[MAX_PATH-1]=0;
strLastSlash = wcsrchr( strExePath, TEXT('\\') );
if( strLastSlash )
{
StringCchCopy( strExeName, MAX_PATH, &strLastSlash[1] );
// Chop the exe name from the exe path
*strLastSlash = 0;
// Chop the .exe from the exe name
strLastSlash = wcsrchr( strExeName, TEXT('.') );
if( strLastSlash )
*strLastSlash = 0;
}
// Typical directories:
// .\
// ..\
// ..\..\
// %EXE_DIR%\
// %EXE_DIR%\..\
// %EXE_DIR%\..\..\
// %EXE_DIR%\..\%EXE_NAME%
// %EXE_DIR%\..\..\%EXE_NAME%
// Typical directory search
bFound = DXUTFindMediaSearchTypicalDirs( strDestPath, cchDest, strFilename, strExePath, strExeName );
if( bFound )
return S_OK;
// Typical directory search again, but also look in a subdir called "\media\"
StringCchPrintf( strSearchFor, MAX_PATH, L"media\\%s", strFilename );
bFound = DXUTFindMediaSearchTypicalDirs( strDestPath, cchDest, strSearchFor, strExePath, strExeName );
if( bFound )
return S_OK;
WCHAR strLeafName[MAX_PATH] = {0};
// Search all parent directories starting at .\ and using strFilename as the leaf name
StringCchCopy( strLeafName, MAX_PATH, strFilename );
bFound = DXUTFindMediaSearchParentDirs( strDestPath, cchDest, L".", strLeafName );
if( bFound )
return S_OK;
// Search all parent directories starting at the exe's dir and using strFilename as the leaf name
bFound = DXUTFindMediaSearchParentDirs( strDestPath, cchDest, strExePath, strLeafName );
if( bFound )
return S_OK;
// Search all parent directories starting at .\ and using "media\strFilename" as the leaf name
StringCchPrintf( strLeafName, MAX_PATH, L"media\\%s", strFilename );
bFound = DXUTFindMediaSearchParentDirs( strDestPath, cchDest, L".", strLeafName );
if( bFound )
return S_OK;
// Search all parent directories starting at the exe's dir and using "media\strFilename" as the leaf name
bFound = DXUTFindMediaSearchParentDirs( strDestPath, cchDest, strExePath, strLeafName );
if( bFound )
return S_OK;
// On failure, return the file as the path but also return an error code
StringCchCopy( strDestPath, cchDest, strFilename );
return DXUTERR_MEDIANOTFOUND;
}
//--------------------------------------------------------------------------------------
// Search a set of typical directories
//--------------------------------------------------------------------------------------
bool DXUTFindMediaSearchTypicalDirs( WCHAR* strSearchPath, int cchSearch, LPCWSTR strLeaf,
WCHAR* strExePath, WCHAR* strExeName )
{
// Typical directories:
// .\
// ..\
// ..\..\
// %EXE_DIR%\
// %EXE_DIR%\..\
// %EXE_DIR%\..\..\
// %EXE_DIR%\..\%EXE_NAME%
// %EXE_DIR%\..\..\%EXE_NAME%
// DXSDK media path
// Search in .\
StringCchCopy( strSearchPath, cchSearch, strLeaf );
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
return true;
// Search in ..\
StringCchPrintf( strSearchPath, cchSearch, L"..\\%s", strLeaf );
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
return true;
// Search in ..\..\
StringCchPrintf( strSearchPath, cchSearch, L"..\\..\\%s", strLeaf );
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
return true;
// Search in ..\..\
StringCchPrintf( strSearchPath, cchSearch, L"..\\..\\%s", strLeaf );
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
return true;
// Search in the %EXE_DIR%\
StringCchPrintf( strSearchPath, cchSearch, L"%s\\%s", strExePath, strLeaf );
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
return true;
// Search in the %EXE_DIR%\..\
StringCchPrintf( strSearchPath, cchSearch, L"%s\\..\\%s", strExePath, strLeaf );
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
return true;
// Search in the %EXE_DIR%\..\..\
StringCchPrintf( strSearchPath, cchSearch, L"%s\\..\\..\\%s", strExePath, strLeaf );
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
return true;
// Search in "%EXE_DIR%\..\%EXE_NAME%\". This matches the DirectX SDK layout
StringCchPrintf( strSearchPath, cchSearch, L"%s\\..\\%s\\%s", strExePath, strExeName, strLeaf );
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
return true;
// Search in "%EXE_DIR%\..\..\%EXE_NAME%\". This matches the DirectX SDK layout
StringCchPrintf( strSearchPath, cchSearch, L"%s\\..\\..\\%s\\%s", strExePath, strExeName, strLeaf );
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
return true;
// Search in media search dir
WCHAR* s_strSearchPath = DXUTMediaSearchPath();
if( s_strSearchPath[0] != 0 )
{
StringCchPrintf( strSearchPath, cchSearch, L"%s%s", s_strSearchPath, strLeaf );
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
return true;
}
return false;
}
//--------------------------------------------------------------------------------------
// Search parent directories starting at strStartAt, and appending strLeafName
// at each parent directory. It stops at the root directory.
//--------------------------------------------------------------------------------------
bool DXUTFindMediaSearchParentDirs( WCHAR* strSearchPath, int cchSearch, WCHAR* strStartAt, WCHAR* strLeafName )
{
WCHAR strFullPath[MAX_PATH] = {0};
WCHAR strFullFileName[MAX_PATH] = {0};
WCHAR strSearch[MAX_PATH] = {0};
WCHAR* strFilePart = NULL;
GetFullPathName( strStartAt, MAX_PATH, strFullPath, &strFilePart );
if( strFilePart == NULL )
return false;
while( strFilePart != NULL && *strFilePart != '\0' )
{
StringCchPrintf( strFullFileName, MAX_PATH, L"%s\\%s", strFullPath, strLeafName );
if( GetFileAttributes( strFullFileName ) != 0xFFFFFFFF )
{
StringCchCopy( strSearchPath, cchSearch, strFullFileName );
return true;
}
StringCchPrintf( strSearch, MAX_PATH, L"%s\\..", strFullPath );
GetFullPathName( strSearch, MAX_PATH, strFullPath, &strFilePart );
}
return false;
}
//--------------------------------------------------------------------------------------
// CDXUTResourceCache
//--------------------------------------------------------------------------------------
CDXUTResourceCache::~CDXUTResourceCache()
{
OnDestroyDevice();
m_TextureCache.RemoveAll();
m_EffectCache.RemoveAll();
m_FontCache.RemoveAll();
}
HRESULT CDXUTResourceCache::CreateTextureFromFile( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, LPDIRECT3DTEXTURE9 *ppTexture )
{
return CreateTextureFromFileEx( pDevice, pSrcFile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, // JJ - added D3D9Ex
0, NULL, NULL, ppTexture );
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateTextureFromFileEx( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, UINT Width, UINT Height, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DTEXTURE9 *ppTexture )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_TextureCache.GetSize(); ++i )
{
DXUTCache_Texture &Entry = m_TextureCache[i];
if( Entry.Location == DXUTCACHE_LOCATION_FILE &&
!lstrcmpW( Entry.wszSource, pSrcFile ) &&
Entry.Width == Width &&
Entry.Height == Height &&
Entry.MipLevels == MipLevels &&
Entry.Usage == Usage &&
Entry.Format == Format &&
Entry.Pool == Pool &&
Entry.Type == D3DRTYPE_TEXTURE )
{
// A match is found. Obtain the IDirect3DTexture9 interface and return that.
return Entry.pTexture->QueryInterface( IID_IDirect3DTexture9, (LPVOID*)ppTexture );
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateTextureFromFileEx( pDevice, pSrcFile, Width, Height, MipLevels, Usage, Format,
Pool, Filter, MipFilter, ColorKey, pSrcInfo, pPalette, ppTexture );
if( FAILED( hr ) )
return hr;
DXUTCache_Texture NewEntry;
NewEntry.Location = DXUTCACHE_LOCATION_FILE;
StringCchCopy( NewEntry.wszSource, MAX_PATH, pSrcFile );
NewEntry.Width = Width;
NewEntry.Height = Height;
NewEntry.MipLevels = MipLevels;
NewEntry.Usage = Usage;
NewEntry.Format = Format;
NewEntry.Pool = Pool;
NewEntry.Type = D3DRTYPE_TEXTURE;
(*ppTexture)->QueryInterface( IID_IDirect3DBaseTexture9, (LPVOID*)&NewEntry.pTexture );
m_TextureCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateTextureFromResource( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, LPDIRECT3DTEXTURE9 *ppTexture )
{
return CreateTextureFromResourceEx( pDevice, hSrcModule, pSrcResource, D3DX_DEFAULT, D3DX_DEFAULT,
D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT,
D3DX_DEFAULT, 0, NULL, NULL, ppTexture ); // JJ - added D3D9Ex
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateTextureFromResourceEx( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, UINT Width, UINT Height, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DTEXTURE9 *ppTexture )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_TextureCache.GetSize(); ++i )
{
DXUTCache_Texture &Entry = m_TextureCache[i];
if( Entry.Location == DXUTCACHE_LOCATION_RESOURCE &&
Entry.hSrcModule == hSrcModule &&
!lstrcmpW( Entry.wszSource, pSrcResource ) &&
Entry.Width == Width &&
Entry.Height == Height &&
Entry.MipLevels == MipLevels &&
Entry.Usage == Usage &&
Entry.Format == Format &&
Entry.Pool == Pool &&
Entry.Type == D3DRTYPE_TEXTURE )
{
// A match is found. Obtain the IDirect3DTexture9 interface and return that.
return Entry.pTexture->QueryInterface( IID_IDirect3DTexture9, (LPVOID*)ppTexture );
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateTextureFromResourceEx( pDevice, hSrcModule, pSrcResource, Width, Height, MipLevels, Usage,
Format, Pool, Filter, MipFilter, ColorKey, pSrcInfo, pPalette, ppTexture );
if( FAILED( hr ) )
return hr;
DXUTCache_Texture NewEntry;
NewEntry.Location = DXUTCACHE_LOCATION_RESOURCE;
NewEntry.hSrcModule = hSrcModule;
StringCchCopy( NewEntry.wszSource, MAX_PATH, pSrcResource );
NewEntry.Width = Width;
NewEntry.Height = Height;
NewEntry.MipLevels = MipLevels;
NewEntry.Usage = Usage;
NewEntry.Format = Format;
NewEntry.Pool = Pool;
NewEntry.Type = D3DRTYPE_TEXTURE;
(*ppTexture)->QueryInterface( IID_IDirect3DBaseTexture9, (LPVOID*)&NewEntry.pTexture );
m_TextureCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateCubeTextureFromFile( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, LPDIRECT3DCUBETEXTURE9 *ppCubeTexture )
{
return CreateCubeTextureFromFileEx( pDevice, pSrcFile, D3DX_DEFAULT, D3DX_DEFAULT, 0,
D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, // JJ - added D3D9Ex
0, NULL, NULL, ppCubeTexture );
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateCubeTextureFromFileEx( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, UINT Size, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DCUBETEXTURE9 *ppCubeTexture )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_TextureCache.GetSize(); ++i )
{
DXUTCache_Texture &Entry = m_TextureCache[i];
if( Entry.Location == DXUTCACHE_LOCATION_FILE &&
!lstrcmpW( Entry.wszSource, pSrcFile ) &&
Entry.Width == Size &&
Entry.MipLevels == MipLevels &&
Entry.Usage == Usage &&
Entry.Format == Format &&
Entry.Pool == Pool &&
Entry.Type == D3DRTYPE_CUBETEXTURE )
{
// A match is found. Obtain the IDirect3DCubeTexture9 interface and return that.
return Entry.pTexture->QueryInterface( IID_IDirect3DCubeTexture9, (LPVOID*)ppCubeTexture );
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateCubeTextureFromFileEx( pDevice, pSrcFile, Size, MipLevels, Usage, Format, Pool, Filter,
MipFilter, ColorKey, pSrcInfo, pPalette, ppCubeTexture );
if( FAILED( hr ) )
return hr;
DXUTCache_Texture NewEntry;
NewEntry.Location = DXUTCACHE_LOCATION_FILE;
StringCchCopy( NewEntry.wszSource, MAX_PATH, pSrcFile );
NewEntry.Width = Size;
NewEntry.MipLevels = MipLevels;
NewEntry.Usage = Usage;
NewEntry.Format = Format;
NewEntry.Pool = Pool;
NewEntry.Type = D3DRTYPE_CUBETEXTURE;
(*ppCubeTexture)->QueryInterface( IID_IDirect3DBaseTexture9, (LPVOID*)&NewEntry.pTexture );
m_TextureCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateCubeTextureFromResource( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, LPDIRECT3DCUBETEXTURE9 *ppCubeTexture )
{
return CreateCubeTextureFromResourceEx( pDevice, hSrcModule, pSrcResource, D3DX_DEFAULT, D3DX_DEFAULT,
0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, // JJ - added D3D9Ex
0, NULL, NULL, ppCubeTexture );
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateCubeTextureFromResourceEx( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, UINT Size, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DCUBETEXTURE9 *ppCubeTexture )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_TextureCache.GetSize(); ++i )
{
DXUTCache_Texture &Entry = m_TextureCache[i];
if( Entry.Location == DXUTCACHE_LOCATION_RESOURCE &&
Entry.hSrcModule == hSrcModule &&
!lstrcmpW( Entry.wszSource, pSrcResource ) &&
Entry.Width == Size &&
Entry.MipLevels == MipLevels &&
Entry.Usage == Usage &&
Entry.Format == Format &&
Entry.Pool == Pool &&
Entry.Type == D3DRTYPE_CUBETEXTURE )
{
// A match is found. Obtain the IDirect3DCubeTexture9 interface and return that.
return Entry.pTexture->QueryInterface( IID_IDirect3DCubeTexture9, (LPVOID*)ppCubeTexture );
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateCubeTextureFromResourceEx( pDevice, hSrcModule, pSrcResource, Size, MipLevels, Usage, Format,
Pool, Filter, MipFilter, ColorKey, pSrcInfo, pPalette, ppCubeTexture );
if( FAILED( hr ) )
return hr;
DXUTCache_Texture NewEntry;
NewEntry.Location = DXUTCACHE_LOCATION_RESOURCE;
NewEntry.hSrcModule = hSrcModule;
StringCchCopy( NewEntry.wszSource, MAX_PATH, pSrcResource );
NewEntry.Width = Size;
NewEntry.MipLevels = MipLevels;
NewEntry.Usage = Usage;
NewEntry.Format = Format;
NewEntry.Pool = Pool;
NewEntry.Type = D3DRTYPE_CUBETEXTURE;
(*ppCubeTexture)->QueryInterface( IID_IDirect3DBaseTexture9, (LPVOID*)&NewEntry.pTexture );
m_TextureCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateVolumeTextureFromFile( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, LPDIRECT3DVOLUMETEXTURE9 *ppVolumeTexture )
{
return CreateVolumeTextureFromFileEx( pDevice, pSrcFile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, // JJ - added D3D9Ex
0, NULL, NULL, ppVolumeTexture );
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateVolumeTextureFromFileEx( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, UINT Width, UINT Height, UINT Depth, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DVOLUMETEXTURE9 *ppTexture )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_TextureCache.GetSize(); ++i )
{
DXUTCache_Texture &Entry = m_TextureCache[i];
if( Entry.Location == DXUTCACHE_LOCATION_FILE &&
!lstrcmpW( Entry.wszSource, pSrcFile ) &&
Entry.Width == Width &&
Entry.Height == Height &&
Entry.Depth == Depth &&
Entry.MipLevels == MipLevels &&
Entry.Usage == Usage &&
Entry.Format == Format &&
Entry.Pool == Pool &&
Entry.Type == D3DRTYPE_VOLUMETEXTURE )
{
// A match is found. Obtain the IDirect3DVolumeTexture9 interface and return that.
return Entry.pTexture->QueryInterface( IID_IDirect3DVolumeTexture9, (LPVOID*)ppTexture );
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateVolumeTextureFromFileEx( pDevice, pSrcFile, Width, Height, Depth, MipLevels, Usage, Format,
Pool, Filter, MipFilter, ColorKey, pSrcInfo, pPalette, ppTexture );
if( FAILED( hr ) )
return hr;
DXUTCache_Texture NewEntry;
NewEntry.Location = DXUTCACHE_LOCATION_FILE;
StringCchCopy( NewEntry.wszSource, MAX_PATH, pSrcFile );
NewEntry.Width = Width;
NewEntry.Height = Height;
NewEntry.Depth = Depth;
NewEntry.MipLevels = MipLevels;
NewEntry.Usage = Usage;
NewEntry.Format = Format;
NewEntry.Pool = Pool;
NewEntry.Type = D3DRTYPE_VOLUMETEXTURE;
(*ppTexture)->QueryInterface( IID_IDirect3DBaseTexture9, (LPVOID*)&NewEntry.pTexture );
m_TextureCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateVolumeTextureFromResource( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, LPDIRECT3DVOLUMETEXTURE9 *ppVolumeTexture )
{
return CreateVolumeTextureFromResourceEx( pDevice, hSrcModule, pSrcResource, D3DX_DEFAULT, D3DX_DEFAULT,
D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, // JJ - added D3D9Ex
D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, ppVolumeTexture );
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateVolumeTextureFromResourceEx( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, UINT Width, UINT Height, UINT Depth, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DVOLUMETEXTURE9 *ppVolumeTexture )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_TextureCache.GetSize(); ++i )
{
DXUTCache_Texture &Entry = m_TextureCache[i];
if( Entry.Location == DXUTCACHE_LOCATION_RESOURCE &&
Entry.hSrcModule == hSrcModule &&
!lstrcmpW( Entry.wszSource, pSrcResource ) &&
Entry.Width == Width &&
Entry.Height == Height &&
Entry.Depth == Depth &&
Entry.MipLevels == MipLevels &&
Entry.Usage == Usage &&
Entry.Format == Format &&
Entry.Pool == Pool &&
Entry.Type == D3DRTYPE_VOLUMETEXTURE )
{
// A match is found. Obtain the IDirect3DVolumeTexture9 interface and return that.
return Entry.pTexture->QueryInterface( IID_IDirect3DVolumeTexture9, (LPVOID*)ppVolumeTexture );
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateVolumeTextureFromResourceEx( pDevice, hSrcModule, pSrcResource, Width, Height, Depth, MipLevels, Usage,
Format, Pool, Filter, MipFilter, ColorKey, pSrcInfo, pPalette, ppVolumeTexture );
if( FAILED( hr ) )
return hr;
DXUTCache_Texture NewEntry;
NewEntry.Location = DXUTCACHE_LOCATION_RESOURCE;
NewEntry.hSrcModule = hSrcModule;
StringCchCopy( NewEntry.wszSource, MAX_PATH, pSrcResource );
NewEntry.Width = Width;
NewEntry.Height = Height;
NewEntry.Depth = Depth;
NewEntry.MipLevels = MipLevels;
NewEntry.Usage = Usage;
NewEntry.Format = Format;
NewEntry.Pool = Pool;
NewEntry.Type = D3DRTYPE_VOLUMETEXTURE;
(*ppVolumeTexture)->QueryInterface( IID_IDirect3DBaseTexture9, (LPVOID*)&NewEntry.pTexture );
m_TextureCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateFont( LPDIRECT3DDEVICE9 pDevice, UINT Height, UINT Width, UINT Weight, UINT MipLevels, BOOL Italic, DWORD CharSet, DWORD OutputPrecision, DWORD Quality, DWORD PitchAndFamily, LPCTSTR pFacename, LPD3DXFONT *ppFont )
{
D3DXFONT_DESCW Desc;
Desc.Height = Height;
Desc.Width = Width;
Desc.Weight = Weight;
Desc.MipLevels = MipLevels;
Desc.Italic = Italic;
Desc.CharSet = (BYTE)CharSet;
Desc.OutputPrecision = (BYTE)OutputPrecision;
Desc.Quality = (BYTE)Quality;
Desc.PitchAndFamily = (BYTE)PitchAndFamily;
StringCchCopy( Desc.FaceName, LF_FACESIZE, pFacename );
return CreateFontIndirect( pDevice, &Desc, ppFont );
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateFontIndirect( LPDIRECT3DDEVICE9 pDevice, CONST D3DXFONT_DESC *pDesc, LPD3DXFONT *ppFont )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_FontCache.GetSize(); ++i )
{
DXUTCache_Font &Entry = m_FontCache[i];
if( Entry.Width == pDesc->Width &&
Entry.Height == pDesc->Height &&
Entry.Weight == pDesc->Weight &&
Entry.MipLevels == pDesc->MipLevels &&
Entry.Italic == pDesc->Italic &&
Entry.CharSet == pDesc->CharSet &&
Entry.OutputPrecision == pDesc->OutputPrecision &&
Entry.Quality == pDesc->Quality &&
Entry.PitchAndFamily == pDesc->PitchAndFamily &&
CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE,
Entry.FaceName, -1,
pDesc->FaceName, -1 ) == CSTR_EQUAL )
{
// A match is found. Increment the reference and return the ID3DXFont object.
Entry.pFont->AddRef();
*ppFont = Entry.pFont;
return S_OK;
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateFontIndirect( pDevice, pDesc, ppFont );
if( FAILED( hr ) )
return hr;
DXUTCache_Font NewEntry;
(D3DXFONT_DESC &)NewEntry = *pDesc;
NewEntry.pFont = *ppFont;
NewEntry.pFont->AddRef();
m_FontCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateEffectFromFile( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, const D3DXMACRO *pDefines, LPD3DXINCLUDE pInclude, DWORD Flags, LPD3DXEFFECTPOOL pPool, LPD3DXEFFECT *ppEffect, LPD3DXBUFFER *ppCompilationErrors )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_EffectCache.GetSize(); ++i )
{
DXUTCache_Effect &Entry = m_EffectCache[i];
if( Entry.Location == DXUTCACHE_LOCATION_FILE &&
!lstrcmpW( Entry.wszSource, pSrcFile ) &&
Entry.dwFlags == Flags )
{
// A match is found. Increment the ref coutn and return the ID3DXEffect object.
*ppEffect = Entry.pEffect;
(*ppEffect)->AddRef();
return S_OK;
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateEffectFromFile( pDevice, pSrcFile, pDefines, pInclude, Flags, pPool, ppEffect, ppCompilationErrors );
if( FAILED( hr ) )
return hr;
DXUTCache_Effect NewEntry;
NewEntry.Location = DXUTCACHE_LOCATION_FILE;
StringCchCopy( NewEntry.wszSource, MAX_PATH, pSrcFile );
NewEntry.dwFlags = Flags;
NewEntry.pEffect = *ppEffect;
NewEntry.pEffect->AddRef();
m_EffectCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateEffectFromResource( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, const D3DXMACRO *pDefines, LPD3DXINCLUDE pInclude, DWORD Flags, LPD3DXEFFECTPOOL pPool, LPD3DXEFFECT *ppEffect, LPD3DXBUFFER *ppCompilationErrors )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_EffectCache.GetSize(); ++i )
{
DXUTCache_Effect &Entry = m_EffectCache[i];
if( Entry.Location == DXUTCACHE_LOCATION_RESOURCE &&
Entry.hSrcModule == hSrcModule &&
!lstrcmpW( Entry.wszSource, pSrcResource ) &&
Entry.dwFlags == Flags )
{
// A match is found. Increment the ref coutn and return the ID3DXEffect object.
*ppEffect = Entry.pEffect;
(*ppEffect)->AddRef();
return S_OK;
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateEffectFromResource( pDevice, hSrcModule, pSrcResource, pDefines, pInclude, Flags,
pPool, ppEffect, ppCompilationErrors );
if( FAILED( hr ) )
return hr;
DXUTCache_Effect NewEntry;
NewEntry.Location = DXUTCACHE_LOCATION_RESOURCE;
NewEntry.hSrcModule = hSrcModule;
StringCchCopy( NewEntry.wszSource, MAX_PATH, pSrcResource );
NewEntry.dwFlags = Flags;
NewEntry.pEffect = *ppEffect;
NewEntry.pEffect->AddRef();
m_EffectCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Device event callbacks
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::OnCreateDevice( IDirect3DDevice9 *pd3dDevice )
{
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::OnResetDevice( IDirect3DDevice9 *pd3dDevice )
{
// Call OnResetDevice on all effect and font objects
for( int i = 0; i < m_EffectCache.GetSize(); ++i )
m_EffectCache[i].pEffect->OnResetDevice();
for( int i = 0; i < m_FontCache.GetSize(); ++i )
m_FontCache[i].pFont->OnResetDevice();
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::OnLostDevice()
{
// Call OnLostDevice on all effect and font objects
for( int i = 0; i < m_EffectCache.GetSize(); ++i )
m_EffectCache[i].pEffect->OnLostDevice();
for( int i = 0; i < m_FontCache.GetSize(); ++i )
m_FontCache[i].pFont->OnLostDevice();
// Release all the default pool textures
for( int i = m_TextureCache.GetSize() - 1; i >= 0; --i )
if( m_TextureCache[i].Pool == D3DPOOL_DEFAULT )
{
SAFE_RELEASE( m_TextureCache[i].pTexture );
m_TextureCache.Remove( i ); // Remove the entry
}
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::OnDestroyDevice()
{
// Release all resources
for( int i = m_EffectCache.GetSize() - 1; i >= 0; --i )
{
SAFE_RELEASE( m_EffectCache[i].pEffect );
m_EffectCache.Remove( i );
}
for( int i = m_FontCache.GetSize() - 1; i >= 0; --i )
{
SAFE_RELEASE( m_FontCache[i].pFont );
m_FontCache.Remove( i );
}
for( int i = m_TextureCache.GetSize() - 1; i >= 0; --i )
{
SAFE_RELEASE( m_TextureCache[i].pTexture );
m_TextureCache.Remove( i );
}
return S_OK;
}
//--------------------------------------------------------------------------------------
CD3DArcBall::CD3DArcBall()
{
Reset();
m_vDownPt = D3DXVECTOR3(0,0,0);
m_vCurrentPt = D3DXVECTOR3(0,0,0);
m_Offset.x = m_Offset.y = 0;
RECT rc;
GetClientRect( GetForegroundWindow(), &rc );
SetWindow( rc.right, rc.bottom );
}
//--------------------------------------------------------------------------------------
void CD3DArcBall::Reset()
{
D3DXQuaternionIdentity( &m_qDown );
D3DXQuaternionIdentity( &m_qNow );
D3DXMatrixIdentity( &m_mRotation );
D3DXMatrixIdentity( &m_mTranslation );
D3DXMatrixIdentity( &m_mTranslationDelta );
m_bDrag = FALSE;
m_fRadiusTranslation = 1.0f;
m_fRadius = 1.0f;
}
//--------------------------------------------------------------------------------------
D3DXVECTOR3 CD3DArcBall::ScreenToVector( float fScreenPtX, float fScreenPtY )
{
// Scale to screen
FLOAT x = -(fScreenPtX - m_Offset.x - m_nWidth/2) / (m_fRadius*m_nWidth/2);
FLOAT y = (fScreenPtY - m_Offset.y - m_nHeight/2) / (m_fRadius*m_nHeight/2);
FLOAT z = 0.0f;
FLOAT mag = x*x + y*y;
if( mag > 1.0f )
{
FLOAT scale = 1.0f/sqrtf(mag);
x *= scale;
y *= scale;
}
else
z = sqrtf( 1.0f - mag );
// Return vector
return D3DXVECTOR3( x, y, z );
}
//--------------------------------------------------------------------------------------
D3DXQUATERNION CD3DArcBall::QuatFromBallPoints(const D3DXVECTOR3 &vFrom, const D3DXVECTOR3 &vTo)
{
D3DXVECTOR3 vPart;
float fDot = D3DXVec3Dot(&vFrom, &vTo);
D3DXVec3Cross(&vPart, &vFrom, &vTo);
return D3DXQUATERNION(vPart.x, vPart.y, vPart.z, fDot);
}
//--------------------------------------------------------------------------------------
void CD3DArcBall::OnBegin( int nX, int nY )
{
// Only enter the drag state if the click falls
// inside the click rectangle.
if( nX >= m_Offset.x &&
nX < m_Offset.x + m_nWidth &&
nY >= m_Offset.y &&
nY < m_Offset.y + m_nHeight )
{
m_bDrag = true;
m_qDown = m_qNow;
m_vDownPt = ScreenToVector( (float)nX, (float)nY );
}
}
//--------------------------------------------------------------------------------------
void CD3DArcBall::OnMove( int nX, int nY )
{
if (m_bDrag)
{
m_vCurrentPt = ScreenToVector( (float)nX, (float)nY );
m_qNow = m_qDown * QuatFromBallPoints( m_vDownPt, m_vCurrentPt );
}
}
//--------------------------------------------------------------------------------------
void CD3DArcBall::OnEnd()
{
m_bDrag = false;
}
//--------------------------------------------------------------------------------------
// Desc:
//--------------------------------------------------------------------------------------
LRESULT CD3DArcBall::HandleMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
// Current mouse position
int iMouseX = (short)LOWORD(lParam);
int iMouseY = (short)HIWORD(lParam);
switch( uMsg )
{
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK:
SetCapture( hWnd );
OnBegin( iMouseX, iMouseY );
return TRUE;
case WM_LBUTTONUP:
ReleaseCapture();
OnEnd();
return TRUE;
case WM_CAPTURECHANGED:
if( (HWND)lParam != hWnd )
{
ReleaseCapture();
OnEnd();
}
return TRUE;
case WM_RBUTTONDOWN:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDOWN:
case WM_MBUTTONDBLCLK:
SetCapture( hWnd );
// Store off the position of the cursor when the button is pressed
m_ptLastMouse.x = iMouseX;
m_ptLastMouse.y = iMouseY;
return TRUE;
case WM_RBUTTONUP:
case WM_MBUTTONUP:
ReleaseCapture();
return TRUE;
case WM_MOUSEMOVE:
if( MK_LBUTTON&wParam )
{
OnMove( iMouseX, iMouseY );
}
else if( (MK_RBUTTON&wParam) || (MK_MBUTTON&wParam) )
{
// Normalize based on size of window and bounding sphere radius
FLOAT fDeltaX = ( m_ptLastMouse.x-iMouseX ) * m_fRadiusTranslation / m_nWidth;
FLOAT fDeltaY = ( m_ptLastMouse.y-iMouseY ) * m_fRadiusTranslation / m_nHeight;
if( wParam & MK_RBUTTON )
{
D3DXMatrixTranslation( &m_mTranslationDelta, -2*fDeltaX, 2*fDeltaY, 0.0f );
D3DXMatrixMultiply( &m_mTranslation, &m_mTranslation, &m_mTranslationDelta );
}
else // wParam & MK_MBUTTON
{
D3DXMatrixTranslation( &m_mTranslationDelta, 0.0f, 0.0f, 5*fDeltaY );
D3DXMatrixMultiply( &m_mTranslation, &m_mTranslation, &m_mTranslationDelta );
}
// Store mouse coordinate
m_ptLastMouse.x = iMouseX;
m_ptLastMouse.y = iMouseY;
}
return TRUE;
}
return FALSE;
}
//--------------------------------------------------------------------------------------
// Constructor
//--------------------------------------------------------------------------------------
CBaseCamera::CBaseCamera()
{
m_cKeysDown = 0;
ZeroMemory( m_aKeys, sizeof(BYTE)*CAM_MAX_KEYS );
ZeroMemory( m_GamePad, sizeof(DXUT_GAMEPAD)*DXUT_MAX_CONTROLLERS );
// Set attributes for the view matrix
D3DXVECTOR3 vEyePt = D3DXVECTOR3(0.0f,0.0f,0.0f);
D3DXVECTOR3 vLookatPt = D3DXVECTOR3(0.0f,0.0f,1.0f);
// Setup the view matrix
SetViewParams( &vEyePt, &vLookatPt );
// Setup the projection matrix
SetProjParams( D3DX_PI/4, 1.0f, 1.0f, 1000.0f );
GetCursorPos( &m_ptLastMousePosition );
m_bMouseLButtonDown = false;
m_bMouseMButtonDown = false;
m_bMouseRButtonDown = false;
m_nCurrentButtonMask = 0;
m_nMouseWheelDelta = 0;
m_fCameraYawAngle = 0.0f;
m_fCameraPitchAngle = 0.0f;
SetRect( &m_rcDrag, LONG_MIN, LONG_MIN, LONG_MAX, LONG_MAX );
m_vVelocity = D3DXVECTOR3(0,0,0);
m_bMovementDrag = false;
m_vVelocityDrag = D3DXVECTOR3(0,0,0);
m_fDragTimer = 0.0f;
m_fTotalDragTimeToZero = 0.25;
m_vRotVelocity = D3DXVECTOR2(0,0);
m_fRotationScaler = 0.01f;
m_fMoveScaler = 5.0f;
m_bInvertPitch = false;
m_bEnableYAxisMovement = true;
m_bEnablePositionMovement = true;
m_vMouseDelta = D3DXVECTOR2(0,0);
m_fFramesToSmoothMouseData = 2.0f;
m_bClipToBoundary = false;
m_vMinBoundary = D3DXVECTOR3(-1,-1,-1);
m_vMaxBoundary = D3DXVECTOR3(1,1,1);
}
//--------------------------------------------------------------------------------------
// Client can call this to change the position and direction of camera
//--------------------------------------------------------------------------------------
VOID CBaseCamera::SetViewParams( D3DXVECTOR3* pvEyePt, D3DXVECTOR3* pvLookatPt )
{
if( NULL == pvEyePt || NULL == pvLookatPt )
return;
m_vDefaultEye = m_vEye = *pvEyePt;
m_vDefaultLookAt = m_vLookAt = *pvLookatPt;
// Calc the view matrix
D3DXVECTOR3 vUp(0,1,0);
D3DXMatrixLookAtLH( &m_mView, pvEyePt, pvLookatPt, &vUp );
D3DXMATRIX mInvView;
D3DXMatrixInverse( &mInvView, NULL, &m_mView );
// The axis basis vectors and camera position are stored inside the
// position matrix in the 4 rows of the camera's world matrix.
// To figure out the yaw/pitch of the camera, we just need the Z basis vector
D3DXVECTOR3* pZBasis = (D3DXVECTOR3*) &mInvView._31;
m_fCameraYawAngle = atan2f( pZBasis->x, pZBasis->z );
float fLen = sqrtf(pZBasis->z*pZBasis->z + pZBasis->x*pZBasis->x);
m_fCameraPitchAngle = -atan2f( pZBasis->y, fLen );
}
//--------------------------------------------------------------------------------------
// Calculates the projection matrix based on input params
//--------------------------------------------------------------------------------------
VOID CBaseCamera::SetProjParams( FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane,
FLOAT fFarPlane )
{
// Set attributes for the projection matrix
m_fFOV = fFOV;
m_fAspect = fAspect;
m_fNearPlane = fNearPlane;
m_fFarPlane = fFarPlane;
D3DXMatrixPerspectiveFovLH( &m_mProj, fFOV, fAspect, fNearPlane, fFarPlane );
}
//--------------------------------------------------------------------------------------
// Call this from your message proc so this class can handle window messages
//--------------------------------------------------------------------------------------
LRESULT CBaseCamera::HandleMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
UNREFERENCED_PARAMETER( hWnd );
UNREFERENCED_PARAMETER( lParam );
switch( uMsg )
{
case WM_KEYDOWN:
{
// Map this key to a D3DUtil_CameraKeys enum and update the
// state of m_aKeys[] by adding the KEY_WAS_DOWN_MASK|KEY_IS_DOWN_MASK mask
// only if the key is not down
D3DUtil_CameraKeys mappedKey = MapKey( (UINT)wParam );
if( mappedKey != CAM_UNKNOWN )
{
if( FALSE == IsKeyDown(m_aKeys[mappedKey]) )
{
m_aKeys[ mappedKey ] = KEY_WAS_DOWN_MASK | KEY_IS_DOWN_MASK;
++m_cKeysDown;
}
}
break;
}
case WM_KEYUP:
{
// Map this key to a D3DUtil_CameraKeys enum and update the
// state of m_aKeys[] by removing the KEY_IS_DOWN_MASK mask.
D3DUtil_CameraKeys mappedKey = MapKey( (UINT)wParam );
if( mappedKey != CAM_UNKNOWN && (DWORD)mappedKey < 8 )
{
m_aKeys[ mappedKey ] &= ~KEY_IS_DOWN_MASK;
--m_cKeysDown;
}
break;
}
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_LBUTTONDOWN:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
case WM_LBUTTONDBLCLK:
{
// Compute the drag rectangle in screen coord.
POINT ptCursor = { (short)LOWORD(lParam), (short)HIWORD(lParam) };
// Update member var state
if( ( uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONDBLCLK ) && PtInRect( &m_rcDrag, ptCursor ) )
{ m_bMouseLButtonDown = true; m_nCurrentButtonMask |= MOUSE_LEFT_BUTTON; }
if( ( uMsg == WM_MBUTTONDOWN || uMsg == WM_MBUTTONDBLCLK ) && PtInRect( &m_rcDrag, ptCursor ) )
{ m_bMouseMButtonDown = true; m_nCurrentButtonMask |= MOUSE_MIDDLE_BUTTON; }
if( ( uMsg == WM_RBUTTONDOWN || uMsg == WM_RBUTTONDBLCLK ) && PtInRect( &m_rcDrag, ptCursor ) )
{ m_bMouseRButtonDown = true; m_nCurrentButtonMask |= MOUSE_RIGHT_BUTTON; }
// Capture the mouse, so if the mouse button is
// released outside the window, we'll get the WM_LBUTTONUP message
SetCapture(hWnd);
GetCursorPos( &m_ptLastMousePosition );
return TRUE;
}
case WM_RBUTTONUP:
case WM_MBUTTONUP:
case WM_LBUTTONUP:
{
// Update member var state
if( uMsg == WM_LBUTTONUP ) { m_bMouseLButtonDown = false; m_nCurrentButtonMask &= ~MOUSE_LEFT_BUTTON; }
if( uMsg == WM_MBUTTONUP ) { m_bMouseMButtonDown = false; m_nCurrentButtonMask &= ~MOUSE_MIDDLE_BUTTON; }
if( uMsg == WM_RBUTTONUP ) { m_bMouseRButtonDown = false; m_nCurrentButtonMask &= ~MOUSE_RIGHT_BUTTON; }
// Release the capture if no mouse buttons down
if( !m_bMouseLButtonDown &&
!m_bMouseRButtonDown &&
!m_bMouseMButtonDown )
{
ReleaseCapture();
}
break;
}
case WM_CAPTURECHANGED:
{
if( (HWND)lParam != hWnd )
{
if( (m_nCurrentButtonMask & MOUSE_LEFT_BUTTON) ||
(m_nCurrentButtonMask & MOUSE_MIDDLE_BUTTON) ||
(m_nCurrentButtonMask & MOUSE_RIGHT_BUTTON) )
{
m_bMouseLButtonDown = false;
m_bMouseMButtonDown = false;
m_bMouseRButtonDown = false;
m_nCurrentButtonMask &= ~MOUSE_LEFT_BUTTON;
m_nCurrentButtonMask &= ~MOUSE_MIDDLE_BUTTON;
m_nCurrentButtonMask &= ~MOUSE_RIGHT_BUTTON;
ReleaseCapture();
}
}
break;
}
case WM_MOUSEWHEEL:
// Update member var state
m_nMouseWheelDelta = (short)HIWORD(wParam) / 120;
break;
}
return FALSE;
}
//--------------------------------------------------------------------------------------
// Figure out the velocity based on keyboard input & drag if any
//--------------------------------------------------------------------------------------
void CBaseCamera::GetInput( bool bGetKeyboardInput, bool bGetMouseInput, bool bGetGamepadInput, bool bResetCursorAfterMove )
{
m_vKeyboardDirection = D3DXVECTOR3(0,0,0);
if( bGetKeyboardInput )
{
// Update acceleration vector based on keyboard state
if( IsKeyDown(m_aKeys[CAM_MOVE_FORWARD]) )
m_vKeyboardDirection.z += 1.0f;
if( IsKeyDown(m_aKeys[CAM_MOVE_BACKWARD]) )
m_vKeyboardDirection.z -= 1.0f;
if( m_bEnableYAxisMovement )
{
if( IsKeyDown(m_aKeys[CAM_MOVE_UP]) )
m_vKeyboardDirection.y += 1.0f;
if( IsKeyDown(m_aKeys[CAM_MOVE_DOWN]) )
m_vKeyboardDirection.y -= 1.0f;
}
if( IsKeyDown(m_aKeys[CAM_STRAFE_RIGHT]) )
m_vKeyboardDirection.x += 1.0f;
if( IsKeyDown(m_aKeys[CAM_STRAFE_LEFT]) )
m_vKeyboardDirection.x -= 1.0f;
}
if( bGetMouseInput )
{
// Get current position of mouse
POINT ptCurMouseDelta;
POINT ptCurMousePos;
if( GetCursorPos( &ptCurMousePos ) )
{
// Calc how far it's moved since last frame
ptCurMouseDelta.x = ptCurMousePos.x - m_ptLastMousePosition.x;
ptCurMouseDelta.y = ptCurMousePos.y - m_ptLastMousePosition.y;
// Record current position for next time
m_ptLastMousePosition = ptCurMousePos;
}
else
{
// If GetCursorPos() returns false, just set delta to zero
ptCurMouseDelta.x = 0;
ptCurMouseDelta.y = 0;
}
if( bResetCursorAfterMove && DXUTIsActive() )
{
// Set position of camera to center of desktop,
// so it always has room to move. This is very useful
// if the cursor is hidden. If this isn't done and cursor is hidden,
// then invisible cursor will hit the edge of the screen
// and the user can't tell what happened
POINT ptCenter;
// Get the center of the current monitor
MONITORINFO mi;
mi.cbSize = sizeof(MONITORINFO);
DXUTGetMonitorInfo( DXUTMonitorFromWindow(DXUTGetHWND(),MONITOR_DEFAULTTONEAREST), &mi );
ptCenter.x = (mi.rcMonitor.left + mi.rcMonitor.right) / 2;
ptCenter.y = (mi.rcMonitor.top + mi.rcMonitor.bottom) / 2;
SetCursorPos( ptCenter.x, ptCenter.y );
m_ptLastMousePosition = ptCenter;
}
// Smooth the relative mouse data over a few frames so it isn't
// jerky when moving slowly at low frame rates.
float fPercentOfNew = 1.0f / m_fFramesToSmoothMouseData;
float fPercentOfOld = 1.0f - fPercentOfNew;
m_vMouseDelta.x = m_vMouseDelta.x*fPercentOfOld + ptCurMouseDelta.x*fPercentOfNew;
m_vMouseDelta.y = m_vMouseDelta.y*fPercentOfOld + ptCurMouseDelta.y*fPercentOfNew;
}
if( bGetGamepadInput )
{
m_vGamePadLeftThumb = D3DXVECTOR3(0,0,0);
m_vGamePadRightThumb = D3DXVECTOR3(0,0,0);
// Get controller state
for( DWORD iUserIndex=0; iUserIndex<DXUT_MAX_CONTROLLERS; iUserIndex++ )
{
DXUTGetGamepadState( iUserIndex, &m_GamePad[iUserIndex], true, true );
// Mark time if the controller is in a non-zero state
if( m_GamePad[iUserIndex].wButtons ||
m_GamePad[iUserIndex].sThumbLX || m_GamePad[iUserIndex].sThumbLX ||
m_GamePad[iUserIndex].sThumbRX || m_GamePad[iUserIndex].sThumbRY ||
m_GamePad[iUserIndex].bLeftTrigger || m_GamePad[iUserIndex].bRightTrigger )
{
m_GamePadLastActive[iUserIndex] = DXUTGetTime();
}
}
// Find out which controller was non-zero last
int iMostRecentlyActive = -1;
double fMostRecentlyActiveTime = 0.0f;
for( DWORD iUserIndex=0; iUserIndex<DXUT_MAX_CONTROLLERS; iUserIndex++ )
{
if( m_GamePadLastActive[iUserIndex] > fMostRecentlyActiveTime )
{
fMostRecentlyActiveTime = m_GamePadLastActive[iUserIndex];
iMostRecentlyActive = iUserIndex;
}
}
// Use the most recent non-zero controller if its connected
if( iMostRecentlyActive >= 0 && m_GamePad[iMostRecentlyActive].bConnected )
{
m_vGamePadLeftThumb.x = m_GamePad[iMostRecentlyActive].fThumbLX;
m_vGamePadLeftThumb.y = 0.0f;
m_vGamePadLeftThumb.z = m_GamePad[iMostRecentlyActive].fThumbLY;
m_vGamePadRightThumb.x = m_GamePad[iMostRecentlyActive].fThumbRX;
m_vGamePadRightThumb.y = 0.0f;
m_vGamePadRightThumb.z = m_GamePad[iMostRecentlyActive].fThumbRY;
}
}
}
//--------------------------------------------------------------------------------------
// Figure out the velocity based on keyboard input & drag if any
//--------------------------------------------------------------------------------------
void CBaseCamera::UpdateVelocity( float fElapsedTime )
{
D3DXMATRIX mRotDelta;
D3DXVECTOR2 vGamePadRightThumb = D3DXVECTOR2(m_vGamePadRightThumb.x, -m_vGamePadRightThumb.z);
m_vRotVelocity = m_vMouseDelta * m_fRotationScaler + vGamePadRightThumb * 0.02f;
D3DXVECTOR3 vAccel = m_vKeyboardDirection + m_vGamePadLeftThumb;
// Normalize vector so if moving 2 dirs (left & forward),
// the camera doesn't move faster than if moving in 1 dir
D3DXVec3Normalize( &vAccel, &vAccel );
// Scale the acceleration vector
vAccel *= m_fMoveScaler;
if( m_bMovementDrag )
{
// Is there any acceleration this frame?
if( D3DXVec3LengthSq( &vAccel ) > 0 )
{
// If so, then this means the user has pressed a movement key\
// so change the velocity immediately to acceleration
// upon keyboard input. This isn't normal physics
// but it will give a quick response to keyboard input
m_vVelocity = vAccel;
m_fDragTimer = m_fTotalDragTimeToZero;
m_vVelocityDrag = vAccel / m_fDragTimer;
}
else
{
// If no key being pressed, then slowly decrease velocity to 0
if( m_fDragTimer > 0 )
{
// Drag until timer is <= 0
m_vVelocity -= m_vVelocityDrag * fElapsedTime;
m_fDragTimer -= fElapsedTime;
}
else
{
// Zero velocity
m_vVelocity = D3DXVECTOR3(0,0,0);
}
}
}
else
{
// No drag, so immediately change the velocity
m_vVelocity = vAccel;
}
}
//--------------------------------------------------------------------------------------
// Clamps pV to lie inside m_vMinBoundary & m_vMaxBoundary
//--------------------------------------------------------------------------------------
void CBaseCamera::ConstrainToBoundary( D3DXVECTOR3* pV )
{
// Constrain vector to a bounding box
pV->x = __max(pV->x, m_vMinBoundary.x);
pV->y = __max(pV->y, m_vMinBoundary.y);
pV->z = __max(pV->z, m_vMinBoundary.z);
pV->x = __min(pV->x, m_vMaxBoundary.x);
pV->y = __min(pV->y, m_vMaxBoundary.y);
pV->z = __min(pV->z, m_vMaxBoundary.z);
}
//--------------------------------------------------------------------------------------
// Maps a windows virtual key to an enum
//--------------------------------------------------------------------------------------
D3DUtil_CameraKeys CBaseCamera::MapKey( UINT nKey )
{
// This could be upgraded to a method that's user-definable but for
// simplicity, we'll use a hardcoded mapping.
switch( nKey )
{
case VK_CONTROL: return CAM_CONTROLDOWN;
case VK_LEFT: return CAM_STRAFE_LEFT;
case VK_RIGHT: return CAM_STRAFE_RIGHT;
case VK_UP: return CAM_MOVE_FORWARD;
case VK_DOWN: return CAM_MOVE_BACKWARD;
case VK_PRIOR: return CAM_MOVE_UP; // pgup
case VK_NEXT: return CAM_MOVE_DOWN; // pgdn
case 'A': return CAM_STRAFE_LEFT;
case 'D': return CAM_STRAFE_RIGHT;
case 'W': return CAM_MOVE_FORWARD;
case 'S': return CAM_MOVE_BACKWARD;
case 'Q': return CAM_MOVE_DOWN;
case 'E': return CAM_MOVE_UP;
case VK_NUMPAD4: return CAM_STRAFE_LEFT;
case VK_NUMPAD6: return CAM_STRAFE_RIGHT;
case VK_NUMPAD8: return CAM_MOVE_FORWARD;
case VK_NUMPAD2: return CAM_MOVE_BACKWARD;
case VK_NUMPAD9: return CAM_MOVE_UP;
case VK_NUMPAD3: return CAM_MOVE_DOWN;
case VK_HOME: return CAM_RESET;
}
return CAM_UNKNOWN;
}
//--------------------------------------------------------------------------------------
// Reset the camera's position back to the default
//--------------------------------------------------------------------------------------
VOID CBaseCamera::Reset()
{
SetViewParams( &m_vDefaultEye, &m_vDefaultLookAt );
}
//--------------------------------------------------------------------------------------
// Constructor
//--------------------------------------------------------------------------------------
CFirstPersonCamera::CFirstPersonCamera() :
m_nActiveButtonMask( 0x07 )
{
m_bRotateWithoutButtonDown = false;
}
//--------------------------------------------------------------------------------------
// Update the view matrix based on user input & elapsed time
//--------------------------------------------------------------------------------------
VOID CFirstPersonCamera::FrameMove( FLOAT fElapsedTime )
{
if( DXUTGetGlobalTimer()->IsStopped() )
fElapsedTime = 1.0f / DXUTGetFPS();
if( IsKeyDown(m_aKeys[CAM_RESET]) )
Reset();
// Get keyboard/mouse/gamepad input
GetInput( m_bEnablePositionMovement, (m_nActiveButtonMask & m_nCurrentButtonMask) || m_bRotateWithoutButtonDown, true, m_bResetCursorAfterMove );
// Get amount of velocity based on the keyboard input and drag (if any)
UpdateVelocity( fElapsedTime );
// Simple euler method to calculate position delta
D3DXVECTOR3 vPosDelta = m_vVelocity * fElapsedTime;
// If rotating the camera
if( (m_nActiveButtonMask & m_nCurrentButtonMask) ||
m_bRotateWithoutButtonDown ||
m_vGamePadRightThumb.x != 0 ||
m_vGamePadRightThumb.z != 0 )
{
// Update the pitch & yaw angle based on mouse movement
float fYawDelta = m_vRotVelocity.x;
float fPitchDelta = m_vRotVelocity.y;
// Invert pitch if requested
if( m_bInvertPitch )
fPitchDelta = -fPitchDelta;
m_fCameraPitchAngle += fPitchDelta;
m_fCameraYawAngle += fYawDelta;
// Limit pitch to straight up or straight down
m_fCameraPitchAngle = __max( -D3DX_PI/2.0f, m_fCameraPitchAngle );
m_fCameraPitchAngle = __min( +D3DX_PI/2.0f, m_fCameraPitchAngle );
}
// Make a rotation matrix based on the camera's yaw & pitch
D3DXMATRIX mCameraRot;
D3DXMatrixRotationYawPitchRoll( &mCameraRot, m_fCameraYawAngle, m_fCameraPitchAngle, 0 );
// Transform vectors based on camera's rotation matrix
D3DXVECTOR3 vWorldUp, vWorldAhead;
D3DXVECTOR3 vLocalUp = D3DXVECTOR3(0,1,0);
D3DXVECTOR3 vLocalAhead = D3DXVECTOR3(0,0,1);
D3DXVec3TransformCoord( &vWorldUp, &vLocalUp, &mCameraRot );
D3DXVec3TransformCoord( &vWorldAhead, &vLocalAhead, &mCameraRot );
// Transform the position delta by the camera's rotation
D3DXVECTOR3 vPosDeltaWorld;
if( !m_bEnableYAxisMovement )
{
// If restricting Y movement, do not include pitch
// when transforming position delta vector.
D3DXMatrixRotationYawPitchRoll( &mCameraRot, m_fCameraYawAngle, 0.0f, 0.0f );
}
D3DXVec3TransformCoord( &vPosDeltaWorld, &vPosDelta, &mCameraRot );
// Move the eye position
m_vEye += vPosDeltaWorld;
if( m_bClipToBoundary )
ConstrainToBoundary( &m_vEye );
// Update the lookAt position based on the eye position
m_vLookAt = m_vEye + vWorldAhead;
// Update the view matrix
D3DXMatrixLookAtLH( &m_mView, &m_vEye, &m_vLookAt, &vWorldUp );
D3DXMatrixInverse( &m_mCameraWorld, NULL, &m_mView );
}
//--------------------------------------------------------------------------------------
// Enable or disable each of the mouse buttons for rotation drag.
//--------------------------------------------------------------------------------------
void CFirstPersonCamera::SetRotateButtons( bool bLeft, bool bMiddle, bool bRight, bool bRotateWithoutButtonDown )
{
m_nActiveButtonMask = ( bLeft ? MOUSE_LEFT_BUTTON : 0 ) |
( bMiddle ? MOUSE_MIDDLE_BUTTON : 0 ) |
( bRight ? MOUSE_RIGHT_BUTTON : 0 );
m_bRotateWithoutButtonDown = bRotateWithoutButtonDown;
}
//--------------------------------------------------------------------------------------
// Constructor
//--------------------------------------------------------------------------------------
CModelViewerCamera::CModelViewerCamera()
{
D3DXMatrixIdentity( &m_mWorld );
D3DXMatrixIdentity( &m_mModelRot );
D3DXMatrixIdentity( &m_mModelLastRot );
D3DXMatrixIdentity( &m_mCameraRotLast );
m_vModelCenter = D3DXVECTOR3(0,0,0);
m_fRadius = 5.0f;
m_fDefaultRadius = 5.0f;
m_fMinRadius = 1.0f;
m_fMaxRadius = FLT_MAX;
m_bLimitPitch = false;
m_bEnablePositionMovement = false;
m_bAttachCameraToModel = false;
m_nRotateModelButtonMask = MOUSE_LEFT_BUTTON;
m_nZoomButtonMask = MOUSE_WHEEL;
m_nRotateCameraButtonMask = MOUSE_RIGHT_BUTTON;
m_bDragSinceLastUpdate = true;
}
//--------------------------------------------------------------------------------------
// Update the view matrix & the model's world matrix based
// on user input & elapsed time
//--------------------------------------------------------------------------------------
VOID CModelViewerCamera::FrameMove( FLOAT fElapsedTime )
{
if( IsKeyDown(m_aKeys[CAM_RESET]) )
Reset();
// If no dragged has happend since last time FrameMove is called,
// and no camera key is held down, then no need to handle again.
if( !m_bDragSinceLastUpdate && 0 == m_cKeysDown )
return;
m_bDragSinceLastUpdate = false;
// Get keyboard/mouse/gamepad input
GetInput( m_bEnablePositionMovement, m_nCurrentButtonMask != 0, true, false );
// Get amount of velocity based on the keyboard input and drag (if any)
UpdateVelocity( fElapsedTime );
// Simple euler method to calculate position delta
D3DXVECTOR3 vPosDelta = m_vVelocity * fElapsedTime;
// Change the radius from the camera to the model based on wheel scrolling
if( m_nMouseWheelDelta && m_nZoomButtonMask == MOUSE_WHEEL )
m_fRadius -= m_nMouseWheelDelta * m_fRadius * 0.1f;
m_fRadius = __min( m_fMaxRadius, m_fRadius );
m_fRadius = __max( m_fMinRadius, m_fRadius );
m_nMouseWheelDelta = 0;
// Get the inverse of the arcball's rotation matrix
D3DXMATRIX mCameraRot;
D3DXMatrixInverse( &mCameraRot, NULL, m_ViewArcBall.GetRotationMatrix() );
// Transform vectors based on camera's rotation matrix
D3DXVECTOR3 vWorldUp, vWorldAhead;
D3DXVECTOR3 vLocalUp = D3DXVECTOR3(0,1,0);
D3DXVECTOR3 vLocalAhead = D3DXVECTOR3(0,0,1);
D3DXVec3TransformCoord( &vWorldUp, &vLocalUp, &mCameraRot );
D3DXVec3TransformCoord( &vWorldAhead, &vLocalAhead, &mCameraRot );
// Transform the position delta by the camera's rotation
D3DXVECTOR3 vPosDeltaWorld;
D3DXVec3TransformCoord( &vPosDeltaWorld, &vPosDelta, &mCameraRot );
// Move the lookAt position
m_vLookAt += vPosDeltaWorld;
if( m_bClipToBoundary )
ConstrainToBoundary( &m_vLookAt );
// Update the eye point based on a radius away from the lookAt position
m_vEye = m_vLookAt - vWorldAhead * m_fRadius;
// Update the view matrix
D3DXMatrixLookAtLH( &m_mView, &m_vEye, &m_vLookAt, &vWorldUp );
D3DXMATRIX mInvView;
D3DXMatrixInverse( &mInvView, NULL, &m_mView );
mInvView._41 = mInvView._42 = mInvView._43 = 0;
D3DXMATRIX mModelLastRotInv;
D3DXMatrixInverse(&mModelLastRotInv, NULL, &m_mModelLastRot);
// Accumulate the delta of the arcball's rotation in view space.
// Note that per-frame delta rotations could be problematic over long periods of time.
D3DXMATRIX mModelRot;
mModelRot = *m_WorldArcBall.GetRotationMatrix();
m_mModelRot *= m_mView * mModelLastRotInv * mModelRot * mInvView;
if( m_ViewArcBall.IsBeingDragged() && m_bAttachCameraToModel && !IsKeyDown(m_aKeys[CAM_CONTROLDOWN]) )
{
// Attach camera to model by inverse of the model rotation
D3DXMATRIX mCameraLastRotInv;
D3DXMatrixInverse(&mCameraLastRotInv, NULL, &m_mCameraRotLast);
D3DXMATRIX mCameraRotDelta = mCameraLastRotInv * mCameraRot; // local to world matrix
m_mModelRot *= mCameraRotDelta;
}
m_mCameraRotLast = mCameraRot;
m_mModelLastRot = mModelRot;
// Since we're accumulating delta rotations, we need to orthonormalize
// the matrix to prevent eventual matrix skew
D3DXVECTOR3* pXBasis = (D3DXVECTOR3*) &m_mModelRot._11;
D3DXVECTOR3* pYBasis = (D3DXVECTOR3*) &m_mModelRot._21;
D3DXVECTOR3* pZBasis = (D3DXVECTOR3*) &m_mModelRot._31;
D3DXVec3Normalize( pXBasis, pXBasis );
D3DXVec3Cross( pYBasis, pZBasis, pXBasis );
D3DXVec3Normalize( pYBasis, pYBasis );
D3DXVec3Cross( pZBasis, pXBasis, pYBasis );
// Translate the rotation matrix to the same position as the lookAt position
m_mModelRot._41 = m_vLookAt.x;
m_mModelRot._42 = m_vLookAt.y;
m_mModelRot._43 = m_vLookAt.z;
// Translate world matrix so its at the center of the model
D3DXMATRIX mTrans;
D3DXMatrixTranslation( &mTrans, -m_vModelCenter.x, -m_vModelCenter.y, -m_vModelCenter.z );
m_mWorld = mTrans * m_mModelRot;
}
void CModelViewerCamera::SetDragRect( RECT &rc )
{
CBaseCamera::SetDragRect( rc );
m_WorldArcBall.SetOffset( rc.left, rc.top );
m_ViewArcBall.SetOffset( rc.left, rc.top );
SetWindow( rc.right - rc.left, rc.bottom - rc.top );
}
//--------------------------------------------------------------------------------------
// Reset the camera's position back to the default
//--------------------------------------------------------------------------------------
VOID CModelViewerCamera::Reset()
{
CBaseCamera::Reset();
D3DXMatrixIdentity( &m_mWorld );
D3DXMatrixIdentity( &m_mModelRot );
D3DXMatrixIdentity( &m_mModelLastRot );
D3DXMatrixIdentity( &m_mCameraRotLast );
m_fRadius = m_fDefaultRadius;
m_WorldArcBall.Reset();
m_ViewArcBall.Reset();
}
//--------------------------------------------------------------------------------------
// Override for setting the view parameters
//--------------------------------------------------------------------------------------
void CModelViewerCamera::SetViewParams( D3DXVECTOR3* pvEyePt, D3DXVECTOR3* pvLookatPt )
{
CBaseCamera::SetViewParams( pvEyePt, pvLookatPt );
// Propogate changes to the member arcball
D3DXQUATERNION quat;
D3DXMATRIXA16 mRotation;
D3DXVECTOR3 vUp(0,1,0);
D3DXMatrixLookAtLH( &mRotation, pvEyePt, pvLookatPt, &vUp );
D3DXQuaternionRotationMatrix( &quat, &mRotation );
m_ViewArcBall.SetQuatNow( quat );
// Set the radius according to the distance
D3DXVECTOR3 vEyeToPoint;
D3DXVec3Subtract( &vEyeToPoint, pvLookatPt, pvEyePt );
SetRadius( D3DXVec3Length( &vEyeToPoint ) );
// View information changed. FrameMove should be called.
m_bDragSinceLastUpdate = true;
}
//--------------------------------------------------------------------------------------
// Call this from your message proc so this class can handle window messages
//--------------------------------------------------------------------------------------
LRESULT CModelViewerCamera::HandleMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
CBaseCamera::HandleMessages( hWnd, uMsg, wParam, lParam );
if( ( (uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONDBLCLK ) && m_nRotateModelButtonMask & MOUSE_LEFT_BUTTON) ||
( (uMsg == WM_MBUTTONDOWN || uMsg == WM_MBUTTONDBLCLK ) && m_nRotateModelButtonMask & MOUSE_MIDDLE_BUTTON) ||
( (uMsg == WM_RBUTTONDOWN || uMsg == WM_RBUTTONDBLCLK ) && m_nRotateModelButtonMask & MOUSE_RIGHT_BUTTON) )
{
int iMouseX = (short)LOWORD(lParam);
int iMouseY = (short)HIWORD(lParam);
m_WorldArcBall.OnBegin( iMouseX, iMouseY );
}
if( ( (uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONDBLCLK ) && m_nRotateCameraButtonMask & MOUSE_LEFT_BUTTON) ||
( (uMsg == WM_MBUTTONDOWN || uMsg == WM_MBUTTONDBLCLK ) && m_nRotateCameraButtonMask & MOUSE_MIDDLE_BUTTON) ||
( (uMsg == WM_RBUTTONDOWN || uMsg == WM_RBUTTONDBLCLK ) && m_nRotateCameraButtonMask & MOUSE_RIGHT_BUTTON) )
{
int iMouseX = (short)LOWORD(lParam);
int iMouseY = (short)HIWORD(lParam);
m_ViewArcBall.OnBegin( iMouseX, iMouseY );
}
if( uMsg == WM_MOUSEMOVE )
{
int iMouseX = (short)LOWORD(lParam);
int iMouseY = (short)HIWORD(lParam);
m_WorldArcBall.OnMove( iMouseX, iMouseY );
m_ViewArcBall.OnMove( iMouseX, iMouseY );
}
if( (uMsg == WM_LBUTTONUP && m_nRotateModelButtonMask & MOUSE_LEFT_BUTTON) ||
(uMsg == WM_MBUTTONUP && m_nRotateModelButtonMask & MOUSE_MIDDLE_BUTTON) ||
(uMsg == WM_RBUTTONUP && m_nRotateModelButtonMask & MOUSE_RIGHT_BUTTON) )
{
m_WorldArcBall.OnEnd();
}
if( (uMsg == WM_LBUTTONUP && m_nRotateCameraButtonMask & MOUSE_LEFT_BUTTON) ||
(uMsg == WM_MBUTTONUP && m_nRotateCameraButtonMask & MOUSE_MIDDLE_BUTTON) ||
(uMsg == WM_RBUTTONUP && m_nRotateCameraButtonMask & MOUSE_RIGHT_BUTTON) )
{
m_ViewArcBall.OnEnd();
}
if( uMsg == WM_CAPTURECHANGED )
{
if( (HWND)lParam != hWnd )
{
if( (m_nRotateModelButtonMask & MOUSE_LEFT_BUTTON) ||
(m_nRotateModelButtonMask & MOUSE_MIDDLE_BUTTON) ||
(m_nRotateModelButtonMask & MOUSE_RIGHT_BUTTON) )
{
m_WorldArcBall.OnEnd();
}
if( (m_nRotateCameraButtonMask & MOUSE_LEFT_BUTTON) ||
(m_nRotateCameraButtonMask & MOUSE_MIDDLE_BUTTON) ||
(m_nRotateCameraButtonMask & MOUSE_RIGHT_BUTTON) )
{
m_ViewArcBall.OnEnd();
}
}
}
if( uMsg == WM_LBUTTONDOWN ||
uMsg == WM_LBUTTONDBLCLK ||
uMsg == WM_MBUTTONDOWN ||
uMsg == WM_MBUTTONDBLCLK ||
uMsg == WM_RBUTTONDOWN ||
uMsg == WM_RBUTTONDBLCLK ||
uMsg == WM_LBUTTONUP ||
uMsg == WM_MBUTTONUP ||
uMsg == WM_RBUTTONUP ||
uMsg == WM_MOUSEWHEEL ||
uMsg == WM_MOUSEMOVE )
{
m_bDragSinceLastUpdate = true;
}
return FALSE;
}
//--------------------------------------------------------------------------------------
// Desc: Returns a view matrix for rendering to a face of a cubemap.
//--------------------------------------------------------------------------------------
D3DXMATRIX DXUTGetCubeMapViewMatrix( DWORD dwFace )
{
D3DXVECTOR3 vEyePt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vLookDir;
D3DXVECTOR3 vUpDir;
switch( dwFace )
{
case D3DCUBEMAP_FACE_POSITIVE_X:
vLookDir = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
case D3DCUBEMAP_FACE_NEGATIVE_X:
vLookDir = D3DXVECTOR3(-1.0f, 0.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
case D3DCUBEMAP_FACE_POSITIVE_Y:
vLookDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 0.0f,-1.0f );
break;
case D3DCUBEMAP_FACE_NEGATIVE_Y:
vLookDir = D3DXVECTOR3( 0.0f,-1.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
break;
case D3DCUBEMAP_FACE_POSITIVE_Z:
vLookDir = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
case D3DCUBEMAP_FACE_NEGATIVE_Z:
vLookDir = D3DXVECTOR3( 0.0f, 0.0f,-1.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
}
// Set the view transform for this cubemap surface
D3DXMATRIXA16 mView;
D3DXMatrixLookAtLH( &mView, &vEyePt, &vLookDir, &vUpDir );
return mView;
}
//--------------------------------------------------------------------------------------
// Returns the string for the given D3DFORMAT.
//--------------------------------------------------------------------------------------
LPCWSTR DXUTD3DFormatToString( D3DFORMAT format, bool bWithPrefix )
{
WCHAR* pstr = NULL;
switch( format )
{
case D3DFMT_UNKNOWN: pstr = L"D3DFMT_UNKNOWN"; break;
case D3DFMT_R8G8B8: pstr = L"D3DFMT_R8G8B8"; break;
case D3DFMT_A8R8G8B8: pstr = L"D3DFMT_A8R8G8B8"; break;
case D3DFMT_X8R8G8B8: pstr = L"D3DFMT_X8R8G8B8"; break;
case D3DFMT_R5G6B5: pstr = L"D3DFMT_R5G6B5"; break;
case D3DFMT_X1R5G5B5: pstr = L"D3DFMT_X1R5G5B5"; break;
case D3DFMT_A1R5G5B5: pstr = L"D3DFMT_A1R5G5B5"; break;
case D3DFMT_A4R4G4B4: pstr = L"D3DFMT_A4R4G4B4"; break;
case D3DFMT_R3G3B2: pstr = L"D3DFMT_R3G3B2"; break;
case D3DFMT_A8: pstr = L"D3DFMT_A8"; break;
case D3DFMT_A8R3G3B2: pstr = L"D3DFMT_A8R3G3B2"; break;
case D3DFMT_X4R4G4B4: pstr = L"D3DFMT_X4R4G4B4"; break;
case D3DFMT_A2B10G10R10: pstr = L"D3DFMT_A2B10G10R10"; break;
case D3DFMT_A8B8G8R8: pstr = L"D3DFMT_A8B8G8R8"; break;
case D3DFMT_X8B8G8R8: pstr = L"D3DFMT_X8B8G8R8"; break;
case D3DFMT_G16R16: pstr = L"D3DFMT_G16R16"; break;
case D3DFMT_A2R10G10B10: pstr = L"D3DFMT_A2R10G10B10"; break;
case D3DFMT_A16B16G16R16: pstr = L"D3DFMT_A16B16G16R16"; break;
case D3DFMT_A8P8: pstr = L"D3DFMT_A8P8"; break;
case D3DFMT_P8: pstr = L"D3DFMT_P8"; break;
case D3DFMT_L8: pstr = L"D3DFMT_L8"; break;
case D3DFMT_A8L8: pstr = L"D3DFMT_A8L8"; break;
case D3DFMT_A4L4: pstr = L"D3DFMT_A4L4"; break;
case D3DFMT_V8U8: pstr = L"D3DFMT_V8U8"; break;
case D3DFMT_L6V5U5: pstr = L"D3DFMT_L6V5U5"; break;
case D3DFMT_X8L8V8U8: pstr = L"D3DFMT_X8L8V8U8"; break;
case D3DFMT_Q8W8V8U8: pstr = L"D3DFMT_Q8W8V8U8"; break;
case D3DFMT_V16U16: pstr = L"D3DFMT_V16U16"; break;
case D3DFMT_A2W10V10U10: pstr = L"D3DFMT_A2W10V10U10"; break;
case D3DFMT_UYVY: pstr = L"D3DFMT_UYVY"; break;
case D3DFMT_YUY2: pstr = L"D3DFMT_YUY2"; break;
case D3DFMT_DXT1: pstr = L"D3DFMT_DXT1"; break;
case D3DFMT_DXT2: pstr = L"D3DFMT_DXT2"; break;
case D3DFMT_DXT3: pstr = L"D3DFMT_DXT3"; break;
case D3DFMT_DXT4: pstr = L"D3DFMT_DXT4"; break;
case D3DFMT_DXT5: pstr = L"D3DFMT_DXT5"; break;
case D3DFMT_D16_LOCKABLE: pstr = L"D3DFMT_D16_LOCKABLE"; break;
case D3DFMT_D32: pstr = L"D3DFMT_D32"; break;
case D3DFMT_D15S1: pstr = L"D3DFMT_D15S1"; break;
case D3DFMT_D24S8: pstr = L"D3DFMT_D24S8"; break;
case D3DFMT_D24X8: pstr = L"D3DFMT_D24X8"; break;
case D3DFMT_D24X4S4: pstr = L"D3DFMT_D24X4S4"; break;
case D3DFMT_D16: pstr = L"D3DFMT_D16"; break;
case D3DFMT_L16: pstr = L"D3DFMT_L16"; break;
case D3DFMT_VERTEXDATA: pstr = L"D3DFMT_VERTEXDATA"; break;
case D3DFMT_INDEX16: pstr = L"D3DFMT_INDEX16"; break;
case D3DFMT_INDEX32: pstr = L"D3DFMT_INDEX32"; break;
case D3DFMT_Q16W16V16U16: pstr = L"D3DFMT_Q16W16V16U16"; break;
case D3DFMT_MULTI2_ARGB8: pstr = L"D3DFMT_MULTI2_ARGB8"; break;
case D3DFMT_R16F: pstr = L"D3DFMT_R16F"; break;
case D3DFMT_G16R16F: pstr = L"D3DFMT_G16R16F"; break;
case D3DFMT_A16B16G16R16F: pstr = L"D3DFMT_A16B16G16R16F"; break;
case D3DFMT_R32F: pstr = L"D3DFMT_R32F"; break;
case D3DFMT_G32R32F: pstr = L"D3DFMT_G32R32F"; break;
case D3DFMT_A32B32G32R32F: pstr = L"D3DFMT_A32B32G32R32F"; break;
case D3DFMT_CxV8U8: pstr = L"D3DFMT_CxV8U8"; break;
default: pstr = L"Unknown format"; break;
}
if( bWithPrefix || wcsstr( pstr, L"D3DFMT_" )== NULL )
return pstr;
else
return pstr + lstrlen( L"D3DFMT_" );
}
//--------------------------------------------------------------------------------------
// Outputs to the debug stream a formatted Unicode string with a variable-argument list.
//--------------------------------------------------------------------------------------
VOID DXUTOutputDebugStringW( LPCWSTR strMsg, ... )
{
#if defined(DEBUG) || defined(_DEBUG)
WCHAR strBuffer[512];
va_list args;
va_start(args, strMsg);
StringCchVPrintfW( strBuffer, 512, strMsg, args );
strBuffer[511] = L'\0';
va_end(args);
OutputDebugString( strBuffer );
#else
UNREFERENCED_PARAMETER(strMsg);
#endif
}
//--------------------------------------------------------------------------------------
// Outputs to the debug stream a formatted MBCS string with a variable-argument list.
//--------------------------------------------------------------------------------------
VOID DXUTOutputDebugStringA( LPCSTR strMsg, ... )
{
#if defined(DEBUG) || defined(_DEBUG)
CHAR strBuffer[512];
va_list args;
va_start(args, strMsg);
StringCchVPrintfA( strBuffer, 512, strMsg, args );
strBuffer[511] = '\0';
va_end(args);
OutputDebugStringA( strBuffer );
#else
UNREFERENCED_PARAMETER(strMsg);
#endif
}
//--------------------------------------------------------------------------------------
CDXUTLineManager::CDXUTLineManager()
{
m_pd3dDevice = NULL;
m_pD3DXLine = NULL;
}
//--------------------------------------------------------------------------------------
CDXUTLineManager::~CDXUTLineManager()
{
OnDeletedDevice();
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTLineManager::OnCreatedDevice( IDirect3DDevice9* pd3dDevice )
{
m_pd3dDevice = pd3dDevice;
HRESULT hr;
hr = D3DXCreateLine( m_pd3dDevice, &m_pD3DXLine );
if( FAILED(hr) )
return hr;
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTLineManager::OnResetDevice()
{
if( m_pD3DXLine )
m_pD3DXLine->OnResetDevice();
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTLineManager::OnRender()
{
HRESULT hr;
if( NULL == m_pD3DXLine )
return E_INVALIDARG;
bool bDrawingHasBegun = false;
float fLastWidth = 0.0f;
bool bLastAntiAlias = false;
for( int i=0; i<m_LinesList.GetSize(); i++ )
{
LINE_NODE* pLineNode = m_LinesList.GetAt(i);
if( pLineNode )
{
if( !bDrawingHasBegun ||
fLastWidth != pLineNode->fWidth ||
bLastAntiAlias != pLineNode->bAntiAlias )
{
if( bDrawingHasBegun )
{
hr = m_pD3DXLine->End();
if( FAILED(hr) )
return hr;
}
m_pD3DXLine->SetWidth( pLineNode->fWidth );
m_pD3DXLine->SetAntialias( pLineNode->bAntiAlias );
fLastWidth = pLineNode->fWidth;
bLastAntiAlias = pLineNode->bAntiAlias;
hr = m_pD3DXLine->Begin();
if( FAILED(hr) )
return hr;
bDrawingHasBegun = true;
}
hr = m_pD3DXLine->Draw( pLineNode->pVertexList, pLineNode->dwVertexListCount, pLineNode->Color );
if( FAILED(hr) )
return hr;
}
}
if( bDrawingHasBegun )
{
hr = m_pD3DXLine->End();
if( FAILED(hr) )
return hr;
}
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTLineManager::OnLostDevice()
{
if( m_pD3DXLine )
m_pD3DXLine->OnLostDevice();
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTLineManager::OnDeletedDevice()
{
RemoveAllLines();
SAFE_RELEASE( m_pD3DXLine );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTLineManager::AddLine( int* pnLineID, D3DXVECTOR2* pVertexList, DWORD dwVertexListCount, D3DCOLOR Color, float fWidth, float fScaleRatio, bool bAntiAlias )
{
if( pVertexList == NULL || dwVertexListCount == 0 )
return E_INVALIDARG;
LINE_NODE* pLineNode = new LINE_NODE;
if( pLineNode == NULL )
return E_OUTOFMEMORY;
ZeroMemory( pLineNode, sizeof(LINE_NODE) );
pLineNode->nLineID = m_LinesList.GetSize();
pLineNode->Color = Color;
pLineNode->fWidth = fWidth;
pLineNode->bAntiAlias = bAntiAlias;
pLineNode->dwVertexListCount = dwVertexListCount;
if( pnLineID )
*pnLineID = pLineNode->nLineID;
pLineNode->pVertexList = new D3DXVECTOR2[dwVertexListCount];
if( pLineNode->pVertexList == NULL )
{
delete pLineNode;
return E_OUTOFMEMORY;
}
for( DWORD i=0; i<dwVertexListCount; i++ )
{
pLineNode->pVertexList[i] = pVertexList[i] * fScaleRatio;
}
m_LinesList.Add( pLineNode );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTLineManager::AddRect( int* pnLineID, RECT rc, D3DCOLOR Color, float fWidth, float fScaleRatio, bool bAntiAlias )
{
if( fWidth > 2.0f )
{
D3DXVECTOR2 vertexList[8];
vertexList[0].x = (float)rc.left;
vertexList[0].y = (float)rc.top - (fWidth/2.0f);
vertexList[1].x = (float)rc.left;
vertexList[1].y = (float)rc.bottom + (fWidth/2.0f);
vertexList[2].x = (float)rc.left;
vertexList[2].y = (float)rc.bottom - 0.5f;
vertexList[3].x = (float)rc.right;
vertexList[3].y = (float)rc.bottom - 0.5f;
vertexList[4].x = (float)rc.right;
vertexList[4].y = (float)rc.bottom + (fWidth/2.0f);
vertexList[5].x = (float)rc.right;
vertexList[5].y = (float)rc.top - (fWidth/2.0f);
vertexList[6].x = (float)rc.right;
vertexList[6].y = (float)rc.top;
vertexList[7].x = (float)rc.left;
vertexList[7].y = (float)rc.top;
return AddLine( pnLineID, vertexList, 8, Color, fWidth, fScaleRatio, bAntiAlias );
}
else
{
D3DXVECTOR2 vertexList[5];
vertexList[0].x = (float)rc.left;
vertexList[0].y = (float)rc.top;
vertexList[1].x = (float)rc.left;
vertexList[1].y = (float)rc.bottom;
vertexList[2].x = (float)rc.right;
vertexList[2].y = (float)rc.bottom;
vertexList[3].x = (float)rc.right;
vertexList[3].y = (float)rc.top;
vertexList[4].x = (float)rc.left;
vertexList[4].y = (float)rc.top;
return AddLine( pnLineID, vertexList, 5, Color, fWidth, fScaleRatio, bAntiAlias );
}
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTLineManager::RemoveLine( int nLineID )
{
for( int i=0; i<m_LinesList.GetSize(); i++ )
{
LINE_NODE* pLineNode = m_LinesList.GetAt(i);
if( pLineNode && pLineNode->nLineID == nLineID )
{
SAFE_DELETE_ARRAY( pLineNode->pVertexList );
delete pLineNode;
m_LinesList.SetAt(i, NULL);
}
}
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTLineManager::RemoveAllLines()
{
for( int i=0; i<m_LinesList.GetSize(); i++ )
{
LINE_NODE* pLineNode = m_LinesList.GetAt(i);
if( pLineNode )
{
SAFE_DELETE_ARRAY( pLineNode->pVertexList );
delete pLineNode;
}
}
m_LinesList.RemoveAll();
return S_OK;
}
//--------------------------------------------------------------------------------------
CDXUTTextHelper::CDXUTTextHelper( ID3DXFont* pFont, ID3DXSprite* pSprite, int nLineHeight )
{
m_pFont = pFont;
m_pSprite = pSprite;
m_clr = D3DXCOLOR(1,1,1,1);
m_pt.x = 0;
m_pt.y = 0;
m_nLineHeight = nLineHeight;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTTextHelper::DrawFormattedTextLine( const WCHAR* strMsg, ... )
{
WCHAR strBuffer[512];
va_list args;
va_start(args, strMsg);
StringCchVPrintf( strBuffer, 512, strMsg, args );
strBuffer[511] = L'\0';
va_end(args);
return DrawTextLine( strBuffer );
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTTextHelper::DrawTextLine( const WCHAR* strMsg )
{
if( NULL == m_pFont )
return DXUT_ERR_MSGBOX( L"DrawTextLine", E_INVALIDARG );
HRESULT hr;
RECT rc;
SetRect( &rc, m_pt.x, m_pt.y, 0, 0 );
hr = m_pFont->DrawText( m_pSprite, strMsg, -1, &rc, DT_NOCLIP, m_clr );
if( FAILED(hr) )
return DXTRACE_ERR_MSGBOX( L"DrawText", hr );
m_pt.y += m_nLineHeight;
return S_OK;
}
HRESULT CDXUTTextHelper::DrawFormattedTextLine( RECT &rc, DWORD dwFlags, const WCHAR* strMsg, ... )
{
WCHAR strBuffer[512];
va_list args;
va_start(args, strMsg);
StringCchVPrintf( strBuffer, 512, strMsg, args );
strBuffer[511] = L'\0';
va_end(args);
return DrawTextLine( rc, dwFlags, strBuffer );
}
HRESULT CDXUTTextHelper::DrawTextLine( RECT &rc, DWORD dwFlags, const WCHAR* strMsg )
{
if( NULL == m_pFont )
return DXUT_ERR_MSGBOX( L"DrawTextLine", E_INVALIDARG );
HRESULT hr;
hr = m_pFont->DrawText( m_pSprite, strMsg, -1, &rc, dwFlags, m_clr );
if( FAILED(hr) )
return DXTRACE_ERR_MSGBOX( L"DrawText", hr );
m_pt.y += m_nLineHeight;
return S_OK;
}
//--------------------------------------------------------------------------------------
void CDXUTTextHelper::Begin()
{
if( m_pSprite )
m_pSprite->Begin( D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_TEXTURE );
}
void CDXUTTextHelper::End()
{
if( m_pSprite )
m_pSprite->End();
}
//--------------------------------------------------------------------------------------
IDirect3DDevice9* CDXUTDirectionWidget::s_pd3dDevice = NULL;
ID3DXEffect* CDXUTDirectionWidget::s_pEffect = NULL;
ID3DXMesh* CDXUTDirectionWidget::s_pMesh = NULL;
//--------------------------------------------------------------------------------------
CDXUTDirectionWidget::CDXUTDirectionWidget()
{
m_fRadius = 1.0f;
m_vDefaultDir = D3DXVECTOR3(0,1,0);
m_vCurrentDir = m_vDefaultDir;
m_nRotateMask = MOUSE_RIGHT_BUTTON;
D3DXMatrixIdentity( &m_mView );
D3DXMatrixIdentity( &m_mRot );
D3DXMatrixIdentity( &m_mRotSnapshot );
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDirectionWidget::StaticOnCreateDevice( IDirect3DDevice9* pd3dDevice )
{
HRESULT hr;
s_pd3dDevice = pd3dDevice;
const char* g_strBuffer =
"float4 g_MaterialDiffuseColor; // Material's diffuse color\r\n"
"float3 g_LightDir; // Light's direction in world space\r\n"
"float4x4 g_mWorld; // World matrix for object\r\n"
"float4x4 g_mWorldViewProjection; // World * View * Projection matrix\r\n"
"\r\n"
"struct VS_OUTPUT\r\n"
"{\r\n"
" float4 Position : POSITION; // vertex position\r\n"
" float4 Diffuse : COLOR0; // vertex diffuse color\r\n"
"};\r\n"
"\r\n"
"VS_OUTPUT RenderWith1LightNoTextureVS( float4 vPos : POSITION,\r\n"
" float3 vNormal : NORMAL )\r\n"
"{\r\n"
" VS_OUTPUT Output;\r\n"
"\r\n"
" // Transform the position from object space to homogeneous projection space\r\n"
" Output.Position = mul(vPos, g_mWorldViewProjection);\r\n"
"\r\n"
" // Transform the normal from object space to world space\r\n"
" float3 vNormalWorldSpace;\r\n"
" vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld)); // normal (world space)\r\n"
"\r\n"
" // Compute simple directional lighting equation\r\n"
" Output.Diffuse.rgb = g_MaterialDiffuseColor * max(0,dot(vNormalWorldSpace, g_LightDir));\r\n"
" Output.Diffuse.a = 1.0f;\r\n"
"\r\n"
" return Output;\r\n"
"}\r\n"
"\r\n"
"float4 RenderWith1LightNoTexturePS( float4 Diffuse : COLOR0 ) : COLOR0\r\n"
"{\r\n"
" return Diffuse;\r\n"
"}\r\n"
"\r\n"
"technique RenderWith1LightNoTexture\r\n"
"{\r\n"
" pass P0\r\n"
" {\r\n"
" VertexShader = compile vs_1_1 RenderWith1LightNoTextureVS();\r\n"
" PixelShader = compile ps_1_1 RenderWith1LightNoTexturePS();\r\n"
" }\r\n"
"}\r\n"
"";
UINT dwBufferSize = (UINT)strlen(g_strBuffer) + 1;
V_RETURN( D3DXCreateEffect( s_pd3dDevice, g_strBuffer, dwBufferSize, NULL, NULL, D3DXFX_NOT_CLONEABLE, NULL, &s_pEffect, NULL ) );
// Load the mesh with D3DX and get back a ID3DXMesh*. For this
// sample we'll ignore the X file's embedded materials since we know
// exactly the model we're loading. See the mesh samples such as
// "OptimizedMesh" for a more generic mesh loading example.
V_RETURN( DXUTCreateArrowMeshFromInternalArray( s_pd3dDevice, &s_pMesh ) );
// Optimize the mesh for this graphics card's vertex cache
// so when rendering the mesh's triangle list the vertices will
// cache hit more often so it won't have to re-execute the vertex shader
// on those vertices so it will improve perf.
DWORD* rgdwAdjacency = new DWORD[s_pMesh->GetNumFaces() * 3];
if( rgdwAdjacency == NULL )
return E_OUTOFMEMORY;
V( s_pMesh->GenerateAdjacency(1e-6f,rgdwAdjacency) );
V( s_pMesh->OptimizeInplace(D3DXMESHOPT_VERTEXCACHE, rgdwAdjacency, NULL, NULL, NULL) );
delete []rgdwAdjacency;
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDirectionWidget::OnResetDevice( const D3DSURFACE_DESC* pBackBufferSurfaceDesc )
{
m_ArcBall.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
return S_OK;
}
//--------------------------------------------------------------------------------------
void CDXUTDirectionWidget::StaticOnLostDevice()
{
if( s_pEffect )
s_pEffect->OnLostDevice();
}
//--------------------------------------------------------------------------------------
void CDXUTDirectionWidget::StaticOnDestroyDevice()
{
SAFE_RELEASE(s_pEffect);
SAFE_RELEASE(s_pMesh);
}
//--------------------------------------------------------------------------------------
LRESULT CDXUTDirectionWidget::HandleMessages( HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN:
{
if( ((m_nRotateMask & MOUSE_LEFT_BUTTON) != 0 && uMsg == WM_LBUTTONDOWN) ||
((m_nRotateMask & MOUSE_MIDDLE_BUTTON) != 0 && uMsg == WM_MBUTTONDOWN) ||
((m_nRotateMask & MOUSE_RIGHT_BUTTON) != 0 && uMsg == WM_RBUTTONDOWN) )
{
int iMouseX = (int)(short)LOWORD(lParam);
int iMouseY = (int)(short)HIWORD(lParam);
m_ArcBall.OnBegin( iMouseX, iMouseY );
SetCapture(hWnd);
}
return TRUE;
}
case WM_MOUSEMOVE:
{
if( m_ArcBall.IsBeingDragged() )
{
int iMouseX = (int)(short)LOWORD(lParam);
int iMouseY = (int)(short)HIWORD(lParam);
m_ArcBall.OnMove( iMouseX, iMouseY );
UpdateLightDir();
}
return TRUE;
}
case WM_LBUTTONUP:
case WM_MBUTTONUP:
case WM_RBUTTONUP:
{
if( ((m_nRotateMask & MOUSE_LEFT_BUTTON) != 0 && uMsg == WM_LBUTTONUP) ||
((m_nRotateMask & MOUSE_MIDDLE_BUTTON) != 0 && uMsg == WM_MBUTTONUP) ||
((m_nRotateMask & MOUSE_RIGHT_BUTTON) != 0 && uMsg == WM_RBUTTONUP) )
{
m_ArcBall.OnEnd();
ReleaseCapture();
}
UpdateLightDir();
return TRUE;
}
case WM_CAPTURECHANGED:
{
if( (HWND)lParam != hWnd )
{
if( (m_nRotateMask & MOUSE_LEFT_BUTTON) ||
(m_nRotateMask & MOUSE_MIDDLE_BUTTON) ||
(m_nRotateMask & MOUSE_RIGHT_BUTTON) )
{
m_ArcBall.OnEnd();
ReleaseCapture();
}
}
return TRUE;
}
}
return 0;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDirectionWidget::OnRender( D3DXCOLOR color, const D3DXMATRIX* pmView,
const D3DXMATRIX* pmProj, const D3DXVECTOR3* pEyePt )
{
m_mView = *pmView;
// Render the light spheres so the user can visually see the light dir
UINT iPass, cPasses;
D3DXMATRIX mRotate;
D3DXMATRIX mScale;
D3DXMATRIX mTrans;
D3DXMATRIXA16 mWorldViewProj;
HRESULT hr;
V( s_pEffect->SetTechnique( "RenderWith1LightNoTexture" ) );
V( s_pEffect->SetVector( "g_MaterialDiffuseColor", (D3DXVECTOR4*)&color ) );
D3DXVECTOR3 vEyePt;
D3DXVec3Normalize( &vEyePt, pEyePt );
V( s_pEffect->SetValue( "g_LightDir", &vEyePt, sizeof(D3DXVECTOR3) ) );
// Rotate arrow model to point towards origin
D3DXMATRIX mRotateA, mRotateB;
D3DXVECTOR3 vAt = D3DXVECTOR3(0,0,0);
D3DXVECTOR3 vUp = D3DXVECTOR3(0,1,0);
D3DXMatrixRotationX( &mRotateB, D3DX_PI );
D3DXMatrixLookAtLH( &mRotateA, &m_vCurrentDir, &vAt, &vUp );
D3DXMatrixInverse( &mRotateA, NULL, &mRotateA );
mRotate = mRotateB * mRotateA;
D3DXVECTOR3 vL = m_vCurrentDir * m_fRadius * 1.0f;
D3DXMatrixTranslation( &mTrans, vL.x, vL.y, vL.z );
D3DXMatrixScaling( &mScale, m_fRadius*0.2f, m_fRadius*0.2f, m_fRadius*0.2f );
D3DXMATRIX mWorld = mRotate * mScale * mTrans;
mWorldViewProj = mWorld * (m_mView) * (*pmProj);
V( s_pEffect->SetMatrix( "g_mWorldViewProjection", &mWorldViewProj ) );
V( s_pEffect->SetMatrix( "g_mWorld", &mWorld ) );
for( int iSubset=0; iSubset<2; iSubset++ )
{
V( s_pEffect->Begin(&cPasses, 0) );
for (iPass = 0; iPass < cPasses; iPass++)
{
V( s_pEffect->BeginPass(iPass) );
V( s_pMesh->DrawSubset(iSubset) );
V( s_pEffect->EndPass() );
}
V( s_pEffect->End() );
}
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDirectionWidget::UpdateLightDir()
{
D3DXMATRIX mInvView;
D3DXMatrixInverse(&mInvView, NULL, &m_mView);
mInvView._41 = mInvView._42 = mInvView._43 = 0;
D3DXMATRIX mLastRotInv;
D3DXMatrixInverse(&mLastRotInv, NULL, &m_mRotSnapshot);
D3DXMATRIX mRot = *m_ArcBall.GetRotationMatrix();
m_mRotSnapshot = mRot;
// Accumulate the delta of the arcball's rotation in view space.
// Note that per-frame delta rotations could be problematic over long periods of time.
m_mRot *= m_mView * mLastRotInv * mRot * mInvView;
// Since we're accumulating delta rotations, we need to orthonormalize
// the matrix to prevent eventual matrix skew
D3DXVECTOR3* pXBasis = (D3DXVECTOR3*) &m_mRot._11;
D3DXVECTOR3* pYBasis = (D3DXVECTOR3*) &m_mRot._21;
D3DXVECTOR3* pZBasis = (D3DXVECTOR3*) &m_mRot._31;
D3DXVec3Normalize( pXBasis, pXBasis );
D3DXVec3Cross( pYBasis, pZBasis, pXBasis );
D3DXVec3Normalize( pYBasis, pYBasis );
D3DXVec3Cross( pZBasis, pXBasis, pYBasis );
// Transform the default direction vector by the light's rotation matrix
D3DXVec3TransformNormal( &m_vCurrentDir, &m_vDefaultDir, &m_mRot );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Direct3D9 dynamic linking support -- calls top-level D3D9 APIs with graceful
// failure if APIs are not present.
//--------------------------------------------------------------------------------------
// Function prototypes
typedef IDirect3D9* (WINAPI * LPDIRECT3DCREATE9) (UINT);
typedef HRESULT (WINAPI * LPDIRECT3DCREATE9EX) (UINT, IDirect3D9Ex**); // JJ - added D3D9Ex
typedef INT (WINAPI * LPD3DPERF_BEGINEVENT)(D3DCOLOR, LPCWSTR);
typedef INT (WINAPI * LPD3DPERF_ENDEVENT)(void);
typedef VOID (WINAPI * LPD3DPERF_SETMARKER)(D3DCOLOR, LPCWSTR);
typedef VOID (WINAPI * LPD3DPERF_SETREGION)(D3DCOLOR, LPCWSTR);
typedef BOOL (WINAPI * LPD3DPERF_QUERYREPEATFRAME)(void);
typedef VOID (WINAPI * LPD3DPERF_SETOPTIONS)( DWORD dwOptions );
typedef DWORD (WINAPI * LPD3DPERF_GETSTATUS)( void );
// Module and function pointers
static HMODULE s_hModD3D9 = NULL;
static LPDIRECT3DCREATE9 s_DynamicDirect3DCreate9 = NULL;
static LPDIRECT3DCREATE9EX s_DynamicDirect3DCreate9Ex = NULL; // JJ - added D3D9Ex
static LPD3DPERF_BEGINEVENT s_DynamicD3DPERF_BeginEvent = NULL;
static LPD3DPERF_ENDEVENT s_DynamicD3DPERF_EndEvent = NULL;
static LPD3DPERF_SETMARKER s_DynamicD3DPERF_SetMarker = NULL;
static LPD3DPERF_SETREGION s_DynamicD3DPERF_SetRegion = NULL;
static LPD3DPERF_QUERYREPEATFRAME s_DynamicD3DPERF_QueryRepeatFrame = NULL;
static LPD3DPERF_SETOPTIONS s_DynamicD3DPERF_SetOptions = NULL;
static LPD3DPERF_GETSTATUS s_DynamicD3DPERF_GetStatus = NULL;
// Ensure function pointers are initialized
static bool DXUT_EnsureD3DAPIs( void )
{
// If module is non-NULL, this function has already been called. Note
// that this doesn't guarantee that all D3D9 procaddresses were found.
if( s_hModD3D9 != NULL )
return true;
// This may fail if DirectX 9 isn't installed
WCHAR wszPath[MAX_PATH+1];
if( !::GetSystemDirectory( wszPath, MAX_PATH+1 ) )
return false;
StringCchCat( wszPath, MAX_PATH, L"\\d3d9.dll" );
s_hModD3D9 = LoadLibrary( wszPath );
if( s_hModD3D9 == NULL )
return false;
s_DynamicDirect3DCreate9 = (LPDIRECT3DCREATE9)GetProcAddress( s_hModD3D9, "Direct3DCreate9" );
s_DynamicDirect3DCreate9Ex = (LPDIRECT3DCREATE9EX)GetProcAddress( s_hModD3D9, "Direct3DCreate9Ex" ); // JJ - added D3D9Ex
s_DynamicD3DPERF_BeginEvent = (LPD3DPERF_BEGINEVENT)GetProcAddress( s_hModD3D9, "D3DPERF_BeginEvent" );
s_DynamicD3DPERF_EndEvent = (LPD3DPERF_ENDEVENT)GetProcAddress( s_hModD3D9, "D3DPERF_EndEvent" );
s_DynamicD3DPERF_SetMarker = (LPD3DPERF_SETMARKER)GetProcAddress( s_hModD3D9, "D3DPERF_SetMarker" );
s_DynamicD3DPERF_SetRegion = (LPD3DPERF_SETREGION)GetProcAddress( s_hModD3D9, "D3DPERF_SetRegion" );
s_DynamicD3DPERF_QueryRepeatFrame = (LPD3DPERF_QUERYREPEATFRAME)GetProcAddress( s_hModD3D9, "D3DPERF_QueryRepeatFrame" );
s_DynamicD3DPERF_SetOptions = (LPD3DPERF_SETOPTIONS)GetProcAddress( s_hModD3D9, "D3DPERF_SetOptions" );
s_DynamicD3DPERF_GetStatus = (LPD3DPERF_GETSTATUS)GetProcAddress( s_hModD3D9, "D3DPERF_GetStatus" );
return true;
}
IDirect3D9 * WINAPI DXUT_Dynamic_Direct3DCreate9(UINT SDKVersion)
{
if( DXUT_EnsureD3DAPIs() && s_DynamicDirect3DCreate9 != NULL )
return s_DynamicDirect3DCreate9( SDKVersion );
else
return NULL;
}
// JJ - added D3D9Ex...
IDirect3D9Ex * WINAPI DXUT_Dynamic_Direct3DCreate9Ex(UINT SDKVersion)
{
if( DXUT_EnsureD3DAPIs() && s_DynamicDirect3DCreate9Ex != NULL )
{
IDirect3D9Ex* pResult = NULL;
HRESULT hr = s_DynamicDirect3DCreate9Ex(SDKVersion, &pResult);
if(SUCCEEDED(hr))
return pResult;
else
return NULL;
}
else
return NULL;
}
// ...JJ - added D3D9Ex
int WINAPI DXUT_Dynamic_D3DPERF_BeginEvent( D3DCOLOR col, LPCWSTR wszName )
{
if( DXUT_EnsureD3DAPIs() && s_DynamicD3DPERF_BeginEvent != NULL )
return s_DynamicD3DPERF_BeginEvent( col, wszName );
else
return -1;
}
int WINAPI DXUT_Dynamic_D3DPERF_EndEvent( void )
{
if( DXUT_EnsureD3DAPIs() && s_DynamicD3DPERF_EndEvent != NULL )
return s_DynamicD3DPERF_EndEvent();
else
return -1;
}
void WINAPI DXUT_Dynamic_D3DPERF_SetMarker( D3DCOLOR col, LPCWSTR wszName )
{
if( DXUT_EnsureD3DAPIs() && s_DynamicD3DPERF_SetMarker != NULL )
s_DynamicD3DPERF_SetMarker( col, wszName );
}
void WINAPI DXUT_Dynamic_D3DPERF_SetRegion( D3DCOLOR col, LPCWSTR wszName )
{
if( DXUT_EnsureD3DAPIs() && s_DynamicD3DPERF_SetRegion != NULL )
s_DynamicD3DPERF_SetRegion( col, wszName );
}
BOOL WINAPI DXUT_Dynamic_D3DPERF_QueryRepeatFrame( void )
{
if( DXUT_EnsureD3DAPIs() && s_DynamicD3DPERF_QueryRepeatFrame != NULL )
return s_DynamicD3DPERF_QueryRepeatFrame();
else
return FALSE;
}
void WINAPI DXUT_Dynamic_D3DPERF_SetOptions( DWORD dwOptions )
{
if( DXUT_EnsureD3DAPIs() && s_DynamicD3DPERF_SetOptions != NULL )
s_DynamicD3DPERF_SetOptions( dwOptions );
}
DWORD WINAPI DXUT_Dynamic_D3DPERF_GetStatus( void )
{
if( DXUT_EnsureD3DAPIs() && s_DynamicD3DPERF_GetStatus != NULL )
return s_DynamicD3DPERF_GetStatus();
else
return 0;
}
//--------------------------------------------------------------------------------------
// Trace a string description of a decl
//--------------------------------------------------------------------------------------
void DXUTTraceDecl( D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE] )
{
int iDecl=0;
for( iDecl=0; iDecl<MAX_FVF_DECL_SIZE; iDecl++ )
{
if( decl[iDecl].Stream == 0xFF )
break;
DXUTOutputDebugString( L"decl[%d]=Stream:%d, Offset:%d, %s, %s, %s, UsageIndex:%d\n", iDecl,
decl[iDecl].Stream,
decl[iDecl].Offset,
DXUTTraceD3DDECLTYPEtoString( decl[iDecl].Type ),
DXUTTraceD3DDECLMETHODtoString( decl[iDecl].Method ),
DXUTTraceD3DDECLUSAGEtoString( decl[iDecl].Usage ),
decl[iDecl].UsageIndex );
}
DXUTOutputDebugString( L"decl[%d]=D3DDECL_END\n", iDecl );
}
//--------------------------------------------------------------------------------------
WCHAR* DXUTTraceD3DDECLTYPEtoString( BYTE t )
{
switch( t )
{
case D3DDECLTYPE_FLOAT1: return L"D3DDECLTYPE_FLOAT1";
case D3DDECLTYPE_FLOAT2: return L"D3DDECLTYPE_FLOAT2";
case D3DDECLTYPE_FLOAT3: return L"D3DDECLTYPE_FLOAT3";
case D3DDECLTYPE_FLOAT4: return L"D3DDECLTYPE_FLOAT4";
case D3DDECLTYPE_D3DCOLOR: return L"D3DDECLTYPE_D3DCOLOR";
case D3DDECLTYPE_UBYTE4: return L"D3DDECLTYPE_UBYTE4";
case D3DDECLTYPE_SHORT2: return L"D3DDECLTYPE_SHORT2";
case D3DDECLTYPE_SHORT4: return L"D3DDECLTYPE_SHORT4";
case D3DDECLTYPE_UBYTE4N: return L"D3DDECLTYPE_UBYTE4N";
case D3DDECLTYPE_SHORT2N: return L"D3DDECLTYPE_SHORT2N";
case D3DDECLTYPE_SHORT4N: return L"D3DDECLTYPE_SHORT4N";
case D3DDECLTYPE_USHORT2N: return L"D3DDECLTYPE_USHORT2N";
case D3DDECLTYPE_USHORT4N: return L"D3DDECLTYPE_USHORT4N";
case D3DDECLTYPE_UDEC3: return L"D3DDECLTYPE_UDEC3";
case D3DDECLTYPE_DEC3N: return L"D3DDECLTYPE_DEC3N";
case D3DDECLTYPE_FLOAT16_2: return L"D3DDECLTYPE_FLOAT16_2";
case D3DDECLTYPE_FLOAT16_4: return L"D3DDECLTYPE_FLOAT16_4";
case D3DDECLTYPE_UNUSED: return L"D3DDECLTYPE_UNUSED";
default: return L"D3DDECLTYPE Unknown";
}
}
WCHAR* DXUTTraceD3DDECLMETHODtoString( BYTE m )
{
switch( m )
{
case D3DDECLMETHOD_DEFAULT: return L"D3DDECLMETHOD_DEFAULT";
case D3DDECLMETHOD_PARTIALU: return L"D3DDECLMETHOD_PARTIALU";
case D3DDECLMETHOD_PARTIALV: return L"D3DDECLMETHOD_PARTIALV";
case D3DDECLMETHOD_CROSSUV: return L"D3DDECLMETHOD_CROSSUV";
case D3DDECLMETHOD_UV: return L"D3DDECLMETHOD_UV";
case D3DDECLMETHOD_LOOKUP: return L"D3DDECLMETHOD_LOOKUP";
case D3DDECLMETHOD_LOOKUPPRESAMPLED: return L"D3DDECLMETHOD_LOOKUPPRESAMPLED";
default: return L"D3DDECLMETHOD Unknown";
}
}
WCHAR* DXUTTraceD3DDECLUSAGEtoString( BYTE u )
{
switch( u )
{
case D3DDECLUSAGE_POSITION: return L"D3DDECLUSAGE_POSITION";
case D3DDECLUSAGE_BLENDWEIGHT: return L"D3DDECLUSAGE_BLENDWEIGHT";
case D3DDECLUSAGE_BLENDINDICES: return L"D3DDECLUSAGE_BLENDINDICES";
case D3DDECLUSAGE_NORMAL: return L"D3DDECLUSAGE_NORMAL";
case D3DDECLUSAGE_PSIZE: return L"D3DDECLUSAGE_PSIZE";
case D3DDECLUSAGE_TEXCOORD: return L"D3DDECLUSAGE_TEXCOORD";
case D3DDECLUSAGE_TANGENT: return L"D3DDECLUSAGE_TANGENT";
case D3DDECLUSAGE_BINORMAL: return L"D3DDECLUSAGE_BINORMAL";
case D3DDECLUSAGE_TESSFACTOR: return L"D3DDECLUSAGE_TESSFACTOR";
case D3DDECLUSAGE_POSITIONT: return L"D3DDECLUSAGE_POSITIONT";
case D3DDECLUSAGE_COLOR: return L"D3DDECLUSAGE_COLOR";
case D3DDECLUSAGE_FOG: return L"D3DDECLUSAGE_FOG";
case D3DDECLUSAGE_DEPTH: return L"D3DDECLUSAGE_DEPTH";
case D3DDECLUSAGE_SAMPLE: return L"D3DDECLUSAGE_SAMPLE";
default: return L"D3DDECLUSAGE Unknown";
}
}
//--------------------------------------------------------------------------------------
// Multimon API handling for OSes with or without multimon API support
//--------------------------------------------------------------------------------------
#define DXUT_PRIMARY_MONITOR ((HMONITOR)0x12340042)
typedef HMONITOR (WINAPI* LPMONITORFROMWINDOW)(HWND, DWORD);
typedef BOOL (WINAPI* LPGETMONITORINFO)(HMONITOR, LPMONITORINFO);
BOOL DXUTGetMonitorInfo(HMONITOR hMonitor, LPMONITORINFO lpMonitorInfo)
{
static bool s_bInited = false;
static LPGETMONITORINFO s_pFnGetMonitorInfo = NULL;
if( !s_bInited )
{
s_bInited = true;
HMODULE hUser32 = GetModuleHandle( L"USER32" );
if (hUser32 )
{
OSVERSIONINFOA osvi = {0}; osvi.dwOSVersionInfoSize = sizeof(osvi); GetVersionExA((OSVERSIONINFOA*)&osvi);
bool bNT = (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId);
s_pFnGetMonitorInfo = (LPGETMONITORINFO) (bNT ? GetProcAddress(hUser32,"GetMonitorInfoW") : GetProcAddress(hUser32,"GetMonitorInfoA"));
}
}
if( s_pFnGetMonitorInfo )
return s_pFnGetMonitorInfo(hMonitor, lpMonitorInfo);
RECT rcWork;
if ((hMonitor == DXUT_PRIMARY_MONITOR) && lpMonitorInfo && (lpMonitorInfo->cbSize >= sizeof(MONITORINFO)) && SystemParametersInfoA(SPI_GETWORKAREA, 0, &rcWork, 0))
{
lpMonitorInfo->rcMonitor.left = 0;
lpMonitorInfo->rcMonitor.top = 0;
lpMonitorInfo->rcMonitor.right = GetSystemMetrics(SM_CXSCREEN);
lpMonitorInfo->rcMonitor.bottom = GetSystemMetrics(SM_CYSCREEN);
lpMonitorInfo->rcWork = rcWork;
lpMonitorInfo->dwFlags = MONITORINFOF_PRIMARY;
return TRUE;
}
return FALSE;
}
HMONITOR DXUTMonitorFromWindow(HWND hWnd, DWORD dwFlags)
{
static bool s_bInited = false;
static LPMONITORFROMWINDOW s_pFnGetMonitorFronWindow = NULL;
if( !s_bInited )
{
s_bInited = true;
HMODULE hUser32 = GetModuleHandle( L"USER32" );
if (hUser32 ) s_pFnGetMonitorFronWindow = (LPMONITORFROMWINDOW) GetProcAddress(hUser32,"MonitorFromWindow");
}
if( s_pFnGetMonitorFronWindow )
return s_pFnGetMonitorFronWindow(hWnd, dwFlags);
if (dwFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST))
return DXUT_PRIMARY_MONITOR;
return NULL;
}
//--------------------------------------------------------------------------------------
// Get the desktop resolution of an adapter. This isn't the same as the current resolution
// from GetAdapterDisplayMode since the device might be fullscreen
//--------------------------------------------------------------------------------------
void DXUTGetDesktopResolution( UINT AdapterOrdinal, UINT* pWidth, UINT* pHeight )
{
CD3DEnumeration* pd3dEnum = DXUTGetEnumeration();
CD3DEnumAdapterInfo* pAdapterInfo = pd3dEnum->GetAdapterInfo( AdapterOrdinal );
DEVMODE devMode;
ZeroMemory( &devMode, sizeof(DEVMODE) );
devMode.dmSize = sizeof(DEVMODE);
WCHAR strDeviceName[256];
MultiByteToWideChar( CP_ACP, 0, pAdapterInfo->AdapterIdentifier.DeviceName, -1, strDeviceName, 256 );
strDeviceName[255] = 0;
EnumDisplaySettings( strDeviceName, ENUM_REGISTRY_SETTINGS, &devMode );
if( pWidth )
*pWidth = devMode.dmPelsWidth;
if( pHeight )
*pHeight = devMode.dmPelsHeight;
}
//--------------------------------------------------------------------------------------
IDirect3DDevice9* DXUTCreateRefDevice( HWND hWnd, bool bNullRef )
{
HRESULT hr;
IDirect3D9* pD3D = DXUT_Dynamic_Direct3DCreate9( D3D_SDK_VERSION );
if( NULL == pD3D )
return NULL;
D3DDISPLAYMODE Mode;
pD3D->GetAdapterDisplayMode(0, &Mode);
D3DPRESENT_PARAMETERS pp;
ZeroMemory( &pp, sizeof(D3DPRESENT_PARAMETERS) );
pp.BackBufferWidth = 1;
pp.BackBufferHeight = 1;
pp.BackBufferFormat = Mode.Format;
pp.BackBufferCount = 1;
pp.SwapEffect = D3DSWAPEFFECT_COPY;
pp.Windowed = TRUE;
pp.hDeviceWindow = hWnd;
IDirect3DDevice9* pd3dDevice = NULL;
hr = pD3D->CreateDevice( D3DADAPTER_DEFAULT, bNullRef ? D3DDEVTYPE_NULLREF : D3DDEVTYPE_REF,
hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pp, &pd3dDevice );
SAFE_RELEASE( pD3D );
return pd3dDevice;
}
typedef DWORD (WINAPI* LPXINPUTGETSTATE)(DWORD dwUserIndex, XINPUT_STATE* pState );
typedef DWORD (WINAPI* LPXINPUTSETSTATE)(DWORD dwUserIndex, XINPUT_VIBRATION* pVibration );
typedef DWORD (WINAPI* LPXINPUTGETCAPABILITIES)( DWORD dwUserIndex, DWORD dwFlags, XINPUT_CAPABILITIES* pCapabilities );
typedef void (WINAPI* LPXINPUTENABLE)(BOOL bEnable);
//--------------------------------------------------------------------------------------
// Does extra processing on XInput data to make it slightly more convenient to use
//--------------------------------------------------------------------------------------
HRESULT DXUTGetGamepadState( DWORD dwPort, DXUT_GAMEPAD* pGamePad, bool bThumbstickDeadZone, bool bSnapThumbstickToCardinals )
{
if( dwPort >= DXUT_MAX_CONTROLLERS || pGamePad == NULL )
return E_FAIL;
static LPXINPUTGETSTATE s_pXInputGetState = NULL;
static LPXINPUTGETCAPABILITIES s_pXInputGetCapabilities = NULL;
if( NULL == s_pXInputGetState || NULL == s_pXInputGetCapabilities )
{
WCHAR wszPath[MAX_PATH];
if( GetSystemDirectory( wszPath, MAX_PATH ) )
{
StringCchCat( wszPath, MAX_PATH, L"\\" );
StringCchCat( wszPath, MAX_PATH, XINPUT_DLL );
HINSTANCE hInst = LoadLibrary( wszPath );
if( hInst )
{
s_pXInputGetState = (LPXINPUTGETSTATE)GetProcAddress( hInst, "XInputGetState" );
s_pXInputGetCapabilities = (LPXINPUTGETCAPABILITIES)GetProcAddress( hInst, "XInputGetCapabilities" );
}
}
}
if( s_pXInputGetState == NULL )
return E_FAIL;
XINPUT_STATE InputState;
DWORD dwResult = s_pXInputGetState( dwPort, &InputState );
// Track insertion and removals
BOOL bWasConnected = pGamePad->bConnected;
pGamePad->bConnected = (dwResult == ERROR_SUCCESS);
pGamePad->bRemoved = ( bWasConnected && !pGamePad->bConnected );
pGamePad->bInserted = ( !bWasConnected && pGamePad->bConnected );
// Don't update rest of the state if not connected
if( !pGamePad->bConnected )
return S_OK;
// Store the capabilities of the device
if( pGamePad->bInserted )
{
ZeroMemory( pGamePad, sizeof(DXUT_GAMEPAD) );
pGamePad->bConnected = true;
pGamePad->bInserted = true;
if( s_pXInputGetCapabilities )
s_pXInputGetCapabilities( dwPort, XINPUT_DEVTYPE_GAMEPAD, &pGamePad->caps );
}
// Copy gamepad to local structure (assumes that XINPUT_GAMEPAD at the front in CONTROLER_STATE)
memcpy( pGamePad, &InputState.Gamepad, sizeof(XINPUT_GAMEPAD) );
if( bSnapThumbstickToCardinals )
{
// Apply deadzone to each axis independantly to slightly snap to up/down/left/right
if( pGamePad->sThumbLX < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && pGamePad->sThumbLX > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE )
pGamePad->sThumbLX = 0;
if( pGamePad->sThumbLY < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && pGamePad->sThumbLY > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE )
pGamePad->sThumbLY = 0;
if( pGamePad->sThumbRX < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && pGamePad->sThumbRX > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE )
pGamePad->sThumbRX = 0;
if( pGamePad->sThumbRY < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && pGamePad->sThumbRY > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE )
pGamePad->sThumbRY = 0;
}
else if( bThumbstickDeadZone )
{
// Apply deadzone if centered
if( (pGamePad->sThumbLX < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && pGamePad->sThumbLX > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) &&
(pGamePad->sThumbLY < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && pGamePad->sThumbLY > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) )
{
pGamePad->sThumbLX = 0;
pGamePad->sThumbLY = 0;
}
if( (pGamePad->sThumbRX < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && pGamePad->sThumbRX > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) &&
(pGamePad->sThumbRY < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && pGamePad->sThumbRY > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) )
{
pGamePad->sThumbRX = 0;
pGamePad->sThumbRY = 0;
}
}
// Convert [-1,+1] range
pGamePad->fThumbLX = pGamePad->sThumbLX / 32767.0f;
pGamePad->fThumbLY = pGamePad->sThumbLY / 32767.0f;
pGamePad->fThumbRX = pGamePad->sThumbRX / 32767.0f;
pGamePad->fThumbRY = pGamePad->sThumbRY / 32767.0f;
// Get the boolean buttons that have been pressed since the last call.
// Each button is represented by one bit.
pGamePad->wPressedButtons = ( pGamePad->wLastButtons ^ pGamePad->wButtons ) & pGamePad->wButtons;
pGamePad->wLastButtons = pGamePad->wButtons;
// Figure out if the left trigger has been pressed or released
bool bPressed = ( pGamePad->bLeftTrigger > DXUT_GAMEPAD_TRIGGER_THRESHOLD );
pGamePad->bPressedLeftTrigger = ( bPressed ) ? !pGamePad->bLastLeftTrigger : false;
pGamePad->bLastLeftTrigger = bPressed;
// Figure out if the right trigger has been pressed or released
bPressed = ( pGamePad->bRightTrigger > DXUT_GAMEPAD_TRIGGER_THRESHOLD );
pGamePad->bPressedRightTrigger = ( bPressed ) ? !pGamePad->bLastRightTrigger : false;
pGamePad->bLastRightTrigger = bPressed;
return S_OK;
}
//--------------------------------------------------------------------------------------
// Don't pause the game or deactive the window without first stopping rumble otherwise
// the controller will continue to rumble
//--------------------------------------------------------------------------------------
void DXUTEnableXInput( bool bEnable )
{
static LPXINPUTENABLE s_pXInputEnable = NULL;
if( NULL == s_pXInputEnable )
{
WCHAR wszPath[MAX_PATH];
if( GetSystemDirectory( wszPath, MAX_PATH ) )
{
StringCchCat( wszPath, MAX_PATH, L"\\" );
StringCchCat( wszPath, MAX_PATH, XINPUT_DLL );
HINSTANCE hInst = LoadLibrary( wszPath );
if( hInst )
s_pXInputEnable = (LPXINPUTENABLE)GetProcAddress( hInst, "XInputEnable" );
}
}
if( s_pXInputEnable )
s_pXInputEnable( bEnable );
}
//--------------------------------------------------------------------------------------
// Don't pause the game or deactive the window without first stopping rumble otherwise
// the controller will continue to rumble
//--------------------------------------------------------------------------------------
HRESULT DXUTStopRumbleOnAllControllers()
{
static LPXINPUTSETSTATE s_pXInputSetState = NULL;
if( NULL == s_pXInputSetState )
{
WCHAR wszPath[MAX_PATH];
if( GetSystemDirectory( wszPath, MAX_PATH ) )
{
StringCchCat( wszPath, MAX_PATH, L"\\" );
StringCchCat( wszPath, MAX_PATH, XINPUT_DLL );
HINSTANCE hInst = LoadLibrary( wszPath );
if( hInst )
s_pXInputSetState = (LPXINPUTSETSTATE)GetProcAddress( hInst, "XInputSetState" );
}
}
if( s_pXInputSetState == NULL )
return E_FAIL;
XINPUT_VIBRATION vibration;
vibration.wLeftMotorSpeed = 0;
vibration.wRightMotorSpeed = 0;
for( int iUserIndex=0; iUserIndex<DXUT_MAX_CONTROLLERS; iUserIndex++ )
s_pXInputSetState( iUserIndex, &vibration );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Helper function to launch the Media Center UI after the program terminates
//--------------------------------------------------------------------------------------
bool DXUTReLaunchMediaCenter()
{
// Skip if not running on a Media Center
if( GetSystemMetrics( 87 ) == 0 ) // SM_MEDIACENTER == 87, but is only defined if _WIN32_WINNT >= 0x0501
return false;
// Get the path to Media Center
WCHAR szExpandedPath[MAX_PATH];
if( !ExpandEnvironmentStrings( L"%SystemRoot%\\ehome\\ehshell.exe", szExpandedPath, MAX_PATH) )
return false;
// Skip if ehshell.exe doesn't exist
if( GetFileAttributes( szExpandedPath ) == 0xFFFFFFFF )
return false;
// Launch ehshell.exe
INT_PTR result = (INT_PTR)ShellExecute( NULL, TEXT("open"), szExpandedPath, NULL, NULL, SW_SHOWNORMAL);
return (result > 32);
}
|