• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當(dāng)自強(qiáng)而不息

            設(shè)計(jì)一個(gè)靈活的Camera類(lèi)(2)

            12.2.2圍繞任意軸旋轉(zhuǎn)

            為了實(shí)現(xiàn)我們的攝像機(jī)旋轉(zhuǎn)方法,我們需要能夠繞著任意軸旋轉(zhuǎn)D3DX庫(kù)提供下面的函數(shù)來(lái)解決這個(gè)問(wèn)題:

            Builds a matrix that rotates around an arbitrary axis.

            D3DXMATRIX * D3DXMatrixRotationAxis(  D3DXMATRIX * pOut,  CONST D3DXVECTOR3 * pV,  FLOAT Angle);

            Parameters

            pOut
            [in, out] Pointer to the D3DXMATRIX structure that     is the result of the operation.
               
            pV
            [in] Pointer to the arbitrary axis. See     D3DXVECTOR3.
               
            Angle
            [in] Angle of rotation in radians. Angles are     measured clockwise when looking along the rotation axis toward the origin.   

            Return Values

            Pointer to a D3DXMATRIX structure rotated around the specified axis.

            Remarks

            The return value for this function is the same value returned in the pOut parameter. In this way, the D3DXMatrixRotationAxis function can be used as a parameter for another function.

            例如,假如我們想繞向量(0.707, 0.707, 0)軸旋轉(zhuǎn)π/2角度。我們可以這樣寫(xiě):

                           
                   

                    D3DXMATRIX R;

                   

                    D3DXVECTOR3 axis(0.707f,         0.707f, 0.0f);

                   

                    D3DXMatrixRotationAxis(&R,         &axis, D3DX_PI / 2.0f);

             

            12.2.3PitchYawRoll

            因?yàn)榉较蛳蛄棵枋隽藬z像機(jī)相對(duì)于世界坐標(biāo)系的方向,我們必須考慮在使用傾斜(pitch)、偏航(yaw)和滾轉(zhuǎn)(roll)時(shí)及時(shí)更新方向向量。這其實(shí)也是非常簡(jiǎn)單的。圖12.412.512.6分別顯示了攝像機(jī)的傾斜、偏航和滾轉(zhuǎn)操作。

            當(dāng)傾斜(pitch)時(shí),我們需要將uplook向量繞著right向量旋轉(zhuǎn)一定角度。同樣的,當(dāng)偏航(yaw)時(shí),我們需要將lookright向量繞著up向量旋轉(zhuǎn)一定角度。最后,當(dāng)滾轉(zhuǎn)(roll)時(shí),我們需要將upright向量繞著look向量旋轉(zhuǎn)一定角度。

            我們現(xiàn)在明白了為什么D3DXMatrixRotationAxis函數(shù)是非常必要的,因?yàn)檫@三個(gè)向量中的任何一個(gè)都可能?chē)@世界坐標(biāo)系中的任意軸旋轉(zhuǎn)。

            對(duì)于傾斜(pitch)、偏航(yaw)和滾轉(zhuǎn)(roll)的執(zhí)行我們已經(jīng)討論了。然而,對(duì)于LAND_OBJECT模式就有一些限制。我們?cè)?/span>偏航(yaw)方法中只圍繞y軸旋轉(zhuǎn),我們完全屏蔽滾轉(zhuǎn)(roll)。當(dāng)然你可以根據(jù)你的程序需要來(lái)改變Camera類(lèi)。我們這里只是一個(gè)示例而已。

            傾斜(pitch)、偏航(yaw)和滾轉(zhuǎn)(roll)方法代碼的具體實(shí)現(xiàn)如下:

                void cCamera::pitch(float angle)
                {
                    D3DXMATRIX transform_matrix;
                    D3DXMatrixRotationAxis(&transform_matrix, &m_right, angle);
               
                    
            // rotate m_up and m_look around m_right vector
               
                    D3DXVec3TransformCoord(&m_up,   &m_up,   &transform_matrix);
                    D3DXVec3TransformCoord(&m_look, &m_look, &transform_matrix);
                }
               
               
            void cCamera::yaw(float angle)
                {
                    D3DXMATRIX transform_matrix;
               
                    
            // rotate around world y-axis (0, 1, 0) always for land object
               
                if(m_camera_type == LAND_OBJECT)
                        D3DXMatrixRotationY(&transform_matrix, angle);
                    
            else    // rotate around own up vector for aircraft
               
                        D3DXMatrixRotationAxis(&transform_matrix, &m_up, angle);
               
                    
            // rotate m_right and m_look around m_up or y-axis
               
                    D3DXVec3TransformCoord(&m_right, &m_right, &transform_matrix);
                    D3DXVec3TransformCoord(&m_look,  &m_look,  &transform_matrix);
                }
               
               
            void cCamera::roll(float angle)
                {
                    
            // only roll for aircraft type
               
                if(m_camera_type == AIR_CRAFT)
                    {
                        D3DXMATRIX transform_matrix;
                        D3DXMatrixRotationAxis(&transform_matrix, &m_look, angle);
               
                        
            // rotate m_up and m_right around m_look vector
               
                        D3DXVec3TransformCoord(&m_up,     &m_up,        &transform_matrix);
                        D3DXVec3TransformCoord(&m_right, &m_right,  &transform_matrix);
                    }
                }

            12.2.4WalkingStrafingFlying

            當(dāng)提到walking時(shí),我們的意思是在我們觀察的方向上移動(dòng)位置(也就是說(shuō),沿著look向量)。Strafing是說(shuō)在我們觀察方向的左右移動(dòng),也就是沿著right向量移動(dòng)。最后,我們說(shuō)flying就是沿著up向量移動(dòng)。為了沿著這些軸移動(dòng),我們只需要簡(jiǎn)單地加一個(gè)向量就可以了(如圖12.7)。

            就象旋轉(zhuǎn)一樣,我們需要對(duì)移動(dòng)作一些限制。例如,LAND_OBJECT不允許飛起來(lái)。因此我們把移動(dòng)限制在xz平面。然而,因?yàn)?span lang="EN-US">LAND_OBJECT能夠允許爬樓梯和登山,所以,我們?cè)O(shè)置cCamera::set_position方法,它允許你手動(dòng)設(shè)置你的攝像機(jī)位置來(lái)達(dá)到你的高度和位置。

            移動(dòng)(walk)、平移(strafe)和飛行(fly)方法代碼的具體實(shí)現(xiàn)如下:

                void cCamera::walk(float units)
                {
                    
            // move only on xz plane for land object
               
                if(m_camera_type == LAND_OBJECT)
                        m_pos += D3DXVECTOR3(m_look.x, 0.0f, m_look.z) * units;
                    
            else    // AIR_CRAFT
               
                    m_pos += m_look * units;
                }
               
               
            void cCamera::strafe(float units)
                {
                    
            // move only on xz plane for land object
               
                if(m_camera_type == LAND_OBJECT)
                        m_pos += D3DXVECTOR3(m_right.x, 0.0f, m_right.z) * units;
                    
            else    // AIR_CRAFT
               
                    m_pos += m_right * units;
                }
               
               
            void cCamera::fly(float units)
                {
                    
            // move only on y_axis for land object
               
                if(m_camera_type == LAND_OBJECT)
                        m_pos.y += units;
                    
            else
                        m_pos += m_up * units;
                }

            posted on 2008-03-30 15:07 lovedday 閱讀(1972) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            公告

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            隨筆分類(lèi)(178)

            3D游戲編程相關(guān)鏈接

            搜索

            最新評(píng)論

            午夜精品久久久久久久久| 日本久久久久亚洲中字幕| 精品乱码久久久久久夜夜嗨| 国产精品久久久久久久久鸭| 久久久久国产精品嫩草影院| 18岁日韩内射颜射午夜久久成人| 精品久久久无码人妻中文字幕豆芽 | 91精品国产色综久久| 欧洲性大片xxxxx久久久| 亚洲va久久久噜噜噜久久狠狠 | 99国产欧美久久久精品蜜芽| 久久996热精品xxxx| 香蕉久久夜色精品升级完成| 久久露脸国产精品| 久久精品www人人爽人人| 人妻丰满?V无码久久不卡| 国产激情久久久久影院老熟女免费 | 伊色综合久久之综合久久| 69久久精品无码一区二区| 久久无码国产专区精品| 久久精品夜色噜噜亚洲A∨ | 亚洲午夜精品久久久久久浪潮| 日本欧美久久久久免费播放网| 国产成人综合久久精品红| 久久九九全国免费| 久久精品人成免费| 亚洲色欲久久久综合网| 色综合久久无码五十路人妻| 中文字幕无码久久精品青草 | 国产免费久久精品99re丫y| 狠狠精品久久久无码中文字幕 | 亚洲精品白浆高清久久久久久| 久久精品国产亚洲av麻豆图片 | 97精品国产91久久久久久| 久久精品一本到99热免费| 亚洲国产小视频精品久久久三级| 香蕉99久久国产综合精品宅男自 | 亚洲中文字幕无码久久2017| 久久99久国产麻精品66| 国产午夜福利精品久久2021| 国产精品9999久久久久|