Annotation of /trunk/DefaultRenderer/Trackball3D.cs
Parent Directory
|
Revision Log
Revision 15 - (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 | 13 | 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 | 13 | private RotateTransform3D _rotationTransform = new RotateTransform3D(); |
| 76 : | albert | 15 | ModelVisual3D clickedVisual; |
| 77 : | albert | 9 | |
| 78 : | albert | 13 | bool isTracking = false; |
| 79 : | albert | 11 | |
| 80 : | albert | 8 | public Trackball(PerspectiveCamera camera) |
| 81 : | albert | 4 | { |
| 82 : | albert | 8 | _camera = camera; |
| 83 : | albert | 10 | _rotationTransform.Rotation = _rotation; |
| 84 : | albert | 4 | _transform = new Transform3DGroup(); |
| 85 : | albert | 14 | |
| 86 : | //_transform.Children.Add(_translate); | ||
| 87 : | _transform.Children.Add(_rotationTransform); | ||
| 88 : | // _transform.Children.Add(_scale); | ||
| 89 : | albert | 13 | |
| 90 : | albert | 4 | } |
| 91 : | |||
| 92 : | /// <summary> | ||
| 93 : | /// A transform to move the camera or scene to the trackball's | ||
| 94 : | /// current orientation and scale. | ||
| 95 : | /// </summary> | ||
| 96 : | albert | 14 | public Transform3DGroup Transform |
| 97 : | albert | 4 | { |
| 98 : | get { return _transform; } | ||
| 99 : | } | ||
| 100 : | albert | 13 | |
| 101 : | albert | 4 | #region Event Handling |
| 102 : | |||
| 103 : | /// <summary> | ||
| 104 : | /// The FrameworkElement we listen to for mouse events. | ||
| 105 : | /// </summary> | ||
| 106 : | public FrameworkElement EventSource | ||
| 107 : | { | ||
| 108 : | get { return _eventSource; } | ||
| 109 : | |||
| 110 : | set | ||
| 111 : | { | ||
| 112 : | if (_eventSource != null) | ||
| 113 : | { | ||
| 114 : | albert | 14 | _eventSource.MouseWheel -= new MouseWheelEventHandler(_eventSource_MouseWheel); |
| 115 : | albert | 4 | _eventSource.MouseUp -= this.OnMouseUp; |
| 116 : | _eventSource.MouseMove -= this.OnMouseMove; | ||
| 117 : | albert | 14 | _eventSource.MouseLeftButtonDown-= _eventSource_MouseLeftButtonDown; |
| 118 : | albert | 4 | } |
| 119 : | _eventSource = value; | ||
| 120 : | albert | 14 | _eventSource.MouseWheel+=new MouseWheelEventHandler(_eventSource_MouseWheel); |
| 121 : | _eventSource.MouseLeftButtonDown += new MouseButtonEventHandler(_eventSource_MouseLeftButtonDown); | ||
| 122 : | albert | 4 | _eventSource.MouseUp += this.OnMouseUp; |
| 123 : | _eventSource.MouseMove += this.OnMouseMove; | ||
| 124 : | albert | 10 | |
| 125 : | albert | 4 | } |
| 126 : | } | ||
| 127 : | albert | 14 | |
| 128 : | void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) | ||
| 129 : | albert | 10 | { |
| 130 : | albert | 14 | |
| 131 : | double scale =1.00 + e.Delta / 1200.00 ; | ||
| 132 : | //if (_camera.FieldOfView >= 180 || _camera.FieldOfView <= 1) | ||
| 133 : | // return; | ||
| 134 : | |||
| 135 : | //_camera.FieldOfView += scale; | ||
| 136 : | |||
| 137 : | |||
| 138 : | _scale.ScaleX *= scale; | ||
| 139 : | _scale.ScaleY *= scale; | ||
| 140 : | // _scale.ScaleZ *= scale; | ||
| 141 : | albert | 10 | } |
| 142 : | albert | 4 | |
| 143 : | albert | 14 | void _eventSource_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
| 144 : | albert | 4 | { |
| 145 : | albert | 14 | Mouse.Capture(EventSource, CaptureMode.Element); |
| 146 : | albert | 9 | |
| 147 : | albert | 14 | Point pclick = e.GetPosition(EventSource); |
| 148 : | albert | 9 | HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
| 149 : | RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; | ||
| 150 : | if (result3d == null) | ||
| 151 : | return; | ||
| 152 : | albert | 11 | |
| 153 : | albert | 9 | ModelVisual3D visul3d = result3d.VisualHit as ModelVisual3D; |
| 154 : | if (visul3d == null) | ||
| 155 : | return; | ||
| 156 : | |||
| 157 : | albert | 11 | TranslateTransform3D tempTranslate = visul3d.Transform as TranslateTransform3D; |
| 158 : | if (tempTranslate == null) | ||
| 159 : | return; | ||
| 160 : | albert | 15 | clickedVisual = visul3d; |
| 161 : | albert | 11 | Petzold.Media3D.LineRange line; |
| 162 : | Petzold.Media3D.ViewportInfo.Point2DtoPoint3D(_eventSource as Viewport3D, pclick, out line); | ||
| 163 : | albert | 14 | _cameraLookPoint = line.PointFromY(visul3d.Content.Bounds.Y); |
| 164 : | albert | 10 | |
| 165 : | albert | 13 | _rotationTransform.CenterX = _cameraLookPoint.X; |
| 166 : | _rotationTransform.CenterY = _cameraLookPoint.Y; | ||
| 167 : | _rotationTransform.CenterZ = _cameraLookPoint.Z; | ||
| 168 : | albert | 7 | |
| 169 : | albert | 14 | _cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 170 : | _camera.LookDirection = _cameraLookDirection; | ||
| 171 : | albert | 13 | |
| 172 : | isTracking = true; | ||
| 173 : | _eventSource.CaptureMouse(); | ||
| 174 : | _eventSource.Focus(); | ||
| 175 : | e.Handled = true; | ||
| 176 : | albert | 4 | } |
| 177 : | albert | 14 | |
| 178 : | albert | 4 | |
| 179 : | albert | 14 | ////need add Petzold.Media3D copyright here. |
| 180 : | //private void OnMouseDown(object sender, MouseEventArgs e) | ||
| 181 : | //{ | ||
| 182 : | // Mouse.Capture(EventSource, CaptureMode.Element); | ||
| 183 : | |||
| 184 : | // Point pclick = e.GetPosition(EventSource); | ||
| 185 : | |||
| 186 : | // HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); | ||
| 187 : | |||
| 188 : | // RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; | ||
| 189 : | // if (result3d == null) | ||
| 190 : | // return; | ||
| 191 : | |||
| 192 : | // ModelVisual3D visul3d = result3d.VisualHit as ModelVisual3D; | ||
| 193 : | // if (visul3d == null) | ||
| 194 : | // return; | ||
| 195 : | |||
| 196 : | // TranslateTransform3D tempTranslate = visul3d.Transform as TranslateTransform3D; | ||
| 197 : | // if (tempTranslate == null) | ||
| 198 : | // return; | ||
| 199 : | |||
| 200 : | // Petzold.Media3D.LineRange line; | ||
| 201 : | // Petzold.Media3D.ViewportInfo.Point2DtoPoint3D(_eventSource as Viewport3D, pclick, out line); | ||
| 202 : | // _cameraLookPoint = line.PointFromY(visul3d.Content.Bounds.Y);// tempTranslate.OffsetX); | ||
| 203 : | |||
| 204 : | // _rotationTransform.CenterX = _cameraLookPoint.X; | ||
| 205 : | // _rotationTransform.CenterY = _cameraLookPoint.Y; | ||
| 206 : | // _rotationTransform.CenterZ = _cameraLookPoint.Z; | ||
| 207 : | |||
| 208 : | // _cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 209 : | |||
| 210 : | // _camera.LookDirection = _cameraLookDirection; | ||
| 211 : | |||
| 212 : | // isTracking = true; | ||
| 213 : | // _eventSource.CaptureMouse(); | ||
| 214 : | // _eventSource.Focus(); | ||
| 215 : | // e.Handled = true; | ||
| 216 : | //} | ||
| 217 : | |||
| 218 : | albert | 4 | private void OnMouseUp(object sender, MouseEventArgs e) |
| 219 : | { | ||
| 220 : | albert | 14 | Mouse.Capture(EventSource, CaptureMode.None); |
| 221 : | albert | 11 | isTracking = false; |
| 222 : | albert | 13 | |
| 223 : | albert | 11 | _eventSource.ReleaseMouseCapture(); |
| 224 : | albert | 15 | _eventSource.Cursor = Cursors.Arrow; |
| 225 : | string mmm = mm; | ||
| 226 : | albert | 4 | } |
| 227 : | |||
| 228 : | private void OnMouseMove(object sender, MouseEventArgs e) | ||
| 229 : | { | ||
| 230 : | Point currentPosition = e.GetPosition(EventSource); | ||
| 231 : | |||
| 232 : | if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) | ||
| 233 : | { | ||
| 234 : | albert | 12 | //Look(currentPosition); |
| 235 : | albert | 4 | } |
| 236 : | albert | 7 | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))) |
| 237 : | { | ||
| 238 : | albert | 11 | if (isTracking) |
| 239 : | albert | 12 | { |
| 240 : | albert | 15 | //_eventSource.Cursor = Cursors.ScrollAll; |
| 241 : | _eventSource.Cursor = Cursors.None; | ||
| 242 : | albert | 13 | Focus(currentPosition); |
| 243 : | albert | 15 | |
| 244 : | albert | 12 | } |
| 245 : | albert | 7 | } |
| 246 : | albert | 13 | else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.Q)) |
| 247 : | albert | 4 | { |
| 248 : | albert | 13 | CameraPanUP(currentPosition); |
| 249 : | albert | 4 | } |
| 250 : | _previousPosition2D = currentPosition; | ||
| 251 : | } | ||
| 252 : | |||
| 253 : | #endregion Event Handling | ||
| 254 : | |||
| 255 : | albert | 15 | string mm = ""; |
| 256 : | bool touch = false; | ||
| 257 : | albert | 7 | private void Focus(Point currentPosition) |
| 258 : | { | ||
| 259 : | //about zoom: | ||
| 260 : | albert | 11 | //set the camera to lookat the position of mouse clicked |
| 261 : | //Move the camera along the camera's looking direction TO zoom in/out | ||
| 262 : | //rotate around with the axix z and center is the position of mouse clicked. | ||
| 263 : | albert | 14 | |
| 264 : | albert | 9 | double yDelta = currentPosition.Y - _previousPosition2D.Y; |
| 265 : | albert | 14 | double scale = yDelta / 100; |
| 266 : | |||
| 267 : | albert | 12 | double distance = Point3D.Subtract(_cameraLookPoint, _camera.Position).Length; |
| 268 : | albert | 15 | |
| 269 : | if (distance > 1024 || distance <=0.5) | ||
| 270 : | { | ||
| 271 : | if (touch == false) | ||
| 272 : | { | ||
| 273 : | touch = true; | ||
| 274 : | } | ||
| 275 : | } | ||
| 276 : | else | ||
| 277 : | { | ||
| 278 : | touch = false; | ||
| 279 : | } | ||
| 280 : | mm += distance.ToString() + "\n\t\r"; | ||
| 281 : | if(touch==true) | ||
| 282 : | scale = -scale; | ||
| 283 : | albert | 14 | _camera.Position = new Point3D(_camera.Position.X - scale*_cameraLookDirection.X, _camera.Position.Y - scale*_cameraLookDirection.Y, _camera.Position.Z - scale*_cameraLookDirection.Z); |
| 284 : | albert | 12 | |
| 285 : | albert | 14 | //_translate.OffsetX -= scale * _cameraLookDirection.X;// _camera.LookDirection.X; |
| 286 : | //_translate.OffsetY -= scale * _cameraLookDirection.Y;//_camera.LookDirection.Y; | ||
| 287 : | //_translate.OffsetZ -= scale * _cameraLookDirection.Z;//_camera.LookDirection.Z; | ||
| 288 : | albert | 12 | |
| 289 : | albert | 14 | //_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 290 : | |||
| 291 : | double axisAngle = currentPosition.X - _previousPosition2D.X; | ||
| 292 : | double angletimes = axisAngle; | ||
| 293 : | albert | 12 | |
| 294 : | albert | 14 | if (angletimes > 360 || angletimes < -360) |
| 295 : | albert | 15 | angletimes =0; |
| 296 : | albert | 12 | |
| 297 : | albert | 14 | if (angletimes + _rotation.Angle >= 360) |
| 298 : | _rotation.Angle = 0; | ||
| 299 : | albert | 12 | _rotation.Axis = new Vector3D(0, 0, 1); |
| 300 : | albert | 13 | _rotation.Angle += axisAngle; |
| 301 : | } | ||
| 302 : | albert | 12 | |
| 303 : | albert | 15 | public HitTestResultBehavior HitTestCallback(HitTestResult result) |
| 304 : | { | ||
| 305 : | if (result.VisualHit != null) | ||
| 306 : | { | ||
| 307 : | return HitTestResultBehavior.Stop; | ||
| 308 : | } | ||
| 309 : | return HitTestResultBehavior.Continue; | ||
| 310 : | } | ||
| 311 : | |||
| 312 : | |||
| 313 : | albert | 13 | private void CameraPanUP(Point currentPosition) |
| 314 : | { | ||
| 315 : | double yDelta = currentPosition.Y - _previousPosition2D.Y; | ||
| 316 : | double xDelta = currentPosition.X - _previousPosition2D.X; | ||
| 317 : | double scale = yDelta / 10; | ||
| 318 : | |||
| 319 : | _camera.Position = new Point3D(_camera.Position.X, _camera.Position.Y, _camera.Position.Z + scale); | ||
| 320 : | } | ||
| 321 : | albert | 12 | |
| 322 : | |||
| 323 : | albert | 4 | } |
| 324 : | albert | 13 | |
| 325 : | |||
| 326 : | albert | 4 | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

