1TrackBall模式Trackball模式是跟踪球模式,具体来说,就是物体的中心位置不变,而改变视点的位置,从而使模型随着鼠标的移动绕着中心位置旋转。对于鼠标移动需要计算相应的视点(照相机)的运动情况,在osg中,通过TrackballManipulator::calcMovement()来实现,并且对于不同的鼠标事件,分别进行如下的操作:1.按下鼠标的左键,则旋转照相机。2.按下鼠标中间键或者同时按下鼠标的左键和与右键,则改变物体的中心位置,也即对物体进行平移操作。3.按下鼠标的右键,则对模型进行缩放。在按下鼠标的左键从而旋转照相机时,这里主要模拟了一个跟踪球技术,球的半径理想情况下应该基于旋转中心到鼠标所指的物体上某点的距离,但是为了简化处理,一般情况下,球的半径TRACKBALLSIZE可以预先给定,也能达到比较好的模拟效果。具体实现过程如下:首先分别记录两次鼠标事件的坐标的(x1,y1)和(x2,y2),根据x坐标的最小值Xmin和最大值Xmax,以及y坐标的最小值Ymin和最大值Ymax,对(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中,由voidTrackballManipulator::computePosition(constosg::Vec3&eye,constosg::Vec3¢er,constosg::Vec3&up)实现。2.模型的旋转通过鼠标控制,因此需要判断鼠标是否移动,具体实现时,可以预先自定义一个速率v,分别记录下连续两次鼠标事件的时间和位置,计算出间隔的时间t和移动的距离s,如果sv*t,就可以认为鼠标移动,并根据鼠标的具体事件(如按下的是左键、右键等)进行相应的操作。在osg中,由boolTrackballManipulator::isMouseMoving()实现。3.需要有一个函数接收不同的鼠标和键盘事件,从而做出相应的处理。在osg中,由boolTrackballManipulator::handle(constGUIEventAdapter&ea,GUIActionAdapter&us)实现,如果事件被正确处理,返回true,否则返回false。其中GUIEventAdapter接受来自GUI的更新事件,GUIActionAdapter则是对这些GUI事件的回应。QuaternionThequaternionsaremembersofanoncommutativedivisionalgebrafirstinventedbyWilliamRowanHamilton.TheideaforquaternionsoccurredtohimwhilehewaswalkingalongtheRoyal2CanalonhiswaytoameetingoftheIrishAcademy,andHamiltonwassopleasedwithhisdiscoverythathescratchedthefundamentalformulaofquaternionalgebra,(1)intothestoneoftheBroughambridge(MishchenkoandSolovyov2000).Thesetofquaternionsisdenoted,H,or,andthequaternionsareasingleexampleofamoregeneralclassofhypercomplexnumbersdiscoveredbyHamilton.Whilethequaternionsarenotcommutative,theyareassociative,andtheyformagroupknownasthequaterniongroup.Byanalogywiththecomplexnumbersbeingrepresentableasasumofrealandimaginaryparts,,aquaternioncanalsobewrittenasalinearcombination(2)ThequaternionisimplementedasQuaternion[a,b,c,d]intheMathematicaadd-onpackageAlgebra`Quaternions`(whichcanbeloadedwiththecommandAlgebra`).Note,however,thatNonCommutativeMultiply(i.e.,**)mustbeusedformultiplicationoftheseobjectsratherthanusualmultiplication(i.e.,*).Thequaternionscanberepresentedusingcomplexmatrices(3)wherezandwarecomplexnumbers,a,b,c,anddarereal,andisthecomplexconjugateofz.AquaternioncanberepresentedusingQuaternion[a,b,c,d]intheMathematicaadd-onpackageAlgebra`Quaternions`(whichcanbeloadedwiththecommandAlgebra`),wherea,b,c,anddareexplicitrealnumbers.Quaternionscanalsoberepresentedusingthecomplexmatrices3(4)(5)(6)(7)(Arfken1985,p.185).Notethathereisusedtodenotetheidentitymatrix,not.ThematricesarecloselyrelatedtothePaulispinmatrices,,,combinedwiththeidentitymatrix.Fromtheabovedefinitions,itfollowsthat(8)(9)(10)Therefore,,andarethreeessentiallydifferentsolutionsofthematrixequation(11)whichcouldbeconsideredthesquarerootsofthenegativeidentitymatrix.AlinearcombinationofbasisquaternionswithintegercoefficientsissometimescalledaHamiltonianinteger.In,thebasisofthequaternionscanbegivenby(12)4(13)(14)(15)Thequaternionssatisfythefollowingidentities,sometimesknownasHamilton'srules,(16)(17)(18)(19)Theyhavethefollowingmultiplicationtable.1ijk11ijkii-1kjj-1ikkj-15Thequaternions±1,,,andformanon-Abeliangroupofordereight(withmultiplicationasthegroupoperation).Thequaternionscanbewrittenintheform(20)Thequaternionconjugateisgivenby(21)Thesumoftwoquaternionsisthen(22)andtheproductoftwoquaternionsis(23)Thequaternionnormisthereforedefinedby(24)Inthisnotation,thequaternionsarecloselyrelatedtofour-vectors.6Quaternionscanbeinterpretedasascalarplusavectorbywriting(25)where.Inthisnotation,quaternionmultiplicationhastheparticularlysimpleform(26)Divisionisuniquelydefined(exceptbyzero),soquaternionsformadivisionalgebra.Theinverseofaquaternionisgivenby(27)andthenormismultiplicative(28)Infact,theproductoftwoquaternionnormsimmediatelygivestheEulerfour-squareidentity.Arotationabouttheunitvectorbyananglecanbecomputedusingthequaternion(29)(Arvo1994,HearnandBaker1996).ThecomponentsofthisquaternionarecalledEulerparameters.Afterrotation,apointisthengivenby7(30)since.Aconcatenationoftworotations,firstandthen,canbecomputedusingtheidentity给定一个单位四元数kajaiaa4321,它对应一个旋转,该旋转角度1arccos2a,旋转轴为242322432/),,(aaaaaa即由),,(432aaa对应的单位向量。给定义一个旋转角度,单位旋转轴n,其对应一个四元数为))21sin(),21(cos(n。给出单位四元数和旋转的一一对应,下面将说明旋转的合成和旋转的结果都可以由四元数的运算来得到。1.旋转结果由四元数来计算任给一个向量),,(zyx,其绕单位轴n旋转后的结果向量)',',(zyx是什么呢?设),,(zyx对应一个四元数为zkyjxip0,)',',(zyx对应的四元数为kzjyixp'''0',而旋转对应的四元数为q,则qqpp',其中q为q的共轭。2.旋转的合成由四元数来计算给一个向量,它先绕单位轴1n旋转角度1,再绕单位轴2n旋转角度2,则两次旋转可以由两个四元数的乘积来得到。绕任意一个轴旋转,对应的矩阵如下:8rotationmatrixbecomes(12)wheretheelementsofthematrixare(13)Here,Einsteinsummationhasbeenused,istheKroneckerdelta,andisthepermutationsymbol.Writtenoutexplicitly,thematri