分享

WPF中3D旋转的实现

 xyjackxjw 2013-05-09

WPF中3D旋转的实现

分类: WPF 3D3365人阅读评论(3)收藏举报

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

《Rotating the Camera with the Mouse》

http:///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;
        }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多