• <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>

            天行健 君子當自強而不息

            使用.x文件模型(3)

            通過四元數實現模型旋轉

            早在1843年,William Roman Hamilton爵士就已經發明了作為復數擴展的四元數(quaternion),但是直到1985年才有一個叫Ken ShoeMake的人在SIGGRAPH(Special Interest Group on Computer Graphics,美國計算機協會的圖形專業組)把四元數引入計算機圖形處理領域。

            四元數將三維空間中旋轉的概念擴展到四維空間,這對于表示和處理3D中點的旋轉很有用。四元數還可以用于以下地方:

            (1)骨骼動畫(skeletal animation)

            (2)反向動力學動畫(inverse cinimatic)

            (3)3D物理學

            在游戲中可以使用四元數來取代旋轉矩陣,它可用于描述3D空間中繞任意軸的任意旋轉。使用四元數來代替旋轉矩陣具有以下優點:

            (1)四元數占用的空間比矩陣少。

            (2)一些操作在視覺上會顯得更為平滑,比如可以在兩個四元數之間做插值運算。

            Direct3D擴展實用庫函數D3DXQuaternionRotationYawPitchRoll()提供了將三個歐拉角轉換為一個四元數的功能,該函數聲明如下:

            Builds a quaternion with the given yaw, pitch, and roll.

            D3DXQUATERNION * D3DXQuaternionRotationYawPitchRoll(
            D3DXQUATERNION * pOut,
            FLOAT Yaw,
            FLOAT Pitch,
            FLOAT Roll
            );

            Parameters

            pOut
            [in, out] Pointer to the D3DXQUATERNION structure that is the result of the operation.
            Yaw
            [in] Yaw around the y-axis, in radians.
            Pitch
            [in] Pitch around the x-axis, in radians.
            Roll
            [in] Roll around the z-axis, in radians.

            Return Values

            Pointer to a D3DXQUATERNION structure with the specified yaw, pitch, and roll.

            Remarks

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

            Use D3DXQuaternionNormalize for any quaternion input that is not already normalized.

            由于Direct3D變換引擎需要用矩陣來執行旋轉,所以需要將四元數轉換為矩陣格式。Direct3D擴展實用庫函數D3DXMatrixRotationQuaternion()提供了將四元數轉換成矩陣的功能,該函數聲明如下:

            Builds a rotation matrix from a quaternion.

            D3DXMATRIX * D3DXMatrixRotationQuaternion(
            D3DXMATRIX * pOut,
            CONST D3DXQUATERNION * pQ
            );

            Parameters

            pOut
            [in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
            pQ
            [in] Pointer to the source D3DXQUATERNION structure.

            Return Values

            Pointer to a D3DXMATRIX structure built from the source quaternion.

            Remarks

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

            For information about how to calculate quaternion values from a direction vector ( x, y, z ) and an angle of rotation, see D3DXQUATERNION.

            該示例程序同樣可以通過鍵盤控制飛機模型的位置和姿態,控制方式和上一個示例程序完全相同,但實現代碼卻簡潔得多,主要改動代碼如下:

            void setup_world_matrix()
            {
            static long previous_time = 0;
            static float elapsed_time = 0.0f;
            	elapsed_time  = (timeGetTime() - previous_time) / 1000.0f;
            previous_time = timeGetTime();
            	float angle_around_right = 0.0f, angle_around_up = 0.0f, angle_around_look = 0.0f;
            	if(g_keys['D'])	angle_around_look  -= 3 * elapsed_time;
            if(g_keys['A']) angle_around_look += 3 * elapsed_time;
            if(g_keys['S']) angle_around_right -= 3 * elapsed_time;
            if(g_keys['W']) angle_around_right += 3 * elapsed_time;
            if(g_keys['Q']) angle_around_up -= 3 * elapsed_time;
            if(g_keys['E']) angle_around_up += 3 * elapsed_time;
            	// now, calculate ratation matrix.
            	D3DXQUATERNION quat;
            D3DXMATRIX mat_rotation;
            	D3DXQuaternionRotationYawPitchRoll(&quat, angle_around_up, angle_around_right, angle_around_look);
            D3DXMatrixRotationQuaternion(&mat_rotation, &quat);
            D3DXMatrixMultiply(&g_mat_world, &mat_rotation, &g_mat_world);
            	// get look vector
            	D3DXVECTOR3 look;	
            	look.x	= g_mat_world._31;
            look.y = g_mat_world._32;
            look.z = g_mat_world._33;
            	// move model forward or backward
            	if(g_keys['F'])
            {
            g_mat_world._41 += 30 * elapsed_time * look.x;
            g_mat_world._42 += 30 * elapsed_time * look.y;
            g_mat_world._43 += 30 * elapsed_time * look.z;
            }
            	if(g_keys['V'])
            {
            g_mat_world._41 -= 30 * elapsed_time * look.x;
            g_mat_world._42 -= 30 * elapsed_time * look.y;
            g_mat_world._43 -= 30 * elapsed_time * look.z;
            }
            	g_device->SetTransform(D3DTS_WORLD, &g_mat_world);
            }

            在該示例程序中,通過用戶輸入,每一幀都對飛機模型進行重新定位,不斷改變飛機模型在空間中的位置或姿態,如果每兩幀間的時間間隔足夠小,則感到飛機模型位置或姿態的變換是連續的,也就形成了連續的動畫,實際上這就是三維動畫的基本原理,通過不斷改變場景中各物體的位置和姿態使各物體運動起來。為了保證動畫的連續性,幀速率要達到25幀/秒以上。

            注意:模型的旋轉是繞自身坐標軸的旋轉,而不是繞世界坐標系的3個坐標軸旋轉,而且在旋轉模型時其自身坐標軸也在不斷變化。

             

            下載示例工程

             

            posted on 2008-05-09 15:32 lovedday 閱讀(1212) 評論(1)  編輯 收藏 引用

            評論

            # re: 使用.x文件模型(3) 2012-06-18 11:02 張憶

            您好!請問.fx文件是什么文件,沒看到這方面的介紹  回復  更多評論   

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            精品免费久久久久国产一区| 久久99国产精品久久久| 久久精品亚洲乱码伦伦中文| 国产成年无码久久久免费| 欧美日韩中文字幕久久伊人| 久久精品国产2020| 尹人香蕉久久99天天拍| 94久久国产乱子伦精品免费| 九九久久自然熟的香蕉图片| 久久人人爽人人爽人人片AV东京热| 无码久久精品国产亚洲Av影片 | 一本久久综合亚洲鲁鲁五月天| 久久人妻无码中文字幕| 91麻豆精品国产91久久久久久| 亚洲午夜无码久久久久| 久久99精品久久久久久噜噜| 日韩AV无码久久一区二区| 久久精品人人做人人爽电影| …久久精品99久久香蕉国产| 狠狠狠色丁香婷婷综合久久五月| 久久久久久久亚洲精品| 91精品国产综合久久香蕉 | 欧美综合天天夜夜久久| 久久妇女高潮几次MBA| 久久久人妻精品无码一区| 久久国产综合精品五月天| 久久天天躁狠狠躁夜夜96流白浆 | 久久99精品国产99久久| 亚洲精品午夜国产VA久久成人| 久久久久久无码国产精品中文字幕| 国产精品久久久久影院色| 国产偷久久久精品专区| 久久99久国产麻精品66| 国内精品久久久久久久久电影网| 亚洲欧洲精品成人久久奇米网| 久久综合色区| 日韩久久无码免费毛片软件| 久久人人爽人人爽人人片av麻烦| 久久久亚洲裙底偷窥综合 | 久久66热人妻偷产精品9| 久久天天躁狠狠躁夜夜96流白浆|