aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/newview/llmaniprotate.cpp
blob: 77c8bd5b42723a6f4b9a2cb6a6ff312912208429 (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
/** 
 * @file llmaniprotate.cpp
 * @brief LLManipRotate class implementation
 *
 * $LicenseInfo:firstyear=2002&license=viewergpl$
 * 
 * Copyright (c) 2002-2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llmaniprotate.h"

// library includes
#include "llmath.h"
#include "llgl.h"
#include "llrender.h"
#include "v4color.h"
#include "llprimitive.h"
#include "llview.h"
#include "llfontgl.h"

// viewer includes
#include "llagent.h"
#include "llbox.h"
#include "llbutton.h"
#include "llviewercontrol.h"
#include "llcriticaldamp.h"
#include "llhoverview.h"
#include "llfloatertools.h"
#include "llselectmgr.h"
#include "llstatusbar.h"
#include "llui.h"
#include "llvoavatar.h"
#include "llviewercamera.h"
#include "llviewerobject.h"
#include "llviewerwindow.h"
#include "llworld.h"
#include "pipeline.h"
#include "lldrawable.h"
#include "llglheaders.h"

#include "hippolimits.h"

const F32 RADIUS_PIXELS = 100.f;		// size in screen space
const F32 SQ_RADIUS = RADIUS_PIXELS * RADIUS_PIXELS;
const F32 WIDTH_PIXELS = 8;
const S32 CIRCLE_STEPS = 100;
const F32 DELTA = F_TWO_PI / CIRCLE_STEPS;
const F32 SIN_DELTA = sin( DELTA );
const F32 COS_DELTA = cos( DELTA );
const F32 MAX_MANIP_SELECT_DISTANCE = 100.f;
const F32 SNAP_ANGLE_INCREMENT = 5.625f;
const F32 SNAP_ANGLE_DETENTE = SNAP_ANGLE_INCREMENT;
const F32 SNAP_GUIDE_RADIUS_1 = 2.8f;
const F32 SNAP_GUIDE_RADIUS_2 = 2.4f;
const F32 SNAP_GUIDE_RADIUS_3 = 2.2f;
const F32 SNAP_GUIDE_RADIUS_4 = 2.1f;
const F32 SNAP_GUIDE_RADIUS_5 = 2.05f;
const F32 SNAP_GUIDE_INNER_RADIUS = 2.f;
const F32 AXIS_ONTO_CAM_TOLERANCE = cos( 80.f * DEG_TO_RAD );
const F32 SELECTED_MANIPULATOR_SCALE = 1.05f;
const F32 MANIPULATOR_SCALE_HALF_LIFE = 0.07f;

extern void handle_reset_rotation(void*);  // in LLViewerWindow

LLManipRotate::LLManipRotate( LLToolComposite* composite )
: 	LLManip( std::string("Rotate"), composite ),
	mRotationCenter(),
	mCenterScreen(),
	mRotation(),
	mMouseDown(),
	mMouseCur(),
	mRadiusMeters(0.f),
	mCenterToCam(),
	mCenterToCamNorm(),
	mCenterToCamMag(0.f),
	mCenterToProfilePlane(),
	mCenterToProfilePlaneMag(0.f),
	mSendUpdateOnMouseUp( FALSE ),
	mSmoothRotate( FALSE ),
	mCamEdgeOn(FALSE),
	mManipulatorScales(1.f, 1.f, 1.f, 1.f)
{ }

void LLManipRotate::handleSelect()
{
	// *FIX: put this in mouseDown?
	LLSelectMgr::getInstance()->saveSelectedObjectTransform(SELECT_ACTION_TYPE_PICK);
	gFloaterTools->setStatusText("rotate");
	LLManip::handleSelect();
}

void LLManipRotate::render()
{
	LLGLSUIDefault gls_ui;
	gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
	LLGLDepthTest gls_depth(GL_TRUE);
	LLGLEnable gl_blend(GL_BLEND);
	LLGLEnable gls_alpha_test(GL_ALPHA_TEST);
	
	// You can rotate if you can move
	LLViewerObject* first_object = mObjectSelection->getFirstMoveableObject(TRUE);
	if( !first_object )
	{
		return;
	}

	if( !updateVisiblity() )
	{
		return;
	}

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	if (mObjectSelection->getSelectType() == SELECT_TYPE_HUD)
	{
		F32 zoom = gAgent.mHUDCurZoom;
		glScalef(zoom, zoom, zoom);
	}


	LLVector3 center = gAgent.getPosAgentFromGlobal( mRotationCenter );

	LLColor4 highlight_outside( 1.f, 1.f, 0.f, 1.f );
	LLColor4 highlight_inside( 0.7f, 0.7f, 0.f, 0.5f );
	F32 width_meters = WIDTH_PIXELS * mRadiusMeters / RADIUS_PIXELS;

	glPushMatrix();
	{
		// are we in the middle of a constrained drag?
		if (mManipPart >= LL_ROT_X && mManipPart <= LL_ROT_Z)
		{
			renderSnapGuides();
		}
		else
		{
			LLGLEnable cull_face(GL_CULL_FACE);
			LLGLDepthTest gls_depth(GL_FALSE);
			glPushMatrix();
			{
				// Draw "sphere" (intersection of sphere with tangent cone that has apex at camera)
				glTranslatef( mCenterToProfilePlane.mV[VX], mCenterToProfilePlane.mV[VY], mCenterToProfilePlane.mV[VZ] );
				glTranslatef( center.mV[VX], center.mV[VY], center.mV[VZ] );

				// Inverse change of basis vectors
				LLVector3 forward = mCenterToCamNorm;
				LLVector3 left = gAgent.getUpAxis() % forward;
				left.normVec();
				LLVector3 up = forward % left;

				LLVector4 a(-forward);
				a.mV[3] = 0;
				LLVector4 b(up);
				b.mV[3] = 0;
				LLVector4 c(left);
				c.mV[3] = 0;
				LLMatrix4 mat;
				mat.initRows(a, b, c, LLVector4(0.f, 0.f, 0.f, 1.f));

				glMultMatrixf( &mat.mMatrix[0][0] );

				glRotatef( -90, 0.f, 1.f, 0.f);
				LLColor4 color;
				if (mManipPart == LL_ROT_ROLL || mHighlightedPart == LL_ROT_ROLL)
				{
					color.setVec(0.8f, 0.8f, 0.8f, 0.8f);
					glScalef(mManipulatorScales.mV[VW], mManipulatorScales.mV[VW], mManipulatorScales.mV[VW]);
				}
				else
				{
					color.setVec( 0.7f, 0.7f, 0.7f, 0.6f );
				}
				gl_washer_2d(mRadiusMeters + width_meters, mRadiusMeters, CIRCLE_STEPS, color, color);


				if (mManipPart == LL_NO_PART)
				{
					gGL.color4f( 0.7f, 0.7f, 0.7f, 0.3f );
					gl_circle_2d( 0, 0,  mRadiusMeters, CIRCLE_STEPS, TRUE );
				}
				
				GLdouble plane_eqn[] = { 0, 0, 1, 0 };
				glClipPlane( GL_CLIP_PLANE0, plane_eqn );
			}
			glPopMatrix();
		}

		glTranslatef( center.mV[VX], center.mV[VY], center.mV[VZ] );

		LLQuaternion rot;
		F32 angle_radians, x, y, z;

		LLVector3 grid_origin;
		LLVector3 grid_scale;
		LLQuaternion grid_rotation;

		LLSelectMgr::getInstance()->getGrid(grid_origin, grid_rotation, grid_scale);

		grid_rotation.getAngleAxis(&angle_radians, &x, &y, &z);
		glRotatef(angle_radians * RAD_TO_DEG, x, y, z);


		if (mManipPart == LL_ROT_Z)
		{
			mManipulatorScales = lerp(mManipulatorScales, LLVector4(1.f, 1.f, SELECTED_MANIPULATOR_SCALE, 1.f), LLCriticalDamp::getInterpolant(MANIPULATOR_SCALE_HALF_LIFE));
			glPushMatrix();
			{
				// selected part
				glScalef(mManipulatorScales.mV[VZ], mManipulatorScales.mV[VZ], mManipulatorScales.mV[VZ]);
				renderActiveRing( mRadiusMeters, width_meters, LLColor4( 0.f, 0.f, 1.f, 1.f) , LLColor4( 0.f, 0.f, 1.f, 0.3f ));
			}
			glPopMatrix();
		}
		else if (mManipPart == LL_ROT_Y)
		{
			mManipulatorScales = lerp(mManipulatorScales, LLVector4(1.f, SELECTED_MANIPULATOR_SCALE, 1.f, 1.f), LLCriticalDamp::getInterpolant(MANIPULATOR_SCALE_HALF_LIFE));
			glPushMatrix();
			{
				glRotatef( 90.f, 1.f, 0.f, 0.f );
				glScalef(mManipulatorScales.mV[VY], mManipulatorScales.mV[VY], mManipulatorScales.mV[VY]);
				renderActiveRing( mRadiusMeters, width_meters, LLColor4( 0.f, 1.f, 0.f, 1.f), LLColor4( 0.f, 1.f, 0.f, 0.3f));
			}
			glPopMatrix();
		}
		else if (mManipPart == LL_ROT_X)
		{
			mManipulatorScales = lerp(mManipulatorScales, LLVector4(SELECTED_MANIPULATOR_SCALE, 1.f, 1.f, 1.f), LLCriticalDamp::getInterpolant(MANIPULATOR_SCALE_HALF_LIFE));
			glPushMatrix();
			{
				glRotatef( 90.f, 0.f, 1.f, 0.f );
				glScalef(mManipulatorScales.mV[VX], mManipulatorScales.mV[VX], mManipulatorScales.mV[VX]);
				renderActiveRing( mRadiusMeters, width_meters, LLColor4( 1.f, 0.f, 0.f, 1.f), LLColor4( 1.f, 0.f, 0.f, 0.3f));
			}
			glPopMatrix();
		}
		else if (mManipPart == LL_ROT_ROLL)
		{
			mManipulatorScales = lerp(mManipulatorScales, LLVector4(1.f, 1.f, 1.f, SELECTED_MANIPULATOR_SCALE), LLCriticalDamp::getInterpolant(MANIPULATOR_SCALE_HALF_LIFE));
		}
		else if (mManipPart == LL_NO_PART)
		{
			if (mHighlightedPart == LL_NO_PART)
			{
				mManipulatorScales = lerp(mManipulatorScales, LLVector4(1.f, 1.f, 1.f, 1.f), LLCriticalDamp::getInterpolant(MANIPULATOR_SCALE_HALF_LIFE));
			}

			LLGLEnable cull_face(GL_CULL_FACE);
			LLGLEnable clip_plane0(GL_CLIP_PLANE0);
			LLGLDepthTest gls_depth(GL_FALSE);

			// First pass: centers. Second pass: sides.
			for( S32 i=0; i<2; i++ )
			{
				glPushMatrix();
				{
					if (mHighlightedPart == LL_ROT_Z)
					{
						mManipulatorScales = lerp(mManipulatorScales, LLVector4(1.f, 1.f, SELECTED_MANIPULATOR_SCALE, 1.f), LLCriticalDamp::getInterpolant(MANIPULATOR_SCALE_HALF_LIFE));
						glScalef(mManipulatorScales.mV[VZ], mManipulatorScales.mV[VZ], mManipulatorScales.mV[VZ]);
						// hovering over part
						gl_ring( mRadiusMeters, width_meters, LLColor4( 0.f, 0.f, 1.f, 1.f ), LLColor4( 0.f, 0.f, 1.f, 0.5f ), CIRCLE_STEPS, i);
					}
					else
					{
						// default
						gl_ring( mRadiusMeters, width_meters, LLColor4( 0.f, 0.f, 0.8f, 0.8f ), LLColor4( 0.f, 0.f, 0.8f, 0.4f ), CIRCLE_STEPS, i);
					}
				}
				glPopMatrix();

				glPushMatrix();
				{
					glRotatef( 90.f, 1.f, 0.f, 0.f );
					if (mHighlightedPart == LL_ROT_Y)
					{
						mManipulatorScales = lerp(mManipulatorScales, LLVector4(1.f, SELECTED_MANIPULATOR_SCALE, 1.f, 1.f), LLCriticalDamp::getInterpolant(MANIPULATOR_SCALE_HALF_LIFE));
						glScalef(mManipulatorScales.mV[VY], mManipulatorScales.mV[VY], mManipulatorScales.mV[VY]);
						// hovering over part
						gl_ring( mRadiusMeters, width_meters, LLColor4( 0.f, 1.f, 0.f, 1.f ), LLColor4( 0.f, 1.f, 0.f, 0.5f ), CIRCLE_STEPS, i);
					}
					else
					{
						// default
						gl_ring( mRadiusMeters, width_meters, LLColor4( 0.f, 0.8f, 0.f, 0.8f ), LLColor4( 0.f, 0.8f, 0.f, 0.4f ), CIRCLE_STEPS, i);
					}						
				}
				glPopMatrix();

				glPushMatrix();
				{
					glRotatef( 90.f, 0.f, 1.f, 0.f );
					if (mHighlightedPart == LL_ROT_X)
					{
						mManipulatorScales = lerp(mManipulatorScales, LLVector4(SELECTED_MANIPULATOR_SCALE, 1.f, 1.f, 1.f), LLCriticalDamp::getInterpolant(MANIPULATOR_SCALE_HALF_LIFE));
						glScalef(mManipulatorScales.mV[VX], mManipulatorScales.mV[VX], mManipulatorScales.mV[VX]);
	
						// hovering over part
						gl_ring( mRadiusMeters, width_meters, LLColor4( 1.f, 0.f, 0.f, 1.f ), LLColor4( 1.f, 0.f, 0.f, 0.5f ), CIRCLE_STEPS, i);
					}
					else
					{
						// default
						gl_ring( mRadiusMeters, width_meters, LLColor4( 0.8f, 0.f, 0.f, 0.8f ), LLColor4( 0.8f, 0.f, 0.f, 0.4f ), CIRCLE_STEPS, i);
					}
				}
				glPopMatrix();

				if (mHighlightedPart == LL_ROT_ROLL)
				{
					mManipulatorScales = lerp(mManipulatorScales, LLVector4(1.f, 1.f, 1.f, SELECTED_MANIPULATOR_SCALE), LLCriticalDamp::getInterpolant(MANIPULATOR_SCALE_HALF_LIFE));
				}
			}
		}
	}
	glPopMatrix();
	glPopMatrix();

	LLVector3 euler_angles;
	LLQuaternion object_rot = first_object->getRotationEdit();
	object_rot.getEulerAngles(&(euler_angles.mV[VX]), &(euler_angles.mV[VY]), &(euler_angles.mV[VZ]));
	euler_angles *= RAD_TO_DEG;
	euler_angles.mV[VX] = llround(fmodf(euler_angles.mV[VX] + 360.f, 360.f), 0.05f);
	euler_angles.mV[VY] = llround(fmodf(euler_angles.mV[VY] + 360.f, 360.f), 0.05f);
	euler_angles.mV[VZ] = llround(fmodf(euler_angles.mV[VZ] + 360.f, 360.f), 0.05f);

	renderXYZ(euler_angles);
}

BOOL LLManipRotate::handleMouseDown(S32 x, S32 y, MASK mask)
{
	BOOL	handled = FALSE;

	LLViewerObject* first_object = mObjectSelection->getFirstMoveableObject(TRUE);
	if( first_object )
	{
		if( mHighlightedPart != LL_NO_PART )
		{
			handled = handleMouseDownOnPart( x, y, mask );
		}
	}

	return handled;
}

// Assumes that one of the parts of the manipulator was hit.
BOOL LLManipRotate::handleMouseDownOnPart( S32 x, S32 y, MASK mask )
{
	BOOL can_rotate = canAffectSelection();
	if (!can_rotate)
	{
		return FALSE;
	}

	highlightManipulators(x, y);
	S32 hit_part = mHighlightedPart;
	// we just started a drag, so save initial object positions
	LLSelectMgr::getInstance()->saveSelectedObjectTransform(SELECT_ACTION_TYPE_ROTATE);

	// save selection center
	mRotationCenter = gAgent.getPosGlobalFromAgent( getPivotPoint() ); //LLSelectMgr::getInstance()->getSelectionCenterGlobal();

	mManipPart = (EManipPart)hit_part;
	LLVector3 center = gAgent.getPosAgentFromGlobal( mRotationCenter );

	if( mManipPart == LL_ROT_GENERAL)
	{
		mMouseDown = intersectMouseWithSphere( x, y, center, mRadiusMeters);
	}
	else
	{
		// Project onto the plane of the ring
		LLVector3 axis = getConstraintAxis();

		F32 axis_onto_cam = llabs( axis * mCenterToCamNorm );
		const F32 AXIS_ONTO_CAM_TOL = cos( 85.f * DEG_TO_RAD );
		if( axis_onto_cam < AXIS_ONTO_CAM_TOL )
		{
			LLVector3 up_from_axis = mCenterToCamNorm % axis;
			up_from_axis.normVec();
			LLVector3 cur_intersection;
			getMousePointOnPlaneAgent(cur_intersection, x, y, center, mCenterToCam);
			cur_intersection -= center;
			mMouseDown = projected_vec(cur_intersection, up_from_axis);
			F32 mouse_depth = SNAP_GUIDE_INNER_RADIUS * mRadiusMeters;
			F32 mouse_dist_sqrd = mMouseDown.magVecSquared();
			if (mouse_dist_sqrd > 0.0001f)
			{
				mouse_depth = sqrtf((SNAP_GUIDE_INNER_RADIUS * mRadiusMeters) * (SNAP_GUIDE_INNER_RADIUS * mRadiusMeters) - 
									mouse_dist_sqrd);
			}
			LLVector3 projected_center_to_cam = mCenterToCamNorm - projected_vec(mCenterToCamNorm, axis);
			mMouseDown += mouse_depth * projected_center_to_cam;

		}
		else
		{
			mMouseDown = findNearestPointOnRing( x, y, center, axis ) - center;
			mMouseDown.normVec();
		}
	}

	mMouseCur = mMouseDown;

	// Route future Mouse messages here preemptively.  (Release on mouse up.)
	setMouseCapture( TRUE );
	LLSelectMgr::getInstance()->enableSilhouette(FALSE);
	return TRUE;
}


LLVector3 LLManipRotate::findNearestPointOnRing( S32 x, S32 y, const LLVector3& center, const LLVector3& axis )
{
	// Project the delta onto the ring and rescale it by the radius so that it's _on_ the ring.
	LLVector3 proj_onto_ring;
	getMousePointOnPlaneAgent(proj_onto_ring, x, y, center, axis);
	proj_onto_ring -= center;
	proj_onto_ring.normVec();

	return center + proj_onto_ring * mRadiusMeters;
}

BOOL LLManipRotate::handleMouseUp(S32 x, S32 y, MASK mask)
{
	// first, perform normal processing in case this was a quick-click
	handleHover(x, y, mask);

	if( hasMouseCapture() )
	{
		for (LLObjectSelection::iterator iter = mObjectSelection->begin();
		 iter != mObjectSelection->end(); iter++)
		{
			LLSelectNode* selectNode = *iter;
			LLViewerObject* object = selectNode->getObject();

			// have permission to move and object is root of selection or individually selected
			if (object->permMove() && (object->isRootEdit() || selectNode->mIndividualSelection))
			{
				object->mUnselectedChildrenPositions.clear() ;
			}
		}

		mManipPart = LL_NO_PART;

		// Might have missed last update due to timing.
		LLSelectMgr::getInstance()->sendMultipleUpdate( UPD_ROTATION | UPD_POSITION );
		LLSelectMgr::getInstance()->enableSilhouette(TRUE);
		//gAgent.setObjectTracking(gSavedSettings.getBOOL("TrackFocusObject"));

		LLSelectMgr::getInstance()->updateSelectionCenter();
		LLSelectMgr::getInstance()->saveSelectedObjectTransform(SELECT_ACTION_TYPE_PICK);
	}

	return LLManip::handleMouseUp(x, y, mask);
}


BOOL LLManipRotate::handleHover(S32 x, S32 y, MASK mask)
{
	if( hasMouseCapture() )
	{
		if( mObjectSelection->isEmpty() )
		{
			// Somehow the object got deselected while we were dragging it.
			setMouseCapture( FALSE );
		}
		else
		{
			drag(x, y);
		}

		lldebugst(LLERR_USER_INPUT) << "hover handled by LLManipRotate (active)" << llendl;		
	}
	else
	{
		highlightManipulators(x, y);
		lldebugst(LLERR_USER_INPUT) << "hover handled by LLManipRotate (inactive)" << llendl;		
	}

	gViewerWindow->setCursor(UI_CURSOR_TOOLROTATE);
	return TRUE;
}


LLVector3 LLManipRotate::projectToSphere( F32 x, F32 y, BOOL* on_sphere ) 
{
	F32 z = 0.f;
	F32 dist_squared = x*x + y*y;

	*on_sphere = dist_squared <= SQ_RADIUS;
    if( *on_sphere )
	{    
        z = sqrt(SQ_RADIUS - dist_squared);
    }
	return LLVector3( x, y, z );
}

// Freeform rotation
void LLManipRotate::drag( S32 x, S32 y )
{
	if( !updateVisiblity() )
	{
		return;
	}

	if( mManipPart == LL_ROT_GENERAL )
	{
		mRotation = dragUnconstrained(x, y);
	}
	else
	{
		mRotation = dragConstrained(x, y);
	}

	BOOL damped = mSmoothRotate;
	mSmoothRotate = FALSE;

	for (LLObjectSelection::iterator iter = mObjectSelection->begin();
		 iter != mObjectSelection->end(); iter++)
	{
		LLSelectNode* selectNode = *iter;
		LLViewerObject* object = selectNode->getObject();

		// have permission to move and object is root of selection or individually selected
		if (object->permMove() && (object->isRootEdit() || selectNode->mIndividualSelection))
		{
			if (!object->isRootEdit())
			{
				// child objects should not update if parent is selected
				LLViewerObject* editable_root = (LLViewerObject*)object->getParent();
				if (editable_root->isSelected())
				{
					// we will be moved properly by our parent, so skip
					continue;
				}
			}

			LLQuaternion new_rot = selectNode->mSavedRotation * mRotation;
			std::vector<LLVector3>& child_positions = object->mUnselectedChildrenPositions ;
			std::vector<LLQuaternion> child_rotations;
			if (object->isRootEdit() && selectNode->mIndividualSelection)
			{
				object->saveUnselectedChildrenRotation(child_rotations) ;
				object->saveUnselectedChildrenPosition(child_positions) ;			
			}

			if (object->getParent() && object->mDrawable.notNull())
			{
				LLQuaternion invParentRotation = object->mDrawable->mXform.getParent()->getWorldRotation();
				invParentRotation.transQuat();

				object->setRotation(new_rot * invParentRotation, damped);
				rebuild(object);
			}
			else
			{
				object->setRotation(new_rot, damped);
				rebuild(object);
			}

			// for individually selected roots, we need to counterrotate all the children
			if (object->isRootEdit() && selectNode->mIndividualSelection)
			{
				//RN: must do non-damped updates on these objects so relative rotation appears constant
				// instead of having two competing slerps making the child objects appear to "wobble"
				object->resetChildrenRotationAndPosition(child_rotations, child_positions) ;
			}
		}
	}

	// update positions
	for (LLObjectSelection::iterator iter = mObjectSelection->begin();
		 iter != mObjectSelection->end(); iter++)
	{
		LLSelectNode* selectNode = *iter;
		LLViewerObject* object = selectNode->getObject();

		// to avoid cumulative position changes we calculate the objects new position using its saved position
		if (object && object->permMove())
		{
			LLVector3 center   = gAgent.getPosAgentFromGlobal( mRotationCenter );

			LLVector3 old_position;
			LLVector3 new_position;

			if (object->isAttachment() && object->mDrawable.notNull())
			{ 
				// need to work in drawable space to handle selected items from multiple attachments 
				// (which have no shared frame of reference other than their render positions)
				LLXform* parent_xform = object->mDrawable->getXform()->getParent();
				new_position = (selectNode->mSavedPositionLocal * parent_xform->getWorldRotation()) + parent_xform->getWorldPosition();
				old_position = (object->getPosition() * parent_xform->getWorldRotation()) + parent_xform->getWorldPosition();//object->getRenderPosition();
			}
			else
			{
				new_position = gAgent.getPosAgentFromGlobal( selectNode->mSavedPositionGlobal );
				old_position = object->getPositionAgent();
			}

			new_position = (new_position - center) * mRotation;		// new relative rotated position
			new_position += center;
			
			if (object->isRootEdit() && !object->isAttachment())
			{
				LLVector3d new_pos_global = gAgent.getPosGlobalFromAgent(new_position);
				new_pos_global = LLWorld::getInstance()->clipToVisibleRegions(selectNode->mSavedPositionGlobal, new_pos_global);
				new_position = gAgent.getPosAgentFromGlobal(new_pos_global);
			}

			// for individually selected child objects
			if (!object->isRootEdit() && selectNode->mIndividualSelection)
			{
				LLViewerObject* parentp = (LLViewerObject*)object->getParent();
				if (!parentp->isSelected())
				{
					if (object->isAttachment() && object->mDrawable.notNull())
					{
						// find position relative to render position of parent
						object->setPosition((new_position - parentp->getRenderPosition()) * ~parentp->getRenderRotation());
						rebuild(object);
					}
					else
					{
						object->setPositionParent((new_position - parentp->getPositionAgent()) * ~parentp->getRotationRegion());
						rebuild(object);
					}
				}
			}
			else if (object->isRootEdit())
			{
				if (object->isAttachment() && object->mDrawable.notNull())
				{
					LLXform* parent_xform = object->mDrawable->getXform()->getParent();
					object->setPosition((new_position - parent_xform->getWorldPosition()) * ~parent_xform->getWorldRotation());
					rebuild(object);
				}
				else
				{
					object->setPositionAgent(new_position);
					rebuild(object);
				}
			}

			// for individually selected roots, we need to counter-translate all unselected children
			if (object->isRootEdit() && selectNode->mIndividualSelection)
			{
				// only offset by parent's translation as we've already countered parent's rotation
				rebuild(object);
				object->resetChildrenPosition(old_position - new_position) ;				
			}
		}
	}

	// store changes to override updates
	for (LLObjectSelection::iterator iter = LLSelectMgr::getInstance()->getSelection()->begin();
		 iter != LLSelectMgr::getInstance()->getSelection()->end(); iter++)
	{
		LLSelectNode* selectNode = *iter;
		LLViewerObject*cur = selectNode->getObject();
		if( cur->permModify() && cur->permMove() && !cur->isAvatar())
		{
			selectNode->mLastRotation = cur->getRotation();
			selectNode->mLastPositionLocal = cur->getPosition();
		}
	}	

	LLSelectMgr::getInstance()->updateSelectionCenter();

	// RN: just clear focus so camera doesn't follow spurious object updates
	gAgent.clearFocusObject();
	dialog_refresh_all();
}

void LLManipRotate::renderActiveRing( F32 radius, F32 width, const LLColor4& front_color, const LLColor4& back_color)
{
	LLGLEnable cull_face(GL_CULL_FACE);
	{
		gl_ring(radius, width, back_color, back_color * 0.5f, CIRCLE_STEPS, FALSE);
		gl_ring(radius, width, back_color, back_color * 0.5f, CIRCLE_STEPS, TRUE);
	}
	{
		LLGLDepthTest gls_depth(GL_FALSE);
		gl_ring(radius, width, front_color, front_color * 0.5f, CIRCLE_STEPS, FALSE);
		gl_ring(radius, width, front_color, front_color * 0.5f, CIRCLE_STEPS, TRUE);
	}
}

void LLManipRotate::renderSnapGuides()
{
	LLVector3 grid_origin;
	LLVector3 grid_scale;
	LLQuaternion grid_rotation;
	LLVector3 constraint_axis = getConstraintAxis();

	LLSelectMgr::getInstance()->getGrid(grid_origin, grid_rotation, grid_scale);

	if (!gSavedSettings.getBOOL("SnapEnabled"))
	{
		return;
	}

	LLVector3 center = gAgent.getPosAgentFromGlobal( mRotationCenter );
	LLVector3 cam_at_axis;
	if (mObjectSelection->getSelectType() == SELECT_TYPE_HUD)
	{
		cam_at_axis.setVec(1.f, 0.f, 0.f);
	}
	else
	{
		cam_at_axis = center - gAgent.getCameraPositionAgent();
		cam_at_axis.normVec();
	}

	LLVector3 world_snap_axis;
	LLVector3 test_axis = constraint_axis;

	BOOL constrain_to_ref_object = FALSE;
	if (mObjectSelection->getSelectType() == SELECT_TYPE_ATTACHMENT && gAgent.getAvatarObject())
	{
		test_axis = test_axis * ~grid_rotation;
	}
	else if (LLSelectMgr::getInstance()->getGridMode() == GRID_MODE_REF_OBJECT)
	{
		test_axis = test_axis * ~grid_rotation;
		constrain_to_ref_object = TRUE;
	}

	test_axis.abs();

	// find closest global/reference axis to local constraint axis;
	if (test_axis.mV[VX] > test_axis.mV[VY] && test_axis.mV[VX] > test_axis.mV[VZ])
	{
		world_snap_axis = LLVector3::y_axis;
	}
	else if (test_axis.mV[VY] > test_axis.mV[VZ])
	{
		world_snap_axis = LLVector3::z_axis;
	}
	else
	{
		world_snap_axis = LLVector3::x_axis;
	}

	LLVector3 projected_snap_axis = world_snap_axis;
	if (mObjectSelection->getSelectType() == SELECT_TYPE_ATTACHMENT && gAgent.getAvatarObject())
	{
		projected_snap_axis = projected_snap_axis * grid_rotation;
	}
	else if (constrain_to_ref_object)
	{
		projected_snap_axis = projected_snap_axis * grid_rotation;
	}

	// project world snap axis onto constraint plane
	projected_snap_axis -= projected_vec(projected_snap_axis, constraint_axis);
	projected_snap_axis.normVec();

	S32 num_rings = mCamEdgeOn ? 2 : 1;
	for (S32 ring_num = 0; ring_num < num_rings; ring_num++)
	{
		LLVector3 center = gAgent.getPosAgentFromGlobal( mRotationCenter );

		if (mCamEdgeOn)
		{
			// draw two opposing rings
			if (ring_num == 0)
			{
				center += constraint_axis * mRadiusMeters * 0.5f;
			}
			else
			{
				center -= constraint_axis * mRadiusMeters * 0.5f;
			}
		}

		LLGLDepthTest gls_depth(GL_FALSE);
		for (S32 pass = 0; pass < 3; pass++)
		{
			// render snap guide ring
			glPushMatrix();
			
			LLQuaternion snap_guide_rot;
			F32 angle_radians, x, y, z;
			snap_guide_rot.shortestArc(LLVector3::z_axis, getConstraintAxis());
			snap_guide_rot.getAngleAxis(&angle_radians, &x, &y, &z);
			glTranslatef(center.mV[VX], center.mV[VY], center.mV[VZ]);
			glRotatef(angle_radians * RAD_TO_DEG, x, y, z);

			LLColor4 line_color = setupSnapGuideRenderPass(pass);

			gGL.color4fv(line_color.mV);

			if (mCamEdgeOn)
			{
				// render an arc
				LLVector3 edge_normal = cam_at_axis % constraint_axis;
				edge_normal.normVec();
				LLVector3 x_axis_snap = LLVector3::x_axis * snap_guide_rot;
				LLVector3 y_axis_snap = LLVector3::y_axis * snap_guide_rot;

				F32 end_angle = atan2(y_axis_snap * edge_normal, x_axis_snap * edge_normal);
				//F32 start_angle = angle_between((-1.f * LLVector3::x_axis) * snap_guide_rot, edge_normal);
				F32 start_angle = end_angle - F_PI;
				gl_arc_2d(0.f, 0.f, mRadiusMeters * SNAP_GUIDE_INNER_RADIUS, CIRCLE_STEPS, FALSE, start_angle, end_angle);
			}
			else
			{
				gl_circle_2d(0.f, 0.f, mRadiusMeters * SNAP_GUIDE_INNER_RADIUS, CIRCLE_STEPS, FALSE);
			}
			glPopMatrix();

			for (S32 i = 0; i < 64; i++)
			{
				BOOL render_text = TRUE;
				F32 deg = 5.625f * (F32)i;
				LLVector3 inner_point;
				LLVector3 outer_point;
				LLVector3 text_point;
				LLQuaternion rot(deg * DEG_TO_RAD, constraint_axis);
				gGL.begin(LLRender::LINES);
				{
					inner_point = (projected_snap_axis * mRadiusMeters * SNAP_GUIDE_INNER_RADIUS * rot) + center;
					F32 tick_length = 0.f;
					if (i % 16 == 0)
					{
						tick_length = mRadiusMeters * (SNAP_GUIDE_RADIUS_1 - SNAP_GUIDE_INNER_RADIUS);
					}
					else if (i % 8 == 0)
					{
						tick_length = mRadiusMeters * (SNAP_GUIDE_RADIUS_2 - SNAP_GUIDE_INNER_RADIUS);
					}
					else if (i % 4 == 0)
					{
						tick_length = mRadiusMeters * (SNAP_GUIDE_RADIUS_3 - SNAP_GUIDE_INNER_RADIUS);
					}
					else if (i % 2 == 0)
					{
						tick_length = mRadiusMeters * (SNAP_GUIDE_RADIUS_4 - SNAP_GUIDE_INNER_RADIUS);
					}
					else
					{
						tick_length = mRadiusMeters * (SNAP_GUIDE_RADIUS_5 - SNAP_GUIDE_INNER_RADIUS);
					}
					
					if (mCamEdgeOn)
					{
						// don't draw ticks that are on back side of circle
						F32 dot = cam_at_axis * (projected_snap_axis * rot);
						if (dot > 0.f)
						{
							outer_point = inner_point;
							render_text = FALSE;
						}
						else
						{
							if (ring_num == 0)
							{
								outer_point = inner_point + (constraint_axis * tick_length) * rot;
							}
							else
							{
								outer_point = inner_point - (constraint_axis * tick_length) * rot;
							}
						}
					}
					else
					{
						outer_point = inner_point + (projected_snap_axis * tick_length) * rot;
					}

					text_point = outer_point + (projected_snap_axis * mRadiusMeters * 0.1f) * rot;

					gGL.vertex3fv(inner_point.mV);
					gGL.vertex3fv(outer_point.mV);
				}
				gGL.end();

				// *TODO: Translate
				//RN: text rendering does own shadow pass, so only render once
				if (pass == 1 && render_text && i % 16 == 0)
				{
					if (world_snap_axis.mV[VX])
					{
						if (i == 0)
						{
							renderTickText(text_point, mObjectSelection->isAttachment() ? std::string("Forward") : std::string("East"), LLColor4::white);
						}
						else if (i == 16)
						{
							if (constraint_axis.mV[VZ] > 0.f)
							{
								renderTickText(text_point, mObjectSelection->isAttachment() ? std::string("Left") : std::string("North"), LLColor4::white);
							}
							else
							{
								renderTickText(text_point, mObjectSelection->isAttachment() ? std::string("Right") : std::string("South"), LLColor4::white);
							}
						}
						else if (i == 32)
						{
							renderTickText(text_point, mObjectSelection->isAttachment() ? std::string("Back") : std::string("West"), LLColor4::white);
						}
						else
						{
							if (constraint_axis.mV[VZ] > 0.f)
							{
								renderTickText(text_point, mObjectSelection->isAttachment() ? std::string("Right") : std::string("South"), LLColor4::white);
							}
							else
							{
								renderTickText(text_point, mObjectSelection->isAttachment() ? std::string("Left") : std::string("North"), LLColor4::white);
							}
						}
					}
					else if (world_snap_axis.mV[VY])
					{
						if (i == 0)
						{
							renderTickText(text_point, mObjectSelection->isAttachment() ? std::string("Left") : std::string("North"), LLColor4::white);
						}
						else if (i == 16)
						{
							if (constraint_axis.mV[VX] > 0.f)
							{
								renderTickText(text_point, std::string("Up"), LLColor4::white);
							}
							else
							{
								renderTickText(text_point, std::string("Down"), LLColor4::white);
							}
						}
						else if (i == 32)
						{
							renderTickText(text_point, mObjectSelection->isAttachment() ? std::string("Right") : std::string("South"), LLColor4::white);
						}
						else
						{
							if (constraint_axis.mV[VX] > 0.f)
							{
								renderTickText(text_point, std::string("Down"), LLColor4::white);
							}
							else
							{
								renderTickText(text_point, std::string("Up"), LLColor4::white);
							}
						}
					}
					else if (world_snap_axis.mV[VZ])
					{
						if (i == 0)
						{
							renderTickText(text_point, std::string("Up"), LLColor4::white);
						}
						else if (i == 16)
						{
							if (constraint_axis.mV[VY] > 0.f)
							{
								renderTickText(text_point, mObjectSelection->isAttachment() ? std::string("Forward") : std::string("East"), LLColor4::white);
							}
							else
							{
								renderTickText(text_point, mObjectSelection->isAttachment() ? std::string("Back") : std::string("West"), LLColor4::white);
							}
						}
						else if (i == 32)
						{
							renderTickText(text_point, std::string("Down"), LLColor4::white);
						}
						else
						{
							if (constraint_axis.mV[VY] > 0.f)
							{
								renderTickText(text_point, mObjectSelection->isAttachment() ? std::string("Back") : std::string("West"), LLColor4::white);
							}
							else
							{
								renderTickText(text_point, mObjectSelection->isAttachment() ? std::string("Forward") : std::string("East"), LLColor4::white);
							}
						}
					}
				}
				gGL.color4fv(line_color.mV);
			}

			// now render projected object axis
			if (mInSnapRegime)
			{
				LLVector3 object_axis;
				getObjectAxisClosestToMouse(object_axis);

				// project onto constraint plane
				LLSelectNode* first_node = mObjectSelection->getFirstMoveableNode(TRUE);
				object_axis = object_axis * first_node->getObject()->getRenderRotation();
				object_axis = object_axis - (object_axis * getConstraintAxis()) * getConstraintAxis();
				object_axis.normVec();
				object_axis = object_axis * SNAP_GUIDE_INNER_RADIUS * mRadiusMeters + center;
				LLVector3 line_start = center;

				gGL.begin(LLRender::LINES);
				{
					gGL.vertex3fv(line_start.mV);
					gGL.vertex3fv(object_axis.mV);
				}
				gGL.end();

				// draw snap guide arrow
				gGL.begin(LLRender::TRIANGLES);
				{
					LLVector3 arrow_dir;
					LLVector3 arrow_span = (object_axis - line_start) % getConstraintAxis();
					arrow_span.normVec();

					arrow_dir = mCamEdgeOn ? getConstraintAxis() : object_axis - line_start;
					arrow_dir.normVec();
					if (ring_num == 1)
					{
						arrow_dir *= -1.f;
					}
					gGL.vertex3fv((object_axis + arrow_dir * mRadiusMeters * 0.1f).mV);
					gGL.vertex3fv((object_axis + arrow_span * mRadiusMeters * 0.1f).mV);
					gGL.vertex3fv((object_axis - arrow_span * mRadiusMeters * 0.1f).mV);
				}
				gGL.end();

				{
					LLGLDepthTest gls_depth(GL_TRUE);
					gGL.begin(LLRender::LINES);
					{
						gGL.vertex3fv(line_start.mV);
						gGL.vertex3fv(object_axis.mV);
					}
					gGL.end();

					// draw snap guide arrow
					gGL.begin(LLRender::TRIANGLES);
					{
						LLVector3 arrow_dir;
						LLVector3 arrow_span = (object_axis - line_start) % getConstraintAxis();
						arrow_span.normVec();

						arrow_dir = mCamEdgeOn ? getConstraintAxis() : object_axis - line_start;
						arrow_dir.normVec();
						if (ring_num == 1)
						{
							arrow_dir *= -1.f;
						}

						gGL.vertex3fv((object_axis + arrow_dir * mRadiusMeters * 0.1f).mV);
						gGL.vertex3fv((object_axis + arrow_span * mRadiusMeters * 0.1f).mV);
						gGL.vertex3fv((object_axis - arrow_span * mRadiusMeters * 0.1f).mV);
					}
					gGL.end();
				}
			}
		}
	}
}

// Returns TRUE if center of sphere is visible.  Also sets a bunch of member variables that are used later (e.g. mCenterToCam)
BOOL LLManipRotate::updateVisiblity()
{
	// Don't want to recalculate the center of the selection during a drag.
	// Due to packet delays, sometimes half the objects in the selection have their
	// new position and half have their old one.  This creates subtle errors in the
	// computed center position for that frame.  Unfortunately, these errors
	// accumulate.  The result is objects seem to "fly apart" during rotations.
	// JC - 03.26.2002
	if (!hasMouseCapture())
	{
		mRotationCenter = gAgent.getPosGlobalFromAgent( getPivotPoint() );//LLSelectMgr::getInstance()->getSelectionCenterGlobal();
	}

	BOOL visible = FALSE;

	LLVector3 center = gAgent.getPosAgentFromGlobal( mRotationCenter );
	if (mObjectSelection->getSelectType() == SELECT_TYPE_HUD)
	{
		mCenterToCam = LLVector3(-1.f / gAgent.mHUDCurZoom, 0.f, 0.f);
		mCenterToCamNorm = mCenterToCam;
		mCenterToCamMag = mCenterToCamNorm.normVec();

		mRadiusMeters = RADIUS_PIXELS / (F32) LLViewerCamera::getInstance()->getViewHeightInPixels();
		mRadiusMeters /= gAgent.mHUDCurZoom;

		mCenterToProfilePlaneMag = mRadiusMeters * mRadiusMeters / mCenterToCamMag;
		mCenterToProfilePlane = -mCenterToProfilePlaneMag * mCenterToCamNorm;

		mCenterScreen.set((S32)((0.5f - mRotationCenter.mdV[VY]) / gAgent.mHUDCurZoom * gViewerWindow->getWindowWidth()),
							(S32)((mRotationCenter.mdV[VZ] + 0.5f) / gAgent.mHUDCurZoom * gViewerWindow->getWindowHeight()));
		visible = TRUE;
	}
	else
	{
		visible = LLViewerCamera::getInstance()->projectPosAgentToScreen(center, mCenterScreen );
		if( visible )
		{
			mCenterToCam = gAgent.getCameraPositionAgent() - center;
			mCenterToCamNorm = mCenterToCam;
			mCenterToCamMag = mCenterToCamNorm.normVec();
			LLVector3 cameraAtAxis = LLViewerCamera::getInstance()->getAtAxis();
			cameraAtAxis.normVec();

			F32 z_dist = -1.f * (mCenterToCam * cameraAtAxis);

			// Don't drag manip if object too far away
			if (!gSavedSettings.getBOOL("DisableMaxBuildConstraints") && gSavedSettings.getBOOL("LimitSelectDistance"))
			{
				F32 max_select_distance = gHippoLimits->getMaxSelectDistance();
				if (dist_vec(gAgent.getPositionAgent(), center) > max_select_distance)
				{
					visible = FALSE;
				}
			}
			
			if (mCenterToCamMag > 0.001f)
			{
				F32 fraction_of_fov = RADIUS_PIXELS / (F32) LLViewerCamera::getInstance()->getViewHeightInPixels();
				F32 apparent_angle = fraction_of_fov * LLViewerCamera::getInstance()->getView();  // radians
				mRadiusMeters = z_dist * tan(apparent_angle);

				mCenterToProfilePlaneMag = mRadiusMeters * mRadiusMeters / mCenterToCamMag;
				mCenterToProfilePlane = -mCenterToProfilePlaneMag * mCenterToCamNorm;
			}
			else
			{
				visible = FALSE;
			}
		}
	}

	mCamEdgeOn = FALSE;
	F32 axis_onto_cam = mManipPart >= LL_ROT_X ? llabs( getConstraintAxis() * mCenterToCamNorm ) : 0.f;
	if( axis_onto_cam < AXIS_ONTO_CAM_TOLERANCE )
	{
		mCamEdgeOn = TRUE;
	}

	return visible;
}

LLQuaternion LLManipRotate::dragUnconstrained( S32 x, S32 y )
{
	LLVector3 cam = gAgent.getCameraPositionAgent();
	LLVector3 center =  gAgent.getPosAgentFromGlobal( mRotationCenter );

	mMouseCur = intersectMouseWithSphere( x, y, center, mRadiusMeters);

	F32 delta_x = (F32)(mCenterScreen.mX - x);
	F32 delta_y = (F32)(mCenterScreen.mY - y);

	F32 dist_from_sphere_center = sqrt(delta_x * delta_x + delta_y * delta_y);

	LLVector3 axis = mMouseDown % mMouseCur;
	axis.normVec();
	F32 angle = acos(mMouseDown * mMouseCur);
	LLQuaternion sphere_rot( angle, axis );

	if (is_approx_zero(1.f - mMouseDown * mMouseCur))
	{
		return LLQuaternion::DEFAULT;
	}
	else if (dist_from_sphere_center < RADIUS_PIXELS)
	{
		return sphere_rot;
	}
	else
	{
		LLVector3 intersection;
		getMousePointOnPlaneAgent( intersection, x, y, center + mCenterToProfilePlane, mCenterToCamNorm );

		// amount dragging in sphere from center to periphery would rotate object
		F32 in_sphere_angle = F_PI_BY_TWO;
		F32 dist_to_tangent_point = mRadiusMeters;
		if( !is_approx_zero( mCenterToProfilePlaneMag ) )
		{
			dist_to_tangent_point = sqrt( mRadiusMeters * mRadiusMeters - mCenterToProfilePlaneMag * mCenterToProfilePlaneMag );
			in_sphere_angle = atan2( dist_to_tangent_point, mCenterToProfilePlaneMag );
		}

		LLVector3 profile_center_to_intersection = intersection - (center + mCenterToProfilePlane);
		F32 dist_to_intersection = profile_center_to_intersection.normVec();
		F32 angle = (-1.f + dist_to_intersection / dist_to_tangent_point) * in_sphere_angle;

		LLVector3 axis;
		if (mObjectSelection->getSelectType() == SELECT_TYPE_HUD)
		{
			axis = LLVector3(-1.f, 0.f, 0.f) % profile_center_to_intersection;
		}
		else
		{
			axis = (cam - center) % profile_center_to_intersection;
			axis.normVec();
		}
		return sphere_rot * LLQuaternion( angle, axis );
	}
}

LLVector3 LLManipRotate::getConstraintAxis()
{
	LLVector3 axis;
	if( LL_ROT_ROLL == mManipPart )
	{
		axis = mCenterToCamNorm;
	}
	else
	{
		S32 axis_dir = mManipPart - LL_ROT_X;
		if ((axis_dir >= 0) && (axis_dir < 3))
		{
			axis.mV[axis_dir] = 1.f;
		}
		else
		{
#ifndef LL_RELEASE_FOR_DOWNLOAD
			llerrs << "Got bogus hit part in LLManipRotate::getConstraintAxis():" << mManipPart << llendl;
#else
			llwarns << "Got bogus hit part in LLManipRotate::getConstraintAxis():" << mManipPart << llendl;
#endif
			axis.mV[0] = 1.f;
		}

		LLVector3 grid_origin;
		LLVector3 grid_scale;
		LLQuaternion grid_rotation;

		LLSelectMgr::getInstance()->getGrid(grid_origin, grid_rotation, grid_scale);

		LLSelectNode* first_node = mObjectSelection->getFirstMoveableNode(TRUE);
		if (first_node)
		{
			// *FIX: get agent local attachment grid working
			// Put rotation into frame of first selected root object
			axis = axis * grid_rotation;
		}
	}

	return axis;
}

LLQuaternion LLManipRotate::dragConstrained( S32 x, S32 y )
{
	LLSelectNode* first_object_node = mObjectSelection->getFirstMoveableNode(TRUE);
	LLVector3 constraint_axis = getConstraintAxis();
	LLVector3 center = gAgent.getPosAgentFromGlobal( mRotationCenter );

	F32 angle = 0.f;

	// build snap axes
	LLVector3 grid_origin;
	LLVector3 grid_scale;
	LLQuaternion grid_rotation;

	LLSelectMgr::getInstance()->getGrid(grid_origin, grid_rotation, grid_scale);

	LLVector3 axis1;
	LLVector3 axis2;

	LLVector3 test_axis = constraint_axis;
	if (mObjectSelection->getSelectType() == SELECT_TYPE_ATTACHMENT && gAgent.getAvatarObject())
	{
		test_axis = test_axis * ~grid_rotation;
	}
	else if (LLSelectMgr::getInstance()->getGridMode() == GRID_MODE_REF_OBJECT)
	{
		test_axis = test_axis * ~grid_rotation;
	}
	test_axis.abs();

	// find closest global axis to constraint axis;
	if (test_axis.mV[VX] > test_axis.mV[VY] && test_axis.mV[VX] > test_axis.mV[VZ])
	{
		axis1 = LLVector3::y_axis;
	}
	else if (test_axis.mV[VY] > test_axis.mV[VZ])
	{
		axis1 = LLVector3::z_axis;
	}
	else
	{
		axis1 = LLVector3::x_axis;
	}

	if (mObjectSelection->getSelectType() == SELECT_TYPE_ATTACHMENT && gAgent.getAvatarObject())
	{
		axis1 = axis1 * grid_rotation;
	}
	else if (LLSelectMgr::getInstance()->getGridMode() == GRID_MODE_REF_OBJECT)
	{
		axis1 = axis1 * grid_rotation;
	}

	//project axis onto constraint plane
	axis1 -= (axis1 * constraint_axis) * constraint_axis;
	axis1.normVec();

	// calculate third and final axis
	axis2 = constraint_axis % axis1;

	//F32 axis_onto_cam = llabs( constraint_axis * mCenterToCamNorm );
	if( mCamEdgeOn )
	{
		// We're looking at the ring edge-on.
		LLVector3 snap_plane_center = (center + (constraint_axis * mRadiusMeters * 0.5f));
		LLVector3 cam_to_snap_plane;
		if (mObjectSelection->getSelectType() == SELECT_TYPE_HUD)
		{
			cam_to_snap_plane.setVec(1.f, 0.f, 0.f);
		}
		else
		{
			cam_to_snap_plane = snap_plane_center - gAgent.getCameraPositionAgent();
			cam_to_snap_plane.normVec();
		}

		LLVector3 projected_mouse;
		BOOL hit = getMousePointOnPlaneAgent(projected_mouse, x, y, snap_plane_center, constraint_axis);
		projected_mouse -= snap_plane_center;

		S32 snap_plane = 0;

		F32 dot = cam_to_snap_plane * constraint_axis;
		if (llabs(dot) < 0.01f)
		{
			// looking at ring edge on, project onto view plane and check if mouse is past ring
			getMousePointOnPlaneAgent(projected_mouse, x, y, snap_plane_center, cam_to_snap_plane);
			projected_mouse -= snap_plane_center;
			dot = projected_mouse * constraint_axis;
			if (projected_mouse * constraint_axis > 0)
			{
				snap_plane = 1;
			}
			projected_mouse -= dot * constraint_axis;
		}
		else if (dot > 0.f)
		{
			// look for mouse position outside and in front of snap circle
			if (hit && projected_mouse.magVec() > SNAP_GUIDE_INNER_RADIUS * mRadiusMeters && projected_mouse * cam_to_snap_plane < 0.f)
			{
				snap_plane = 1;
			}
		}
		else
		{
			// look for mouse position inside or in back of snap circle
			if (projected_mouse.magVec() < SNAP_GUIDE_INNER_RADIUS * mRadiusMeters || projected_mouse * cam_to_snap_plane > 0.f || !hit)
			{
				snap_plane = 1;
			}
		}

		if (snap_plane == 0)
		{
			// try other plane
			snap_plane_center = (center - (constraint_axis * mRadiusMeters * 0.5f));
			if (mObjectSelection->getSelectType() == SELECT_TYPE_HUD)
			{
				cam_to_snap_plane.setVec(1.f, 0.f, 0.f);
			}
			else
			{
				cam_to_snap_plane = snap_plane_center - gAgent.getCameraPositionAgent();
				cam_to_snap_plane.normVec();
			}

			hit = getMousePointOnPlaneAgent(projected_mouse, x, y, snap_plane_center, constraint_axis);
			projected_mouse -= snap_plane_center;

			dot = cam_to_snap_plane * constraint_axis;
			if (llabs(dot) < 0.01f)
			{
				// looking at ring edge on, project onto view plane and check if mouse is past ring
				getMousePointOnPlaneAgent(projected_mouse, x, y, snap_plane_center, cam_to_snap_plane);
				projected_mouse -= snap_plane_center;
				dot = projected_mouse * constraint_axis;
				if (projected_mouse * constraint_axis < 0)
				{
					snap_plane = 2;
				}
				projected_mouse -= dot * constraint_axis;
			}
			else if (dot < 0.f)
			{
				// look for mouse position outside and in front of snap circle
				if (hit && projected_mouse.magVec() > SNAP_GUIDE_INNER_RADIUS * mRadiusMeters && projected_mouse * cam_to_snap_plane < 0.f)
				{
					snap_plane = 2;
				}
			}
			else
			{
				// look for mouse position inside or in back of snap circle
				if (projected_mouse.magVec() < SNAP_GUIDE_INNER_RADIUS * mRadiusMeters || projected_mouse * cam_to_snap_plane > 0.f || !hit)
				{
					snap_plane = 2;
				}
			}
		}

		if (snap_plane > 0)
		{
			LLVector3 cam_at_axis;
			if (mObjectSelection->getSelectType() == SELECT_TYPE_HUD)
			{
				cam_at_axis.setVec(1.f, 0.f, 0.f);
			}
			else
			{
				cam_at_axis = snap_plane_center - gAgent.getCameraPositionAgent();
				cam_at_axis.normVec();
			}

			// first, project mouse onto screen plane at point tangent to rotation radius. 
			getMousePointOnPlaneAgent(projected_mouse, x, y, snap_plane_center, cam_at_axis);
			// project that point onto rotation plane
			projected_mouse -= snap_plane_center;
			projected_mouse -= projected_vec(projected_mouse, constraint_axis);

			F32 mouse_lateral_dist = llmin(SNAP_GUIDE_INNER_RADIUS * mRadiusMeters, projected_mouse.magVec());
			F32 mouse_depth = SNAP_GUIDE_INNER_RADIUS * mRadiusMeters;
			if (llabs(mouse_lateral_dist) > 0.01f)
			{
				mouse_depth = sqrtf((SNAP_GUIDE_INNER_RADIUS * mRadiusMeters) * (SNAP_GUIDE_INNER_RADIUS * mRadiusMeters) - 
									(mouse_lateral_dist * mouse_lateral_dist));
			}
			LLVector3 projected_camera_at = cam_at_axis - projected_vec(cam_at_axis, constraint_axis);
			projected_mouse -= mouse_depth * projected_camera_at;

			if (!mInSnapRegime)
			{
				mSmoothRotate = TRUE;
			}
			mInSnapRegime = TRUE;
			// 0 to 360 deg
			F32 mouse_angle = fmodf(atan2(projected_mouse * axis1, projected_mouse * axis2) * RAD_TO_DEG + 360.f, 360.f);
			
			F32 relative_mouse_angle = fmodf(mouse_angle + (SNAP_ANGLE_DETENTE / 2), SNAP_ANGLE_INCREMENT);
			//fmodf(llround(mouse_angle * RAD_TO_DEG, 7.5f) + 360.f, 360.f);

			LLVector3 object_axis;
			getObjectAxisClosestToMouse(object_axis);
			object_axis = object_axis * first_object_node->mSavedRotation;

			// project onto constraint plane
			object_axis = object_axis - (object_axis * getConstraintAxis()) * getConstraintAxis();
			object_axis.normVec();

			if (relative_mouse_angle < SNAP_ANGLE_DETENTE)
			{
				F32 quantized_mouse_angle = mouse_angle - (relative_mouse_angle - (SNAP_ANGLE_DETENTE * 0.5f));
				angle = (quantized_mouse_angle * DEG_TO_RAD) - atan2(object_axis * axis1, object_axis * axis2);
			}
			else
			{
				angle = (mouse_angle * DEG_TO_RAD) - atan2(object_axis * axis1, object_axis * axis2);
			}
			return LLQuaternion( -angle, constraint_axis );
		}
		else
		{
			if (mInSnapRegime)
			{
				mSmoothRotate = TRUE;
			}
			mInSnapRegime = FALSE;

			LLVector3 up_from_axis = mCenterToCamNorm % constraint_axis;
			up_from_axis.normVec();
			LLVector3 cur_intersection;
			getMousePointOnPlaneAgent(cur_intersection, x, y, center, mCenterToCam);
			cur_intersection -= center;
			mMouseCur = projected_vec(cur_intersection, up_from_axis);
			F32 mouse_depth = SNAP_GUIDE_INNER_RADIUS * mRadiusMeters;
			F32 mouse_dist_sqrd = mMouseCur.magVecSquared();
			if (mouse_dist_sqrd > 0.0001f)
			{
				mouse_depth = sqrtf((SNAP_GUIDE_INNER_RADIUS * mRadiusMeters) * (SNAP_GUIDE_INNER_RADIUS * mRadiusMeters) - 
									mouse_dist_sqrd);
			}
			LLVector3 projected_center_to_cam = mCenterToCamNorm - projected_vec(mCenterToCamNorm, constraint_axis);
			mMouseCur += mouse_depth * projected_center_to_cam;

			F32 dist = (cur_intersection * up_from_axis) - (mMouseDown * up_from_axis);
			angle = dist / (SNAP_GUIDE_INNER_RADIUS * mRadiusMeters) * -F_PI_BY_TWO;
		}
	}
	else
	{
		LLVector3 projected_mouse;
		getMousePointOnPlaneAgent(projected_mouse, x, y, center, constraint_axis);
		projected_mouse -= center;
		mMouseCur = projected_mouse;
		mMouseCur.normVec();

		if (!first_object_node)
		{
			return LLQuaternion::DEFAULT;
		}

		if (gSavedSettings.getBOOL("SnapEnabled") && projected_mouse.magVec() > SNAP_GUIDE_INNER_RADIUS * mRadiusMeters)
		{
			if (!mInSnapRegime)
			{
				mSmoothRotate = TRUE;
			}
			mInSnapRegime = TRUE;
			// 0 to 360 deg
			F32 mouse_angle = fmodf(atan2(projected_mouse * axis1, projected_mouse * axis2) * RAD_TO_DEG + 360.f, 360.f);
			
			F32 relative_mouse_angle = fmodf(mouse_angle + (SNAP_ANGLE_DETENTE / 2), SNAP_ANGLE_INCREMENT);
			//fmodf(llround(mouse_angle * RAD_TO_DEG, 7.5f) + 360.f, 360.f);

			LLVector3 object_axis;
			getObjectAxisClosestToMouse(object_axis);
			object_axis = object_axis * first_object_node->mSavedRotation;

			// project onto constraint plane
			object_axis = object_axis - (object_axis * getConstraintAxis()) * getConstraintAxis();
			object_axis.normVec();

			if (relative_mouse_angle < SNAP_ANGLE_DETENTE)
			{
				F32 quantized_mouse_angle = mouse_angle - (relative_mouse_angle - (SNAP_ANGLE_DETENTE * 0.5f));
				angle = (quantized_mouse_angle * DEG_TO_RAD) - atan2(object_axis * axis1, object_axis * axis2);
			}
			else
			{
				angle = (mouse_angle * DEG_TO_RAD) - atan2(object_axis * axis1, object_axis * axis2);
			}
			return LLQuaternion( -angle, constraint_axis );
		}
		else
		{
			if (mInSnapRegime)
			{
				mSmoothRotate = TRUE;
			}
			mInSnapRegime = FALSE;
		}

		angle = acos(mMouseCur * mMouseDown);

		F32 dir = (mMouseDown % mMouseCur) * constraint_axis;  // cross product
		if( dir < 0.f )
		{
			angle *= -1.f;
		}
	}

	F32 rot_step = gSavedSettings.getF32("RotationStep");
	F32 step_size = DEG_TO_RAD * rot_step;
	angle -= fmod(angle, step_size);
		
	return LLQuaternion( angle, constraint_axis );
}



LLVector3 LLManipRotate::intersectMouseWithSphere( S32 x, S32 y, const LLVector3& sphere_center, F32 sphere_radius)
{
	LLVector3 ray_pt;
	LLVector3 ray_dir;
	mouseToRay( x, y, &ray_pt, &ray_dir);
	return intersectRayWithSphere( ray_pt, ray_dir, sphere_center, sphere_radius );
}

LLVector3 LLManipRotate::intersectRayWithSphere( const LLVector3& ray_pt, const LLVector3& ray_dir, const LLVector3& sphere_center, F32 sphere_radius)
{
	LLVector3 ray_pt_to_center = sphere_center - ray_pt;
	F32 center_distance = ray_pt_to_center.normVec();

	F32 dot = ray_dir * ray_pt_to_center;

	if (dot == 0.f)
	{
		return LLVector3::zero;
	}

	// point which ray hits plane centered on sphere origin, facing ray origin
	LLVector3 intersection_sphere_plane = ray_pt + (ray_dir * center_distance / dot); 
	// vector from sphere origin to the point, normalized to sphere radius
	LLVector3 sphere_center_to_intersection = (intersection_sphere_plane - sphere_center) / sphere_radius;

	F32 dist_squared = sphere_center_to_intersection.magVecSquared();
	LLVector3 result;

	if (dist_squared > 1.f)
	{
		result = sphere_center_to_intersection;
		result.normVec();
	}
	else
	{
		result = sphere_center_to_intersection - ray_dir * sqrt(1.f - dist_squared);
	}

	return result;
}

// Utility function.  Should probably be moved to another class.
//static
void LLManipRotate::mouseToRay( S32 x, S32 y, LLVector3* ray_pt, LLVector3* ray_dir )
{
	if (LLSelectMgr::getInstance()->getSelection()->getSelectType() == SELECT_TYPE_HUD)
	{
		F32 mouse_x = (((F32)x / gViewerWindow->getWindowWidth()) - 0.5f) / gAgent.mHUDCurZoom;
		F32 mouse_y = ((((F32)y) / gViewerWindow->getWindowHeight()) - 0.5f) / gAgent.mHUDCurZoom;

		*ray_pt = LLVector3(-1.f, -mouse_x, mouse_y);
		*ray_dir = LLVector3(1.f, 0.f, 0.f);
	}
	else
	{
		*ray_pt = gAgent.getCameraPositionAgent();
		LLViewerCamera::getInstance()->projectScreenToPosAgent(x, y, ray_dir);
		*ray_dir -= *ray_pt;
		ray_dir->normVec();
	}
}

void LLManipRotate::highlightManipulators( S32 x, S32 y )
{
	mHighlightedPart = LL_NO_PART;

	//LLBBox bbox = LLSelectMgr::getInstance()->getBBoxOfSelection();
	LLViewerObject *first_object = mObjectSelection->getFirstMoveableObject(TRUE);
	
	if (!first_object)
	{
		return;
	}
	
	LLQuaternion object_rot = first_object->getRenderRotation();
	LLVector3 rotation_center = gAgent.getPosAgentFromGlobal(mRotationCenter);
	LLVector3 mouse_dir_x;
	LLVector3 mouse_dir_y;
	LLVector3 mouse_dir_z;
	LLVector3 intersection_roll;

	LLVector3 grid_origin;
	LLVector3 grid_scale;
	LLQuaternion grid_rotation;

	LLSelectMgr::getInstance()->getGrid(grid_origin, grid_rotation, grid_scale);

	LLVector3 rot_x_axis = LLVector3::x_axis * grid_rotation;
	LLVector3 rot_y_axis = LLVector3::y_axis * grid_rotation;
	LLVector3 rot_z_axis = LLVector3::z_axis * grid_rotation;

	F32 proj_rot_x_axis = llabs(rot_x_axis * mCenterToCamNorm);
	F32 proj_rot_y_axis = llabs(rot_y_axis * mCenterToCamNorm);
	F32 proj_rot_z_axis = llabs(rot_z_axis * mCenterToCamNorm);

	F32 min_select_distance = 0.f;
	F32 cur_select_distance = 0.f;

	// test x
	getMousePointOnPlaneAgent(mouse_dir_x, x, y, rotation_center, rot_x_axis);
	mouse_dir_x -= rotation_center;
	// push intersection point out when working at obtuse angle to make ring easier to hit
	mouse_dir_x *= 1.f + (1.f - llabs(rot_x_axis * mCenterToCamNorm)) * 0.1f;

	// test y
	getMousePointOnPlaneAgent(mouse_dir_y, x, y, rotation_center, rot_y_axis);
	mouse_dir_y -= rotation_center;
	mouse_dir_y *= 1.f + (1.f - llabs(rot_y_axis * mCenterToCamNorm)) * 0.1f;

	// test z
	getMousePointOnPlaneAgent(mouse_dir_z, x, y, rotation_center, rot_z_axis);
	mouse_dir_z -= rotation_center;
	mouse_dir_z *= 1.f + (1.f - llabs(rot_z_axis * mCenterToCamNorm)) * 0.1f;

	// test roll
	getMousePointOnPlaneAgent(intersection_roll, x, y, rotation_center, mCenterToCamNorm);
	intersection_roll -= rotation_center;

	F32 dist_x = mouse_dir_x.normVec();
	F32 dist_y = mouse_dir_y.normVec();
	F32 dist_z = mouse_dir_z.normVec();

	F32 distance_threshold = (MAX_MANIP_SELECT_DISTANCE * mRadiusMeters) / gViewerWindow->getWindowHeight();

	if (llabs(dist_x - mRadiusMeters) * llmax(0.05f, proj_rot_x_axis) < distance_threshold)
	{
		// selected x
		cur_select_distance = dist_x * mouse_dir_x * mCenterToCamNorm;
		if (cur_select_distance >= -0.05f && (min_select_distance == 0.f || cur_select_distance > min_select_distance))
		{
			min_select_distance = cur_select_distance;
			mHighlightedPart = LL_ROT_X;
		}
	}
	if (llabs(dist_y - mRadiusMeters) * llmax(0.05f, proj_rot_y_axis) < distance_threshold)
	{
		// selected y
		cur_select_distance = dist_y * mouse_dir_y * mCenterToCamNorm;
		if (cur_select_distance >= -0.05f && (min_select_distance == 0.f || cur_select_distance > min_select_distance))
		{
			min_select_distance = cur_select_distance;
			mHighlightedPart = LL_ROT_Y;
		}
	}
	if (llabs(dist_z - mRadiusMeters) * llmax(0.05f, proj_rot_z_axis) < distance_threshold)
	{
		// selected z
		cur_select_distance = dist_z * mouse_dir_z * mCenterToCamNorm;
		if (cur_select_distance >= -0.05f && (min_select_distance == 0.f || cur_select_distance > min_select_distance))
		{
			min_select_distance = cur_select_distance;
			mHighlightedPart = LL_ROT_Z;
		}
	}

	// test for edge-on intersections
	if (proj_rot_x_axis < 0.05f)
	{
		if ((proj_rot_y_axis > 0.05f && (dist_y * llabs(mouse_dir_y * rot_x_axis) < distance_threshold) && dist_y < mRadiusMeters) ||
			(proj_rot_z_axis > 0.05f && (dist_z * llabs(mouse_dir_z * rot_x_axis) < distance_threshold) && dist_z < mRadiusMeters))
		{
			mHighlightedPart = LL_ROT_X;
		}
	}

	if (proj_rot_y_axis < 0.05f)
	{
		if ((proj_rot_x_axis > 0.05f && (dist_x * llabs(mouse_dir_x * rot_y_axis) < distance_threshold) && dist_x < mRadiusMeters) ||
			(proj_rot_z_axis > 0.05f && (dist_z * llabs(mouse_dir_z * rot_y_axis) < distance_threshold) && dist_z < mRadiusMeters))
		{
			mHighlightedPart = LL_ROT_Y;
		}
	}

	if (proj_rot_z_axis < 0.05f)
	{
		if ((proj_rot_x_axis > 0.05f && (dist_x * llabs(mouse_dir_x * rot_z_axis) < distance_threshold) && dist_x < mRadiusMeters) ||
			(proj_rot_y_axis > 0.05f && (dist_y * llabs(mouse_dir_y * rot_z_axis) < distance_threshold) && dist_y < mRadiusMeters))
		{
			mHighlightedPart = LL_ROT_Z;
		}
	}

	// test for roll
	if (mHighlightedPart == LL_NO_PART)
	{
		F32 roll_distance = intersection_roll.magVec();
		F32 width_meters = WIDTH_PIXELS * mRadiusMeters / RADIUS_PIXELS;

		// use larger distance threshold for roll as it is checked only if something else wasn't highlighted
		if (llabs(roll_distance - (mRadiusMeters + (width_meters * 2.f))) < distance_threshold * 2.f)
		{
			mHighlightedPart = LL_ROT_ROLL;
		}
		else if (roll_distance < mRadiusMeters)
		{
			mHighlightedPart = LL_ROT_GENERAL;
		}
	}
}

S32 LLManipRotate::getObjectAxisClosestToMouse(LLVector3& object_axis)
{
	LLSelectNode* first_object_node = mObjectSelection->getFirstMoveableNode(TRUE);

	if (!first_object_node)
	{
		object_axis.clearVec();
		return -1;
	}

	LLQuaternion obj_rotation = first_object_node->mSavedRotation;
	LLVector3 mouse_down_object = mMouseDown * ~obj_rotation;
	LLVector3 mouse_down_abs = mouse_down_object;
	mouse_down_abs.abs();

	S32 axis_index = 0;
	if (mouse_down_abs.mV[VX] > mouse_down_abs.mV[VY] && mouse_down_abs.mV[VX] > mouse_down_abs.mV[VZ])
	{
		if (mouse_down_object.mV[VX] > 0.f)
		{
			object_axis = LLVector3::x_axis;
		}
		else
		{
			object_axis = LLVector3::x_axis_neg;
		}
		axis_index = VX;
	}
	else if (mouse_down_abs.mV[VY] > mouse_down_abs.mV[VZ])
	{
		if (mouse_down_object.mV[VY] > 0.f)
		{
			object_axis = LLVector3::y_axis;
		}
		else
		{
			object_axis = LLVector3::y_axis_neg;
		}
		axis_index = VY;
	}
	else
	{
		if (mouse_down_object.mV[VZ] > 0.f)
		{
			object_axis = LLVector3::z_axis;
		}
		else
		{
			object_axis = LLVector3::z_axis_neg;
		}
		axis_index = VZ;
	}

	return axis_index;
}

//virtual
BOOL LLManipRotate::canAffectSelection()
{
	BOOL can_rotate = mObjectSelection->getObjectCount() != 0;
	if (can_rotate)
	{
		struct f : public LLSelectedObjectFunctor
		{
			virtual bool apply(LLViewerObject* objectp)
			{
				return objectp->permMove() && (objectp->permModify() || !gSavedSettings.getBOOL("EditLinkedParts"));
			}
		} func;
		can_rotate = mObjectSelection->applyToObjects(&func);
	}
	return can_rotate;
}