Annotation of /trunk/DefaultRenderer/CameraControl.cs
Parent Directory
|
Revision Log
Revision 16 - (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 : | using System; | ||
| 15 : | using System.Windows; | ||
| 16 : | using System.Windows.Input; | ||
| 17 : | using System.Windows.Media; | ||
| 18 : | using System.Windows.Media.Media3D; | ||
| 19 : | using System.Windows.Controls; | ||
| 20 : | using Petzold.Media3D; | ||
| 21 : | |||
| 22 : | |||
| 23 : | namespace Xenki.DefaultRenderer | ||
| 24 : | { | ||
| 25 : | public class CameraControl | ||
| 26 : | { | ||
| 27 : | private FrameworkElement _eventSource; | ||
| 28 : | private Point _previousPosition2D; | ||
| 29 : | private Vector3D _previousPosition3D = new Vector3D(0, 0, 1); | ||
| 30 : | |||
| 31 : | private Transform3DGroup _transform; | ||
| 32 : | private ScaleTransform3D _scale = new ScaleTransform3D(); | ||
| 33 : | private AxisAngleRotation3D _rotation = new AxisAngleRotation3D(); | ||
| 34 : | private TranslateTransform3D _translate = new TranslateTransform3D(); | ||
| 35 : | |||
| 36 : | private Vector3D _cameraLookDirection = new Vector3D(128, 128, 10); | ||
| 37 : | |||
| 38 : | private PerspectiveCamera _camera; | ||
| 39 : | private Point3D _cameraLookPoint; | ||
| 40 : | private RotateTransform3D _rotationTransform = new RotateTransform3D(); | ||
| 41 : | ModelVisual3D clickedVisual; | ||
| 42 : | private TranslateTransform3D _clickedTranslate = new TranslateTransform3D(); | ||
| 43 : | |||
| 44 : | bool isTracking = false; | ||
| 45 : | |||
| 46 : | public CameraControl(PerspectiveCamera camera) | ||
| 47 : | { | ||
| 48 : | _camera = camera; | ||
| 49 : | _rotationTransform.Rotation = _rotation; | ||
| 50 : | _transform = new Transform3DGroup(); | ||
| 51 : | |||
| 52 : | //_transform.Children.Add(_translate); | ||
| 53 : | _transform.Children.Add(_rotationTransform); | ||
| 54 : | // _transform.Children.Add(_scale); | ||
| 55 : | |||
| 56 : | } | ||
| 57 : | |||
| 58 : | /// <summary> | ||
| 59 : | /// A transform to move the camera or scene to the trackball's | ||
| 60 : | /// current orientation and scale. | ||
| 61 : | /// </summary> | ||
| 62 : | public Transform3DGroup Transform | ||
| 63 : | { | ||
| 64 : | get { return _transform; } | ||
| 65 : | } | ||
| 66 : | |||
| 67 : | #region Event Handling | ||
| 68 : | |||
| 69 : | /// <summary> | ||
| 70 : | /// The FrameworkElement we listen to for mouse events. | ||
| 71 : | /// </summary> | ||
| 72 : | public FrameworkElement EventSource | ||
| 73 : | { | ||
| 74 : | get { return _eventSource; } | ||
| 75 : | |||
| 76 : | set | ||
| 77 : | { | ||
| 78 : | if (_eventSource != null) | ||
| 79 : | { | ||
| 80 : | _eventSource.MouseWheel -= _eventSource_MouseWheel; | ||
| 81 : | _eventSource.MouseUp -= this.OnMouseUp; | ||
| 82 : | _eventSource.MouseMove -= this.OnMouseMove; | ||
| 83 : | _eventSource.MouseLeftButtonDown -= _eventSource_MouseLeftButtonDown; | ||
| 84 : | } | ||
| 85 : | _eventSource = value; | ||
| 86 : | _eventSource.MouseWheel += _eventSource_MouseWheel; | ||
| 87 : | _eventSource.MouseLeftButtonDown += new MouseButtonEventHandler(_eventSource_MouseLeftButtonDown); | ||
| 88 : | _eventSource.MouseUp += this.OnMouseUp; | ||
| 89 : | _eventSource.MouseMove += this.OnMouseMove; | ||
| 90 : | |||
| 91 : | } | ||
| 92 : | } | ||
| 93 : | |||
| 94 : | void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) | ||
| 95 : | { | ||
| 96 : | double scale = e.Delta / 1200.00; | ||
| 97 : | double distance = Point3D.Subtract(_cameraLookPoint, _camera.Position).Length; | ||
| 98 : | |||
| 99 : | if (distance > 1024 || distance <= 0.5) | ||
| 100 : | { | ||
| 101 : | if (touch == false) | ||
| 102 : | { | ||
| 103 : | touch = true; | ||
| 104 : | } | ||
| 105 : | } | ||
| 106 : | else | ||
| 107 : | { | ||
| 108 : | touch = false; | ||
| 109 : | } | ||
| 110 : | |||
| 111 : | if (touch == true) | ||
| 112 : | scale = -scale; | ||
| 113 : | _camera.Position = new Point3D(_camera.Position.X - scale * _cameraLookDirection.X, _camera.Position.Y - scale * _cameraLookDirection.Y, _camera.Position.Z - scale * _cameraLookDirection.Z); | ||
| 114 : | _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 115 : | } | ||
| 116 : | |||
| 117 : | void _eventSource_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) | ||
| 118 : | { | ||
| 119 : | Mouse.Capture(EventSource, CaptureMode.Element); | ||
| 120 : | |||
| 121 : | Point pclick = e.GetPosition(EventSource); | ||
| 122 : | HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); | ||
| 123 : | RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; | ||
| 124 : | if (result3d == null) | ||
| 125 : | return; | ||
| 126 : | |||
| 127 : | ModelVisual3D visul3d = result3d.VisualHit as ModelVisual3D; | ||
| 128 : | if (visul3d == null) | ||
| 129 : | return; | ||
| 130 : | |||
| 131 : | _clickedTranslate = visul3d.Transform as TranslateTransform3D; | ||
| 132 : | if (_clickedTranslate == null) | ||
| 133 : | return; | ||
| 134 : | |||
| 135 : | clickedVisual = visul3d; | ||
| 136 : | clickedVisual.Transform = _clickedTranslate; | ||
| 137 : | |||
| 138 : | Petzold.Media3D.LineRange line; | ||
| 139 : | Petzold.Media3D.ViewportInfo.Point2DtoPoint3D(_eventSource as Viewport3D, pclick, out line); | ||
| 140 : | _cameraLookPoint = line.PointFromY(visul3d.Content.Bounds.Y); | ||
| 141 : | |||
| 142 : | _rotationTransform.CenterX = _cameraLookPoint.X; | ||
| 143 : | _rotationTransform.CenterY = _cameraLookPoint.Y; | ||
| 144 : | _rotationTransform.CenterZ = _cameraLookPoint.Z; | ||
| 145 : | |||
| 146 : | _cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 147 : | _camera.LookDirection = _cameraLookDirection; | ||
| 148 : | |||
| 149 : | isTracking = true; | ||
| 150 : | _eventSource.CaptureMouse(); | ||
| 151 : | _eventSource.Focus(); | ||
| 152 : | e.Handled = true; | ||
| 153 : | } | ||
| 154 : | |||
| 155 : | private void OnMouseUp(object sender, MouseEventArgs e) | ||
| 156 : | { | ||
| 157 : | Mouse.Capture(EventSource, CaptureMode.None); | ||
| 158 : | isTracking = false; | ||
| 159 : | |||
| 160 : | _eventSource.ReleaseMouseCapture(); | ||
| 161 : | _eventSource.Cursor = Cursors.Arrow; | ||
| 162 : | string mmm = mm; | ||
| 163 : | } | ||
| 164 : | |||
| 165 : | private void OnMouseMove(object sender, MouseEventArgs e) | ||
| 166 : | { | ||
| 167 : | Point currentPosition = e.GetPosition(EventSource); | ||
| 168 : | |||
| 169 : | if (e.RightButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) | ||
| 170 : | { | ||
| 171 : | LookUpDown(currentPosition); | ||
| 172 : | } | ||
| 173 : | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))) | ||
| 174 : | { | ||
| 175 : | if (isTracking) | ||
| 176 : | { | ||
| 177 : | _eventSource.Cursor = Cursors.None; | ||
| 178 : | Focus(currentPosition); | ||
| 179 : | } | ||
| 180 : | } | ||
| 181 : | else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.Q)) | ||
| 182 : | { | ||
| 183 : | CameraPanUP(currentPosition); | ||
| 184 : | } | ||
| 185 : | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) | ||
| 186 : | { | ||
| 187 : | PanObject(currentPosition); | ||
| 188 : | } | ||
| 189 : | |||
| 190 : | _previousPosition2D = currentPosition; | ||
| 191 : | } | ||
| 192 : | |||
| 193 : | private void PanObject(Point currentPosition) | ||
| 194 : | { | ||
| 195 : | double scale = currentPosition.Y - _previousPosition2D.Y; | ||
| 196 : | scale = scale / 5; | ||
| 197 : | |||
| 198 : | |||
| 199 : | LineRange range; | ||
| 200 : | ViewportInfo.Point2DtoPoint3D(_eventSource as Viewport3D, currentPosition, out range); | ||
| 201 : | Point3D pointNew = range.PointFromZ(clickedVisual.Content.Bounds.X); | ||
| 202 : | //Vector3D v = pointNew- new Point3D(clickedVisual.Content.Bounds.X,clickedVisual.Content.Bounds.Y,clickedVisual.Content.Bounds.Z); | ||
| 203 : | |||
| 204 : | Vector3D vectMouse = pointNew - _cameraLookPoint; | ||
| 205 : | _clickedTranslate.OffsetX = _clickedTranslate.OffsetX + vectMouse.X; | ||
| 206 : | _clickedTranslate.OffsetY = _clickedTranslate.OffsetY + vectMouse.Y; | ||
| 207 : | _clickedTranslate.OffsetZ = _clickedTranslate.OffsetZ + vectMouse.Z; | ||
| 208 : | |||
| 209 : | //_camera.LookDirection = new Vector3D(_camera.LookDirection.X, _camera.LookDirection.Y, _camera.LookDirection.Z + scale); | ||
| 210 : | |||
| 211 : | } | ||
| 212 : | |||
| 213 : | #endregion Event Handling | ||
| 214 : | |||
| 215 : | private void LookUpDown(Point currentPosition) | ||
| 216 : | { | ||
| 217 : | double scale = currentPosition.Y- _previousPosition2D.Y; | ||
| 218 : | scale = scale/5; | ||
| 219 : | _camera.LookDirection = new Vector3D(_camera.LookDirection.X, _camera.LookDirection.Y, _camera.LookDirection.Z + scale); | ||
| 220 : | } | ||
| 221 : | |||
| 222 : | string mm = ""; | ||
| 223 : | bool touch = false; | ||
| 224 : | private void Focus(Point currentPosition) | ||
| 225 : | { | ||
| 226 : | double yDelta = currentPosition.Y - _previousPosition2D.Y; | ||
| 227 : | double scale = yDelta / 100; | ||
| 228 : | //Maybe this part need replaced by collision detection method | ||
| 229 : | double distance = Point3D.Subtract(_cameraLookPoint, _camera.Position).Length; | ||
| 230 : | if (distance > 1024 || distance <= 0.5) | ||
| 231 : | { | ||
| 232 : | if (touch == false) | ||
| 233 : | touch = true; | ||
| 234 : | } | ||
| 235 : | else | ||
| 236 : | { | ||
| 237 : | touch = false; | ||
| 238 : | } | ||
| 239 : | if (touch == true) | ||
| 240 : | scale = -scale; | ||
| 241 : | |||
| 242 : | _camera.Position = new Point3D(_camera.Position.X - scale * _cameraLookDirection.X, _camera.Position.Y - scale * _cameraLookDirection.Y, _camera.Position.Z - scale * _cameraLookDirection.Z); | ||
| 243 : | |||
| 244 : | //_translate.OffsetX -= scale * _cameraLookDirection.X;// _camera.LookDirection.X; | ||
| 245 : | //_translate.OffsetY -= scale * _cameraLookDirection.Y;//_camera.LookDirection.Y; | ||
| 246 : | //_translate.OffsetZ -= scale * _cameraLookDirection.Z;//_camera.LookDirection.Z; | ||
| 247 : | |||
| 248 : | _camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); | ||
| 249 : | |||
| 250 : | double axisAngle = currentPosition.X - _previousPosition2D.X; | ||
| 251 : | double angletimes = axisAngle; | ||
| 252 : | |||
| 253 : | if (angletimes > 360 || angletimes < -360) | ||
| 254 : | angletimes = 0; | ||
| 255 : | |||
| 256 : | if (angletimes + _rotation.Angle >= 360) | ||
| 257 : | _rotation.Angle = 0; | ||
| 258 : | _rotation.Axis = new Vector3D(0, 0, 1); | ||
| 259 : | _rotation.Angle += axisAngle; | ||
| 260 : | } | ||
| 261 : | |||
| 262 : | public HitTestResultBehavior HitTestCallback(HitTestResult result) | ||
| 263 : | { | ||
| 264 : | if (result.VisualHit != null) | ||
| 265 : | { | ||
| 266 : | return HitTestResultBehavior.Stop; | ||
| 267 : | } | ||
| 268 : | return HitTestResultBehavior.Continue; | ||
| 269 : | } | ||
| 270 : | |||
| 271 : | private void CameraPanUP(Point currentPosition) | ||
| 272 : | { | ||
| 273 : | double yDelta = currentPosition.Y - _previousPosition2D.Y; | ||
| 274 : | double xDelta = currentPosition.X - _previousPosition2D.X; | ||
| 275 : | double scale = yDelta / 10; | ||
| 276 : | |||
| 277 : | _camera.Position = new Point3D(_camera.Position.X, _camera.Position.Y, _camera.Position.Z + scale); | ||
| 278 : | } | ||
| 279 : | |||
| 280 : | |||
| 281 : | } | ||
| 282 : | |||
| 283 : | |||
| 284 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

