WPF中3D旋转的实现

本文详细介绍了3D旋转的基本原理及其在计算机图形学中的应用。通过解析《Rotating the Camera with the Mouse》一文,深入浅出地阐述了如何使用鼠标操作进行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;
        }

评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值