Annotation of /trunk/DefaultRenderer/Trackball3D.cs
Parent Directory
|
Revision Log
Revision 11 - (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 : | albert | 9 | using System.Windows.Media; |
| 18 : | albert | 4 | using System.Windows.Media.Media3D; |
| 19 : | albert | 9 | using System.Windows.Controls; |
| 20 : | using Petzold.Media3D; | ||
| 21 : | albert | 4 | |
| 22 : | albert | 11 | |
| 23 : | albert | 4 | namespace Xenki.DefaultRenderer |
| 24 : | { | ||
| 25 : | /// <summary> | ||
| 26 : | /// Trackball is a utility class which observes the mouse events | ||
| 27 : | /// on a specified FrameworkElement and produces a Transform3D | ||
| 28 : | /// with the resultant rotation and scale. | ||
| 29 : | /// | ||
| 30 : | /// Example Usage: | ||
| 31 : | /// | ||
| 32 : | /// Trackball trackball = new Trackball(); | ||
| 33 : | /// trackball.EventSource = myElement; | ||
| 34 : | /// myViewport3D.Camera.Transform = trackball.Transform; | ||
| 35 : | /// | ||
| 36 : | /// Because Viewport3Ds only raise events when the mouse is over the | ||
| 37 : | /// rendered 3D geometry (as opposed to not when the mouse is within | ||
| 38 : | /// the layout bounds) you usually want to use another element as | ||
| 39 : | /// your EventSource. For example, a transparent border placed on | ||
| 40 : | /// top of your Viewport3D works well: | ||
| 41 : | /// | ||
| 42 : | /// <Grid> | ||
| 43 : | /// <ColumnDefinition /> | ||
| 44 : | /// <RowDefinition /> | ||
| 45 : | /// <Viewport3D Name="myViewport" ClipToBounds="True" Grid.Row="0" Grid.Column="0" /> | ||
| 46 : | /// <Border Name="myElement" Background="Transparent" Grid.Row="0" Grid.Column="0" /> | ||
| 47 : | /// </Grid> | ||
| 48 : | /// | ||
| 49 : | /// NOTE: The Transform property may be shared by multiple Cameras | ||
| 50 : | /// if you want to have auxilary views following the trackball. | ||
| 51 : | /// | ||
| 52 : | /// It can also be useful to share the Transform property with | ||
| 53 : | /// models in the scene that you want to move with the camera. | ||
| 54 : | /// (For example, the Trackport3D's headlight is implemented | ||
| 55 : | /// this way.) | ||
| 56 : | /// | ||
| 57 : | /// You may also use a Transform3DGroup to combine the | ||
| 58 : | /// Transform property with additional Transforms. | ||
| 59 : | /// </summary> | ||
| 60 : | public class Trackball | ||
| 61 : | { | ||
| 62 : | private FrameworkElement _eventSource; | ||
| 63 : | private Point _previousPosition2D; | ||
| 64 : | private Vector3D _previousPosition3D = new Vector3D(0, 0, 1); | ||
| 65 : | |||
| 66 : | private Transform3DGroup _transform; | ||
| 67 : | private ScaleTransform3D _scale = new ScaleTransform3D(); | ||
| 68 : | private AxisAngleRotation3D _rotation = new AxisAngleRotation3D(); | ||
| 69 : | private TranslateTransform3D _translate = new TranslateTransform3D(); | ||
| 70 : | |||
| 71 : | albert | 8 | private Vector3D _cameraLookDirection= new Vector3D(128,128,10); |
| 72 : | albert | 9 | |
| 73 : | albert | 8 | private PerspectiveCamera _camera; |
| 74 : | albert | 9 | private Point3D _cameraLookPoint; |
| 75 : | albert | 10 | private RotateTransform3D _rotationTransform =new RotateTransform3D(); |
| 76 : | albert | 9 | |
| 77 : | albert | 11 | bool isTracking=false; |
| 78 : | |||
| 79 : | albert | 8 | public Trackball(PerspectiveCamera camera) |
| 80 : | albert | 4 | { |
| 81 : | albert | 8 | _camera = camera; |
| 82 : | albert | 10 | _rotationTransform.Rotation = _rotation; |
| 83 : | albert | 4 | _transform = new Transform3DGroup(); |
| 84 : | _transform.Children.Add(_scale); | ||
| 85 : | albert | 10 | _transform.Children.Add(_rotationTransform); |
| 86 : | albert | 4 | _transform.Children.Add(_translate); |
| 87 : | } | ||
| 88 : | |||
| 89 : | /// <summary> | ||
| 90 : | /// A transform to move the camera or scene to the trackball's | ||
| 91 : | /// current orientation and scale. | ||
| 92 : | /// </summary> | ||
| 93 : | public Transform3D Transform | ||
| 94 : | { | ||
| 95 : | get { return _transform; } | ||
| 96 : | } | ||
| 97 : | albert | 9 | |
| 98 : | albert | 4 | #region Event Handling |
| 99 : | |||
| 100 : | /// <summary> | ||
| 101 : | /// The FrameworkElement we listen to for mouse events. | ||
| 102 : | /// </summary> | ||
| 103 : | public FrameworkElement EventSource | ||
| 104 : | { | ||
| 105 : | get { return _eventSource; } | ||
| 106 : | |||
| 107 : | set | ||
| 108 : | { | ||
| 109 : | if (_eventSource != null) | ||
| 110 : | { | ||
| 111 : | _eventSource.MouseDown -= this.OnMouseDown; | ||
| 112 : | _eventSource.MouseUp -= this.OnMouseUp; | ||
| 113 : | _eventSource.MouseMove -= this.OnMouseMove; | ||
| 114 : | albert | 10 | |
| 115 : | _eventSource.KeyDown -= this.OnKeyDown; | ||
| 116 : | albert | 4 | } |
| 117 : | |||
| 118 : | _eventSource = value; | ||
| 119 : | |||
| 120 : | _eventSource.MouseDown += this.OnMouseDown; | ||
| 121 : | _eventSource.MouseUp += this.OnMouseUp; | ||
| 122 : | _eventSource.MouseMove += this.OnMouseMove; | ||
| 123 : | albert | 10 | |
| 124 : | _eventSource.KeyDown += OnKeyDown; | ||
| 125 : | albert | 4 | } |
| 126 : | } | ||
| 127 : | albert | 11 | //this need be replaced by button clicks. |
| 128 : | albert | 10 | private void OnKeyDown(object sender, KeyEventArgs e) |
| 129 : | { | ||
| 130 : | if (e.Key == Key.Left) | ||
| 131 : | _translate.OffsetX = -1; | ||
| 132 : | else if (e.Key == Key.Right) | ||
| 133 : | _translate.OffsetX = 1; | ||
| 134 : | else if (e.Key == Key.Up) | ||
| 135 : | _translate.OffsetY = 1; | ||
| 136 : | else if (e.Key == Key.Down) | ||
| 137 : | _translate.OffsetY = -1; | ||
| 138 : | } | ||
| 139 : | albert | 4 | |
| 140 : | albert | 11 | //need add Petzold.Media3D copyright here. |
| 141 : | albert | 4 | private void OnMouseDown(object sender, MouseEventArgs e) |
| 142 : | { | ||
| 143 : | albert | 11 | //Mouse.Capture(EventSource, CaptureMode.Element); |
| 144 : | albert | 9 | |
| 145 : | Point pclick = e.GetPosition(EventSource); | ||
| 146 : | HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); | ||
| 147 : | RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; | ||
| 148 : | if (result3d == null) | ||
| 149 : | return; | ||
| 150 : | albert | 11 | |
| 151 : | albert | 9 | ModelVisual3D visul3d = result3d.VisualHit as ModelVisual3D; |
| 152 : | if (visul3d == null) | ||
| 153 : | return; | ||
| 154 : | |||
| 155 : | albert | 10 | _cameraLookPoint = new Point3D(visul3d.Content.Bounds.X, visul3d.Content.Bounds.Y, visul3d.Content.Bounds.Z); |
| 156 : | |||
| 157 : | albert | 11 | TranslateTransform3D tempTranslate = visul3d.Transform as TranslateTransform3D; |
| 158 : | if (tempTranslate == null) | ||
| 159 : | return; | ||
| 160 : | albert | 7 | |
| 161 : | albert | 11 | Petzold.Media3D.LineRange line; |
| 162 : | albert | 10 | |
| 163 : | albert | 11 | Petzold.Media3D.ViewportInfo.Point2DtoPoint3D(_eventSource as Viewport3D, pclick, out line); |
| 164 : | _cameraLookPoint = line.PointFromZ(tempTranslate.OffsetY); | ||
| 165 : | albert | 10 | |
| 166 : | albert | 11 | _rotationTransform.CenterX = _cameraLookPoint.X; |
| 167 : | albert | 10 | _rotationTransform.CenterY = _cameraLookPoint.Y; |
| 168 : | _rotationTransform.CenterZ = _cameraLookPoint.Z; | ||
| 169 : | albert | 11 | |
| 170 : | _camera.LookDirection = _cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 171 : | albert | 7 | |
| 172 : | albert | 11 | isTracking = true; |
| 173 : | _eventSource.CaptureMouse(); | ||
| 174 : | _eventSource.Focus(); | ||
| 175 : | albert | 4 | } |
| 176 : | |||
| 177 : | private void OnMouseUp(object sender, MouseEventArgs e) | ||
| 178 : | { | ||
| 179 : | Mouse.Capture(EventSource, CaptureMode.None); | ||
| 180 : | albert | 11 | isTracking = false; |
| 181 : | _eventSource.ReleaseMouseCapture(); | ||
| 182 : | albert | 4 | } |
| 183 : | |||
| 184 : | private void OnMouseMove(object sender, MouseEventArgs e) | ||
| 185 : | { | ||
| 186 : | Point currentPosition = e.GetPosition(EventSource); | ||
| 187 : | |||
| 188 : | if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) | ||
| 189 : | { | ||
| 190 : | Look(currentPosition); | ||
| 191 : | } | ||
| 192 : | albert | 7 | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))) |
| 193 : | { | ||
| 194 : | albert | 11 | if (isTracking) |
| 195 : | Focus(currentPosition); | ||
| 196 : | albert | 7 | } |
| 197 : | albert | 4 | else if (e.LeftButton == MouseButtonState.Pressed) |
| 198 : | { | ||
| 199 : | albert | 10 | // Pan(currentPosition); |
| 200 : | albert | 4 | } |
| 201 : | else if (e.RightButton == MouseButtonState.Pressed) | ||
| 202 : | { | ||
| 203 : | albert | 10 | // Zoom(currentPosition); |
| 204 : | albert | 4 | } |
| 205 : | |||
| 206 : | albert | 7 | |
| 207 : | |||
| 208 : | albert | 4 | _previousPosition2D = currentPosition; |
| 209 : | } | ||
| 210 : | |||
| 211 : | #endregion Event Handling | ||
| 212 : | |||
| 213 : | private void Look(Point currentPosition) | ||
| 214 : | { | ||
| 215 : | Vector3D currentPosition3D = ProjectToTrackball( | ||
| 216 : | EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); | ||
| 217 : | |||
| 218 : | Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); | ||
| 219 : | double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); | ||
| 220 : | Quaternion delta = new Quaternion(axis, -angle); | ||
| 221 : | |||
| 222 : | // Get the current orientantion from the RotateTransform3D | ||
| 223 : | AxisAngleRotation3D r = _rotation; | ||
| 224 : | Quaternion q = new Quaternion(_rotation.Axis, _rotation.Angle); | ||
| 225 : | |||
| 226 : | // Compose the delta with the previous orientation | ||
| 227 : | q *= delta; | ||
| 228 : | |||
| 229 : | // Write the new orientation back to the Rotation3D | ||
| 230 : | _rotation.Axis = q.Axis; | ||
| 231 : | _rotation.Angle = q.Angle; | ||
| 232 : | |||
| 233 : | _previousPosition3D = currentPosition3D; | ||
| 234 : | } | ||
| 235 : | |||
| 236 : | private void Pan(Point currentPosition) | ||
| 237 : | { | ||
| 238 : | Vector3D currentPosition3D = ProjectToTrackball( | ||
| 239 : | EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); | ||
| 240 : | |||
| 241 : | Vector change = Point.Subtract(_previousPosition2D, currentPosition); | ||
| 242 : | |||
| 243 : | Vector3D changeVector = new Vector3D(change.X, change.Y, 0); | ||
| 244 : | |||
| 245 : | _translate.OffsetX += changeVector.X * .104; | ||
| 246 : | _translate.OffsetY -= changeVector.Y * .104; | ||
| 247 : | _translate.OffsetZ += changeVector.Z * .104; | ||
| 248 : | |||
| 249 : | _previousPosition3D = currentPosition3D; | ||
| 250 : | } | ||
| 251 : | |||
| 252 : | private Vector3D ProjectToTrackball(double width, double height, Point point) | ||
| 253 : | { | ||
| 254 : | double x = point.X / (width / 2); // Scale so bounds map to [0,0] - [2,2] | ||
| 255 : | double y = point.Y / (height / 2); | ||
| 256 : | |||
| 257 : | x = x - 1; // Translate 0,0 to the center | ||
| 258 : | y = 1 - y; // Flip so +Y is up instead of down | ||
| 259 : | |||
| 260 : | double z2 = 1 - x * x - y * y; // z^2 = 1 - x^2 - y^2 | ||
| 261 : | double z = z2 > 0 ? Math.Sqrt(z2) : 0; | ||
| 262 : | |||
| 263 : | return new Vector3D(x, y, z); | ||
| 264 : | } | ||
| 265 : | |||
| 266 : | private void Zoom(Point currentPosition) | ||
| 267 : | { | ||
| 268 : | double yDelta = currentPosition.Y - _previousPosition2D.Y; | ||
| 269 : | |||
| 270 : | double scale = Math.Exp(yDelta / 100); // e^(yDelta/100) is fairly arbitrary. | ||
| 271 : | |||
| 272 : | _scale.ScaleX *= scale; | ||
| 273 : | _scale.ScaleY *= scale; | ||
| 274 : | _scale.ScaleZ *= scale; | ||
| 275 : | } | ||
| 276 : | albert | 7 | |
| 277 : | albert | 11 | |
| 278 : | albert | 7 | private void Focus(Point currentPosition) |
| 279 : | { | ||
| 280 : | //about zoom: | ||
| 281 : | albert | 11 | //set the camera to lookat the position of mouse clicked |
| 282 : | //Move the camera along the camera's looking direction TO zoom in/out | ||
| 283 : | //rotate around with the axix z and center is the position of mouse clicked. | ||
| 284 : | albert | 9 | |
| 285 : | double yDelta = currentPosition.Y - _previousPosition2D.Y; | ||
| 286 : | double scale = yDelta / 100; | ||
| 287 : | |||
| 288 : | albert | 11 | double distance ;//= Point3D.Subtract(_cameraLookPoint, _camera.Position).Length; |
| 289 : | |||
| 290 : | distance = (_cameraLookPoint.X - _camera.Position.X) * (_cameraLookPoint.X - _camera.Position.X) + | ||
| 291 : | (_cameraLookPoint.Y - _camera.Position.Y) * (_cameraLookPoint.Y - _camera.Position.Y) | ||
| 292 : | + (_cameraLookPoint.Z - _camera.Position.Z) * (_cameraLookPoint.Z - _camera.Position.Z); | ||
| 293 : | |||
| 294 : | if (distance >= 1024*1024 || distance <= 1) | ||
| 295 : | { | ||
| 296 : | isTracking = false; | ||
| 297 : | albert | 9 | return; |
| 298 : | albert | 11 | } |
| 299 : | albert | 10 | _translate.OffsetX -= scale * _cameraLookDirection.X; |
| 300 : | _translate.OffsetY -= scale * _cameraLookDirection.Y; | ||
| 301 : | _translate.OffsetZ -= scale * _cameraLookDirection.Z; | ||
| 302 : | albert | 11 | |
| 303 : | //rotation | ||
| 304 : | albert | 10 | double xDelta = currentPosition.X - _previousPosition2D.X; |
| 305 : | albert | 7 | |
| 306 : | albert | 11 | _rotation.Axis = new Vector3D(0, 0, 1); |
| 307 : | _rotation.Angle += xDelta/10; | ||
| 308 : | |||
| 309 : | albert | 7 | } |
| 310 : | albert | 8 | |
| 311 : | albert | 9 | |
| 312 : | albert | 4 | } |
| 313 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

