TrackBall模式Trackball模式是跟踪球模式,具体来说,就是物体的中心位置不变,而改变视点的位置,从而使模型随着鼠标的移动绕着中心位置旋转。
对于鼠标移动需要计算相应的视点(照相机)的运动情况,在osg中,通过TrackballManipulator::calcMovement()来实现,并且对于不同的鼠标事件,分别进行如下的操作:1.按下鼠标的左键,则旋转照相机。
2.按下鼠标中间键或者同时按下鼠标的左键和与右键,则改变物体的中心位置,也即对物体进行平移操作。
3.按下鼠标的右键,则对模型进行缩放。
在按下鼠标的左键从而旋转照相机时,这里主要模拟了一个跟踪球技术,球的半径理想情况下应该基于旋转中心到鼠标所指的物体上某点的距离,但是为了简化处理,一般情况下,球的半径TRACKBALLSIZE可以预先给定,也能达到比较好的模拟效果。
具体实现过程如下:首先分别记录两次鼠标事件的坐标的(x1,y1)和(x2,y2),根据x坐标的最小值X min和最大值X max,以及y坐标的最小值Y min和最大值Y max,对(x1,y1)和(x2,y2)坐标进行规范化,转化到(-1,1)之间,不妨假设新的坐标点为p1和p2,然后通过将p1和p2投影到跟踪球上,从而获取相应的z 坐标。
这样,以球的中心为起始点,计算出的球上的两个点为终点,可以确定两个向量,不妨记作P1和P2,那么旋转轴axis = P2^ P1,旋转的弧度为:t = ||(P2- P1)|| / (2.0*TRACKBALLSIZE),为了避免产生某些不可操纵的结果,必须先将t规范到(-1,1)之间,再将其转化为角度angles,然后将照相机绕着axis旋转angles的角度,从而实现了对照相机的旋转操作。
在osg的实现中,该跟踪球并不是一个正规的球体,而是进行了一定的变形,在中心附近是一个半径为TRACKBALLSIZE的球体,而在离中心达到一定距离时(小于球的半径TRACKBALLSIZE),该球体被扭曲成了四个双曲面。
在Trackball模式的具体实现时,有许多细节问题,如1.首先需要将世界坐标系转化到视点坐标系,这可以通过先将中心平移到视点后根据照相机的up、物体的中心以及视点重新确立坐标系。
在osg中,由void TrackballManipulator::computePosition(const osg::Vec3& eye,const osg::Vec3& center,const osg::Vec3& up)实现。
2.模型的旋转通过鼠标控制,因此需要判断鼠标是否移动,具体实现时,可以预先自定义一个速率v,分别记录下连续两次鼠标事件的时间和位置,计算出间隔的时间∆t和移动的距离∆s,如果∆s > v * ∆t,就可以认为鼠标移动,并根据鼠标的具体事件(如按下的是左键、右键等)进行相应的操作。
在osg中,由bool TrackballManipulator::isMouseMoving()实现。
3.需要有一个函数接收不同的鼠标和键盘事件,从而做出相应的处理。
在osg中,由bool TrackballManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us)实现,如果事件被正确处理,返回true,否则返回false。
其中GUIEventAdapter接受来自GUI的更新事件,GUIActionAdapter则是对这些GUI事件的回应。
QuaternionThe quaternions are members of a noncommutative division algebra first invented by WilliamRowan Hamilton. The idea for quaternions occurred to him while he was walking along the RoyalCanal on his way to a meeting of the Irish Academy, and Hamilton was so pleased with his discovery that he scratched the fundamental formula of quaternion algebra,(1)into the stone of the Brougham bridge (Mishchenko and Solovyov 2000). The set of quaternions isdenoted , H, or , and the quaternions are a single example of a more general class of hypercomplex numbers discovered by Hamilton. While the quaternions are not commutative, they are associative, and they form a group known as the quaternion group.By analogy with the complex numbers being representable as a sum of real and imaginary parts,, a quaternion can also be written as a linear combination(2)The quaternion is implemented as Quaternion[a, b, c, d] in the Mathematica add-on package Algebra`Quaternions` (which can be loaded with the command<<Algebra`). Note, however, that NonCommutativeMultiply (i.e., **) must be used for multiplication of these objects rather than usual multiplication (i.e., *).The quaternions can be represented using complex matrices(3)where z and w are complex numbers, a, b, c, and d are real, and is the complex conjugate of z.A quaternion can be represented using Quaternion[a, b, c, d] in the Mathematica add-on package Algebra`Quaternions` (which can be loaded with the command <<Algebra`), where a, b, c, and d are explicit real numbers.Quaternions can also be represented using the complex matrices(4)(5)(6)(7)(Arfken 1985, p. 185). Note that here is used to denote the identity matrix, not . The matrices are closely related to the Pauli spin matrices,, , combined with the identity matrix. From the above definitions, it follows that(8)(9)(10)Therefore,, and are three essentially different solutions of the matrix equation(11)which could be considered the square roots of the negative identity matrix. A linear combination of basis quaternions with integer coefficients is sometimes called a Hamiltonian integer.In , the basis of the quaternions can be given by(12)(13)(14)(15) The quaternions satisfy the following identities, sometimes known as Hamilton's rules,(16)(17)(18)(19) They have the following multiplication table.The quaternions ± 1, , , and form a non-Abelian group of order eight (with multiplication as the group operation).The quaternions can be written in the form(20)The quaternion conjugate is given by(21)The sum of two quaternions is then(22)and the product of two quaternions is(23) The quaternion norm is therefore defined by(24)In this notation, the quaternions are closely related to four-vectors.Quaternions can be interpreted as a scalar plus a vector by writing(25)where . In this notation, quaternion multiplication has the particularly simple form(26)Division is uniquely defined (except by zero), so quaternions form a division algebra. The inverse of a quaternion is given by(27)and the norm is multiplicative(28)In fact, the product of two quaternion norms immediately gives the Euler four-square identity.A rotation about the unit vector by an angle can be computed using the quaternion(29) (Arvo 1994, Hearn and Baker 1996). The components of this quaternion are called Eulerparameters. After rotation, a point is then given by(30)since . A concatenation of two rotations, first and then , can be computed usingthe identity给定一个单位四元数k a j a i a a 4321+++,它对应一个旋转,该旋转角度1arccos 2a =θ,旋转轴为242322432/),,(a a a a a a ++即由),,(432a a a 对应的单位向量。