Annotation of /trunk/DefaultRenderer/CameraControl.cs
Parent Directory
|
Revision Log
Revision 81 - (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 | 16 | private PerspectiveCamera _camera; |
| 41 : | private Point3D _cameraLookPoint; | ||
| 42 : | albert | 19 | ModelVisual3D visul3d; |
| 43 : | albert | 17 | |
| 44 : | albert | 19 | Point3D _clickedCenterPosition; |
| 45 : | albert | 16 | |
| 46 : | albert | 77 | #region avatar |
| 47 : | |||
| 48 : | albert | 74 | DefaultAvatarManager avatarManager = DefaultAvatarManager.SingletonInstance(); |
| 49 : | albert | 77 | |
| 50 : | #endregion | ||
| 51 : | albert | 81 | HotkeyDispatcher KeyDispatcher = HotkeyDispatcher.GetInstance(); |
| 52 : | albert | 74 | |
| 53 : | albert | 16 | bool isTracking = false; |
| 54 : | albert | 29 | bool isPanningObject = false; |
| 55 : | albert | 16 | |
| 56 : | albert | 60 | DefaultPrimControl m_primitiveData; |
| 57 : | albert | 50 | |
| 58 : | albert | 77 | public event CamPositionChanged OnCameraPositionChanged; |
| 59 : | albert | 64 | |
| 60 : | albert | 76 | public CameraControl(PerspectiveCamera camera, DefaultPrimControl datacontrol) |
| 61 : | albert | 16 | { |
| 62 : | albert | 76 | //dataControl = DefaultNetwork.DefaultNetwork.m_primitiveData; |
| 63 : | albert | 16 | _camera = camera; |
| 64 : | albert | 50 | m_primitiveData = datacontrol; |
| 65 : | albert | 77 | avatarManager.OnAvatarPosChanged += new AvatarPositionChanged(avatarManager_OnAvatarPosChanged); |
| 66 : | albert | 81 | avatarManager.OnAvatarTrunAround += new AvatarTurnAround(avatarManager_OnAvatarTrunAround); |
| 67 : | |||
| 68 : | KeyDispatcher.OnMouseLeftDown += new MouseLeftDown(KeyDispatcher_OnMouseLeftDown); | ||
| 69 : | KeyDispatcher.OnMouseMove += new MouseMove(KeyDispatcher_OnMouseMove); | ||
| 70 : | KeyDispatcher.OnMouseRightDown += new MouseRightDown(KeyDispatcher_OnMouseRightDown); | ||
| 71 : | KeyDispatcher.OnMouseWheel += new MouseWheel(KeyDispatcher_OnMouseWheel); | ||
| 72 : | KeyDispatcher.OnMouseUp += new MouseUp(KeyDispatcher_OnMouseUp); | ||
| 73 : | |||
| 74 : | albert | 16 | } |
| 75 : | albert | 77 | |
| 76 : | albert | 81 | void KeyDispatcher_OnMouseUp(XMouseEventArgs arg) |
| 77 : | { | ||
| 78 : | //set the mouse to the original position when clicked the object. | ||
| 79 : | if (_eventSource.Cursor != Cursors.Arrow) | ||
| 80 : | { | ||
| 81 : | SetCursorPos((int)originalMouseClickPos.X, (int)originalMouseClickPos.Y); | ||
| 82 : | _eventSource.Cursor = Cursors.Arrow; | ||
| 83 : | } | ||
| 84 : | Mouse.Capture(EventSource, CaptureMode.None); | ||
| 85 : | isTracking = false; | ||
| 86 : | if (isPanningObject == true) | ||
| 87 : | { | ||
| 88 : | isPanningObject = false; | ||
| 89 : | _cameraLookPoint = GetObjectCenterPosition(visul3d); | ||
| 90 : | } | ||
| 91 : | |||
| 92 : | _eventSource.ReleaseMouseCapture(); | ||
| 93 : | } | ||
| 94 : | |||
| 95 : | void KeyDispatcher_OnMouseWheel(MouseWheelEventArgs arg) | ||
| 96 : | { | ||
| 97 : | double scale = -arg.Delta / 800.000; | ||
| 98 : | if (_cameraLookPoint != null && _cameraLookPoint != new Point3D(0, 0, 0)) | ||
| 99 : | _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 100 : | |||
| 101 : | int count = Math.Abs((int)(scale / zoomFactor)); | ||
| 102 : | for (int i = 1; i <= count; i++) | ||
| 103 : | { | ||
| 104 : | if (scale > 0) | ||
| 105 : | AdjustCameraPosition(_camera, _camera.LookDirection, zoomFactor); | ||
| 106 : | else | ||
| 107 : | AdjustCameraPosition(_camera, _camera.LookDirection, -zoomFactor); | ||
| 108 : | } | ||
| 109 : | } | ||
| 110 : | |||
| 111 : | |||
| 112 : | |||
| 113 : | void KeyDispatcher_OnMouseRightDown(XMouseEventArgs arg) | ||
| 114 : | { | ||
| 115 : | Mouse.Capture(_eventSource, CaptureMode.Element); | ||
| 116 : | |||
| 117 : | Point pclick = arg.MouseClicked; | ||
| 118 : | originalMouseClickPos = arg.ScreenPoint; | ||
| 119 : | |||
| 120 : | HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); | ||
| 121 : | RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; | ||
| 122 : | if (result3d == null) | ||
| 123 : | return; | ||
| 124 : | |||
| 125 : | visul3d = result3d.VisualHit as ModelVisual3D; | ||
| 126 : | if (visul3d == null) | ||
| 127 : | return; | ||
| 128 : | |||
| 129 : | //for ALT + MOUSE + ROTATE | ||
| 130 : | VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); | ||
| 131 : | |||
| 132 : | _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 133 : | |||
| 134 : | isTracking = true; | ||
| 135 : | _eventSource.CaptureMouse(); | ||
| 136 : | _eventSource.Focus(); | ||
| 137 : | } | ||
| 138 : | |||
| 139 : | void KeyDispatcher_OnMouseMove(XMouseEventArgs arg) | ||
| 140 : | { | ||
| 141 : | currentPosition = arg.MouseClicked; | ||
| 142 : | |||
| 143 : | if (arg.MouseArgs.RightButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) | ||
| 144 : | { | ||
| 145 : | LookUpDown(currentPosition); | ||
| 146 : | } | ||
| 147 : | else if (arg.MouseArgs.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))) | ||
| 148 : | { | ||
| 149 : | if (isTracking) | ||
| 150 : | { | ||
| 151 : | _eventSource.Cursor = Cursors.None; | ||
| 152 : | Focus(currentPosition); | ||
| 153 : | } | ||
| 154 : | } | ||
| 155 : | else if (arg.MouseArgs.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.A)) | ||
| 156 : | { | ||
| 157 : | if (isTracking) | ||
| 158 : | { | ||
| 159 : | _eventSource.Cursor = Cursors.None; | ||
| 160 : | CameraPanLeftRight(currentPosition); | ||
| 161 : | } | ||
| 162 : | } | ||
| 163 : | else if (arg.MouseArgs.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) | ||
| 164 : | && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) | ||
| 165 : | { | ||
| 166 : | if (isTracking) | ||
| 167 : | { | ||
| 168 : | _eventSource.Cursor = Cursors.None; | ||
| 169 : | |||
| 170 : | RotateObject(currentPosition); | ||
| 171 : | } | ||
| 172 : | } | ||
| 173 : | else if (arg.MouseArgs.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) | ||
| 174 : | { | ||
| 175 : | if (isTracking) | ||
| 176 : | { | ||
| 177 : | _eventSource.Cursor = Cursors.None; | ||
| 178 : | isPanningObject = true; | ||
| 179 : | PanObject(currentPosition); | ||
| 180 : | } | ||
| 181 : | } | ||
| 182 : | else if (arg.MouseArgs.LeftButton == MouseButtonState.Pressed) | ||
| 183 : | { | ||
| 184 : | if (isTracking) | ||
| 185 : | LookAround(currentPosition); | ||
| 186 : | } | ||
| 187 : | else if (arg.MouseArgs.RightButton == MouseButtonState.Pressed) | ||
| 188 : | { | ||
| 189 : | if (isTracking) | ||
| 190 : | CameraPanUP(currentPosition); | ||
| 191 : | } | ||
| 192 : | |||
| 193 : | _previousPosition2D = currentPosition; | ||
| 194 : | } | ||
| 195 : | |||
| 196 : | |||
| 197 : | void KeyDispatcher_OnMouseLeftDown(XMouseEventArgs arg) | ||
| 198 : | { | ||
| 199 : | Point pclick = arg.MouseClicked; | ||
| 200 : | originalMouseClickPos = arg.ScreenPoint; | ||
| 201 : | |||
| 202 : | HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); | ||
| 203 : | RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; | ||
| 204 : | if (result3d == null) | ||
| 205 : | return; | ||
| 206 : | |||
| 207 : | visul3d = result3d.VisualHit as ModelVisual3D; | ||
| 208 : | if (visul3d == null) | ||
| 209 : | return; | ||
| 210 : | |||
| 211 : | //for ALT + MOUSE + ROTATE | ||
| 212 : | VisualTreeHelper.HitTest(EventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); | ||
| 213 : | |||
| 214 : | _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 215 : | |||
| 216 : | _clickedCenterPosition = GetObjectCenterPosition(visul3d); | ||
| 217 : | |||
| 218 : | //TRACK BALL | ||
| 219 : | _previousPosition3D = ProjectToTrackball( | ||
| 220 : | EventSource.ActualWidth, | ||
| 221 : | EventSource.ActualHeight, | ||
| 222 : | _previousPosition2D); | ||
| 223 : | |||
| 224 : | isTracking = true; | ||
| 225 : | } | ||
| 226 : | |||
| 227 : | void avatarManager_OnAvatarTrunAround(Point3D avatarposition, Vector3D avatarlookdir) | ||
| 228 : | { | ||
| 229 : | ResetCameraPos(avatarposition); | ||
| 230 : | |||
| 231 : | ResetCameraLookDirection(avatarlookdir); | ||
| 232 : | } | ||
| 233 : | |||
| 234 : | albert | 77 | void avatarManager_OnAvatarPosChanged(uint avatarid, Point3D oldpos, Point3D newpos) |
| 235 : | { | ||
| 236 : | ResetCameraPos(newpos); | ||
| 237 : | |||
| 238 : | if (OnCameraPositionChanged != null) | ||
| 239 : | { | ||
| 240 : | OnCameraPositionChanged(_camera.Position, newpos); | ||
| 241 : | } | ||
| 242 : | } | ||
| 243 : | albert | 29 | |
| 244 : | albert | 24 | [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetCursorPos")] |
| 245 : | [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] | ||
| 246 : | public static extern bool SetCursorPos(int X, int Y); | ||
| 247 : | |||
| 248 : | |||
| 249 : | albert | 16 | #region Event Handling |
| 250 : | |||
| 251 : | /// <summary> | ||
| 252 : | /// The FrameworkElement we listen to for mouse events. | ||
| 253 : | /// </summary> | ||
| 254 : | public FrameworkElement EventSource | ||
| 255 : | { | ||
| 256 : | get { return _eventSource; } | ||
| 257 : | |||
| 258 : | set | ||
| 259 : | { | ||
| 260 : | albert | 81 | _eventSource = value; |
| 261 : | /* | ||
| 262 : | albert | 16 | if (_eventSource != null) |
| 263 : | { | ||
| 264 : | _eventSource.MouseWheel -= _eventSource_MouseWheel; | ||
| 265 : | _eventSource.MouseUp -= this.OnMouseUp; | ||
| 266 : | _eventSource.MouseMove -= this.OnMouseMove; | ||
| 267 : | _eventSource.MouseLeftButtonDown -= _eventSource_MouseLeftButtonDown; | ||
| 268 : | albert | 22 | _eventSource.MouseRightButtonDown -= _eventSource_MouseRightButtonDown; |
| 269 : | albert | 31 | _eventSource.KeyDown -= this._eventSource_KeyDown; |
| 270 : | albert | 16 | } |
| 271 : | albert | 81 | |
| 272 : | albert | 16 | _eventSource.MouseWheel += _eventSource_MouseWheel; |
| 273 : | albert | 22 | _eventSource.MouseLeftButtonDown += _eventSource_MouseLeftButtonDown; |
| 274 : | _eventSource.MouseRightButtonDown += _eventSource_MouseRightButtonDown; | ||
| 275 : | albert | 24 | _eventSource.MouseUp += this.OnMouseUp; |
| 276 : | albert | 16 | _eventSource.MouseMove += this.OnMouseMove; |
| 277 : | albert | 81 | _eventSource.KeyDown += _eventSource_KeyDown; |
| 278 : | |||
| 279 : | albert | 31 | _eventSource.Focusable = true; |
| 280 : | _eventSource.Focus(); | ||
| 281 : | Keyboard.Focus(_eventSource); | ||
| 282 : | albert | 81 | */ |
| 283 : | albert | 16 | } |
| 284 : | } | ||
| 285 : | albert | 81 | /* |
| 286 : | albert | 31 | void _eventSource_KeyDown(object sender, KeyEventArgs e) |
| 287 : | { | ||
| 288 : | albert | 81 | if ((e.Key == Key.Left || e.Key == Key.A) && Mouse.LeftButton == MouseButtonState.Released && MouseButtonState.Released == Mouse.RightButton) |
| 289 : | albert | 31 | { |
| 290 : | CameraLookLeftRight(-4.0); | ||
| 291 : | |||
| 292 : | e.Handled = true; | ||
| 293 : | } | ||
| 294 : | albert | 81 | else if (e.Key == Key.Right || e.Key == Key.D) |
| 295 : | albert | 31 | { |
| 296 : | CameraLookLeftRight(4.0); | ||
| 297 : | |||
| 298 : | e.Handled = true; | ||
| 299 : | } | ||
| 300 : | albert | 80 | else if (e.Key == Key.Up || e.Key == Key.W) |
| 301 : | { | ||
| 302 : | //walk forward | ||
| 303 : | AvatarWalk(-0.1f); | ||
| 304 : | e.Handled = true; | ||
| 305 : | } | ||
| 306 : | else if (e.Key == Key.Down || e.Key == Key.S) | ||
| 307 : | { | ||
| 308 : | //walk back | ||
| 309 : | AvatarWalk(0.1f); | ||
| 310 : | e.Handled = true; | ||
| 311 : | } | ||
| 312 : | albert | 81 | }*/ |
| 313 : | albert | 31 | |
| 314 : | albert | 81 | ////camera walk should be setting according to the avatar's walking path |
| 315 : | //private void AvatarWalk(float distanceTick) | ||
| 316 : | //{ | ||
| 317 : | // avatarManager.Walk(distanceTick); | ||
| 318 : | albert | 74 | |
| 319 : | albert | 81 | // Point3D npos; |
| 320 : | // DefaultUtil.TryGetPoint3D(avatarManager.Myself.Position , out npos); | ||
| 321 : | albert | 76 | |
| 322 : | albert | 81 | // ResetCameraPos(npos); |
| 323 : | albert | 74 | |
| 324 : | albert | 77 | |
| 325 : | albert | 81 | // //_camera.Position = new Point3D(newpos.X + 5, newpos.Y + 5, newpos.Z + 5);// pos; |
| 326 : | //} | ||
| 327 : | albert | 31 | |
| 328 : | albert | 75 | void ResetCameraPos(Point3D avatarpos) |
| 329 : | { | ||
| 330 : | _camera.Position = Point3D.Add(avatarpos, new Vector3D(8, 8, 8)); | ||
| 331 : | albert | 77 | |
| 332 : | ResetCameraLookDirection(avatarpos); | ||
| 333 : | albert | 75 | } |
| 334 : | |||
| 335 : | albert | 77 | void ResetCameraLookDirection(Point3D npos) |
| 336 : | albert | 75 | { |
| 337 : | //TODO: | ||
| 338 : | //_camera.LookDirection = avatarLookDir; | ||
| 339 : | albert | 77 | Vector3D cameralookdir = npos - _camera.Position; |
| 340 : | albert | 81 | cameralookdir.Z += 8; |
| 341 : | albert | 77 | _camera.LookDirection = cameralookdir; |
| 342 : | albert | 75 | } |
| 343 : | |||
| 344 : | albert | 81 | void ResetCameraLookDirection(Vector3D avatarlookdir) |
| 345 : | albert | 31 | { |
| 346 : | albert | 81 | //TODO: |
| 347 : | //_camera.LookDirection = avatarLookDir; | ||
| 348 : | Vector3D cameralookdir = avatarlookdir; | ||
| 349 : | cameralookdir.Z += 8; | ||
| 350 : | _camera.LookDirection = cameralookdir; | ||
| 351 : | } | ||
| 352 : | |||
| 353 : | private void CameraLookLeftRight(Point3D rotatecenter, double angle) | ||
| 354 : | { | ||
| 355 : | albert | 31 | Vector3D axis = new Vector3D(0, 0, 1); |
| 356 : | Quaternion delta = new Quaternion(axis, -angle); | ||
| 357 : | albert | 81 | Matrix3D avatarMatrix3D = new Matrix3D(); |
| 358 : | avatarMatrix3D.RotateAt(delta, rotatecenter); | ||
| 359 : | albert | 31 | |
| 360 : | albert | 81 | //avatarManager.MySelfLookDirection = Vector3D.Multiply(avatarManager.MySelfLookDirection, avatarMatrix3D); |
| 361 : | _camera.LookDirection = Vector3D.Add(avatarManager.MySelfLookDirection,new Vector3D(0,0,-2));// avatarMatrix3D);// Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 362 : | albert | 31 | |
| 363 : | } | ||
| 364 : | |||
| 365 : | albert | 81 | //void _eventSource_MouseRightButtonDown(object sender, MouseButtonEventArgs e) |
| 366 : | //{ | ||
| 367 : | // Mouse.Capture(_eventSource, CaptureMode.Element); | ||
| 368 : | albert | 16 | |
| 369 : | albert | 81 | // Point pclick = e.GetPosition(_eventSource); |
| 370 : | // originalMouseClickPos = _eventSource.PointToScreen(pclick); | ||
| 371 : | albert | 31 | |
| 372 : | albert | 81 | // HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
| 373 : | // RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; | ||
| 374 : | // if (result3d == null) | ||
| 375 : | // return; | ||
| 376 : | albert | 22 | |
| 377 : | albert | 81 | // visul3d = result3d.VisualHit as ModelVisual3D; |
| 378 : | // if (visul3d == null) | ||
| 379 : | // return; | ||
| 380 : | albert | 22 | |
| 381 : | albert | 81 | // //for ALT + MOUSE + ROTATE |
| 382 : | // VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); | ||
| 383 : | albert | 22 | |
| 384 : | albert | 81 | // _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 385 : | albert | 22 | |
| 386 : | albert | 81 | // isTracking = true; |
| 387 : | // _eventSource.CaptureMouse(); | ||
| 388 : | // _eventSource.Focus(); | ||
| 389 : | // e.Handled = true; | ||
| 390 : | //} | ||
| 391 : | albert | 16 | |
| 392 : | albert | 24 | Point originalMouseClickPos = new Point(); |
| 393 : | albert | 16 | |
| 394 : | albert | 81 | //void _eventSource_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
| 395 : | //{ | ||
| 396 : | // Mouse.Capture(EventSource, CaptureMode.Element); | ||
| 397 : | albert | 24 | |
| 398 : | albert | 81 | // Point pclick = e.GetPosition(EventSource); |
| 399 : | // originalMouseClickPos = _eventSource.PointToScreen(pclick); | ||
| 400 : | albert | 16 | |
| 401 : | albert | 81 | // HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
| 402 : | // RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; | ||
| 403 : | // if (result3d == null) | ||
| 404 : | // return; | ||
| 405 : | albert | 16 | |
| 406 : | albert | 81 | // visul3d = result3d.VisualHit as ModelVisual3D; |
| 407 : | // if (visul3d == null) | ||
| 408 : | // return; | ||
| 409 : | albert | 16 | |
| 410 : | albert | 81 | // //for ALT + MOUSE + ROTATE |
| 411 : | // VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); | ||
| 412 : | albert | 16 | |
| 413 : | albert | 81 | // _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 414 : | albert | 17 | |
| 415 : | albert | 81 | // _clickedCenterPosition = GetObjectCenterPosition(visul3d); |
| 416 : | albert | 48 | |
| 417 : | albert | 81 | // //uint? key = DefaultNetwork.DefaultNetwork.m_primitiveData.GetKeyInSceneByValue(visul3d); |
| 418 : | // //if (key.HasValue) | ||
| 419 : | // //{ | ||
| 420 : | // // OpenMetaverse.Primitive dataaaa =DefaultNetwork.DefaultNetwork.m_primitiveData.PrimModelsInScene[key.Value]; | ||
| 421 : | // // uint id = dataaaa.ParentID; | ||
| 422 : | // // OpenMetaverse.Vector3 parentpos = DefaultNetwork.DefaultNetwork.m_primitiveData.GetModelsParentPosition(id); | ||
| 423 : | // // OpenMetaverse.Vector3 mypos = dataaaa.Position; | ||
| 424 : | |||
| 425 : | // //} | ||
| 426 : | // //TRACK BALL | ||
| 427 : | // _previousPosition3D = ProjectToTrackball( | ||
| 428 : | // EventSource.ActualWidth, | ||
| 429 : | // EventSource.ActualHeight, | ||
| 430 : | // _previousPosition2D); | ||
| 431 : | albert | 31 | |
| 432 : | albert | 81 | // isTracking = true; |
| 433 : | // _eventSource.CaptureMouse(); | ||
| 434 : | // _eventSource.Focus(); | ||
| 435 : | // e.Handled = true; | ||
| 436 : | //} | ||
| 437 : | albert | 16 | |
| 438 : | albert | 29 | private Point3D GetObjectCenterPosition(ModelVisual3D visual) |
| 439 : | { | ||
| 440 : | return Point3D.Add(visual.Content.Bounds.Location, | ||
| 441 : | new Vector3D(visual.Content.Bounds.SizeX / 2, visual.Content.Bounds.SizeY / 2, visual.Content.Bounds.SizeZ / 2)); | ||
| 442 : | } | ||
| 443 : | |||
| 444 : | albert | 81 | //private void OnMouseUp(object sender, MouseEventArgs e) |
| 445 : | //{ | ||
| 446 : | // //set the mouse to the original position when clicked the object. | ||
| 447 : | // if (_eventSource.Cursor != Cursors.Arrow) | ||
| 448 : | // { | ||
| 449 : | // SetCursorPos((int)originalMouseClickPos.X, (int)originalMouseClickPos.Y); | ||
| 450 : | // _eventSource.Cursor = Cursors.Arrow; | ||
| 451 : | // } | ||
| 452 : | // Mouse.Capture(EventSource, CaptureMode.None); | ||
| 453 : | // isTracking = false; | ||
| 454 : | // if (isPanningObject == true) | ||
| 455 : | // { | ||
| 456 : | // isPanningObject = false; | ||
| 457 : | // _cameraLookPoint = GetObjectCenterPosition(visul3d); | ||
| 458 : | // } | ||
| 459 : | albert | 76 | |
| 460 : | albert | 81 | // _eventSource.ReleaseMouseCapture(); |
| 461 : | //} | ||
| 462 : | albert | 26 | |
| 463 : | Point currentPosition; | ||
| 464 : | |||
| 465 : | albert | 16 | private void OnMouseMove(object sender, MouseEventArgs e) |
| 466 : | { | ||
| 467 : | albert | 26 | currentPosition = e.GetPosition(EventSource); |
| 468 : | albert | 16 | |
| 469 : | if (e.RightButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) | ||
| 470 : | { | ||
| 471 : | LookUpDown(currentPosition); | ||
| 472 : | } | ||
| 473 : | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))) | ||
| 474 : | { | ||
| 475 : | if (isTracking) | ||
| 476 : | { | ||
| 477 : | _eventSource.Cursor = Cursors.None; | ||
| 478 : | Focus(currentPosition); | ||
| 479 : | } | ||
| 480 : | } | ||
| 481 : | albert | 17 | else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.A)) |
| 482 : | { | ||
| 483 : | albert | 22 | if (isTracking) |
| 484 : | { | ||
| 485 : | _eventSource.Cursor = Cursors.None; | ||
| 486 : | CameraPanLeftRight(currentPosition); | ||
| 487 : | } | ||
| 488 : | albert | 17 | } |
| 489 : | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) | ||
| 490 : | && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) | ||
| 491 : | { | ||
| 492 : | if (isTracking) | ||
| 493 : | albert | 20 | { |
| 494 : | _eventSource.Cursor = Cursors.None; | ||
| 495 : | albert | 23 | |
| 496 : | albert | 17 | RotateObject(currentPosition); |
| 497 : | albert | 20 | } |
| 498 : | albert | 17 | } |
| 499 : | albert | 16 | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) |
| 500 : | { | ||
| 501 : | albert | 20 | if (isTracking) |
| 502 : | { | ||
| 503 : | _eventSource.Cursor = Cursors.None; | ||
| 504 : | albert | 29 | isPanningObject = true; |
| 505 : | albert | 20 | PanObject(currentPosition); |
| 506 : | } | ||
| 507 : | albert | 16 | } |
| 508 : | albert | 22 | else if (e.LeftButton == MouseButtonState.Pressed) |
| 509 : | { | ||
| 510 : | albert | 31 | if(isTracking) |
| 511 : | LookAround(currentPosition); | ||
| 512 : | albert | 22 | } |
| 513 : | albert | 31 | else if (e.RightButton == MouseButtonState.Pressed) |
| 514 : | { | ||
| 515 : | if (isTracking) | ||
| 516 : | CameraPanUP(currentPosition); | ||
| 517 : | } | ||
| 518 : | albert | 24 | |
| 519 : | albert | 16 | _previousPosition2D = currentPosition; |
| 520 : | } | ||
| 521 : | |||
| 522 : | albert | 30 | private void LookAround(Point currentPosition) |
| 523 : | albert | 17 | { |
| 524 : | albert | 29 | |
| 525 : | albert | 30 | if (_cameraLookPoint == new Point3D(0, 0, 0)) |
| 526 : | return; | ||
| 527 : | |||
| 528 : | albert | 19 | Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); |
| 529 : | albert | 30 | |
| 530 : | if (_previousPosition3D == currentPosition3D) | ||
| 531 : | return; | ||
| 532 : | |||
| 533 : | albert | 17 | Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); |
| 534 : | double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); | ||
| 535 : | albert | 22 | |
| 536 : | albert | 30 | angle *= 2; |
| 537 : | |||
| 538 : | albert | 22 | // We negate the angle because we are rotating the camera. |
| 539 : | // Do not do this if you are rotating the scene instead. | ||
| 540 : | albert | 17 | Quaternion delta = new Quaternion(axis, -angle); |
| 541 : | |||
| 542 : | albert | 29 | Matrix3D cameraMatrix3D = new Matrix3D(); |
| 543 : | cameraMatrix3D.RotateAt(delta,_cameraLookPoint); | ||
| 544 : | |||
| 545 : | _camera.Position = Point3D.Multiply(_camera.Position, cameraMatrix3D); | ||
| 546 : | _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 547 : | |||
| 548 : | albert | 17 | _previousPosition3D = currentPosition3D; |
| 549 : | } | ||
| 550 : | |||
| 551 : | albert | 81 | //void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) |
| 552 : | //{ | ||
| 553 : | // double scale = -e.Delta / 800.000; | ||
| 554 : | // if(_cameraLookPoint!=null&&_cameraLookPoint!=new Point3D(0,0,0)) | ||
| 555 : | // _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 556 : | albert | 23 | |
| 557 : | albert | 81 | // int count = Math.Abs((int)(scale / zoomFactor)); |
| 558 : | // for (int i = 1; i <= count; i++) | ||
| 559 : | // { | ||
| 560 : | // if (scale > 0) | ||
| 561 : | // AdjustCameraPosition(_camera, _camera.LookDirection, zoomFactor); | ||
| 562 : | // else | ||
| 563 : | // AdjustCameraPosition(_camera, _camera.LookDirection, -zoomFactor); | ||
| 564 : | // } | ||
| 565 : | //} | ||
| 566 : | albert | 22 | |
| 567 : | private void RotateObject(Point currentPosition) | ||
| 568 : | { | ||
| 569 : | albert | 65 | |
| 570 : | albert | 30 | Vector3D currentPosition3D = ProjectToTrackball(_eventSource.ActualWidth, _eventSource.ActualHeight, currentPosition); |
| 571 : | albert | 22 | Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); |
| 572 : | albert | 65 | double angle = Vector3D.AngleBetween(currentPosition3D, _previousPosition3D); |
| 573 : | albert | 22 | |
| 574 : | albert | 35 | angle *= 3.00000; |
| 575 : | albert | 30 | |
| 576 : | albert | 29 | Matrix3D mat = new Matrix3D(); |
| 577 : | mat.RotateAt(new Quaternion(axis, angle), _clickedCenterPosition); | ||
| 578 : | albert | 65 | |
| 579 : | RotateObject(mat, visul3d); | ||
| 580 : | |||
| 581 : | albert | 64 | IEnumerator<Visual3D> children = m_primitiveData.GetChildrenByVisual3D(visul3d); |
| 582 : | albert | 22 | |
| 583 : | albert | 65 | while (children.MoveNext()) |
| 584 : | albert | 34 | { |
| 585 : | albert | 64 | RotateObject(mat, children.Current); |
| 586 : | albert | 34 | } |
| 587 : | |||
| 588 : | _previousPosition3D = currentPosition3D; | ||
| 589 : | |||
| 590 : | albert | 65 | } |
| 591 : | |||
| 592 : | albert | 60 | private void RotateObject(Matrix3D mat,Visual3D visual) |
| 593 : | albert | 34 | { |
| 594 : | GeometryModel3D geometryModel3D = (visual as ModelVisual3D).Content as GeometryModel3D; | ||
| 595 : | |||
| 596 : | albert | 29 | if (geometryModel3D != null) |
| 597 : | { | ||
| 598 : | MeshGeometry3D meshGeometry3D = geometryModel3D.Geometry as MeshGeometry3D; | ||
| 599 : | if (meshGeometry3D != null) | ||
| 600 : | { | ||
| 601 : | Point3DCollection points = meshGeometry3D.Positions; | ||
| 602 : | albert | 28 | |
| 603 : | albert | 29 | for (int i = 0; i < points.Count; i++) |
| 604 : | points[i] = Point3D.Multiply(points[i], mat); | ||
| 605 : | } | ||
| 606 : | } | ||
| 607 : | albert | 28 | |
| 608 : | albert | 34 | } |
| 609 : | albert | 22 | |
| 610 : | albert | 17 | private Vector3D ProjectToTrackball(double width, double height, Point point) |
| 611 : | { | ||
| 612 : | double x = point.X / (width / 2); // Scale so bounds map to [0,0] - [2,2] | ||
| 613 : | double y = point.Y / (height / 2); | ||
| 614 : | |||
| 615 : | x = x - 1; // Translate 0,0 to the center | ||
| 616 : | y = 1 - y; // Flip so +Y is up instead of down | ||
| 617 : | |||
| 618 : | double z2 = 1 - x * x - y * y; // z^2 = 1 - x^2 - y^2 | ||
| 619 : | double z = z2 > 0 ? Math.Sqrt(z2) : 0; | ||
| 620 : | |||
| 621 : | albert | 22 | Vector3D v = new Vector3D(x, z, y); |
| 622 : | albert | 23 | v.Normalize(); |
| 623 : | albert | 22 | return v; |
| 624 : | albert | 17 | } |
| 625 : | |||
| 626 : | #endregion Event Handling | ||
| 627 : | |||
| 628 : | albert | 20 | |
| 629 : | albert | 22 | private void CameraPanLeftRight(Point currentPosition) |
| 630 : | albert | 17 | { |
| 631 : | albert | 22 | double scalex = currentPosition.X - _previousPosition2D.X; |
| 632 : | |||
| 633 : | Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); | ||
| 634 : | albert | 65 | Point3D cPos = _camera.Position; |
| 635 : | albert | 17 | |
| 636 : | albert | 65 | cPos += Math.Abs(scalex) * vector3D; |
| 637 : | |||
| 638 : | albert | 77 | if (OnCameraPositionChanged != null) |
| 639 : | OnCameraPositionChanged(_camera.Position, cPos); | ||
| 640 : | albert | 65 | |
| 641 : | _camera.Position = cPos; | ||
| 642 : | albert | 17 | } |
| 643 : | |||
| 644 : | albert | 16 | private void PanObject(Point currentPosition) |
| 645 : | { | ||
| 646 : | albert | 64 | IEnumerator<Visual3D> children = m_primitiveData.GetChildrenByVisual3D(visul3d); |
| 647 : | albert | 33 | |
| 648 : | albert | 21 | Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); |
| 649 : | albert | 34 | MoveObject(visul3d, vector3D); |
| 650 : | |||
| 651 : | albert | 64 | while( children.MoveNext()) |
| 652 : | albert | 34 | { |
| 653 : | albert | 64 | MoveObject(children.Current, vector3D); |
| 654 : | albert | 34 | } |
| 655 : | albert | 33 | } |
| 656 : | |||
| 657 : | albert | 34 | private void MoveObject(Visual3D visual, Vector3D vector3D) |
| 658 : | albert | 33 | { |
| 659 : | |||
| 660 : | albert | 34 | GeometryModel3D geometryModel3D = (visual as ModelVisual3D).Content as GeometryModel3D; |
| 661 : | albert | 29 | if (geometryModel3D != null) |
| 662 : | { | ||
| 663 : | MeshGeometry3D meshGeometry3D = geometryModel3D.Geometry as MeshGeometry3D; | ||
| 664 : | if (meshGeometry3D != null) | ||
| 665 : | { | ||
| 666 : | Point3DCollection points = meshGeometry3D.Positions; | ||
| 667 : | albert | 21 | |
| 668 : | albert | 29 | for (int i = 0; i < points.Count; i++) |
| 669 : | albert | 30 | points[i] = Point3D.Add(points[i], vector3D * 50); |
| 670 : | albert | 29 | } |
| 671 : | } | ||
| 672 : | albert | 16 | } |
| 673 : | albert | 30 | |
| 674 : | albert | 28 | string mm = ""; |
| 675 : | albert | 21 | private Vector3D GetTranslationVector3D(DependencyObject modelHit, Point startPosition, Point endPosition) |
| 676 : | { | ||
| 677 : | Vector3D translationVector3D = new Vector3D(); | ||
| 678 : | albert | 16 | |
| 679 : | albert | 21 | Viewport3DVisual viewport = null; |
| 680 : | bool success = false; | ||
| 681 : | albert | 19 | |
| 682 : | albert | 21 | Matrix3D matrix3D = MathUtils.TryTransformTo2DAncestor(modelHit, out viewport, out success); |
| 683 : | if (success && matrix3D.HasInverse) | ||
| 684 : | { | ||
| 685 : | matrix3D.Invert(); | ||
| 686 : | Point3D startPoint3D = new Point3D(startPosition.X, startPosition.Y, 0); | ||
| 687 : | Point3D endPoint3D = new Point3D(endPosition.X, endPosition.Y, 0); | ||
| 688 : | Vector3D vector3D = endPoint3D - startPoint3D; | ||
| 689 : | translationVector3D = matrix3D.Transform(vector3D); | ||
| 690 : | } | ||
| 691 : | |||
| 692 : | return translationVector3D; | ||
| 693 : | } | ||
| 694 : | |||
| 695 : | |||
| 696 : | |||
| 697 : | albert | 16 | private void LookUpDown(Point currentPosition) |
| 698 : | { | ||
| 699 : | double scale = currentPosition.Y- _previousPosition2D.Y; | ||
| 700 : | albert | 29 | // scale = scale; |
| 701 : | albert | 16 | _camera.LookDirection = new Vector3D(_camera.LookDirection.X, _camera.LookDirection.Y, _camera.LookDirection.Z + scale); |
| 702 : | } | ||
| 703 : | |||
| 704 : | albert | 30 | double zoomFactor = 0.0500; |
| 705 : | albert | 16 | private void Focus(Point currentPosition) |
| 706 : | { | ||
| 707 : | albert | 23 | //zoom |
| 708 : | albert | 16 | double yDelta = currentPosition.Y - _previousPosition2D.Y; |
| 709 : | albert | 30 | double scale = yDelta/20 ; |
| 710 : | int count = Math.Abs( (int)(scale / zoomFactor)); | ||
| 711 : | for (int i = 1; i <= count; i++) | ||
| 712 : | { | ||
| 713 : | if (yDelta > 0) | ||
| 714 : | AdjustCameraPosition(_camera, _camera.LookDirection, zoomFactor); | ||
| 715 : | else | ||
| 716 : | AdjustCameraPosition(_camera, _camera.LookDirection, -zoomFactor); | ||
| 717 : | albert | 66 | } |
| 718 : | albert | 23 | |
| 719 : | albert | 29 | //rotate the camera |
| 720 : | albert | 32 | double axisAngle = _previousPosition2D.X - currentPosition.X; |
| 721 : | albert | 29 | Matrix3D matrixRoateCamera = new Matrix3D(); |
| 722 : | matrixRoateCamera.RotateAt(new Quaternion(new Vector3D(0, 0, 1), axisAngle), _cameraLookPoint); | ||
| 723 : | |||
| 724 : | albert | 65 | Point3D pos = Point3D.Multiply(_camera.Position, matrixRoateCamera); |
| 725 : | albert | 77 | if (OnCameraPositionChanged != null) |
| 726 : | OnCameraPositionChanged(_camera.Position, pos); | ||
| 727 : | albert | 65 | _camera.Position = pos; |
| 728 : | |||
| 729 : | albert | 29 | _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 730 : | albert | 24 | } |
| 731 : | albert | 16 | |
| 732 : | albert | 30 | private void AdjustCameraPosition(PerspectiveCamera camera,Vector3D lookDirection, double factor ) |
| 733 : | albert | 24 | { |
| 734 : | albert | 30 | Point3D cameraPos = new Point3D(camera.Position.X - factor * lookDirection.X, |
| 735 : | camera.Position.Y - factor * lookDirection.Y, | ||
| 736 : | camera.Position.Z - factor * lookDirection.Z); | ||
| 737 : | albert | 16 | |
| 738 : | albert | 24 | //too fast |
| 739 : | albert | 30 | //if (Point3D.Subtract(cameraPos, camera.Position).Length > 0.5000000000) |
| 740 : | // cameraPos = new Point3D(camera.Position.X - factor / 10 * lookDirection.X, | ||
| 741 : | // camera.Position.Y - factor / 10 * lookDirection.Y, | ||
| 742 : | // camera.Position.Z - factor / 10 * lookDirection.Z); | ||
| 743 : | albert | 24 | |
| 744 : | double distance = Point3D.Subtract(_cameraLookPoint, cameraPos).Length; | ||
| 745 : | |||
| 746 : | albert | 30 | if (distance > 512.0000 || distance <= 0.5000000) |
| 747 : | albert | 24 | { |
| 748 : | } | ||
| 749 : | else | ||
| 750 : | albert | 65 | { |
| 751 : | albert | 77 | if (OnCameraPositionChanged != null) |
| 752 : | OnCameraPositionChanged(camera.Position, cameraPos); | ||
| 753 : | albert | 65 | |
| 754 : | albert | 24 | camera.Position = cameraPos; |
| 755 : | albert | 65 | } |
| 756 : | |||
| 757 : | albert | 16 | } |
| 758 : | albert | 24 | //get the clicked 3d position |
| 759 : | albert | 16 | public HitTestResultBehavior HitTestCallback(HitTestResult result) |
| 760 : | { | ||
| 761 : | if (result.VisualHit != null) | ||
| 762 : | { | ||
| 763 : | albert | 23 | RayHitTestResult r = result as RayHitTestResult; |
| 764 : | if (r != null) | ||
| 765 : | { | ||
| 766 : | RayMeshGeometry3DHitTestResult t = r as RayMeshGeometry3DHitTestResult; | ||
| 767 : | _cameraLookPoint = t.PointHit; | ||
| 768 : | albert | 24 | return HitTestResultBehavior.Stop; |
| 769 : | albert | 23 | } |
| 770 : | albert | 16 | } |
| 771 : | return HitTestResultBehavior.Continue; | ||
| 772 : | } | ||
| 773 : | |||
| 774 : | private void CameraPanUP(Point currentPosition) | ||
| 775 : | { | ||
| 776 : | double yDelta = currentPosition.Y - _previousPosition2D.Y; | ||
| 777 : | double xDelta = currentPosition.X - _previousPosition2D.X; | ||
| 778 : | albert | 20 | double scale = yDelta / 5.00; |
| 779 : | albert | 16 | |
| 780 : | _camera.Position = new Point3D(_camera.Position.X, _camera.Position.Y, _camera.Position.Z + scale); | ||
| 781 : | } | ||
| 782 : | |||
| 783 : | albert | 64 | |
| 784 : | albert | 16 | } |
| 785 : | |||
| 786 : | |||
| 787 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

