Annotation of /trunk/DefaultRenderer/Trackball3D.cs
Parent Directory
|
Revision Log
Revision 7 - (view) (download)
| 1 : | albert | 4 | //------------------------------------------------------------------ |
| 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.Media3D; | ||
| 18 : | |||
| 19 : | namespace Xenki.DefaultRenderer | ||
| 20 : | { | ||
| 21 : | /// <summary> | ||
| 22 : | /// Trackball is a utility class which observes the mouse events | ||
| 23 : | /// on a specified FrameworkElement and produces a Transform3D | ||
| 24 : | /// with the resultant rotation and scale. | ||
| 25 : | /// | ||
| 26 : | /// Example Usage: | ||
| 27 : | /// | ||
| 28 : | /// Trackball trackball = new Trackball(); | ||
| 29 : | /// trackball.EventSource = myElement; | ||
| 30 : | /// myViewport3D.Camera.Transform = trackball.Transform; | ||
| 31 : | /// | ||
| 32 : | /// Because Viewport3Ds only raise events when the mouse is over the | ||
| 33 : | /// rendered 3D geometry (as opposed to not when the mouse is within | ||
| 34 : | /// the layout bounds) you usually want to use another element as | ||
| 35 : | /// your EventSource. For example, a transparent border placed on | ||
| 36 : | /// top of your Viewport3D works well: | ||
| 37 : | /// | ||
| 38 : | /// <Grid> | ||
| 39 : | /// <ColumnDefinition /> | ||
| 40 : | /// <RowDefinition /> | ||
| 41 : | /// <Viewport3D Name="myViewport" ClipToBounds="True" Grid.Row="0" Grid.Column="0" /> | ||
| 42 : | /// <Border Name="myElement" Background="Transparent" Grid.Row="0" Grid.Column="0" /> | ||
| 43 : | /// </Grid> | ||
| 44 : | /// | ||
| 45 : | /// NOTE: The Transform property may be shared by multiple Cameras | ||
| 46 : | /// if you want to have auxilary views following the trackball. | ||
| 47 : | /// | ||
| 48 : | /// It can also be useful to share the Transform property with | ||
| 49 : | /// models in the scene that you want to move with the camera. | ||
| 50 : | /// (For example, the Trackport3D's headlight is implemented | ||
| 51 : | /// this way.) | ||
| 52 : | /// | ||
| 53 : | /// You may also use a Transform3DGroup to combine the | ||
| 54 : | /// Transform property with additional Transforms. | ||
| 55 : | /// </summary> | ||
| 56 : | public class Trackball | ||
| 57 : | { | ||
| 58 : | private FrameworkElement _eventSource; | ||
| 59 : | private Point _previousPosition2D; | ||
| 60 : | private Vector3D _previousPosition3D = new Vector3D(0, 0, 1); | ||
| 61 : | |||
| 62 : | private Transform3DGroup _transform; | ||
| 63 : | private ScaleTransform3D _scale = new ScaleTransform3D(); | ||
| 64 : | private AxisAngleRotation3D _rotation = new AxisAngleRotation3D(); | ||
| 65 : | private TranslateTransform3D _translate = new TranslateTransform3D(); | ||
| 66 : | |||
| 67 : | public Trackball() | ||
| 68 : | { | ||
| 69 : | _transform = new Transform3DGroup(); | ||
| 70 : | _transform.Children.Add(_scale); | ||
| 71 : | _transform.Children.Add(new RotateTransform3D(_rotation)); | ||
| 72 : | _transform.Children.Add(_translate); | ||
| 73 : | } | ||
| 74 : | |||
| 75 : | /// <summary> | ||
| 76 : | /// A transform to move the camera or scene to the trackball's | ||
| 77 : | /// current orientation and scale. | ||
| 78 : | /// </summary> | ||
| 79 : | public Transform3D Transform | ||
| 80 : | { | ||
| 81 : | get { return _transform; } | ||
| 82 : | } | ||
| 83 : | |||
| 84 : | #region Event Handling | ||
| 85 : | |||
| 86 : | /// <summary> | ||
| 87 : | /// The FrameworkElement we listen to for mouse events. | ||
| 88 : | /// </summary> | ||
| 89 : | public FrameworkElement EventSource | ||
| 90 : | { | ||
| 91 : | get { return _eventSource; } | ||
| 92 : | |||
| 93 : | set | ||
| 94 : | { | ||
| 95 : | if (_eventSource != null) | ||
| 96 : | { | ||
| 97 : | _eventSource.MouseDown -= this.OnMouseDown; | ||
| 98 : | _eventSource.MouseUp -= this.OnMouseUp; | ||
| 99 : | _eventSource.MouseMove -= this.OnMouseMove; | ||
| 100 : | } | ||
| 101 : | |||
| 102 : | _eventSource = value; | ||
| 103 : | |||
| 104 : | _eventSource.MouseDown += this.OnMouseDown; | ||
| 105 : | _eventSource.MouseUp += this.OnMouseUp; | ||
| 106 : | _eventSource.MouseMove += this.OnMouseMove; | ||
| 107 : | } | ||
| 108 : | } | ||
| 109 : | |||
| 110 : | private void OnMouseDown(object sender, MouseEventArgs e) | ||
| 111 : | { | ||
| 112 : | Mouse.Capture(EventSource, CaptureMode.Element); | ||
| 113 : | albert | 7 | |
| 114 : | //set the focus point to original point | ||
| 115 : | Vector3D currentPosition3D = ProjectToTrackball( | ||
| 116 : | EventSource.ActualWidth, EventSource.ActualHeight, e.GetPosition(EventSource)); | ||
| 117 : | |||
| 118 : | Vector change = Point.Subtract(_previousPosition2D, e.GetPosition(EventSource)); | ||
| 119 : | |||
| 120 : | Vector3D changeVector = Vector3D.Subtract(_previousPosition3D, currentPosition3D); | ||
| 121 : | _translate.OffsetX += changeVector.X * 1; | ||
| 122 : | _translate.OffsetY += changeVector.Y * 1; | ||
| 123 : | _translate.OffsetZ += changeVector.Z * 1; | ||
| 124 : | // | ||
| 125 : | albert | 4 | _previousPosition2D = e.GetPosition(EventSource); |
| 126 : | _previousPosition3D = ProjectToTrackball( | ||
| 127 : | EventSource.ActualWidth, | ||
| 128 : | EventSource.ActualHeight, | ||
| 129 : | _previousPosition2D); | ||
| 130 : | albert | 7 | |
| 131 : | |||
| 132 : | |||
| 133 : | albert | 4 | } |
| 134 : | |||
| 135 : | private void OnMouseUp(object sender, MouseEventArgs e) | ||
| 136 : | { | ||
| 137 : | Mouse.Capture(EventSource, CaptureMode.None); | ||
| 138 : | } | ||
| 139 : | |||
| 140 : | private void OnMouseMove(object sender, MouseEventArgs e) | ||
| 141 : | { | ||
| 142 : | Point currentPosition = e.GetPosition(EventSource); | ||
| 143 : | |||
| 144 : | if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) | ||
| 145 : | { | ||
| 146 : | Look(currentPosition); | ||
| 147 : | } | ||
| 148 : | albert | 7 | else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))) |
| 149 : | { | ||
| 150 : | Focus(currentPosition); | ||
| 151 : | } | ||
| 152 : | albert | 4 | else if (e.LeftButton == MouseButtonState.Pressed) |
| 153 : | { | ||
| 154 : | Pan(currentPosition); | ||
| 155 : | } | ||
| 156 : | else if (e.RightButton == MouseButtonState.Pressed) | ||
| 157 : | { | ||
| 158 : | Zoom(currentPosition); | ||
| 159 : | } | ||
| 160 : | |||
| 161 : | albert | 7 | |
| 162 : | |||
| 163 : | albert | 4 | _previousPosition2D = currentPosition; |
| 164 : | } | ||
| 165 : | |||
| 166 : | #endregion Event Handling | ||
| 167 : | |||
| 168 : | private void Look(Point currentPosition) | ||
| 169 : | { | ||
| 170 : | Vector3D currentPosition3D = ProjectToTrackball( | ||
| 171 : | EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); | ||
| 172 : | |||
| 173 : | Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); | ||
| 174 : | double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); | ||
| 175 : | Quaternion delta = new Quaternion(axis, -angle); | ||
| 176 : | |||
| 177 : | // Get the current orientantion from the RotateTransform3D | ||
| 178 : | AxisAngleRotation3D r = _rotation; | ||
| 179 : | Quaternion q = new Quaternion(_rotation.Axis, _rotation.Angle); | ||
| 180 : | |||
| 181 : | // Compose the delta with the previous orientation | ||
| 182 : | q *= delta; | ||
| 183 : | |||
| 184 : | // Write the new orientation back to the Rotation3D | ||
| 185 : | _rotation.Axis = q.Axis; | ||
| 186 : | _rotation.Angle = q.Angle; | ||
| 187 : | |||
| 188 : | _previousPosition3D = currentPosition3D; | ||
| 189 : | } | ||
| 190 : | |||
| 191 : | private void Pan(Point currentPosition) | ||
| 192 : | { | ||
| 193 : | Vector3D currentPosition3D = ProjectToTrackball( | ||
| 194 : | EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); | ||
| 195 : | |||
| 196 : | Vector change = Point.Subtract(_previousPosition2D, currentPosition); | ||
| 197 : | |||
| 198 : | Vector3D changeVector = new Vector3D(change.X, change.Y, 0); | ||
| 199 : | |||
| 200 : | _translate.OffsetX += changeVector.X * .104; | ||
| 201 : | _translate.OffsetY -= changeVector.Y * .104; | ||
| 202 : | _translate.OffsetZ += changeVector.Z * .104; | ||
| 203 : | |||
| 204 : | _previousPosition3D = currentPosition3D; | ||
| 205 : | } | ||
| 206 : | |||
| 207 : | private Vector3D ProjectToTrackball(double width, double height, Point point) | ||
| 208 : | { | ||
| 209 : | double x = point.X / (width / 2); // Scale so bounds map to [0,0] - [2,2] | ||
| 210 : | double y = point.Y / (height / 2); | ||
| 211 : | |||
| 212 : | x = x - 1; // Translate 0,0 to the center | ||
| 213 : | y = 1 - y; // Flip so +Y is up instead of down | ||
| 214 : | |||
| 215 : | double z2 = 1 - x * x - y * y; // z^2 = 1 - x^2 - y^2 | ||
| 216 : | double z = z2 > 0 ? Math.Sqrt(z2) : 0; | ||
| 217 : | |||
| 218 : | return new Vector3D(x, y, z); | ||
| 219 : | } | ||
| 220 : | |||
| 221 : | private void Zoom(Point currentPosition) | ||
| 222 : | { | ||
| 223 : | double yDelta = currentPosition.Y - _previousPosition2D.Y; | ||
| 224 : | |||
| 225 : | double scale = Math.Exp(yDelta / 100); // e^(yDelta/100) is fairly arbitrary. | ||
| 226 : | |||
| 227 : | _scale.ScaleX *= scale; | ||
| 228 : | _scale.ScaleY *= scale; | ||
| 229 : | _scale.ScaleZ *= scale; | ||
| 230 : | } | ||
| 231 : | albert | 7 | |
| 232 : | //Point focusPoint = new Point(); | ||
| 233 : | |||
| 234 : | private void Focus(Point currentPosition) | ||
| 235 : | { | ||
| 236 : | //about zoom: | ||
| 237 : | //set the camera to lookat the position of current position | ||
| 238 : | //then, calculate the subcontract of the previous position and current pos that is the distance of zoomin/zoomout | ||
| 239 : | //then, set teh camera's position which can zoom in/out encircle object(position of mouse clicked) | ||
| 240 : | |||
| 241 : | //about rotation | ||
| 242 : | // | ||
| 243 : | |||
| 244 : | |||
| 245 : | |||
| 246 : | //_previousPosition3D = currentPosition3D; | ||
| 247 : | |||
| 248 : | //double yDeltax = currentPosition - focusPoint.Y; | ||
| 249 : | |||
| 250 : | //double scalex = Math.Exp(changeVector.X / 100); // e^(yDelta/100) is fairly arbitrary. | ||
| 251 : | //double scaley = Math.Exp(changeVector.Y / 100); // e^(yDelta/100) is fairly arbitrary. | ||
| 252 : | //double scalez = Math.Exp(changeVector.Z / 100); // e^(yDelta/100) is fairly arbitrary. | ||
| 253 : | |||
| 254 : | //_scale.ScaleX *= scalex; | ||
| 255 : | //_scale.ScaleY *= scaley; | ||
| 256 : | //_scale.ScaleZ *= scalez; | ||
| 257 : | } | ||
| 258 : | albert | 4 | } |
| 259 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

