| 11 |
// |
// |
| 12 |
//------------------------------------------------------------------ |
//------------------------------------------------------------------ |
| 13 |
|
|
|
//------------------------------------------------------------------ |
|
|
// |
|
|
// using http://www.codeplex.com/3DTools |
|
|
// |
|
|
//------------------------------------------------------------------- |
|
| 14 |
|
|
| 15 |
|
|
| 16 |
using System; |
using System; |
| 49 |
|
|
| 50 |
frameTimer = new System.Windows.Threading.DispatcherTimer(); |
frameTimer = new System.Windows.Threading.DispatcherTimer(); |
| 51 |
frameTimer.Tick += new EventHandler(frameTimer_Tick); |
frameTimer.Tick += new EventHandler(frameTimer_Tick); |
| 52 |
frameTimer.Interval = new TimeSpan(0, 0, 0, 0, 1); |
frameTimer.Interval = new TimeSpan(0, 0, 0, 0, 10); |
| 53 |
frameTimer.Start(); |
frameTimer.Start(); |
| 54 |
} |
} |
| 55 |
|
|
| 56 |
void frameTimer_Tick(object sender, EventArgs e) |
void frameTimer_Tick(object sender, EventArgs e) |
| 57 |
{ |
{ |
| 58 |
|
|
| 59 |
//this.OnMouseMove(sender, e); |
// FPSMouseMove(); |
| 60 |
} |
} |
| 61 |
|
|
| 62 |
|
|
| 178 |
{ |
{ |
| 179 |
isPanningObject = false; |
isPanningObject = false; |
| 180 |
_cameraLookPoint = GetObjectCenterPosition(visul3d); |
_cameraLookPoint = GetObjectCenterPosition(visul3d); |
|
|
|
| 181 |
} |
} |
| 182 |
//mm+="ResultPosition:" + visul3d.Content.Bounds.Location.ToString(); |
|
| 183 |
//string s = mm; |
string s = mm; |
| 184 |
|
|
| 185 |
_eventSource.ReleaseMouseCapture(); |
_eventSource.ReleaseMouseCapture(); |
| 186 |
_eventSource.Cursor = Cursors.Arrow; |
_eventSource.Cursor = Cursors.Arrow; |
| 238 |
} |
} |
| 239 |
else if (e.LeftButton == MouseButtonState.Pressed) |
else if (e.LeftButton == MouseButtonState.Pressed) |
| 240 |
{ |
{ |
| 241 |
LookAround(currentPosition,_camera.Transform); |
LookAround(currentPosition); |
| 242 |
} |
} |
| 243 |
|
|
| 244 |
|
|
| 246 |
|
|
| 247 |
} |
} |
| 248 |
|
|
| 249 |
private void LookAround(Point currentPosition,Transform3D transformgroup) |
private void LookAround(Point currentPosition) |
| 250 |
{ |
{ |
| 251 |
|
|
| 252 |
|
if (_cameraLookPoint == new Point3D(0, 0, 0)) |
| 253 |
|
return; |
| 254 |
|
|
| 255 |
Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); |
Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); |
| 256 |
|
|
| 257 |
|
if (_previousPosition3D == currentPosition3D) |
| 258 |
|
return; |
| 259 |
|
|
| 260 |
Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); |
Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); |
| 261 |
double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); |
double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); |
| 262 |
|
|
| 263 |
|
angle *= 2; |
| 264 |
|
|
| 265 |
// We negate the angle because we are rotating the camera. |
// We negate the angle because we are rotating the camera. |
| 266 |
// Do not do this if you are rotating the scene instead. |
// Do not do this if you are rotating the scene instead. |
| 267 |
Quaternion delta = new Quaternion(axis, -angle); |
Quaternion delta = new Quaternion(axis, -angle); |
| 268 |
|
|
|
//foreach (Transform3D tran in group.Children) |
|
|
//{ |
|
|
// 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; |
|
|
//} |
|
|
|
|
|
if (_cameraLookPoint == new Point3D(0, 0, 0)) |
|
|
return; |
|
|
|
|
|
// delta *= delta; |
|
| 269 |
Matrix3D cameraMatrix3D = new Matrix3D(); |
Matrix3D cameraMatrix3D = new Matrix3D(); |
| 270 |
cameraMatrix3D.RotateAt(delta,_cameraLookPoint); |
cameraMatrix3D.RotateAt(delta,_cameraLookPoint); |
| 271 |
|
|
|
|
|
| 272 |
_camera.Position = Point3D.Multiply(_camera.Position, cameraMatrix3D); |
_camera.Position = Point3D.Multiply(_camera.Position, cameraMatrix3D); |
| 273 |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 274 |
|
|
| 275 |
|
|
| 276 |
_previousPosition3D = currentPosition3D; |
_previousPosition3D = currentPosition3D; |
| 277 |
} |
} |
| 278 |
|
|
| 279 |
void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) |
void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) |
| 280 |
{ |
{ |
| 281 |
double scale = -e.Delta / 1200.000; |
double scale = -e.Delta / 800.000; |
| 282 |
if(_cameraLookPoint!=null&&_cameraLookPoint!=new Point3D(0,0,0)) |
if(_cameraLookPoint!=null&&_cameraLookPoint!=new Point3D(0,0,0)) |
| 283 |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 284 |
|
|
| 285 |
AdjustCameraPosition(_camera, _camera.LookDirection, scale); |
int count = Math.Abs((int)(scale / zoomFactor)); |
| 286 |
|
for (int i = 1; i <= count; i++) |
| 287 |
|
{ |
| 288 |
|
if (scale > 0) |
| 289 |
|
AdjustCameraPosition(_camera, _camera.LookDirection, zoomFactor); |
| 290 |
|
else |
| 291 |
|
AdjustCameraPosition(_camera, _camera.LookDirection, -zoomFactor); |
| 292 |
|
} |
| 293 |
} |
} |
| 294 |
|
|
| 295 |
|
|
| 296 |
private void RotateObject(Point currentPosition) |
private void RotateObject(Point currentPosition) |
| 297 |
{ |
{ |
| 298 |
Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); |
Vector3D currentPosition3D = ProjectToTrackball(_eventSource.ActualWidth, _eventSource.ActualHeight, currentPosition); |
| 299 |
Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); |
Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); |
| 300 |
double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); |
double angle = Vector3D.AngleBetween(currentPosition3D,_previousPosition3D ); |
| 301 |
|
|
| 302 |
|
angle *= 2; |
| 303 |
|
|
| 304 |
Matrix3D mat = new Matrix3D(); |
Matrix3D mat = new Matrix3D(); |
| 305 |
mat.RotateAt(new Quaternion(axis, angle), _clickedCenterPosition); |
mat.RotateAt(new Quaternion(axis, angle), _clickedCenterPosition); |
| 361 |
Point3DCollection points = meshGeometry3D.Positions; |
Point3DCollection points = meshGeometry3D.Positions; |
| 362 |
|
|
| 363 |
for (int i = 0; i < points.Count; i++) |
for (int i = 0; i < points.Count; i++) |
| 364 |
points[i] = Point3D.Add(points[i], vector3D * 10); |
points[i] = Point3D.Add(points[i], vector3D * 50); |
|
|
|
|
|
|
| 365 |
} |
} |
| 366 |
} |
} |
| 367 |
} |
} |
| 368 |
|
|
| 369 |
string mm = ""; |
string mm = ""; |
| 370 |
private Vector3D GetTranslationVector3D(DependencyObject modelHit, Point startPosition, Point endPosition) |
private Vector3D GetTranslationVector3D(DependencyObject modelHit, Point startPosition, Point endPosition) |
| 371 |
{ |
{ |
| 396 |
_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); |
| 397 |
} |
} |
| 398 |
|
|
| 399 |
|
double zoomFactor = 0.0500; |
| 400 |
private void Focus(Point currentPosition) |
private void Focus(Point currentPosition) |
| 401 |
{ |
{ |
| 402 |
|
|
| 403 |
//zoom |
//zoom |
| 404 |
double yDelta = currentPosition.Y - _previousPosition2D.Y; |
double yDelta = currentPosition.Y - _previousPosition2D.Y; |
| 405 |
double scale = yDelta / 100.000000; |
double scale = yDelta/20 ; |
| 406 |
AdjustCameraPosition(_camera,_camera.LookDirection,scale); |
int count = Math.Abs( (int)(scale / zoomFactor)); |
| 407 |
|
for (int i = 1; i <= count; i++) |
| 408 |
|
{ |
| 409 |
|
if (yDelta > 0) |
| 410 |
|
AdjustCameraPosition(_camera, _camera.LookDirection, zoomFactor); |
| 411 |
|
else |
| 412 |
|
AdjustCameraPosition(_camera, _camera.LookDirection, -zoomFactor); |
| 413 |
|
|
| 414 |
|
} |
| 415 |
//rotate the camera |
//rotate the camera |
| 416 |
double axisAngle = currentPosition.X - _previousPosition2D.X; |
double axisAngle = currentPosition.X - _previousPosition2D.X; |
| 417 |
Matrix3D matrixRoateCamera = new Matrix3D(); |
Matrix3D matrixRoateCamera = new Matrix3D(); |
| 421 |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 422 |
} |
} |
| 423 |
|
|
| 424 |
private void AdjustCameraPosition(PerspectiveCamera camera,Vector3D lookDirection, double scale ) |
private void AdjustCameraPosition(PerspectiveCamera camera,Vector3D lookDirection, double factor ) |
| 425 |
{ |
{ |
| 426 |
Point3D cameraPos = new Point3D(camera.Position.X - scale * lookDirection.X, |
Point3D cameraPos = new Point3D(camera.Position.X - factor * lookDirection.X, |
| 427 |
camera.Position.Y - scale * lookDirection.Y, |
camera.Position.Y - factor * lookDirection.Y, |
| 428 |
camera.Position.Z - scale * lookDirection.Z); |
camera.Position.Z - factor * lookDirection.Z); |
| 429 |
|
|
| 430 |
//too fast |
//too fast |
| 431 |
if (Point3D.Subtract(cameraPos, camera.Position).Length > 1.000000000) |
//if (Point3D.Subtract(cameraPos, camera.Position).Length > 0.5000000000) |
| 432 |
cameraPos = new Point3D(camera.Position.X - scale / 10 * lookDirection.X, |
// cameraPos = new Point3D(camera.Position.X - factor / 10 * lookDirection.X, |
| 433 |
camera.Position.Y - scale / 10 * lookDirection.Y, |
// camera.Position.Y - factor / 10 * lookDirection.Y, |
| 434 |
camera.Position.Z - scale / 10 * lookDirection.Z); |
// camera.Position.Z - factor / 10 * lookDirection.Z); |
| 435 |
|
|
| 436 |
double distance = Point3D.Subtract(_cameraLookPoint, cameraPos).Length; |
double distance = Point3D.Subtract(_cameraLookPoint, cameraPos).Length; |
| 437 |
|
|
| 438 |
if (distance > 524 || distance <= 1.000000) |
if (distance > 512.0000 || distance <= 0.5000000) |
| 439 |
{ |
{ |
| 440 |
} |
} |
| 441 |
else |
else |
| 468 |
|
|
| 469 |
//for test |
//for test |
| 470 |
/* |
/* |
| 471 |
private void FPSMouseMove(object sender, EventArgs e) |
private void FPSMouseMove() |
| 472 |
{ |
{ |
| 473 |
currentPosition = e.GetPosition(EventSource); |
Point curr = Mouse.GetPosition(_eventSource); |
| 474 |
|
currentPosition = curr; |
| 475 |
|
|
| 476 |
if (isTracking) |
if (isTracking) |
| 477 |
{ |
{ |
| 478 |
if (isLookAround == true) |
if (Mouse.RightButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) |
| 479 |
|
{ |
| 480 |
LookUpDown(currentPosition); |
LookUpDown(currentPosition); |
| 481 |
else if (isFocus == true) |
} |
| 482 |
|
else if (Mouse.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))) |
| 483 |
|
{ |
| 484 |
|
if (isTracking) |
| 485 |
{ |
{ |
| 486 |
_eventSource.Cursor = Cursors.None; |
_eventSource.Cursor = Cursors.None; |
| 487 |
Focus(currentPosition); |
Focus(currentPosition); |
| 488 |
} |
} |
| 489 |
else if (isCameraPanUP) |
} |
| 490 |
|
else if (Mouse.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.Q)) |
| 491 |
|
{ |
| 492 |
|
if (isTracking) |
| 493 |
CameraPanUP(currentPosition); |
CameraPanUP(currentPosition); |
| 494 |
else if (isCameraPanLeftRight) |
} |
| 495 |
|
else if (Mouse.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.A)) |
| 496 |
|
{ |
| 497 |
|
if (isTracking) |
| 498 |
{ |
{ |
| 499 |
_eventSource.Cursor = Cursors.None; |
_eventSource.Cursor = Cursors.None; |
| 500 |
CameraPanLeftRight(currentPosition); |
CameraPanLeftRight(currentPosition); |
| 501 |
} |
} |
| 502 |
else if (isRotateObject) |
} |
| 503 |
|
else if (Mouse.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) |
| 504 |
|
&& (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) |
| 505 |
|
{ |
| 506 |
|
if (isTracking) |
| 507 |
{ |
{ |
| 508 |
_eventSource.Cursor = Cursors.None; |
_eventSource.Cursor = Cursors.None; |
| 509 |
|
|
| 510 |
RotateObject(currentPosition); |
RotateObject(currentPosition); |
| 511 |
} |
} |
| 512 |
else if (isPanObject) |
} |
| 513 |
|
else if (Mouse.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) |
| 514 |
|
{ |
| 515 |
|
if (isTracking) |
| 516 |
{ |
{ |
| 517 |
_eventSource.Cursor = Cursors.None; |
_eventSource.Cursor = Cursors.None; |
| 518 |
|
isPanningObject = true; |
| 519 |
PanObject(currentPosition); |
PanObject(currentPosition); |
| 520 |
} |
} |
| 521 |
else if (isLookAround) |
} |
| 522 |
|
else if (Mouse.LeftButton == MouseButtonState.Pressed) |
| 523 |
{ |
{ |
| 524 |
LookAround(currentPosition); |
LookAround(currentPosition); |
| 525 |
} |
} |
| 526 |
|
|
| 527 |
} |
} |
| 528 |
|
|
| 529 |
_previousPosition2D = currentPosition; |
_previousPosition2D = currentPosition; |
| 530 |
|
|
| 531 |
}*/ |
} |
|
|
|
| 532 |
|
|
| 533 |
|
*/ |
| 534 |
} |
} |
| 535 |
|
|
| 536 |
|
|