WPF中3D旋转的实现

Posted lonelyxmas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF中3D旋转的实现相关的知识,希望对你有一定的参考价值。

原文:WPF中3D旋转的实现

关于3D旋转的原理,请看Daniel Lehenbauer的文章

《Rotating the Camera with the Mouse》

http://viewport3d.com/trackball.htm

?

里面非常清楚的讲解了原理和方法,很受用。

?

相关代码:

?

2.1 Finding the Point on the Sphere

private Vector3D ProjectToTrackball(Double width, Double height, Point point)
??????? {
??????????? Double x = point.X / (width / 2);??? // Scale so bounds map to [0,0] - [2,2]
??????????? Double y = point.Y / (height / 2);

??????????? x = x - 1;?????????????????????????? // Translate 0,0 to the center
??????????? y = 1 - y;?????????????????????????? // Flip so +Y is up instead of down

??????????? Double z2 = 1 - x * x - y * y;?????? // z^2 = 1 - x^2 - y^2
??????????? Double z = z2 > 0 ? Math.Sqrt(z2) : 0;
??????????? return new Vector3D(x, y, z);
??????? }

?

2.2 Rotating Between the Points

??????? private void Rotate(Point currentPosition)
??????? {
??????????? Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition);

??????????? Vector3D axis = Vector3D.CrossProduct(_previousRotPosition3D, currentPosition3D);
??????????? Double angle = Vector3D.AngleBetween(_previousRotPosition3D, currentPosition3D);

??????????? Rotate(axis, angle);

??????????? _previousRotPosition3D = currentPosition3D;
??????? }

?

??????? private void Rotate(Vector3D axis, Double angle)
??????? {
??????????? Quaternion delta = new Quaternion(axis, -angle * _rotScale);

??????????? Quaternion q = new Quaternion(_axisAngleRotation3D.Axis, _axisAngleRotation3D.Angle);

??????????? q *= delta;

??????????? Vector3D zeorVec = new Vector3D(0.0, 0.0, 0.0);
??????????? if (Vector3D.Equals(q.Axis, zeorVec))
??????????????? return;

??????????? _axisAngleRotation3D.Axis = q.Axis;
??????????? _axisAngleRotation3D.Angle = q.Angle;
??????? }


















以上是关于WPF中3D旋转的实现的主要内容,如果未能解决你的问题,请参考以下文章

WPF中使用TranslateTransform3D修改CAD的3D旋转中心

WPF 画一个3D矩形并旋转

如何实现WPF窗体旋转

WPF能做出3D窗体吗?(比如一个旋转的地球,透明又闪光,很炫的那种)

在WPF中将图片转换成3D图像并可以旋转

请问WPF在Blend 4中怎么实现对无边框的窗体拖动。。。会请详细说下。会额外多加50分。。