Annotation of /trunk/DefaultRenderer/Trackball3D.cs
Parent Directory
|
Revision Log
Revision 8 - (view) (download)
| 1 : | albert | 4 | //------------------------------------------------------------------ |
| 2 : | // | ||
| 3 : | // The following article discusses the mechanics behind this | ||
| 4 : | // trackball implementation: http://viewport3d.com/trackball.htm | ||
| 5 : | // | ||
| 6 : | // Reading the article is not required to use this sample code, | ||
| 7 : | // but skimming it might be useful. | ||
| 8 : | // | ||
| 9 : | // For licensing information and to get the latest version go to: | ||
| 10 : | // http://workspaces.gotdotnet.com/3dtools | ||
| 11 : | // | ||
| 12 : | //------------------------------------------------------------------ | ||
| 13 : | |||
| 14 : | using System; | ||
| 15 : | using System.Windows; | ||
| 16 : | using System.Windows.Input; | ||
| 17 : | using System.Windows.Media.Media3D; | ||
| 18 : | |||
| 19 : | namespace Xenki.DefaultRenderer | ||
| 20 : | { | ||
| 21 : | /// <summary> | ||
| 22 : | /// Trackball is a utility class which observes the mouse events | ||
| 23 : | /// on a specified FrameworkElement and produces a Transform3D | ||
| 24 : | /// with the resultant rotation and scale. | ||
| 25 : | /// | ||
| 26 : | /// Example Usage: | ||
| 27 : | /// | ||
| 28 : | /// Trackball trackball = new Trackball(); | ||
| 29 : | /// trackball.EventSource = myElement; | ||
| 30 : | /// myViewport3D.Camera.Transform = trackball.Transform; | ||
| 31 : | /// | ||
| 32 : | /// Because Viewport3Ds only raise events when the mouse is over the | ||
| 33 : | /// rendered 3D geometry (as opposed to not when the mouse is within | ||
| 34 : | /// the layout bounds) you usually want to use another element as | ||
| 35 : | /// your EventSource. For example, a transparent border placed on | ||
| 36 : | /// top of your Viewport3D works well: | ||
| 37 : | /// | ||
| 38 : | /// <Grid> | ||
| 39 : | /// <ColumnDefinition /> | ||
| 40 : | /// <RowDefinition /> | ||
| 41 : | /// <Viewport3D Name="myViewport" ClipToBounds="True" Grid.Row="0" Grid.Column="0" /> | ||
| 42 : | /// <Border Name="myElement" Background="Transparent" Grid.Row="0" Grid.Column="0" /> | ||
| 43 : | /// </Grid> | ||
| 44 : | /// | ||
| 45 : | /// NOTE: The Transform property may be shared by multiple Cameras | ||
| 46 : | /// if you want to have auxilary views following the trackball. | ||
| 47 : | /// | ||
| 48 : | /// It can also be useful to share the Transform property with | ||
| 49 : | /// models in the scene that you want to move with the camera. | ||
| 50 : | /// (For example, the Trackport3D's headlight is implemented | ||
| 51 : | /// this way.) | ||
| 52 : | /// | ||
| 53 : | /// You may also use a Transform3DGroup to combine the | ||
| 54 : | /// Transform property with additional Transforms. | ||
| 55 : | /// </summary> | ||
| 56 : | public class Trackball | ||
| 57 : | { | ||
| 58 : | private FrameworkElement _eventSource; | ||
| 59 : | private Point _previousPosition2D; | ||
| 60 : | private Vector3D _previousPosition3D = new Vector3D(0, 0, 1); | ||
| 61 : | |||
| 62 : | private Transform3DGroup _transform; | ||
| 63 : | private ScaleTransform3D _scale = new ScaleTransform3D(); | ||
| 64 : | private AxisAngleRotation3D _rotation = new AxisAngleRotation3D(); | ||
| 65 : | private TranslateTransform3D _translate = new TranslateTransform3D(); | ||
| 66 : | |||
| 67 : | albert | 8 | private Point3D _cameraPos = new Point3D(0,0,50); |
| 68 : | private Vector3D _cameraLookDirection= new Vector3D(128,128,10); | ||
| 69 : | private Vector3D _cameraUpDirection = new Vector3D(0, 0, 1); | ||
| 70 : | private PerspectiveCamera _camera; | ||
| 71 : | public Trackball(PerspectiveCamera camera) | ||
| 72 : | albert | 4 | { |
| 73 : | albert | 8 | _camera = camera; |
| 74 : | albert | 4 | _transform = new Transform3DGroup(); |
| 75 : | _transform.Children.Add(_scale); | ||
| 76 : | _transform.Children.Add(new RotateTransform3D(_rotation)); | ||
| 77 : | _transform.Children.Add(_translate); | ||
| 78 : | } | ||
| 79 : | |||
| 80 : | /// <summary> | ||
| 81 : | /// A transform to move the camera or scene to the trackball's | ||
| 82 : | /// current orientation and scale. | ||
| 83 : | /// </summary> | ||
| 84 : | public Transform3D Transform | ||
| 85 : | { | ||
| 86 : | get { return _transform; } | ||
| 87 : | } | ||
| 88 : | albert | 8 | public Point3D CameraPos |
| 89 : | { | ||
| 90 : | get { return _cameraPos; } | ||
| 91 : | } | ||
| 92 : | public Vector3D CameraLookDirection | ||
| 93 : | { | ||
| 94 : | get { return _cameraLookDirection; } | ||
| 95 : | set { _cameraLookDirection = value; } | ||
| 96 : | albert | 4 | |
| 97 : | albert | 8 | } |
| 98 : | public Vector3D CameraUpDirection | ||
| 99 : | { | ||
| 100 : | get { return _cameraUpDirection; } | ||
| 101 : | } | ||
| 102 : | |||
| 103 : | albert | 4 | #region Event Handling |
| 104 : | |||
| 105 : | /// <summary> | ||
| 106 : | /// The FrameworkElement we listen to for mouse events. | ||
| 107 : | /// </summary> | ||
| 108 : | public FrameworkElement EventSource | ||
| 109 : | { | ||
| 110 : | get { return _eventSource; } | ||
| 111 : | |||
| 112 : | set | ||
| 113 : | { | ||
| 114 : | if (_eventSource != null) | ||
| 115 : | { | ||
| 116 : | _eventSource.MouseDown -= this.OnMouseDown; | ||
| 117 : | _eventSource.MouseUp -= this.OnMouseUp; | ||
| 118 : | _eventSource.MouseMove -= this.OnMouseMove; | ||
| 119 : | } | ||
| 120 : | |||
| 121 : | _eventSource = value; | ||
| 122 : | |||
| 123 : | _eventSource.MouseDown += this.OnMouseDown; | ||
| 124 : | _eventSource.MouseUp += this.OnMouseUp; | ||
| 125 : | _eventSource.MouseMove += this.OnMouseMove; | ||
| 126 : | } | ||
| 127 : | } | ||
| 128 : | |||
| 129 : | private void OnMouseDown(object sender, MouseEventArgs e) | ||
| 130 : | { | ||
| 131 : | Mouse.Capture(EventSource, CaptureMode.Element); | ||
| 132 : | albert | 7 | |
| 133 : | albert | 8 | //set the click point as the aim point |
| 134 : | albert | 7 | Vector3D currentPosition3D = ProjectToTrackball( |
| 135 : | EventSource.ActualWidth, EventSource.ActualHeight, e.GetPosition(EventSource)); | ||
| 136 : | albert | 8 | |
| 137 : | albert | 7 | |
| 138 : | albert | 8 | _camera.LookDirection = currentPosition3D; |
| 139 : | albert | 7 | |
| 140 : | albert | 4 | _previousPosition2D = e.GetPosition(EventSource); |
| 141 : | _previousPosition3D = ProjectToTrackball( | ||
| 142 : | EventSource.ActualWidth, | ||
| 143 : | EventSource.ActualHeight, | ||
| 144 : | _previousPosition2D); | ||
| 145 : | albert | 7 | |
| 146 : | |||
| 147 : | |||
| 148 : | albert | 4 | } |
| 149 : | |||
| 150 : | private void OnMouseUp(object sender, MouseEventArgs e) | ||
| 151 : | { | ||
| 152 : | Mouse.Capture(EventSource, CaptureMode.None); | ||
| 153 : | } | ||
| 154 : | |||
| 155 : | private void OnMouseMove(object sender, MouseEventArgs e) | ||
| 156 : | { | ||
| 157 : | Point currentPosition = e.GetPosition(EventSource); | ||
| 158 : | |||
| 159 : | if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) | ||
| 160 : | { | ||
| 161 : | Look(currentPosition); | ||
| 162 : | } | ||
| 163 : | albert | 7 | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))) |
| 164 : | { | ||
| 165 : | Focus(currentPosition); | ||
| 166 : | } | ||
| 167 : | albert | 4 | else if (e.LeftButton == MouseButtonState.Pressed) |
| 168 : | { | ||
| 169 : | Pan(currentPosition); | ||
| 170 : | } | ||
| 171 : | else if (e.RightButton == MouseButtonState.Pressed) | ||
| 172 : | { | ||
| 173 : | Zoom(currentPosition); | ||
| 174 : | } | ||
| 175 : | |||
| 176 : | albert | 7 | |
| 177 : | |||
| 178 : | albert | 4 | _previousPosition2D = currentPosition; |
| 179 : | } | ||
| 180 : | |||
| 181 : | #endregion Event Handling | ||
| 182 : | |||
| 183 : | private void Look(Point currentPosition) | ||
| 184 : | { | ||
| 185 : | Vector3D currentPosition3D = ProjectToTrackball( | ||
| 186 : | EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); | ||
| 187 : | |||
| 188 : | Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); | ||
| 189 : | double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); | ||
| 190 : | Quaternion delta = new Quaternion(axis, -angle); | ||
| 191 : | |||
| 192 : | // Get the current orientantion from the RotateTransform3D | ||
| 193 : | AxisAngleRotation3D r = _rotation; | ||
| 194 : | Quaternion q = new Quaternion(_rotation.Axis, _rotation.Angle); | ||
| 195 : | |||
| 196 : | // Compose the delta with the previous orientation | ||
| 197 : | q *= delta; | ||
| 198 : | |||
| 199 : | // Write the new orientation back to the Rotation3D | ||
| 200 : | _rotation.Axis = q.Axis; | ||
| 201 : | _rotation.Angle = q.Angle; | ||
| 202 : | |||
| 203 : | _previousPosition3D = currentPosition3D; | ||
| 204 : | } | ||
| 205 : | |||
| 206 : | private void Pan(Point currentPosition) | ||
| 207 : | { | ||
| 208 : | Vector3D currentPosition3D = ProjectToTrackball( | ||
| 209 : | EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); | ||
| 210 : | |||
| 211 : | Vector change = Point.Subtract(_previousPosition2D, currentPosition); | ||
| 212 : | |||
| 213 : | Vector3D changeVector = new Vector3D(change.X, change.Y, 0); | ||
| 214 : | |||
| 215 : | _translate.OffsetX += changeVector.X * .104; | ||
| 216 : | _translate.OffsetY -= changeVector.Y * .104; | ||
| 217 : | _translate.OffsetZ += changeVector.Z * .104; | ||
| 218 : | |||
| 219 : | _previousPosition3D = currentPosition3D; | ||
| 220 : | } | ||
| 221 : | |||
| 222 : | private Vector3D ProjectToTrackball(double width, double height, Point point) | ||
| 223 : | { | ||
| 224 : | double x = point.X / (width / 2); // Scale so bounds map to [0,0] - [2,2] | ||
| 225 : | double y = point.Y / (height / 2); | ||
| 226 : | |||
| 227 : | x = x - 1; // Translate 0,0 to the center | ||
| 228 : | y = 1 - y; // Flip so +Y is up instead of down | ||
| 229 : | |||
| 230 : | double z2 = 1 - x * x - y * y; // z^2 = 1 - x^2 - y^2 | ||
| 231 : | double z = z2 > 0 ? Math.Sqrt(z2) : 0; | ||
| 232 : | |||
| 233 : | return new Vector3D(x, y, z); | ||
| 234 : | } | ||
| 235 : | |||
| 236 : | private void Zoom(Point currentPosition) | ||
| 237 : | { | ||
| 238 : | double yDelta = currentPosition.Y - _previousPosition2D.Y; | ||
| 239 : | |||
| 240 : | double scale = Math.Exp(yDelta / 100); // e^(yDelta/100) is fairly arbitrary. | ||
| 241 : | |||
| 242 : | _scale.ScaleX *= scale; | ||
| 243 : | _scale.ScaleY *= scale; | ||
| 244 : | _scale.ScaleZ *= scale; | ||
| 245 : | } | ||
| 246 : | albert | 7 | |
| 247 : | //Point focusPoint = new Point(); | ||
| 248 : | |||
| 249 : | private void Focus(Point currentPosition) | ||
| 250 : | { | ||
| 251 : | //about zoom: | ||
| 252 : | //set the camera to lookat the position of current position | ||
| 253 : | //then, calculate the subcontract of the previous position and current pos that is the distance of zoomin/zoomout | ||
| 254 : | //then, set teh camera's position which can zoom in/out encircle object(position of mouse clicked) | ||
| 255 : | |||
| 256 : | //about rotation | ||
| 257 : | // | ||
| 258 : | albert | 8 | Vector3D currentPosition3D = ProjectToTrackball( |
| 259 : | EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); | ||
| 260 : | albert | 7 | |
| 261 : | albert | 8 | Vector change = Point.Subtract(_previousPosition2D, currentPosition); |
| 262 : | albert | 7 | |
| 263 : | albert | 8 | Vector3D changeVector = new Vector3D(change.X, change.Y, 0); |
| 264 : | albert | 7 | |
| 265 : | albert | 8 | _translate.OffsetX += changeVector.X * .104; |
| 266 : | _translate.OffsetY -= changeVector.Y * .104; | ||
| 267 : | _translate.OffsetZ += changeVector.Z * .104; | ||
| 268 : | albert | 7 | |
| 269 : | albert | 8 | _previousPosition3D = currentPosition3D; |
| 270 : | albert | 7 | } |
| 271 : | albert | 8 | |
| 272 : | public void SetCameraPosition(Point3D point) | ||
| 273 : | { | ||
| 274 : | |||
| 275 : | } | ||
| 276 : | albert | 4 | } |
| 277 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

