Annotation of /trunk/DefaultRenderer/CameraControl.cs
Parent Directory
|
Revision Log
Revision 22 - (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 clickedVisual; |
| 50 : | ModelVisual3D visul3d; | ||
| 51 : | albert | 17 | |
| 52 : | albert | 19 | //rotate and pan object clicked |
| 53 : | private TranslateTransform3D _clickedTranslate ;//= new TranslateTransform3D(); | ||
| 54 : | private TranslateTransform3D _clickedTranslateOriginal;// = new TranslateTransform3D(); | ||
| 55 : | albert | 22 | private RotateTransform3D _clickedRotate = new RotateTransform3D(); |
| 56 : | albert | 17 | private AxisAngleRotation3D _clickedAxisAngleRotate = new AxisAngleRotation3D(); |
| 57 : | albert | 19 | Point3D _clickedCenterPosition; |
| 58 : | |||
| 59 : | albert | 16 | |
| 60 : | bool isTracking = false; | ||
| 61 : | |||
| 62 : | 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 : | //_transform.Children.Add(_translate); | ||
| 70 : | _transform.Children.Add(_rotationTransform); | ||
| 71 : | // _transform.Children.Add(_scale); | ||
| 72 : | } | ||
| 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 : | #region Event Handling | ||
| 84 : | |||
| 85 : | /// <summary> | ||
| 86 : | /// The FrameworkElement we listen to for mouse events. | ||
| 87 : | /// </summary> | ||
| 88 : | public FrameworkElement EventSource | ||
| 89 : | { | ||
| 90 : | get { return _eventSource; } | ||
| 91 : | |||
| 92 : | set | ||
| 93 : | { | ||
| 94 : | if (_eventSource != null) | ||
| 95 : | { | ||
| 96 : | _eventSource.MouseWheel -= _eventSource_MouseWheel; | ||
| 97 : | _eventSource.MouseUp -= this.OnMouseUp; | ||
| 98 : | _eventSource.MouseMove -= this.OnMouseMove; | ||
| 99 : | _eventSource.MouseLeftButtonDown -= _eventSource_MouseLeftButtonDown; | ||
| 100 : | albert | 22 | _eventSource.MouseRightButtonDown -= _eventSource_MouseRightButtonDown; |
| 101 : | albert | 16 | } |
| 102 : | _eventSource = value; | ||
| 103 : | _eventSource.MouseWheel += _eventSource_MouseWheel; | ||
| 104 : | albert | 22 | _eventSource.MouseLeftButtonDown += _eventSource_MouseLeftButtonDown; |
| 105 : | _eventSource.MouseRightButtonDown += _eventSource_MouseRightButtonDown; | ||
| 106 : | _eventSource.MouseUp += this.OnMouseUp; | ||
| 107 : | albert | 16 | _eventSource.MouseMove += this.OnMouseMove; |
| 108 : | |||
| 109 : | } | ||
| 110 : | } | ||
| 111 : | |||
| 112 : | albert | 22 | void _eventSource_MouseRightButtonDown(object sender, MouseButtonEventArgs e) |
| 113 : | albert | 16 | { |
| 114 : | albert | 22 | Mouse.Capture(EventSource, CaptureMode.Element); |
| 115 : | albert | 16 | |
| 116 : | albert | 22 | Point pclick = e.GetPosition(EventSource); |
| 117 : | HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); | ||
| 118 : | RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; | ||
| 119 : | if (result3d == null) | ||
| 120 : | return; | ||
| 121 : | |||
| 122 : | visul3d = result3d.VisualHit as ModelVisual3D; | ||
| 123 : | if (visul3d == null) | ||
| 124 : | return; | ||
| 125 : | |||
| 126 : | Transform3DGroup group = visul3d.Transform as Transform3DGroup; | ||
| 127 : | if (group != null) | ||
| 128 : | albert | 16 | { |
| 129 : | albert | 22 | foreach (Transform3D tran in group.Children) |
| 130 : | albert | 16 | { |
| 131 : | albert | 22 | if (tran is RotateTransform3D) |
| 132 : | { | ||
| 133 : | _clickedRotate = new RotateTransform3D(); | ||
| 134 : | _clickedRotate = tran as RotateTransform3D; | ||
| 135 : | if (_clickedRotate != null) | ||
| 136 : | { | ||
| 137 : | _clickedAxisAngleRotate = new AxisAngleRotation3D(); | ||
| 138 : | _clickedRotate.Rotation = _clickedAxisAngleRotate; | ||
| 139 : | } | ||
| 140 : | } | ||
| 141 : | else if (tran is TranslateTransform3D) | ||
| 142 : | _clickedTranslate = tran as TranslateTransform3D; | ||
| 143 : | albert | 16 | } |
| 144 : | } | ||
| 145 : | else | ||
| 146 : | albert | 22 | return; |
| 147 : | //for pan the object | ||
| 148 : | _clickedTranslateOriginal = _clickedTranslate.Clone(); | ||
| 149 : | albert | 16 | |
| 150 : | albert | 22 | //for ALT + MOUSE + ROTATE |
| 151 : | Petzold.Media3D.LineRange line; | ||
| 152 : | Petzold.Media3D.ViewportInfo.Point2DtoPoint3D(_eventSource as Viewport3D, pclick, out line); | ||
| 153 : | _cameraLookPoint = line.PointFromY(visul3d.Content.Bounds.Y); | ||
| 154 : | |||
| 155 : | _rotationTransform.CenterX = _cameraLookPoint.X; | ||
| 156 : | _rotationTransform.CenterY = _cameraLookPoint.Y; | ||
| 157 : | _rotationTransform.CenterZ = _cameraLookPoint.Z; | ||
| 158 : | |||
| 159 : | //for rotate the object mouse clicked | ||
| 160 : | |||
| 161 : | _cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 162 : | _camera.LookDirection = _cameraLookDirection; | ||
| 163 : | |||
| 164 : | isTracking = true; | ||
| 165 : | _eventSource.CaptureMouse(); | ||
| 166 : | _eventSource.Focus(); | ||
| 167 : | e.Handled = true; | ||
| 168 : | albert | 16 | } |
| 169 : | |||
| 170 : | albert | 22 | |
| 171 : | albert | 16 | void _eventSource_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
| 172 : | { | ||
| 173 : | Mouse.Capture(EventSource, CaptureMode.Element); | ||
| 174 : | |||
| 175 : | Point pclick = e.GetPosition(EventSource); | ||
| 176 : | HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); | ||
| 177 : | RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; | ||
| 178 : | if (result3d == null) | ||
| 179 : | return; | ||
| 180 : | |||
| 181 : | albert | 19 | visul3d = result3d.VisualHit as ModelVisual3D; |
| 182 : | albert | 16 | if (visul3d == null) |
| 183 : | return; | ||
| 184 : | |||
| 185 : | albert | 17 | Transform3DGroup group = visul3d.Transform as Transform3DGroup; |
| 186 : | if (group != null) | ||
| 187 : | { | ||
| 188 : | albert | 19 | foreach (Transform3D tran in group.Children) |
| 189 : | albert | 17 | { |
| 190 : | albert | 19 | if (tran is RotateTransform3D) |
| 191 : | { | ||
| 192 : | _clickedRotate = new RotateTransform3D(); | ||
| 193 : | albert | 22 | _clickedRotate = tran as RotateTransform3D ; |
| 194 : | albert | 19 | if (_clickedRotate != null) |
| 195 : | { | ||
| 196 : | _clickedAxisAngleRotate = new AxisAngleRotation3D(); | ||
| 197 : | _clickedRotate.Rotation = _clickedAxisAngleRotate; | ||
| 198 : | } | ||
| 199 : | } | ||
| 200 : | else if (tran is TranslateTransform3D) | ||
| 201 : | albert | 22 | { |
| 202 : | //_clickedTranslate = new TranslateTransform3D(); | ||
| 203 : | _clickedTranslate =(TranslateTransform3D)tran ; | ||
| 204 : | } | ||
| 205 : | albert | 17 | } |
| 206 : | } | ||
| 207 : | albert | 19 | else |
| 208 : | return; | ||
| 209 : | albert | 16 | |
| 210 : | albert | 17 | //for pan the object |
| 211 : | _clickedTranslateOriginal = _clickedTranslate.Clone(); | ||
| 212 : | albert | 16 | |
| 213 : | albert | 17 | //for ALT + MOUSE + ROTATE |
| 214 : | albert | 16 | Petzold.Media3D.LineRange line; |
| 215 : | Petzold.Media3D.ViewportInfo.Point2DtoPoint3D(_eventSource as Viewport3D, pclick, out line); | ||
| 216 : | _cameraLookPoint = line.PointFromY(visul3d.Content.Bounds.Y); | ||
| 217 : | |||
| 218 : | _rotationTransform.CenterX = _cameraLookPoint.X; | ||
| 219 : | _rotationTransform.CenterY = _cameraLookPoint.Y; | ||
| 220 : | _rotationTransform.CenterZ = _cameraLookPoint.Z; | ||
| 221 : | |||
| 222 : | albert | 17 | //for rotate the object mouse clicked |
| 223 : | albert | 19 | _clickedRotate.CenterX = _clickedCenterPosition.X = visul3d.Content.Bounds.Location.X + visul3d.Content.Bounds.SizeX / 2; |
| 224 : | _clickedRotate.CenterY = _clickedCenterPosition.Y = visul3d.Content.Bounds.Location.Y + visul3d.Content.Bounds.SizeY / 2; | ||
| 225 : | _clickedRotate.CenterZ = _clickedCenterPosition.Z = visul3d.Content.Bounds.Location.Z + visul3d.Content.Bounds.SizeZ / 2; | ||
| 226 : | albert | 17 | |
| 227 : | albert | 16 | _cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 228 : | _camera.LookDirection = _cameraLookDirection; | ||
| 229 : | |||
| 230 : | albert | 17 | //TRACK BALL |
| 231 : | _previousPosition3D = ProjectToTrackball( | ||
| 232 : | EventSource.ActualWidth, | ||
| 233 : | EventSource.ActualHeight, | ||
| 234 : | _previousPosition2D); | ||
| 235 : | |||
| 236 : | albert | 16 | isTracking = true; |
| 237 : | _eventSource.CaptureMouse(); | ||
| 238 : | _eventSource.Focus(); | ||
| 239 : | e.Handled = true; | ||
| 240 : | } | ||
| 241 : | |||
| 242 : | private void OnMouseUp(object sender, MouseEventArgs e) | ||
| 243 : | { | ||
| 244 : | Mouse.Capture(EventSource, CaptureMode.None); | ||
| 245 : | isTracking = false; | ||
| 246 : | |||
| 247 : | _eventSource.ReleaseMouseCapture(); | ||
| 248 : | _eventSource.Cursor = Cursors.Arrow; | ||
| 249 : | } | ||
| 250 : | |||
| 251 : | 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 | 20 | //TODO: |
| 275 : | albert | 22 | if (isTracking) |
| 276 : | { | ||
| 277 : | _eventSource.Cursor = Cursors.None; | ||
| 278 : | CameraPanLeftRight(currentPosition); | ||
| 279 : | } | ||
| 280 : | albert | 17 | } |
| 281 : | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) | ||
| 282 : | && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) | ||
| 283 : | { | ||
| 284 : | if (isTracking) | ||
| 285 : | albert | 20 | { |
| 286 : | _eventSource.Cursor = Cursors.None; | ||
| 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 : | } | ||
| 297 : | albert | 16 | } |
| 298 : | albert | 22 | else if (e.LeftButton == MouseButtonState.Pressed) |
| 299 : | { | ||
| 300 : | LookAround(currentPosition); | ||
| 301 : | } | ||
| 302 : | albert | 17 | |
| 303 : | albert | 16 | _previousPosition2D = currentPosition; |
| 304 : | } | ||
| 305 : | |||
| 306 : | albert | 22 | private void LookAround(Point currentPosition) |
| 307 : | albert | 17 | { |
| 308 : | albert | 19 | Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); |
| 309 : | albert | 17 | |
| 310 : | Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); | ||
| 311 : | double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); | ||
| 312 : | albert | 22 | |
| 313 : | // We negate the angle because we are rotating the camera. | ||
| 314 : | // Do not do this if you are rotating the scene instead. | ||
| 315 : | albert | 17 | Quaternion delta = new Quaternion(axis, -angle); |
| 316 : | albert | 22 | |
| 317 : | albert | 17 | // Get the current orientantion from the RotateTransform3D |
| 318 : | albert | 22 | Transform3DGroup group = _camera.Transform as Transform3DGroup; |
| 319 : | if (group == null) | ||
| 320 : | return; | ||
| 321 : | albert | 17 | |
| 322 : | albert | 22 | foreach (RotateTransform3D rotate in group.Children) |
| 323 : | { | ||
| 324 : | RotateTransform3D rt = (RotateTransform3D)rotate; | ||
| 325 : | AxisAngleRotation3D r = (AxisAngleRotation3D)rt.Rotation; | ||
| 326 : | Quaternion q = new Quaternion(r.Axis, r.Angle); | ||
| 327 : | albert | 17 | |
| 328 : | albert | 22 | // Compose the delta with the previous orientation |
| 329 : | q *= delta; | ||
| 330 : | albert | 17 | |
| 331 : | albert | 22 | // Write the new orientation back to the Rotation3D |
| 332 : | r.Axis = q.Axis; | ||
| 333 : | r.Angle = q.Angle; | ||
| 334 : | } | ||
| 335 : | albert | 20 | |
| 336 : | albert | 17 | _previousPosition3D = currentPosition3D; |
| 337 : | } | ||
| 338 : | |||
| 339 : | albert | 22 | void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) |
| 340 : | { | ||
| 341 : | double scale = e.Delta / 1200.00; | ||
| 342 : | double distance = Point3D.Subtract(_cameraLookPoint, _camera.Position).Length; | ||
| 343 : | |||
| 344 : | if (distance > 1024 || distance <= 10) | ||
| 345 : | { | ||
| 346 : | if (touch == false) | ||
| 347 : | { | ||
| 348 : | touch = true; | ||
| 349 : | } | ||
| 350 : | } | ||
| 351 : | else | ||
| 352 : | { | ||
| 353 : | touch = false; | ||
| 354 : | } | ||
| 355 : | |||
| 356 : | if (touch == true) | ||
| 357 : | scale = -scale; | ||
| 358 : | _camera.Position = new Point3D(_camera.Position.X + scale * _cameraLookDirection.X, | ||
| 359 : | _camera.Position.Y + scale * _cameraLookDirection.Y, | ||
| 360 : | _camera.Position.Z + scale * _cameraLookDirection.Z); | ||
| 361 : | _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 362 : | } | ||
| 363 : | |||
| 364 : | private void RotateObject(Point currentPosition) | ||
| 365 : | { | ||
| 366 : | Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); | ||
| 367 : | |||
| 368 : | Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); | ||
| 369 : | double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); | ||
| 370 : | |||
| 371 : | //if (_clickedAxisAngleRotate.Angle >= 360||_clickedAxisAngleRotate.Angle<=-360) | ||
| 372 : | // _clickedAxisAngleRotate.Angle = 0; | ||
| 373 : | |||
| 374 : | _clickedAxisAngleRotate.Axis = axis; | ||
| 375 : | _clickedAxisAngleRotate.Angle -= angle; | ||
| 376 : | |||
| 377 : | _previousPosition3D = currentPosition3D; | ||
| 378 : | |||
| 379 : | } | ||
| 380 : | |||
| 381 : | albert | 17 | private Vector3D ProjectToTrackball(double width, double height, Point point) |
| 382 : | { | ||
| 383 : | double x = point.X / (width / 2); // Scale so bounds map to [0,0] - [2,2] | ||
| 384 : | double y = point.Y / (height / 2); | ||
| 385 : | |||
| 386 : | x = x - 1; // Translate 0,0 to the center | ||
| 387 : | y = 1 - y; // Flip so +Y is up instead of down | ||
| 388 : | |||
| 389 : | double z2 = 1 - x * x - y * y; // z^2 = 1 - x^2 - y^2 | ||
| 390 : | double z = z2 > 0 ? Math.Sqrt(z2) : 0; | ||
| 391 : | |||
| 392 : | albert | 22 | Vector3D v = new Vector3D(x, z, y); |
| 393 : | //v.Normalize(); | ||
| 394 : | return v; | ||
| 395 : | albert | 17 | } |
| 396 : | |||
| 397 : | #endregion Event Handling | ||
| 398 : | |||
| 399 : | albert | 20 | |
| 400 : | albert | 22 | private void CameraPanLeftRight(Point currentPosition) |
| 401 : | albert | 17 | { |
| 402 : | albert | 22 | double scalex = currentPosition.X - _previousPosition2D.X; |
| 403 : | |||
| 404 : | Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); | ||
| 405 : | albert | 17 | |
| 406 : | albert | 22 | _camera.Position += Math.Abs(scalex) * vector3D; |
| 407 : | albert | 17 | } |
| 408 : | |||
| 409 : | albert | 16 | private void PanObject(Point currentPosition) |
| 410 : | { | ||
| 411 : | albert | 17 | double scaley = currentPosition.Y - _previousPosition2D.Y; |
| 412 : | albert | 22 | scaley = Math.Abs( scaley / 1.00); |
| 413 : | albert | 16 | |
| 414 : | albert | 17 | |
| 415 : | double scalex = currentPosition.X - _previousPosition2D.X; | ||
| 416 : | albert | 22 | scalex = Math.Abs( scalex / 1.00); |
| 417 : | albert | 17 | |
| 418 : | albert | 21 | Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); |
| 419 : | |||
| 420 : | albert | 22 | _clickedTranslate.OffsetX += scalex * vector3D.X;// _clickedTranslateOriginal.OffsetX + vector3D.X; |
| 421 : | _clickedTranslate.OffsetY += scalex * vector3D.Y;// _clickedTranslateOriginal.OffsetY + vector3D.Y; | ||
| 422 : | _clickedTranslate.OffsetZ += scaley * vector3D.Z;// _clickedTranslateOriginal.OffsetZ + vector3D.Z;// vectMouse.Z; | ||
| 423 : | albert | 21 | |
| 424 : | 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)); |
| 425 : | albert | 21 | |
| 426 : | albert | 16 | } |
| 427 : | |||
| 428 : | albert | 21 | private Vector3D GetTranslationVector3D(DependencyObject modelHit, Point startPosition, Point endPosition) |
| 429 : | { | ||
| 430 : | Vector3D translationVector3D = new Vector3D(); | ||
| 431 : | albert | 16 | |
| 432 : | albert | 21 | Viewport3DVisual viewport = null; |
| 433 : | bool success = false; | ||
| 434 : | albert | 19 | |
| 435 : | albert | 21 | Matrix3D matrix3D = MathUtils.TryTransformTo2DAncestor(modelHit, out viewport, out success); |
| 436 : | if (success && matrix3D.HasInverse) | ||
| 437 : | { | ||
| 438 : | matrix3D.Invert(); | ||
| 439 : | Point3D startPoint3D = new Point3D(startPosition.X, startPosition.Y, 0); | ||
| 440 : | Point3D endPoint3D = new Point3D(endPosition.X, endPosition.Y, 0); | ||
| 441 : | Vector3D vector3D = endPoint3D - startPoint3D; | ||
| 442 : | translationVector3D = matrix3D.Transform(vector3D); | ||
| 443 : | } | ||
| 444 : | |||
| 445 : | return translationVector3D; | ||
| 446 : | } | ||
| 447 : | |||
| 448 : | |||
| 449 : | |||
| 450 : | albert | 16 | private void LookUpDown(Point currentPosition) |
| 451 : | { | ||
| 452 : | double scale = currentPosition.Y- _previousPosition2D.Y; | ||
| 453 : | scale = scale/5; | ||
| 454 : | _camera.LookDirection = new Vector3D(_camera.LookDirection.X, _camera.LookDirection.Y, _camera.LookDirection.Z + scale); | ||
| 455 : | } | ||
| 456 : | |||
| 457 : | bool touch = false; | ||
| 458 : | private void Focus(Point currentPosition) | ||
| 459 : | { | ||
| 460 : | double yDelta = currentPosition.Y - _previousPosition2D.Y; | ||
| 461 : | albert | 20 | double scale = yDelta / 100.0; |
| 462 : | albert | 16 | //Maybe this part need replaced by collision detection method |
| 463 : | double distance = Point3D.Subtract(_cameraLookPoint, _camera.Position).Length; | ||
| 464 : | albert | 20 | if (distance > 1024 || distance <= 1) |
| 465 : | albert | 16 | { |
| 466 : | if (touch == false) | ||
| 467 : | touch = true; | ||
| 468 : | } | ||
| 469 : | else | ||
| 470 : | { | ||
| 471 : | touch = false; | ||
| 472 : | } | ||
| 473 : | if (touch == true) | ||
| 474 : | scale = -scale; | ||
| 475 : | |||
| 476 : | _camera.Position = new Point3D(_camera.Position.X - scale * _cameraLookDirection.X, _camera.Position.Y - scale * _cameraLookDirection.Y, _camera.Position.Z - scale * _cameraLookDirection.Z); | ||
| 477 : | |||
| 478 : | //_translate.OffsetX -= scale * _cameraLookDirection.X;// _camera.LookDirection.X; | ||
| 479 : | //_translate.OffsetY -= scale * _cameraLookDirection.Y;//_camera.LookDirection.Y; | ||
| 480 : | //_translate.OffsetZ -= scale * _cameraLookDirection.Z;//_camera.LookDirection.Z; | ||
| 481 : | |||
| 482 : | _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 483 : | |||
| 484 : | double axisAngle = currentPosition.X - _previousPosition2D.X; | ||
| 485 : | double angletimes = axisAngle; | ||
| 486 : | |||
| 487 : | albert | 20 | if ( _rotation.Angle- angletimes <= -360) |
| 488 : | albert | 16 | _rotation.Angle = 0; |
| 489 : | _rotation.Axis = new Vector3D(0, 0, 1); | ||
| 490 : | albert | 20 | _rotation.Angle -= axisAngle; |
| 491 : | albert | 16 | } |
| 492 : | |||
| 493 : | public HitTestResultBehavior HitTestCallback(HitTestResult result) | ||
| 494 : | { | ||
| 495 : | if (result.VisualHit != null) | ||
| 496 : | { | ||
| 497 : | return HitTestResultBehavior.Stop; | ||
| 498 : | } | ||
| 499 : | return HitTestResultBehavior.Continue; | ||
| 500 : | } | ||
| 501 : | |||
| 502 : | private void CameraPanUP(Point currentPosition) | ||
| 503 : | { | ||
| 504 : | double yDelta = currentPosition.Y - _previousPosition2D.Y; | ||
| 505 : | double xDelta = currentPosition.X - _previousPosition2D.X; | ||
| 506 : | albert | 20 | double scale = yDelta / 5.00; |
| 507 : | albert | 16 | |
| 508 : | _camera.Position = new Point3D(_camera.Position.X, _camera.Position.Y, _camera.Position.Z + scale); | ||
| 509 : | } | ||
| 510 : | |||
| 511 : | |||
| 512 : | } | ||
| 513 : | |||
| 514 : | |||
| 515 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

