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

            天行健 君子當自強而不息

            創建游戲內核(6)


            本篇是 創建游戲內核(5)的續篇,其中涉及到矩陣以及坐標系統變換的知識請參閱DirectX 9的一些數學計算函數:矩陣,坐標變換。以及DirectX 9的坐標系統變換。

            視口變換和CAMERA

            CAMERA類和 WORLD_POSITION類非常類似,但CAMERA類處理的是視口變換矩陣。

            來看看CAMERA類的定義:
             
            //================================================================================
            // Defines for class CAMERA.
            //================================================================================
            class CAMERA
            {
            protected:
                
            float _x_pos, _y_pos, _z_pos;                   // camera current position
                float _x_rot, _y_rot, _z_rot;                   // camera current rotation

                
            float _start_x_pos, _start_y_pos, _start_z_pos; // start tracking position
                float _start_x_rot, _start_y_rot, _start_z_rot; // start tracking rotation

                
            float _end_x_pos, _end_y_pos, _end_z_pos;       // end tracking position
                float _end_x_rot, _end_y_rot, _end_z_rot;       // end tracking rotation

                D3DXMATRIX _mat_world;          
            // world transform matrix
                D3DXMATRIX _mat_translation;    // translation matrix
                D3DXMATRIX _mat_rotation;       // rotation matrix

            public:
                CAMERA();

                D3DXMATRIX* Get_Matrix();
                
            void Update();

                
            void Move(float x_pos, float y_pos, float z_pos);
                
            void Move_Rel(float x_add, float y_add, float z_add);

                
            void Rotate(float x_rot, float y_rot, float z_rot);
                
            void Rotate_Rel(float x_add, float y_add, float z_add);

                
            void Point(float x_eye, float y_eye, float z_eye, float x_at, float y_at, float z_at);

                
            void Set_Start_Track();
                
            void Set_End_Track();
                
            void Track(float time_ratio, float time_length);

                
            float Get_X_Pos();
                
            float Get_Y_Pos();
                
            float Get_Z_Pos();
                
            float Get_X_Rotation();
                
            float Get_Y_Rotation();
                
            float Get_Z_Rotation();
            };

            接著是CAMERA類的實現:
             
            //-------------------------------------------------------------------
            // Constructor, initialize camera's current position and rotation.
            //-------------------------------------------------------------------
            CAMERA::CAMERA()
            {
                Move(0.0, 0.0, 0.0);
                Rotate(0.0, 0.0, 0.0);
                Update();
            }

            //-------------------------------------------------------------------
            // Move camera to new position.
            //-------------------------------------------------------------------
            void CAMERA::Move(float x_pos, float y_pos, float z_pos)
            {
                _x_pos = x_pos;  _y_pos = y_pos;  _z_pos = z_pos;

                D3DXMatrixTranslation(&_mat_translation, -x_pos, -y_pos, -z_pos);
            }

            //-------------------------------------------------------------------
            // Move camera to new positoin which is summed by current position and 
            // added position.
            //-------------------------------------------------------------------
            void CAMERA::Move_Rel(float x_add, float y_add, float z_add)
            {
                Move(_x_pos + x_add, _y_pos + y_add, _z_pos + z_add);
            }

            //-------------------------------------------------------------------
            // Build rotation matrix.
            //-------------------------------------------------------------------
            void CAMERA::Rotate(float x_rot, float y_rot, float z_rot)
            {
                D3DXMATRIX mat_x_rot, mat_y_rot, mat_z_rot;

                _x_rot = x_rot;  _y_rot = y_rot; _z_rot = z_rot;

                D3DXMatrixRotationX(&mat_x_rot, -x_rot);
                D3DXMatrixRotationX(&mat_y_rot, -y_rot);
                D3DXMatrixRotationX(&mat_z_rot, -z_rot);

                _mat_rotation = mat_z_rot;
                
                D3DXMatrixMultiply(&_mat_rotation, &_mat_rotation, &mat_y_rot);
                D3DXMatrixMultiply(&_mat_rotation, &_mat_rotation, &mat_x_rot);       
            }

            //-------------------------------------------------------------------
            // Build rotation matrix which is summed by current rotation value 
            // and added rotation value.
            //-------------------------------------------------------------------
            void CAMERA::Rotate_Rel(float x_add, float y_add, float z_add)
            {
                Rotate(_x_rot + x_add, _y_rot + y_add, _z_rot + z_add);
            }

            //-------------------------------------------------------------------
            // Move camera to new position and look at new target position.
            //-------------------------------------------------------------------
            void CAMERA::Point(float x_eye, float y_eye, float z_eye, float x_at, float y_at, float z_at)
            {
                
            // Calculate angles between points

                
            float x_diff = x_at - x_eye;
                
            float y_diff = y_at - y_eye;
                
            float z_diff = z_at - z_eye;

                
            float x_rot = (float) atan2(-y_diff, sqrt(x_diff * x_diff + z_diff * z_diff));
                
            float y_rot = (float) atan2(x_diff, z_diff);

                
            // Move camera to new position and look at new target
                Move(x_eye, y_eye, z_eye);
                Rotate(x_rot, y_rot, 0.0);
            }

            //-------------------------------------------------------------------
            // Set camera's start tracking position and rotation.
            //-------------------------------------------------------------------
            void CAMERA::Set_Start_Track()
            {
                _start_x_pos = _x_pos;  _start_y_pos = _y_pos;  _start_z_pos = _z_pos;
                _start_x_rot = _x_rot;  _start_y_rot = _y_rot;  _start_z_rot = _z_rot;
            }

            //-------------------------------------------------------------------
            // Set camera's end tracking position and rotation.
            //-------------------------------------------------------------------
            void CAMERA::Set_End_Track()
            {
                _end_x_pos = _x_pos;  _end_y_pos = _y_pos;  _end_z_pos = _z_pos;
                _end_x_rot = _x_rot;  _end_y_rot = _y_rot;  _end_z_rot = _z_rot;
            }

            //-------------------------------------------------------------------
            // Move camera to new position and ratation by giving time, 
            // 0 <= time_ratio <= 1.
            //-------------------------------------------------------------------
            void CAMERA::Track(float time_ratio, float time_length)
            {
                
            float time_offset = time_length * time_ratio;

                
            float x = (_end_x_pos - _start_x_pos) / time_length * time_offset;
                
            float y = (_end_y_pos - _start_y_pos) / time_length * time_offset;
                
            float z = (_end_z_pos - _start_z_pos) / time_length * time_offset;

                Move(_start_x_pos + x, _start_y_pos + y, _start_z_pos + z);

                x = (_end_x_rot - _start_x_rot) / time_length * time_offset;
                y = (_end_y_rot - _start_y_rot) / time_length * time_offset;
                z = (_end_z_rot - _start_z_rot) / time_length * time_offset;

                Rotate(_start_x_rot + x, _start_y_rot + y, _start_z_rot + z);
            }

            //-------------------------------------------------------------------
            // Update new camera world transform matrix.
            //-------------------------------------------------------------------
            void CAMERA::Update()
            {
                D3DXMatrixMultiply(&_mat_world, &_mat_translation, &_mat_rotation);
            }

            //-------------------------------------------------------------------
            // Get camera world transform matrix.
            //-------------------------------------------------------------------
            D3DXMATRIX* CAMERA::Get_Matrix()
            {
                Update();

                
            return &_mat_world;
            }

            //-------------------------------------------------------------------
            // Get camera current position (x coordinate).
            //-------------------------------------------------------------------
            float CAMERA::Get_X_Pos()
            {
                
            return _x_pos;
            }

            //-------------------------------------------------------------------
            // Get camera current position (y coordinate).
            //-------------------------------------------------------------------
            float CAMERA::Get_Y_Pos()
            {
                
            return _y_pos;
            }

            //-------------------------------------------------------------------
            // Get camera current position (z coordinate).
            //-------------------------------------------------------------------
            float CAMERA::Get_Z_Pos()
            {
                
            return _z_pos;
            }

            //-------------------------------------------------------------------
            // Get camera current rotation (x coordinate).
            //-------------------------------------------------------------------
            float CAMERA::Get_X_Rotation()
            {
                
            return _x_rot;
            }

            //-------------------------------------------------------------------
            // Get camera current rotation (y coordinate).
            //-------------------------------------------------------------------
            float CAMERA::Get_Y_Rotation()
            {
                
            return _y_rot;
            }

            //-------------------------------------------------------------------
            // Get camera current rotation (z coordinate).
            //-------------------------------------------------------------------
            float CAMERA::Get_Z_Rotation()
            {
                
            return _z_rot;
            }

            CAMERA類和WORLD_POSITION類惟一的不同就是加上了Point、Set_Start_Track、 Set_End_Track以及Track函數。Point函數用來確定觀察點的方位并在瞬間將它指向一個特定的方向。

            三個同追蹤有關的函數隨著時間追蹤攝像機的移動路徑。要使用攝像機追蹤方位,將攝像機放到預想的起始位置并調用CAMERA:: Set_Start_Track,然后再將攝像機移動到預想的結束方位并調用CAMERA::Set_End_Track即可。

            接下來需要調用CAMERA::Track函數(要在調用CAMERA::Update之前調用它)沿著創建的軌跡來定位攝像機。 Track函數的time_ratio參數的范圍從0.0(起始方位)-1.0(結束方位),任何介于此范圍內的值都會將攝像機沿著軌跡進行移動, time_length可以是任意起作用的值(舉例來說,毫秒)。

            攝像機追蹤創建了一些很棒的效果,下面的示例說明了這一點:
             
                CAMERA camera;

                
            // 移到位置(0.0, 100.0, -100.0)處并朝向原點
                camera.Point(0.0, 100.0, -100.0, 0.0, 0.0, 0.0);
                camera.Set_Start_Track();

                
            // 移到結束方位
                camera.Point(-100.0, 0.0, 0.0, 0.0, 100.0, 0.0);
                camera.Set_End_Track();

                
            // 每過10000毫秒將攝像機放置到起始方位和結束方位的一半處
                camera.Track(0.5, 10000);
                camera.Update();

            posted on 2007-08-29 18:14 lovedday 閱讀(339) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            久久久久久狠狠丁香| 精品久久久久中文字幕日本| 国产视频久久| 中文字幕精品无码久久久久久3D日动漫 | 国内精品久久久久久久97牛牛| 久久人人爽人人爽人人AV东京热| 久久婷婷五月综合97色| 久久久精品国产Sm最大网站| 亚洲人成伊人成综合网久久久| 国产精品一区二区久久国产| 久久精品国产半推半就| 日韩久久久久中文字幕人妻 | 久久久国产乱子伦精品作者| 999久久久免费国产精品播放| 区久久AAA片69亚洲| 精品人妻伦一二三区久久| 久久精品中文无码资源站| 久久亚洲高清观看| 久久亚洲AV成人出白浆无码国产 | 久久这里有精品视频| 91精品久久久久久无码| 亚洲国产精品久久久天堂| 香港aa三级久久三级老师2021国产三级精品三级在 | 久久精品国产国产精品四凭| 久久99国内精品自在现线| 一本色道久久88综合日韩精品 | 久久夜色tv网站| 久久久久久国产精品免费无码| 99久久香蕉国产线看观香| 精品国产综合区久久久久久| 久久综合久久综合久久| 99久久精品国内| 久久青青草原亚洲av无码app | 97视频久久久| 伊人久久大香线蕉AV色婷婷色| 亚洲精品成人网久久久久久| 久久夜色精品国产www| 久久一本综合| 99久久香蕉国产线看观香| 久久91精品国产91久| 无码人妻精品一区二区三区久久久|