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

