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

#ifndef IMESH_H
#define IMESH_H

#ifdef _WIN32
#pragma once
#endif

#include "tier1/interface.h"
#include "materialsystem/imaterial.h"
#include <float.h>
#include <string.h>
#include "tier0/dbg.h"
#include "tier2/meshutils.h"
#include "mathlib/mathlib.h"

#if defined( DX_TO_GL_ABSTRACTION )
// Swap these so that we do color swapping on 10.6.2, which doesn't have EXT_vertex_array_bgra
#define	OPENGL_SWAP_COLORS
#endif

//-----------------------------------------------------------------------------
// forward declarations
//-----------------------------------------------------------------------------
class IMaterial;
class CMeshBuilder;
class IMaterialVar;
typedef uint64 VertexFormat_t;


//-----------------------------------------------------------------------------
// Define this to find write-combine problems
//-----------------------------------------------------------------------------
#ifdef _DEBUG
//#ifndef DEBUG_WRITE_COMBINE
//#define DEBUG_WRITE_COMBINE 1
//#endif
#endif


//-----------------------------------------------------------------------------
// The Vertex Buffer interface
//-----------------------------------------------------------------------------
enum
{
	VERTEX_MAX_TEXTURE_COORDINATES = 8,
	BONE_MATRIX_INDEX_INVALID = 255
};

// Internal maximums for sizes. Don't use directly, use IMaterialSystem::GetMaxToRender()
enum
{
	INDEX_BUFFER_SIZE  = 32768,
	DYNAMIC_VERTEX_BUFFER_MEMORY = ( 1024 + 512 ) * 1024,
	DYNAMIC_VERTEX_BUFFER_MEMORY_SMALL = 384 * 1024, // Only allocate this much during map transitions
};

// Vertex fields must be written in well-defined order to achieve write combining, 
// which is a perf booster
enum WriteCombineOrdering_t
{
	MB_FIELD_NONE = -1,
	MB_FIELD_POSITION = 0,
	MB_FIELD_BONE_WEIGHTS,
	MB_FIELD_BONE_INDEX,
	MB_FIELD_NORMAL,
	MB_FIELD_COLOR,
	MB_FIELD_SPECULAR,
	MB_FIELD_TEXCOORD_FIRST,
	MB_FIELD_TEXCOORD_LAST = MB_FIELD_TEXCOORD_FIRST + VERTEX_MAX_TEXTURE_COORDINATES - 1,
	MB_FIELD_TANGENT_S,
	MB_FIELD_TANGENT_T,
	MB_FIELD_USERDATA,
};

#define MB_FIELD_TEXCOORD( nStage ) ( MB_FIELD_TEXCOORD_FIRST + ( nStage ) )

struct VertexDesc_t
{
	// These can be set to zero if there are pointers to dummy buffers, when the
	// actual buffer format doesn't contain the data but it needs to be safe to
	// use all the CMeshBuilder functions.
	int	m_VertexSize_Position;
	int m_VertexSize_BoneWeight;
	int m_VertexSize_BoneMatrixIndex;
	int	m_VertexSize_Normal;
	int	m_VertexSize_Color;
	int	m_VertexSize_Specular;
	int m_VertexSize_TexCoord[VERTEX_MAX_TEXTURE_COORDINATES];
	int m_VertexSize_TangentS;
	int m_VertexSize_TangentT;
	int m_VertexSize_Wrinkle;

	int m_VertexSize_UserData;

	int m_ActualVertexSize;	// Size of the vertices.. Some of the m_VertexSize_ elements above
							// are set to this value and some are set to zero depending on which
							// fields exist in a buffer's vertex format.

	// The type of compression applied to this vertex data
	VertexCompressionType_t m_CompressionType;

	// Number of bone weights per vertex...
	int m_NumBoneWeights;

	// Pointers to our current vertex data
	float			*m_pPosition;

	float			*m_pBoneWeight;

#ifndef NEW_SKINNING
	unsigned char	*m_pBoneMatrixIndex;
#else
	float			*m_pBoneMatrixIndex;
#endif

	float			*m_pNormal;

	unsigned char	*m_pColor;
	unsigned char	*m_pSpecular;
	float			*m_pTexCoord[VERTEX_MAX_TEXTURE_COORDINATES];

	// Tangent space *associated with one particular set of texcoords*
	float			*m_pTangentS;
	float			*m_pTangentT;

	float			*m_pWrinkle;

	// user data
	float			*m_pUserData;

	// The first vertex index (used for buffered vertex buffers, or cards that don't support stream offset)
	int	m_nFirstVertex;

	// The offset in bytes of the memory we're writing into 
	// from the start of the D3D buffer (will be 0 for static meshes)
	unsigned int	m_nOffset;

#ifdef DEBUG_WRITE_COMBINE
	int m_nLastWrittenField;
	unsigned char* m_pLastWrittenAddress;
#endif
};

struct IndexDesc_t
{
	// Pointers to the index data
	unsigned short	*m_pIndices;

	// The offset in bytes of the memory we're writing into 
	// from the start of the D3D buffer (will be 0 for static meshes)
	unsigned int	m_nOffset;

	// The first index (used for buffered index buffers, or cards that don't support stream offset)
	unsigned int	m_nFirstIndex;

	// 1 if the device is active, 0 if the device isn't active.
	// Faster than doing if checks for null m_pIndices if someone is
	// trying to write the m_pIndices while the device is inactive.
	unsigned char m_nIndexSize;
};


//-----------------------------------------------------------------------------
// The Mesh memory descriptor
//-----------------------------------------------------------------------------
struct MeshDesc_t : public VertexDesc_t, public IndexDesc_t
{
};


//-----------------------------------------------------------------------------
// Standard vertex formats for models
//-----------------------------------------------------------------------------
struct ModelVertexDX7_t
{
	Vector			m_vecPosition;
	Vector2D		m_flBoneWeights;
	unsigned int	m_nBoneIndices;
	Vector			m_vecNormal;
	unsigned int	m_nColor;	// ARGB
	Vector2D		m_vecTexCoord;
};

struct ModelVertexDX8_t	: public ModelVertexDX7_t
{
	Vector4D		m_vecUserData;
};


//-----------------------------------------------------------------------------
// Utility methods for buffer builders
//-----------------------------------------------------------------------------
inline float *OffsetFloatPointer( float *pBufferPointer, int nVertexCount, int vertexSize )
{
	return reinterpret_cast<float *>(
		reinterpret_cast<unsigned char *>(pBufferPointer) + 
		nVertexCount * vertexSize);
}

inline const float *OffsetFloatPointer( const float *pBufferPointer, int nVertexCount, int vertexSize )
{
	return reinterpret_cast<const float*>(
		reinterpret_cast<unsigned char const*>(pBufferPointer) + 
		nVertexCount * vertexSize);
}

inline void IncrementFloatPointer( float* &pBufferPointer, int vertexSize )
{
	pBufferPointer = reinterpret_cast<float*>( reinterpret_cast<unsigned char*>( pBufferPointer ) + vertexSize );
}


//-----------------------------------------------------------------------------
// Used in lists of indexed primitives.
//-----------------------------------------------------------------------------
class CPrimList
{
public:
	CPrimList();
	CPrimList( int nFirstIndex, int nIndexCount );

	int			m_FirstIndex;
	int			m_NumIndices;
};

inline CPrimList::CPrimList()
{
}

inline CPrimList::CPrimList( int nFirstIndex, int nIndexCount )
{
	m_FirstIndex = nFirstIndex;
	m_NumIndices = nIndexCount;
}

abstract_class IVertexBuffer
{
public:
	// Add a virtual destructor to silence the clang warning.
	// This is harmless but not important since the only derived class
	// doesn't have a destructor.
	virtual ~IVertexBuffer() {}

	// NOTE: The following two methods are only valid for static vertex buffers
	// Returns the number of vertices and the format of the vertex buffer
	virtual int VertexCount() const = 0;
	virtual VertexFormat_t GetVertexFormat() const = 0;

	// Is this vertex buffer dynamic?
	virtual bool IsDynamic() const = 0;

	// NOTE: For dynamic vertex buffers only!
	// Casts the memory of the dynamic vertex buffer to the appropriate type
	virtual void BeginCastBuffer( VertexFormat_t format ) = 0;
	virtual void EndCastBuffer() = 0;

	// Returns the number of vertices that can still be written into the buffer
	virtual int GetRoomRemaining() const = 0;

	virtual bool Lock( int nVertexCount, bool bAppend, VertexDesc_t &desc ) = 0;
	virtual void Unlock( int nVertexCount, VertexDesc_t &desc ) = 0;

	// Spews the mesh data
	virtual void Spew( int nVertexCount, const VertexDesc_t &desc ) = 0;

	// Call this in debug mode to make sure our data is good.
	virtual void ValidateData( int nVertexCount, const VertexDesc_t & desc ) = 0;
};

abstract_class IIndexBuffer
{
public:
	// Add a virtual destructor to silence the clang warning.
	// This is harmless but not important since the only derived class
	// doesn't have a destructor.
	virtual ~IIndexBuffer() {}

	// NOTE: The following two methods are only valid for static index buffers
	// Returns the number of indices and the format of the index buffer
	virtual int IndexCount() const = 0;
	virtual MaterialIndexFormat_t IndexFormat() const = 0;

	// Is this index buffer dynamic?
	virtual bool IsDynamic() const = 0;

	// NOTE: For dynamic index buffers only!
	// Casts the memory of the dynamic index buffer to the appropriate type
	virtual void BeginCastBuffer( MaterialIndexFormat_t format ) = 0;
	virtual void EndCastBuffer() = 0;

	// Returns the number of indices that can still be written into the buffer
	virtual int GetRoomRemaining() const = 0;

	// Locks, unlocks the index buffer
	virtual bool Lock( int nMaxIndexCount, bool bAppend, IndexDesc_t &desc ) = 0;
	virtual void Unlock( int nWrittenIndexCount, IndexDesc_t &desc ) = 0;

	// FIXME: Remove this!!
	// Locks, unlocks the index buffer for modify
	virtual void ModifyBegin( bool bReadOnly, int nFirstIndex, int nIndexCount, IndexDesc_t& desc ) = 0;
	virtual void ModifyEnd( IndexDesc_t& desc ) = 0;

	// Spews the mesh data
	virtual void Spew( int nIndexCount, const IndexDesc_t &desc ) = 0;

	// Ensures the data in the index buffer is valid
	virtual void ValidateData( int nIndexCount, const IndexDesc_t &desc ) = 0;
};


//-----------------------------------------------------------------------------
// Interface to the mesh - needs to contain an IVertexBuffer and an IIndexBuffer to emulate old mesh behavior
//-----------------------------------------------------------------------------
abstract_class IMesh : public IVertexBuffer, public IIndexBuffer
{
public:
	// -----------------------------------

	// Sets/gets the primitive type
	virtual void SetPrimitiveType( MaterialPrimitiveType_t type ) = 0;

	// Draws the mesh
	virtual void Draw( int nFirstIndex = -1, int nIndexCount = 0 ) = 0;

	virtual void SetColorMesh( IMesh *pColorMesh, int nVertexOffset ) = 0;

	// Draw a list of (lists of) primitives. Batching your lists together that use
	// the same lightmap, material, vertex and index buffers with multipass shaders
	// can drastically reduce state-switching overhead.
	// NOTE: this only works with STATIC meshes.
	virtual void Draw( CPrimList *pLists, int nLists ) = 0;

	// Copy verts and/or indices to a mesh builder. This only works for temp meshes!
	virtual void CopyToMeshBuilder( 
		int iStartVert,		// Which vertices to copy.
		int nVerts, 
		int iStartIndex,	// Which indices to copy.
		int nIndices, 
		int indexOffset,	// This is added to each index.
		CMeshBuilder &builder ) = 0;

	// Spews the mesh data
	virtual void Spew( int nVertexCount, int nIndexCount, const MeshDesc_t &desc ) = 0;

	// Call this in debug mode to make sure our data is good.
	virtual void ValidateData( int nVertexCount, int nIndexCount, const MeshDesc_t &desc ) = 0;

	// New version
	// Locks/unlocks the mesh, providing space for nVertexCount and nIndexCount.
	// nIndexCount of -1 means don't lock the index buffer...
	virtual void LockMesh( int nVertexCount, int nIndexCount, MeshDesc_t &desc ) = 0;
	virtual void ModifyBegin( int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount, MeshDesc_t& desc ) = 0;
	virtual void ModifyEnd( MeshDesc_t& desc ) = 0;
	virtual void UnlockMesh( int nVertexCount, int nIndexCount, MeshDesc_t &desc ) = 0;

	virtual void ModifyBeginEx( bool bReadOnly, int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount, MeshDesc_t &desc ) = 0;

	virtual void SetFlexMesh( IMesh *pMesh, int nVertexOffset ) = 0;

	virtual void DisableFlexMesh() = 0;

	virtual void MarkAsDrawn() = 0;

	virtual unsigned ComputeMemoryUsed() = 0;
};


#include "meshreader.h"

#define INVALID_BUFFER_OFFSET 0xFFFFFFFFUL

// flags for advancevertex optimization
#define VTX_HAVEPOS 1
#define VTX_HAVENORMAL 2
#define VTX_HAVECOLOR 4
#define VTX_HAVEALL ( VTX_HAVEPOS | VTX_HAVENORMAL | VTX_HAVECOLOR )


//-----------------------------------------------------------------------------
//
// Helper class used to define vertex buffers
//
//-----------------------------------------------------------------------------
class CVertexBuilder : private VertexDesc_t
{
public:
	CVertexBuilder();
	CVertexBuilder( IVertexBuffer *pVertexBuffer, VertexFormat_t fmt = 0 );
	~CVertexBuilder();

	// Begins, ends modification of the index buffer (returns true if the lock succeeded)
	// A lock may not succeed if append is set to true and there isn't enough room
	// NOTE: Append is only used with dynamic index buffers; it's ignored for static buffers
	bool Lock( int nMaxIndexCount, bool bAppend = false );
	void Unlock();

	// Spews the current data
	// NOTE: Can only be called during a lock/unlock block
	void SpewData();

	// Returns the number of indices we can fit into the buffer without needing to discard
	int GetRoomRemaining() const;

	// Binds this vertex buffer
	void Bind( IMatRenderContext *pContext, int nStreamID, VertexFormat_t usage = 0 );

	// Returns the byte offset
	int Offset() const;

	// This must be called before Begin, if a vertex buffer with a compressed format is to be used
	void SetCompressionType( VertexCompressionType_t compressionType );
	void ValidateCompressionType();

	void Begin( IVertexBuffer *pVertexBuffer, int nVertexCount, int *nFirstVertex );
	void Begin( IVertexBuffer *pVertexBuffer, int nVertexCount );

	// Use this when you're done writing
	// Set bDraw to true to call m_pMesh->Draw automatically.
	void End( bool bSpewData = false );

	// Locks the vertex buffer to modify existing data
	// Passing nVertexCount == -1 says to lock all the vertices for modification.
	void BeginModify( IVertexBuffer *pVertexBuffer, int nFirstVertex = 0, int nVertexCount = -1 );
	void EndModify( bool bSpewData = false );

	// returns the number of vertices
	int VertexCount() const;

	// Returns the total number of vertices across all Locks()
	int TotalVertexCount() const;

	// Resets the mesh builder so it points to the start of everything again
	void Reset();

	// Returns the size of the vertex
	int VertexSize() { return m_ActualVertexSize; }

	// returns the data size of a given texture coordinate
	int TextureCoordinateSize( int nTexCoordNumber ) { return m_VertexSize_TexCoord[ nTexCoordNumber ]; }

	// Returns the base vertex memory pointer
	void* BaseVertexData();

	// Selects the nth Vertex and Index 
	void SelectVertex( int idx );

	// Advances the current vertex and index by one
	void AdvanceVertex( void );
	template<int nFlags, int nNumTexCoords> void AdvanceVertexF( void );
	void AdvanceVertices( int nVerts );

	int GetCurrentVertex() const;
	int GetFirstVertex() const;

	// Data retrieval...
	const float *Position() const;

	const float *Normal() const;

	unsigned int Color() const;

	unsigned char *Specular() const;

	const float *TexCoord( int stage ) const;

	const float *TangentS() const;
	const float *TangentT() const;

	const float *BoneWeight() const;
	float Wrinkle() const;

	int NumBoneWeights() const;
#ifndef NEW_SKINNING
	unsigned char *BoneMatrix() const;
#else
	float *BoneMatrix() const;
#endif

	// position setting
	void Position3f( float x, float y, float z );
	void Position3fv( const float *v );

	// normal setting
	void Normal3f( float nx, float ny, float nz );
	void Normal3fv( const float *n );
	void NormalDelta3fv( const float *n );
	void NormalDelta3f( float nx, float ny, float nz );
	// normal setting (templatized for code which needs to support compressed vertices)
	template <VertexCompressionType_t T> void CompressedNormal3f( float nx, float ny, float nz );
	template <VertexCompressionType_t T> void CompressedNormal3fv( const float *n );

	// color setting
	void Color3f( float r, float g, float b );
	void Color3fv( const float *rgb );
	void Color4f( float r, float g, float b, float a );
	void Color4fv( const float *rgba );

	// Faster versions of color
	void Color3ub( unsigned char r, unsigned char g, unsigned char b );
	void Color3ubv( unsigned char const* rgb );
	void Color4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a );
	void Color4ubv( unsigned char const* rgba );

	// specular color setting
	void Specular3f( float r, float g, float b );
	void Specular3fv( const float *rgb );
	void Specular4f( float r, float g, float b, float a );
	void Specular4fv( const float *rgba );

	// Faster version of specular
	void Specular3ub( unsigned char r, unsigned char g, unsigned char b );
	void Specular3ubv( unsigned char const *c );
	void Specular4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a );
	void Specular4ubv( unsigned char const *c );

	// texture coordinate setting
	void TexCoord1f( int stage, float s );
	void TexCoord2f( int stage, float s, float t );
	void TexCoord2fv( int stage, const float *st );
	void TexCoord3f( int stage, float s, float t, float u );
	void TexCoord3fv( int stage, const float *stu );
	void TexCoord4f( int stage, float s, float t, float u, float w );
	void TexCoord4fv( int stage, const float *stuv );

	void TexCoordSubRect2f( int stage, float s, float t, float offsetS, float offsetT, float scaleS, float scaleT );
	void TexCoordSubRect2fv( int stage, const float *st, const float *offset, const float *scale );

	// tangent space 
	void TangentS3f( float sx, float sy, float sz );
	void TangentS3fv( const float* s );

	void TangentT3f( float tx, float ty, float tz );
	void TangentT3fv( const float* t );

	// Wrinkle
	void Wrinkle1f( float flWrinkle );

	// bone weights
	void BoneWeight( int idx, float weight );
	// bone weights (templatized for code which needs to support compressed vertices)
	template <VertexCompressionType_t T> void CompressedBoneWeight3fv( const float * pWeights );

	// bone matrix index
	void BoneMatrix( int idx, int matrixIndex );

	// Generic per-vertex data
	void UserData( const float* pData );
	// Generic per-vertex data (templatized for code which needs to support compressed vertices)
	template <VertexCompressionType_t T> void CompressedUserData( const float* pData );

	// Fast Vertex! No need to call advance vertex, and no random access allowed. 
	// WARNING - these are low level functions that are intended only for use
	// in the software vertex skinner.
	void FastVertex( const ModelVertexDX7_t &vertex );
	void FastVertexSSE( const ModelVertexDX7_t &vertex );

	// store 4 dx7 vertices fast. for special sse dx7 pipeline
	void Fast4VerticesSSE( 
		ModelVertexDX7_t const *vtx_a,
		ModelVertexDX7_t const *vtx_b,
		ModelVertexDX7_t const *vtx_c,
		ModelVertexDX7_t const *vtx_d);

	void FastVertex( const ModelVertexDX8_t &vertex );
	void FastVertexSSE( const ModelVertexDX8_t &vertex );

	// Add number of verts and current vert since FastVertex routines do not update.
	void FastAdvanceNVertices( int n );	

#if defined( _X360 )
	void VertexDX8ToX360( const ModelVertexDX8_t &vertex );
#endif

	// FIXME: Remove! Backward compat so we can use this from a CMeshBuilder.
	void AttachBegin( IMesh* pMesh, int nMaxVertexCount, const MeshDesc_t &desc );
	void AttachEnd();
	void AttachBeginModify( IMesh* pMesh, int nFirstVertex, int nVertexCount, const MeshDesc_t &desc );
	void AttachEndModify();

private:
	// The vertex buffer we're modifying
	IVertexBuffer *m_pVertexBuffer;

	// Used to make sure Begin/End calls and BeginModify/EndModify calls match.
	bool m_bModify;

	// Max number of indices and vertices
	int m_nMaxVertexCount;

	// Number of indices and vertices
	int m_nVertexCount;

	// The current vertex and index
	mutable int m_nCurrentVertex;

	// Optimization: Pointer to the current pos, norm, texcoord, and color
	mutable float			*m_pCurrPosition;
	mutable float			*m_pCurrNormal;
	mutable float			*m_pCurrTexCoord[VERTEX_MAX_TEXTURE_COORDINATES];
	mutable unsigned char	*m_pCurrColor;

	// Total number of vertices appended
	int m_nTotalVertexCount;

	// First vertex buffer offset + index
	unsigned int m_nBufferOffset;
	unsigned int m_nBufferFirstVertex;

#if ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 )
	// Debug checks to make sure we write userdata4/tangents AFTER normals
	bool m_bWrittenNormal   : 1;
	bool m_bWrittenUserData : 1;
#endif

	friend class CMeshBuilder;
};


//-----------------------------------------------------------------------------
//
// Inline methods of CVertexBuilder
//
//-----------------------------------------------------------------------------
inline CVertexBuilder::CVertexBuilder()
{
	m_pVertexBuffer = NULL;
	m_nBufferOffset = INVALID_BUFFER_OFFSET;
	m_nBufferFirstVertex = 0;
	m_nVertexCount = 0;
	m_nCurrentVertex = 0;
	m_nMaxVertexCount = 0;
	m_nTotalVertexCount = 0;
	m_CompressionType = VERTEX_COMPRESSION_INVALID;

#ifdef _DEBUG
	m_pCurrPosition = NULL;
	m_pCurrNormal = NULL;
	m_pCurrColor = NULL;
	memset( m_pCurrTexCoord, 0, sizeof( m_pCurrTexCoord ) );
	m_bModify = false;
#endif
}

inline CVertexBuilder::CVertexBuilder( IVertexBuffer *pVertexBuffer, VertexFormat_t fmt )
{
	m_pVertexBuffer = pVertexBuffer;
	m_nBufferOffset = INVALID_BUFFER_OFFSET;
	m_nBufferFirstVertex = 0;
	m_nVertexCount = 0;
	m_nCurrentVertex = 0;
	m_nMaxVertexCount = 0;
	m_nTotalVertexCount = 0;
	m_CompressionType = VERTEX_COMPRESSION_INVALID;

	if ( m_pVertexBuffer->IsDynamic() )
	{
		m_pVertexBuffer->BeginCastBuffer( fmt );
	}
	else
	{
		Assert( m_pVertexBuffer->GetVertexFormat() == fmt );
	}

#ifdef _DEBUG
	m_pCurrPosition = NULL;
	m_pCurrNormal = NULL;
	m_pCurrColor = NULL;
	memset( m_pCurrTexCoord, 0, sizeof( m_pCurrTexCoord ) );
	m_bModify = false;
#endif
}

inline CVertexBuilder::~CVertexBuilder()
{
	if ( m_pVertexBuffer && m_pVertexBuffer->IsDynamic() )
	{
		m_pVertexBuffer->EndCastBuffer();
	}
}

//-----------------------------------------------------------------------------
// Begins, ends modification of the index buffer
//-----------------------------------------------------------------------------
inline bool CVertexBuilder::Lock( int nMaxVertexCount, bool bAppend )
{
	Assert( m_pVertexBuffer );
	m_bModify = false;
	m_nMaxVertexCount = nMaxVertexCount;
	bool bFirstLock = ( m_nBufferOffset == INVALID_BUFFER_OFFSET );
	if ( bFirstLock )
	{
		bAppend = false;
	}
	if ( !bAppend )
	{
		m_nTotalVertexCount = 0;
	}

	// Lock the vertex buffer
	if ( !m_pVertexBuffer->Lock( m_nMaxVertexCount, bAppend, *this ) )
	{
		m_nMaxVertexCount = 0;
		return false;
	}

	Reset();

	if ( bFirstLock )
	{
		m_nBufferOffset = m_nOffset;
		m_nBufferFirstVertex = m_nFirstVertex;
	}

	return true;
}

inline void CVertexBuilder::Unlock()
{
	Assert( !m_bModify && m_pVertexBuffer );

#ifdef _DEBUG
	m_pVertexBuffer->ValidateData( m_nVertexCount, *this );
#endif

	m_pVertexBuffer->Unlock( m_nVertexCount, *this );
	m_nTotalVertexCount += m_nVertexCount;

	m_nMaxVertexCount = 0;

#ifdef _DEBUG
	// Null out our data...
	m_pCurrPosition = NULL;
	m_pCurrNormal = NULL;
	m_pCurrColor = NULL;
	memset( m_pCurrTexCoord, 0, sizeof( m_pCurrTexCoord ) );
	memset( static_cast<VertexDesc_t*>( this ), 0, sizeof(VertexDesc_t) );
#endif
}

inline void CVertexBuilder::SpewData()
{
	m_pVertexBuffer->Spew( m_nVertexCount, *this );
}


//-----------------------------------------------------------------------------
// Binds this vertex buffer
//-----------------------------------------------------------------------------
inline void CVertexBuilder::Bind( IMatRenderContext *pContext, int nStreamID, VertexFormat_t usage )
{
	if ( m_pVertexBuffer && ( m_nBufferOffset != INVALID_BUFFER_OFFSET ) )
	{
		pContext->BindVertexBuffer( nStreamID, m_pVertexBuffer, m_nBufferOffset,
			m_nFirstVertex, m_nTotalVertexCount, usage ? usage : m_pVertexBuffer->GetVertexFormat() );
	}
	else
	{
		pContext->BindVertexBuffer( nStreamID, NULL, 0, 0, 0, 0 );
	}
}


//-----------------------------------------------------------------------------
// Returns the byte offset
//-----------------------------------------------------------------------------
inline int CVertexBuilder::Offset() const
{
	return m_nBufferOffset;
}

inline int CVertexBuilder::GetFirstVertex() const
{
	return m_nBufferFirstVertex;
}

//-----------------------------------------------------------------------------
// Specify the type of vertex compression that this CMeshBuilder will perform
//-----------------------------------------------------------------------------
inline void CVertexBuilder::SetCompressionType( VertexCompressionType_t compressionType )
{
	// The real purpose of this method is to allow us to emit a Warning in Begin()
	m_CompressionType = compressionType;
}

inline void CVertexBuilder::ValidateCompressionType()
{
#ifdef _DEBUG
	VertexCompressionType_t vbCompressionType = CompressionType( m_pVertexBuffer->GetVertexFormat() );
	if ( vbCompressionType != VERTEX_COMPRESSION_NONE )
	{
		Assert( m_CompressionType == vbCompressionType );
		if ( m_CompressionType != vbCompressionType )
		{
			Warning( "ERROR: CVertexBuilder::SetCompressionType() must be called to specify the same vertex compression type (%s) as the vertex buffer being modified."
					 "Junk vertices will be rendered, or there will be a crash in CVertexBuilder!\n",
					  vbCompressionType == VERTEX_COMPRESSION_ON ? "VERTEX_COMPRESSION_ON" : "VERTEX_COMPRESSION_NONE" );
		}
		// Never use vertex compression for dynamic VBs (the conversions can really hurt perf)
		Assert(	!m_pVertexBuffer->IsDynamic() );
	}
#endif
}

inline void CVertexBuilder::Begin( IVertexBuffer *pVertexBuffer, int nVertexCount )
{
	Assert( pVertexBuffer && (!m_pVertexBuffer) );

	m_pVertexBuffer = pVertexBuffer;
	m_bModify = false;

	m_nMaxVertexCount = nVertexCount;
	m_nVertexCount = 0;

	// Make sure SetCompressionType was called correctly, if this VB is compressed
	ValidateCompressionType();

	// Lock the vertex and index buffer
	m_pVertexBuffer->Lock( m_nMaxVertexCount, false, *this );

	// Point to the start of the buffers..
	Reset();
}


//-----------------------------------------------------------------------------
// Use this when you're done modifying the mesh
//-----------------------------------------------------------------------------
inline void CVertexBuilder::End( bool bSpewData )
{
	// Make sure they called Begin()
	Assert( !m_bModify );

	if ( bSpewData )
	{
		m_pVertexBuffer->Spew( m_nVertexCount, *this );
	}

#ifdef _DEBUG
	m_pVertexBuffer->ValidateData( m_nVertexCount, *this );
#endif

	// Unlock our buffers
	m_pVertexBuffer->Unlock( m_nVertexCount, *this );

	m_pVertexBuffer = 0;
	m_nMaxVertexCount = 0;

	m_CompressionType = VERTEX_COMPRESSION_INVALID;

#ifdef _DEBUG
	// Null out our pointers...
	m_pCurrPosition = NULL;
	m_pCurrNormal = NULL;
	m_pCurrColor = NULL;
	memset( m_pCurrTexCoord, 0, sizeof( m_pCurrTexCoord ) );
	memset( static_cast< VertexDesc_t* >( this ), 0, sizeof(VertexDesc_t) );
#endif
}


//-----------------------------------------------------------------------------
// FIXME: Remove! Backward compat so we can use this from a CMeshBuilder.
//-----------------------------------------------------------------------------
inline void CVertexBuilder::AttachBegin( IMesh* pMesh, int nMaxVertexCount, const MeshDesc_t &desc )
{
	VertexCompressionType_t compressionType = m_CompressionType;

	m_pVertexBuffer = pMesh;
	memcpy( static_cast<VertexDesc_t*>( this ), static_cast<const VertexDesc_t*>( &desc ), sizeof(VertexDesc_t) );
	m_nMaxVertexCount = nMaxVertexCount;
	m_NumBoneWeights = m_NumBoneWeights == 0 ? 0 : 2;	// Two weights if any
	m_nVertexCount = 0;
	m_bModify = false;

	if ( compressionType != VERTEX_COMPRESSION_INVALID )
		m_CompressionType = compressionType;

	// Make sure SetCompressionType was called correctly, if this VB is compressed
	ValidateCompressionType();

	if ( m_nBufferOffset == INVALID_BUFFER_OFFSET )
	{
		m_nTotalVertexCount = 0;
		m_nBufferOffset = static_cast< const VertexDesc_t* >( &desc )->m_nOffset;
		m_nBufferFirstVertex = desc.m_nFirstVertex;
	}
}

inline void CVertexBuilder::AttachEnd()
{
	// Make sure they called Begin()
	Assert( !m_bModify );

	m_nMaxVertexCount = 0;
	m_pVertexBuffer = NULL;

	m_CompressionType = VERTEX_COMPRESSION_INVALID;

#ifdef _DEBUG
	// Null out our pointers...
	m_pCurrPosition = NULL;
	m_pCurrNormal = NULL;
	m_pCurrColor = NULL;
	memset( m_pCurrTexCoord, 0, sizeof( m_pCurrTexCoord ) );
	memset( static_cast<VertexDesc_t*>( this ), 0, sizeof(VertexDesc_t) );
#endif
}

inline void CVertexBuilder::AttachBeginModify( IMesh* pMesh, int nFirstVertex, int nVertexCount, const MeshDesc_t &desc )
{
	Assert( pMesh && (!m_pVertexBuffer) );

	m_pVertexBuffer = pMesh;
	memcpy( static_cast<VertexDesc_t*>( this ), static_cast<const VertexDesc_t*>( &desc ), sizeof(VertexDesc_t) );
	m_nMaxVertexCount = m_nVertexCount = nVertexCount;
	m_NumBoneWeights = m_NumBoneWeights == 0 ? 0 : 2;	// Two weights if any
	m_bModify = true;

	// Make sure SetCompressionType was called correctly, if this VB is compressed
	ValidateCompressionType();
}

inline void CVertexBuilder::AttachEndModify()
{
	Assert( m_pVertexBuffer );
	Assert( m_bModify );	// Make sure they called BeginModify.

	m_pVertexBuffer = 0;
	m_nMaxVertexCount = 0;

	m_CompressionType = VERTEX_COMPRESSION_INVALID;

#ifdef _DEBUG
	// Null out our pointers...
	m_pCurrPosition = NULL;
	m_pCurrNormal = NULL;
	m_pCurrColor = NULL;
	memset( m_pCurrTexCoord, 0, sizeof( m_pCurrTexCoord ) );
	memset( static_cast<VertexDesc_t*>( this ), 0, sizeof(VertexDesc_t) );
#endif
}


//-----------------------------------------------------------------------------
// Computes the first min non-null address
//-----------------------------------------------------------------------------
inline unsigned char* FindMinAddress( void *pAddress1, void *pAddress2, int nAddress2Size )
{
	if ( nAddress2Size == 0 )
		return (unsigned char*)pAddress1;
	if ( !pAddress1 )
		return (unsigned char*)pAddress2;
	return ( pAddress1 < pAddress2 ) ? (unsigned char*)pAddress1 : (unsigned char*)pAddress2;
}

//-----------------------------------------------------------------------------
// Resets the vertex buffer builder so it points to the start of everything again
//-----------------------------------------------------------------------------
inline void CVertexBuilder::Reset()
{
	m_nCurrentVertex = 0;

	m_pCurrPosition = m_pPosition;
	m_pCurrNormal = m_pNormal;
	for ( int i = 0; i < NELEMS( m_pCurrTexCoord ); i++ )
	{
		m_pCurrTexCoord[i] = m_pTexCoord[i];
	}
	m_pCurrColor = m_pColor;

#if ( defined( _DEBUG ) && ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 ) )
	m_bWrittenNormal   = false;
	m_bWrittenUserData = false;
#endif

#ifdef DEBUG_WRITE_COMBINE
	// Logic for m_pLastWrittenAddress is tricky. It really wants the min of the
	// non-null address pointers.
	m_nLastWrittenField = MB_FIELD_NONE;
	m_pLastWrittenAddress = NULL;
	m_pLastWrittenAddress =	FindMinAddress( m_pLastWrittenAddress, m_pPosition, m_VertexSize_Position );
	m_pLastWrittenAddress =	FindMinAddress( m_pLastWrittenAddress, m_pBoneWeight, m_VertexSize_BoneWeight );
	m_pLastWrittenAddress =	FindMinAddress( m_pLastWrittenAddress, m_pBoneMatrixIndex, m_VertexSize_BoneMatrixIndex );
	m_pLastWrittenAddress =	FindMinAddress( m_pLastWrittenAddress, m_pNormal, m_VertexSize_Normal );
	m_pLastWrittenAddress =	FindMinAddress( m_pLastWrittenAddress, m_pColor, m_VertexSize_Color );
	m_pLastWrittenAddress =	FindMinAddress( m_pLastWrittenAddress, m_pSpecular, m_VertexSize_Specular );
	for ( int i = 0; i < VERTEX_MAX_TEXTURE_COORDINATES; ++i )
	{
		m_pLastWrittenAddress =	FindMinAddress( m_pLastWrittenAddress, m_pTexCoord[i], m_VertexSize_TexCoord[i] );
	}
	m_pLastWrittenAddress =	FindMinAddress( m_pLastWrittenAddress, m_pTangentS, m_VertexSize_TangentS );
	m_pLastWrittenAddress =	FindMinAddress( m_pLastWrittenAddress, m_pTangentT, m_VertexSize_TangentT );
	m_pLastWrittenAddress =	FindMinAddress( m_pLastWrittenAddress, m_pUserData, m_VertexSize_UserData );
#endif
}


//-----------------------------------------------------------------------------
// returns the number of vertices
//-----------------------------------------------------------------------------
inline int CVertexBuilder::VertexCount() const
{
	return m_nVertexCount;
}


//-----------------------------------------------------------------------------
// Returns the total number of vertices across all Locks()
//-----------------------------------------------------------------------------
inline int CVertexBuilder::TotalVertexCount() const
{
	return m_nTotalVertexCount;
}


//-----------------------------------------------------------------------------
// Returns the base vertex memory pointer
//-----------------------------------------------------------------------------
inline void* CVertexBuilder::BaseVertexData()
{
	// FIXME: If there's no position specified, we need to find
	// the base address 
	Assert( m_pPosition );
	return m_pPosition;
}


//-----------------------------------------------------------------------------
// Selects the current vertex
//-----------------------------------------------------------------------------
inline void CVertexBuilder::SelectVertex( int nIndex )
{
	// NOTE: This index is expected to be relative 
	Assert( (nIndex >= 0) && (nIndex < m_nMaxVertexCount) );
	m_nCurrentVertex = nIndex;

	m_pCurrPosition = OffsetFloatPointer( m_pPosition, m_nCurrentVertex, m_VertexSize_Position );
	m_pCurrNormal = OffsetFloatPointer( m_pNormal, m_nCurrentVertex, m_VertexSize_Normal );

	COMPILE_TIME_ASSERT( VERTEX_MAX_TEXTURE_COORDINATES == 8 );
	m_pCurrTexCoord[0] = OffsetFloatPointer( m_pTexCoord[0], m_nCurrentVertex, m_VertexSize_TexCoord[0] );
	m_pCurrTexCoord[1] = OffsetFloatPointer( m_pTexCoord[1], m_nCurrentVertex, m_VertexSize_TexCoord[1] );
	m_pCurrTexCoord[2] = OffsetFloatPointer( m_pTexCoord[2], m_nCurrentVertex, m_VertexSize_TexCoord[2] );
	m_pCurrTexCoord[3] = OffsetFloatPointer( m_pTexCoord[3], m_nCurrentVertex, m_VertexSize_TexCoord[3] );
	m_pCurrTexCoord[4] = OffsetFloatPointer( m_pTexCoord[4], m_nCurrentVertex, m_VertexSize_TexCoord[4] );
	m_pCurrTexCoord[5] = OffsetFloatPointer( m_pTexCoord[5], m_nCurrentVertex, m_VertexSize_TexCoord[5] );
	m_pCurrTexCoord[6] = OffsetFloatPointer( m_pTexCoord[6], m_nCurrentVertex, m_VertexSize_TexCoord[6] );
	m_pCurrTexCoord[7] = OffsetFloatPointer( m_pTexCoord[7], m_nCurrentVertex, m_VertexSize_TexCoord[7] );
	m_pCurrColor = m_pColor + m_nCurrentVertex * m_VertexSize_Color;

#if ( defined( _DEBUG ) && ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 ) )
	m_bWrittenNormal   = false;
	m_bWrittenUserData = false;
#endif
}


//-----------------------------------------------------------------------------
// Advances vertex after you're done writing to it.
//-----------------------------------------------------------------------------

template<int nFlags, int nNumTexCoords> FORCEINLINE void CVertexBuilder::AdvanceVertexF()
{
	if ( ++m_nCurrentVertex > m_nVertexCount )
	{
		m_nVertexCount = m_nCurrentVertex;
	}

	if ( nFlags & VTX_HAVEPOS )
		IncrementFloatPointer( m_pCurrPosition, m_VertexSize_Position );
	if ( nFlags & VTX_HAVENORMAL )
		IncrementFloatPointer( m_pCurrNormal, m_VertexSize_Normal );
	if ( nFlags & VTX_HAVECOLOR )
		m_pCurrColor += m_VertexSize_Color;

	COMPILE_TIME_ASSERT( VERTEX_MAX_TEXTURE_COORDINATES == 8 );
	if ( nNumTexCoords > 0 )
		IncrementFloatPointer( m_pCurrTexCoord[0], m_VertexSize_TexCoord[0] );
	if ( nNumTexCoords > 1 )
		IncrementFloatPointer( m_pCurrTexCoord[1], m_VertexSize_TexCoord[1] );
	if ( nNumTexCoords > 2 )
		IncrementFloatPointer( m_pCurrTexCoord[2], m_VertexSize_TexCoord[2] );
	if ( nNumTexCoords > 3 )
		IncrementFloatPointer( m_pCurrTexCoord[3], m_VertexSize_TexCoord[3] );
	if ( nNumTexCoords > 4 )
		IncrementFloatPointer( m_pCurrTexCoord[4], m_VertexSize_TexCoord[4] );
	if ( nNumTexCoords > 5 )
		IncrementFloatPointer( m_pCurrTexCoord[5], m_VertexSize_TexCoord[5] );
	if ( nNumTexCoords > 6 )
		IncrementFloatPointer( m_pCurrTexCoord[6], m_VertexSize_TexCoord[6] );
	if ( nNumTexCoords > 7 )
		IncrementFloatPointer( m_pCurrTexCoord[7], m_VertexSize_TexCoord[7] );

#if ( defined( _DEBUG ) && ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 ) )
	m_bWrittenNormal   = false;
	m_bWrittenUserData = false;
#endif
}

inline void CVertexBuilder::AdvanceVertex()
{
	AdvanceVertexF<VTX_HAVEALL, 8>();
}


inline void CVertexBuilder::AdvanceVertices( int nVerts )
{
	m_nCurrentVertex += nVerts;
	if ( m_nCurrentVertex > m_nVertexCount )
	{
		m_nVertexCount = m_nCurrentVertex;
	}

	IncrementFloatPointer( m_pCurrPosition, m_VertexSize_Position*nVerts );
	IncrementFloatPointer( m_pCurrNormal, m_VertexSize_Normal*nVerts );

	COMPILE_TIME_ASSERT( VERTEX_MAX_TEXTURE_COORDINATES == 8 );
	IncrementFloatPointer( m_pCurrTexCoord[0], m_VertexSize_TexCoord[0]*nVerts );
	IncrementFloatPointer( m_pCurrTexCoord[1], m_VertexSize_TexCoord[1]*nVerts );
	IncrementFloatPointer( m_pCurrTexCoord[2], m_VertexSize_TexCoord[2]*nVerts );
	IncrementFloatPointer( m_pCurrTexCoord[3], m_VertexSize_TexCoord[3]*nVerts );
	IncrementFloatPointer( m_pCurrTexCoord[4], m_VertexSize_TexCoord[4]*nVerts );
	IncrementFloatPointer( m_pCurrTexCoord[5], m_VertexSize_TexCoord[5]*nVerts );
	IncrementFloatPointer( m_pCurrTexCoord[6], m_VertexSize_TexCoord[6]*nVerts );
	IncrementFloatPointer( m_pCurrTexCoord[7], m_VertexSize_TexCoord[7]*nVerts );
	m_pCurrColor += m_VertexSize_Color*nVerts;

#if ( defined( _DEBUG ) && ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 ) )
	m_bWrittenNormal   = false;
	m_bWrittenUserData = false;
#endif
}


//-----------------------------------------------------------------------------
// For use with the FastVertex methods, advances the current vertex by N
//-----------------------------------------------------------------------------
inline void CVertexBuilder::FastAdvanceNVertices( int n )
{
	m_nCurrentVertex += n;
	m_nVertexCount = m_nCurrentVertex;
}



#ifndef COMPILER_MSVC64
// Implement for 64-bit Windows if needed.
//-----------------------------------------------------------------------------
// Fast Vertex! No need to call advance vertex, and no random access allowed
//-----------------------------------------------------------------------------
inline void CVertexBuilder::FastVertex( const ModelVertexDX7_t &vertex )
{
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE ); // FIXME: support compressed verts if needed
	Assert( m_nCurrentVertex < m_nMaxVertexCount );

#if defined( _WIN32 ) && !defined( _X360 )
	const void *pRead = &vertex;
	void *pCurrPos = m_pCurrPosition;

	__asm
	{
		mov esi, pRead
			mov edi, pCurrPos

			movq mm0, [esi + 0]
		movq mm1, [esi + 8]
		movq mm2, [esi + 16]
		movq mm3, [esi + 24]
		movq mm4, [esi + 32]
		movq mm5, [esi + 40]

		movntq [edi + 0], mm0
			movntq [edi + 8], mm1
			movntq [edi + 16], mm2
			movntq [edi + 24], mm3
			movntq [edi + 32], mm4
			movntq [edi + 40], mm5

			emms
	}
#elif defined(GNUC)
	const void *pRead = &vertex;
	void *pCurrPos = m_pCurrPosition;
	__asm__ __volatile__ (
						  "movq (%0), %%mm0\n"
						  "movq 8(%0), %%mm1\n"
						  "movq 16(%0), %%mm2\n"
						  "movq 24(%0), %%mm3\n"
						  "movq 32(%0), %%mm4\n"
						  "movq 40(%0), %%mm5\n"
						  "movntq %%mm0, (%1)\n"
						  "movntq %%mm1, 8(%1)\n"
						  "movntq %%mm2, 16(%1)\n"
						  "movntq %%mm3, 24(%1)\n"
						  "movntq %%mm4, 32(%1)\n"
						  "movntq %%mm5, 40(%1)\n"
						  "emms\n"
						  :: "r" (pRead), "r" (pCurrPos) : "memory");
#else
	Error( "Implement CMeshBuilder::FastVertex(dx7) ");
#endif

	IncrementFloatPointer( m_pCurrPosition, m_VertexSize_Position );
	//m_nVertexCount = ++m_nCurrentVertex;

#if ( defined( _DEBUG ) && ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 ) )
	m_bWrittenNormal   = false;
	m_bWrittenUserData = false;
#endif
}

inline void CVertexBuilder::FastVertexSSE( const ModelVertexDX7_t &vertex )
{
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE ); // FIXME: support compressed verts if needed
	Assert( m_nCurrentVertex < m_nMaxVertexCount );

#if defined( _WIN32 ) && !defined( _X360 )
	const void *pRead = &vertex;
	void *pCurrPos = m_pCurrPosition;
	__asm
	{
		mov esi, pRead
			mov edi, pCurrPos

			movaps xmm0, [esi + 0]
		movaps xmm1, [esi + 16]
		movaps xmm2, [esi + 32]

		movntps [edi + 0], xmm0
			movntps [edi + 16], xmm1
			movntps [edi + 32], xmm2
	}
#elif defined(GNUC)
	const char *pRead = (char *)&vertex;
	char *pCurrPos = (char *)m_pCurrPosition;
	__m128 m1 = _mm_load_ps( (float *)pRead );
	__m128 m2 = _mm_load_ps( (float *)(pRead + 16) );
	__m128 m3 = _mm_load_ps( (float *)(pRead + 32) );
	_mm_stream_ps( (float *)pCurrPos, m1 );
	_mm_stream_ps( (float *)(pCurrPos + 16), m2 );
	_mm_stream_ps( (float *)(pCurrPos + 32), m3 );
#else
	Error( "Implement CMeshBuilder::FastVertexSSE(dx7)" );
#endif

	IncrementFloatPointer( m_pCurrPosition, m_VertexSize_Position );
	//m_nVertexCount = ++m_nCurrentVertex;

#if ( defined( _DEBUG ) && ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 ) )
	m_bWrittenNormal   = false;
	m_bWrittenUserData = false;
#endif
}

inline void CVertexBuilder::Fast4VerticesSSE( 
	ModelVertexDX7_t const *vtx_a,
	ModelVertexDX7_t const *vtx_b,
	ModelVertexDX7_t const *vtx_c,
	ModelVertexDX7_t const *vtx_d)
{
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE ); // FIXME: support compressed verts if needed
	Assert( m_nCurrentVertex < m_nMaxVertexCount-3 );

#if defined( _WIN32 ) && !defined( _X360 )
	void *pCurrPos = m_pCurrPosition;
	__asm
	{
		mov esi, vtx_a
			mov ecx, vtx_b

			mov edi, pCurrPos
			nop

			movaps xmm0, [esi + 0]
		movaps xmm1, [esi + 16]
		movaps xmm2, [esi + 32]
		movaps xmm3, [ecx + 0]
		movaps xmm4, [ecx + 16]
		movaps xmm5, [ecx + 32]

		mov esi, vtx_c
			mov ecx, vtx_d

			movntps [edi + 0], xmm0
			movntps [edi + 16], xmm1
			movntps [edi + 32], xmm2
			movntps [edi + 48], xmm3
			movntps [edi + 64], xmm4
			movntps [edi + 80], xmm5

			movaps xmm0, [esi + 0]
		movaps xmm1, [esi + 16]
		movaps xmm2, [esi + 32]
		movaps xmm3, [ecx + 0]
		movaps xmm4, [ecx + 16]
		movaps xmm5, [ecx + 32]

		movntps [edi + 0+96], xmm0
			movntps [edi + 16+96], xmm1
			movntps [edi + 32+96], xmm2
			movntps [edi + 48+96], xmm3
			movntps [edi + 64+96], xmm4
			movntps [edi + 80+96], xmm5

	}
#else
	Error( "Implement CMeshBuilder::Fast4VerticesSSE\n");
#endif
	IncrementFloatPointer( m_pCurrPosition, 4*m_VertexSize_Position );

#if ( defined( _DEBUG ) && ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 ) )
	m_bWrittenNormal   = false;
	m_bWrittenUserData = false;
#endif
}

inline void CVertexBuilder::FastVertex( const ModelVertexDX8_t &vertex )
{
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE ); // FIXME: support compressed verts if needed
	Assert( m_nCurrentVertex < m_nMaxVertexCount );

#if defined( _WIN32 ) && !defined( _X360 )
	const void *pRead = &vertex;
	void *pCurrPos = m_pCurrPosition;
	__asm
	{
		mov esi, pRead
			mov edi, pCurrPos

			movq mm0, [esi + 0]
		movq mm1, [esi + 8]
		movq mm2, [esi + 16]
		movq mm3, [esi + 24]
		movq mm4, [esi + 32]
		movq mm5, [esi + 40]
		movq mm6, [esi + 48]
		movq mm7, [esi + 56]

		movntq [edi + 0], mm0
			movntq [edi + 8], mm1
			movntq [edi + 16], mm2
			movntq [edi + 24], mm3
			movntq [edi + 32], mm4
			movntq [edi + 40], mm5
			movntq [edi + 48], mm6
			movntq [edi + 56], mm7

			emms
	}
#elif defined(GNUC)
	const void *pRead = &vertex;
	void *pCurrPos = m_pCurrPosition;
	__asm__ __volatile__ (
						  "movq (%0), %%mm0\n"
						  "movq 8(%0), %%mm1\n"
						  "movq 16(%0), %%mm2\n"
						  "movq 24(%0), %%mm3\n"
						  "movq 32(%0), %%mm4\n"
						  "movq 40(%0), %%mm5\n"
						  "movq 48(%0), %%mm6\n"
						  "movq 56(%0), %%mm7\n"
						  "movntq %%mm0, (%1)\n"
						  "movntq %%mm1, 8(%1)\n"
						  "movntq %%mm2, 16(%1)\n"
						  "movntq %%mm3, 24(%1)\n"
						  "movntq %%mm4, 32(%1)\n"
						  "movntq %%mm5, 40(%1)\n"
						  "movntq %%mm6, 48(%1)\n"
						  "movntq %%mm7, 56(%1)\n"
						  "emms\n"
						  :: "r" (pRead), "r" (pCurrPos) : "memory");
#else
	Error( "Implement CMeshBuilder::FastVertex(dx8)" );
#endif

	IncrementFloatPointer( m_pCurrPosition, m_VertexSize_Position );
	//	m_nVertexCount = ++m_nCurrentVertex;

#if ( defined( _DEBUG ) && ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 ) )
	m_bWrittenNormal   = false;
	m_bWrittenUserData = false;
#endif
}

inline void CVertexBuilder::FastVertexSSE( const ModelVertexDX8_t &vertex )
{
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE ); // FIXME: support compressed verts if needed
	Assert( m_nCurrentVertex < m_nMaxVertexCount );

#if defined( _WIN32 ) && !defined( _X360 )
	const void *pRead = &vertex;
	void *pCurrPos = m_pCurrPosition;
	__asm
	{
		mov esi, pRead
			mov edi, pCurrPos

			movaps xmm0, [esi + 0]
		movaps xmm1, [esi + 16]
		movaps xmm2, [esi + 32]
		movaps xmm3, [esi + 48]

		movntps [edi + 0], xmm0
			movntps [edi + 16], xmm1
			movntps [edi + 32], xmm2
			movntps [edi + 48], xmm3
	}
#elif defined(GNUC)
	const void *pRead = &vertex;
	void *pCurrPos = m_pCurrPosition;
	__asm__ __volatile__ (
						  "movaps (%0), %%xmm0\n"
						  "movaps 16(%0), %%xmm1\n"
						  "movaps 32(%0), %%xmm2\n"
						  "movaps 48(%0), %%xmm3\n"
						  "movntps %%xmm0, (%1)\n"
						  "movntps %%xmm1, 16(%1)\n"
						  "movntps %%xmm2, 32(%1)\n"
						  "movntps %%xmm3, 48(%1)\n"						  
						  :: "r" (pRead), "r" (pCurrPos) : "memory");
#else
	Error( "Implement CMeshBuilder::FastVertexSSE((dx8)" );
#endif

	IncrementFloatPointer( m_pCurrPosition, m_VertexSize_Position );
	//	m_nVertexCount = ++m_nCurrentVertex;

#if ( defined( _DEBUG ) && ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 ) )
	m_bWrittenNormal   = false;
	m_bWrittenUserData = false;
#endif
}
#endif // COMPILER_MSVC64


//-----------------------------------------------------------------------------
// Returns the current vertex
//-----------------------------------------------------------------------------
inline int CVertexBuilder::GetCurrentVertex() const
{
	return m_nCurrentVertex;
}


//-----------------------------------------------------------------------------
// Copies a vertex into the x360 format
//-----------------------------------------------------------------------------
#if defined( _X360 )
inline void CVertexBuilder::VertexDX8ToX360( const ModelVertexDX8_t &vertex )
{
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE ); // FIXME: support compressed verts if needed
	Assert( m_nCurrentVertex < m_nMaxVertexCount );

	// get the start of the data
	unsigned char *pDst = (unsigned char*)m_pCurrPosition;

	Assert( m_VertexSize_Position > 0 ); // Assume position is always present
	Assert( GetVertexElementSize( VERTEX_ELEMENT_POSITION, VERTEX_COMPRESSION_NONE ) == sizeof( vertex.m_vecPosition ) );
	memcpy( pDst, vertex.m_vecPosition.Base(), sizeof( vertex.m_vecPosition ) );
	pDst += sizeof( vertex.m_vecPosition );

	if ( m_VertexSize_BoneWeight )
	{
		Assert( vertex.m_flBoneWeights[0] >= 0 && vertex.m_flBoneWeights[0] <= 1.0f );
		Assert( vertex.m_flBoneWeights[1] >= 0 && vertex.m_flBoneWeights[1] <= 1.0f );
		Assert( GetVertexElementSize( VERTEX_ELEMENT_BONEWEIGHTS2, VERTEX_COMPRESSION_NONE ) == sizeof( vertex.m_flBoneWeights ) );
		memcpy( pDst, vertex.m_flBoneWeights.Base(), sizeof( vertex.m_flBoneWeights ) );
		pDst += sizeof( vertex.m_flBoneWeights );

		if ( m_VertexSize_BoneMatrixIndex )
		{
			Assert( GetVertexElementSize( VERTEX_ELEMENT_BONEINDEX, VERTEX_COMPRESSION_NONE ) == sizeof( vertex.m_nBoneIndices ) );
			*(unsigned int*)pDst = vertex.m_nBoneIndices;
			pDst += sizeof( vertex.m_nBoneIndices );
		}
	}

	if ( m_VertexSize_Normal )
	{
		Assert( GetVertexElementSize( VERTEX_ELEMENT_NORMAL, VERTEX_COMPRESSION_NONE ) == sizeof( vertex.m_vecNormal ) );
		memcpy( pDst, vertex.m_vecNormal.Base(), sizeof( vertex.m_vecNormal ) );
		pDst += sizeof( vertex.m_vecNormal );
	}

	if ( m_VertexSize_Color )
	{
		Assert( GetVertexElementSize( VERTEX_ELEMENT_COLOR, VERTEX_COMPRESSION_NONE ) == sizeof( vertex.m_nColor ) );
		*(unsigned int*)pDst = vertex.m_nColor;
		pDst += sizeof( vertex.m_nColor );
	}

	if ( m_VertexSize_TexCoord[0] )
	{
		Assert( GetVertexElementSize( VERTEX_ELEMENT_TEXCOORD2D_0, VERTEX_COMPRESSION_NONE ) == sizeof( vertex.m_vecTexCoord ) );
		memcpy( pDst, vertex.m_vecTexCoord.Base(), sizeof( vertex.m_vecTexCoord ) );
		pDst += sizeof( vertex.m_vecTexCoord );
	}

	if ( m_VertexSize_UserData )
	{
		Assert( GetVertexElementSize( VERTEX_ELEMENT_USERDATA4, VERTEX_COMPRESSION_NONE ) == sizeof( vertex.m_vecUserData ) );
		memcpy( pDst, vertex.m_vecUserData.Base(), sizeof( vertex.m_vecUserData ) );
		pDst += sizeof( vertex.m_vecUserData );
	}

	// ensure code is synced with the mesh builder that established the offsets
	Assert( pDst - (unsigned char*)m_pCurrPosition == m_VertexSize_Position );

	IncrementFloatPointer( m_pCurrPosition, m_VertexSize_Position );

#if ( defined( _DEBUG ) && ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 ) )
	m_bWrittenNormal   = false;
	m_bWrittenUserData = false;
#endif
}
#endif


//-----------------------------------------------------------------------------
// Data retrieval...
//-----------------------------------------------------------------------------
inline const float* CVertexBuilder::Position() const
{
	// FIXME: add a templatized accessor (return type varies to ensure calling code is updated appropriately)
	//        for code that needs to access compressed data (and/or a return-by-value templatized accessor)
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE );
	Assert( m_nCurrentVertex < m_nMaxVertexCount );
	return m_pCurrPosition;
}

inline const float* CVertexBuilder::Normal() const
{
	// FIXME: add a templatized accessor (return type varies to ensure calling code is updated appropriately)
	//        for code that needs to access compressed data (and/or a return-by-value templatized accessor)
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE );
	Assert( m_nCurrentVertex < m_nMaxVertexCount );
	return m_pCurrNormal;
}

inline unsigned int CVertexBuilder::Color() const
{
	// FIXME: add a templatized accessor (return type varies to ensure calling code is updated appropriately)
	//        for code that needs to access compressed data (and/or a return-by-value templatized accessor)
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE );
	// Swizzle it so it returns the same format as accepted by Color4ubv - rgba
	Assert( m_nCurrentVertex < m_nMaxVertexCount );
	unsigned int color;
	if ( IsPC() || !IsX360() )
	{
		color = (m_pCurrColor[3] << 24) | (m_pCurrColor[0] << 16) | (m_pCurrColor[1] << 8) | (m_pCurrColor[2]);
	}
	else
	{
		// in memory as argb, back to rgba
		color = (m_pCurrColor[1] << 24) | (m_pCurrColor[2] << 16) | (m_pCurrColor[3] << 8) | (m_pCurrColor[0]);
	}
	return color;
}

inline unsigned char *CVertexBuilder::Specular() const
{
	// FIXME: add a templatized accessor (return type varies to ensure calling code is updated appropriately)
	//        for code that needs to access compressed data (and/or a return-by-value templatized accessor)
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE );
	Assert( m_nCurrentVertex < m_nMaxVertexCount );
	return m_pSpecular + m_nCurrentVertex * m_VertexSize_Specular;
}

inline const float* CVertexBuilder::TexCoord( int stage ) const
{
	// FIXME: add a templatized accessor (return type varies to ensure calling code is updated appropriately)
	//        for code that needs to access compressed data (and/or a return-by-value templatized accessor)
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE );
	Assert( m_nCurrentVertex < m_nMaxVertexCount );
	return m_pCurrTexCoord[stage];
}

inline const float* CVertexBuilder::TangentS() const
{
	// FIXME: add a templatized accessor (return type varies to ensure calling code is updated appropriately)
	//        for code that needs to access compressed data (and/or a return-by-value templatized accessor)
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE );
	Assert( m_nCurrentVertex < m_nMaxVertexCount );
	return OffsetFloatPointer( m_pTangentS, m_nCurrentVertex, m_VertexSize_TangentS );
}

inline const float* CVertexBuilder::TangentT() const
{
	// FIXME: add a templatized accessor (return type varies to ensure calling code is updated appropriately)
	//        for code that needs to access compressed data (and/or a return-by-value templatized accessor)
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE );
	Assert( m_nCurrentVertex < m_nMaxVertexCount );
	return OffsetFloatPointer( m_pTangentT, m_nCurrentVertex, m_VertexSize_TangentT );
}

inline float CVertexBuilder::Wrinkle() const
{
	// FIXME: add a templatized accessor (return type varies to ensure calling code is updated appropriately)
	//        for code that needs to access compressed data (and/or a return-by-value templatized accessor)
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE );
	Assert( m_nCurrentVertex < m_nMaxVertexCount );
	return *OffsetFloatPointer( m_pWrinkle, m_nCurrentVertex, m_VertexSize_Wrinkle );
}

inline const float* CVertexBuilder::BoneWeight() const
{
	// FIXME: add a templatized accessor (return type varies to ensure calling code is updated appropriately)
	//        for code that needs to access compressed data (and/or a return-by-value templatized accessor)
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE );
	Assert( m_nCurrentVertex < m_nMaxVertexCount );
	return OffsetFloatPointer( m_pBoneWeight, m_nCurrentVertex, m_VertexSize_BoneWeight );
}

inline int CVertexBuilder::NumBoneWeights() const
{
	return m_NumBoneWeights;
}

#ifndef NEW_SKINNING
inline unsigned char* CVertexBuilder::BoneMatrix() const
{
	// FIXME: add a templatized accessor (return type varies to ensure calling code is updated appropriately)
	//        for code that needs to access compressed data (and/or a return-by-value templatized accessor)
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE );
	Assert( m_nCurrentVertex < m_nMaxVertexCount );
	return m_pBoneMatrixIndex + m_nCurrentVertex * m_VertexSize_BoneMatrixIndex;
}
#else
inline float* CVertexBuilder::BoneMatrix() const
{
	// FIXME: add a templatized accessor (return type varies to ensure calling code is updated appropriately)
	//        for code that needs to access compressed data (and/or a return-by-value templatized accessor)
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE );
	Assert( m_nCurrentVertex < m_nMaxVertexCount );
	return m_pBoneMatrixIndex + m_nCurrentVertex * m_VertexSize_BoneMatrixIndex;
}
#endif


//-----------------------------------------------------------------------------
// Position setting methods
//-----------------------------------------------------------------------------
inline void	CVertexBuilder::Position3f( float x, float y, float z )
{
	Assert( m_pPosition && m_pCurrPosition );
	Assert( IsFinite(x) && IsFinite(y) && IsFinite(z) );
	float *pDst = m_pCurrPosition;
	*pDst++ = x;
	*pDst++ = y;
	*pDst = z;
}

inline void	CVertexBuilder::Position3fv( const float *v )
{
	Assert(v);
	Assert( m_pPosition && m_pCurrPosition );

	float *pDst = m_pCurrPosition;
	*pDst++ = *v++;
	*pDst++ = *v++;
	*pDst = *v;
}


//-----------------------------------------------------------------------------
// Normal setting methods
//-----------------------------------------------------------------------------
inline void	CVertexBuilder::Normal3f( float nx, float ny, float nz )
{
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE ); // Use the templatized version if you want to support compression
	Assert( m_pNormal );
	Assert( IsFinite(nx) && IsFinite(ny) && IsFinite(nz) );
	Assert( nx >= -1.05f && nx <= 1.05f );
	Assert( ny >= -1.05f && ny <= 1.05f );
	Assert( nz >= -1.05f && nz <= 1.05f );

	float *pDst = m_pCurrNormal;
	*pDst++ = nx;
	*pDst++ = ny;
	*pDst = nz;
}

inline void	CVertexBuilder::Normal3fv( const float *n )
{
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE ); // Use the templatized version if you want to support compression
	Assert( n );
	Assert( m_pNormal && m_pCurrNormal );
	Assert( IsFinite(n[0]) && IsFinite(n[1]) && IsFinite(n[2]) );
	Assert( n[0] >= -1.05f && n[0] <= 1.05f );
	Assert( n[1] >= -1.05f && n[1] <= 1.05f );
	Assert( n[2] >= -1.05f && n[2] <= 1.05f );

	float *pDst = m_pCurrNormal;
	*pDst++ = *n++;
	*pDst++ = *n++;
	*pDst = *n;
}

inline void	CVertexBuilder::NormalDelta3f( float nx, float ny, float nz )
{
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE ); // Use the templatized version if you want to support compression
	Assert( m_pNormal );
	Assert( IsFinite(nx) && IsFinite(ny) && IsFinite(nz) );

	float *pDst = m_pCurrNormal;
	*pDst++ = nx;
	*pDst++ = ny;
	*pDst = nz;
}

inline void	CVertexBuilder::NormalDelta3fv( const float *n )
{
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE ); // Use the templatized version if you want to support compression
	Assert( n );
	Assert( m_pNormal && m_pCurrNormal );
	Assert( IsFinite(n[0]) && IsFinite(n[1]) && IsFinite(n[2]) );

	float *pDst = m_pCurrNormal;
	*pDst++ = *n++;
	*pDst++ = *n++;
	*pDst = *n;
}

//-----------------------------------------------------------------------------
// Templatized normal setting methods which support compressed vertices
//-----------------------------------------------------------------------------
template <VertexCompressionType_t T> inline void CVertexBuilder::CompressedNormal3f( float nx, float ny, float nz )
{
	Assert( T == m_CompressionType );
	Assert( m_pNormal && m_pCurrNormal );
	Assert( IsFinite(nx) && IsFinite(ny) && IsFinite(nz) );
	Assert( nx >= -1.05f && nx <= 1.05f );
	Assert( ny >= -1.05f && ny <= 1.05f );
	Assert( nz >= -1.05f && nz <= 1.05f );
	// FIXME: studiorender is passing in non-unit normals
	//float lengthSqd = nx*nx + ny*ny + nz*nz;
	//Assert( lengthSqd >= 0.95f && lengthSqd <= 1.05f );

	if ( T == VERTEX_COMPRESSION_ON )
	{
#if		( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_SEPARATETANGENTS_SHORT2 )
		PackNormal_SHORT2( nx, ny, nz, (unsigned int *)m_pCurrNormal );

#else //( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 )
		// NOTE: write the normal into the lower 16 bits of a word, clearing the top 16 bits - a userdata4
		//       tangent must be written into the upper 16 bits by CompressedUserData() *AFTER* this.
#ifdef _DEBUG
		Assert( m_bWrittenUserData == false );
		m_bWrittenNormal = true;
#endif
		PackNormal_UBYTE4( nx, ny, nz, (unsigned int *)m_pCurrNormal );
#endif
	}
	else
	{
		float *pDst = m_pCurrNormal;
		*pDst++ = nx;
		*pDst++ = ny;
		*pDst = nz;
	}
}

template <VertexCompressionType_t T> inline void CVertexBuilder::CompressedNormal3fv( const float *n )
{
	Assert( n );
	CompressedNormal3f<T>( n[0], n[1], n[2] );
}


//-----------------------------------------------------------------------------
// Color setting methods
//-----------------------------------------------------------------------------
inline void	CVertexBuilder::Color3f( float r, float g, float b )
{
	Assert( m_pColor && m_pCurrColor );
	Assert( IsFinite(r) && IsFinite(g) && IsFinite(b) );
	Assert( (r >= 0.0) && (g >= 0.0) && (b >= 0.0) );
	Assert( (r <= 1.0) && (g <= 1.0) && (b <= 1.0) );

#ifdef OPENGL_SWAP_COLORS
	int col = (FastFToC(r)) | (FastFToC(g) << 8) | (FastFToC(b) << 16) | 0xFF000000;
#else
	int col = (FastFToC(b)) | (FastFToC(g) << 8) | (FastFToC(r) << 16) | 0xFF000000;
#endif
	*(int*)m_pCurrColor = col;
}

inline void	CVertexBuilder::Color3fv( const float *rgb )
{
	Assert(rgb);
	Assert( m_pColor && m_pCurrColor );
	Assert( IsFinite(rgb[0]) && IsFinite(rgb[1]) && IsFinite(rgb[2]) );
	Assert( (rgb[0] >= 0.0) && (rgb[1] >= 0.0) && (rgb[2] >= 0.0) );
	Assert( (rgb[0] <= 1.0) && (rgb[1] <= 1.0) && (rgb[2] <= 1.0) );

#ifdef OPENGL_SWAP_COLORS	
	int col = (FastFToC(rgb[0])) | (FastFToC(rgb[1]) << 8) | (FastFToC(rgb[2]) << 16) | 0xFF000000;
#else
	int col = (FastFToC(rgb[2])) | (FastFToC(rgb[1]) << 8) | (FastFToC(rgb[0]) << 16) | 0xFF000000;
#endif
	*(int*)m_pCurrColor = col;
}

inline void	CVertexBuilder::Color4f( float r, float g, float b, float a )
{
	Assert( m_pColor && m_pCurrColor );
	Assert( IsFinite(r) && IsFinite(g) && IsFinite(b) && IsFinite(a) );
	Assert( (r >= 0.0) && (g >= 0.0) && (b >= 0.0) && (a >= 0.0) );
	Assert( (r <= 1.0) && (g <= 1.0) && (b <= 1.0) && (a <= 1.0) );

#ifdef OPENGL_SWAP_COLORS
	int col = (FastFToC(r)) | (FastFToC(g) << 8) | (FastFToC(b) << 16) | (FastFToC(a) << 24);
#else
	int col = (FastFToC(b)) | (FastFToC(g) << 8) | (FastFToC(r) << 16) | (FastFToC(a) << 24);
#endif
	*(int*)m_pCurrColor = col;
}

inline void	CVertexBuilder::Color4fv( const float *rgba )
{
	Assert(rgba);
	Assert( m_pColor && m_pCurrColor );
	Assert( IsFinite(rgba[0]) && IsFinite(rgba[1]) && IsFinite(rgba[2]) && IsFinite(rgba[3]) );
	Assert( (rgba[0] >= 0.0) && (rgba[1] >= 0.0) && (rgba[2] >= 0.0) && (rgba[3] >= 0.0) );
	Assert( (rgba[0] <= 1.0) && (rgba[1] <= 1.0) && (rgba[2] <= 1.0) && (rgba[3] <= 1.0) );

#ifdef OPENGL_SWAP_COLORS
	int col = (FastFToC(rgba[0])) | (FastFToC(rgba[1]) << 8) | (FastFToC(rgba[2]) << 16) | (FastFToC(rgba[3]) << 24);
#else
	int col = (FastFToC(rgba[2])) | (FastFToC(rgba[1]) << 8) | (FastFToC(rgba[0]) << 16) | (FastFToC(rgba[3]) << 24);
#endif
	*(int*)m_pCurrColor = col;
}


//-----------------------------------------------------------------------------
// Faster versions of color
//-----------------------------------------------------------------------------

// note that on the OSX target (OpenGL) whenever there is vertex data being written as bytes - they need to be written in R,G,B,A memory order

inline void CVertexBuilder::Color3ub( unsigned char r, unsigned char g, unsigned char b )
{
	Assert( m_pColor && m_pCurrColor );
	#ifdef OPENGL_SWAP_COLORS
		int col = r | (g << 8) | (b << 16) | 0xFF000000;	// r, g, b, a in memory
	#else
		int col = b | (g << 8) | (r << 16) | 0xFF000000;
	#endif
	
	*(int*)m_pCurrColor = col;
}

inline void CVertexBuilder::Color3ubv( unsigned char const* rgb )
{
	Assert(rgb);
	Assert( m_pColor && m_pCurrColor );
	#ifdef OPENGL_SWAP_COLORS
		int col = rgb[0] | (rgb[1] << 8) | (rgb[2] << 16) | 0xFF000000;	// r, g, b, a in memory
	#else
		int col = rgb[2] | (rgb[1] << 8) | (rgb[0] << 16) | 0xFF000000;
	#endif

	*(int*)m_pCurrColor = col;
}

inline void CVertexBuilder::Color4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a )
{
	Assert( m_pColor && m_pCurrColor );
	#ifdef OPENGL_SWAP_COLORS
		int col = r | (g << 8) | (b << 16) | (a << 24);	// r, g, b, a in memory
	#else
		int col = b | (g << 8) | (r << 16) | (a << 24);
	#endif

	*(int*)m_pCurrColor = col;
}

inline void CVertexBuilder::Color4ubv( unsigned char const* rgba )
{
	Assert( rgba );
	Assert( m_pColor && m_pCurrColor );
	#ifdef OPENGL_SWAP_COLORS
		int col = rgba[0] | (rgba[1] << 8) | (rgba[2] << 16) | (rgba[3] << 24);	// r, g, b, a in memory
	#else
		int col = rgba[2] | (rgba[1] << 8) | (rgba[0] << 16) | (rgba[3] << 24);
	#endif
	*(int*)m_pCurrColor = col;
}

inline void	CVertexBuilder::Specular3f( float r, float g, float b )
{
	Assert( m_pSpecular );
	Assert( IsFinite(r) && IsFinite(g) && IsFinite(b) );
	Assert( (r >= 0.0) && (g >= 0.0) && (b >= 0.0) );
	Assert( (r <= 1.0) && (g <= 1.0) && (b <= 1.0) );

	unsigned char* pSpecular = &m_pSpecular[m_nCurrentVertex * m_VertexSize_Specular];
#ifdef OPENGL_SWAP_COLORS
	int col = (FastFToC(r)) | (FastFToC(g) << 8) | (FastFToC(b) << 16) | 0xFF000000;
#else
	int col = (FastFToC(b)) | (FastFToC(g) << 8) | (FastFToC(r) << 16) | 0xFF000000;
#endif
	*(int*)pSpecular = col;
}

inline void	CVertexBuilder::Specular3fv( const float *rgb )
{
	Assert(rgb);
	Assert( m_pSpecular );
	Assert( IsFinite(rgb[0]) && IsFinite(rgb[1]) && IsFinite(rgb[2]) );
	Assert( (rgb[0] >= 0.0) && (rgb[1] >= 0.0) && (rgb[2] >= 0.0) );
	Assert( (rgb[0] <= 1.0) && (rgb[1] <= 1.0) && (rgb[2] <= 1.0) );

	unsigned char* pSpecular = &m_pSpecular[m_nCurrentVertex * m_VertexSize_Specular];
#ifdef OPENGL_SWAP_COLORS
	int col = (FastFToC(rgb[0])) | (FastFToC(rgb[1]) << 8) | (FastFToC(rgb[2]) << 16) | 0xFF000000;
#else
	int col = (FastFToC(rgb[2])) | (FastFToC(rgb[1]) << 8) | (FastFToC(rgb[0]) << 16) | 0xFF000000;
#endif
	*(int*)pSpecular = col;
}

inline void	CVertexBuilder::Specular4f( float r, float g, float b, float a )
{
	Assert( m_pSpecular );
	Assert( IsFinite(r) && IsFinite(g) && IsFinite(b) && IsFinite(a) );
	Assert( (r >= 0.0) && (g >= 0.0) && (b >= 0.0) && (a >= 0.0) );
	Assert( (r <= 1.0) && (g <= 1.0) && (b <= 1.0) && (a <= 1.0f) );

	unsigned char* pSpecular = &m_pSpecular[m_nCurrentVertex * m_VertexSize_Specular];
#ifdef OPENGL_SWAP_COLORS
	int col = (FastFToC(r)) | (FastFToC(g) << 8) | (FastFToC(b) << 16) | (FastFToC(a) << 24);
#else
	int col = (FastFToC(b)) | (FastFToC(g) << 8) | (FastFToC(r) << 16) | (FastFToC(a) << 24);
#endif
	*(int*)pSpecular = col;
}

inline void	CVertexBuilder::Specular4fv( const float *rgb )
{
	Assert(rgb);
	Assert( m_pSpecular );
	Assert( IsFinite(rgb[0]) && IsFinite(rgb[1]) && IsFinite(rgb[2]) && IsFinite(rgb[3]) );
	Assert( (rgb[0] >= 0.0) && (rgb[1] >= 0.0) && (rgb[2] >= 0.0) && (rgb[3] >= 0.0) );
	Assert( (rgb[0] <= 1.0) && (rgb[1] <= 1.0) && (rgb[2] <= 1.0) && (rgb[3] <= 1.0) );

	unsigned char* pSpecular = &m_pSpecular[m_nCurrentVertex * m_VertexSize_Specular];
#ifdef OPENGL_SWAP_COLORS
	int col = (FastFToC(rgb[0])) | (FastFToC(rgb[1]) << 8) | (FastFToC(rgb[2]) << 16) | (FastFToC(rgb[3]) << 24);
#else
	int col = (FastFToC(rgb[2])) | (FastFToC(rgb[1]) << 8) | (FastFToC(rgb[0]) << 16) | (FastFToC(rgb[3]) << 24);
#endif
	*(int*)pSpecular = col;
}

inline void CVertexBuilder::Specular3ub( unsigned char r, unsigned char g, unsigned char b )
{
	Assert( m_pSpecular );
	unsigned char *pSpecular = &m_pSpecular[m_nCurrentVertex * m_VertexSize_Specular];

	#ifdef OPENGL_SWAP_COLORS
		int col = r | (g << 8) | (b << 16) | 0xFF000000;	// r, g, b, a in memory
	#else
		int col = b | (g << 8) | (r << 16) | 0xFF000000;
	#endif
	
	*(int*)pSpecular = col;
}

inline void CVertexBuilder::Specular3ubv( unsigned char const *c )
{
	Assert( m_pSpecular );
	unsigned char *pSpecular = &m_pSpecular[m_nCurrentVertex * m_VertexSize_Specular];

	#ifdef OPENGL_SWAP_COLORS
		int col = c[0] | (c[1] << 8) | (c[2] << 16) | 0xFF000000;	// r, g, b, a in memory
	#else
		int col = c[2] | (c[1] << 8) | (c[0] << 16) | 0xFF000000;
	#endif
	
	*(int*)pSpecular = col;
}

inline void CVertexBuilder::Specular4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a )
{
	Assert( m_pSpecular );
	unsigned char *pSpecular = &m_pSpecular[m_nCurrentVertex * m_VertexSize_Specular];

	#ifdef OPENGL_SWAP_COLORS
		int col = r | (g << 8) | (b << 16) | (a << 24);	// r, g, b, a in memory
	#else
		int col = b | (g << 8) | (r << 16) | (a << 24);
	#endif

	*(int*)pSpecular = col;
}

inline void CVertexBuilder::Specular4ubv( unsigned char const *c )
{
	Assert( m_pSpecular );
	unsigned char *pSpecular = &m_pSpecular[m_nCurrentVertex * m_VertexSize_Specular];

	#ifdef OPENGL_SWAP_COLORS
		int col = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24);
	#else
		int col = c[2] | (c[1] << 8) | (c[0] << 16) | (c[3] << 24);
	#endif
	
	*(int*)pSpecular = col;
}


//-----------------------------------------------------------------------------
// Texture coordinate setting methods
//-----------------------------------------------------------------------------
inline void CVertexBuilder::TexCoord1f( int nStage, float s )
{
	Assert( m_pTexCoord[nStage] && m_pCurrTexCoord[nStage] );
	Assert( IsFinite(s) );

	float *pDst = m_pCurrTexCoord[nStage];
	*pDst = s;
}

inline void	CVertexBuilder::TexCoord2f( int nStage, float s, float t )
{
	Assert( m_pTexCoord[nStage] && m_pCurrTexCoord[nStage] );
	Assert( IsFinite(s) && IsFinite(t) );

	float *pDst = m_pCurrTexCoord[nStage];
	*pDst++ = s;
	*pDst = t;
}

inline void	CVertexBuilder::TexCoord2fv( int nStage, const float *st )
{
	Assert(st);
	Assert( m_pTexCoord[nStage] && m_pCurrTexCoord[nStage] );
	Assert( IsFinite(st[0]) && IsFinite(st[1]) );

	float *pDst = m_pCurrTexCoord[nStage];
	*pDst++ = *st++;
	*pDst = *st;
}

inline void	CVertexBuilder::TexCoord3f( int stage, float s, float t, float u )
{
	// Tried to add too much!
	Assert( m_pTexCoord[stage] && m_pCurrTexCoord[stage] );
	Assert( IsFinite(s) && IsFinite(t)  && IsFinite(u) );
	float *pDst = m_pCurrTexCoord[stage];
	*pDst++ = s;
	*pDst++ = t;
	*pDst = u;
}

inline void	CVertexBuilder::TexCoord3fv( int stage, const float *stu )
{
	Assert(stu);
	Assert( m_pTexCoord[stage] && m_pCurrTexCoord[stage] );
	Assert( IsFinite(stu[0]) && IsFinite(stu[1]) && IsFinite(stu[2]) );

	float *pDst = m_pCurrTexCoord[stage];
	*pDst++ = *stu++;
	*pDst++ = *stu++;
	*pDst = *stu;
}

inline void	CVertexBuilder::TexCoord4f( int stage, float s, float t, float u, float v )
{
	// Tried to add too much!
	Assert( m_pTexCoord[stage] && m_pCurrTexCoord[stage] );
	Assert( IsFinite(s) && IsFinite(t)  && IsFinite(u) );
	float *pDst = m_pCurrTexCoord[stage];
	*pDst++ = s;
	*pDst++ = t;
	*pDst++ = u;
	*pDst = v;
}

inline void	CVertexBuilder::TexCoord4fv( int stage, const float *stuv )
{
	Assert(stuv);
	Assert( m_pTexCoord[stage] && m_pCurrTexCoord[stage] );
	Assert( IsFinite(stuv[0]) && IsFinite(stuv[1]) && IsFinite(stuv[2]) );

	float *pDst = m_pCurrTexCoord[stage];
	*pDst++ = *stuv++;
	*pDst++ = *stuv++;
	*pDst++ = *stuv++;
	*pDst = *stuv;
}


inline void CVertexBuilder::TexCoordSubRect2f( int stage, float s, float t, float offsetS, float offsetT, float scaleS, float scaleT )
{
	Assert( m_pTexCoord[stage] && m_pCurrTexCoord[stage] );
	Assert( IsFinite(s) && IsFinite(t) );

	float *pDst = m_pCurrTexCoord[stage];
	*pDst++ = ( s * scaleS ) + offsetS;
	*pDst = ( t * scaleT ) + offsetT;
}

inline void CVertexBuilder::TexCoordSubRect2fv( int stage, const float *st, const float *offset, const float *scale )
{
	Assert(st);
	Assert( m_pTexCoord[stage] && m_pCurrTexCoord[stage] );
	Assert( IsFinite(st[0]) && IsFinite(st[1]) );

	float *pDst = m_pCurrTexCoord[stage];
	*pDst++ = ( *st++ * *scale++ ) + *offset++;
	*pDst = ( *st * *scale ) + *offset;
}


//-----------------------------------------------------------------------------
// Tangent space setting methods
//-----------------------------------------------------------------------------
inline void CVertexBuilder::TangentS3f( float sx, float sy, float sz )
{
	Assert( m_pTangentS );
	Assert( IsFinite(sx) && IsFinite(sy) && IsFinite(sz) );

	float* pTangentS = OffsetFloatPointer( m_pTangentS, m_nCurrentVertex, m_VertexSize_TangentS );
	*pTangentS++ = sx;
	*pTangentS++ = sy;
	*pTangentS = sz;
}

inline void CVertexBuilder::TangentS3fv( const float* s )
{
	Assert( s );
	Assert( m_pTangentS );
	Assert( IsFinite(s[0]) && IsFinite(s[1]) && IsFinite(s[2]) );

	float* pTangentS = OffsetFloatPointer( m_pTangentS, m_nCurrentVertex, m_VertexSize_TangentS );
	*pTangentS++ = *s++;
	*pTangentS++ = *s++;
	*pTangentS = *s;
}

inline void CVertexBuilder::TangentT3f( float tx, float ty, float tz )
{
	Assert( m_pTangentT );
	Assert( IsFinite(tx) && IsFinite(ty) && IsFinite(tz) );

	float* pTangentT = OffsetFloatPointer( m_pTangentT, m_nCurrentVertex, m_VertexSize_TangentT );
	*pTangentT++ = tx;
	*pTangentT++ = ty;
	*pTangentT = tz;
}

inline void CVertexBuilder::TangentT3fv( const float* t )
{
	Assert( t );
	Assert( m_pTangentT );
	Assert( IsFinite(t[0]) && IsFinite(t[1]) && IsFinite(t[2]) );

	float* pTangentT = OffsetFloatPointer( m_pTangentT, m_nCurrentVertex, m_VertexSize_TangentT );
	*pTangentT++ = *t++;
	*pTangentT++ = *t++;
	*pTangentT = *t;
}


//-----------------------------------------------------------------------------
// Wrinkle setting methods
//-----------------------------------------------------------------------------
inline void CVertexBuilder::Wrinkle1f( float flWrinkle )
{
	Assert( m_pWrinkle );
	Assert( IsFinite(flWrinkle) );

	float *pWrinkle = OffsetFloatPointer( m_pWrinkle, m_nCurrentVertex, m_VertexSize_Wrinkle );
	*pWrinkle = flWrinkle;
}


//-----------------------------------------------------------------------------
// Bone weight setting methods
//-----------------------------------------------------------------------------
inline void CVertexBuilder::BoneWeight( int idx, float weight )
{
	Assert( m_pBoneWeight );
	Assert( IsFinite( weight ) );
	Assert( idx >= 0 );
	AssertOnce( m_NumBoneWeights == 2 );

	// This test is here because we store N-1 bone weights (the Nth is computed in
	// the vertex shader as "1 - C", where C is the sum of the (N-1) other weights)
	if ( idx < m_NumBoneWeights )
	{
		float* pBoneWeight = OffsetFloatPointer( m_pBoneWeight, m_nCurrentVertex, m_VertexSize_BoneWeight );
		pBoneWeight[idx] = weight;
	}
}

static int sg_IndexSwap[4] = { 2, 1, 0, 3 };

inline void CVertexBuilder::BoneMatrix( int idx, int matrixIdx )
{
	Assert( m_pBoneMatrixIndex );
	Assert( idx >= 0 );
	Assert( idx < 4 );

	// garymcthack
	if ( matrixIdx == BONE_MATRIX_INDEX_INVALID )
	{
		matrixIdx = 0;
	}
	Assert( (matrixIdx >= 0) && (matrixIdx < 53) );

#ifdef OPENGL_SWAP_COLORS
	idx = sg_IndexSwap[idx];
#endif
	
#ifndef NEW_SKINNING
	unsigned char* pBoneMatrix = &m_pBoneMatrixIndex[m_nCurrentVertex * m_VertexSize_BoneMatrixIndex];
	if ( IsX360() )
	{
		// store sequentially as wzyx order, gpu delivers as xyzw
		idx = 3-idx;
	}
	pBoneMatrix[idx] = (unsigned char)matrixIdx;
#else
	float* pBoneMatrix = &m_pBoneMatrixIndex[m_nCurrentVertex * m_VertexSize_BoneMatrixIndex];
	pBoneMatrix[idx] = matrixIdx;
#endif
}

//-----------------------------------------------------------------------------
// Templatized bone weight setting methods which support compressed vertices
//-----------------------------------------------------------------------------
template <VertexCompressionType_t T> inline void CVertexBuilder::CompressedBoneWeight3fv( const float * pWeights )
{
	Assert( T == m_CompressionType );
	Assert( m_pBoneWeight );
	Assert( pWeights );

	float *pDestWeights = OffsetFloatPointer( m_pBoneWeight, m_nCurrentVertex, m_VertexSize_BoneWeight );

	if ( T == VERTEX_COMPRESSION_ON )
	{
		// Quantize to 15 bits per weight (we use D3DDECLTYPE_SHORT2)
		// NOTE: we perform careful normalization (weights sum to 1.0f in the vertex shader), so
		// as to avoid cracking at boundaries between meshes with different numbers of weights
		// per vertex. For example, (1) needs to yield the same normalized weights as (1,0),
		// and (0.5,0.49) needs to normalize the same normalized weights as (0.5,0.49,0).
		// The key is that values which are *computed* in the shader (e.g. the second weight
		// in a 2-weight mesh) must exactly equal values which are *read* from the vertex
		// stream (e.g. the second weight in a 3-weight mesh).

		// Only 1 or 2 weights (SHORT2N) supported for compressed verts so far
		Assert( m_NumBoneWeights <= 2 );

		const int WEIGHT0_SHIFT = IsX360() ? 16 : 0;
		const int WEIGHT1_SHIFT = IsX360() ? 0 : 16;
		unsigned int *weights = (unsigned int *)pDestWeights;

		// We scale our weights so that they sum to 32768, then subtract 1 (which gets added
		// back in the shader), because dividing by 32767 introduces nasty rounding issues.
		Assert( IsFinite( pWeights[0] ) && ( pWeights[0] >= 0.0f ) && ( pWeights[0] <= 1.0f ) );
		unsigned int weight0 = Float2Int( pWeights[0] * 32768.0f );
		*weights = ( 0x0000FFFF & (weight0 - 1) ) << WEIGHT0_SHIFT;

#ifdef DEBUG
		if ( m_NumBoneWeights == 1 )
		{
			// Double-check the validity of the values that were passed in
			Assert( IsFinite( pWeights[1] ) && ( pWeights[1] >= 0.0f ) && ( pWeights[1] <= 1.0f ) );
			unsigned int weight1 = Float2Int( pWeights[1] * 32768.0f );
			Assert( ( weight0 + weight1 ) <= 32768 );
		}
#endif

		if ( m_NumBoneWeights > 1 )
		{
			// This path for 3 weights per vert (2 are stored and the 3rd is computed
			// in the shader - we do post-quantization normalization here in such a
			// way as to avoid mesh-boundary cracking)
			Assert( m_NumBoneWeights == 2 );
			Assert( IsFinite( pWeights[1] ) && ( pWeights[1] >= 0.0f ) && ( pWeights[1] <= 1.0f ) );
			Assert( IsFinite( pWeights[2] ) && ( pWeights[2] >= 0.0f ) && ( pWeights[2] <= 1.0f ) );
			unsigned int weight1 = Float2Int( pWeights[1] * 32768.0f );
			unsigned int weight2 = Float2Int( pWeights[2] * 32768.0f );
			Assert( ( weight0 + weight1 + weight2 ) <= 32768 );
			unsigned int residual = 32768 - ( weight0 + weight1 + weight2 );
			weight1 += residual; // Normalize
			*weights |= ( 0x0000FFFF & ( weight1 - 1 ) ) << WEIGHT1_SHIFT;
		}
	}
	else	// Uncompressed path
	{
		pDestWeights[0] = pWeights[0];
		pDestWeights[1] = pWeights[1];
	}
}

//-----------------------------------------------------------------------------
// Generic per-vertex data setting method
//-----------------------------------------------------------------------------
inline void CVertexBuilder::UserData( const float* pData )
{
	Assert( m_CompressionType == VERTEX_COMPRESSION_NONE ); // Use the templatized version if you want to support compression
	Assert( pData );

	int userDataSize = 4; // garymcthack
	float *pUserData = OffsetFloatPointer( m_pUserData, m_nCurrentVertex, m_VertexSize_UserData );
	memcpy( pUserData, pData, sizeof( float ) * userDataSize );
}

//-----------------------------------------------------------------------------
// Templatized generic per-vertex data setting method which supports compressed vertices
//-----------------------------------------------------------------------------
template <VertexCompressionType_t T> inline void CVertexBuilder::CompressedUserData( const float* pData )
{
	Assert( T == m_CompressionType );
	Assert( pData );
	// This is always in fact a tangent vector, not generic 'userdata'
	Assert( IsFinite(pData[0]) && IsFinite(pData[1]) && IsFinite(pData[2]) );
	Assert( pData[0] >= -1.05f && pData[0] <= 1.05f );
	Assert( pData[1] >= -1.05f && pData[1] <= 1.05f );
	Assert( pData[2] >= -1.05f && pData[2] <= 1.05f );
	Assert( pData[3] == +1.0f  || pData[3] == -1.0f );
	// FIXME: studiorender is passing in non-unit normals
	//float lengthSqd = pData[0]*pData[0] + pData[1]*pData[1] + pData[2]*pData[2];
	//Assert( lengthSqd >= 0.95f && lengthSqd <= 1.05f );

	if ( T == VERTEX_COMPRESSION_ON )
	{
		float binormalSign = pData[3];

#if		( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_SEPARATETANGENTS_SHORT2 )
		float *pUserData = OffsetFloatPointer( m_pUserData, m_nCurrentVertex, m_VertexSize_UserData );
		PackNormal_SHORT2( pData, (unsigned int *)pUserData, binormalSign );
#else //( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 ) 
		// FIXME: add a combined CompressedNormalAndTangent() accessor, to avoid reading back from write-combined memory here
		// The normal should have already been written into the lower 16
		// bits - here, we OR in the tangent into the upper 16 bits
		unsigned int existingNormalData = *(unsigned int *)m_pCurrNormal;
		Assert( ( existingNormalData & 0xFFFF0000 ) == 0 );
#ifdef _DEBUG
		Assert( m_bWrittenNormal == true );
		m_bWrittenUserData = true;
#endif
		bool bIsTangent = true;
		unsigned int tangentData = 0;
		PackNormal_UBYTE4( pData, &tangentData, bIsTangent, binormalSign );
		*(unsigned int *)m_pCurrNormal = existingNormalData | tangentData;
#endif
	}
	else
	{
		int userDataSize = 4; // garymcthack
		float *pUserData = OffsetFloatPointer( m_pUserData, m_nCurrentVertex, m_VertexSize_UserData );
		memcpy( pUserData, pData, sizeof( float ) * userDataSize );
	}
}


//-----------------------------------------------------------------------------
//
// Helper class used to define index buffers
//
//-----------------------------------------------------------------------------
class CIndexBuilder : private IndexDesc_t
{
public:
	CIndexBuilder();
	CIndexBuilder( IIndexBuffer *pIndexBuffer, MaterialIndexFormat_t fmt = MATERIAL_INDEX_FORMAT_UNKNOWN );
	~CIndexBuilder();

	// Begins, ends modification of the index buffer (returns true if the lock succeeded)
	// A lock may not succeed if append is set to true and there isn't enough room
	// NOTE: Append is only used with dynamic index buffers; it's ignored for static buffers
	bool Lock( int nMaxIndexCount, int nIndexOffset, bool bAppend = false );
	void Unlock();

	// Spews the current data
	// NOTE: Can only be called during a lock/unlock block
	void SpewData();

	// Returns the number of indices we can fit into the buffer without needing to discard
	int GetRoomRemaining() const;

	// Binds this index buffer
	void Bind( IMatRenderContext *pContext );

	// Returns the byte offset
	int Offset() const;

	// Begins, ends modification of the index buffer
	// NOTE: IndexOffset is the number to add to all indices written into the buffer;
	// useful when using dynamic vertex buffers.
	void Begin( IIndexBuffer *pIndexBuffer, int nMaxIndexCount, int nIndexOffset = 0 );
	void End( bool bSpewData = false );

	// Locks the index buffer to modify existing data
	// Passing nVertexCount == -1 says to lock all the vertices for modification.
	// Pass 0 for nIndexCount to not lock the index buffer.
	void BeginModify( IIndexBuffer *pIndexBuffer, int nFirstIndex = 0, int nIndexCount = 0, int nIndexOffset = 0 );
	void EndModify( bool bSpewData = false );

	// returns the number of indices
	int	IndexCount() const;

	// Returns the total number of indices across all Locks()
	int TotalIndexCount() const;

	// Resets the mesh builder so it points to the start of everything again
	void Reset();

	// Selects the nth Index 
	void SelectIndex( int nBufferIndex );

	// Advances the current index by one
	void AdvanceIndex();
	void AdvanceIndices( int nIndexCount );

	int GetCurrentIndex();
	int GetFirstIndex() const;

	unsigned short const* Index() const;

	// Used to define the indices (only used if you aren't using primitives)
	void Index( unsigned short nIndex );

	// Fast Index! No need to call advance index, and no random access allowed
	void FastIndex( unsigned short nIndex );

	// NOTE: This version is the one you really want to achieve write-combining;
	// Write combining only works if you write in 4 bytes chunks.
	void FastIndex2( unsigned short nIndex1, unsigned short nIndex2 );

	// Generates indices for a particular primitive type
	void GenerateIndices( MaterialPrimitiveType_t primitiveType, int nIndexCount );

	// FIXME: Remove! Backward compat so we can use this from a CMeshBuilder.
	void AttachBegin( IMesh* pMesh, int nMaxIndexCount, const MeshDesc_t &desc );
	void AttachEnd();
	void AttachBeginModify( IMesh* pMesh, int nFirstIndex, int nIndexCount, const MeshDesc_t &desc );
	void AttachEndModify();

	void FastTriangle( int startVert );
	void FastQuad( int startVert );
	void FastPolygon( int startVert, int numTriangles );
	void FastPolygonList( int startVert, int *pVertexCount, int polygonCount );
	void FastIndexList( const unsigned short *pIndexList, int startVert, int indexCount );

private:
	// The mesh we're modifying
	IIndexBuffer	*m_pIndexBuffer;

	// Max number of indices
	int				m_nMaxIndexCount;

	// Number of indices
	int				m_nIndexCount;

	// Offset to add to each index as it's written into the buffer
	int				m_nIndexOffset;

	// The current index
	mutable int		m_nCurrentIndex;

	// Total number of indices appended
	int				m_nTotalIndexCount;

	// First index buffer offset + first index
	unsigned int	m_nBufferOffset;
	unsigned int	m_nBufferFirstIndex;

	// Used to make sure Begin/End calls and BeginModify/EndModify calls match.
	bool			m_bModify;
};


//-----------------------------------------------------------------------------
//
// Inline methods related to CIndexBuilder
//
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
inline CIndexBuilder::CIndexBuilder() : m_pIndexBuffer(0), m_nIndexCount(0), 
	m_nCurrentIndex(0),	m_nMaxIndexCount(0)
{
	m_nTotalIndexCount = 0;
	m_nBufferOffset = INVALID_BUFFER_OFFSET;
	m_nBufferFirstIndex = 0;
#ifdef _DEBUG
	m_bModify = false;
#endif
}

inline CIndexBuilder::CIndexBuilder( IIndexBuffer *pIndexBuffer, MaterialIndexFormat_t fmt )
{
	m_pIndexBuffer = pIndexBuffer;
	m_nBufferOffset = INVALID_BUFFER_OFFSET;
	m_nBufferFirstIndex = 0;
	m_nIndexCount = 0;
	m_nCurrentIndex = 0;
	m_nMaxIndexCount = 0;
	m_nTotalIndexCount = 0;
	if ( m_pIndexBuffer->IsDynamic() )
	{
		m_pIndexBuffer->BeginCastBuffer( fmt );
	}
	else
	{
		Assert( m_pIndexBuffer->IndexFormat() == fmt );
	}
#ifdef _DEBUG
	m_bModify = false;
#endif
}

inline CIndexBuilder::~CIndexBuilder()
{
	if ( m_pIndexBuffer && m_pIndexBuffer->IsDynamic() )
	{
		m_pIndexBuffer->EndCastBuffer();
	}
}


//-----------------------------------------------------------------------------
// Begins, ends modification of the index buffer
//-----------------------------------------------------------------------------
inline bool CIndexBuilder::Lock( int nMaxIndexCount, int nIndexOffset, bool bAppend )
{
 	Assert( m_pIndexBuffer );
	m_bModify = false;
	m_nIndexOffset = nIndexOffset;
	m_nMaxIndexCount = nMaxIndexCount;
	bool bFirstLock = ( m_nBufferOffset == INVALID_BUFFER_OFFSET );
	if ( bFirstLock )
	{
		bAppend = false;
	}
	if ( !bAppend )
	{
		m_nTotalIndexCount = 0;
	}
	Reset();

	// Lock the index buffer
	if ( !m_pIndexBuffer->Lock( m_nMaxIndexCount, bAppend, *this ) )
	{
		m_nMaxIndexCount = 0;
		return false;
	}

	if ( bFirstLock )
	{
		m_nBufferOffset = m_nOffset;
		m_nBufferFirstIndex = m_nFirstIndex;
	}

	return true;
}

inline void CIndexBuilder::Unlock()
{
	Assert( !m_bModify && m_pIndexBuffer );

	m_pIndexBuffer->Unlock( m_nIndexCount, *this );
	m_nTotalIndexCount += m_nIndexCount;

	m_nMaxIndexCount = 0;

#ifdef _DEBUG
	// Null out our data...
	memset( (IndexDesc_t*)this, 0, sizeof(IndexDesc_t) );
#endif
}

inline void CIndexBuilder::SpewData()
{
	m_pIndexBuffer->Spew( m_nIndexCount, *this );
}


//-----------------------------------------------------------------------------
// Binds this index buffer
//-----------------------------------------------------------------------------
inline void CIndexBuilder::Bind( IMatRenderContext *pContext )
{
	if ( m_pIndexBuffer && ( m_nBufferOffset != INVALID_BUFFER_OFFSET ) )
	{
		pContext->BindIndexBuffer( m_pIndexBuffer, m_nBufferOffset );
	}
	else
	{
		pContext->BindIndexBuffer( NULL, 0 );
	}
}


//-----------------------------------------------------------------------------
// Returns the byte offset
//-----------------------------------------------------------------------------
inline int CIndexBuilder::Offset() const
{
	return m_nBufferOffset;
}

inline int CIndexBuilder::GetFirstIndex() const
{
	return m_nBufferFirstIndex;
}


//-----------------------------------------------------------------------------
// Begins, ends modification of the index buffer
//-----------------------------------------------------------------------------
inline void CIndexBuilder::Begin( IIndexBuffer *pIndexBuffer, int nMaxIndexCount, int nIndexOffset )
{
	Assert( pIndexBuffer && (!m_pIndexBuffer) );

	m_pIndexBuffer = pIndexBuffer;
	m_nIndexCount = 0;
	m_nMaxIndexCount = nMaxIndexCount;
	m_nIndexOffset = nIndexOffset;

	m_bModify = false;

	// Lock the index buffer
	m_pIndexBuffer->Lock( m_nMaxIndexCount, false, *this );

	// Point to the start of the buffers..
	Reset();
}

inline void CIndexBuilder::End( bool bSpewData )
{
	// Make sure they called Begin()
	Assert( !m_bModify );

	if ( bSpewData )
	{
		m_pIndexBuffer->Spew( m_nIndexCount, *this );
	}

	// Unlock our buffers
	m_pIndexBuffer->Unlock( m_nIndexCount, *this );

	m_pIndexBuffer = 0;
	m_nMaxIndexCount = 0;

#ifdef _DEBUG
	// Null out our data...
	memset( (IndexDesc_t*)this, 0, sizeof(IndexDesc_t) );
#endif
}


//-----------------------------------------------------------------------------
// Begins, ends modification of an existing index buffer which has already been filled out
//-----------------------------------------------------------------------------
inline void CIndexBuilder::BeginModify( IIndexBuffer* pIndexBuffer, int nFirstIndex, int nIndexCount, int nIndexOffset )
{
	m_pIndexBuffer = pIndexBuffer;
	m_nIndexCount = nIndexCount;
	m_nMaxIndexCount = nIndexCount;
	m_nIndexOffset = nIndexOffset;
	m_bModify = true;

	// Lock the vertex and index buffer
	m_pIndexBuffer->ModifyBegin( false, nFirstIndex, nIndexCount, *this );

	// Point to the start of the buffers..
	Reset();
}

inline void CIndexBuilder::EndModify( bool bSpewData )
{
	Assert( m_pIndexBuffer );
	Assert( m_bModify );	// Make sure they called BeginModify.

	if ( bSpewData )
	{
		m_pIndexBuffer->Spew( m_nIndexCount, *this );
	}

	// Unlock our buffers
	m_pIndexBuffer->ModifyEnd( *this );

	m_pIndexBuffer = 0;
	m_nMaxIndexCount = 0;

#ifdef _DEBUG
	// Null out our data...
	memset( (IndexDesc_t*)this, 0, sizeof(IndexDesc_t) );
#endif
}


//-----------------------------------------------------------------------------
// FIXME: Remove! Backward compat so we can use this from a CMeshBuilder.
//-----------------------------------------------------------------------------
inline void CIndexBuilder::AttachBegin( IMesh* pMesh, int nMaxIndexCount, const MeshDesc_t &desc )
{
	m_pIndexBuffer = pMesh;
	m_nIndexCount = 0;
	m_nMaxIndexCount = nMaxIndexCount;

	m_bModify = false;

	// Copy relevant data from the mesh desc
	m_nIndexOffset = desc.m_nFirstVertex;
	m_pIndices = desc.m_pIndices;
	m_nIndexSize = desc.m_nIndexSize;

	// Point to the start of the buffers..
	Reset();
}

inline void CIndexBuilder::AttachEnd()
{
	Assert( m_pIndexBuffer );
	Assert( !m_bModify );	// Make sure they called AttachBegin.

	m_pIndexBuffer = 0;
	m_nMaxIndexCount = 0;

#ifdef _DEBUG
	// Null out our data...
	memset( (IndexDesc_t*)this, 0, sizeof(IndexDesc_t) );
#endif
}

inline void CIndexBuilder::AttachBeginModify( IMesh* pMesh, int nFirstIndex, int nIndexCount, const MeshDesc_t &desc )
{
	m_pIndexBuffer = pMesh;
	m_nIndexCount = nIndexCount;
	m_nMaxIndexCount = nIndexCount;
	m_bModify = true;

	// Copy relevant data from the mesh desc
	m_nIndexOffset = desc.m_nFirstVertex;
	m_pIndices = desc.m_pIndices;
	m_nIndexSize = desc.m_nIndexSize;

	// Point to the start of the buffers..
	Reset();
}

inline void CIndexBuilder::AttachEndModify()
{
	Assert( m_pIndexBuffer );
	Assert( m_bModify );	// Make sure they called AttachBeginModify.

	m_pIndexBuffer = 0;
	m_nMaxIndexCount = 0;

#ifdef _DEBUG
	// Null out our data...
	memset( (IndexDesc_t*)this, 0, sizeof(IndexDesc_t) );
#endif
}


//-----------------------------------------------------------------------------
// Resets the index buffer builder so it points to the start of everything again
//-----------------------------------------------------------------------------
inline void CIndexBuilder::Reset()
{
	m_nCurrentIndex = 0;
}


//-----------------------------------------------------------------------------
// returns the number of indices
//-----------------------------------------------------------------------------
inline int CIndexBuilder::IndexCount() const
{
	return m_nIndexCount;
}


//-----------------------------------------------------------------------------
// Returns the total number of indices across all Locks()
//-----------------------------------------------------------------------------
inline int CIndexBuilder::TotalIndexCount() const
{
	return m_nTotalIndexCount;
}


//-----------------------------------------------------------------------------
// Advances the current index
//-----------------------------------------------------------------------------
inline void CIndexBuilder::AdvanceIndex()
{
	m_nCurrentIndex += m_nIndexSize;
	if ( m_nCurrentIndex > m_nIndexCount )
	{
		m_nIndexCount = m_nCurrentIndex; 
	}
}

inline void CIndexBuilder::AdvanceIndices( int nIndices )
{
	m_nCurrentIndex += nIndices * m_nIndexSize;
	if ( m_nCurrentIndex > m_nIndexCount )
	{
		m_nIndexCount = m_nCurrentIndex; 
	}
}


//-----------------------------------------------------------------------------
// Returns the current index
//-----------------------------------------------------------------------------
inline int CIndexBuilder::GetCurrentIndex()
{
	return m_nCurrentIndex;
}

inline unsigned short const* CIndexBuilder::Index() const
{
	Assert( m_nCurrentIndex < m_nMaxIndexCount );
	return &m_pIndices[m_nCurrentIndex];
}

inline void CIndexBuilder::SelectIndex( int nIndex )
{
	Assert( ( nIndex >= 0 ) && ( nIndex < m_nIndexCount ) );
	m_nCurrentIndex = nIndex * m_nIndexSize;
}


//-----------------------------------------------------------------------------
// Used to write data into the index buffer
//-----------------------------------------------------------------------------
inline void CIndexBuilder::Index( unsigned short nIndex )
{
	Assert( m_pIndices );
	Assert( m_nCurrentIndex < m_nMaxIndexCount );
	m_pIndices[ m_nCurrentIndex ] = (unsigned short)( m_nIndexOffset + nIndex );
}

// Fast Index! No need to call advance index
inline void CIndexBuilder::FastIndex( unsigned short nIndex )
{
	Assert( m_pIndices );
	Assert( m_nCurrentIndex < m_nMaxIndexCount );
	m_pIndices[m_nCurrentIndex] = (unsigned short)( m_nIndexOffset + nIndex );
	m_nCurrentIndex += m_nIndexSize;
	m_nIndexCount = m_nCurrentIndex;	
}

inline void CIndexBuilder::FastTriangle( int startVert )
{
	startVert += m_nIndexOffset;
	m_pIndices[m_nCurrentIndex+0] = startVert;
	m_pIndices[m_nCurrentIndex+1] = startVert + 1;
	m_pIndices[m_nCurrentIndex+2] = startVert + 2;

	AdvanceIndices(3);
}

inline void CIndexBuilder::FastQuad( int startVert )
{
	startVert += m_nIndexOffset;
	m_pIndices[m_nCurrentIndex+0] = startVert;
	m_pIndices[m_nCurrentIndex+1] = startVert + 1;
	m_pIndices[m_nCurrentIndex+2] = startVert + 2;
	m_pIndices[m_nCurrentIndex+3] = startVert;
	m_pIndices[m_nCurrentIndex+4] = startVert + 2;
	m_pIndices[m_nCurrentIndex+5] = startVert + 3;
	AdvanceIndices(6);
}

inline void CIndexBuilder::FastPolygon( int startVert, int triangleCount )
{
	unsigned short *pIndex = &m_pIndices[m_nCurrentIndex];
	startVert += m_nIndexOffset;
	if ( !IsX360() )
	{
		// NOTE: IndexSize is 1 or 0 (0 for alt-tab)
		// This prevents us from writing into bogus memory
		Assert( m_nIndexSize == 0 || m_nIndexSize == 1 );
		triangleCount *= m_nIndexSize;
	}
	for ( int v = 0; v < triangleCount; ++v )
	{
		*pIndex++ = startVert;
		*pIndex++ = startVert + v + 1;
		*pIndex++ = startVert + v + 2;
	}
	AdvanceIndices(triangleCount*3);
}

inline void CIndexBuilder::FastPolygonList( int startVert, int *pVertexCount, int polygonCount )
{
	unsigned short *pIndex = &m_pIndices[m_nCurrentIndex];
	startVert += m_nIndexOffset;
	int indexOut = 0;

	if ( !IsX360() )
	{
		// NOTE: IndexSize is 1 or 0 (0 for alt-tab)
		// This prevents us from writing into bogus memory
		Assert( m_nIndexSize == 0 || m_nIndexSize == 1 );
		polygonCount *= m_nIndexSize;
	}

	for ( int i = 0; i < polygonCount; i++ )
	{
		int vertexCount = pVertexCount[i];
		int triangleCount = vertexCount-2;
		for ( int v = 0; v < triangleCount; ++v )
		{
			*pIndex++ = startVert;
			*pIndex++ = startVert + v + 1;
			*pIndex++ = startVert + v + 2;
		}
		startVert += vertexCount;
		indexOut += triangleCount * 3;
	}
	AdvanceIndices(indexOut);
}

inline void CIndexBuilder::FastIndexList( const unsigned short *pIndexList, int startVert, int indexCount )
{
	unsigned short *pIndexOut = &m_pIndices[m_nCurrentIndex];
	startVert += m_nIndexOffset;
	if ( !IsX360() )
	{
		// NOTE: IndexSize is 1 or 0 (0 for alt-tab)
		// This prevents us from writing into bogus memory
		Assert( m_nIndexSize == 0 || m_nIndexSize == 1 );
		indexCount *= m_nIndexSize;
	}
	for ( int i = 0; i < indexCount; ++i )
	{
		pIndexOut[i] = startVert + pIndexList[i];
	}
	AdvanceIndices(indexCount);
}


//-----------------------------------------------------------------------------
// NOTE: This version is the one you really want to achieve write-combining;
// Write combining only works if you write in 4 bytes chunks.
//-----------------------------------------------------------------------------
inline void CIndexBuilder::FastIndex2( unsigned short nIndex1, unsigned short nIndex2 )
{
	Assert( m_pIndices );
	Assert( m_nCurrentIndex < m_nMaxIndexCount - 1 );
//	Assert( ( (int)( &m_pIndices[m_nCurrentIndex] ) & 0x3 ) == 0 );

#ifndef _X360
	unsigned int nIndices = ( (unsigned int)nIndex1 + m_nIndexOffset ) | ( ( (unsigned int)nIndex2 + m_nIndexOffset ) << 16 );
#else
	unsigned int nIndices = ( (unsigned int)nIndex2 + m_nIndexOffset ) | ( ( (unsigned int)nIndex1 + m_nIndexOffset ) << 16 );
#endif

	*(int*)( &m_pIndices[m_nCurrentIndex] ) = nIndices;
	m_nCurrentIndex += m_nIndexSize + m_nIndexSize;
	m_nIndexCount = m_nCurrentIndex;	
}


//-----------------------------------------------------------------------------
// Generates indices for a particular primitive type
//-----------------------------------------------------------------------------
inline void CIndexBuilder::GenerateIndices( MaterialPrimitiveType_t primitiveType, int nIndexCount )
{
	// FIXME: How to make this work with short vs int sized indices?
	// Don't generate indices if we've got an empty buffer
	if ( m_nIndexSize == 0 )
		return;

	int nMaxIndices = m_nMaxIndexCount - m_nCurrentIndex;
	nIndexCount = Min( nMaxIndices, nIndexCount );
	if ( nIndexCount == 0 )
		return;

	unsigned short *pIndices = &m_pIndices[m_nCurrentIndex];

	switch( primitiveType )
	{
	case MATERIAL_INSTANCED_QUADS:
		Assert(0); // Shouldn't get here (this primtype is unindexed)
		break;
	case MATERIAL_QUADS:
		GenerateQuadIndexBuffer( pIndices, nIndexCount, m_nIndexOffset );
		break;
	case MATERIAL_POLYGON:
		GeneratePolygonIndexBuffer( pIndices, nIndexCount, m_nIndexOffset );
		break;
	case MATERIAL_LINE_STRIP:
		GenerateLineStripIndexBuffer( pIndices, nIndexCount, m_nIndexOffset );
		break;
	case MATERIAL_LINE_LOOP:
		GenerateLineLoopIndexBuffer( pIndices, nIndexCount, m_nIndexOffset );
		break;
	case MATERIAL_POINTS:
		Assert(0); // Shouldn't get here (this primtype is unindexed)
		break;
	default:
		GenerateSequentialIndexBuffer( pIndices, nIndexCount, m_nIndexOffset );
		break;
	}

	AdvanceIndices( nIndexCount );
}


//-----------------------------------------------------------------------------
//
// Helper class used to define meshes
//
//-----------------------------------------------------------------------------
//class CMeshBuilder : private MeshDesc_t
// hack fixme
class CMeshBuilder : public MeshDesc_t
{
public:
	CMeshBuilder();
	~CMeshBuilder() { Assert(!m_pMesh); }		// if this fires you did a Begin() without an End()

	operator CIndexBuilder&() { return m_IndexBuilder; }

	// This must be called before Begin, if a vertex buffer with a compressed format is to be used
	void SetCompressionType( VertexCompressionType_t compressionType );

	// Locks the vertex buffer
	// (*cannot* use the Index() call below)
	void Begin( IMesh *pMesh, MaterialPrimitiveType_t type, int numPrimitives );

	// Locks the vertex buffer, can specify arbitrary index lists
	// (must use the Index() call below)
	void Begin( IMesh *pMesh, MaterialPrimitiveType_t type, int nVertexCount, int nIndexCount, int *nFirstVertex );
	void Begin( IMesh *pMesh, MaterialPrimitiveType_t type, int nVertexCount, int nIndexCount );

	// forward compat
	void Begin( IVertexBuffer *pVertexBuffer, MaterialPrimitiveType_t type, int numPrimitives );
	void Begin( IVertexBuffer *pVertexBuffer, IIndexBuffer *pIndexBuffer, MaterialPrimitiveType_t type, int nVertexCount, int nIndexCount, int *nFirstVertex );
	void Begin( IVertexBuffer *pVertexBuffer, IIndexBuffer *pIndexBuffer, MaterialPrimitiveType_t type, int nVertexCount, int nIndexCount );

	// Use this when you're done writing
	// Set bDraw to true to call m_pMesh->Draw automatically.
	void End( bool bSpewData = false, bool bDraw = false );

	// Locks the vertex buffer to modify existing data
	// Passing nVertexCount == -1 says to lock all the vertices for modification.
	// Pass 0 for nIndexCount to not lock the index buffer.
	void BeginModify( IMesh *pMesh, int nFirstVertex = 0, int nVertexCount = -1, int nFirstIndex = 0, int nIndexCount = 0 );
	void EndModify( bool bSpewData = false );

	// A helper method since this seems to be done a whole bunch.
	void DrawQuad( IMesh* pMesh, const float *v1, const float *v2, 
		const float *v3, const float *v4, unsigned char const *pColor, bool wireframe = false );

	// returns the number of indices and vertices
	int VertexCount() const;
	int	IndexCount() const;

	// Resets the mesh builder so it points to the start of everything again
	void Reset();

	// Returns the size of the vertex
	int VertexSize() { return m_ActualVertexSize; }

	// returns the data size of a given texture coordinate
	int TextureCoordinateSize( int nTexCoordNumber ) { return m_VertexSize_TexCoord[ nTexCoordNumber ]; }

	// Returns the base vertex memory pointer
	void* BaseVertexData();

	// Selects the nth Vertex and Index 
	void SelectVertex( int idx );
	void SelectIndex( int idx );

	// Given an index, point to the associated vertex
	void SelectVertexFromIndex( int idx );

	// Advances the current vertex and index by one
	void AdvanceVertex();
	template<int nFlags, int nNumTexCoords> void AdvanceVertexF();
	void AdvanceVertices( int nVerts );
	void AdvanceIndex();
	void AdvanceIndices( int nIndices );

	int GetCurrentVertex();
	int GetCurrentIndex();

	// Data retrieval...
	const float *Position() const;

	const float *Normal() const;

	unsigned int Color() const;

	unsigned char *Specular() const;

	const float *TexCoord( int stage ) const;

	const float *TangentS() const;
	const float *TangentT() const;

	const float *BoneWeight() const;
	float Wrinkle() const;

	int NumBoneWeights() const;
#ifndef NEW_SKINNING
	unsigned char *BoneMatrix() const;
#else
	float *BoneMatrix() const;
#endif
	unsigned short const *Index() const; 

	// position setting
	void Position3f( float x, float y, float z );
	void Position3fv( const float *v );

	// normal setting
	void Normal3f( float nx, float ny, float nz );
	void Normal3fv( const float *n );
	void NormalDelta3fv( const float *n );
	void NormalDelta3f( float nx, float ny, float nz );

	// normal setting (templatized for code which needs to support compressed vertices)
	template <VertexCompressionType_t T> void CompressedNormal3f( float nx, float ny, float nz );
	template <VertexCompressionType_t T> void CompressedNormal3fv( const float *n );

	// color setting
	void Color3f( float r, float g, float b );
	void Color3fv( const float *rgb );
	void Color4f( float r, float g, float b, float a );
	void Color4fv( const float *rgba );

	// Faster versions of color
	void Color3ub( unsigned char r, unsigned char g, unsigned char b );
	void Color3ubv( unsigned char const* rgb );
	void Color4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a );
	void Color4ubv( unsigned char const* rgba );

	// specular color setting
	void Specular3f( float r, float g, float b );
	void Specular3fv( const float *rgb );
	void Specular4f( float r, float g, float b, float a );
	void Specular4fv( const float *rgba );

	// Faster version of specular
	void Specular3ub( unsigned char r, unsigned char g, unsigned char b );
	void Specular3ubv( unsigned char const *c );
	void Specular4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a );
	void Specular4ubv( unsigned char const *c );

	// texture coordinate setting
	void TexCoord1f( int stage, float s );
	void TexCoord2f( int stage, float s, float t );
	void TexCoord2fv( int stage, const float *st );
	void TexCoord3f( int stage, float s, float t, float u );
	void TexCoord3fv( int stage, const float *stu );
	void TexCoord4f( int stage, float s, float t, float u, float w );
	void TexCoord4fv( int stage, const float *stuv );

	void TexCoordSubRect2f( int stage, float s, float t, float offsetS, float offsetT, float scaleS, float scaleT );
	void TexCoordSubRect2fv( int stage, const float *st, const float *offset, const float *scale );

	// tangent space 
	void TangentS3f( float sx, float sy, float sz );
	void TangentS3fv( const float *s );

	void TangentT3f( float tx, float ty, float tz );
	void TangentT3fv( const float *t );

	// Wrinkle
	void Wrinkle1f( float flWrinkle );

	// bone weights
	void BoneWeight( int idx, float weight );
	// bone weights (templatized for code which needs to support compressed vertices)
	template <VertexCompressionType_t T> void CompressedBoneWeight3fv( const float * pWeights );

	// bone matrix index
	void BoneMatrix( int idx, int matrixIndex );

	// Generic per-vertex data
	void UserData( const float *pData );
	// Generic per-vertex data (templatized for code which needs to support compressed vertices)
	template <VertexCompressionType_t T> void CompressedUserData( const float* pData );

	// Used to define the indices (only used if you aren't using primitives)
	void Index( unsigned short index );

	// NOTE: Use this one to get write combining! Much faster than the other version of FastIndex
	// Fast Index! No need to call advance index, and no random access allowed
	void FastIndex2( unsigned short nIndex1, unsigned short nIndex2 );

	// Fast Index! No need to call advance index, and no random access allowed
	void FastIndex( unsigned short index );

	// Fast Vertex! No need to call advance vertex, and no random access allowed. 
	// WARNING - these are low level functions that are intended only for use
	// in the software vertex skinner.
	void FastVertex( const ModelVertexDX7_t &vertex );
	void FastVertexSSE( const ModelVertexDX7_t &vertex );

	// store 4 dx7 vertices fast. for special sse dx7 pipeline
	void Fast4VerticesSSE( 
		ModelVertexDX7_t const *vtx_a,
		ModelVertexDX7_t const *vtx_b,
		ModelVertexDX7_t const *vtx_c,
		ModelVertexDX7_t const *vtx_d);

	void FastVertex( const ModelVertexDX8_t &vertex );
	void FastVertexSSE( const ModelVertexDX8_t &vertex );

	// Add number of verts and current vert since FastVertexxx routines do not update.
	void FastAdvanceNVertices(int n);	

#if defined( _X360 )
	void VertexDX8ToX360( const ModelVertexDX8_t &vertex );
#endif

private:
	// Computes number of verts and indices 
	void ComputeNumVertsAndIndices( int *pMaxVertices, int *pMaxIndices, 
		MaterialPrimitiveType_t type, int nPrimitiveCount );
	int IndicesFromVertices( MaterialPrimitiveType_t type, int nVertexCount ); 

	// The mesh we're modifying
	IMesh *m_pMesh;

	MaterialPrimitiveType_t m_Type;

	// Generate indices?
	bool m_bGenerateIndices;

	CIndexBuilder	m_IndexBuilder;
	CVertexBuilder	m_VertexBuilder;
};


//-----------------------------------------------------------------------------
// Forward compat
//-----------------------------------------------------------------------------
inline void CMeshBuilder::Begin( IVertexBuffer* pVertexBuffer, MaterialPrimitiveType_t type, int numPrimitives )
{
	Assert( 0 );
	//	Begin( pVertexBuffer->GetMesh(), type, numPrimitives );
}

inline void CMeshBuilder::Begin( IVertexBuffer* pVertexBuffer, IIndexBuffer *pIndexBuffer, MaterialPrimitiveType_t type, int nVertexCount, int nIndexCount, int *nFirstVertex )
{
	Assert( 0 );
	//	Begin( pVertexBuffer->GetMesh(), type, nVertexCount, nIndexCount, nFirstVertex );
}

inline void CMeshBuilder::Begin( IVertexBuffer* pVertexBuffer, IIndexBuffer *pIndexBuffer, MaterialPrimitiveType_t type, int nVertexCount, int nIndexCount )
{
	Assert( 0 );
	//	Begin( pVertexBuffer->GetMesh(), type, nVertexCount, nIndexCount );
}


//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
inline CMeshBuilder::CMeshBuilder()	: m_pMesh(0), m_bGenerateIndices(false)
{
}


//-----------------------------------------------------------------------------
// Computes the number of verts and indices based on primitive type and count
//-----------------------------------------------------------------------------
inline void CMeshBuilder::ComputeNumVertsAndIndices( int *pMaxVertices, int *pMaxIndices, 
													MaterialPrimitiveType_t type, int nPrimitiveCount )
{
	switch(type)
	{
	case MATERIAL_POINTS:
		*pMaxVertices = *pMaxIndices = nPrimitiveCount;
		break;

	case MATERIAL_LINES:
		*pMaxVertices = *pMaxIndices = nPrimitiveCount * 2;
		break;

	case MATERIAL_LINE_STRIP:
		*pMaxVertices = nPrimitiveCount + 1;
		*pMaxIndices = nPrimitiveCount * 2;
		break;

	case MATERIAL_LINE_LOOP:
		*pMaxVertices = nPrimitiveCount;
		*pMaxIndices = nPrimitiveCount * 2;
		break;

	case MATERIAL_TRIANGLES:
		*pMaxVertices = *pMaxIndices = nPrimitiveCount * 3;
		break;

	case MATERIAL_TRIANGLE_STRIP:
		*pMaxVertices = *pMaxIndices = nPrimitiveCount + 2;
		break;

	case MATERIAL_QUADS:
		*pMaxVertices = nPrimitiveCount * 4;
		*pMaxIndices = nPrimitiveCount * 6;
		break;

	case MATERIAL_INSTANCED_QUADS:
		*pMaxVertices = nPrimitiveCount;
		*pMaxIndices = 0; // This primtype is unindexed
		break;

	case MATERIAL_POLYGON:
		*pMaxVertices = nPrimitiveCount;
		*pMaxIndices = (nPrimitiveCount - 2) * 3;
		break;

	default:
		*pMaxVertices = 0;
		*pMaxIndices = 0;
		Assert(0);
	}

	// FIXME: need to get this from meshdx8.cpp, or move it to somewhere common
	Assert( *pMaxVertices <= 32768 );
	Assert( *pMaxIndices <= 32768 );
}


inline int CMeshBuilder::IndicesFromVertices( MaterialPrimitiveType_t type, int nVertexCount )
{
	switch( type )
	{
	case MATERIAL_QUADS:
		Assert( (nVertexCount & 0x3) == 0 );
		return (nVertexCount * 6) / 4;

	case MATERIAL_INSTANCED_QUADS:
		// This primtype is unindexed
		return 0;

	case MATERIAL_POLYGON:
		Assert( nVertexCount >= 3 );
		return (nVertexCount - 2) * 3;

	case MATERIAL_LINE_STRIP:
		Assert( nVertexCount >= 2 );
		return (nVertexCount - 1) * 2;

	case MATERIAL_LINE_LOOP:
		Assert( nVertexCount >= 3 );
		return nVertexCount * 2;

	default:
		return nVertexCount;
	}
}

//-----------------------------------------------------------------------------
// Specify the type of vertex compression that this CMeshBuilder will perform
//-----------------------------------------------------------------------------
inline void CMeshBuilder::SetCompressionType( VertexCompressionType_t vertexCompressionType )
{
	m_VertexBuilder.SetCompressionType( vertexCompressionType );
}

//-----------------------------------------------------------------------------
// Begins modifying the mesh
//-----------------------------------------------------------------------------
inline void CMeshBuilder::Begin( IMesh *pMesh, MaterialPrimitiveType_t type, int numPrimitives )
{
	Assert( pMesh && (!m_pMesh) );
	Assert( type != MATERIAL_HETEROGENOUS );

	m_pMesh = pMesh;
	m_bGenerateIndices = true;
	m_Type = type;

	int nMaxVertexCount, nMaxIndexCount;
	ComputeNumVertsAndIndices( &nMaxVertexCount, &nMaxIndexCount, type, numPrimitives );

	switch( type )
	{
	case MATERIAL_INSTANCED_QUADS:
		m_pMesh->SetPrimitiveType( MATERIAL_INSTANCED_QUADS );
		break;

	case MATERIAL_QUADS:
	case MATERIAL_POLYGON:
		m_pMesh->SetPrimitiveType( MATERIAL_TRIANGLES );
		break;

	case MATERIAL_LINE_STRIP:
	case MATERIAL_LINE_LOOP:
		m_pMesh->SetPrimitiveType( MATERIAL_LINES );
		break;

	default:
		m_pMesh->SetPrimitiveType( type );
	}

	// Lock the mesh
	m_pMesh->LockMesh( nMaxVertexCount, nMaxIndexCount, *this );

	m_IndexBuilder.AttachBegin( pMesh, nMaxIndexCount, *this );
	m_VertexBuilder.AttachBegin( pMesh, nMaxVertexCount, *this );

	// Point to the start of the index and vertex buffers
	Reset();
}

inline void CMeshBuilder::Begin( IMesh *pMesh, MaterialPrimitiveType_t type, int nVertexCount, int nIndexCount, int *nFirstVertex )
{
	Begin( pMesh, type, nVertexCount, nIndexCount );

	*nFirstVertex = m_VertexBuilder.m_nFirstVertex * m_VertexBuilder.VertexSize();
}

inline void CMeshBuilder::Begin( IMesh* pMesh, MaterialPrimitiveType_t type, int nVertexCount, int nIndexCount )
{
	Assert( pMesh && (!m_pMesh) );

	// NOTE: We can't specify the indices when we use quads, polygons, or
	// linestrips; they aren't actually directly supported by 
	// the material system
	Assert( (type != MATERIAL_QUADS) && (type != MATERIAL_INSTANCED_QUADS) && (type != MATERIAL_POLYGON) &&
		(type != MATERIAL_LINE_STRIP) && (type != MATERIAL_LINE_LOOP));

	// Dx8 doesn't support indexed points...
	Assert( type != MATERIAL_POINTS );

	m_pMesh = pMesh;
	m_bGenerateIndices = false;
	m_Type = type;

	// Set the primitive type
	m_pMesh->SetPrimitiveType( type );

	// Lock the vertex and index buffer
	m_pMesh->LockMesh( nVertexCount, nIndexCount, *this );

	m_IndexBuilder.AttachBegin( pMesh, nIndexCount, *this );
	m_VertexBuilder.AttachBegin( pMesh, nVertexCount, *this );

	// Point to the start of the buffers..
	Reset();
}


//-----------------------------------------------------------------------------
// Use this when you're done modifying the mesh
//-----------------------------------------------------------------------------
inline void CMeshBuilder::End( bool bSpewData, bool bDraw )
{
	if ( m_bGenerateIndices )
	{
		int nIndexCount = IndicesFromVertices( m_Type, m_VertexBuilder.VertexCount() );
		m_IndexBuilder.GenerateIndices( m_Type, nIndexCount );
	}

	if ( bSpewData )
	{
		m_pMesh->Spew( m_VertexBuilder.VertexCount(), m_IndexBuilder.IndexCount(), *this );
	}

#ifdef _DEBUG
	m_pMesh->ValidateData( m_VertexBuilder.VertexCount(), m_IndexBuilder.IndexCount(), *this );
#endif

	// Unlock our buffers
	m_pMesh->UnlockMesh( m_VertexBuilder.VertexCount(), m_IndexBuilder.IndexCount(), *this );

	m_IndexBuilder.AttachEnd();
	m_VertexBuilder.AttachEnd();

	if ( bDraw )
	{
		m_pMesh->Draw();
	}

	m_pMesh = 0;

#ifdef _DEBUG
	memset( (MeshDesc_t*)this, 0, sizeof(MeshDesc_t) );
#endif
}


//-----------------------------------------------------------------------------
// Locks the vertex buffer to modify existing data
//-----------------------------------------------------------------------------
inline void CMeshBuilder::BeginModify( IMesh* pMesh, int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount )
{
	Assert( pMesh && (!m_pMesh) );

	if (nVertexCount < 0)
	{
		nVertexCount = pMesh->VertexCount();
	}

	m_pMesh = pMesh;
	m_bGenerateIndices = false;

	// Locks mesh for modifying
	pMesh->ModifyBeginEx( false, nFirstVertex, nVertexCount, nFirstIndex, nIndexCount, *this );

	m_IndexBuilder.AttachBeginModify( pMesh, nFirstIndex, nIndexCount, *this );
	m_VertexBuilder.AttachBeginModify( pMesh, nFirstVertex, nVertexCount, *this );

	// Point to the start of the buffers..
	Reset();
}

inline void CMeshBuilder::EndModify( bool bSpewData )
{
	Assert( m_pMesh );

	if (bSpewData)
	{
		m_pMesh->Spew( m_VertexBuilder.VertexCount(), m_IndexBuilder.IndexCount(), *this );
	}
#ifdef _DEBUG
	m_pMesh->ValidateData( m_VertexBuilder.VertexCount(), m_IndexBuilder.IndexCount(), *this );
#endif

	// Unlocks mesh
	m_pMesh->ModifyEnd( *this );
	m_pMesh = 0;

	m_IndexBuilder.AttachEndModify();
	m_VertexBuilder.AttachEndModify();

#ifdef _DEBUG
	// Null out our pointers...
	memset( (MeshDesc_t*)this, 0, sizeof(MeshDesc_t) );
#endif
}


//-----------------------------------------------------------------------------
// Resets the mesh builder so it points to the start of everything again
//-----------------------------------------------------------------------------
inline void CMeshBuilder::Reset()
{
	m_IndexBuilder.Reset();
	m_VertexBuilder.Reset();
}


//-----------------------------------------------------------------------------
// Selects the current Vertex and Index 
//-----------------------------------------------------------------------------
FORCEINLINE void CMeshBuilder::SelectVertex( int nIndex )
{
	m_VertexBuilder.SelectVertex( nIndex );
}

inline void CMeshBuilder::SelectVertexFromIndex( int idx )
{
	// NOTE: This index is expected to be relative 
	int vertIdx = idx - m_nFirstVertex;
	SelectVertex( vertIdx );
}

FORCEINLINE void CMeshBuilder::SelectIndex( int idx )
{
	m_IndexBuilder.SelectIndex( idx );
}


//-----------------------------------------------------------------------------
// Advances the current vertex and index by one
//-----------------------------------------------------------------------------
template<int nFlags, int nNumTexCoords> FORCEINLINE void CMeshBuilder::AdvanceVertexF()
{
	m_VertexBuilder.AdvanceVertexF<nFlags, nNumTexCoords>();
}
FORCEINLINE void CMeshBuilder::AdvanceVertex()
{
	m_VertexBuilder.AdvanceVertex();
}

FORCEINLINE void CMeshBuilder::AdvanceVertices( int nVertexCount )
{
	m_VertexBuilder.AdvanceVertices( nVertexCount );
}

FORCEINLINE void CMeshBuilder::AdvanceIndex()
{
	m_IndexBuilder.AdvanceIndex();
}

FORCEINLINE void CMeshBuilder::AdvanceIndices( int nIndices )
{
	m_IndexBuilder.AdvanceIndices( nIndices );
}

FORCEINLINE int CMeshBuilder::GetCurrentVertex()
{
	return m_VertexBuilder.GetCurrentVertex();
}

FORCEINLINE int CMeshBuilder::GetCurrentIndex()
{
	return m_IndexBuilder.GetCurrentIndex();
}


//-----------------------------------------------------------------------------
// A helper method since this seems to be done a whole bunch.
//-----------------------------------------------------------------------------
inline void CMeshBuilder::DrawQuad( IMesh* pMesh, const float* v1, const float* v2, 
								   const float* v3, const float* v4, unsigned char const* pColor, bool wireframe )
{
	if (!wireframe)
	{
		Begin( pMesh, MATERIAL_TRIANGLE_STRIP, 2 );

		Position3fv (v1);
		Color4ubv( pColor );
		AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();

		Position3fv (v2);
		Color4ubv( pColor );
		AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();

		Position3fv (v4);
		Color4ubv( pColor );
		AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();

		Position3fv (v3);
		Color4ubv( pColor );
		AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();
	}
	else
	{
		Begin( pMesh, MATERIAL_LINE_LOOP, 4 );
		Position3fv (v1);
		Color4ubv( pColor );
		AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();

		Position3fv (v2);
		Color4ubv( pColor );
		AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();

		Position3fv (v3);
		Color4ubv( pColor );
		AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();

		Position3fv (v4);
		Color4ubv( pColor );
		AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();
	}

	End();
	pMesh->Draw();
}


//-----------------------------------------------------------------------------
// returns the number of indices and vertices
//-----------------------------------------------------------------------------
FORCEINLINE int CMeshBuilder::VertexCount() const
{
	return m_VertexBuilder.VertexCount();
}

FORCEINLINE int CMeshBuilder::IndexCount() const
{
	return m_IndexBuilder.IndexCount();
}


//-----------------------------------------------------------------------------
// Returns the base vertex memory pointer
//-----------------------------------------------------------------------------
FORCEINLINE void* CMeshBuilder::BaseVertexData()
{
	return m_VertexBuilder.BaseVertexData();
}

//-----------------------------------------------------------------------------
// Data retrieval...
//-----------------------------------------------------------------------------
FORCEINLINE const float* CMeshBuilder::Position() const
{
	return m_VertexBuilder.Position();
}

FORCEINLINE const float* CMeshBuilder::Normal()	const
{
	return m_VertexBuilder.Normal();
}

FORCEINLINE unsigned int CMeshBuilder::Color() const
{
	return m_VertexBuilder.Color();
}

FORCEINLINE unsigned char *CMeshBuilder::Specular() const
{
	return m_VertexBuilder.Specular();
}

FORCEINLINE const float* CMeshBuilder::TexCoord( int nStage ) const
{
	return m_VertexBuilder.TexCoord( nStage );
}

FORCEINLINE const float* CMeshBuilder::TangentS() const
{
	return m_VertexBuilder.TangentS();
}

FORCEINLINE const float* CMeshBuilder::TangentT() const
{
	return m_VertexBuilder.TangentT();
}

FORCEINLINE float CMeshBuilder::Wrinkle() const
{
	return m_VertexBuilder.Wrinkle();
}

FORCEINLINE const float* CMeshBuilder::BoneWeight() const
{
	return m_VertexBuilder.BoneWeight();
}

FORCEINLINE int CMeshBuilder::NumBoneWeights() const
{
	return m_VertexBuilder.NumBoneWeights();
}

FORCEINLINE unsigned short const* CMeshBuilder::Index() const
{
	return m_IndexBuilder.Index();
}


//-----------------------------------------------------------------------------
// Index
//-----------------------------------------------------------------------------
FORCEINLINE void CMeshBuilder::Index( unsigned short idx )
{
	m_IndexBuilder.Index( idx );
}


//-----------------------------------------------------------------------------
// Fast Index! No need to call advance index
//-----------------------------------------------------------------------------
FORCEINLINE void CMeshBuilder::FastIndex( unsigned short idx )
{
	m_IndexBuilder.FastIndex( idx );
}

// NOTE: Use this one to get write combining! Much faster than the other version of FastIndex
// Fast Index! No need to call advance index, and no random access allowed
FORCEINLINE void CMeshBuilder::FastIndex2( unsigned short nIndex1, unsigned short nIndex2 )
{
	m_IndexBuilder.FastIndex2( nIndex1, nIndex2 );
}

//-----------------------------------------------------------------------------
// For use with the FastVertex methods, advances the current vertex by N
//-----------------------------------------------------------------------------
FORCEINLINE void CMeshBuilder::FastAdvanceNVertices( int nVertexCount )
{
	m_VertexBuilder.FastAdvanceNVertices( nVertexCount );
}


//-----------------------------------------------------------------------------
// Fast Vertex! No need to call advance vertex, and no random access allowed
//-----------------------------------------------------------------------------
FORCEINLINE void CMeshBuilder::FastVertex( const ModelVertexDX7_t &vertex )
{
	m_VertexBuilder.FastVertex( vertex );
}

FORCEINLINE void CMeshBuilder::FastVertexSSE( const ModelVertexDX7_t &vertex )
{
	m_VertexBuilder.FastVertexSSE( vertex );
}

FORCEINLINE void CMeshBuilder::Fast4VerticesSSE( 
	const ModelVertexDX7_t *vtx_a, const ModelVertexDX7_t *vtx_b,
	const ModelVertexDX7_t *vtx_c, const ModelVertexDX7_t *vtx_d )
{
	m_VertexBuilder.Fast4VerticesSSE( vtx_a, vtx_b, vtx_c, vtx_d );
}

FORCEINLINE void CMeshBuilder::FastVertex( const ModelVertexDX8_t &vertex )
{
	m_VertexBuilder.FastVertex( vertex );
}

FORCEINLINE void CMeshBuilder::FastVertexSSE( const ModelVertexDX8_t &vertex )
{
	m_VertexBuilder.FastVertexSSE( vertex );
}

//-----------------------------------------------------------------------------
// Copies a vertex into the x360 format
//-----------------------------------------------------------------------------
#if defined( _X360 )
inline void CMeshBuilder::VertexDX8ToX360( const ModelVertexDX8_t &vertex )
{
	m_VertexBuilder.VertexDX8ToX360( vertex );
}
#endif

//-----------------------------------------------------------------------------
// Vertex field setting methods
//-----------------------------------------------------------------------------
FORCEINLINE void CMeshBuilder::Position3f( float x, float y, float z )
{
	m_VertexBuilder.Position3f( x, y, z );
}

FORCEINLINE void CMeshBuilder::Position3fv( const float *v )
{
	m_VertexBuilder.Position3fv( v );
}

FORCEINLINE void CMeshBuilder::Normal3f( float nx, float ny, float nz )
{
	m_VertexBuilder.Normal3f( nx, ny, nz );
}

FORCEINLINE void CMeshBuilder::Normal3fv( const float *n )
{
	m_VertexBuilder.Normal3fv( n );
}

FORCEINLINE void CMeshBuilder::NormalDelta3f( float nx, float ny, float nz )
{
	m_VertexBuilder.NormalDelta3f( nx, ny, nz );
}

FORCEINLINE void CMeshBuilder::NormalDelta3fv( const float *n )
{
	m_VertexBuilder.NormalDelta3fv( n );
}

FORCEINLINE void CMeshBuilder::Color3f( float r, float g, float b )
{
	m_VertexBuilder.Color3f( r, g, b );
}

FORCEINLINE void CMeshBuilder::Color3fv( const float *rgb )
{
	m_VertexBuilder.Color3fv( rgb );
}

FORCEINLINE void CMeshBuilder::Color4f( float r, float g, float b, float a )
{
	m_VertexBuilder.Color4f( r, g ,b, a );
}

FORCEINLINE void CMeshBuilder::Color4fv( const float *rgba )
{
	m_VertexBuilder.Color4fv( rgba );
}

FORCEINLINE void CMeshBuilder::Color3ub( unsigned char r, unsigned char g, unsigned char b )
{
	m_VertexBuilder.Color3ub( r, g, b );
}

FORCEINLINE void CMeshBuilder::Color3ubv( unsigned char const* rgb )
{
	m_VertexBuilder.Color3ubv( rgb );
}

FORCEINLINE void CMeshBuilder::Color4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a )
{
	m_VertexBuilder.Color4ub( r, g, b, a );
}

FORCEINLINE void CMeshBuilder::Color4ubv( unsigned char const* rgba )
{
	m_VertexBuilder.Color4ubv( rgba );
}

FORCEINLINE void CMeshBuilder::Specular3f( float r, float g, float b )
{
	m_VertexBuilder.Specular3f( r, g, b );
}

FORCEINLINE void CMeshBuilder::Specular3fv( const float *rgb )
{
	m_VertexBuilder.Specular3fv( rgb );
}

FORCEINLINE void CMeshBuilder::Specular4f( float r, float g, float b, float a )
{
	m_VertexBuilder.Specular4f( r, g, b, a );
}

FORCEINLINE void CMeshBuilder::Specular4fv( const float *rgba )
{
	m_VertexBuilder.Specular4fv( rgba );
}

FORCEINLINE void CMeshBuilder::Specular3ub( unsigned char r, unsigned char g, unsigned char b )
{
	m_VertexBuilder.Specular3ub( r, g, b );
}

FORCEINLINE void CMeshBuilder::Specular3ubv( unsigned char const *c )
{
	m_VertexBuilder.Specular3ubv( c );
}

FORCEINLINE void CMeshBuilder::Specular4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a )
{
	m_VertexBuilder.Specular4ub( r, g, b, a );
}

FORCEINLINE void CMeshBuilder::Specular4ubv( unsigned char const *c )
{
	m_VertexBuilder.Specular4ubv( c );
}

FORCEINLINE void CMeshBuilder::TexCoord1f( int nStage, float s )
{
	m_VertexBuilder.TexCoord1f( nStage, s );
}

FORCEINLINE void CMeshBuilder::TexCoord2f( int nStage, float s, float t )
{
	m_VertexBuilder.TexCoord2f( nStage, s, t );
}

FORCEINLINE void CMeshBuilder::TexCoord2fv( int nStage, const float *st )
{
	m_VertexBuilder.TexCoord2fv( nStage, st );
}

FORCEINLINE void CMeshBuilder::TexCoord3f( int nStage, float s, float t, float u )
{
	m_VertexBuilder.TexCoord3f( nStage, s, t, u );
}

FORCEINLINE void CMeshBuilder::TexCoord3fv( int nStage, const float *stu )
{
	m_VertexBuilder.TexCoord3fv( nStage, stu );
}

FORCEINLINE void CMeshBuilder::TexCoord4f( int nStage, float s, float t, float u, float v )
{
	m_VertexBuilder.TexCoord4f( nStage, s, t, u, v );
}

FORCEINLINE void CMeshBuilder::TexCoord4fv( int nStage, const float *stuv )
{
	m_VertexBuilder.TexCoord4fv( nStage, stuv );
}

FORCEINLINE void CMeshBuilder::TexCoordSubRect2f( int nStage, float s, float t, float offsetS, float offsetT, float scaleS, float scaleT )
{
	m_VertexBuilder.TexCoordSubRect2f( nStage, s, t, offsetS, offsetT, scaleS, scaleT );
}

FORCEINLINE void CMeshBuilder::TexCoordSubRect2fv( int nStage, const float *st, const float *offset, const float *scale )
{
	m_VertexBuilder.TexCoordSubRect2fv( nStage, st, offset, scale );
}

FORCEINLINE void CMeshBuilder::TangentS3f( float sx, float sy, float sz )
{
	m_VertexBuilder.TangentS3f( sx, sy, sz );
}

FORCEINLINE void CMeshBuilder::TangentS3fv( const float* s )
{
	m_VertexBuilder.TangentS3fv( s );
}

FORCEINLINE void CMeshBuilder::TangentT3f( float tx, float ty, float tz )
{
	m_VertexBuilder.TangentT3f( tx, ty, tz );
}

FORCEINLINE void CMeshBuilder::TangentT3fv( const float* t )
{
	m_VertexBuilder.TangentT3fv( t );
}

FORCEINLINE void CMeshBuilder::Wrinkle1f( float flWrinkle )
{
	m_VertexBuilder.Wrinkle1f( flWrinkle );
}

FORCEINLINE void CMeshBuilder::BoneWeight( int nIndex, float flWeight )
{
	m_VertexBuilder.BoneWeight( nIndex, flWeight );
}

template <VertexCompressionType_t T> FORCEINLINE void CMeshBuilder::CompressedBoneWeight3fv( const float * pWeights )
{
	m_VertexBuilder.CompressedBoneWeight3fv<T>( pWeights );
}

FORCEINLINE void CMeshBuilder::BoneMatrix( int nIndex, int nMatrixIdx )
{
	m_VertexBuilder.BoneMatrix( nIndex, nMatrixIdx );
}

FORCEINLINE void CMeshBuilder::UserData( const float* pData )
{
	m_VertexBuilder.UserData( pData );
}

template <VertexCompressionType_t T> FORCEINLINE void CMeshBuilder::CompressedUserData( const float* pData )
{
	m_VertexBuilder.CompressedUserData<T>( pData );
}

//-----------------------------------------------------------------------------
// Templatized vertex field setting methods which support compression
//-----------------------------------------------------------------------------

template <VertexCompressionType_t T> FORCEINLINE void CMeshBuilder::CompressedNormal3f( float nx, float ny, float nz )
{
	m_VertexBuilder.CompressedNormal3f<T>( nx, ny, nz );
}

template <VertexCompressionType_t T> FORCEINLINE void CMeshBuilder::CompressedNormal3fv( const float *n )
{
	m_VertexBuilder.CompressedNormal3fv<T>( n );
}

#endif // IMESH_H