| 64 |
private AxisAngleRotation3D _rotation = new AxisAngleRotation3D(); |
private AxisAngleRotation3D _rotation = new AxisAngleRotation3D(); |
| 65 |
private TranslateTransform3D _translate = new TranslateTransform3D(); |
private TranslateTransform3D _translate = new TranslateTransform3D(); |
| 66 |
|
|
| 67 |
public Trackball() |
private Point3D _cameraPos = new Point3D(0,0,50); |
| 68 |
|
private Vector3D _cameraLookDirection= new Vector3D(128,128,10); |
| 69 |
|
private Vector3D _cameraUpDirection = new Vector3D(0, 0, 1); |
| 70 |
|
private PerspectiveCamera _camera; |
| 71 |
|
public Trackball(PerspectiveCamera camera) |
| 72 |
{ |
{ |
| 73 |
|
_camera = camera; |
| 74 |
_transform = new Transform3DGroup(); |
_transform = new Transform3DGroup(); |
| 75 |
_transform.Children.Add(_scale); |
_transform.Children.Add(_scale); |
| 76 |
_transform.Children.Add(new RotateTransform3D(_rotation)); |
_transform.Children.Add(new RotateTransform3D(_rotation)); |
| 85 |
{ |
{ |
| 86 |
get { return _transform; } |
get { return _transform; } |
| 87 |
} |
} |
| 88 |
|
public Point3D CameraPos |
| 89 |
|
{ |
| 90 |
|
get { return _cameraPos; } |
| 91 |
|
} |
| 92 |
|
public Vector3D CameraLookDirection |
| 93 |
|
{ |
| 94 |
|
get { return _cameraLookDirection; } |
| 95 |
|
set { _cameraLookDirection = value; } |
| 96 |
|
|
| 97 |
|
} |
| 98 |
|
public Vector3D CameraUpDirection |
| 99 |
|
{ |
| 100 |
|
get { return _cameraUpDirection; } |
| 101 |
|
} |
| 102 |
|
|
| 103 |
#region Event Handling |
#region Event Handling |
| 104 |
|
|
| 130 |
{ |
{ |
| 131 |
Mouse.Capture(EventSource, CaptureMode.Element); |
Mouse.Capture(EventSource, CaptureMode.Element); |
| 132 |
|
|
| 133 |
//set the focus point to original point |
//set the click point as the aim point |
| 134 |
Vector3D currentPosition3D = ProjectToTrackball( |
Vector3D currentPosition3D = ProjectToTrackball( |
| 135 |
EventSource.ActualWidth, EventSource.ActualHeight, e.GetPosition(EventSource)); |
EventSource.ActualWidth, EventSource.ActualHeight, e.GetPosition(EventSource)); |
| 136 |
|
|
|
Vector change = Point.Subtract(_previousPosition2D, e.GetPosition(EventSource)); |
|
| 137 |
|
|
| 138 |
Vector3D changeVector = Vector3D.Subtract(_previousPosition3D, currentPosition3D); |
_camera.LookDirection = currentPosition3D; |
| 139 |
_translate.OffsetX += changeVector.X * 1; |
|
|
_translate.OffsetY += changeVector.Y * 1; |
|
|
_translate.OffsetZ += changeVector.Z * 1; |
|
|
// |
|
| 140 |
_previousPosition2D = e.GetPosition(EventSource); |
_previousPosition2D = e.GetPosition(EventSource); |
| 141 |
_previousPosition3D = ProjectToTrackball( |
_previousPosition3D = ProjectToTrackball( |
| 142 |
EventSource.ActualWidth, |
EventSource.ActualWidth, |
| 255 |
|
|
| 256 |
//about rotation |
//about rotation |
| 257 |
// |
// |
| 258 |
|
Vector3D currentPosition3D = ProjectToTrackball( |
| 259 |
|
EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); |
| 260 |
|
|
| 261 |
|
Vector change = Point.Subtract(_previousPosition2D, currentPosition); |
| 262 |
|
|
| 263 |
|
Vector3D changeVector = new Vector3D(change.X, change.Y, 0); |
| 264 |
|
|
| 265 |
//_previousPosition3D = currentPosition3D; |
_translate.OffsetX += changeVector.X * .104; |
| 266 |
|
_translate.OffsetY -= changeVector.Y * .104; |
| 267 |
|
_translate.OffsetZ += changeVector.Z * .104; |
| 268 |
|
|
| 269 |
//double yDeltax = currentPosition - focusPoint.Y; |
_previousPosition3D = currentPosition3D; |
| 270 |
|
} |
| 271 |
|
|
| 272 |
//double scalex = Math.Exp(changeVector.X / 100); // e^(yDelta/100) is fairly arbitrary. |
public void SetCameraPosition(Point3D point) |
| 273 |
//double scaley = Math.Exp(changeVector.Y / 100); // e^(yDelta/100) is fairly arbitrary. |
{ |
|
//double scalez = Math.Exp(changeVector.Z / 100); // e^(yDelta/100) is fairly arbitrary. |
|
| 274 |
|
|
|
//_scale.ScaleX *= scalex; |
|
|
//_scale.ScaleY *= scaley; |
|
|
//_scale.ScaleZ *= scalez; |
|
| 275 |
} |
} |
| 276 |
} |
} |
| 277 |
} |
} |