| 11 |
// |
// |
| 12 |
//------------------------------------------------------------------ |
//------------------------------------------------------------------ |
| 13 |
|
|
|
//------------------------------------------------------------------ |
|
|
// |
|
|
// using http://www.codeplex.com/3DTools |
|
|
// |
|
|
//------------------------------------------------------------------- |
|
| 14 |
|
|
| 15 |
|
|
| 16 |
using System; |
using System; |
| 21 |
using System.Windows.Controls; |
using System.Windows.Controls; |
| 22 |
using Petzold.Media3D; |
using Petzold.Media3D; |
| 23 |
using _3DTools; |
using _3DTools; |
| 24 |
|
using Xenki.Framework; |
| 25 |
|
using System.Collections.Generic; |
| 26 |
|
|
| 27 |
|
|
| 28 |
namespace Xenki.DefaultRenderer |
namespace Xenki.DefaultRenderer |
| 29 |
{ |
{ |
| 30 |
|
public delegate void CameraPositionChanged(Point3D oldpos,Point3D newpos); |
| 31 |
|
|
| 32 |
public class CameraControl |
public class CameraControl |
| 33 |
{ |
{ |
| 34 |
private FrameworkElement _eventSource; |
private FrameworkElement _eventSource; |
| 35 |
private Point _previousPosition2D; |
private Point _previousPosition2D; |
| 36 |
private Vector3D _previousPosition3D = new Vector3D(0, 0, 1); |
private Vector3D _previousPosition3D = new Vector3D(0, 0, 1); |
| 37 |
|
|
|
private Transform3DGroup _transform; |
|
|
private ScaleTransform3D _scale = new ScaleTransform3D(); |
|
|
private AxisAngleRotation3D _rotation = new AxisAngleRotation3D(); |
|
|
private TranslateTransform3D _translate = new TranslateTransform3D(); |
|
|
|
|
|
private Vector3D _cameraLookDirection = new Vector3D(128, 128, 10); |
|
|
|
|
| 38 |
private PerspectiveCamera _camera; |
private PerspectiveCamera _camera; |
| 39 |
private Point3D _cameraLookPoint; |
private Point3D _cameraLookPoint; |
|
private RotateTransform3D _rotationTransform = new RotateTransform3D(); |
|
| 40 |
ModelVisual3D visul3d; |
ModelVisual3D visul3d; |
| 41 |
|
|
|
//rotate and pan object clicked |
|
|
private TranslateTransform3D _clickedTranslate = new TranslateTransform3D(); |
|
|
private RotateTransform3D _clickedRotate = new RotateTransform3D(); |
|
|
private AxisAngleRotation3D _clickedAxisAngleRotate = new AxisAngleRotation3D(); |
|
| 42 |
Point3D _clickedCenterPosition; |
Point3D _clickedCenterPosition; |
| 43 |
Transform3DGroup _clickedGroup; |
|
| 44 |
|
DefaultAvatarManager avatarManager = DefaultAvatarManager.SingletonInstance(); |
| 45 |
|
|
| 46 |
bool isTracking = false; |
bool isTracking = false; |
| 47 |
|
bool isPanningObject = false; |
| 48 |
|
|
| 49 |
public CameraControl(PerspectiveCamera camera) |
DefaultPrimControl m_primitiveData; |
| 50 |
{ |
|
| 51 |
_camera = camera; |
public event CameraPositionChanged OncameraPositionChanged; |
| 52 |
|
|
| 53 |
|
System.Windows.Threading.DispatcherTimer frameTimer; |
| 54 |
|
|
|
_rotationTransform.Rotation = _rotation; |
|
| 55 |
|
|
| 56 |
_transform = new Transform3DGroup(); |
public CameraControl(PerspectiveCamera camera,DefaultPrimControl datacontrol) |
| 57 |
// _transform.Children.Add(_translate); |
{ |
| 58 |
_transform.Children.Add(_rotationTransform); |
//dataControl = DefaultNetwork.DefaultNetwork.m_primitiveData; |
| 59 |
|
|
| 60 |
_cameraLookDirection = _camera.LookDirection; |
_camera = camera; |
| 61 |
|
m_primitiveData = datacontrol; |
| 62 |
|
|
| 63 |
|
frameTimer = new System.Windows.Threading.DispatcherTimer(); |
| 64 |
|
frameTimer.Tick += new EventHandler(frameTimer_Tick); |
| 65 |
|
frameTimer.Interval = new TimeSpan(0, 0, 0, 1, 10); |
| 66 |
|
frameTimer.Start(); |
| 67 |
} |
} |
| 68 |
|
|
| 69 |
/// <summary> |
void frameTimer_Tick(object sender, EventArgs e) |
|
/// A transform to move the camera or scene to the trackball's |
|
|
/// current orientation and scale. |
|
|
/// </summary> |
|
|
public Transform3DGroup Transform |
|
| 70 |
{ |
{ |
| 71 |
get { return _transform; } |
// FPSMouseMove(); |
| 72 |
} |
} |
| 73 |
|
|
| 74 |
|
|
| 75 |
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetCursorPos")] |
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetCursorPos")] |
| 76 |
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] |
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] |
| 77 |
public static extern bool SetCursorPos(int X, int Y); |
public static extern bool SetCursorPos(int X, int Y); |
| 95 |
_eventSource.MouseMove -= this.OnMouseMove; |
_eventSource.MouseMove -= this.OnMouseMove; |
| 96 |
_eventSource.MouseLeftButtonDown -= _eventSource_MouseLeftButtonDown; |
_eventSource.MouseLeftButtonDown -= _eventSource_MouseLeftButtonDown; |
| 97 |
_eventSource.MouseRightButtonDown -= _eventSource_MouseRightButtonDown; |
_eventSource.MouseRightButtonDown -= _eventSource_MouseRightButtonDown; |
| 98 |
|
_eventSource.KeyDown -= this._eventSource_KeyDown; |
| 99 |
|
|
| 100 |
} |
} |
| 101 |
_eventSource = value; |
_eventSource = value; |
| 102 |
_eventSource.MouseWheel += _eventSource_MouseWheel; |
_eventSource.MouseWheel += _eventSource_MouseWheel; |
| 104 |
_eventSource.MouseRightButtonDown += _eventSource_MouseRightButtonDown; |
_eventSource.MouseRightButtonDown += _eventSource_MouseRightButtonDown; |
| 105 |
_eventSource.MouseUp += this.OnMouseUp; |
_eventSource.MouseUp += this.OnMouseUp; |
| 106 |
_eventSource.MouseMove += this.OnMouseMove; |
_eventSource.MouseMove += this.OnMouseMove; |
| 107 |
|
|
| 108 |
|
_eventSource.Focusable = true; |
| 109 |
|
_eventSource.Focus(); |
| 110 |
|
Keyboard.Focus(_eventSource); |
| 111 |
|
|
| 112 |
|
_eventSource.KeyDown += _eventSource_KeyDown; |
| 113 |
} |
} |
| 114 |
} |
} |
| 115 |
|
|
| 116 |
|
void _eventSource_KeyDown(object sender, KeyEventArgs e) |
| 117 |
|
{ |
| 118 |
|
if ((e.Key == Key.Left||e.Key == Key.A)&&Mouse.LeftButton == MouseButtonState.Released&&MouseButtonState.Released==Mouse.RightButton) |
| 119 |
|
{ |
| 120 |
|
CameraLookLeftRight(-4.0); |
| 121 |
|
|
| 122 |
|
e.Handled = true; |
| 123 |
|
} |
| 124 |
|
else if (e.Key == Key.Right||e.Key == Key.D) |
| 125 |
|
{ |
| 126 |
|
CameraLookLeftRight(4.0); |
| 127 |
|
|
| 128 |
|
e.Handled = true; |
| 129 |
|
} |
| 130 |
|
else if (e.Key == Key.Up || e.Key == Key.W) |
| 131 |
|
{ |
| 132 |
|
//walk forward |
| 133 |
|
CameraWalk(0.05); |
| 134 |
|
e.Handled = true; |
| 135 |
|
} |
| 136 |
|
else if (e.Key == Key.Down || e.Key == Key.S) |
| 137 |
|
{ |
| 138 |
|
//walk back |
| 139 |
|
CameraWalk(-0.05); |
| 140 |
|
|
| 141 |
|
e.Handled = true; |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
//camera walk should be setting according to the avatar's walking path |
| 147 |
|
private void CameraWalk(double distanceTick) |
| 148 |
|
{ |
| 149 |
|
|
| 150 |
|
OpenMetaverse.Vector3 newpos = OpenMetaverse.Vector3.Add(avatarManager.m_Myself.Position,avatarManager.m_Myself.Position* ((float)distanceTick)); |
| 151 |
|
|
| 152 |
|
avatarManager.AvatarMove(avatarManager.m_Myself,newpos); |
| 153 |
|
|
| 154 |
|
//Vector3D cameraLookDirection = _camera.LookDirection; |
| 155 |
|
//Vector3D vzAxis = new Vector3D(0, 0, 1); |
| 156 |
|
//Vector3D walkDirection = Vector3D.Add(vzAxis, cameraLookDirection); |
| 157 |
|
_camera.LookDirection = new Vector3D(_camera.LookDirection.X, _camera.LookDirection.Y, 0); |
| 158 |
|
|
| 159 |
|
Point3D pos = Point3D.Add(_camera.Position, _camera.LookDirection * distanceTick); |
| 160 |
|
if (OncameraPositionChanged != null) |
| 161 |
|
{ |
| 162 |
|
OncameraPositionChanged(_camera.Position, pos); |
| 163 |
|
} |
| 164 |
|
|
| 165 |
|
_camera.Position = new Point3D(newpos.X + 5, newpos.Y + 5, newpos.Z + 5);// pos; |
| 166 |
|
|
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
private void CameraLookLeftRight(double angle) |
| 170 |
|
{ |
| 171 |
|
Vector3D axis = new Vector3D(0, 0, 1); |
| 172 |
|
|
| 173 |
|
Quaternion delta = new Quaternion(axis, -angle); |
| 174 |
|
|
| 175 |
|
Matrix3D cameraMatrix3D = new Matrix3D(); |
| 176 |
|
cameraMatrix3D.RotateAt(delta, _camera.Position); |
| 177 |
|
|
| 178 |
|
_camera.LookDirection = Vector3D.Multiply(_camera.LookDirection, cameraMatrix3D);// Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 179 |
|
} |
| 180 |
|
|
| 181 |
void _eventSource_MouseRightButtonDown(object sender, MouseButtonEventArgs e) |
void _eventSource_MouseRightButtonDown(object sender, MouseButtonEventArgs e) |
| 182 |
{ |
{ |
| 183 |
Mouse.Capture(_eventSource, CaptureMode.Element); |
Mouse.Capture(_eventSource, CaptureMode.Element); |
| 184 |
|
|
| 185 |
Point pclick = e.GetPosition(_eventSource); |
Point pclick = e.GetPosition(_eventSource); |
| 186 |
|
originalMouseClickPos = _eventSource.PointToScreen(pclick); |
| 187 |
|
|
| 188 |
HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
| 189 |
RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; |
RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; |
| 190 |
if (result3d == null) |
if (result3d == null) |
| 194 |
if (visul3d == null) |
if (visul3d == null) |
| 195 |
return; |
return; |
| 196 |
|
|
|
Transform3DGroup group = visul3d.Transform as Transform3DGroup; |
|
|
if (group != null) |
|
|
{ |
|
|
foreach (Transform3D tran in group.Children) |
|
|
{ |
|
|
if (tran is RotateTransform3D) |
|
|
{ |
|
|
|
|
|
_clickedRotate = tran as RotateTransform3D; |
|
|
if (_clickedRotate != null) |
|
|
{ |
|
|
_clickedAxisAngleRotate = new AxisAngleRotation3D(); |
|
|
_clickedRotate.Rotation = _clickedAxisAngleRotate; |
|
|
} |
|
|
} |
|
|
else if (tran is TranslateTransform3D) |
|
|
_clickedTranslate = tran as TranslateTransform3D; |
|
|
} |
|
|
} |
|
|
else |
|
|
return; |
|
|
|
|
| 197 |
//for ALT + MOUSE + ROTATE |
//for ALT + MOUSE + ROTATE |
| 198 |
VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); |
VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); |
| 199 |
|
|
| 200 |
_rotationTransform.CenterX = _cameraLookPoint.X; |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
|
_rotationTransform.CenterY = _cameraLookPoint.Y; |
|
|
_rotationTransform.CenterZ = _cameraLookPoint.Z; |
|
|
|
|
|
_cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
|
|
_camera.LookDirection = _cameraLookDirection; |
|
| 201 |
|
|
| 202 |
isTracking = true; |
isTracking = true; |
| 203 |
_eventSource.CaptureMouse(); |
_eventSource.CaptureMouse(); |
| 222 |
if (visul3d == null) |
if (visul3d == null) |
| 223 |
return; |
return; |
| 224 |
|
|
|
_clickedGroup = visul3d.Transform as Transform3DGroup; |
|
|
if (_clickedGroup != null) |
|
|
{ |
|
|
|
|
|
foreach (Transform3D tran in _clickedGroup.Children) |
|
|
{ |
|
|
if (tran is RotateTransform3D) |
|
|
{ |
|
|
_clickedRotate = tran as RotateTransform3D; |
|
|
if (_clickedRotate != null) |
|
|
{ |
|
|
_clickedAxisAngleRotate = _clickedRotate.Rotation as AxisAngleRotation3D; |
|
|
if (_clickedAxisAngleRotate == null) |
|
|
_clickedAxisAngleRotate = new AxisAngleRotation3D(); |
|
|
|
|
|
_clickedRotate.Rotation = _clickedAxisAngleRotate; |
|
|
} |
|
|
} |
|
|
else if (tran is TranslateTransform3D) |
|
|
{ |
|
|
_clickedTranslate = tran as TranslateTransform3D; |
|
|
} |
|
|
} |
|
|
} |
|
|
else |
|
|
return; |
|
|
|
|
| 225 |
//for ALT + MOUSE + ROTATE |
//for ALT + MOUSE + ROTATE |
| 226 |
VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); |
VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); |
| 227 |
|
|
| 228 |
_rotationTransform.CenterX = _cameraLookPoint.X; |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 229 |
_rotationTransform.CenterY = _cameraLookPoint.Y; |
|
| 230 |
_rotationTransform.CenterZ = _cameraLookPoint.Z; |
_clickedCenterPosition = GetObjectCenterPosition(visul3d); |
|
|
|
|
//for rotate the object mouse clicked |
|
|
_clickedRotate.CenterX = _clickedCenterPosition.X = visul3d.Content.Bounds.Location.X + visul3d.Content.Bounds.SizeX / 2; |
|
|
_clickedRotate.CenterY = _clickedCenterPosition.Y = visul3d.Content.Bounds.Location.Y + visul3d.Content.Bounds.SizeY / 2; |
|
|
_clickedRotate.CenterZ = _clickedCenterPosition.Z = visul3d.Content.Bounds.Location.Z + visul3d.Content.Bounds.SizeZ / 2; |
|
| 231 |
|
|
| 232 |
_cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
//uint? key = DefaultNetwork.DefaultNetwork.m_primitiveData.GetKeyInSceneByValue(visul3d); |
| 233 |
_camera.LookDirection = _cameraLookDirection; |
//if (key.HasValue) |
| 234 |
|
//{ |
| 235 |
|
// OpenMetaverse.Primitive dataaaa =DefaultNetwork.DefaultNetwork.m_primitiveData.PrimModelsInScene[key.Value]; |
| 236 |
|
// uint id = dataaaa.ParentID; |
| 237 |
|
// OpenMetaverse.Vector3 parentpos = DefaultNetwork.DefaultNetwork.m_primitiveData.GetModelsParentPosition(id); |
| 238 |
|
// OpenMetaverse.Vector3 mypos = dataaaa.Position; |
| 239 |
|
|
| 240 |
|
//} |
| 241 |
//TRACK BALL |
//TRACK BALL |
| 242 |
_previousPosition3D = ProjectToTrackball( |
_previousPosition3D = ProjectToTrackball( |
| 243 |
EventSource.ActualWidth, |
EventSource.ActualWidth, |
| 250 |
e.Handled = true; |
e.Handled = true; |
| 251 |
} |
} |
| 252 |
|
|
| 253 |
|
private Point3D GetObjectCenterPosition(ModelVisual3D visual) |
| 254 |
|
{ |
| 255 |
|
return Point3D.Add(visual.Content.Bounds.Location, |
| 256 |
|
new Vector3D(visual.Content.Bounds.SizeX / 2, visual.Content.Bounds.SizeY / 2, visual.Content.Bounds.SizeZ / 2)); |
| 257 |
|
} |
| 258 |
|
|
| 259 |
private void OnMouseUp(object sender, MouseEventArgs e) |
private void OnMouseUp(object sender, MouseEventArgs e) |
| 260 |
{ |
{ |
|
Mouse.Capture(EventSource, CaptureMode.None); |
|
|
isTracking = false; |
|
| 261 |
//set the mouse to the original position when clicked the object. |
//set the mouse to the original position when clicked the object. |
| 262 |
|
if (_eventSource.Cursor != Cursors.Arrow) |
| 263 |
|
{ |
| 264 |
SetCursorPos((int)originalMouseClickPos.X, (int)originalMouseClickPos.Y); |
SetCursorPos((int)originalMouseClickPos.X, (int)originalMouseClickPos.Y); |
| 265 |
|
_eventSource.Cursor = Cursors.Arrow; |
| 266 |
|
} |
| 267 |
|
Mouse.Capture(EventSource, CaptureMode.None); |
| 268 |
|
isTracking = false; |
| 269 |
|
if (isPanningObject == true) |
| 270 |
|
{ |
| 271 |
|
isPanningObject = false; |
| 272 |
|
_cameraLookPoint = GetObjectCenterPosition(visul3d); |
| 273 |
|
} |
| 274 |
|
|
| 275 |
_eventSource.ReleaseMouseCapture(); |
_eventSource.ReleaseMouseCapture(); |
| 276 |
_eventSource.Cursor = Cursors.Arrow; |
|
| 277 |
} |
} |
| 278 |
|
|
| 279 |
|
Point currentPosition; |
| 280 |
|
|
| 281 |
private void OnMouseMove(object sender, MouseEventArgs e) |
private void OnMouseMove(object sender, MouseEventArgs e) |
| 282 |
{ |
{ |
| 283 |
Point currentPosition = e.GetPosition(EventSource); |
currentPosition = e.GetPosition(EventSource); |
| 284 |
|
|
| 285 |
if (e.RightButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) |
if (e.RightButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) |
| 286 |
{ |
{ |
| 294 |
Focus(currentPosition); |
Focus(currentPosition); |
| 295 |
} |
} |
| 296 |
} |
} |
|
else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.Q)) |
|
|
{ |
|
|
if(isTracking) |
|
|
CameraPanUP(currentPosition); |
|
|
} |
|
| 297 |
else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.A)) |
else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.A)) |
| 298 |
{ |
{ |
| 299 |
if (isTracking) |
if (isTracking) |
| 317 |
if (isTracking) |
if (isTracking) |
| 318 |
{ |
{ |
| 319 |
_eventSource.Cursor = Cursors.None; |
_eventSource.Cursor = Cursors.None; |
| 320 |
|
isPanningObject = true; |
| 321 |
PanObject(currentPosition); |
PanObject(currentPosition); |
|
|
|
| 322 |
} |
} |
| 323 |
} |
} |
| 324 |
else if (e.LeftButton == MouseButtonState.Pressed) |
else if (e.LeftButton == MouseButtonState.Pressed) |
| 325 |
{ |
{ |
| 326 |
|
if(isTracking) |
| 327 |
LookAround(currentPosition); |
LookAround(currentPosition); |
| 328 |
} |
} |
| 329 |
|
else if (e.RightButton == MouseButtonState.Pressed) |
| 330 |
|
{ |
| 331 |
|
if (isTracking) |
| 332 |
|
CameraPanUP(currentPosition); |
| 333 |
|
} |
| 334 |
|
|
| 335 |
_previousPosition2D = currentPosition; |
_previousPosition2D = currentPosition; |
|
|
|
| 336 |
} |
} |
| 337 |
|
|
| 338 |
private void LookAround(Point currentPosition) |
private void LookAround(Point currentPosition) |
| 339 |
{ |
{ |
| 340 |
|
|
| 341 |
|
if (_cameraLookPoint == new Point3D(0, 0, 0)) |
| 342 |
|
return; |
| 343 |
|
|
| 344 |
Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); |
Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); |
| 345 |
|
|
| 346 |
|
if (_previousPosition3D == currentPosition3D) |
| 347 |
|
return; |
| 348 |
|
|
| 349 |
Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); |
Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); |
| 350 |
double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); |
double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); |
| 351 |
|
|
| 352 |
|
angle *= 2; |
| 353 |
|
|
| 354 |
// We negate the angle because we are rotating the camera. |
// We negate the angle because we are rotating the camera. |
| 355 |
// Do not do this if you are rotating the scene instead. |
// Do not do this if you are rotating the scene instead. |
| 356 |
Quaternion delta = new Quaternion(axis, -angle); |
Quaternion delta = new Quaternion(axis, -angle); |
| 357 |
|
|
| 358 |
// Get the current orientantion from the RotateTransform3D |
Matrix3D cameraMatrix3D = new Matrix3D(); |
| 359 |
Transform3DGroup group = _camera.Transform as Transform3DGroup; |
cameraMatrix3D.RotateAt(delta,_cameraLookPoint); |
|
if (group == null) |
|
|
return; |
|
| 360 |
|
|
| 361 |
foreach (Transform3D tran in group.Children) |
_camera.Position = Point3D.Multiply(_camera.Position, cameraMatrix3D); |
| 362 |
{ |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
|
RotateTransform3D rotate = tran as RotateTransform3D; |
|
|
if (rotate == null) |
|
|
continue; |
|
|
RotateTransform3D rt = (RotateTransform3D)rotate; |
|
|
AxisAngleRotation3D r = (AxisAngleRotation3D)rt.Rotation; |
|
|
Quaternion q = new Quaternion(r.Axis, r.Angle); |
|
|
|
|
|
// Compose the delta with the previous orientation |
|
|
q *= delta; |
|
|
|
|
|
// Write the new orientation back to the Rotation3D |
|
|
r.Axis = q.Axis; |
|
|
r.Angle = q.Angle; |
|
|
} |
|
| 363 |
|
|
| 364 |
_previousPosition3D = currentPosition3D; |
_previousPosition3D = currentPosition3D; |
| 365 |
} |
} |
| 366 |
|
|
| 367 |
void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) |
void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) |
| 368 |
{ |
{ |
| 369 |
double scale = -e.Delta / 1200.000; |
double scale = -e.Delta / 800.000; |
| 370 |
|
if(_cameraLookPoint!=null&&_cameraLookPoint!=new Point3D(0,0,0)) |
| 371 |
|
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 372 |
|
|
| 373 |
AdjustCameraPosition(_camera, _cameraLookDirection, scale); |
int count = Math.Abs((int)(scale / zoomFactor)); |
| 374 |
|
for (int i = 1; i <= count; i++) |
| 375 |
|
{ |
| 376 |
|
if (scale > 0) |
| 377 |
|
AdjustCameraPosition(_camera, _camera.LookDirection, zoomFactor); |
| 378 |
|
else |
| 379 |
|
AdjustCameraPosition(_camera, _camera.LookDirection, -zoomFactor); |
| 380 |
|
} |
| 381 |
} |
} |
| 382 |
|
|
| 383 |
private void RotateObject(Point currentPosition) |
private void RotateObject(Point currentPosition) |
| 384 |
{ |
{ |
|
Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); |
|
| 385 |
|
|
| 386 |
|
Vector3D currentPosition3D = ProjectToTrackball(_eventSource.ActualWidth, _eventSource.ActualHeight, currentPosition); |
| 387 |
Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); |
Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); |
| 388 |
double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); |
double angle = Vector3D.AngleBetween(currentPosition3D, _previousPosition3D); |
| 389 |
|
|
| 390 |
|
angle *= 3.00000; |
| 391 |
|
|
| 392 |
if (_clickedAxisAngleRotate.Angle >= 360 || _clickedAxisAngleRotate.Angle <= -360) |
Matrix3D mat = new Matrix3D(); |
| 393 |
_clickedAxisAngleRotate.Angle = 0; |
mat.RotateAt(new Quaternion(axis, angle), _clickedCenterPosition); |
| 394 |
|
|
| 395 |
_clickedAxisAngleRotate.Axis = axis; |
RotateObject(mat, visul3d); |
| 396 |
_clickedAxisAngleRotate.Angle -= angle; |
|
| 397 |
|
IEnumerator<Visual3D> children = m_primitiveData.GetChildrenByVisual3D(visul3d); |
| 398 |
|
|
| 399 |
|
while (children.MoveNext()) |
| 400 |
|
{ |
| 401 |
|
RotateObject(mat, children.Current); |
| 402 |
|
} |
| 403 |
|
|
| 404 |
_previousPosition3D = currentPosition3D; |
_previousPosition3D = currentPosition3D; |
| 405 |
|
|
| 406 |
} |
} |
| 407 |
|
|
| 408 |
|
private void RotateObject(Matrix3D mat,Visual3D visual) |
| 409 |
|
{ |
| 410 |
|
|
| 411 |
|
GeometryModel3D geometryModel3D = (visual as ModelVisual3D).Content as GeometryModel3D; |
| 412 |
|
|
| 413 |
|
if (geometryModel3D != null) |
| 414 |
|
{ |
| 415 |
|
MeshGeometry3D meshGeometry3D = geometryModel3D.Geometry as MeshGeometry3D; |
| 416 |
|
if (meshGeometry3D != null) |
| 417 |
|
{ |
| 418 |
|
Point3DCollection points = meshGeometry3D.Positions; |
| 419 |
|
|
| 420 |
|
for (int i = 0; i < points.Count; i++) |
| 421 |
|
points[i] = Point3D.Multiply(points[i], mat); |
| 422 |
|
} |
| 423 |
|
} |
| 424 |
|
|
| 425 |
|
} |
| 426 |
|
|
| 427 |
private Vector3D ProjectToTrackball(double width, double height, Point point) |
private Vector3D ProjectToTrackball(double width, double height, Point point) |
| 428 |
{ |
{ |
| 429 |
double x = point.X / (width / 2); // Scale so bounds map to [0,0] - [2,2] |
double x = point.X / (width / 2); // Scale so bounds map to [0,0] - [2,2] |
| 448 |
double scalex = currentPosition.X - _previousPosition2D.X; |
double scalex = currentPosition.X - _previousPosition2D.X; |
| 449 |
|
|
| 450 |
Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); |
Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); |
| 451 |
|
Point3D cPos = _camera.Position; |
| 452 |
|
|
| 453 |
_camera.Position += Math.Abs(scalex) * vector3D; |
cPos += Math.Abs(scalex) * vector3D; |
| 454 |
|
|
| 455 |
|
if (OncameraPositionChanged != null) |
| 456 |
|
OncameraPositionChanged(_camera.Position, cPos); |
| 457 |
|
|
| 458 |
|
_camera.Position = cPos; |
| 459 |
} |
} |
| 460 |
|
|
| 461 |
private void PanObject(Point currentPosition) |
private void PanObject(Point currentPosition) |
| 462 |
{ |
{ |
| 463 |
double scaley = currentPosition.Y - _previousPosition2D.Y; |
IEnumerator<Visual3D> children = m_primitiveData.GetChildrenByVisual3D(visul3d); |
|
scaley = Math.Abs( scaley ); |
|
|
|
|
|
double scalex = currentPosition.X - _previousPosition2D.X; |
|
|
scalex = Math.Abs( scalex); |
|
|
|
|
| 464 |
|
|
| 465 |
Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); |
Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); |
| 466 |
|
MoveObject(visul3d, vector3D); |
| 467 |
|
|
| 468 |
_clickedTranslate.OffsetX += scalex * vector3D.X;// _clickedTranslateOriginal.OffsetX + vector3D.X; |
while( children.MoveNext()) |
| 469 |
_clickedTranslate.OffsetY += scalex * vector3D.Y;// _clickedTranslateOriginal.OffsetY + vector3D.Y; |
{ |
| 470 |
_clickedTranslate.OffsetZ += scaley * vector3D.Z;// _clickedTranslateOriginal.OffsetZ + vector3D.Z;// vectMouse.Z; |
MoveObject(children.Current, vector3D); |
| 471 |
|
} |
| 472 |
|
} |
| 473 |
|
|
| 474 |
visul3d.Transform.Transform(new Point3D(visul3d.Content.Bounds.Location.X + scalex * vector3D.X, visul3d.Content.Bounds.Location.Y + scalex * vector3D.Y, visul3d.Content.Bounds.Location.Z + scaley * vector3D.Z)); |
private void MoveObject(Visual3D visual, Vector3D vector3D) |
| 475 |
|
{ |
| 476 |
|
|
| 477 |
|
GeometryModel3D geometryModel3D = (visual as ModelVisual3D).Content as GeometryModel3D; |
| 478 |
|
if (geometryModel3D != null) |
| 479 |
|
{ |
| 480 |
|
MeshGeometry3D meshGeometry3D = geometryModel3D.Geometry as MeshGeometry3D; |
| 481 |
|
if (meshGeometry3D != null) |
| 482 |
|
{ |
| 483 |
|
Point3DCollection points = meshGeometry3D.Positions; |
| 484 |
|
|
| 485 |
|
for (int i = 0; i < points.Count; i++) |
| 486 |
|
points[i] = Point3D.Add(points[i], vector3D * 50); |
| 487 |
|
} |
| 488 |
|
} |
| 489 |
} |
} |
| 490 |
|
|
| 491 |
|
string mm = ""; |
| 492 |
private Vector3D GetTranslationVector3D(DependencyObject modelHit, Point startPosition, Point endPosition) |
private Vector3D GetTranslationVector3D(DependencyObject modelHit, Point startPosition, Point endPosition) |
| 493 |
{ |
{ |
| 494 |
Vector3D translationVector3D = new Vector3D(); |
Vector3D translationVector3D = new Vector3D(); |
| 514 |
private void LookUpDown(Point currentPosition) |
private void LookUpDown(Point currentPosition) |
| 515 |
{ |
{ |
| 516 |
double scale = currentPosition.Y- _previousPosition2D.Y; |
double scale = currentPosition.Y- _previousPosition2D.Y; |
| 517 |
scale = scale/5.000000; |
// scale = scale; |
| 518 |
_camera.LookDirection = new Vector3D(_camera.LookDirection.X, _camera.LookDirection.Y, _camera.LookDirection.Z + scale); |
_camera.LookDirection = new Vector3D(_camera.LookDirection.X, _camera.LookDirection.Y, _camera.LookDirection.Z + scale); |
| 519 |
} |
} |
| 520 |
|
|
| 521 |
|
double zoomFactor = 0.0500; |
| 522 |
private void Focus(Point currentPosition) |
private void Focus(Point currentPosition) |
| 523 |
{ |
{ |
| 524 |
//zoom |
//zoom |
| 525 |
double yDelta = currentPosition.Y - _previousPosition2D.Y; |
double yDelta = currentPosition.Y - _previousPosition2D.Y; |
| 526 |
double scale = yDelta / 100.000000; |
double scale = yDelta/20 ; |
| 527 |
AdjustCameraPosition(_camera,_cameraLookDirection,scale); |
int count = Math.Abs( (int)(scale / zoomFactor)); |
| 528 |
|
for (int i = 1; i <= count; i++) |
| 529 |
|
{ |
| 530 |
|
if (yDelta > 0) |
| 531 |
|
AdjustCameraPosition(_camera, _camera.LookDirection, zoomFactor); |
| 532 |
|
else |
| 533 |
|
AdjustCameraPosition(_camera, _camera.LookDirection, -zoomFactor); |
| 534 |
|
} |
| 535 |
|
|
| 536 |
//rotate the camera |
//rotate the camera |
| 537 |
double axisAngle = currentPosition.X - _previousPosition2D.X; |
double axisAngle = _previousPosition2D.X - currentPosition.X; |
| 538 |
_rotation.Axis = new Vector3D(0, 0, 1); |
Matrix3D matrixRoateCamera = new Matrix3D(); |
| 539 |
_rotation.Angle -= axisAngle; |
matrixRoateCamera.RotateAt(new Quaternion(new Vector3D(0, 0, 1), axisAngle), _cameraLookPoint); |
| 540 |
|
|
| 541 |
|
Point3D pos = Point3D.Multiply(_camera.Position, matrixRoateCamera); |
| 542 |
|
if (OncameraPositionChanged != null) |
| 543 |
|
OncameraPositionChanged(_camera.Position, pos); |
| 544 |
|
_camera.Position = pos; |
| 545 |
|
|
| 546 |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 547 |
} |
} |
| 548 |
|
|
| 549 |
private void AdjustCameraPosition(PerspectiveCamera camera,Vector3D lookDirection, double scale ) |
private void AdjustCameraPosition(PerspectiveCamera camera,Vector3D lookDirection, double factor ) |
| 550 |
{ |
{ |
| 551 |
Point3D cameraPos = new Point3D(camera.Position.X - scale * lookDirection.X, |
Point3D cameraPos = new Point3D(camera.Position.X - factor * lookDirection.X, |
| 552 |
camera.Position.Y - scale * lookDirection.Y, |
camera.Position.Y - factor * lookDirection.Y, |
| 553 |
camera.Position.Z - scale * lookDirection.Z); |
camera.Position.Z - factor * lookDirection.Z); |
| 554 |
|
|
| 555 |
//too fast |
//too fast |
| 556 |
if (Point3D.Subtract(cameraPos, camera.Position).Length > 1.000000000) |
//if (Point3D.Subtract(cameraPos, camera.Position).Length > 0.5000000000) |
| 557 |
cameraPos = new Point3D(camera.Position.X - scale / 10 * lookDirection.X, |
// cameraPos = new Point3D(camera.Position.X - factor / 10 * lookDirection.X, |
| 558 |
camera.Position.Y - scale / 10 * lookDirection.Y, |
// camera.Position.Y - factor / 10 * lookDirection.Y, |
| 559 |
camera.Position.Z - scale / 10 * lookDirection.Z); |
// camera.Position.Z - factor / 10 * lookDirection.Z); |
| 560 |
|
|
| 561 |
double distance = Point3D.Subtract(_cameraLookPoint, cameraPos).Length; |
double distance = Point3D.Subtract(_cameraLookPoint, cameraPos).Length; |
| 562 |
|
|
| 563 |
if (distance > 524 || distance <= 1.000000) |
if (distance > 512.0000 || distance <= 0.5000000) |
| 564 |
{ |
{ |
| 565 |
} |
} |
| 566 |
else |
else |
| 567 |
|
{ |
| 568 |
|
if (OncameraPositionChanged != null) |
| 569 |
|
OncameraPositionChanged(camera.Position, cameraPos); |
| 570 |
|
|
| 571 |
camera.Position = cameraPos; |
camera.Position = cameraPos; |
| 572 |
} |
} |
| 573 |
|
|
| 574 |
|
} |
| 575 |
//get the clicked 3d position |
//get the clicked 3d position |
| 576 |
public HitTestResultBehavior HitTestCallback(HitTestResult result) |
public HitTestResultBehavior HitTestCallback(HitTestResult result) |
| 577 |
{ |
{ |