summaryrefslogtreecommitdiff
path: root/indra/llprimitive/llprimitive.cpp
blob: bfa65666b5e839e6d22c6168e6df8670049cb8dd (plain)
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
/** 
 * @file llprimitive.cpp
 * @brief LLPrimitive base class
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"

#include "material_codes.h"
#include "llerror.h"
#include "message.h"
#include "llprimitive.h"
#include "llvolume.h"
#include "legacy_object_types.h"
#include "v4coloru.h"
#include "llvolumemgr.h"
#include "llstring.h"
#include "lldatapacker.h"
#include "llsdutil_math.h"
#include "llprimtexturelist.h"
#include "llmaterialid.h"

/**
 * exported constants
 */

const F32 OBJECT_CUT_MIN = 0.f;
const F32 OBJECT_CUT_MAX = 1.f;
const F32 OBJECT_CUT_INC = 0.05f;
const F32 OBJECT_MIN_CUT_INC = 0.02f;
const F32 OBJECT_ROTATION_PRECISION = 0.05f;

const F32 OBJECT_TWIST_MIN = -360.f;
const F32 OBJECT_TWIST_MAX =  360.f;
const F32 OBJECT_TWIST_INC =   18.f;

// This is used for linear paths,
// since twist is used in a slightly different manner.
const F32 OBJECT_TWIST_LINEAR_MIN	= -180.f;
const F32 OBJECT_TWIST_LINEAR_MAX	=  180.f;
const F32 OBJECT_TWIST_LINEAR_INC	=    9.f;

const F32 OBJECT_MIN_HOLE_SIZE = 0.05f;
const F32 OBJECT_MAX_HOLE_SIZE_X = 1.0f;
const F32 OBJECT_MAX_HOLE_SIZE_Y = 0.5f;

// Revolutions parameters.
const F32 OBJECT_REV_MIN = 1.0f;
const F32 OBJECT_REV_MAX = 4.0f;
const F32 OBJECT_REV_INC = 0.1f;

// lights
const F32 LIGHT_MIN_RADIUS = 0.0f;
const F32 LIGHT_DEFAULT_RADIUS = 5.0f;
const F32 LIGHT_MAX_RADIUS = 20.0f;
const F32 LIGHT_MIN_FALLOFF = 0.0f;
const F32 LIGHT_DEFAULT_FALLOFF = 1.0f;
const F32 LIGHT_MAX_FALLOFF = 2.0f;
const F32 LIGHT_MIN_CUTOFF = 0.0f;
const F32 LIGHT_DEFAULT_CUTOFF = 0.0f;
const F32 LIGHT_MAX_CUTOFF = 180.f;

// "Tension" => [0,10], increments of 0.1
const F32 FLEXIBLE_OBJECT_MIN_TENSION = 0.0f;
const F32 FLEXIBLE_OBJECT_DEFAULT_TENSION = 1.0f;
const F32 FLEXIBLE_OBJECT_MAX_TENSION = 10.0f; 

// "Drag" => [0,10], increments of 0.1
const F32 FLEXIBLE_OBJECT_MIN_AIR_FRICTION = 0.0f;
const F32 FLEXIBLE_OBJECT_DEFAULT_AIR_FRICTION = 2.0f;
const F32 FLEXIBLE_OBJECT_MAX_AIR_FRICTION = 10.0f;

// "Gravity" = [-10,10], increments of 0.1
const F32 FLEXIBLE_OBJECT_MIN_GRAVITY = -10.0f;
const F32 FLEXIBLE_OBJECT_DEFAULT_GRAVITY = 0.3f;
const F32 FLEXIBLE_OBJECT_MAX_GRAVITY = 10.0f;

// "Wind" = [0,10], increments of 0.1
const F32 FLEXIBLE_OBJECT_MIN_WIND_SENSITIVITY = 0.0f;
const F32 FLEXIBLE_OBJECT_DEFAULT_WIND_SENSITIVITY = 0.0f;
const F32 FLEXIBLE_OBJECT_MAX_WIND_SENSITIVITY = 10.0f;

// I'll explain later...
const F32 FLEXIBLE_OBJECT_MAX_INTERNAL_TENSION_FORCE = 0.99f; 

const F32 FLEXIBLE_OBJECT_DEFAULT_LENGTH = 1.0f;
const BOOL FLEXIBLE_OBJECT_DEFAULT_USING_COLLISION_SPHERE = FALSE;
const BOOL FLEXIBLE_OBJECT_DEFAULT_RENDERING_COLLISION_SPHERE = FALSE;

const char *SCULPT_DEFAULT_TEXTURE = "be293869-d0d9-0a69-5989-ad27f1946fd4"; // old inverted texture: "7595d345-a24c-e7ef-f0bd-78793792133e";

// Texture rotations are sent over the wire as a S16.  This is used to scale the actual float
// value to a S16.   Don't use 7FFF as it introduces some odd rounding with 180 since it 
// can't be divided by 2.   See DEV-19108
const F32	TEXTURE_ROTATION_PACK_FACTOR = ((F32) 0x08000);

//static 
// LEGACY: by default we use the LLVolumeMgr::gVolumeMgr global
// TODO -- eliminate this global from the codebase!
LLVolumeMgr* LLPrimitive::sVolumeManager = NULL;

// static
void LLPrimitive::setVolumeManager( LLVolumeMgr* volume_manager )
{
	if ( !volume_manager || sVolumeManager )
	{
		LL_ERRS() << "LLPrimitive::sVolumeManager attempting to be set to NULL or it already has been set." << LL_ENDL;
	}
	sVolumeManager = volume_manager;
}

// static
bool LLPrimitive::cleanupVolumeManager()
{
	BOOL res = FALSE;
	if (sVolumeManager) 
	{
		res = sVolumeManager->cleanup();
		delete sVolumeManager;
		sVolumeManager = NULL;
	}
	return res;
}


//===============================================================
LLPrimitive::LLPrimitive()
:	mTextureList(),
	mNumTEs(0),
	mMiscFlags(0),
	mNumBumpmapTEs(0)
{
	mPrimitiveCode = 0;

	mMaterial = LL_MCODE_STONE;
	mVolumep  = NULL;

	mChanged  = UNCHANGED;

	mPosition.setVec(0.f,0.f,0.f);
	mVelocity.setVec(0.f,0.f,0.f);
	mAcceleration.setVec(0.f,0.f,0.f);

	mRotation.loadIdentity();
	mAngularVelocity.setVec(0.f,0.f,0.f);
	
	mScale.setVec(1.f,1.f,1.f);
}

//===============================================================
LLPrimitive::~LLPrimitive()
{
	clearTextureList();
	// Cleanup handled by volume manager
	if (mVolumep && sVolumeManager)
	{
		sVolumeManager->unrefVolume(mVolumep);
	}
	mVolumep = NULL;
}

void LLPrimitive::clearTextureList()
{
}

//===============================================================
// static
LLPrimitive *LLPrimitive::createPrimitive(LLPCode p_code)
{
	LLPrimitive *retval = new LLPrimitive();
	
	if (retval)
	{
		retval->init_primitive(p_code);
	}
	else
	{
		LL_ERRS() << "primitive allocation failed" << LL_ENDL;
	}

	return retval;
}

//===============================================================
void LLPrimitive::init_primitive(LLPCode p_code)
{
	clearTextureList();
	mPrimitiveCode = p_code;
}

void LLPrimitive::setPCode(const U8 p_code)
{
	mPrimitiveCode = p_code;
}

//===============================================================
LLTextureEntry* LLPrimitive::getTE(const U8 index) const
{
	return mTextureList.getTexture(index);
}

//===============================================================
void LLPrimitive::setNumTEs(const U8 num_tes)
{
	mTextureList.setSize(num_tes);
}

//===============================================================
void  LLPrimitive::setAllTETextures(const LLUUID &tex_id)
{
	mTextureList.setAllIDs(tex_id);
}

//===============================================================
void LLPrimitive::setTE(const U8 index, const LLTextureEntry& te)
{
	if(mTextureList.copyTexture(index, te) != TEM_CHANGE_NONE && te.getBumpmap() > 0)
	{
		mNumBumpmapTEs++;
	}
}

S32  LLPrimitive::setTETexture(const U8 index, const LLUUID &id)
{
	return mTextureList.setID(index, id);
}

S32  LLPrimitive::setTEColor(const U8 index, const LLColor4 &color)
{
	return mTextureList.setColor(index, color);
}

S32  LLPrimitive::setTEColor(const U8 index, const LLColor3 &color)
{
	return mTextureList.setColor(index, color);
}

S32  LLPrimitive::setTEAlpha(const U8 index, const F32 alpha)
{
	return mTextureList.setAlpha(index, alpha);
}

//===============================================================
S32  LLPrimitive::setTEScale(const U8 index, const F32 s, const F32 t)
{
	return mTextureList.setScale(index, s, t);
}


// BUG: slow - done this way because texture entries have some
// voodoo related to texture coords
S32 LLPrimitive::setTEScaleS(const U8 index, const F32 s)
{
	return mTextureList.setScaleS(index, s);
}


// BUG: slow - done this way because texture entries have some
// voodoo related to texture coords
S32 LLPrimitive::setTEScaleT(const U8 index, const F32 t)
{
	return mTextureList.setScaleT(index, t);
}


//===============================================================
S32  LLPrimitive::setTEOffset(const U8 index, const F32 s, const F32 t)
{
	return mTextureList.setOffset(index, s, t);
}


// BUG: slow - done this way because texture entries have some
// voodoo related to texture coords
S32 LLPrimitive::setTEOffsetS(const U8 index, const F32 s)
{
	return mTextureList.setOffsetS(index, s);
}


// BUG: slow - done this way because texture entries have some
// voodoo related to texture coords
S32 LLPrimitive::setTEOffsetT(const U8 index, const F32 t)
{
	return mTextureList.setOffsetT(index, t);
}


//===============================================================
S32  LLPrimitive::setTERotation(const U8 index, const F32 r)
{
	return mTextureList.setRotation(index, r);
}

S32 LLPrimitive::setTEMaterialID(const U8 index, const LLMaterialID& pMaterialID)
{
	return mTextureList.setMaterialID(index, pMaterialID);
}

S32 LLPrimitive::setTEMaterialParams(const U8 index, const LLMaterialPtr pMaterialParams)
{
	return mTextureList.setMaterialParams(index, pMaterialParams);
}

LLMaterialPtr LLPrimitive::getTEMaterialParams(const U8 index)
{
	return mTextureList.getMaterialParams(index);
}

//===============================================================
S32  LLPrimitive::setTEBumpShinyFullbright(const U8 index, const U8 bump)
{
	updateNumBumpmap(index, bump);
	return mTextureList.setBumpShinyFullbright(index, bump);
}

S32  LLPrimitive::setTEMediaTexGen(const U8 index, const U8 media)
{
	return mTextureList.setMediaTexGen(index, media);
}

S32  LLPrimitive::setTEBumpmap(const U8 index, const U8 bump)
{
	updateNumBumpmap(index, bump);
	return mTextureList.setBumpMap(index, bump);
}

S32  LLPrimitive::setTEBumpShiny(const U8 index, const U8 bump_shiny)
{
	updateNumBumpmap(index, bump_shiny);
	return mTextureList.setBumpShiny(index, bump_shiny);
}

S32  LLPrimitive::setTETexGen(const U8 index, const U8 texgen)
{
	return mTextureList.setTexGen(index, texgen);
}

S32  LLPrimitive::setTEShiny(const U8 index, const U8 shiny)
{
	return mTextureList.setShiny(index, shiny);
}

S32  LLPrimitive::setTEFullbright(const U8 index, const U8 fullbright)
{
	return mTextureList.setFullbright(index, fullbright);
}

S32  LLPrimitive::setTEMediaFlags(const U8 index, const U8 media_flags)
{
	return mTextureList.setMediaFlags(index, media_flags);
}

S32 LLPrimitive::setTEGlow(const U8 index, const F32 glow)
{
	return mTextureList.setGlow(index, glow);
}

void LLPrimitive::setAllTESelected(bool sel)
{
	for (int i = 0, cnt = getNumTEs(); i < cnt; i++)
	{
		setTESelected(i, sel);
	}
}
	
void LLPrimitive::setTESelected(const U8 te, bool sel)
{
	LLTextureEntry* tep = getTE(te);
	if ( (tep) && (tep->setSelected(sel)) && (!sel) && (tep->hasPendingMaterialUpdate()) )
	{
		LLMaterialID material_id = tep->getMaterialID();
		setTEMaterialID(te, material_id);
	}
}

LLPCode LLPrimitive::legacyToPCode(const U8 legacy)
{
	// TODO: Should this default to something valid?
	// Maybe volume?
	LLPCode pcode = 0;

	switch (legacy)
	{
		/*
	case BOX:
		pcode = LL_PCODE_CUBE;
		break;
	case CYLINDER:
		pcode = LL_PCODE_CYLINDER;
		break;
	case CONE:
		pcode = LL_PCODE_CONE;
		break;
	case HALF_CONE:
		pcode = LL_PCODE_CONE_HEMI;
		break;
	case HALF_CYLINDER:
		pcode = LL_PCODE_CYLINDER_HEMI;
		break;
	case HALF_SPHERE:
		pcode = LL_PCODE_SPHERE_HEMI;
		break;
	case PRISM:
		pcode = LL_PCODE_PRISM;
		break;
	case PYRAMID:
		pcode = LL_PCODE_PYRAMID;
		break;
	case SPHERE:
		pcode = LL_PCODE_SPHERE;
		break;
	case TETRAHEDRON:
		pcode = LL_PCODE_TETRAHEDRON;
		break;
	case DEMON:
		pcode = LL_PCODE_LEGACY_DEMON;
		break;
	case LSL_TEST:
		pcode = LL_PCODE_LEGACY_LSL_TEST;
		break;
	case ORACLE:
		pcode = LL_PCODE_LEGACY_ORACLE;
		break;
	case TEXTBUBBLE:
		pcode = LL_PCODE_LEGACY_TEXT_BUBBLE;
		break;
	case ATOR:
		pcode = LL_PCODE_LEGACY_ATOR;
		break;
	case BASIC_SHOT:
		pcode = LL_PCODE_LEGACY_SHOT;
		break;
	case BIG_SHOT:
		pcode = LL_PCODE_LEGACY_SHOT_BIG;
		break;
	case BIRD:
		pcode = LL_PCODE_LEGACY_BIRD;
		break;
	case ROCK:
		pcode = LL_PCODE_LEGACY_ROCK;
		break;
	case SMOKE:
		pcode = LL_PCODE_LEGACY_SMOKE;
		break;
	case SPARK:
		pcode = LL_PCODE_LEGACY_SPARK;
		break;
		*/
	case PRIMITIVE_VOLUME:
		pcode = LL_PCODE_VOLUME;
		break;
	case GRASS:
		pcode = LL_PCODE_LEGACY_GRASS;
		break;
	case PART_SYS:
		pcode = LL_PCODE_LEGACY_PART_SYS;
		break;
	case PLAYER:
		pcode = LL_PCODE_LEGACY_AVATAR;
		break;
	case TREE:
		pcode = LL_PCODE_LEGACY_TREE;
		break;
	case TREE_NEW:
		pcode = LL_PCODE_TREE_NEW;
		break;
	default:
		LL_WARNS() << "Unknown legacy code " << legacy << " [" << (S32)legacy << "]!" << LL_ENDL;
	}

	return pcode;
}

U8 LLPrimitive::pCodeToLegacy(const LLPCode pcode)
{
	U8 legacy;
	switch (pcode)
	{
/*
	case LL_PCODE_CUBE:
		legacy = BOX;
		break;
	case LL_PCODE_CYLINDER:
		legacy = CYLINDER;
		break;
	case LL_PCODE_CONE:
		legacy = CONE;
		break;
	case LL_PCODE_CONE_HEMI:
		legacy = HALF_CONE;
		break;
	case LL_PCODE_CYLINDER_HEMI:
		legacy = HALF_CYLINDER;
		break;
	case LL_PCODE_SPHERE_HEMI:
		legacy = HALF_SPHERE;
		break;
	case LL_PCODE_PRISM:
		legacy = PRISM;
		break;
	case LL_PCODE_PYRAMID:
		legacy = PYRAMID;
		break;
	case LL_PCODE_SPHERE:
		legacy = SPHERE;
		break;
	case LL_PCODE_TETRAHEDRON:
		legacy = TETRAHEDRON;
		break;
	case LL_PCODE_LEGACY_ATOR:
		legacy = ATOR;
		break;
	case LL_PCODE_LEGACY_SHOT:
		legacy = BASIC_SHOT;
		break;
	case LL_PCODE_LEGACY_SHOT_BIG:
		legacy = BIG_SHOT;
		break;
	case LL_PCODE_LEGACY_BIRD:
		legacy = BIRD;
		break;		
	case LL_PCODE_LEGACY_DEMON:
		legacy = DEMON;
		break;
	case LL_PCODE_LEGACY_LSL_TEST:
		legacy = LSL_TEST;
		break;
	case LL_PCODE_LEGACY_ORACLE:
		legacy = ORACLE;
		break;
	case LL_PCODE_LEGACY_ROCK:
		legacy = ROCK;
		break;
	case LL_PCODE_LEGACY_TEXT_BUBBLE:
		legacy = TEXTBUBBLE;
		break;
	case LL_PCODE_LEGACY_SMOKE:
		legacy = SMOKE;
		break;
	case LL_PCODE_LEGACY_SPARK:
		legacy = SPARK;
		break;
*/
	case LL_PCODE_VOLUME:
		legacy = PRIMITIVE_VOLUME;
		break;
	case LL_PCODE_LEGACY_GRASS:
		legacy = GRASS;
		break;
	case LL_PCODE_LEGACY_PART_SYS:
		legacy = PART_SYS;
		break;
	case LL_PCODE_LEGACY_AVATAR:
		legacy = PLAYER;
		break;
	case LL_PCODE_LEGACY_TREE:
		legacy = TREE;
		break;
	case LL_PCODE_TREE_NEW:
		legacy = TREE_NEW;
		break;
	default:
		LL_WARNS() << "Unknown pcode " << (S32)pcode << ":" << pcode << "!" << LL_ENDL;
		return 0;
	}
	return legacy;
}


// static
// Don't crash or LL_ERRS() here!  This function is used for debug strings.
std::string LLPrimitive::pCodeToString(const LLPCode pcode)
{
	std::string pcode_string;

	U8 base_code = pcode & LL_PCODE_BASE_MASK;
	if (!pcode)
	{
		pcode_string = "null";
	}
	else if ((base_code) == LL_PCODE_LEGACY)
	{
		// It's a legacy object
		switch (pcode)
		{
		case LL_PCODE_LEGACY_GRASS:
		  pcode_string = "grass";
			break;
		case LL_PCODE_LEGACY_PART_SYS:
		  pcode_string = "particle system";
			break;
		case LL_PCODE_LEGACY_AVATAR:
		  pcode_string = "avatar";
			break;
		case LL_PCODE_LEGACY_TEXT_BUBBLE:
		  pcode_string = "text bubble";
			break;
		case LL_PCODE_LEGACY_TREE:
		  pcode_string = "tree";
			break;
		case LL_PCODE_TREE_NEW:
		  pcode_string = "tree_new";
			break;
		default:
		  pcode_string = llformat( "unknown legacy pcode %i",(U32)pcode);
		}
	}
	else
	{
		std::string shape;
		std::string mask;
		if (base_code == LL_PCODE_CUBE)
		{
			shape = "cube";
		}
		else if (base_code == LL_PCODE_CYLINDER)
		{
			shape = "cylinder";
		}
		else if (base_code == LL_PCODE_CONE)
		{
			shape = "cone";
		}
		else if (base_code == LL_PCODE_PRISM)
		{
			shape = "prism";
		}
		else if (base_code == LL_PCODE_PYRAMID)
		{
			shape = "pyramid";
		}
		else if (base_code == LL_PCODE_SPHERE)
		{
			shape = "sphere";
		}
		else if (base_code == LL_PCODE_TETRAHEDRON)
		{
			shape = "tetrahedron";
		}
		else if (base_code == LL_PCODE_VOLUME)
		{
			shape = "volume";
		}
		else if (base_code == LL_PCODE_APP)
		{
			shape = "app";
		}
		else
		{
			LL_WARNS() << "Unknown base mask for pcode: " << base_code << LL_ENDL;
		}

		U8 mask_code = pcode & (~LL_PCODE_BASE_MASK);
		if (base_code == LL_PCODE_APP)
		{
			mask = llformat( "%x", mask_code);
		}
		else if (mask_code & LL_PCODE_HEMI_MASK)
		{
			mask = "hemi";
		}
		else 
		{
			mask = llformat( "%x", mask_code);
		}

		if (mask[0])
		{
			pcode_string = llformat( "%s-%s", shape.c_str(), mask.c_str());
		}
		else
		{
			pcode_string = llformat( "%s", shape.c_str());
		}
	}

	return pcode_string;
}


void LLPrimitive::copyTEs(const LLPrimitive *primitivep)
{
	U32 i;
	if (primitivep->getExpectedNumTEs() != getExpectedNumTEs())
	{
		LL_WARNS() << "Primitives don't have same expected number of TE's" << LL_ENDL;
	}
	U32 num_tes = llmin(primitivep->getExpectedNumTEs(), getExpectedNumTEs());
	if (mTextureList.size() < getExpectedNumTEs())
	{
		mTextureList.setSize(getExpectedNumTEs());
	}
	for (i = 0; i < num_tes; i++)
	{
		mTextureList.copyTexture(i, *(primitivep->getTE(i)));
	}
}

S32	face_index_from_id(LLFaceID face_ID, const std::vector<LLProfile::Face>& faceArray)
{
	S32 i;
	for (i = 0; i < (S32)faceArray.size(); i++)
	{
		if (faceArray[i].mFaceID == face_ID)
		{
			return i;
		}
	}
	return -1;
}

BOOL LLPrimitive::setVolume(const LLVolumeParams &volume_params, const S32 detail, bool unique_volume)
{
	LLVolume *volumep;
	if (unique_volume)
	{
		F32 volume_detail = LLVolumeLODGroup::getVolumeScaleFromDetail(detail);
		if (mVolumep.notNull() && volume_params == mVolumep->getParams() && (volume_detail == mVolumep->getDetail()))
		{
			return FALSE;
		}
		volumep = new LLVolume(volume_params, volume_detail, FALSE, TRUE);
	}
	else
	{
		if (mVolumep.notNull())
		{
			F32 volume_detail = LLVolumeLODGroup::getVolumeScaleFromDetail(detail);
			if (volume_params == mVolumep->getParams() && (volume_detail == mVolumep->getDetail()))
			{
				return FALSE;
			}
		}

		volumep = sVolumeManager->refVolume(volume_params, detail);
		if (volumep == mVolumep)
		{
			sVolumeManager->unrefVolume( volumep );  // LLVolumeMgr::refVolume() creates a reference, but we don't need a second one.
			return TRUE;
		}
	}

	setChanged(GEOMETRY);

	
	if (!mVolumep)
	{
		mVolumep = volumep;
		//mFaceMask = mVolumep->generateFaceMask();
		setNumTEs(mVolumep->getNumFaces());
		return TRUE;
	}
	
#if 0 
	// #if 0'd out by davep
	// this is a lot of cruft to set texture entry values that just stay the same for LOD switch 
	// or immediately get overridden by an object update message, also crashes occasionally
	U32 old_face_mask = mVolumep->mFaceMask;

	S32 face_bit = 0;
	S32 cur_mask = 0;

	// Grab copies of the old faces from the original shape, ordered by type.
	// We will use these to figure out what old texture info gets mapped to new
	// faces in the new shape.
	std::vector<LLProfile::Face> old_faces; 
	for (S32 face = 0; face < mVolumep->getNumFaces(); face++)
	{
		old_faces.push_back(mVolumep->getProfile().mFaces[face]);
	}

	// Copy the old texture info off to the side, but not in the order in which
	// they live in the mTextureList, rather in order of ther "face id" which
	// is the corresponding value of LLVolueParams::LLProfile::mFaces::mIndex.
	//
	// Hence, some elements of old_tes::mEntryList will be invalid.  It is
	// initialized to a size of 9 (max number of possible faces on a volume?)
	// and only the ones with valid types are filled in.
	LLPrimTextureList old_tes;
	old_tes.setSize(9);
	for (face_bit = 0; face_bit < 9; face_bit++)
	{
		cur_mask = 0x1 << face_bit;
		if (old_face_mask & cur_mask)
		{
			S32 te_index = face_index_from_id(cur_mask, old_faces);
			old_tes.copyTexture(face_bit, *(getTE(te_index)));
			//LL_INFOS() << face_bit << ":" << te_index << ":" << old_tes[face_bit].getID() << LL_ENDL;
		}
	}


	// build the new object
	sVolumeManager->unrefVolume(mVolumep);
	mVolumep = volumep;
	
	U32 new_face_mask = mVolumep->mFaceMask;
	S32 i;

	if (old_face_mask == new_face_mask) 
	{
		// nothing to do
		return TRUE;
	}

	if (mVolumep->getNumFaces() == 0 && new_face_mask != 0)
	{
		LL_WARNS() << "Object with 0 faces found...INCORRECT!" << LL_ENDL;
		setNumTEs(mVolumep->getNumFaces());
		return TRUE;
	}

	// initialize face_mapping
	S32 face_mapping[9];
	for (face_bit = 0; face_bit < 9; face_bit++)
	{
		face_mapping[face_bit] = face_bit;
	}

	// The new shape may have more faces than the original, but we can't just
	// add them to the end -- the ordering matters and it may be that we must
	// insert the new faces in the middle of the list.  When we add a face it
	// will pick up the texture/color info of one of the old faces an so we
	// now figure out which old face info gets mapped to each new face, and 
	// store in the face_mapping lookup table.
	for (face_bit = 0; face_bit < 9; face_bit++)
	{
		cur_mask = 0x1 << face_bit;
		if (!(new_face_mask & cur_mask))
		{
			// Face doesn't exist in new map.
			face_mapping[face_bit] = -1;
			continue;
		}
		else if (old_face_mask & cur_mask)
		{
			// Face exists in new and old map.
			face_mapping[face_bit] = face_bit;
			continue;
		}

		// OK, how we've got a mismatch, where we have to fill a new face with one from
		// the old face.
		if (cur_mask & (LL_FACE_PATH_BEGIN | LL_FACE_PATH_END | LL_FACE_INNER_SIDE))
		{
			// It's a top/bottom/hollow interior face.
			if (old_face_mask & LL_FACE_PATH_END)
			{
				face_mapping[face_bit] = 1;
				continue;
			}
			else
			{
				S32 cur_outer_mask = LL_FACE_OUTER_SIDE_0;
				for (i = 0; i < 4; i++)
				{
					if (old_face_mask & cur_outer_mask)
					{
						face_mapping[face_bit] = 5 + i;
						break;
					}
					cur_outer_mask <<= 1;
				}
				if (i == 4)
				{
					LL_WARNS() << "No path end or outer face in volume!" << LL_ENDL;
				}
				continue;
			}
		}

		if (cur_mask & (LL_FACE_PROFILE_BEGIN | LL_FACE_PROFILE_END))
		{
			// A cut slice.  Use the hollow interior if we have it.
			if (old_face_mask & LL_FACE_INNER_SIDE)
			{
				face_mapping[face_bit] = 2;
				continue;
			}

			// No interior, use the bottom face.
			// Could figure out which of the outer faces was nearest, but that would be harder.
			if (old_face_mask & LL_FACE_PATH_END)
			{
				face_mapping[face_bit] = 1;
				continue;
			}
			else
			{
				S32 cur_outer_mask = LL_FACE_OUTER_SIDE_0;
				for (i = 0; i < 4; i++)
				{
					if (old_face_mask & cur_outer_mask)
					{
						face_mapping[face_bit] = 5 + i;
						break;
					}
					cur_outer_mask <<= 1;
				}
				if (i == 4)
				{
					LL_WARNS() << "No path end or outer face in volume!" << LL_ENDL;
				}
				continue;
			}
		}

		// OK, the face that's missing is an outer face...
		// Pull from the nearest adjacent outer face (there's always guaranteed to be one...
		S32 cur_outer = face_bit - 5;
		S32 min_dist = 5;
		S32 min_outer_bit = -1;
		S32 i;
		for (i = 0; i < 4; i++)
		{
			if (old_face_mask & (LL_FACE_OUTER_SIDE_0 << i))
			{
				S32 dist = abs(i - cur_outer);
				if (dist < min_dist)
				{
					min_dist = dist;
					min_outer_bit = i + 5;
				}
			}
		}
		if (-1 == min_outer_bit)
		{
			LL_INFOS() << (LLVolume *)mVolumep << LL_ENDL;
			LL_WARNS() << "Bad!  No outer faces, impossible!" << LL_ENDL;
		}
		face_mapping[face_bit] = min_outer_bit;
	}
	

	setNumTEs(mVolumep->getNumFaces());
	for (face_bit = 0; face_bit < 9; face_bit++)
	{
		// For each possible face type on the new shape we check to see if that
		// face exists and if it does we create a texture entry that is a copy
		// of one of the originals.  Since the originals might not have a
		// matching face, we use the face_mapping lookup table to figure out
		// which face information to copy.
		cur_mask = 0x1 << face_bit;
		if (new_face_mask & cur_mask)
		{
			if (-1 == face_mapping[face_bit])
			{
				LL_WARNS() << "No mapping from old face to new face!" << LL_ENDL;
			}

			S32 te_num = face_index_from_id(cur_mask, mVolumep->getProfile().mFaces);
			setTE(te_num, *(old_tes.getTexture(face_mapping[face_bit])));
		}
	}
#else
	// build the new object
	sVolumeManager->unrefVolume(mVolumep);
	mVolumep = volumep; 

	setNumTEs(mVolumep->getNumFaces());
#endif
	return TRUE;
}

BOOL LLPrimitive::setMaterial(U8 material)
{
	if (material != mMaterial)
	{
		mMaterial = material;
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

S32 LLPrimitive::packTEField(U8 *cur_ptr, U8 *data_ptr, U8 data_size, U8 last_face_index, EMsgVariableType type) const
{
	S32 face_index;
	S32 i;
	U64 exception_faces;
	U8 *start_loc = cur_ptr;

	htonmemcpy(cur_ptr,data_ptr + (last_face_index * data_size), type, data_size);
	cur_ptr += data_size;
	
	for (face_index = last_face_index-1; face_index >= 0; face_index--)
	{
		BOOL already_sent = FALSE;
		for (i = face_index+1; i <= last_face_index; i++)
		{ 
			if (!memcmp(data_ptr+(data_size *face_index), data_ptr+(data_size *i), data_size))
			{
				already_sent = TRUE;
				break;
			}
		}

		if (!already_sent)
		{
			exception_faces = 0;
			for (i = face_index; i >= 0; i--)
			{ 
				if (!memcmp(data_ptr+(data_size *face_index), data_ptr+(data_size *i), data_size))
				{
					exception_faces |= ((U64)1 << i); 
				}
			}
			
			//assign exception faces to cur_ptr
			if (exception_faces >= (0x1 << 7))
			{
				if (exception_faces >= (0x1 << 14))
				{
					if (exception_faces >= (0x1 << 21))
					{
						if (exception_faces >= (0x1 << 28))
						{
							*cur_ptr++ = (U8)(((exception_faces >> 28) & 0x7F) | 0x80);
						}
						*cur_ptr++ = (U8)(((exception_faces >> 21) & 0x7F) | 0x80);
					}
					*cur_ptr++ = (U8)(((exception_faces >> 14) & 0x7F) | 0x80);
				}
				*cur_ptr++ = (U8)(((exception_faces >> 7) & 0x7F) | 0x80);
			}
			
			*cur_ptr++ = (U8)(exception_faces & 0x7F);
			
			htonmemcpy(cur_ptr,data_ptr + (face_index * data_size), type, data_size);
			cur_ptr += data_size;
   		}
	}
	return (S32)(cur_ptr - start_loc);
}

S32 LLPrimitive::unpackTEField(U8 *cur_ptr, U8 *buffer_end, U8 *data_ptr, U8 data_size, U8 face_count, EMsgVariableType type)
{
	U8 *start_loc = cur_ptr;
	U64 i;
	htonmemcpy(data_ptr,cur_ptr, type,data_size);
	cur_ptr += data_size;

	for (i = 1; i < face_count; i++)
	{
		// Already unswizzled, don't need to unswizzle it again!
		memcpy(data_ptr+(i*data_size),data_ptr,data_size);	/* Flawfinder: ignore */ 
	}
	
	while ((cur_ptr < buffer_end) && (*cur_ptr != 0))
	{
		LL_DEBUGS("TEFieldDecode") << "TE exception" << LL_ENDL;
		i = 0;
		while (*cur_ptr & 0x80)
		{
			i |= ((*cur_ptr++) & 0x7F);
			i = i << 7;
		}

		i |= *cur_ptr++;

		for (S32 j = 0; j < face_count; j++)
		{
			if (i & 0x01)
			{
				htonmemcpy(data_ptr+(j*data_size),cur_ptr,type,data_size);
				LL_DEBUGS("TEFieldDecode") << "Assigning " ;
				char foo[64];
				sprintf(foo,"%x %x",*(data_ptr+(j*data_size)), *(data_ptr+(j*data_size)+1));
				LL_CONT << foo << " to face " << j << LL_ENDL;
			}
			i = i >> 1;
		}
		cur_ptr += data_size;		
	}
	llassert(cur_ptr <= buffer_end); // buffer underrun
	return (S32)(cur_ptr - start_loc);
}


// Pack information about all texture entries into container:
// { TextureEntry Variable 2 }
// Includes information about image ID, color, scale S,T, offset S,T and rotation
BOOL LLPrimitive::packTEMessage(LLMessageSystem *mesgsys) const
{
	const U32 MAX_TES = 32;

	U8     image_ids[MAX_TES*16];
	U8     colors[MAX_TES*4];
	F32    scale_s[MAX_TES];
	F32    scale_t[MAX_TES];
	S16    offset_s[MAX_TES];
	S16    offset_t[MAX_TES];
	S16    image_rot[MAX_TES];
	U8	   bump[MAX_TES];
	U8	   media_flags[MAX_TES];
    U8     glow[MAX_TES];
	U8     material_data[MAX_TES*16];
	
	const U32 MAX_TE_BUFFER = 4096;
	U8 packed_buffer[MAX_TE_BUFFER];
	U8 *cur_ptr = packed_buffer;
	
	S32 last_face_index = llmin((U32) getNumTEs(), MAX_TES) - 1;
	
	if (last_face_index > -1)
	{
		// ...if we hit the front, send one image id
		S8 face_index;
		LLColor4U coloru;
		for (face_index = 0; face_index <= last_face_index; face_index++)
		{
			// Directly sending image_ids is not safe!
			memcpy(&image_ids[face_index*16],getTE(face_index)->getID().mData,16);	/* Flawfinder: ignore */ 

			// Cast LLColor4 to LLColor4U
			coloru.setVec( getTE(face_index)->getColor() );

			// Note:  This is an optimization to send common colors (1.f, 1.f, 1.f, 1.f)
			// as all zeros.  However, the subtraction and addition must be done in unsigned
			// byte space, not in float space, otherwise off-by-one errors occur. JC
			colors[4*face_index]     = 255 - coloru.mV[0];
			colors[4*face_index + 1] = 255 - coloru.mV[1];
			colors[4*face_index + 2] = 255 - coloru.mV[2];
			colors[4*face_index + 3] = 255 - coloru.mV[3];

			const LLTextureEntry* te = getTE(face_index);
			scale_s[face_index] = (F32) te->mScaleS;
			scale_t[face_index] = (F32) te->mScaleT;
			offset_s[face_index] = (S16) ll_round((llclamp(te->mOffsetS,-1.0f,1.0f) * (F32)0x7FFF)) ;
			offset_t[face_index] = (S16) ll_round((llclamp(te->mOffsetT,-1.0f,1.0f) * (F32)0x7FFF)) ;
			image_rot[face_index] = (S16) ll_round(((fmod(te->mRotation, F_TWO_PI)/F_TWO_PI) * TEXTURE_ROTATION_PACK_FACTOR));
			bump[face_index] = te->getBumpShinyFullbright();
			media_flags[face_index] = te->getMediaTexGen();
			glow[face_index] = (U8) ll_round((llclamp(te->getGlow(), 0.0f, 1.0f) * (F32)0xFF));

			// Directly sending material_ids is not safe!
			memcpy(&material_data[face_index*16],getTE(face_index)->getMaterialID().get(),16);	/* Flawfinder: ignore */ 
		}

		cur_ptr += packTEField(cur_ptr, (U8 *)image_ids, sizeof(LLUUID),last_face_index, MVT_LLUUID);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)colors, 4 ,last_face_index, MVT_U8);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)scale_s, 4 ,last_face_index, MVT_F32);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)scale_t, 4 ,last_face_index, MVT_F32);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)offset_s, 2 ,last_face_index, MVT_S16Array);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)offset_t, 2 ,last_face_index, MVT_S16Array);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)image_rot, 2 ,last_face_index, MVT_S16Array);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)bump, 1 ,last_face_index, MVT_U8);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)media_flags, 1 ,last_face_index, MVT_U8);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)glow, 1 ,last_face_index, MVT_U8);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)material_data, 16, last_face_index, MVT_LLUUID);
	}
   	mesgsys->addBinaryDataFast(_PREHASH_TextureEntry, packed_buffer, (S32)(cur_ptr - packed_buffer));

	return FALSE;
}


BOOL LLPrimitive::packTEMessage(LLDataPacker &dp) const
{
	const U32 MAX_TES = 32;

	U8     image_ids[MAX_TES*16];
	U8     colors[MAX_TES*4];
	F32    scale_s[MAX_TES];
	F32    scale_t[MAX_TES];
	S16    offset_s[MAX_TES];
	S16    offset_t[MAX_TES];
	S16    image_rot[MAX_TES];
	U8	   bump[MAX_TES];
	U8	   media_flags[MAX_TES];
    U8     glow[MAX_TES];
	U8     material_data[MAX_TES*16];
	
	const U32 MAX_TE_BUFFER = 4096;
	U8 packed_buffer[MAX_TE_BUFFER];
	U8 *cur_ptr = packed_buffer;
	
	S32 last_face_index = getNumTEs() - 1;
	
	if (last_face_index > -1)
	{
		// ...if we hit the front, send one image id
		S8 face_index;
		LLColor4U coloru;
		for (face_index = 0; face_index <= last_face_index; face_index++)
		{
			// Directly sending image_ids is not safe!
			memcpy(&image_ids[face_index*16],getTE(face_index)->getID().mData,16);	/* Flawfinder: ignore */ 

			// Cast LLColor4 to LLColor4U
			coloru.setVec( getTE(face_index)->getColor() );

			// Note:  This is an optimization to send common colors (1.f, 1.f, 1.f, 1.f)
			// as all zeros.  However, the subtraction and addition must be done in unsigned
			// byte space, not in float space, otherwise off-by-one errors occur. JC
			colors[4*face_index]     = 255 - coloru.mV[0];
			colors[4*face_index + 1] = 255 - coloru.mV[1];
			colors[4*face_index + 2] = 255 - coloru.mV[2];
			colors[4*face_index + 3] = 255 - coloru.mV[3];

			const LLTextureEntry* te = getTE(face_index);
			scale_s[face_index] = (F32) te->mScaleS;
			scale_t[face_index] = (F32) te->mScaleT;
			offset_s[face_index] = (S16) ll_round((llclamp(te->mOffsetS,-1.0f,1.0f) * (F32)0x7FFF)) ;
			offset_t[face_index] = (S16) ll_round((llclamp(te->mOffsetT,-1.0f,1.0f) * (F32)0x7FFF)) ;
			image_rot[face_index] = (S16) ll_round(((fmod(te->mRotation, F_TWO_PI)/F_TWO_PI) * TEXTURE_ROTATION_PACK_FACTOR));
			bump[face_index] = te->getBumpShinyFullbright();
			media_flags[face_index] = te->getMediaTexGen();
            glow[face_index] = (U8) ll_round((llclamp(te->getGlow(), 0.0f, 1.0f) * (F32)0xFF));

			// Directly sending material_ids is not safe!
			memcpy(&material_data[face_index*16],getTE(face_index)->getMaterialID().get(),16);	/* Flawfinder: ignore */ 
		}

		cur_ptr += packTEField(cur_ptr, (U8 *)image_ids, sizeof(LLUUID),last_face_index, MVT_LLUUID);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)colors, 4 ,last_face_index, MVT_U8);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)scale_s, 4 ,last_face_index, MVT_F32);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)scale_t, 4 ,last_face_index, MVT_F32);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)offset_s, 2 ,last_face_index, MVT_S16Array);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)offset_t, 2 ,last_face_index, MVT_S16Array);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)image_rot, 2 ,last_face_index, MVT_S16Array);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)bump, 1 ,last_face_index, MVT_U8);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)media_flags, 1 ,last_face_index, MVT_U8);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)glow, 1 ,last_face_index, MVT_U8);
		*cur_ptr++ = 0;
		cur_ptr += packTEField(cur_ptr, (U8 *)material_data, 16, last_face_index, MVT_LLUUID);
	}

	dp.packBinaryData(packed_buffer, (S32)(cur_ptr - packed_buffer), "TextureEntry");
	return FALSE;
}

S32 LLPrimitive::parseTEMessage(LLMessageSystem* mesgsys, char const* block_name, const S32 block_num, LLTEContents& tec)
{
	S32 retval = 0;
   // temp buffer for material ID processing
   // data will end up in tec.material_id[]	
   U8 material_data[LLTEContents::MAX_TES*16];

	if (block_num < 0)
	{
		tec.size = mesgsys->getSizeFast(block_name, _PREHASH_TextureEntry);
	}
	else
	{
		tec.size = mesgsys->getSizeFast(block_name, block_num, _PREHASH_TextureEntry);
	}

	if (tec.size == 0)
	{
		tec.face_count = 0;
		return retval;
	}

	if (block_num < 0)
	{
		mesgsys->getBinaryDataFast(block_name, _PREHASH_TextureEntry, tec.packed_buffer, 0, 0, LLTEContents::MAX_TE_BUFFER);
	}
	else
	{
		mesgsys->getBinaryDataFast(block_name, _PREHASH_TextureEntry, tec.packed_buffer, 0, block_num, LLTEContents::MAX_TE_BUFFER);
	}

	tec.face_count = llmin((U32)getNumTEs(),(U32)LLTEContents::MAX_TES);

	U8 *cur_ptr = tec.packed_buffer;
	cur_ptr += unpackTEField(cur_ptr, tec.packed_buffer+tec.size, (U8 *)tec.image_data, 16, tec.face_count, MVT_LLUUID);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, tec.packed_buffer+tec.size, (U8 *)tec.colors, 4, tec.face_count, MVT_U8);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, tec.packed_buffer+tec.size, (U8 *)tec.scale_s, 4, tec.face_count, MVT_F32);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, tec.packed_buffer+tec.size, (U8 *)tec.scale_t, 4, tec.face_count, MVT_F32);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, tec.packed_buffer+tec.size, (U8 *)tec.offset_s, 2, tec.face_count, MVT_S16Array);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, tec.packed_buffer+tec.size, (U8 *)tec.offset_t, 2, tec.face_count, MVT_S16Array);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, tec.packed_buffer+tec.size, (U8 *)tec.image_rot, 2, tec.face_count, MVT_S16Array);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, tec.packed_buffer+tec.size, (U8 *)tec.bump, 1, tec.face_count, MVT_U8);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, tec.packed_buffer+tec.size, (U8 *)tec.media_flags, 1, tec.face_count, MVT_U8);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, tec.packed_buffer+tec.size, (U8 *)tec.glow, 1, tec.face_count, MVT_U8);

	if (cur_ptr < tec.packed_buffer + tec.size)
	{
		cur_ptr++;
		cur_ptr += unpackTEField(cur_ptr, tec.packed_buffer+tec.size, (U8 *)material_data, 16, tec.face_count, MVT_LLUUID);
	}
	else
	{
		memset(material_data, 0, sizeof(material_data));
	}
	
	for (U32 i = 0; i < tec.face_count; i++)
	{
		tec.material_ids[i].set(&material_data[i * 16]);
	}
	
	retval = 1;
	return retval;
	}
	
S32 LLPrimitive::applyParsedTEMessage(LLTEContents& tec)
{
	S32 retval = 0;
	
	LLColor4 color;
	LLColor4U coloru;
	for (U32 i = 0; i < tec.face_count; i++)
	{
		LLUUID& req_id = ((LLUUID*)tec.image_data)[i];
		retval |= setTETexture(i, req_id);
		retval |= setTEScale(i, tec.scale_s[i], tec.scale_t[i]);
		retval |= setTEOffset(i, (F32)tec.offset_s[i] / (F32)0x7FFF, (F32) tec.offset_t[i] / (F32) 0x7FFF);
		retval |= setTERotation(i, ((F32)tec.image_rot[i] / TEXTURE_ROTATION_PACK_FACTOR) * F_TWO_PI);
		retval |= setTEBumpShinyFullbright(i, tec.bump[i]);
		retval |= setTEMediaTexGen(i, tec.media_flags[i]);
		retval |= setTEGlow(i, (F32)tec.glow[i] / (F32)0xFF);
		retval |= setTEMaterialID(i, tec.material_ids[i]);

		coloru = LLColor4U(tec.colors + 4*i);

		// Note:  This is an optimization to send common colors (1.f, 1.f, 1.f, 1.f)
		// as all zeros.  However, the subtraction and addition must be done in unsigned
		// byte space, not in float space, otherwise off-by-one errors occur. JC
		color.mV[VRED]		= F32(255 - coloru.mV[VRED])   / 255.f;
		color.mV[VGREEN]	= F32(255 - coloru.mV[VGREEN]) / 255.f;
		color.mV[VBLUE]		= F32(255 - coloru.mV[VBLUE])  / 255.f;
		color.mV[VALPHA]	= F32(255 - coloru.mV[VALPHA]) / 255.f;

		retval |= setTEColor(i, color);

	}

	return retval;
}

S32 LLPrimitive::unpackTEMessage(LLMessageSystem* mesgsys, char const* block_name, const S32 block_num)
{
	LLTEContents tec;
	S32 retval = parseTEMessage(mesgsys, block_name, block_num, tec);
	if (!retval)
		return retval;
	return applyParsedTEMessage(tec);
}

S32 LLPrimitive::unpackTEMessage(LLDataPacker &dp)
{
	// use a negative block_num to indicate a single-block read (a non-variable block)
	S32 retval = 0;
	const U32 MAX_TES = 32;

	// Avoid construction of 32 UUIDs per call
	static LLUUID image_ids[MAX_TES];
	static LLMaterialID material_ids[MAX_TES];

	U8     image_data[MAX_TES*16];
	U8	   colors[MAX_TES*4];
	F32    scale_s[MAX_TES];
	F32    scale_t[MAX_TES];
	S16    offset_s[MAX_TES];
	S16    offset_t[MAX_TES];
	S16    image_rot[MAX_TES];
	U8	   bump[MAX_TES];
	U8	   media_flags[MAX_TES];
    U8     glow[MAX_TES];
	U8     material_data[MAX_TES*16];

	const U32 MAX_TE_BUFFER = 4096;
	U8 packed_buffer[MAX_TE_BUFFER];
	U8 *cur_ptr = packed_buffer;

	S32 size;
	U32 face_count = 0;

	if (!dp.unpackBinaryData(packed_buffer, size, "TextureEntry"))
	{
		retval = TEM_INVALID;
		LL_WARNS() << "Bad texture entry block!  Abort!" << LL_ENDL;
		return retval;
	}

	if (size == 0)
	{
		return retval;
	}

	face_count = llmin((U32) getNumTEs(), MAX_TES);
	U32 i;

	cur_ptr += unpackTEField(cur_ptr, packed_buffer+size, (U8 *)image_data, 16, face_count, MVT_LLUUID);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, packed_buffer+size, (U8 *)colors, 4, face_count, MVT_U8);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, packed_buffer+size, (U8 *)scale_s, 4, face_count, MVT_F32);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, packed_buffer+size, (U8 *)scale_t, 4, face_count, MVT_F32);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, packed_buffer+size, (U8 *)offset_s, 2, face_count, MVT_S16Array);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, packed_buffer+size, (U8 *)offset_t, 2, face_count, MVT_S16Array);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, packed_buffer+size, (U8 *)image_rot, 2, face_count, MVT_S16Array);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, packed_buffer+size, (U8 *)bump, 1, face_count, MVT_U8);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, packed_buffer+size, (U8 *)media_flags, 1, face_count, MVT_U8);
	cur_ptr++;
	cur_ptr += unpackTEField(cur_ptr, packed_buffer+size, (U8 *)glow, 1, face_count, MVT_U8);
	if (cur_ptr < packed_buffer + size)
	{
		cur_ptr++;
		cur_ptr += unpackTEField(cur_ptr, packed_buffer+size, (U8 *)material_data, 16, face_count, MVT_LLUUID);
	}
	else
	{
		memset(material_data, 0, sizeof(material_data));
	}

	for (i = 0; i < face_count; i++)
	{
		memcpy(image_ids[i].mData,&image_data[i*16],16);	/* Flawfinder: ignore */ 	
		material_ids[i].set(&material_data[i * 16]);
	}
	
	LLColor4 color;
	LLColor4U coloru;
	for (i = 0; i < face_count; i++)
	{
		retval |= setTETexture(i, image_ids[i]);
		retval |= setTEScale(i, scale_s[i], scale_t[i]);
		retval |= setTEOffset(i, (F32)offset_s[i] / (F32)0x7FFF, (F32) offset_t[i] / (F32) 0x7FFF);
		retval |= setTERotation(i, ((F32)image_rot[i] / TEXTURE_ROTATION_PACK_FACTOR) * F_TWO_PI);
		retval |= setTEBumpShinyFullbright(i, bump[i]);
		retval |= setTEMediaTexGen(i, media_flags[i]);
		retval |= setTEGlow(i, (F32)glow[i] / (F32)0xFF);
		retval |= setTEMaterialID(i, material_ids[i]);
		coloru = LLColor4U(colors + 4*i);

		// Note:  This is an optimization to send common colors (1.f, 1.f, 1.f, 1.f)
		// as all zeros.  However, the subtraction and addition must be done in unsigned
		// byte space, not in float space, otherwise off-by-one errors occur. JC
		color.mV[VRED]		= F32(255 - coloru.mV[VRED])   / 255.f;
		color.mV[VGREEN]	= F32(255 - coloru.mV[VGREEN]) / 255.f;
		color.mV[VBLUE]		= F32(255 - coloru.mV[VBLUE])  / 255.f;
		color.mV[VALPHA]	= F32(255 - coloru.mV[VALPHA]) / 255.f;

		retval |= setTEColor(i, color);
	}

	return retval;
}

U8	LLPrimitive::getExpectedNumTEs() const
{
	U8 expected_face_count = 0;
	if (mVolumep)
	{
		expected_face_count = mVolumep->getNumFaces();
	}
	return expected_face_count;
}

void LLPrimitive::copyTextureList(const LLPrimTextureList& other_list)
{
	mTextureList.copy(other_list);
}

void LLPrimitive::takeTextureList(LLPrimTextureList& other_list)
{
	mTextureList.take(other_list);
}

void LLPrimitive::updateNumBumpmap(const U8 index, const U8 bump)
{
	LLTextureEntry* te = getTE(index);
	if(!te)
	{
		return;
	}

	U8 old_bump = te->getBumpmap();	
	if(old_bump > 0)
	{
		mNumBumpmapTEs--;
	}
	if((bump & TEM_BUMP_MASK) > 0)
	{
		mNumBumpmapTEs++;
	}

	return;
}
//============================================================================

// Moved from llselectmgr.cpp
// BUG: Only works for boxes.
// Face numbering for flex boxes as of 1.14.2

// static
bool LLPrimitive::getTESTAxes(const U8 face, U32* s_axis, U32* t_axis)
{
	if (face == 0)
	{
		*s_axis = VX; *t_axis = VY;
		return true;
	}
	else if (face == 1)
	{
		*s_axis = VX; *t_axis = VZ;
		return true;
	}
	else if (face == 2)
	{
		*s_axis = VY; *t_axis = VZ;
		return true;
	}
	else if (face == 3)
	{
		*s_axis = VX; *t_axis = VZ;
		return true;
	}
	else if (face == 4)
	{
		*s_axis = VY; *t_axis = VZ;
		return true;
	}
	else if (face == 5)
	{
		*s_axis = VX; *t_axis = VY;
		return true;
	}
	else
	{
		// unknown face
		return false;
	}
}

//============================================================================

//static 
BOOL LLNetworkData::isValid(U16 param_type, U32 size)
{
	// ew - better mechanism needed
	
	switch(param_type)
	{
	case PARAMS_FLEXIBLE:
		return (size == 16);
	case PARAMS_LIGHT:
		return (size == 16);
	case PARAMS_SCULPT:
		return (size == 17);
	case PARAMS_LIGHT_IMAGE:
		return (size == 28);
	}
	
	return FALSE;
}

//============================================================================

LLLightParams::LLLightParams()
{
	mColor.setToWhite();
	mRadius = 10.f;
	mCutoff = 0.0f;
	mFalloff = 0.75f;

	mType = PARAMS_LIGHT;
}

BOOL LLLightParams::pack(LLDataPacker &dp) const
{
	LLColor4U color4u(mColor);
	dp.packColor4U(color4u, "color");
	dp.packF32(mRadius, "radius");
	dp.packF32(mCutoff, "cutoff");
	dp.packF32(mFalloff, "falloff");
	return TRUE;
}

BOOL LLLightParams::unpack(LLDataPacker &dp)
{
	LLColor4U color;
	dp.unpackColor4U(color, "color");
	setColor(LLColor4(color));

	F32 radius;
	dp.unpackF32(radius, "radius");
	setRadius(radius);

	F32 cutoff;
	dp.unpackF32(cutoff, "cutoff");
	setCutoff(cutoff);

	F32 falloff;
	dp.unpackF32(falloff, "falloff");
	setFalloff(falloff);
	
	return TRUE;
}

bool LLLightParams::operator==(const LLNetworkData& data) const
{
	if (data.mType != PARAMS_LIGHT)
	{
		return false;
	}
	const LLLightParams *param = (const LLLightParams*)&data;
	if (param->mColor != mColor ||
		param->mRadius != mRadius ||
		param->mCutoff != mCutoff ||
		param->mFalloff != mFalloff)
	{
		return false;
	}
	return true;
}

void LLLightParams::copy(const LLNetworkData& data)
{
	const LLLightParams *param = (LLLightParams*)&data;
	mType = param->mType;
	mColor = param->mColor;
	mRadius = param->mRadius;
	mCutoff = param->mCutoff;
	mFalloff = param->mFalloff;
}

LLSD LLLightParams::asLLSD() const
{
	LLSD sd;
	
	sd["color"] = ll_sd_from_color4(getColor());
	sd["radius"] = getRadius();
	sd["falloff"] = getFalloff();
	sd["cutoff"] = getCutoff();
		
	return sd;
}

bool LLLightParams::fromLLSD(LLSD& sd)
{
	const char *w;
	w = "color";
	if (sd.has(w))
	{
		setColor( ll_color4_from_sd(sd["color"]) );
	} else goto fail;
	w = "radius";
	if (sd.has(w))
	{
		setRadius( (F32)sd[w].asReal() );
	} else goto fail;
	w = "falloff";
	if (sd.has(w))
	{
		setFalloff( (F32)sd[w].asReal() );
	} else goto fail;
	w = "cutoff";
	if (sd.has(w))
	{
		setCutoff( (F32)sd[w].asReal() );
	} else goto fail;
	
	return true;
 fail:
	return false;
}

//============================================================================

LLFlexibleObjectData::LLFlexibleObjectData()
{
	mSimulateLOD				= FLEXIBLE_OBJECT_DEFAULT_NUM_SECTIONS;
	mGravity					= FLEXIBLE_OBJECT_DEFAULT_GRAVITY;
	mAirFriction				= FLEXIBLE_OBJECT_DEFAULT_AIR_FRICTION;
	mWindSensitivity			= FLEXIBLE_OBJECT_DEFAULT_WIND_SENSITIVITY;
	mTension					= FLEXIBLE_OBJECT_DEFAULT_TENSION;
	//mUsingCollisionSphere		= FLEXIBLE_OBJECT_DEFAULT_USING_COLLISION_SPHERE;
	//mRenderingCollisionSphere	= FLEXIBLE_OBJECT_DEFAULT_RENDERING_COLLISION_SPHERE;
	mUserForce					= LLVector3(0.f, 0.f, 0.f);

	mType = PARAMS_FLEXIBLE;
}

BOOL LLFlexibleObjectData::pack(LLDataPacker &dp) const
{
	// Custom, uber-svelte pack "softness" in upper bits of tension & drag
	U8 bit1 = (mSimulateLOD & 2) << 6;
	U8 bit2 = (mSimulateLOD & 1) << 7;
	dp.packU8((U8)(mTension*10.01f) + bit1, "tension");
	dp.packU8((U8)(mAirFriction*10.01f) + bit2, "drag");
	dp.packU8((U8)((mGravity+10.f)*10.01f), "gravity");
	dp.packU8((U8)(mWindSensitivity*10.01f), "wind");
	dp.packVector3(mUserForce, "userforce");
	return TRUE;
}

BOOL LLFlexibleObjectData::unpack(LLDataPacker &dp)
{
	U8 tension, friction, gravity, wind;
	U8 bit1, bit2;
	dp.unpackU8(tension, "tension");	bit1 = (tension >> 6) & 2;
										mTension = ((F32)(tension&0x7f))/10.f;
	dp.unpackU8(friction, "drag");		bit2 = (friction >> 7) & 1;
										mAirFriction = ((F32)(friction&0x7f))/10.f;
										mSimulateLOD = bit1 | bit2;
	dp.unpackU8(gravity, "gravity");	mGravity = ((F32)gravity)/10.f - 10.f;
	dp.unpackU8(wind, "wind");			mWindSensitivity = ((F32)wind)/10.f;
	if (dp.hasNext())
	{
		dp.unpackVector3(mUserForce, "userforce");
	}
	else
	{
		mUserForce.setVec(0.f, 0.f, 0.f);
	}
	return TRUE;
}

bool LLFlexibleObjectData::operator==(const LLNetworkData& data) const
{
	if (data.mType != PARAMS_FLEXIBLE)
	{
		return false;
	}
	LLFlexibleObjectData *flex_data = (LLFlexibleObjectData*)&data;
	return (mSimulateLOD == flex_data->mSimulateLOD &&
			mGravity == flex_data->mGravity &&
			mAirFriction == flex_data->mAirFriction &&
			mWindSensitivity == flex_data->mWindSensitivity &&
			mTension == flex_data->mTension &&
			mUserForce == flex_data->mUserForce);
			//mUsingCollisionSphere == flex_data->mUsingCollisionSphere &&
			//mRenderingCollisionSphere == flex_data->mRenderingCollisionSphere
}

void LLFlexibleObjectData::copy(const LLNetworkData& data)
{
	const LLFlexibleObjectData *flex_data = (LLFlexibleObjectData*)&data;
	mSimulateLOD = flex_data->mSimulateLOD;
	mGravity = flex_data->mGravity;
	mAirFriction = flex_data->mAirFriction;
	mWindSensitivity = flex_data->mWindSensitivity;
	mTension = flex_data->mTension;
	mUserForce = flex_data->mUserForce;
	//mUsingCollisionSphere = flex_data->mUsingCollisionSphere;
	//mRenderingCollisionSphere = flex_data->mRenderingCollisionSphere;
}

LLSD LLFlexibleObjectData::asLLSD() const
{
	LLSD sd;

	sd["air_friction"] = getAirFriction();
	sd["gravity"] = getGravity();
	sd["simulate_lod"] = getSimulateLOD();
	sd["tension"] = getTension();
	sd["user_force"] = getUserForce().getValue();
	sd["wind_sensitivity"] = getWindSensitivity();
	
	return sd;
}

bool LLFlexibleObjectData::fromLLSD(LLSD& sd)
{
	const char *w;
	w = "air_friction";
	if (sd.has(w))
	{
		setAirFriction( (F32)sd[w].asReal() );
	} else goto fail;
	w = "gravity";
	if (sd.has(w))
	{
		setGravity( (F32)sd[w].asReal() );
	} else goto fail;
	w = "simulate_lod";
	if (sd.has(w))
	{
		setSimulateLOD( sd[w].asInteger() );
	} else goto fail;
	w = "tension";
	if (sd.has(w))
	{
		setTension( (F32)sd[w].asReal() );
	} else goto fail;
	w = "user_force";
	if (sd.has(w))
	{
		LLVector3 user_force = ll_vector3_from_sd(sd[w], 0);
		setUserForce( user_force );
	} else goto fail;
	w = "wind_sensitivity";
	if (sd.has(w))
	{
		setWindSensitivity( (F32)sd[w].asReal() );
	} else goto fail;
	
	return true;
 fail:
	return false;
}

//============================================================================

LLSculptParams::LLSculptParams()
{
	mType = PARAMS_SCULPT;
	mSculptTexture.set(SCULPT_DEFAULT_TEXTURE);
	mSculptType = LL_SCULPT_TYPE_SPHERE;
}

BOOL LLSculptParams::pack(LLDataPacker &dp) const
{
	dp.packUUID(mSculptTexture, "texture");
	dp.packU8(mSculptType, "type");
	
	return TRUE;
}

BOOL LLSculptParams::unpack(LLDataPacker &dp)
{
	U8 type;
	LLUUID id;
	dp.unpackUUID(id, "texture");
	dp.unpackU8(type, "type");

	setSculptTexture(id, type);
	return TRUE;
}

bool LLSculptParams::operator==(const LLNetworkData& data) const
{
	if (data.mType != PARAMS_SCULPT)
	{
		return false;
	}
	
	const LLSculptParams *param = (const LLSculptParams*)&data;
	if ( (param->mSculptTexture != mSculptTexture) ||
		 (param->mSculptType != mSculptType) )
		 
	{
		return false;
	}
	
	return true;
}

void LLSculptParams::copy(const LLNetworkData& data)
{
	const LLSculptParams *param = (LLSculptParams*)&data;
	setSculptTexture(param->mSculptTexture, param->mSculptType);
}



LLSD LLSculptParams::asLLSD() const
{
	LLSD sd;
	
	sd["texture"] = mSculptTexture;
	sd["type"] = mSculptType;
		
	return sd;
}

bool LLSculptParams::fromLLSD(LLSD& sd)
{
	const char *w;
	U8 type;
	w = "type";
	if (sd.has(w))
	{
		type = sd[w].asInteger();
	}
	else return false;

	w = "texture";
	if (sd.has(w))
	{
		setSculptTexture(sd[w], type);
	}
	else return false;

	return true;
}

void LLSculptParams::setSculptTexture(const LLUUID& texture_id, U8 sculpt_type)
{
	U8 type = sculpt_type & LL_SCULPT_TYPE_MASK;
	U8 flags = sculpt_type & LL_SCULPT_FLAG_MASK;
	if (sculpt_type != (type | flags) || type > LL_SCULPT_TYPE_MAX)
	{
		mSculptTexture.set(SCULPT_DEFAULT_TEXTURE);
		mSculptType = LL_SCULPT_TYPE_SPHERE;
	}
	else
	{
		mSculptTexture = texture_id;
		mSculptType = sculpt_type;
	}
}

//============================================================================

LLLightImageParams::LLLightImageParams()
{
	mType = PARAMS_LIGHT_IMAGE;
	mParams.setVec(F_PI*0.5f, 0.f, 0.f);
}

BOOL LLLightImageParams::pack(LLDataPacker &dp) const
{
	dp.packUUID(mLightTexture, "texture");
	dp.packVector3(mParams, "params");

	return TRUE;
}

BOOL LLLightImageParams::unpack(LLDataPacker &dp)
{
	dp.unpackUUID(mLightTexture, "texture");
	dp.unpackVector3(mParams, "params");
	
	return TRUE;
}

bool LLLightImageParams::operator==(const LLNetworkData& data) const
{
	if (data.mType != PARAMS_LIGHT_IMAGE)
	{
		return false;
	}
	
	const LLLightImageParams *param = (const LLLightImageParams*)&data;
	if ( (param->mLightTexture != mLightTexture) )
	{
		return false;
	}

	if ( (param->mParams != mParams ) )
	{
		return false;
	}
	
	return true;
}

void LLLightImageParams::copy(const LLNetworkData& data)
{
	const LLLightImageParams *param = (LLLightImageParams*)&data;
	mLightTexture = param->mLightTexture;
	mParams = param->mParams;
}



LLSD LLLightImageParams::asLLSD() const
{
	LLSD sd;
	
	sd["texture"] = mLightTexture;
	sd["params"] = mParams.getValue();
		
	return sd;
}

bool LLLightImageParams::fromLLSD(LLSD& sd)
{
	if (sd.has("texture"))
	{
		setLightTexture( sd["texture"] );
		setParams( LLVector3( sd["params"] ) );
		return true;
	} 
	
	return false;
}