WPF中3D旋转的实现
2010-04-27 14:11
246 查看
关于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;
}
《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第四弹:用Viewport2DVisual3D实现3D旋转效果
- 好玩的WPF第四弹:用Viewport2DVisual3D实现3D旋转效果
- Unity 3D 中实现对物体 位置(position) 旋转(rotation) 大小(scale) 的全面控制
- CATransform3D 矩阵变换之立方体旋转实现细节
- JS实现固定在网页右上角3D风格旋转文字
- WPF技术触屏上的应用系列(一): 3D 图片(照片)墙、柱面墙(凹面墙或者叫远景墙、凸面墙或者叫近景墙)实现
- WPF 3D 模型旋转
- Android自定义动画类——实现3D旋转动画
- 基于HT for Web矢量实现3D叶轮旋转
- 使用WPF实现3D场景[二]
- 具有3D旋转效果的图片组的一种实现
- Android Camera控制图旋转、倾斜 - 可用于实现伪3D效果
- Android实现3D旋转效果
- SilverLight用代码实现对任意控件向4个方向进行3D旋转的类
- Js_实现3D球体旋转
- Android自己定义组件系列【11】——实现3D立体旋转效果
- JS实现3D图片旋转展示效果代码
- unity 3d中简单巧妙的实现拖动鼠标旋转物体
- WPF特效-实现3D足球效果