原文: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;
        }

最新文章

  1. Spring 4 + Reactor Integration Example--转
  2. springboot 的dataSource 一些配置
  3. android 发送GET请求 服务端接收乱码的问题
  4. 《深入理解Windows Phone 8.1 UI控件编程》基于最新的Runtime框架
  5. PHP小记录
  6. Java静态类
  7. WisDom.Net 框架设计(二) 服务总线
  8. bzoj3208
  9. Solr导入数据库数据
  10. Sqlserver系列(二) 模糊查询 like
  11. SQL Server 2008中文企业版下载地址和序列号[转]
  12. 数组的map方法
  13. POI读取excel工具类 返回实体bean集合(xls,xlsx通用)
  14. java.lang.reflect.InvocationTargetException
  15. Python并发编程之消息队列补充及如何创建线程池(六)
  16. 1120 机器人走方格 V3(组合数)
  17. R绘图 第八篇:绘制饼图(ggplot2)
  18. ARM 平台下的 SSHD 配置
  19. valgrind- 内存泄漏-how to install and use
  20. dubbo实战

热门文章

  1. UVA 10106 Product (大数相乘)
  2. (二)RabbitMQ消息队列-RabbitMQ消息队列架构与基本概念
  3. struts2-token防止重复提交解决办法
  4. Perl读写Excel简单操作
  5. [HTTP] Understand 2xx HTTP Status Code Responses
  6. ios开发swift学习第三天:逻辑分支
  7. jquery修改获取radio的选中项
  8. 用SQL找出前N名
  9. JNI_Android 项目中调用.so动态库实现详解
  10. oracle 复制表数据,复制表结构