VIS2 SS2013 CVD DVR
 All Classes Namespaces Functions Enumerations Properties
InteractiveCamera.cs
1 #region Using Statement
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using Microsoft.Xna.Framework;
7 using Microsoft.Xna.Framework.Input;
8 using Microsoft.Xna.Framework.Graphics;
9 #endregion
10 
11 namespace visLU
12 {
17  {
18 
19  #region Variables
20  //camera rotation
21  protected Quaternion cameraRotation = new Quaternion(0, 0, 0, 1);
22  #endregion
23 
24  #region Properties
25  public Quaternion CameraRotation { get { return cameraRotation; } set { cameraRotation = value; } }
29 
30  #endregion
31 
32  #region Constructor
33  public InteractiveCamera() : base()
37  {
38 
39  cameraRotation = new Quaternion(0, 0, 0, 1);
40 
41  }
42  #endregion
43 
44  #region Initialize
45  public override void Initialize()
49  {
50  //init update
51  needUpdateView = true;
52  needUpdateProjection = true;
53  needUpdateViewProjection = true;
54 
55  Update();
56  }
57  #endregion
58 
59  #region Update
60  public override void Update()
65  {
66  Vector3 mouseVector = new Vector3(cameraInput.X, cameraInput.Y, 0.0f);
67  float mouseTranslate = cameraInput.Z;
68  if (mouseVector.X != 0.0f || mouseVector.Y != 0.0f)
69  {
70  //axis = Vector3.Cross(Vector3.UnitX, mCamera.View.Right);
71  //axis.Normalize();
72 
73  Revolve(Vector3.UnitX, mouseVector.Y / GameProperties.Instance.XNADrawSurfaceHeight * 3.2f);
74 
75  //axis = Vector3.Cross(Vector3.UnitX, mCamera.View.Forward);
76  //axis.Normalize();
77 
78  RevolveGlobal(Vector3.UnitZ, -mouseVector.X / GameProperties.Instance.XNADrawSurfaceWidth * 3.2f);
79  }
80  if (mouseTranslate != 0.0f)
81  {
82  Translate(new Vector3(0, 0, mouseTranslate / GameProperties.Instance.XNADrawSurfaceWidth * 3.2f));
83  }
84 
85  base.Update();
86  }
87 
91  protected override void updateView()
92  {
93  worldMatrix = Matrix.Identity;
94 
95  viewMatrix = Matrix.Invert(
96  Matrix.CreateFromQuaternion(cameraRotation) *
97  Matrix.CreateTranslation(cameraPosition)
98  );
99 
100  needUpdateView = false;
101  needUpdateViewProjection = true;
102  }
103 
104  #endregion
105 
106  #region additional methods
107  private void Rotate(Vector3 axis, float angle)
108  {
109  axis = Vector3.Transform(axis, Matrix.CreateFromQuaternion(cameraRotation));
110  cameraRotation = Quaternion.Normalize(Quaternion.CreateFromAxisAngle(axis, angle) * cameraRotation);
111  needUpdateView = true;
112  }
113 
114  private void Translate(Vector3 distance)
115  {
116  cameraPosition += Vector3.Transform(distance, Matrix.CreateFromQuaternion(cameraRotation));
117  needUpdateView = true;
118  }
119 
120  private void Revolve(Vector3 axis, float angle)
121  {
122  Vector3 revolveAxis = Vector3.Transform(axis, Matrix.CreateFromQuaternion(cameraRotation));
123  Quaternion rotate = Quaternion.CreateFromAxisAngle(revolveAxis, angle);
124  cameraPosition = Vector3.Transform(cameraPosition - cameraTarget, Matrix.CreateFromQuaternion(rotate)) + cameraTarget;
125 
126  Rotate(axis, angle);
127  }
128 
129  private void RotateGlobal(Vector3 axis, float angle)
130  {
131  cameraRotation = Quaternion.Normalize(Quaternion.CreateFromAxisAngle(axis, angle) * cameraRotation);
132  needUpdateView = true;
133  }
134 
135  private void TranslateGlobal(Vector3 distance)
136  {
137  cameraPosition += distance;
138  needUpdateView = true;
139  }
140 
141  private void RevolveGlobal(Vector3 axis, float angle)
142  {
143  Quaternion rotate = Quaternion.CreateFromAxisAngle(axis, angle);
144  cameraPosition = Vector3.Transform(cameraPosition - cameraTarget, Matrix.CreateFromQuaternion(rotate)) + cameraTarget;
145 
146  RotateGlobal(axis, angle);
147  }
148 
149  #endregion
150 
151  }
152 }