Source

ogre-ccs / CameraControlSystem / include / CCSBasicCameraModes.h

Full commit
  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
/* -------------------------------------------------------
Copyright (c) 2012 Alberto G. Salguero (alberto.salguero at uca.es)

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions of
the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------- */

#ifndef _BasicCameraModes_H_
#define _BasicCameraModes_H_

#include "CCSPrerequisites.h"

#include "CCSCameraControlSystem.h"

namespace CCS
{
	/**
	 * In this mode the camera position and orientation never change. 
	 */
    class DllExport FixedCameraMode : public CameraControlSystem::CameraMode 
	{
	public:

        FixedCameraMode(CameraControlSystem* cam, const Ogre::Vector3 &fixedAxis = Ogre::Vector3::UNIT_Y) 
            : CameraControlSystem::CameraMode(cam)
			, mFixedAxis(fixedAxis)
            , mLastPosition(Ogre::Vector3::ZERO)
            , mLastOrientation(Ogre::Quaternion::IDENTITY)
            
        { };

        virtual ~FixedCameraMode(){};

        virtual bool init()
        {
			CameraMode::init();

            mCameraCS->setFixedYawAxis(true,mFixedAxis);
            mCameraCS->setAutoTrackingTarget(false);

            instantUpdate();

            return true;
        }

        virtual void update(const Ogre::Real& timeSinceLastFrame){};
        virtual void instantUpdate()
        {
            mCameraPosition = mLastPosition;
            mCameraOrientation = mLastOrientation;
        };

        // Specific methods

        inline virtual void setCameraPosition(const Ogre::Vector3& pos)
        {
            mLastPosition = pos;
            mCameraPosition = pos;
        }

        inline virtual void setCameraOrientation(const Ogre::Quaternion& orient)
        {
            mLastOrientation = orient;
            mCameraOrientation = mLastOrientation;
        }

        inline virtual void setCameraOrientation(const Ogre::Radian roll, 
            const Ogre::Radian yaw, const Ogre::Radian pitch)
        {
            mLastOrientation = Ogre::Quaternion(roll,Ogre::Vector3::UNIT_Z) 
                            * Ogre::Quaternion(yaw,Ogre::Vector3::UNIT_Y)
                            * Ogre::Quaternion(pitch,Ogre::Vector3::UNIT_X);

            mCameraOrientation = mLastOrientation;
        }
		
    protected:
        Ogre::Vector3 mFixedAxis;
        Ogre::Vector3 mLastPosition;
        Ogre::Quaternion mLastOrientation;
	};

    //--------------------------------------------------------------------------------------------------------------------
	/**
	 * In this mode the camera position is fixed and the camera always 
	 * points to the target. 
	 */
    class DllExport FixedTrackingCameraMode : public FixedCameraMode 
	{
	public:

        FixedTrackingCameraMode(CameraControlSystem* cam, const Ogre::Vector3& fixedAxis = Ogre::Vector3::UNIT_Y) 
            : FixedCameraMode(cam,fixedAxis)
        { };

        virtual ~FixedTrackingCameraMode(){};

        virtual bool init()
        {
			CameraMode::init();

            mCameraCS->setFixedYawAxis(true,mFixedAxis);
            mCameraCS->setAutoTrackingTarget(true);

            instantUpdate();

            return true;
        }
		    
	};

    //--------------------------------------------------------------------------------------------------------------------
	/**
	 * In this mode the camera follows the target. The second parameter is the relative position
     * to the target. The orientation of the camera is fixed by a yaw axis (UNIT_Y by default). 
	 */
    class DllExport ChaseCameraMode : public CameraControlSystem::CameraModeWithTightness, public CameraControlSystem::CollidableCamera 
	{
	public:

		/**
		 * @param fixedStep true to try to avoid camera jittering (experimental)
		 */
        ChaseCameraMode(CameraControlSystem* cam, const Ogre::Vector3& relativePositionToCameraTarget
			, Ogre::Real margin = 0.1f
			, bool fixedStep = false, Ogre::Real delta = 0.001
			, const Ogre::Vector3& fixedAxis = Ogre::Vector3::UNIT_Y) 
            : CameraControlSystem::CameraModeWithTightness(cam)	
			, CameraControlSystem::CollidableCamera(cam, margin)
            , mFixedAxis(fixedAxis)
			, mRelativePositionToCameraTarget(relativePositionToCameraTarget)
            , mFixedStep(fixedStep)
			, mDelta(delta)
        {
            mTightness = 0.01;
			mRemainingTime = 0;
        };

        virtual ~ChaseCameraMode(){};

        virtual bool init()
        {
			CameraMode::init();

			mDelta = 0;
			mRemainingTime = 0;

            setFixedYawAxis(true, mFixedAxis);
            mCameraCS->setAutoTrackingTarget(true);

			this->collisionDelegate = newCollisionDelegate(this
				, &CollidableCamera::DefaultCollisionDetectionFunction);

            instantUpdate();

            return true;
        }

        virtual void update(const Ogre::Real& timeSinceLastFrame)
        {           
			if(mCameraCS->getTargetSceneNode())
			{
				Ogre::Vector3 cameraCurrentPosition = mCameraCS->getCameraPosition();
				Ogre::Vector3 cameraFinalPositionIfNoTightness = mCameraCS->getCameraTargetPosition() 
						+ mCameraCS->getCameraTargetOrientation() * mRelativePositionToCameraTarget;

				if(!mFixedStep)
				{
					Ogre::Vector3 diff = (cameraFinalPositionIfNoTightness - cameraCurrentPosition) * mTightness;
					mCameraPosition += diff;
					//! @todo mCameraPosition += diff * timeSinceLastFrame; this makes the camera mode time independent but it also make impossible to get a completely rigid link (tightness = 1)
				}
				else
				{
					mRemainingTime += timeSinceLastFrame;
					int steps = (int)(mRemainingTime / mDelta);

					for(int i=0;i<steps;i++)
					{
						Ogre::Real percentage = ((i+1)/(Ogre::Real)steps);

						Ogre::Vector3 intermediatePositionIfNoTightness = cameraCurrentPosition + ((cameraFinalPositionIfNoTightness - cameraCurrentPosition) * percentage);

						Ogre::Vector3 diff = (intermediatePositionIfNoTightness - cameraCurrentPosition) * mTightness;
						mCameraPosition += diff;
					}
				}

				if(mCollisionsEnabled)
				{
					mCameraPosition = collisionDelegate(mCameraCS->getCameraTargetPosition(), mCameraPosition);
				}
			}
        }

        virtual void instantUpdate()
        {
			if(mCameraCS->getTargetSceneNode())
			{
				mCameraPosition = mCameraCS->getCameraTargetPosition() 
					+ mCameraCS->getCameraTargetOrientation() * mRelativePositionToCameraTarget;

				if(mCollisionsEnabled)
				{
					mCameraPosition = collisionDelegate(mCameraCS->getCameraTargetPosition(), mCameraPosition);
				}
			}
        }

        // Specific methods

        inline virtual void setCameraRelativePosition(const Ogre::Vector3& posRelativeToCameraTarget)
        {
            mRelativePositionToCameraTarget = posRelativeToCameraTarget;
            instantUpdate();
        }

        inline virtual void setFixedYawAxis(bool useFixedAxis, const Ogre::Vector3& fixedAxis = Ogre::Vector3::UNIT_Y)
        {
            mFixedAxis = mFixedAxis;
            mCameraCS->setFixedYawAxis(true,mFixedAxis);
        }
		
    protected:
        Ogre::Vector3 mFixedAxis;
        Ogre::Vector3 mRelativePositionToCameraTarget;
		
		bool mFixedStep;
		Ogre::Real mDelta;
		Ogre::Real mRemainingTime;
	};

    //--------------------------------------------------------------------------------------------------------------------
	/**
	 * This mode is similar to "Chase" camera mode but the camera orientation is not fixed by
     * a yaw axis. The camera orientation will be the same as the target.
	 */
    class DllExport ChaseFreeYawAxisCameraMode : public ChaseCameraMode 
	{
	public:

        ChaseFreeYawAxisCameraMode(CameraControlSystem* cam, const Ogre::Vector3& relativePositionToCameraTarget
            , const Ogre::Quaternion& rotation, Ogre::Real collisionmargin = 0.1f) 
            : ChaseCameraMode(cam, relativePositionToCameraTarget, collisionmargin)
        {
            mRotationOffset = rotation;
        }

        ChaseFreeYawAxisCameraMode(CameraControlSystem* cam, const Ogre::Vector3& relativePositionToCameraTarget
			, const Ogre::Radian roll, const Ogre::Radian yaw, const Ogre::Radian pitch, Ogre::Real collisionmargin = 0.1f) 
			: ChaseCameraMode(cam, relativePositionToCameraTarget, collisionmargin)
        {
            mRotationOffset = Ogre::Quaternion(roll,Ogre::Vector3::UNIT_Z) 
                            * Ogre::Quaternion(yaw,Ogre::Vector3::UNIT_Y)
                            * Ogre::Quaternion(pitch,Ogre::Vector3::UNIT_X);
        }

        virtual ~ChaseFreeYawAxisCameraMode(){};

        virtual bool init()
        {
			CameraMode::init();

            ChaseCameraMode::setFixedYawAxis(false);
            mCameraCS->setAutoTrackingTarget(false);

			this->collisionDelegate = newCollisionDelegate(this
				, &CollidableCamera::DefaultCollisionDetectionFunction);

            instantUpdate();

            return true;
        }

        virtual void update(const Ogre::Real& timeSinceLastFrame)
        {            
            // Update camera position
            ChaseCameraMode::update(timeSinceLastFrame);

            // Update camera orientation
            mCameraOrientation = mCameraCS->getCameraTargetOrientation() * mRotationOffset;
        }

        virtual void instantUpdate()
        {
			if(mCameraCS->getTargetSceneNode())
			{
				// Update camera position
				ChaseCameraMode::instantUpdate();

				// Update camera orientation
				mCameraOrientation = mCameraCS->getCameraTargetOrientation() * mRotationOffset;
			}
        }

        // Specific methods

        inline virtual void setCameraRelativePosition(const Ogre::Vector3& posRelativeToCameraTarget
            , const Ogre::Quaternion& rotation)
        {
            mRelativePositionToCameraTarget = posRelativeToCameraTarget;

            mRotationOffset = rotation;

            instantUpdate();
        }

        inline virtual void setCameraRelativePosition(const Ogre::Vector3& posRelativeToCameraTarget
            , const Ogre::Radian roll, const Ogre::Radian yaw, const Ogre::Radian pitch)
        {
            mRelativePositionToCameraTarget = posRelativeToCameraTarget;

            mRotationOffset = Ogre::Quaternion(roll,Ogre::Vector3::UNIT_Z) 
                            * Ogre::Quaternion(yaw,Ogre::Vector3::UNIT_Y)
                            * Ogre::Quaternion(pitch,Ogre::Vector3::UNIT_X);

            instantUpdate();
        }
		
    protected:
        Ogre::Quaternion mRotationOffset;
	};

    //--------------------------------------------------------------------------------------------------------------------
	/**
	 * In this mode the camera node is attached to the target node as a child.
	 */
    class DllExport AttachedCameraMode : public CameraControlSystem::CameraMode
	{
	public:

        AttachedCameraMode(CameraControlSystem* cam, const Ogre::Vector3& relativePositionToCameraTarget
            , const Ogre::Quaternion& rotation)
            : CameraMode(cam)
            , mRotation(rotation)
            , mRelativePositionToCameraTarget(relativePositionToCameraTarget)
        { }

        AttachedCameraMode(CameraControlSystem* cam, const Ogre::Vector3& relativePositionToCameraTarget,
            const Ogre::Radian roll, const Ogre::Radian yaw, const Ogre::Radian pitch) 
            : CameraMode(cam)
            , mRelativePositionToCameraTarget(relativePositionToCameraTarget)
        {
            mRotation = Ogre::Quaternion(roll,Ogre::Vector3::UNIT_Z) 
                * Ogre::Quaternion(yaw,Ogre::Vector3::UNIT_Y)
                * Ogre::Quaternion(pitch,Ogre::Vector3::UNIT_X);
        }

        virtual ~AttachedCameraMode(){};

        virtual bool init()
        {
			CameraMode::init();

            mCameraCS->setFixedYawAxis(false);
            mCameraCS->setAutoTrackingTarget(false);

            mCameraPreviousParentSceneNode = mCameraCS->getCameraSceneNode()->getParent();
            mCameraPreviousParentSceneNode->removeChild(mCameraCS->getCameraSceneNode());
            mCameraCS->getTargetSceneNode()->addChild(mCameraCS->getCameraSceneNode());
            
            mCameraPosition = mRelativePositionToCameraTarget;
            
            mCameraOrientation = mRotation;

            instantUpdate();

            return true;
        }

        virtual void stop()
        {
            mCameraCS->getCameraSceneNode()->getParent()->removeChild(mCameraCS->getCameraSceneNode());
            mCameraPreviousParentSceneNode->addChild(mCameraCS->getCameraSceneNode());
        }

        virtual void update(const Ogre::Real& timeSinceLastFrame) { }
        virtual void instantUpdate() { }

    protected:
        Ogre::Quaternion mRotation;
        Ogre::Vector3 mRelativePositionToCameraTarget;
        Ogre::Node* mCameraPreviousParentSceneNode; 

	};

	//--------------------------------------------------------------------------------------------------------------------
	/**
	 * This is basically an attached camera mode with the posibility of hide 
	 * or show the focus of the scene
	 */
    class DllExport FirstPersonCameraMode : public AttachedCameraMode
	{
	public:

        FirstPersonCameraMode(CameraControlSystem* cam, const Ogre::Vector3& relativePositionToCameraTarget
            , const Ogre::Quaternion& rotation)
			: AttachedCameraMode(cam, relativePositionToCameraTarget,rotation)
            , mCharacterVisible(true)
        { }

        FirstPersonCameraMode(CameraControlSystem* cam, const Ogre::Vector3& relativePositionToCameraTarget
            , const Ogre::Radian roll, const Ogre::Radian yaw, const Ogre::Radian pitch) 
			: AttachedCameraMode(cam, relativePositionToCameraTarget,roll,yaw,pitch)
            , mCharacterVisible(true)
        { }

        virtual ~FirstPersonCameraMode(){};

		virtual bool init()
        {
			AttachedCameraMode::init();

            mCameraCS->getTargetSceneNode()->setVisible(mCharacterVisible);
            
            return true;
        }

		virtual void stop()
        {
			AttachedCameraMode::stop();

            mCameraCS->getTargetSceneNode()->setVisible(true);
        }

		// Specific methods

        void setCharacterVisible(bool value)
        { 
            mCharacterVisible = value;
            if(mCameraCS->getTargetSceneNode())
            {
                mCameraCS->getTargetSceneNode()->setVisible(mCharacterVisible);
            }
        }

        bool getCharacterVisible() const { return mCharacterVisible; }

	protected:
		bool mCharacterVisible;
	};

    //--------------------------------------------------------------------------------------------------------------------
	/**
	 * In this mode the camera is constrained to the limits of a plane. 
	 * The camera always points to the target, perpendicularly to the plane.
	 */
    class DllExport PlaneBindedCameraMode : public CameraControlSystem::CameraModeWithTightness 
	{
	public:

        PlaneBindedCameraMode(CameraControlSystem* cam, const Ogre::Plane& plane, const Ogre::Vector3& fixedAxis = Ogre::Vector3::UNIT_Y) 
            : CameraControlSystem::CameraModeWithTightness(cam)
			, mFixedAxis(fixedAxis)
			, mPlane(plane)
        {
            mTightness = 1;
        };

        virtual ~PlaneBindedCameraMode(){};

        virtual bool init()
        {
			CameraMode::init();

            mCameraCS->setFixedYawAxis(true,mFixedAxis);
            mCameraCS->setAutoTrackingTarget(true);

            instantUpdate();

            return true;
        }

        virtual void update(const Ogre::Real& timeSinceLastFrame)
        {
            Ogre::Real distance = mPlane.getDistance(mCameraCS->getCameraTargetPosition());

            const Ogre::Vector3& cameraCurrentPosition = mCameraCS->getCameraPosition();
            Ogre::Vector3 cameraFinalPositionIfNoTightness = mCameraCS->getCameraTargetPosition() -
                mPlane.normal.normalisedCopy() * distance;

            Ogre::Vector3 diff = (cameraFinalPositionIfNoTightness - cameraCurrentPosition) * mTightness;
            mCameraPosition += diff;
        };

        virtual void instantUpdate()
        {
            Ogre::Real distance = mPlane.getDistance(mCameraCS->getCameraTargetPosition());
            mCameraPosition = mCameraCS->getCameraTargetPosition() - 
                mPlane.normal.normalisedCopy() * distance;
        };
		
    protected:
        Ogre::Vector3 mFixedAxis;
        Ogre::Plane mPlane;
	};

    //--------------------------------------------------------------------------------------------------------------------
	/**
	 * In this mode the camera points to a given position (the "focus") 
	 * throuh the target. The camera orientation is fixed by a yaw axis.
	 */
    class DllExport ThroughTargetCameraMode : public CameraControlSystem::CameraMode 
	{
	public:

        ThroughTargetCameraMode(CameraControlSystem* cam, Ogre::Real margin, bool inverse = false
            , const Ogre::Vector3& focusPos = Ogre::Vector3::ZERO, const Ogre::Vector3& fixedAxis = Ogre::Vector3::UNIT_Y) 
            : CameraControlSystem::CameraMode(cam),
			  mFixedAxis(fixedAxis),
			  mFocusPos(focusPos),
			  mMargin(margin),
              mInverse(inverse)
        { };

        virtual ~ThroughTargetCameraMode(){};

        virtual bool init()
        {
			CameraMode::init();

            mCameraCS->setFixedYawAxis(true,mFixedAxis);
            mCameraCS->setAutoTrackingTarget(true);

            instantUpdate();

            return true;
        }

        virtual void update(const Ogre::Real& timeSinceLastFrame)
        {
            instantUpdate();
        };

        virtual void instantUpdate()
        {
            if(mCameraCS->hasCameraTarget())
            {
                if(!mInverse)
                {
                    Ogre::Vector3 focusToTarget = mCameraCS->getCameraTargetPosition() - mFocusPos;
                    mCameraPosition = mCameraCS->getCameraTargetPosition() + 
                        focusToTarget.normalisedCopy() * mMargin;
                }
                else
                {
                    Ogre::Vector3 focusToTarget = mCameraCS->getCameraTargetPosition() - mFocusPos;
                    mCameraPosition = mFocusPos - 
                        focusToTarget.normalisedCopy() * mMargin;
                }
            }
        };

        // Specific methods

        inline virtual void setCameraFocusPosition(const Ogre::Vector3& pos)
        { 
            mFocusPos = pos; 
            init();
        };

        void setInverse(bool value){ mInverse = value; }
        bool getInverse() const { return mInverse; }
		
    protected:
        Ogre::Vector3 mFixedAxis;
        Ogre::Vector3 mFocusPos;
        Ogre::Real mMargin;
        bool mInverse;
	};

    //--------------------------------------------------------------------------------------------------------------------
	/**
	 * In this camera mode the position of the camera is chosen to be 
	 * the closest to the target of a given list. The camera orientation 
	 * is fixed by a yaw axis.
	 */
    class DllExport ClosestToTargetCameraMode : public FixedTrackingCameraMode 
	{
	public:

        ClosestToTargetCameraMode(CameraControlSystem* cam, Ogre::Real timeInterval = 1
            , const Ogre::Vector3& fixedAxis = Ogre::Vector3::UNIT_Y) 
            : FixedTrackingCameraMode(cam, fixedAxis)
			, mTimeInterval(timeInterval)
            , mTime(timeInterval)
        { };

        virtual ~ClosestToTargetCameraMode(){};

        virtual void update(const Ogre::Real& timeSinceLastFrame)
        {
            instantUpdate();
            return;

            mTime -= timeSinceLastFrame;

            if(mTime < 0)
            {
                instantUpdate();
                mTime = mTimeInterval;
            }
        };

        virtual void instantUpdate()
        {
            if(mCameraCS->hasCameraTarget())
            {
                Ogre::Real minDistance = std::numeric_limits<float>::max();
                Ogre::Vector3 targetPosition = mCameraCS->getCameraTargetPosition();

                std::vector<Ogre::Vector3>::const_iterator it;
                std::vector<Ogre::Vector3>::const_iterator closest;
                for(it = mPositionsList.begin(); it != mPositionsList.end(); ++it)
                {
                    Ogre::Vector3 vector_i = ((Ogre::Vector3)(*it));
                    Ogre::Real distance = (vector_i - targetPosition).length();
                    if( distance < minDistance)
                    {
                        closest = it;
                        minDistance = distance;
                    }
                }

                mCameraPosition = (Ogre::Vector3)(*closest);
            }
        };

        // Specific methods

        inline virtual void addCameraPosition(const Ogre::Vector3& pos)
        { 
            mPositionsList.push_back(pos); 
            init();
        };
		
    protected:
        std::vector<Ogre::Vector3> mPositionsList;
        Ogre::Real mTimeInterval;
        Ogre::Real mTime;
        bool mInverse;
	};

    //--------------------------------------------------------------------------------------------------------------------
	/**
	 * In this mode the camera is controlled by the user. In this mode the 
	 * target is always seen from the same point of view.
	 */
    class DllExport FixedDirectionCameraMode : public CameraControlSystem::CameraModeWithTightness 
	{
	public:

        FixedDirectionCameraMode(CameraControlSystem* cam, const Ogre::Vector3& direction, const Ogre::Real distance
			, const Ogre::Vector3& fixedAxis = Ogre::Vector3::UNIT_Y) 
            : CameraControlSystem::CameraModeWithTightness(cam)
			, mFixedAxis(fixedAxis)
            , mDistance(distance)
			, mDirection(direction.normalisedCopy())            
        {
            mTightness = 1;
        };

        virtual ~FixedDirectionCameraMode(){};

        virtual bool init()
        {
			CameraMode::init();

            mCameraCS->setFixedYawAxis(true, mFixedAxis);
            mCameraCS->setAutoTrackingTarget(true);

            instantUpdate();

            return true;
        }

        virtual void update(const Ogre::Real& timeSinceLastFrame)
        {            
            const Ogre::Vector3 &cameraCurrentPosition = mCameraCS->getCameraPosition();
            const Ogre::Vector3 &cameraFinalPositionIfNoTightness = mCameraCS->getCameraTargetPosition() 
                - mDirection * mDistance;

            Ogre::Vector3 diff = (cameraFinalPositionIfNoTightness - cameraCurrentPosition) * mTightness;
            mCameraPosition += diff;
        }

        virtual void instantUpdate()
        {
            mCameraPosition = mCameraCS->getCameraTargetPosition() 
                - mDirection * mDistance;
        }

        // Specific methods

        inline virtual void setDirection(const Ogre::Vector3& direction)
        {
            mDirection = direction.normalisedCopy();
            instantUpdate();
        }

        inline virtual void setDistance(Ogre::Real distance)
        {
            mDistance = distance;
            instantUpdate();
        }
		
    protected:
        Ogre::Vector3 mFixedAxis;
        Ogre::Real mDistance;
        Ogre::Vector3 mDirection;
	};

    //--------------------------------------------------------------------------------------------------------------------
	/**
	 * In this mode the camera is constrained to the limits of a plane. 
	 * This is the typical camera you can found in any Real Time Strategy game.
	 */
    class DllExport RTSCameraMode : public CameraControlSystem::CameraMode 
	{
	public:

        RTSCameraMode(CameraControlSystem* cam
            , const Ogre::Vector3& initialPosition = Ogre::Vector3::ZERO
            , const Ogre::Vector3& upAxis = Ogre::Vector3::NEGATIVE_UNIT_Z
            , const Ogre::Vector3& leftAxis = Ogre::Vector3::NEGATIVE_UNIT_X
            , const Ogre::Radian cameraPitch = Ogre::Radian(Ogre::Degree(90))
            , Ogre::Real minZoom = 0, Ogre::Real maxZoom = 0) 
            : CameraControlSystem::CameraMode(cam),
              mRotation(cameraPitch, leftAxis),
              mCameraPitch(cameraPitch),
              mUpAxis(upAxis.normalisedCopy()),
              mLeftAxis(leftAxis.normalisedCopy()),
              mHeightAxis(0,0,0),
              mMoveFactor(1),
              mHeightDisplacement(0),
              mLateralDisplacement(0),
              mVerticalDisplacement(0),
              mZoom(0),
              mMinZoom(minZoom),
              mMaxZoom(maxZoom)
        {
            mHeightAxis = -upAxis.crossProduct(leftAxis).normalisedCopy();
            mCameraPosition = initialPosition;
        };

        virtual ~RTSCameraMode(){};

        virtual bool init()
        {
			CameraMode::init();

            mCameraCS->setFixedYawAxis(false);
            mCameraCS->setAutoTrackingTarget(false);
            
            mCameraOrientation = mRotation;

            instantUpdate();

            return true;
        }

        virtual void update(const Ogre::Real& timeSinceLastFrame)
        {
            Ogre::Vector3 displacement = ((mUpAxis * mVerticalDisplacement)
                + (mHeightAxis * mHeightDisplacement)
                + (mLeftAxis * mLateralDisplacement)) * timeSinceLastFrame * mMoveFactor;

            mCameraPosition += displacement;

            mHeightDisplacement = 0;
            mLateralDisplacement = 0;
            mVerticalDisplacement = 0; 

            mCameraOrientation = mRotation;
        }

        virtual void instantUpdate()
        {
            Ogre::Vector3 displacement = ((mUpAxis * mVerticalDisplacement)
                + (mHeightAxis * mHeightDisplacement)
                + (mLeftAxis * mLateralDisplacement));

            mCameraPosition += displacement;

            mHeightDisplacement = 0;
            mLateralDisplacement = 0;
            mVerticalDisplacement = 0; 

            mCameraOrientation = mRotation;
        }

        // Specific methods

        void setCameraPitch(Ogre::Radian cameraPitch)
        {
            mCameraPitch = cameraPitch;
            mRotation = Ogre::Quaternion(mCameraPitch, mLeftAxis);
        }

        Ogre::Radian getCameraPitch() const { return mCameraPitch; };

        /**
		 * @brief Set the moving speed factor (increase the height)
		 * 
		 * @param unitsPerSecond the units the camera will be translated per second
		 */
        inline virtual void setMoveFactor(Ogre::Real unitsPerSecond){ mMoveFactor = unitsPerSecond; }

        void setZoom(Ogre::Real zoom)
        {
            Ogre::Real desiredDisplacement = zoom - mZoom;

            //Ogre::Real desiredZoom = mZoom + desiredDisplacement;

            if(zoom <= mMaxZoom && zoom >= mMinZoom)
            {
                mHeightDisplacement = desiredDisplacement;
                mZoom = zoom;
            }
            else if(zoom > mMaxZoom)
            {
                mHeightDisplacement = mMaxZoom - mZoom;
                mZoom = mMaxZoom;
            }
            else if(zoom < mMinZoom)
            {
                mHeightDisplacement = mMinZoom - mZoom;
                mZoom = mMinZoom;
            }

            instantUpdate();
        }

        Ogre::Real getZoom() const { return mZoom; };

		/**
		 * @brief Tell the camera to zoom in (reduce the height)
		 * 
		 * @param percentage the relative speed of the movement acording to the move factor (1: 100%, 0: 0%, -1: -100%)
		 */
        inline virtual void zoomIn(Ogre::Real percentage = 1)
        { 
            Ogre::Real desiredDisplacement = mMoveFactor * percentage;
            Ogre::Real desiredZoom = mZoom + desiredDisplacement;

            setZoom(desiredZoom);

            /*if(desiredZoom <= mMaxZoom && desiredZoom >= mMinZoom)
            {
                mHeightDisplacement = desiredDisplacement;
                mZoom = desiredZoom;
            }
            else if(desiredZoom > mMaxZoom)
            {
                mHeightDisplacement = mMaxZoom - mZoom;
                mZoom = mMaxZoom;
            }
            else if(desiredZoom < mMinZoom)
            {
                mHeightDisplacement = mMinZoom - mZoom;
                mZoom = mMinZoom;
            }*/
        }

		/**
		 * @brief Tell the camera to zoom out
		 * 
		 * @param percentage the relative speed of the movement acording to the move factor (1: 100%, 0: 0%, -1: -100%)
		 */
        inline virtual void zoomOut(Ogre::Real percentage = 1){ zoomIn(-percentage); }

		/**
		 * @brief Tell the camera to go right (along the leftAxis)
		 * 
		 * @param percentage the relative speed of the movement acording to the move factor (1: 100%, 0: 0%, -1: -100%)
		 */
        inline virtual void goRight(Ogre::Real percentage = 1){ mLateralDisplacement -= mMoveFactor * percentage; }

		/**
		 * @brief Tell the camera to go left (along the leftAxis)
		 * 
		 * @param percentage the relative speed of the movement acording to the move factor (1: 100%, 0: 0%, -1: -100%)
		 */
        inline virtual void goLeft(Ogre::Real percentage = 1){ goRight(-percentage); }

		/**
		 * @brief Tell the camera to go up (along the upAxis)
		 * 
		 * @param percentage the relative speed of the movement acording to the move factor (1: 100%, 0: 0%, -1: -100%)
		 */
        inline virtual void goUp(Ogre::Real percentage = 1){ mVerticalDisplacement += mMoveFactor * percentage; }

		/**
		 * @brief Tell the camera to go down (along the upAxis)
		 * 
		 * @param percentage the relative speed of the movement acording to the move factor (1: 100%, 0: 0%, -1: -100%)
		 */
        inline virtual void goDown(Ogre::Real percentage = 1){ goUp(-percentage); }
		
    protected:

        Ogre::Quaternion mRotation;
        Ogre::Radian mCameraPitch;
        Ogre::Vector3 mUpAxis;
        Ogre::Vector3 mLeftAxis;
        Ogre::Vector3 mHeightAxis;
        Ogre::Real mMoveFactor;
        Ogre::Real mHeightDisplacement;
        Ogre::Real mLateralDisplacement;
        Ogre::Real mVerticalDisplacement;
        Ogre::Real mZoom;
        Ogre::Real mMinZoom;
        Ogre::Real mMaxZoom;
	};
}

#endif