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

