• <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)【接口與實(shí)現(xiàn)分離版】

             

            本篇是創(chuàng)建游戲內(nèi)核(5)【接口與實(shí)現(xiàn)分離版】的續(xù)篇,關(guān)于該內(nèi)核的細(xì)節(jié)說(shuō)明請(qǐng)參考創(chuàng)建游戲內(nèi)核(6),這個(gè)版本主要是按照功能劃分模塊的思想,并嚴(yán)格按照接口與實(shí)現(xiàn)相分離的原則來(lái)寫(xiě)的,沒(méi)有用面向?qū)ο蟮乃枷雭?lái)寫(xiě),沒(méi)有繼承沒(méi)有多態(tài)。大家可以對(duì)比兩個(gè)版本,比較優(yōu)劣。

            接口:

            /*************************************************************************
            PURPOSE:
                Interface for D3D camera.
            *************************************************************************/


            #ifndef _CORE_CAMERA_H_
            #define _CORE_CAMERA_H_

            #include "core_common.h"

            typedef 
            void* CAMERA;

            CAMERA camera_create();

            void camera_move(CAMERA camera, float x_pos, float y_pos, float z_pos);
            void camera_rotate(CAMERA camera, float x_rot, float y_rot, float z_rot);

            void camera_point(CAMERA camera, 
                              
            float x_eye, float y_eye, float z_eye, 
                              
            float x_at, float y_at, float z_at);

            void camera_set_start_track(CAMERA camera);
            void camera_set_end_track(CAMERA camera);

            void camera_track(CAMERA camera, float time_ratio, float time_length);
            void camera_update(CAMERA camera);

            D3DXMATRIX* camera_get_matrix(CAMERA camera);

            float camera_get_x_pos(CAMERA camera);
            float camera_get_y_pos(CAMERA camera);
            float camera_get_z_pos(CAMERA camera);

            float camera_get_x_rot(CAMERA camera);
            float camera_get_y_rot(CAMERA camera);
            float camera_get_z_rot(CAMERA camera);

            #endif
             

            實(shí)現(xiàn):
            /***********************************************************************************
            PURPOSE:
                Implement for D3D camera.
            ***********************************************************************************/


            #include "core_common.h"
            #include "core_camera.h"

            typedef 
            struct _CAMERA
            {
                
            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
            } *_CAMERA_PTR;

            //----------------------------------------------------------------------------------------
            // Create camera.
            //----------------------------------------------------------------------------------------
            CAMERA camera_create()
            {
                _CAMERA_PTR _camera = (_CAMERA_PTR) malloc(
            sizeof(_CAMERA));

                memset(_camera, 0, 
            sizeof(_CAMERA));

                
            return (CAMERA) _camera;
            }

            //----------------------------------------------------------------------------------------
            // move camera to new position.
            //----------------------------------------------------------------------------------------
            void camera_move(CAMERA camera, float x_pos, float y_pos, float z_pos)
            {
                _CAMERA_PTR _camera = (_CAMERA_PTR) camera;

                _camera->x_pos = x_pos;
                _camera->y_pos = y_pos;
                _camera->z_pos = z_pos;

                D3DXMatrixTranslation(&_camera->mat_translation, -x_pos, -y_pos, -z_pos);
            }

            //-------------------------------------------------------------------------
            // rotate camera.
            //-------------------------------------------------------------------------
            void camera_rotate(CAMERA camera, float x_rot, float y_rot, float z_rot)
            {
                D3DXMATRIX _mat_x_rot, _mat_y_rot, _mat_z_rot;

                _CAMERA_PTR _camera = (_CAMERA_PTR) camera;

                _camera->x_rot = x_rot;
                _camera->y_rot = y_rot;
                _camera->z_rot = z_rot;

                _camera->mat_rotation = _mat_z_rot;

                D3DXMatrixMultiply(&_camera->mat_rotation, &_camera->mat_rotation, &_mat_y_rot);
                D3DXMatrixMultiply(&_camera->mat_rotation, &_camera->mat_rotation, &_mat_x_rot);
            }

            //-------------------------------------------------------------------------
            // move camera to new position and look at new target position.
            //-------------------------------------------------------------------------
            void camera_point(CAMERA camera, 
                              
            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
                camera_move(camera, x_eye, y_eye, z_eye);
                camera_rotate(camera, _x_rot, _y_rot, 0.0);
            }

            //-------------------------------------------------------------------------
            // set camera's start tracking position and rotation.
            //-------------------------------------------------------------------------
            void camera_set_start_track(CAMERA camera)
            {
                _CAMERA_PTR _camera = (_CAMERA_PTR) camera;

                _camera->start_x_pos = _camera->x_pos;
                _camera->start_y_pos = _camera->y_pos;
                _camera->start_z_pos = _camera->z_pos;

                _camera->start_x_rot = _camera->x_rot;
                _camera->start_y_rot = _camera->y_rot;
                _camera->start_z_rot = _camera->z_rot;
            }

            //-------------------------------------------------------------------------
            // set camera's end tracking position and rotation.
            //-------------------------------------------------------------------------
            void camera_set_end_track(CAMERA camera)
            {
                _CAMERA_PTR _camera = (_CAMERA_PTR) camera;

                _camera->end_x_pos = _camera->x_pos;
                _camera->end_y_pos = _camera->y_pos;
                _camera->end_z_pos = _camera->z_pos;

                _camera->end_x_rot = _camera->x_rot;
                _camera->end_y_rot = _camera->y_rot;
                _camera->end_z_rot = _camera->z_rot;
            }

            //-------------------------------------------------------------------------
            // move camera to new position and ratation by giving time, 
            // 0 <= time_ratio <= 1.
            //-------------------------------------------------------------------------
            void camera_track(CAMERA camera, float time_ratio, float time_length)
            {
                _CAMERA_PTR _camera = (_CAMERA_PTR) camera;

                
            float _time_offset = time_length * time_ratio;

                
            float _x = (_camera->end_x_pos - _camera->start_x_pos) / time_length * _time_offset;
                
            float _y = (_camera->end_y_pos - _camera->start_y_pos) / time_length * _time_offset;
                
            float _z = (_camera->end_z_pos - _camera->start_z_pos) / time_length * _time_offset;

                camera_move(camera, _camera->start_x_pos + _x, _camera->start_y_pos + _y, _camera->start_z_pos + _z);

                _x = (_camera->end_x_rot - _camera->start_x_rot) / time_length * _time_offset;
                _y = (_camera->end_y_rot - _camera->start_y_rot) / time_length * _time_offset;
                _z = (_camera->end_z_rot - _camera->start_z_rot) / time_length * _time_offset;

                camera_rotate(camera, _camera->start_x_rot + _x, _camera->start_y_rot + _y, _camera->start_z_rot + _z);
            }

            //-------------------------------------------------------------------------
            // update new camera world transform matrix.
            //-------------------------------------------------------------------------
            void camera_update(CAMERA camera)
            {
                _CAMERA_PTR _camera = (_CAMERA_PTR) camera;

                D3DXMatrixMultiply(&_camera->mat_world, &_camera->mat_translation, &_camera->mat_rotation);
            }

            //-------------------------------------------------------------------------
            // Get camera world transform matrix.
            //-------------------------------------------------------------------------
            D3DXMATRIX* camera_get_matrix(CAMERA camera)
            {
                camera_update(camera);

                
            return & ((_CAMERA_PTR) camera)->mat_world;
            }

            //-------------------------------------------------------------------------
            // Get camera current position (x coordinate).
            //-------------------------------------------------------------------------
            float camera_get_x_pos(CAMERA camera)
            {
                
            return ((_CAMERA_PTR) camera)->x_pos;
            }

            //-------------------------------------------------------------------------
            // Get camera current position (y coordinate).
            //-------------------------------------------------------------------------
            float camera_get_y_pos(CAMERA camera)
            {
                
            return ((_CAMERA_PTR) camera)->y_pos;
            }

            //-------------------------------------------------------------------------
            // Get camera current position (z coordinate).
            //-------------------------------------------------------------------------
            float camera_get_z_pos(CAMERA camera)
            {
                
            return ((_CAMERA_PTR) camera)->z_pos;
            }

            //-------------------------------------------------------------------------
            // Get camera current rotation (x coordinate).
            //-------------------------------------------------------------------------
            float camera_get_x_rot(CAMERA camera)
            {
                
            return ((_CAMERA_PTR) camera)->x_rot;
            }

            //-------------------------------------------------------------------------
            // Get camera current rotation (y coordinate).
            //-------------------------------------------------------------------------
            float camera_get_y_rot(CAMERA camera)
            {
                
            return ((_CAMERA_PTR) camera)->y_rot;
            }

            //-------------------------------------------------------------------------
            // Get camera current rotation (z coordinate).
            //-------------------------------------------------------------------------
            float camera_get_z_rot(CAMERA camera)
            {
                
            return ((_CAMERA_PTR) camera)->z_rot;
            }

             

            posted on 2007-10-02 23:52 lovedday 閱讀(185) 評(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)論

            国产精品99久久久久久董美香| 综合人妻久久一区二区精品| 久久精品国产亚洲AV无码娇色| 无码任你躁久久久久久| 国产成人精品久久一区二区三区av| 久久天天躁狠狠躁夜夜网站| 伊人久久综合精品无码AV专区| 日本五月天婷久久网站| 精品久久久久久国产| 亚洲中文字幕无码久久综合网| 一本一本久久a久久综合精品蜜桃| 久久精品国产亚洲AV不卡| 精品久久久久久国产| 久久久久人妻一区精品性色av| 精品久久久久久国产潘金莲| 精品999久久久久久中文字幕| 国产精品一区二区久久| 久久综合丁香激情久久| 久久精品国产精品亚洲| 久久这里的只有是精品23| 久久亚洲精品成人无码网站| 久久精品国产亚洲AV高清热| 久久免费国产精品一区二区| 久久青青草原精品国产不卡| 亚洲国产成人久久笫一页| 亚洲精品无码久久久久| 人人狠狠综合久久亚洲婷婷 | 久久久亚洲精品蜜桃臀| 久久亚洲精品国产亚洲老地址| 欧美午夜精品久久久久免费视| 韩国三级中文字幕hd久久精品| yy6080久久| 国产999精品久久久久久| 思思久久精品在热线热| 久久ww精品w免费人成| 精品久久久久久无码人妻热| 亚洲人成精品久久久久| 精品多毛少妇人妻AV免费久久| 久久国产精品成人片免费| 成人国内精品久久久久影院VR| 久久亚洲熟女cc98cm|