Annotation of /trunk/DefaultRenderer/CameraControl.cs
Parent Directory
|
Revision Log
Revision 24 - (view) (download)
| 1 : | albert | 16 | //------------------------------------------------------------------ |
| 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 : | albert | 21 | //------------------------------------------------------------------ |
| 15 : | // | ||
| 16 : | // using http://www.codeplex.com/3DTools | ||
| 17 : | // | ||
| 18 : | //------------------------------------------------------------------- | ||
| 19 : | |||
| 20 : | |||
| 21 : | albert | 16 | using System; |
| 22 : | using System.Windows; | ||
| 23 : | using System.Windows.Input; | ||
| 24 : | using System.Windows.Media; | ||
| 25 : | using System.Windows.Media.Media3D; | ||
| 26 : | using System.Windows.Controls; | ||
| 27 : | using Petzold.Media3D; | ||
| 28 : | albert | 21 | using _3DTools; |
| 29 : | albert | 16 | |
| 30 : | |||
| 31 : | namespace Xenki.DefaultRenderer | ||
| 32 : | { | ||
| 33 : | public class CameraControl | ||
| 34 : | { | ||
| 35 : | private FrameworkElement _eventSource; | ||
| 36 : | private Point _previousPosition2D; | ||
| 37 : | private Vector3D _previousPosition3D = new Vector3D(0, 0, 1); | ||
| 38 : | |||
| 39 : | private Transform3DGroup _transform; | ||
| 40 : | private ScaleTransform3D _scale = new ScaleTransform3D(); | ||
| 41 : | private AxisAngleRotation3D _rotation = new AxisAngleRotation3D(); | ||
| 42 : | private TranslateTransform3D _translate = new TranslateTransform3D(); | ||
| 43 : | |||
| 44 : | private Vector3D _cameraLookDirection = new Vector3D(128, 128, 10); | ||
| 45 : | |||
| 46 : | private PerspectiveCamera _camera; | ||
| 47 : | private Point3D _cameraLookPoint; | ||
| 48 : | private RotateTransform3D _rotationTransform = new RotateTransform3D(); | ||
| 49 : | albert | 19 | ModelVisual3D visul3d; |
| 50 : | albert | 17 | |
| 51 : | albert | 19 | //rotate and pan object clicked |
| 52 : | albert | 23 | private TranslateTransform3D _clickedTranslate = new TranslateTransform3D(); |
| 53 : | albert | 22 | private RotateTransform3D _clickedRotate = new RotateTransform3D(); |
| 54 : | albert | 17 | private AxisAngleRotation3D _clickedAxisAngleRotate = new AxisAngleRotation3D(); |
| 55 : | albert | 19 | Point3D _clickedCenterPosition; |
| 56 : | albert | 23 | Transform3DGroup _clickedGroup; |
| 57 : | albert | 16 | |
| 58 : | bool isTracking = false; | ||
| 59 : | |||
| 60 : | public CameraControl(PerspectiveCamera camera) | ||
| 61 : | { | ||
| 62 : | _camera = camera; | ||
| 63 : | albert | 17 | |
| 64 : | albert | 16 | _rotationTransform.Rotation = _rotation; |
| 65 : | albert | 17 | |
| 66 : | albert | 16 | _transform = new Transform3DGroup(); |
| 67 : | albert | 23 | // _transform.Children.Add(_translate); |
| 68 : | albert | 16 | _transform.Children.Add(_rotationTransform); |
| 69 : | albert | 23 | |
| 70 : | _cameraLookDirection = _camera.LookDirection; | ||
| 71 : | |||
| 72 : | albert | 16 | } |
| 73 : | |||
| 74 : | /// <summary> | ||
| 75 : | /// A transform to move the camera or scene to the trackball's | ||
| 76 : | /// current orientation and scale. | ||
| 77 : | /// </summary> | ||
| 78 : | public Transform3DGroup Transform | ||
| 79 : | { | ||
| 80 : | get { return _transform; } | ||
| 81 : | } | ||
| 82 : | |||
| 83 : | albert | 24 | [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetCursorPos")] |
| 84 : | [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] | ||
| 85 : | public static extern bool SetCursorPos(int X, int Y); | ||
| 86 : | |||
| 87 : | |||
| 88 : | albert | 16 | #region Event Handling |
| 89 : | |||
| 90 : | /// <summary> | ||
| 91 : | /// The FrameworkElement we listen to for mouse events. | ||
| 92 : | /// </summary> | ||
| 93 : | public FrameworkElement EventSource | ||
| 94 : | { | ||
| 95 : | get { return _eventSource; } | ||
| 96 : | |||
| 97 : | set | ||
| 98 : | { | ||
| 99 : | if (_eventSource != null) | ||
| 100 : | { | ||
| 101 : | _eventSource.MouseWheel -= _eventSource_MouseWheel; | ||
| 102 : | _eventSource.MouseUp -= this.OnMouseUp; | ||
| 103 : | _eventSource.MouseMove -= this.OnMouseMove; | ||
| 104 : | _eventSource.MouseLeftButtonDown -= _eventSource_MouseLeftButtonDown; | ||
| 105 : | albert | 22 | _eventSource.MouseRightButtonDown -= _eventSource_MouseRightButtonDown; |
| 106 : | albert | 16 | } |
| 107 : | _eventSource = value; | ||
| 108 : | _eventSource.MouseWheel += _eventSource_MouseWheel; | ||
| 109 : | albert | 22 | _eventSource.MouseLeftButtonDown += _eventSource_MouseLeftButtonDown; |
| 110 : | _eventSource.MouseRightButtonDown += _eventSource_MouseRightButtonDown; | ||
| 111 : | albert | 24 | _eventSource.MouseUp += this.OnMouseUp; |
| 112 : | albert | 16 | _eventSource.MouseMove += this.OnMouseMove; |
| 113 : | } | ||
| 114 : | } | ||
| 115 : | albert | 23 | |
| 116 : | |||
| 117 : | albert | 22 | void _eventSource_MouseRightButtonDown(object sender, MouseButtonEventArgs e) |
| 118 : | albert | 16 | { |
| 119 : | albert | 24 | Mouse.Capture(_eventSource, CaptureMode.Element); |
| 120 : | albert | 16 | |
| 121 : | albert | 24 | Point pclick = e.GetPosition(_eventSource); |
| 122 : | albert | 22 | HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
| 123 : | RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; | ||
| 124 : | if (result3d == null) | ||
| 125 : | return; | ||
| 126 : | |||
| 127 : | visul3d = result3d.VisualHit as ModelVisual3D; | ||
| 128 : | if (visul3d == null) | ||
| 129 : | return; | ||
| 130 : | |||
| 131 : | Transform3DGroup group = visul3d.Transform as Transform3DGroup; | ||
| 132 : | if (group != null) | ||
| 133 : | albert | 16 | { |
| 134 : | albert | 22 | foreach (Transform3D tran in group.Children) |
| 135 : | albert | 16 | { |
| 136 : | albert | 22 | if (tran is RotateTransform3D) |
| 137 : | { | ||
| 138 : | albert | 24 | |
| 139 : | albert | 22 | _clickedRotate = tran as RotateTransform3D; |
| 140 : | if (_clickedRotate != null) | ||
| 141 : | { | ||
| 142 : | _clickedAxisAngleRotate = new AxisAngleRotation3D(); | ||
| 143 : | _clickedRotate.Rotation = _clickedAxisAngleRotate; | ||
| 144 : | } | ||
| 145 : | } | ||
| 146 : | else if (tran is TranslateTransform3D) | ||
| 147 : | _clickedTranslate = tran as TranslateTransform3D; | ||
| 148 : | albert | 16 | } |
| 149 : | } | ||
| 150 : | else | ||
| 151 : | albert | 22 | return; |
| 152 : | albert | 16 | |
| 153 : | albert | 22 | //for ALT + MOUSE + ROTATE |
| 154 : | albert | 24 | VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); |
| 155 : | albert | 22 | |
| 156 : | _rotationTransform.CenterX = _cameraLookPoint.X; | ||
| 157 : | _rotationTransform.CenterY = _cameraLookPoint.Y; | ||
| 158 : | _rotationTransform.CenterZ = _cameraLookPoint.Z; | ||
| 159 : | |||
| 160 : | _cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 161 : | _camera.LookDirection = _cameraLookDirection; | ||
| 162 : | |||
| 163 : | isTracking = true; | ||
| 164 : | _eventSource.CaptureMouse(); | ||
| 165 : | _eventSource.Focus(); | ||
| 166 : | e.Handled = true; | ||
| 167 : | albert | 16 | } |
| 168 : | |||
| 169 : | albert | 24 | Point originalMouseClickPos = new Point(); |
| 170 : | albert | 16 | void _eventSource_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
| 171 : | { | ||
| 172 : | Mouse.Capture(EventSource, CaptureMode.Element); | ||
| 173 : | |||
| 174 : | Point pclick = e.GetPosition(EventSource); | ||
| 175 : | albert | 24 | originalMouseClickPos = _eventSource.PointToScreen(pclick); |
| 176 : | |||
| 177 : | albert | 16 | HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
| 178 : | RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; | ||
| 179 : | if (result3d == null) | ||
| 180 : | return; | ||
| 181 : | |||
| 182 : | albert | 19 | visul3d = result3d.VisualHit as ModelVisual3D; |
| 183 : | albert | 16 | if (visul3d == null) |
| 184 : | return; | ||
| 185 : | |||
| 186 : | albert | 23 | _clickedGroup = visul3d.Transform as Transform3DGroup; |
| 187 : | if (_clickedGroup != null) | ||
| 188 : | albert | 17 | { |
| 189 : | albert | 23 | |
| 190 : | foreach (Transform3D tran in _clickedGroup.Children) | ||
| 191 : | albert | 17 | { |
| 192 : | albert | 19 | if (tran is RotateTransform3D) |
| 193 : | { | ||
| 194 : | albert | 23 | _clickedRotate = tran as RotateTransform3D; |
| 195 : | albert | 19 | if (_clickedRotate != null) |
| 196 : | { | ||
| 197 : | albert | 23 | _clickedAxisAngleRotate = _clickedRotate.Rotation as AxisAngleRotation3D; |
| 198 : | if (_clickedAxisAngleRotate == null) | ||
| 199 : | _clickedAxisAngleRotate = new AxisAngleRotation3D(); | ||
| 200 : | |||
| 201 : | albert | 19 | _clickedRotate.Rotation = _clickedAxisAngleRotate; |
| 202 : | } | ||
| 203 : | } | ||
| 204 : | else if (tran is TranslateTransform3D) | ||
| 205 : | albert | 22 | { |
| 206 : | albert | 23 | _clickedTranslate = tran as TranslateTransform3D; |
| 207 : | albert | 22 | } |
| 208 : | albert | 17 | } |
| 209 : | } | ||
| 210 : | albert | 19 | else |
| 211 : | return; | ||
| 212 : | albert | 16 | |
| 213 : | albert | 17 | //for ALT + MOUSE + ROTATE |
| 214 : | albert | 24 | VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); |
| 215 : | albert | 16 | |
| 216 : | _rotationTransform.CenterX = _cameraLookPoint.X; | ||
| 217 : | _rotationTransform.CenterY = _cameraLookPoint.Y; | ||
| 218 : | _rotationTransform.CenterZ = _cameraLookPoint.Z; | ||
| 219 : | |||
| 220 : | albert | 17 | //for rotate the object mouse clicked |
| 221 : | albert | 19 | _clickedRotate.CenterX = _clickedCenterPosition.X = visul3d.Content.Bounds.Location.X + visul3d.Content.Bounds.SizeX / 2; |
| 222 : | _clickedRotate.CenterY = _clickedCenterPosition.Y = visul3d.Content.Bounds.Location.Y + visul3d.Content.Bounds.SizeY / 2; | ||
| 223 : | _clickedRotate.CenterZ = _clickedCenterPosition.Z = visul3d.Content.Bounds.Location.Z + visul3d.Content.Bounds.SizeZ / 2; | ||
| 224 : | albert | 17 | |
| 225 : | albert | 16 | _cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 226 : | _camera.LookDirection = _cameraLookDirection; | ||
| 227 : | |||
| 228 : | albert | 17 | //TRACK BALL |
| 229 : | _previousPosition3D = ProjectToTrackball( | ||
| 230 : | EventSource.ActualWidth, | ||
| 231 : | EventSource.ActualHeight, | ||
| 232 : | _previousPosition2D); | ||
| 233 : | |||
| 234 : | albert | 16 | isTracking = true; |
| 235 : | _eventSource.CaptureMouse(); | ||
| 236 : | _eventSource.Focus(); | ||
| 237 : | e.Handled = true; | ||
| 238 : | } | ||
| 239 : | |||
| 240 : | private void OnMouseUp(object sender, MouseEventArgs e) | ||
| 241 : | { | ||
| 242 : | Mouse.Capture(EventSource, CaptureMode.None); | ||
| 243 : | isTracking = false; | ||
| 244 : | albert | 24 | //set the mouse to the original position when clicked the object. |
| 245 : | SetCursorPos((int)originalMouseClickPos.X, (int)originalMouseClickPos.Y); | ||
| 246 : | |||
| 247 : | albert | 16 | _eventSource.ReleaseMouseCapture(); |
| 248 : | _eventSource.Cursor = Cursors.Arrow; | ||
| 249 : | } | ||
| 250 : | albert | 23 | |
| 251 : | albert | 16 | private void OnMouseMove(object sender, MouseEventArgs e) |
| 252 : | { | ||
| 253 : | Point currentPosition = e.GetPosition(EventSource); | ||
| 254 : | |||
| 255 : | if (e.RightButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) | ||
| 256 : | { | ||
| 257 : | LookUpDown(currentPosition); | ||
| 258 : | } | ||
| 259 : | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))) | ||
| 260 : | { | ||
| 261 : | if (isTracking) | ||
| 262 : | { | ||
| 263 : | _eventSource.Cursor = Cursors.None; | ||
| 264 : | Focus(currentPosition); | ||
| 265 : | } | ||
| 266 : | } | ||
| 267 : | else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.Q)) | ||
| 268 : | { | ||
| 269 : | albert | 22 | if(isTracking) |
| 270 : | CameraPanUP(currentPosition); | ||
| 271 : | albert | 16 | } |
| 272 : | albert | 17 | else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.A)) |
| 273 : | { | ||
| 274 : | albert | 22 | if (isTracking) |
| 275 : | { | ||
| 276 : | _eventSource.Cursor = Cursors.None; | ||
| 277 : | CameraPanLeftRight(currentPosition); | ||
| 278 : | } | ||
| 279 : | albert | 17 | } |
| 280 : | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) | ||
| 281 : | && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) | ||
| 282 : | { | ||
| 283 : | if (isTracking) | ||
| 284 : | albert | 20 | { |
| 285 : | _eventSource.Cursor = Cursors.None; | ||
| 286 : | albert | 23 | |
| 287 : | albert | 17 | RotateObject(currentPosition); |
| 288 : | albert | 20 | } |
| 289 : | albert | 17 | } |
| 290 : | albert | 16 | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) |
| 291 : | { | ||
| 292 : | albert | 20 | if (isTracking) |
| 293 : | { | ||
| 294 : | _eventSource.Cursor = Cursors.None; | ||
| 295 : | PanObject(currentPosition); | ||
| 296 : | albert | 23 | |
| 297 : | albert | 20 | } |
| 298 : | albert | 16 | } |
| 299 : | albert | 22 | else if (e.LeftButton == MouseButtonState.Pressed) |
| 300 : | { | ||
| 301 : | LookAround(currentPosition); | ||
| 302 : | } | ||
| 303 : | albert | 24 | |
| 304 : | |||
| 305 : | albert | 16 | _previousPosition2D = currentPosition; |
| 306 : | albert | 24 | |
| 307 : | albert | 16 | } |
| 308 : | |||
| 309 : | albert | 22 | private void LookAround(Point currentPosition) |
| 310 : | albert | 17 | { |
| 311 : | albert | 19 | Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); |
| 312 : | albert | 17 | |
| 313 : | Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); | ||
| 314 : | double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); | ||
| 315 : | albert | 22 | |
| 316 : | // We negate the angle because we are rotating the camera. | ||
| 317 : | // Do not do this if you are rotating the scene instead. | ||
| 318 : | albert | 17 | Quaternion delta = new Quaternion(axis, -angle); |
| 319 : | albert | 22 | |
| 320 : | albert | 17 | // Get the current orientantion from the RotateTransform3D |
| 321 : | albert | 22 | Transform3DGroup group = _camera.Transform as Transform3DGroup; |
| 322 : | if (group == null) | ||
| 323 : | return; | ||
| 324 : | albert | 17 | |
| 325 : | albert | 23 | foreach (Transform3D tran in group.Children) |
| 326 : | albert | 22 | { |
| 327 : | albert | 23 | RotateTransform3D rotate = tran as RotateTransform3D; |
| 328 : | if (rotate == null) | ||
| 329 : | continue; | ||
| 330 : | albert | 22 | RotateTransform3D rt = (RotateTransform3D)rotate; |
| 331 : | AxisAngleRotation3D r = (AxisAngleRotation3D)rt.Rotation; | ||
| 332 : | Quaternion q = new Quaternion(r.Axis, r.Angle); | ||
| 333 : | albert | 17 | |
| 334 : | albert | 22 | // Compose the delta with the previous orientation |
| 335 : | q *= delta; | ||
| 336 : | albert | 17 | |
| 337 : | albert | 22 | // Write the new orientation back to the Rotation3D |
| 338 : | r.Axis = q.Axis; | ||
| 339 : | r.Angle = q.Angle; | ||
| 340 : | } | ||
| 341 : | albert | 20 | |
| 342 : | albert | 17 | _previousPosition3D = currentPosition3D; |
| 343 : | } | ||
| 344 : | |||
| 345 : | albert | 22 | void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) |
| 346 : | { | ||
| 347 : | albert | 24 | double scale = -e.Delta / 1200.000; |
| 348 : | albert | 23 | |
| 349 : | albert | 24 | AdjustCameraPosition(_camera, _cameraLookDirection, scale); |
| 350 : | albert | 22 | } |
| 351 : | |||
| 352 : | private void RotateObject(Point currentPosition) | ||
| 353 : | { | ||
| 354 : | Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); | ||
| 355 : | |||
| 356 : | Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); | ||
| 357 : | double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); | ||
| 358 : | |||
| 359 : | albert | 23 | if (_clickedAxisAngleRotate.Angle >= 360 || _clickedAxisAngleRotate.Angle <= -360) |
| 360 : | _clickedAxisAngleRotate.Angle = 0; | ||
| 361 : | albert | 22 | |
| 362 : | _clickedAxisAngleRotate.Axis = axis; | ||
| 363 : | _clickedAxisAngleRotate.Angle -= angle; | ||
| 364 : | albert | 23 | |
| 365 : | albert | 22 | _previousPosition3D = currentPosition3D; |
| 366 : | |||
| 367 : | } | ||
| 368 : | |||
| 369 : | albert | 17 | private Vector3D ProjectToTrackball(double width, double height, Point point) |
| 370 : | { | ||
| 371 : | double x = point.X / (width / 2); // Scale so bounds map to [0,0] - [2,2] | ||
| 372 : | double y = point.Y / (height / 2); | ||
| 373 : | |||
| 374 : | x = x - 1; // Translate 0,0 to the center | ||
| 375 : | y = 1 - y; // Flip so +Y is up instead of down | ||
| 376 : | |||
| 377 : | double z2 = 1 - x * x - y * y; // z^2 = 1 - x^2 - y^2 | ||
| 378 : | double z = z2 > 0 ? Math.Sqrt(z2) : 0; | ||
| 379 : | |||
| 380 : | albert | 22 | Vector3D v = new Vector3D(x, z, y); |
| 381 : | albert | 23 | v.Normalize(); |
| 382 : | albert | 22 | return v; |
| 383 : | albert | 17 | } |
| 384 : | |||
| 385 : | #endregion Event Handling | ||
| 386 : | |||
| 387 : | albert | 20 | |
| 388 : | albert | 22 | private void CameraPanLeftRight(Point currentPosition) |
| 389 : | albert | 17 | { |
| 390 : | albert | 22 | double scalex = currentPosition.X - _previousPosition2D.X; |
| 391 : | |||
| 392 : | Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); | ||
| 393 : | albert | 17 | |
| 394 : | albert | 22 | _camera.Position += Math.Abs(scalex) * vector3D; |
| 395 : | albert | 17 | } |
| 396 : | |||
| 397 : | albert | 16 | private void PanObject(Point currentPosition) |
| 398 : | { | ||
| 399 : | albert | 17 | double scaley = currentPosition.Y - _previousPosition2D.Y; |
| 400 : | albert | 23 | scaley = Math.Abs( scaley ); |
| 401 : | albert | 16 | |
| 402 : | albert | 17 | double scalex = currentPosition.X - _previousPosition2D.X; |
| 403 : | albert | 23 | scalex = Math.Abs( scalex); |
| 404 : | albert | 17 | |
| 405 : | albert | 23 | |
| 406 : | albert | 21 | Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); |
| 407 : | |||
| 408 : | albert | 22 | _clickedTranslate.OffsetX += scalex * vector3D.X;// _clickedTranslateOriginal.OffsetX + vector3D.X; |
| 409 : | _clickedTranslate.OffsetY += scalex * vector3D.Y;// _clickedTranslateOriginal.OffsetY + vector3D.Y; | ||
| 410 : | _clickedTranslate.OffsetZ += scaley * vector3D.Z;// _clickedTranslateOriginal.OffsetZ + vector3D.Z;// vectMouse.Z; | ||
| 411 : | albert | 21 | |
| 412 : | albert | 22 | visul3d.Transform.Transform(new Point3D(visul3d.Content.Bounds.Location.X + scalex * vector3D.X, visul3d.Content.Bounds.Location.Y + scalex * vector3D.Y, visul3d.Content.Bounds.Location.Z + scaley * vector3D.Z)); |
| 413 : | albert | 21 | |
| 414 : | albert | 16 | } |
| 415 : | |||
| 416 : | albert | 21 | private Vector3D GetTranslationVector3D(DependencyObject modelHit, Point startPosition, Point endPosition) |
| 417 : | { | ||
| 418 : | Vector3D translationVector3D = new Vector3D(); | ||
| 419 : | albert | 16 | |
| 420 : | albert | 21 | Viewport3DVisual viewport = null; |
| 421 : | bool success = false; | ||
| 422 : | albert | 19 | |
| 423 : | albert | 21 | Matrix3D matrix3D = MathUtils.TryTransformTo2DAncestor(modelHit, out viewport, out success); |
| 424 : | if (success && matrix3D.HasInverse) | ||
| 425 : | { | ||
| 426 : | matrix3D.Invert(); | ||
| 427 : | Point3D startPoint3D = new Point3D(startPosition.X, startPosition.Y, 0); | ||
| 428 : | Point3D endPoint3D = new Point3D(endPosition.X, endPosition.Y, 0); | ||
| 429 : | Vector3D vector3D = endPoint3D - startPoint3D; | ||
| 430 : | translationVector3D = matrix3D.Transform(vector3D); | ||
| 431 : | } | ||
| 432 : | |||
| 433 : | return translationVector3D; | ||
| 434 : | } | ||
| 435 : | |||
| 436 : | |||
| 437 : | |||
| 438 : | albert | 16 | private void LookUpDown(Point currentPosition) |
| 439 : | { | ||
| 440 : | double scale = currentPosition.Y- _previousPosition2D.Y; | ||
| 441 : | albert | 23 | scale = scale/5.000000; |
| 442 : | albert | 16 | _camera.LookDirection = new Vector3D(_camera.LookDirection.X, _camera.LookDirection.Y, _camera.LookDirection.Z + scale); |
| 443 : | } | ||
| 444 : | |||
| 445 : | private void Focus(Point currentPosition) | ||
| 446 : | { | ||
| 447 : | albert | 23 | //zoom |
| 448 : | albert | 16 | double yDelta = currentPosition.Y - _previousPosition2D.Y; |
| 449 : | albert | 24 | double scale = yDelta / 100.000000; |
| 450 : | AdjustCameraPosition(_camera,_cameraLookDirection,scale); | ||
| 451 : | albert | 23 | |
| 452 : | //rotate the camera | ||
| 453 : | double axisAngle = currentPosition.X - _previousPosition2D.X; | ||
| 454 : | _rotation.Axis = new Vector3D(0, 0, 1); | ||
| 455 : | _rotation.Angle -= axisAngle; | ||
| 456 : | |||
| 457 : | _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 458 : | albert | 24 | } |
| 459 : | albert | 16 | |
| 460 : | albert | 24 | private void AdjustCameraPosition(PerspectiveCamera camera,Vector3D lookDirection, double scale ) |
| 461 : | { | ||
| 462 : | Point3D cameraPos = new Point3D(camera.Position.X - scale * lookDirection.X, | ||
| 463 : | camera.Position.Y - scale * lookDirection.Y, | ||
| 464 : | camera.Position.Z - scale * lookDirection.Z); | ||
| 465 : | albert | 16 | |
| 466 : | albert | 24 | //too fast |
| 467 : | if (Point3D.Subtract(cameraPos, camera.Position).Length > 1.000000000) | ||
| 468 : | cameraPos = new Point3D(camera.Position.X - scale / 10 * lookDirection.X, | ||
| 469 : | camera.Position.Y - scale / 10 * lookDirection.Y, | ||
| 470 : | camera.Position.Z - scale / 10 * lookDirection.Z); | ||
| 471 : | |||
| 472 : | double distance = Point3D.Subtract(_cameraLookPoint, cameraPos).Length; | ||
| 473 : | |||
| 474 : | if (distance > 524 || distance <= 1.000000) | ||
| 475 : | { | ||
| 476 : | } | ||
| 477 : | else | ||
| 478 : | camera.Position = cameraPos; | ||
| 479 : | albert | 16 | } |
| 480 : | albert | 24 | //get the clicked 3d position |
| 481 : | albert | 16 | public HitTestResultBehavior HitTestCallback(HitTestResult result) |
| 482 : | { | ||
| 483 : | if (result.VisualHit != null) | ||
| 484 : | { | ||
| 485 : | albert | 23 | RayHitTestResult r = result as RayHitTestResult; |
| 486 : | if (r != null) | ||
| 487 : | { | ||
| 488 : | RayMeshGeometry3DHitTestResult t = r as RayMeshGeometry3DHitTestResult; | ||
| 489 : | _cameraLookPoint = t.PointHit; | ||
| 490 : | albert | 24 | return HitTestResultBehavior.Stop; |
| 491 : | albert | 23 | } |
| 492 : | albert | 16 | } |
| 493 : | return HitTestResultBehavior.Continue; | ||
| 494 : | } | ||
| 495 : | |||
| 496 : | private void CameraPanUP(Point currentPosition) | ||
| 497 : | { | ||
| 498 : | double yDelta = currentPosition.Y - _previousPosition2D.Y; | ||
| 499 : | double xDelta = currentPosition.X - _previousPosition2D.X; | ||
| 500 : | albert | 20 | double scale = yDelta / 5.00; |
| 501 : | albert | 16 | |
| 502 : | _camera.Position = new Point3D(_camera.Position.X, _camera.Position.Y, _camera.Position.Z + scale); | ||
| 503 : | } | ||
| 504 : | |||
| 505 : | |||
| 506 : | } | ||
| 507 : | |||
| 508 : | |||
| 509 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

