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

