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

