summaryrefslogtreecommitdiff
path: root/indra/newview/llfollowcam.cpp
blob: d6dc964802dc6ae2391a89d70a60d96262c0fd5c (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
/** 
 * @file llfollowcam.cpp
 * @author Jeffrey Ventrella
 * @brief LLFollowCam class implementation
 *
 * $LicenseInfo:firstyear=2005&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 "llviewerprecompiledheaders.h"
#include "llfollowcam.h"
#include "llagent.h"

//-------------------------------------------------------
// class statics
//-------------------------------------------------------
std::map<LLUUID, LLFollowCamParams*> LLFollowCamMgr::sParamMap;
std::vector<LLFollowCamParams*> LLFollowCamMgr::sParamStack;

//-------------------------------------------------------
// constants
//-------------------------------------------------------
const F32 ONE_HALF							= 0.5; 
const F32 FOLLOW_CAM_ZOOM_FACTOR			= 0.1f;
const F32 FOLLOW_CAM_MIN_ZOOM_AMOUNT		= 0.1f;
const F32 DISTANCE_EPSILON					= 0.0001f;
const F32 DEFAULT_MAX_DISTANCE_FROM_SUBJECT	= 1000.0;	// this will be correctly set on me by my caller

//----------------------------------------------------------------------------------------
// This is how slowly the camera position moves to its ideal position 
//----------------------------------------------------------------------------------------
const F32 FOLLOW_CAM_MIN_POSITION_LAG		= 0.0f;		
const F32 FOLLOW_CAM_DEFAULT_POSITION_LAG	= 0.1f;  
const F32 FOLLOW_CAM_MAX_POSITION_LAG		= 3.0f;

//----------------------------------------------------------------------------------------
// This is how slowly the camera focus moves to its subject
//----------------------------------------------------------------------------------------
const F32 FOLLOW_CAM_MIN_FOCUS_LAG		= 0.0f;
const F32 FOLLOW_CAM_DEFAULT_FOCUS_LAG	= 0.1f;
const F32 FOLLOW_CAM_MAX_FOCUS_LAG		= 3.0f;

//----------------------------------------------------------------------------------------
// This is far the position can get from its IDEAL POSITION before it starts getting pulled
//----------------------------------------------------------------------------------------
const F32 FOLLOW_CAM_MIN_POSITION_THRESHOLD		= 0.0f;
const F32 FOLLOW_CAM_DEFAULT_POSITION_THRESHOLD	= 1.0f;
const F32 FOLLOW_CAM_MAX_POSITION_THRESHOLD		= 4.0f;

//----------------------------------------------------------------------------------------
// This is far the focus can get from the subject before it starts getting pulled
//----------------------------------------------------------------------------------------
const F32 FOLLOW_CAM_MIN_FOCUS_THRESHOLD		= 0.0f;
const F32 FOLLOW_CAM_DEFAULT_FOCUS_THRESHOLD	= 1.0f;
const F32 FOLLOW_CAM_MAX_FOCUS_THRESHOLD		= 4.0f;

//----------------------------------------------------------------------------------------
// This is the distance the camera wants to be from the subject
//----------------------------------------------------------------------------------------
const F32 FOLLOW_CAM_MIN_DISTANCE		= 0.5f;
const F32 FOLLOW_CAM_DEFAULT_DISTANCE	= 3.0f;
//const F32 FOLLOW_CAM_MAX_DISTANCE		= 10.0f; // from now on I am using mMaxCameraDistantFromSubject

//----------------------------------------------------------------------------------------
// this is an angluar value
// It affects the angle that the camera rises (pitches) in relation 
// to the horizontal plane
//----------------------------------------------------------------------------------------
const F32 FOLLOW_CAM_MIN_PITCH		= -45.0f; 
const F32 FOLLOW_CAM_DEFAULT_PITCH	=   0.0f;
const F32 FOLLOW_CAM_MAX_PITCH		=  80.0f;	// keep under 90 degrees - avoid gimble lock!


//----------------------------------------------------------------------------------------
// how high or low the camera considers its ideal focus to be (relative to its subject)
//----------------------------------------------------------------------------------------
const F32 FOLLOW_CAM_MIN_FOCUS_OFFSET		= -10.0f;
const LLVector3 FOLLOW_CAM_DEFAULT_FOCUS_OFFSET	=  LLVector3(1.0f, 0.f, 0.f);
const F32 FOLLOW_CAM_MAX_FOCUS_OFFSET		=  10.0f;

//----------------------------------------------------------------------------------------
// This affects the rate at which the camera adjusts to stay behind the subject
//----------------------------------------------------------------------------------------
const F32 FOLLOW_CAM_MIN_BEHINDNESS_LAG		= 0.0f;
const F32 FOLLOW_CAM_DEFAULT_BEHINDNESS_LAG		= 0.f;
const F32 FOLLOW_CAM_MAX_BEHINDNESS_LAG		= 3.0f;

//---------------------------------------------------------------------------------------------------------------------
// in  degrees: This is the size of the pie slice behind the subject matter within which the camera is free to move
//---------------------------------------------------------------------------------------------------------------------
const F32 FOLLOW_CAM_MIN_BEHINDNESS_ANGLE		= 0.0f;
const F32 FOLLOW_CAM_DEFAULT_BEHINDNESS_ANGLE	= 10.0f;
const F32 FOLLOW_CAM_MAX_BEHINDNESS_ANGLE		= 180.0f;
const F32 FOLLOW_CAM_BEHINDNESS_EPSILON			= 1.0f;

//------------------------------------
// Constructor
//------------------------------------
LLFollowCamParams::LLFollowCamParams()
{
	mMaxCameraDistantFromSubject		= DEFAULT_MAX_DISTANCE_FROM_SUBJECT;
	mPositionLocked						= false;
	mFocusLocked						= false;
	mUsePosition						= false;
	mUseFocus							= false;

	//------------------------------------------------------
	// setting the attributes to their defaults
	//------------------------------------------------------
	setPositionLag		( FOLLOW_CAM_DEFAULT_POSITION_LAG			);
	setFocusLag			( FOLLOW_CAM_DEFAULT_FOCUS_LAG				);
	setPositionThreshold( FOLLOW_CAM_DEFAULT_POSITION_THRESHOLD		);
	setFocusThreshold	( FOLLOW_CAM_DEFAULT_FOCUS_THRESHOLD		);
	setBehindnessLag	( FOLLOW_CAM_DEFAULT_BEHINDNESS_LAG		);
	setDistance			( FOLLOW_CAM_DEFAULT_DISTANCE				);
	setPitch			( FOLLOW_CAM_DEFAULT_PITCH					);
	setFocusOffset		( FOLLOW_CAM_DEFAULT_FOCUS_OFFSET	);
	setBehindnessAngle	( FOLLOW_CAM_DEFAULT_BEHINDNESS_ANGLE		);
	setPositionThreshold( FOLLOW_CAM_DEFAULT_POSITION_THRESHOLD		);
	setFocusThreshold	( FOLLOW_CAM_DEFAULT_FOCUS_THRESHOLD		);

}

LLFollowCamParams::~LLFollowCamParams() { }

//---------------------------------------------------------
// buncho set methods
//---------------------------------------------------------

//---------------------------------------------------------
void LLFollowCamParams::setPositionLag( F32 p ) 
{ 
	mPositionLag = llclamp(p, FOLLOW_CAM_MIN_POSITION_LAG, FOLLOW_CAM_MAX_POSITION_LAG); 
}


//---------------------------------------------------------
void LLFollowCamParams::setFocusLag( F32 f ) 
{ 
	mFocusLag = llclamp(f, FOLLOW_CAM_MIN_FOCUS_LAG, FOLLOW_CAM_MAX_FOCUS_LAG); 
}


//---------------------------------------------------------
void LLFollowCamParams::setPositionThreshold( F32 p ) 
{ 
	mPositionThreshold = llclamp(p, FOLLOW_CAM_MIN_POSITION_THRESHOLD, FOLLOW_CAM_MAX_POSITION_THRESHOLD); 
}


//---------------------------------------------------------
void LLFollowCamParams::setFocusThreshold( F32 f ) 
{ 
	mFocusThreshold = llclamp(f, FOLLOW_CAM_MIN_FOCUS_THRESHOLD, FOLLOW_CAM_MAX_FOCUS_THRESHOLD); 
}


//---------------------------------------------------------
void LLFollowCamParams::setPitch( F32 p ) 
{ 
	mPitch = llclamp(p, FOLLOW_CAM_MIN_PITCH, FOLLOW_CAM_MAX_PITCH); 
}


//---------------------------------------------------------
void LLFollowCamParams::setBehindnessLag( F32 b ) 
{ 
	mBehindnessLag = llclamp(b, FOLLOW_CAM_MIN_BEHINDNESS_LAG, FOLLOW_CAM_MAX_BEHINDNESS_LAG); 
}

//---------------------------------------------------------
void LLFollowCamParams::setBehindnessAngle( F32 b ) 
{ 
	mBehindnessMaxAngle = llclamp(b, FOLLOW_CAM_MIN_BEHINDNESS_ANGLE, FOLLOW_CAM_MAX_BEHINDNESS_ANGLE); 
}

//---------------------------------------------------------
void LLFollowCamParams::setDistance( F32 d ) 
{ 
	mDistance = llclamp(d, FOLLOW_CAM_MIN_DISTANCE, mMaxCameraDistantFromSubject); 
}

//---------------------------------------------------------
void LLFollowCamParams::setPositionLocked( bool l ) 
{
	mPositionLocked = l;
}

//---------------------------------------------------------
void LLFollowCamParams::setFocusLocked( bool l ) 
{
	mFocusLocked = l;

}

//---------------------------------------------------------
void LLFollowCamParams::setFocusOffset( const LLVector3& v ) 
{ 
	mFocusOffset = v; 
	mFocusOffset.clamp(FOLLOW_CAM_MIN_FOCUS_OFFSET, FOLLOW_CAM_MAX_FOCUS_OFFSET);
}

//---------------------------------------------------------
void LLFollowCamParams::setPosition( const LLVector3& p ) 
{ 
	mUsePosition = true;
	mPosition = p;
}

//---------------------------------------------------------
void LLFollowCamParams::setFocus( const LLVector3& f ) 
{ 
	mUseFocus = true;
	mFocus = f;
}

//---------------------------------------------------------
// buncho get methods
//---------------------------------------------------------
F32			LLFollowCamParams::getPositionLag		() const { return mPositionLag;			}
F32			LLFollowCamParams::getFocusLag			() const { return mFocusLag;			}
F32			LLFollowCamParams::getPositionThreshold	() const { return mPositionThreshold;	}
F32			LLFollowCamParams::getFocusThreshold	() const { return mFocusThreshold;		}
F32			LLFollowCamParams::getDistance			() const { return mDistance;			}
F32			LLFollowCamParams::getPitch				() const { return mPitch;				}
LLVector3	LLFollowCamParams::getFocusOffset		() const { return mFocusOffset;			}
F32			LLFollowCamParams::getBehindnessAngle	() const { return mBehindnessMaxAngle;	}
F32			LLFollowCamParams::getBehindnessLag		() const { return mBehindnessLag;		}
LLVector3	LLFollowCamParams::getPosition			() const { return mPosition;			}
LLVector3	LLFollowCamParams::getFocus				() const { return mFocus;				}
bool		LLFollowCamParams::getPositionLocked	() const { return mPositionLocked;		}
bool		LLFollowCamParams::getFocusLocked		() const { return mFocusLocked;			}

//------------------------------------
// Constructor
//------------------------------------
LLFollowCam::LLFollowCam() : LLFollowCamParams()
{
	mUpVector							= LLVector3::z_axis;
	mSubjectPosition					= LLVector3::zero;
	mSubjectRotation					= LLQuaternion::DEFAULT;

	mZoomedToMinimumDistance			= false;
	mPitchCos = mPitchSin = 0.f;
	mPitchSineAndCosineNeedToBeUpdated	= true; 

	mSimulatedDistance = mDistance;
}

void LLFollowCam::copyParams(LLFollowCamParams& params)
{
	setPositionLag(params.getPositionLag());
	setFocusLag(params.getFocusLag());
	setFocusThreshold( params.getFocusThreshold());
	setPositionThreshold(params.getPositionThreshold());
	setPitch(params.getPitch());
	setFocusOffset(params.getFocusOffset());
	setBehindnessAngle(params.getBehindnessAngle());
	setBehindnessLag(params.getBehindnessLag());

	setPositionLocked(params.getPositionLocked());
	setFocusLocked(params.getFocusLocked());

	setDistance(params.getDistance());	
	if (params.getUsePosition())
	{
		setPosition(params.getPosition());
	}
	if (params.getUseFocus())
	{
		setFocus(params.getFocus());
	}
}

//---------------------------------------------------------------------------------------------------------
void LLFollowCam::update()
{
	//####################################################################################
	// update Focus
	//####################################################################################
	LLVector3 offsetSubjectPosition = mSubjectPosition + (mFocusOffset * mSubjectRotation);

	LLVector3 simulated_pos_agent = gAgent.getPosAgentFromGlobal(mSimulatedPositionGlobal);
	LLVector3 vectorFromCameraToSubject = offsetSubjectPosition - simulated_pos_agent;
	F32 distanceFromCameraToSubject = vectorFromCameraToSubject.magVec();

	LLVector3 whereFocusWantsToBe = mFocus;
	LLVector3 focus_pt_agent = gAgent.getPosAgentFromGlobal(mSimulatedFocusGlobal);
	if ( mFocusLocked ) // if focus is locked, only relative focus needs to be updated
	{
		mRelativeFocus = (focus_pt_agent - mSubjectPosition) * ~mSubjectRotation;
	}
	else
	{
		LLVector3 focusOffset = offsetSubjectPosition - focus_pt_agent;
		F32 focusOffsetDistance = focusOffset.magVec();

		LLVector3 focusOffsetDirection = focusOffset / focusOffsetDistance;
		whereFocusWantsToBe = focus_pt_agent + 
			(focusOffsetDirection * (focusOffsetDistance - mFocusThreshold));
		if ( focusOffsetDistance > mFocusThreshold )
		{
			// this version normalizes focus threshold by distance 
			// so that the effect is not changed with distance
			/*
			F32 focusThresholdNormalizedByDistance = distanceFromCameraToSubject * mFocusThreshold;
			if ( focusOffsetDistance > focusThresholdNormalizedByDistance )
			{
				LLVector3 focusOffsetDirection = focusOffset / focusOffsetDistance;
				F32 force = focusOffsetDistance - focusThresholdNormalizedByDistance;
			*/

			F32 focusLagLerp = LLSmoothInterpolation::getInterpolant( mFocusLag );
			focus_pt_agent = lerp( focus_pt_agent, whereFocusWantsToBe, focusLagLerp );
			mSimulatedFocusGlobal = gAgent.getPosGlobalFromAgent(focus_pt_agent);
		}
		mRelativeFocus = lerp(mRelativeFocus, (focus_pt_agent - mSubjectPosition) * ~mSubjectRotation, LLSmoothInterpolation::getInterpolant(0.05f));
	}// if focus is not locked ---------------------------------------------


	LLVector3 whereCameraPositionWantsToBe = gAgent.getPosAgentFromGlobal(mSimulatedPositionGlobal);
	if (  mPositionLocked )
	{
		mRelativePos = (whereCameraPositionWantsToBe - mSubjectPosition) * ~mSubjectRotation;
	}
	else
	{
		//####################################################################################
		// update Position
		//####################################################################################
		//-------------------------------------------------------------------------
		// I determine the horizontal vector from the camera to the subject
		//-------------------------------------------------------------------------
		LLVector3 horizontalVectorFromCameraToSubject = vectorFromCameraToSubject;
		horizontalVectorFromCameraToSubject.mV[VZ] = 0.0f;

		//---------------------------------------------------------
		// Now I determine the horizontal distance
		//---------------------------------------------------------
		F32 horizontalDistanceFromCameraToSubject = horizontalVectorFromCameraToSubject.magVec();  

		//---------------------------------------------------------
		// Then I get the (normalized) horizontal direction...
		//---------------------------------------------------------
		LLVector3 horizontalDirectionFromCameraToSubject;	
		if ( horizontalDistanceFromCameraToSubject < DISTANCE_EPSILON )
		{
			// make sure we still have a normalized vector if distance is really small 
			// (this case is rare and fleeting)
			horizontalDirectionFromCameraToSubject = LLVector3::z_axis;
		}
		else
		{
			// I'm not using the "normalize" method, because I can just divide by horizontalDistanceFromCameraToSubject
			horizontalDirectionFromCameraToSubject = horizontalVectorFromCameraToSubject / horizontalDistanceFromCameraToSubject;
		}

		//------------------------------------------------------------------------------------------------------------
		// Here is where I determine an offset relative to subject position in oder to set the ideal position. 
		//------------------------------------------------------------------------------------------------------------
		if ( mPitchSineAndCosineNeedToBeUpdated )
		{
			calculatePitchSineAndCosine();
			mPitchSineAndCosineNeedToBeUpdated = false;
		}

		LLVector3 positionOffsetFromSubject;
		positionOffsetFromSubject.setVec
			( 
				horizontalDirectionFromCameraToSubject.mV[ VX ] * mPitchCos,
				horizontalDirectionFromCameraToSubject.mV[ VY ] * mPitchCos,
				-mPitchSin
			);

		positionOffsetFromSubject *= mSimulatedDistance;

		//----------------------------------------------------------------------
		// Finally, ideal position is set by taking the subject position and 
		// extending the positionOffsetFromSubject from that
		//----------------------------------------------------------------------
		LLVector3 idealCameraPosition = offsetSubjectPosition - positionOffsetFromSubject;

		//--------------------------------------------------------------------------------
		// Now I prepare to move the current camera position towards its ideal position...
		//--------------------------------------------------------------------------------
		LLVector3 vectorFromPositionToIdealPosition = idealCameraPosition - simulated_pos_agent;
		F32 distanceFromPositionToIdealPosition = vectorFromPositionToIdealPosition.magVec();
		
		//put this inside of the block?		
		LLVector3 normalFromPositionToIdealPosition = vectorFromPositionToIdealPosition / distanceFromPositionToIdealPosition;
		
		whereCameraPositionWantsToBe = simulated_pos_agent + 
			(normalFromPositionToIdealPosition * (distanceFromPositionToIdealPosition - mPositionThreshold));
		//-------------------------------------------------------------------------------------------------
		// The following method takes the target camera position and resets it so that it stays "behind" the subject, 
		// using behindness angle and behindness force as parameters affecting the exact behavior
		//-------------------------------------------------------------------------------------------------
		if ( distanceFromPositionToIdealPosition > mPositionThreshold )
		{
			F32 positionPullLerp = LLSmoothInterpolation::getInterpolant( mPositionLag );
			simulated_pos_agent = lerp( simulated_pos_agent, whereCameraPositionWantsToBe, positionPullLerp );
		}

		//--------------------------------------------------------------------
		// don't let the camera get farther than its official max distance
		//--------------------------------------------------------------------
		if ( distanceFromCameraToSubject > mMaxCameraDistantFromSubject )
		{
			LLVector3 directionFromCameraToSubject = vectorFromCameraToSubject / distanceFromCameraToSubject;
			simulated_pos_agent = offsetSubjectPosition - directionFromCameraToSubject * mMaxCameraDistantFromSubject;
		}

		////-------------------------------------------------------------------------------------------------
		//// The following method takes mSimulatedPositionGlobal and resets it so that it stays "behind" the subject, 
		//// using behindness angle and behindness force as parameters affecting the exact behavior
		////-------------------------------------------------------------------------------------------------
		updateBehindnessConstraint(gAgent.getPosAgentFromGlobal(mSimulatedFocusGlobal), simulated_pos_agent);
		mSimulatedPositionGlobal = gAgent.getPosGlobalFromAgent(simulated_pos_agent);

		mRelativePos = lerp(mRelativePos, (simulated_pos_agent - mSubjectPosition) * ~mSubjectRotation, LLSmoothInterpolation::getInterpolant(0.05f));
	} // if position is not locked -----------------------------------------------------------


	//####################################################################################
	// update UpVector
	//####################################################################################
	// this just points upward for now, but I anticipate future effects requiring 
	// some rolling ("banking" effects for fun, swoopy vehicles, etc.)
	mUpVector = LLVector3::z_axis;
}



//-------------------------------------------------------------------------------------
BOOL LLFollowCam::updateBehindnessConstraint(LLVector3 focus, LLVector3& cam_position)
{
	BOOL constraint_active = FALSE;
	// only apply this stuff if the behindness angle is something other than opened up all the way
	if ( mBehindnessMaxAngle < FOLLOW_CAM_MAX_BEHINDNESS_ANGLE - FOLLOW_CAM_BEHINDNESS_EPSILON )
	{
		//--------------------------------------------------------------
		// horizontalized vector from focus to camera 
		//--------------------------------------------------------------
		LLVector3 horizontalVectorFromFocusToCamera;
		horizontalVectorFromFocusToCamera.setVec(cam_position - focus);
		horizontalVectorFromFocusToCamera.mV[ VZ ] = 0.0f; 
		F32 cameraZ = cam_position.mV[ VZ ];

		//--------------------------------------------------------------
		// distance of horizontalized vector
		//--------------------------------------------------------------
		F32 horizontalDistance = horizontalVectorFromFocusToCamera.magVec();

		//--------------------------------------------------------------------------------------------------
		// calculate horizontalized back vector of the subject and scale by horizontalDistance
		//--------------------------------------------------------------------------------------------------
		LLVector3 horizontalSubjectBack( -1.0f, 0.0f, 0.0f );
		horizontalSubjectBack *= mSubjectRotation;
		horizontalSubjectBack.mV[ VZ ] = 0.0f; 
		horizontalSubjectBack.normVec(); // because horizontalizing might make it shorter than 1
		horizontalSubjectBack *= horizontalDistance;

		//--------------------------------------------------------------------------------------------------
		// find the angle (in degrees) between these vectors
		//--------------------------------------------------------------------------------------------------
		F32 cameraOffsetAngle = 0.f;
		LLVector3 cameraOffsetRotationAxis;
		LLQuaternion camera_offset_rotation;
		camera_offset_rotation.shortestArc(horizontalSubjectBack, horizontalVectorFromFocusToCamera);
		camera_offset_rotation.getAngleAxis(&cameraOffsetAngle, cameraOffsetRotationAxis);
		cameraOffsetAngle *= RAD_TO_DEG;

		if ( cameraOffsetAngle > mBehindnessMaxAngle )
		{
			F32 fraction = ((cameraOffsetAngle - mBehindnessMaxAngle) / cameraOffsetAngle) * LLSmoothInterpolation::getInterpolant(mBehindnessLag);
			cam_position = focus + horizontalSubjectBack * (slerp(fraction, camera_offset_rotation, LLQuaternion::DEFAULT));
			cam_position.mV[VZ] = cameraZ; // clamp z value back to what it was before we started messing with it
			constraint_active = TRUE;
		}
	}
	return constraint_active;
}


//---------------------------------------------------------
void LLFollowCam::calculatePitchSineAndCosine()
{
	F32 radian = mPitch * DEG_TO_RAD;
	mPitchCos = cos( radian );
	mPitchSin = sin( radian );
}

//---------------------------------------------------------
void LLFollowCam::setSubjectPositionAndRotation( const LLVector3 p, const LLQuaternion r )
{
	mSubjectPosition = p;
	mSubjectRotation = r;
}


//---------------------------------------------------------
void LLFollowCam::zoom( S32 z ) 
{ 
	F32 zoomAmount = z * mSimulatedDistance * FOLLOW_CAM_ZOOM_FACTOR;

	if (( zoomAmount <  FOLLOW_CAM_MIN_ZOOM_AMOUNT )
	&&  ( zoomAmount > -FOLLOW_CAM_MIN_ZOOM_AMOUNT ))
	{
		if ( zoomAmount < 0.0f )
		{
			zoomAmount = -FOLLOW_CAM_MIN_ZOOM_AMOUNT;
		}
		else
		{
			zoomAmount = FOLLOW_CAM_MIN_ZOOM_AMOUNT;
		}
	}

	mSimulatedDistance += zoomAmount;

	mZoomedToMinimumDistance = false;
	if ( mSimulatedDistance < FOLLOW_CAM_MIN_DISTANCE ) 
	{
		mSimulatedDistance = FOLLOW_CAM_MIN_DISTANCE;

		// if zoomAmount is negative (i.e., getting closer), then
		// this signifies having hit the minimum:
		if ( zoomAmount < 0.0f )
		{
			mZoomedToMinimumDistance = true;
		}
	}
	else if ( mSimulatedDistance > mMaxCameraDistantFromSubject ) 
	{
		mSimulatedDistance = mMaxCameraDistantFromSubject;
	}
}


//---------------------------------------------------------
bool LLFollowCam::isZoomedToMinimumDistance()
{
	return mZoomedToMinimumDistance;
}


//---------------------------------------------------------
void LLFollowCam::reset( const LLVector3 p, const LLVector3 f , const LLVector3 u )
{
	setPosition(p);
	setFocus(f);
	mUpVector	= u;
}

//---------------------------------------------------------
void LLFollowCam::setMaxCameraDistantFromSubject( F32 m )
{
	mMaxCameraDistantFromSubject = m;
}


void LLFollowCam::setPitch( F32 p ) 
{ 
	LLFollowCamParams::setPitch(p);
	mPitchSineAndCosineNeedToBeUpdated = true; // important
}

void LLFollowCam::setDistance( F32 d ) 
{ 
	if (d != mDistance)
	{
		LLFollowCamParams::setDistance(d);
		mSimulatedDistance = d;
		mZoomedToMinimumDistance = false; 
	}
}

void LLFollowCam::setPosition( const LLVector3& p ) 
{ 
	if (p != mPosition)
	{
		LLFollowCamParams::setPosition(p);
		mSimulatedPositionGlobal = gAgent.getPosGlobalFromAgent(mPosition);
		if (mPositionLocked)
		{
			mRelativePos = (mPosition - mSubjectPosition) * ~mSubjectRotation;
		}
	}
}

void LLFollowCam::setFocus( const LLVector3& f ) 
{ 
	if (f != mFocus)
	{
		LLFollowCamParams::setFocus(f);
		mSimulatedFocusGlobal = gAgent.getPosGlobalFromAgent(f);
		if (mFocusLocked)
		{
			mRelativeFocus = (mFocus - mSubjectPosition) * ~mSubjectRotation;
		}
	}
}

void LLFollowCam::setPositionLocked( bool locked )
{
	LLFollowCamParams::setPositionLocked(locked);
	if (locked)
	{
		// propagate set position to relative position
		mRelativePos = (gAgent.getPosAgentFromGlobal(mSimulatedPositionGlobal) - mSubjectPosition) * ~mSubjectRotation;
	}
}

void LLFollowCam::setFocusLocked( bool locked )
{
	LLFollowCamParams::setFocusLocked(locked);
	if (locked)	
	{
		// propagate set position to relative position
		mRelativeFocus = (gAgent.getPosAgentFromGlobal(mSimulatedFocusGlobal) - mSubjectPosition) * ~mSubjectRotation;
	}
}


LLVector3	LLFollowCam::getSimulatedPosition() const 
{ 
	// return simulated position
	return mSubjectPosition + (mRelativePos * mSubjectRotation);
}

LLVector3	LLFollowCam::getSimulatedFocus() const 
{ 
	// return simulated focus point
	return mSubjectPosition + (mRelativeFocus * mSubjectRotation);
}

LLVector3	LLFollowCam::getUpVector() 
{ 
	return mUpVector;			
}


//------------------------------------
// Destructor
//------------------------------------
LLFollowCam::~LLFollowCam()
{
}


//-------------------------------------------------------
// LLFollowCamMgr
//-------------------------------------------------------
//static
void LLFollowCamMgr::cleanupClass()
{
	for (param_map_t::iterator iter = sParamMap.begin(); iter != sParamMap.end(); ++iter)
	{
		LLFollowCamParams* params = iter->second;
		delete params;
	}
	sParamMap.clear();
}

//static
void LLFollowCamMgr::setPositionLag( const LLUUID& source, F32 lag)
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setPositionLag(lag);
	}
}

//static
void LLFollowCamMgr::setFocusLag( const LLUUID& source, F32 lag)
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setFocusLag(lag);
	}
}

//static
void LLFollowCamMgr::setFocusThreshold( const LLUUID& source, F32 threshold)
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setFocusThreshold(threshold);
	}

}

//static
void LLFollowCamMgr::setPositionThreshold( const LLUUID& source, F32 threshold)
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setPositionThreshold(threshold);
	}
}

//static
void LLFollowCamMgr::setDistance( const LLUUID& source, F32 distance)
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setDistance(distance);
	}
}

//static
void LLFollowCamMgr::setPitch( const LLUUID& source, F32 pitch)
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setPitch(pitch);
	}
}

//static
void LLFollowCamMgr::setFocusOffset( const LLUUID& source, const LLVector3& offset)
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setFocusOffset(offset);
	}
}

//static
void LLFollowCamMgr::setBehindnessAngle( const LLUUID& source, F32 angle)
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setBehindnessAngle(angle);
	}
}

//static
void LLFollowCamMgr::setBehindnessLag( const LLUUID& source, F32 force)
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setBehindnessLag(force);
	}
}

//static
void LLFollowCamMgr::setPosition( const LLUUID& source, const LLVector3 position)
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setPosition(position);
	}
}

//static
void LLFollowCamMgr::setFocus( const LLUUID& source, const LLVector3 focus)
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setFocus(focus);
	}
}

//static
void LLFollowCamMgr::setPositionLocked( const LLUUID& source, bool locked)
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setPositionLocked(locked);
	}
}

//static
void LLFollowCamMgr::setFocusLocked( const LLUUID& source, bool locked )
{
	LLFollowCamParams* paramsp = getParamsForID(source);
	if (paramsp)
	{
		paramsp->setFocusLocked(locked);
	}
}

//static 
LLFollowCamParams* LLFollowCamMgr::getParamsForID(const LLUUID& source)
{
	LLFollowCamParams* params = NULL;

	param_map_t::iterator found_it = sParamMap.find(source);
	if (found_it == sParamMap.end()) // didn't find it?
	{
		params = new LLFollowCamParams();
		sParamMap[source] = params;
	}
	else
	{
		params = found_it->second;
	}

	return params;
}

//static
LLFollowCamParams* LLFollowCamMgr::getActiveFollowCamParams()
{
	if (sParamStack.empty())
	{
		return NULL;
	}

	return sParamStack.back();
}

//static 
void LLFollowCamMgr::setCameraActive( const LLUUID& source, bool active )
{
	LLFollowCamParams* params = getParamsForID(source);
	param_stack_t::iterator found_it = std::find(sParamStack.begin(), sParamStack.end(), params);
	if (found_it != sParamStack.end())
	{
		sParamStack.erase(found_it);
	}
	// put on top of stack
	if(active)
	{
		sParamStack.push_back(params);
	}
}

//static
void LLFollowCamMgr::removeFollowCamParams(const LLUUID& source)
{
	setCameraActive(source, FALSE);
	LLFollowCamParams* params = getParamsForID(source);
	sParamMap.erase(source);
	delete params;
}

//static
bool LLFollowCamMgr::isScriptedCameraSource(const LLUUID& source)
{
	param_map_t::iterator found_it = sParamMap.find(source);
	return (found_it != sParamMap.end());
}

//static 
void LLFollowCamMgr::dump()
{
	S32 param_count = 0;
	llinfos << "Scripted camera active stack" << llendl;
	for (param_stack_t::iterator param_it = sParamStack.begin();
		param_it != sParamStack.end();
		++param_it)
	{
		llinfos << param_count++ << 
			" rot_limit: " << (*param_it)->getBehindnessAngle() << 
			" rot_lag: " << (*param_it)->getBehindnessLag() << 
			" distance: " << (*param_it)->getDistance() << 
			" focus: " << (*param_it)->getFocus() << 
			" foc_lag: " << (*param_it)->getFocusLag() << 
			" foc_lock: " << ((*param_it)->getFocusLocked() ? "Y" : "N") << 
			" foc_offset: " << (*param_it)->getFocusOffset() << 
			" foc_thresh: " << (*param_it)->getFocusThreshold() << 
			" pitch: " << (*param_it)->getPitch() << 
			" pos: " << (*param_it)->getPosition() << 
			" pos_lag: " << (*param_it)->getPositionLag() << 
			" pos_lock: " << ((*param_it)->getPositionLocked() ? "Y" : "N") << 
			" pos_thresh: " << (*param_it)->getPositionThreshold() << llendl;
	}
}