




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、TrackBall模式Trackball模式是跟蹤球模式,具體來說,就是物體的中心位置不變,而改變視點的位置,從而使模型隨著鼠標的移動繞著中心位置旋轉(zhuǎn)。對于鼠標移動需要計算相應(yīng)的視點(照相機)的運動情況,在osg中,通過TrackballManipulator:calcMovement()來實現(xiàn),并且對于不同的鼠標事件,分別進行如下的操作:1 按下鼠標的左鍵,則旋轉(zhuǎn)照相機。2 按下鼠標中間鍵或者同時按下鼠標的左鍵和與右鍵,則改變物體的中心位置,也即對物體進行平移操作。3 按下鼠標的右鍵,則對模型進行縮放。在按下鼠標的左鍵從而旋轉(zhuǎn)照相機時,這里主要模擬了一個跟蹤球技術(shù),球的半徑理想情況下應(yīng)該基于
2、旋轉(zhuǎn)中心到鼠標所指的物體上某點的距離,但是為了簡化處理,一般情況下,球的半徑TRACKBALLSIZE可以預(yù)先給定,也能達到比較好的模擬效果。具體實現(xiàn)過程如下:首先分別記錄兩次鼠標事件的坐標的(x1,y1)和(x2,y2),根據(jù)x坐標的最小值Xmin和最大值Xmax,以及y坐標的最小值Ymin和最大值Ymax,對(x1,y1)和(x2,y2)坐標進行規(guī)范化,轉(zhuǎn)化到(-1,1)之間,不妨假設(shè)新的坐標點為p1和p2,然后通過將p1和p2投影到跟蹤球上,從而獲取相應(yīng)的z坐標。這樣,以球的中心為起始點,計算出的球上的兩個點為終點,可以確定兩個向量,不妨記作P1和P2,那么旋轉(zhuǎn)軸axis = P2 P1
3、,旋轉(zhuǎn)的弧度為:t = |(P2- P1)| / (2.0*TRACKBALLSIZE),為了避免產(chǎn)生某些不可操縱的結(jié)果,必須先將t規(guī)范到(-1,1)之間,再將其轉(zhuǎn)化為角度angles,然后將照相機繞著axis旋轉(zhuǎn)angles的角度,從而實現(xiàn)了對照相機的旋轉(zhuǎn)操作。在osg的實現(xiàn)中,該跟蹤球并不是一個正規(guī)的球體,而是進行了一定的變形,在中心附近是一個半徑為TRACKBALLSIZE的球體,而在離中心達到一定距離時(小于球的半徑TRACKBALLSIZE),該球體被扭曲成了四個雙曲面。在Trackball模式的具體實現(xiàn)時,有許多細節(jié)問題,如1首先需要將世界坐標系轉(zhuǎn)化到視點坐標系,這可以通過先將中心
4、平移到視點后根據(jù)照相機的up、物體的中心以及視點重新確立坐標系。在osg中,由void TrackballManipulator:computePosition(const osg:Vec3& eye,const osg:Vec3& center,const osg:Vec3& up)實現(xiàn)。2模型的旋轉(zhuǎn)通過鼠標控制,因此需要判斷鼠標是否移動,具體實現(xiàn)時,可以預(yù)先自定義一個速率v,分別記錄下連續(xù)兩次鼠標事件的時間和位置,計算出間隔的時間t和移動的距離s,如果s v * t,就可以認為鼠標移動,并根據(jù)鼠標的具體事件(如按下的是左鍵、右鍵等)進行相應(yīng)的操作。在osg中,由bool Trackball
5、Manipulator:isMouseMoving()實現(xiàn)。3需要有一個函數(shù)接收不同的鼠標和鍵盤事件,從而做出相應(yīng)的處理。在osg中,由bool TrackballManipulator:handle(const GUIEventAdapter& ea,GUIActionAdapter& us) 實現(xiàn),如果事件被正確處理,返回true,否則返回false。其中GUIEventAdapter接受來自GUI的更新事件,GUIActionAdapter則是對這些GUI事件的回應(yīng)。QuaternionThe quaternions are members of a noncommutative div
6、ision algebra first invented by William Rowan Hamilton. The idea for quaternions occurred to him while he was walking along the Royal Canal 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
7、)into the stone of the Brougham bridge (Mishchenko and Solovyov 2000). The set of quaternions is denoted , 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 the
8、y 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 Quaterniona, b, c, d in the Mathematica add-on package AlgebraQua
9、ternions (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
10、, b, c, and d are real, and is the complex conjugate of z. A quaternion can be represented using Quaterniona, b, c, d in the Mathematica add-on package AlgebraQuaternions (which can be loaded with the command Algebra), where a, b, c, and d are explicit real numbers. Quaternions can also be represent
11、ed 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
12、 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
13、by (12)(13)(14)(15)The quaternions satisfy the following identities, sometimes known as Hamiltons rules, (16)(17)(18)(19)They have the following multiplication table. 1ijk11ijkii-1kjj-1ikkj-1The quaternions 1, , , and form a non-Abelian group of order eight (with multiplication as the group operatio
14、n). 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. Quaterni
15、ons 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
16、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 Euler parameters. Af
17、ter rotation, a point is then given by (30)since . A concatenation of two rotations, first and then , can be computed using the identity 給定一個單位四元數(shù),它對應(yīng)一個旋轉(zhuǎn),該旋轉(zhuǎn)角度,旋轉(zhuǎn)軸為即由對應(yīng)的單位向量。給定義一個旋轉(zhuǎn)角度,單位旋轉(zhuǎn)軸,其對應(yīng)一個四元數(shù)為。給出單位四元數(shù)和旋轉(zhuǎn)的一一對應(yīng),下面將說明旋轉(zhuǎn)的合成和旋轉(zhuǎn)的結(jié)果都可以由四元數(shù)的運算來得到。1 旋轉(zhuǎn)結(jié)果由四元數(shù)來計算任給一個向量,其繞單位軸旋轉(zhuǎn)后的結(jié)果向量是什么呢?設(shè)對應(yīng)一個四元數(shù)為,對應(yīng)的四元
18、數(shù)為,而旋轉(zhuǎn)對應(yīng)的四元數(shù)為,則,其中為的共軛。2 旋轉(zhuǎn)的合成由四元數(shù)來計算給一個向量,它先繞單位軸旋轉(zhuǎn)角度,再繞單位軸旋轉(zhuǎn)角度,則兩次旋轉(zhuǎn)可以由兩個四元數(shù)的乘積來得到。繞任意一個軸旋轉(zhuǎn),對應(yīng)的矩陣如下:rotation matrix becomes (12)where the elements of the matrix are (13)Here, Einstein summation has been used, is the Kronecker delta, and is the permutation symbol. Written out explicitly, the matrix
19、elements are (14)(15)(16)(17)(18)(19)(20)(21)其中就是旋轉(zhuǎn)對應(yīng)的四元數(shù)。用矢量形式來表示旋轉(zhuǎn):Rotation FormulaA formula which transforms a given coordinate system by rotating it through a counterclockwise angle about an axis . This formula is used implicitly to transform objects in VRML (virtual reality markup language) usi
20、ng the command Rotation angle nx ny nz Phi. Referring to the above figure (Goldstein 1980), the equation for the fixed vector in the transformed coordinate system (i.e., the above figure corresponds to an alias transformation), is (1)(2)(3)(Goldstein 1980; Varshalovich et al. 1988, p.24). The angle
21、and unit normal may also be expressed as Euler angles. In terms of the Euler parameters, DriveManipulator結(jié)構(gòu)原理及功能說明當打開場景,按鍵盤“3”鍵是,場景處于DriveManipulator漫游模式狀態(tài),形象的理解:處于DriveManipulator漫游模式下,就好象一個虛擬人在場景里面行走一樣。一類結(jié)構(gòu)SpeedControlMode: enum SpeedControlMode USE_MOUSE_Y_FOR_SPEED, USE_MOUSE_BUTTONS_FOR_SPEED ;
22、用戶按鍵盤“A”鍵時,使用USE_MOUSE_BUTTONS_FOR_SPEED模式,在該模式漫游時,鼠標左鍵為加速鍵;中鍵按下為停止漫游,或者左右鍵同時按下;鼠標右鍵為減速。用戶如果按鍵盤“Q”鍵時,使用USE_MOUSE_Y_FOR_SPEED模式,在這種模式下漫游,鼠標左、中、右鍵功能相同,向前為加速、向后為減速。void addMouseEvent(const GUIEventAdapter& ea) osg:ref_ptr _ga_t1; osg:ref_ptr _ga_t0;系統(tǒng)定義兩個GUIEventAdapter對象,相當于雙緩沖功能DriveManipulator:calcM
23、ovement()1 判斷有沒有鼠標事件2 判斷速度控制模式,默認為USE_MOUSE_BUTTONS_FOR_SPEED模式3 確定照相機的方向4 完成碰撞檢測virtual const char* className() const return Drive; 繼承基類方法,得到類的名稱DriveManipulator:computePosition(const osg:Vec3d& eye,const osg:Vec3d& center,const osg:Vec3d& up) 視圖變換,確定照相機的位置和方向DriveManipulator:DriveManipulator()構(gòu)造函數(shù)
24、,主要初始化漫游初始速度、漫游結(jié)點的高度、相機結(jié)點的包圍盒以及漫游速度控制模式DriveManipulator:flushMouseEventStack() 清空鼠標事件堆棧、也即使osg:ref_ptr _ga_t1;osg:ref_ptr _ga_t0;指向空指針(_ga_t1 = NULL; _ga_t0 = NULL;)osg:Matrixd DriveManipulator:getInverseMatrix() const 獲得反轉(zhuǎn)矩陣,主要完成OSG和openGL之間的坐標轉(zhuǎn)換osg:Matrixd DriveManipulator:getMatrix() const 獲取視點矩陣
25、const osg:Node* DriveManipulator:getNode() const 獲取照相機的結(jié)點void DriveManipulator:getUsage(osg:ApplicationUsage& usage) const 獲取鼠標和鍵盤的操控使用說明bool DriveManipulator:handle(const GUIEventAdapter& ea,GUIActionAdapter& us)handle方法主要獲取外來事件響應(yīng)相應(yīng)的操作,在OSG里主要有以下處理事件:PUSH:當鼠標鍵推動時,實現(xiàn)場景的漫游RELEASE:繼續(xù)PUSH后的操作,實現(xiàn)場景漫游DRA
26、G:功能和PUSH 一樣MOVE:功能和PUSH 一樣KEYDOWN:主要獲取空格、A、Q鍵,空格鍵按下、調(diào)用home()方法、回到場景的初始狀態(tài)、清空事件堆棧,禁止連續(xù)更新場景;A鍵按下,使用USE_MOUSE_BUTTONS_FOR_SPEED模式;Q鍵按下,使用USE_MOUSE_Y_FOR_SPEED模式。FRAME:主要更新場景RESIZE:當窗口大小變化時的響應(yīng)狀態(tài),起功能和home()相似,唯一不同,相機結(jié)點不要重新獲取 void DriveManipulator:home(const GUIEventAdapter& ea,GUIActionAdapter& us) Drive
27、Manipulator:computeHomePosition()當用戶按空格鍵時,回到漫游的初時狀態(tài),主要完成下面事情:1 獲取結(jié)點,通過IntersecVisitor確定漫游結(jié)點在場景中的初始位置2 請求繪制場景3 重新獲取鼠標位置4 清空事件堆棧void DriveManipulator:init(const GUIEventAdapter& ea,GUIActionAdapter& us)當窗口大小變化時響應(yīng),主要完成下面事情:1 清空事件堆棧2 禁止場景連續(xù)更新3 通過IntersecVisitor初始化漫游結(jié)點在場景中的位置virtual void setByInverseMatr
28、ix(const osg:Matrixd& matrix) setByMatrix(osg:Matrixd:inverse(matrix); 設(shè)置反轉(zhuǎn)矩陣void DriveManipulator:setByMatrix(const osg:Matrixd& matrix) 設(shè)置矩陣void DriveManipulator:setNode(osg:Node* node) 獲取結(jié)點的包圍盒,提供給碰撞檢測屏幕的中心(0,0)點在中間。屏幕左下角最小,右上角最大。照相機新的的朝向,主要根據(jù)鼠標位置及照相機原有的朝向來變化。pitch,roll,yaw均是作用于改變_ratation的值,即改變照
29、相機的朝向,而物體的移動方向是照相機移動方向的反方向。其中sv,lv,up即對于照相機的局部坐標系。Pitch:可以形象的認為是抬頭和低頭,根據(jù)鼠標的Y值大小,決定pitch正負。 double pitch = -inDegrees(dy*75.0f*dt); pitch_rotate.makeRotate(pitch,sv.x(),sv.y(),sv.z();/ 繞sv旋轉(zhuǎn)一個角度鼠標在中心上方,Y值為正,pitch為負,照相機鏡頭向下轉(zhuǎn)(逆向),表現(xiàn)為物體向上轉(zhuǎn)(正向)。鼠標在中心下方,Y值為負,pitch為正,照相機鏡頭向上轉(zhuǎn)(正向),表現(xiàn)為物體向下(逆向)。Roll:照相機的側(cè)身。根據(jù)
30、鼠標的X值大小,決定roll正負。 double roll = inDegrees(dx*50.0f*dt);roll_rotate.makeRotate(roll,lv.x(),lv.y(),lv.z();/繞lv旋轉(zhuǎn)一個角度鼠標在中心右方,X值為正,roll為正,照相機鏡頭向右轉(zhuǎn),表現(xiàn)為物體向左轉(zhuǎn)。鼠標在中心左方,X值為負,roll為負,照相機鏡頭向左轉(zhuǎn),表現(xiàn)為物體向右轉(zhuǎn)。Yaw: 照相機的轉(zhuǎn)角。根據(jù)照相機原有的自身坐標系sv,lv,up,決定yaw的正負,繞著Z軸轉(zhuǎn)。 double bank = asinf(sv *getUpVector(cf); double yaw = inRadi
31、ans(bank)*dt; osg:Quat yaw_rotate; yaw_rotate.makeRotate(yaw,getUpVector(cf);/注意:這里是繞著osg的z軸轉(zhuǎn)當照相機的自身坐標系sv(x)方向與osg的z軸夾角為銳角,bank值為正,照相機繞Z軸正向轉(zhuǎn),表現(xiàn)為物體從右面(逆向)繞到照相機后面去。 若為鈍角,則相反。Yaw的改變是由sv的改變而引起的,sv方向的改變是由于roll的變化引起的,pitch的改變不影響yaw的變化。所以表現(xiàn)為:若yaw有效,當照相機側(cè)身時,會同時轉(zhuǎn)身。 lv *= (_velocity*dt);_velocity是在lv方向上的,即照相機
32、(視點位置)移動在lv方向上。_rotation變化,首先pitch,再roll,最后yaw,即照相機局部坐標系(sv,lv,up)在改變。另外,即使鼠標不動,dt也是有值的,即兩幀之間的刷新時間,所以只要鼠標不在(0,)點,就算鼠標不動,物體也會移動。問題:為什么在判斷鼠標事件的時候用的是ga_t1? unsigned int buttonMask = _ga_t1-getButtonMask();Flight模式當打開場景,按鍵盤“2”鍵切換到Flight漫游模式狀態(tài),采用Flight模式就像駕駛飛機觀察場景,默認情況下:1 按下鼠標左鍵,相機加速向前移動2 按下鼠標右鍵,相機減速,當速度
33、等于0時相機開始向后倒退3 按下鼠標中間鍵 或者鼠標左右鍵同時按下為停止。工作流程:一、運行程序時首先調(diào)用各個漫游模式的構(gòu)造函數(shù)和setNode()函數(shù),進行初始化,獲取結(jié)點的包圍盒,二、當切換到Flight模式時:首先,調(diào)用setByMatrix()通過該函數(shù)的參數(shù)設(shè)置視點位置,確定相機的視圖矩陣(主要體現(xiàn)在旋轉(zhuǎn)角度上)。接著,調(diào)用init()進行初始化,設(shè)置漫游初始速度,清空事件堆棧,設(shè)置鼠標初始位置等,通過handle()方法捕獲鼠標和鍵盤事件、根據(jù)相應(yīng)的事件來確定相機的移動方式,主要工作的流程:1 確定照相機的初始位置和方向,初始狀態(tài)下相機位于原點,相機朝向為Z的負方向,相機向上的方向
34、為Y的方向。2 漫游速度控制:通過對_velocity參數(shù)的控制實現(xiàn)對漫游速度和漫游方向的改變,利用_velocity += dt*(_acceleration+_velocity)控制漫游加速,dt為鼠標按下的持續(xù)時間,持續(xù)時間越長漫游速度越快;減速操作也是類似。由此可知,當_velocity0時,向著視線的方向行進;當_velocity0時,向視線相反的方向倒退;而其為0時,則相機靜止在場景中。 3 照相機旋轉(zhuǎn)角度控制:使用NO_AUTOMATIC_YAW模式,只計算相機的旋轉(zhuǎn)角度,不考慮飛行時的傾斜角度,使用YAW_AUTOMATICALLY_WHEN_BANKED模式,不僅需要計算相機
35、的旋轉(zhuǎn)角度,還要考慮飛行時的傾斜角度。4 相機移動檢測:通過判斷鼠標是否移動來決定相機是否移動,若進行了操作,如推,拉,更改窗口大小等操作后,由calcMovement()計算得相機已經(jīng)移動了,則照相機看到的為新的場景;5 隨時間不斷的改變視線(lv*=(_velocity*dt) 和視點_eye+=lv的位置,更新相機的旋轉(zhuǎn)角度。當按空格鍵時,調(diào)用home(),整個場景回到初始位置。一類結(jié)構(gòu)YawControlMode: enum YawControlMode YAW_AUTOMATICALLY_WHEN_BANKED, NO_AUTOMATIC_YAW ;用戶按鍵盤“A”鍵時,使用NO_A
36、UTOMATIC_YAW模式,只計算相機的旋轉(zhuǎn)角度,不考慮飛行時的傾斜角度。用戶如果按鍵盤“Q”鍵時,使用YAW_AUTOMATICALLY_WHEN_BANKED模式,不僅需要計算相機的旋轉(zhuǎn)角度,還要考慮飛行時的傾斜角度,該模式是默認的飛行漫游模式。void addMouseEvent(const GUIEventAdapter& ea) osg:ref_ptr _ga_t1; osg:ref_ptr _ga_t0;系統(tǒng)定義兩個GUIEventAdapter對象,相當于雙緩沖功能FlightManipulator:calcMovement()5 計算飛行時的漫游速度6 計算相機的旋轉(zhuǎn)角度,默認為YAW_AUTOMATICALLY_WHEN_BANKED模式7 確定照相機的方向FlightManipulator:computePosition(const osg:Vec3d& eye,const osg:Vec3d& center,const osg:Vec3d& up) 視圖變換,確定照相機的位置和方向FlightManipulator:FlightManipulator()構(gòu)造函數(shù),主要初始化漫游初始速度、相機結(jié)點的包圍盒以及相機視角模式FlightManipulator:flushMouseEventStack() 清空鼠標事件堆棧、也即使os
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- DB31/T 1349-2022機關(guān)會議服務(wù)管理規(guī)范
- DB31/T 1169-2019知識產(chǎn)權(quán)評議技術(shù)導(dǎo)則
- DB31/T 1142-2019燃氣工業(yè)鍋爐能效在線監(jiān)測技術(shù)規(guī)范
- DB31/T 1124-2018電梯應(yīng)急處置公共服務(wù)平臺功能要求
- 硬件設(shè)計中的節(jié)能技術(shù)與綠色標準考核試卷
- 2024年可降解聚烯烴專用料項目資金申請報告代可行性研究報告
- 2025年中國壁掛式浴室柜行業(yè)市場前景預(yù)測及投資價值評估分析報告
- 環(huán)保設(shè)施改造補充協(xié)議
- 網(wǎng)紅奶茶店區(qū)域代理加盟經(jīng)營合同
- 跨國醫(yī)療援助物資運輸與配送合同
- DBJ41-T311-2025 《人民防空節(jié)鎳型不銹鋼防護設(shè)備選用與安裝技術(shù)標準》
- 2025-2030年中國軍用機器人行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 2024年佛山市三水樂投控股有限公司招聘考試真題
- 新聞閱讀-2024年中考語文記敘文閱讀專項復(fù)習(原卷版)
- 2025-2030年電石項目投資價值分析報告
- 2025江蘇中天鋼鐵集團有限公司產(chǎn)品采購銷售合同
- 《演講與表達技巧》課件
- (四檢)泉州市2025屆高中畢業(yè)班適應(yīng)性練習卷生物試卷(含答案)
- 水務(wù)集團面試試題及答案
- 驛站場地安全管理制度
- 食堂材料領(lǐng)用管理制度
評論
0/150
提交評論