• <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)而不息

            創(chuàng)建游戲內(nèi)核(6)【OO改良版】

             

            本篇是創(chuàng)建游戲內(nèi)核(5)【OO改良版】的續(xù)篇,關(guān)于該內(nèi)核的細(xì)節(jié)說(shuō)明請(qǐng)參閱創(chuàng)建游戲內(nèi)核(6)


            接口:

            class CAMERA
            {
            public:
                CAMERA();

                D3DXMATRIX* get_matrix();

                
            void init();
                
            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();

            private:
                
            float m_x_pos, m_y_pos, m_z_pos;                   // camera current position
                float m_x_rot, m_y_rot, m_z_rot;                   // camera current rotation

                
            float m_start_x_pos, m_start_y_pos, m_start_z_pos; // start tracking position
                float m_start_x_rot, m_start_y_rot, m_start_z_rot; // start tracking rotation

                
            float m_end_x_pos, m_end_y_pos, m_end_z_pos;       // end tracking position
                float m_end_x_rot, m_end_y_rot, m_end_z_rot;       // end tracking rotation

                D3DXMATRIX m_mat_world;          
            // world transform matrix
                D3DXMATRIX m_mat_translation;    // translation matrix
                D3DXMATRIX m_mat_rotation;       // rotation matrix
            };

            typedef CAMERA* CAMERA_PTR;
             

            實(shí)現(xiàn):

            //-------------------------------------------------------------------------
            // Constructor, zero member data.
            //-------------------------------------------------------------------------
            CAMERA::CAMERA()
            {
                memset(
            this, 0, sizeof(*this));   
            }

            //-------------------------------------------------------------------------
            // initialize camera.
            //-------------------------------------------------------------------------
            void CAMERA::init()
            {    
                update();
            }

            //-------------------------------------------------------------------------
            // move camera to new position.
            //-------------------------------------------------------------------------
            void CAMERA::move(float x_pos, float y_pos, float z_pos)
            {
                m_x_pos = x_pos;  m_y_pos = y_pos;  m_z_pos = z_pos;

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

            //-------------------------------------------------------------------------
            // move camera to new positoin which relative to current position.
            //-------------------------------------------------------------------------
            void CAMERA::move_rel(float x_add, float y_add, float z_add)
            {
                move(m_x_pos + x_add, m_y_pos + y_add, m_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;

                m_x_rot = x_rot;  m_y_rot = y_rot; m_z_rot = z_rot;

                D3DXMatrixRotationX(&_mat_x_rot, -x_rot);
                D3DXMatrixRotationX(&_mat_y_rot, -y_rot);
                D3DXMatrixRotationX(&_mat_z_rot, -z_rot);

                m_mat_rotation = _mat_z_rot;
                
                D3DXMatrixMultiply(&m_mat_rotation, &m_mat_rotation, &_mat_y_rot);
                D3DXMatrixMultiply(&m_mat_rotation, &m_mat_rotation, &_mat_x_rot);       
            }

            //-------------------------------------------------------------------------
            // Build rotation matrix which relative to current rotation.
            //-------------------------------------------------------------------------
            void CAMERA::rotate_rel(float x_add, float y_add, float z_add)
            {
                rotate(m_x_rot + x_add, m_y_rot + y_add, m_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()
            {
                m_start_x_pos = m_x_pos;  m_start_x_pos = m_y_pos;  m_start_z_pos = m_z_pos;
                m_start_x_rot = m_x_rot;  m_start_y_rot = m_y_rot;  m_start_z_rot = m_z_rot;
            }

            //-------------------------------------------------------------------------
            // set camera's end tracking position and rotation.
            //-------------------------------------------------------------------------
            void CAMERA::set_end_track()
            {
                m_end_x_pos = m_x_pos;  m_end_y_pos = m_y_pos;  m_end_z_pos = m_z_pos;
                m_end_x_rot = m_x_rot;  m_end_y_rot = m_y_rot;  m_end_z_rot = m_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 = (m_end_x_pos - m_start_x_pos) / time_length * time_offset;
                
            float y = (m_end_y_pos - m_start_x_pos) / time_length * time_offset;
                
            float z = (m_end_z_pos - m_start_z_pos) / time_length * time_offset;

                move(m_start_x_pos + x, m_start_x_pos + y, m_start_z_pos + z);

                x = (m_end_x_rot - m_start_x_rot) / time_length * time_offset;
                y = (m_end_y_rot - m_start_y_rot) / time_length * time_offset;
                z = (m_end_z_rot - m_start_z_rot) / time_length * time_offset;

                rotate(m_start_x_rot + x, m_start_y_rot + y, m_start_z_rot + z);
            }

            //-------------------------------------------------------------------------
            // update new camera world transform matrix.
            //-------------------------------------------------------------------------
            void CAMERA::update()
            {
                D3DXMatrixMultiply(&m_mat_world, &m_mat_translation, &m_mat_rotation);
            }

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

                
            return &m_mat_world;
            }

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

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

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

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

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

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

            posted on 2007-10-06 18:04 lovedday 閱讀(273) 評(píng)論(0)  編輯 收藏 引用


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


            公告

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            隨筆分類(178)

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

            搜索

            最新評(píng)論

            亚洲乱码日产精品a级毛片久久| 久久久这里有精品| 四虎影视久久久免费观看| 国产精品热久久无码av| 国产精品内射久久久久欢欢| 国产高清国内精品福利99久久| 无码人妻久久一区二区三区免费| 久久99精品久久久久久水蜜桃| 免费观看久久精彩视频| 天天综合久久久网| 欧美久久综合九色综合| 久久精品无码免费不卡| 亚洲国产精品综合久久网络| 性做久久久久久免费观看| 性高湖久久久久久久久| 99热成人精品热久久669| 亚洲va中文字幕无码久久不卡 | 99久久国产亚洲高清观看2024| 久久国产乱子精品免费女| 成人亚洲欧美久久久久| 狠狠久久综合伊人不卡| 亚洲人成无码久久电影网站| 久久午夜伦鲁片免费无码| 日本久久久精品中文字幕| 久久笫一福利免费导航| 久久精品国产99国产电影网 | 国产香蕉久久精品综合网| 色狠狠久久AV五月综合| 久久99久久无码毛片一区二区| 亚洲综合精品香蕉久久网| 国产精品久久久久久久久鸭| 精品欧美一区二区三区久久久| 无码人妻精品一区二区三区久久久| 久久99国产精一区二区三区 | 国产精品成人99久久久久 | 亚洲国产精品综合久久网络| 99久久无码一区人妻a黑| 亚洲性久久久影院| 久久强奷乱码老熟女网站| 久久精品国产久精国产| 嫩草伊人久久精品少妇AV|