• <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)【C風(fēng)格版】

             

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

             

            接口:

            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_view;                           
            // world transform matrix
                D3DXMATRIX mat_translation;                     // translation matrix
                D3DXMATRIX mat_rotation;                        // rotation matrix
            } *CAMERA_PTR;

            CAMERA_PTR create_camera();
            void destroy_camera(CAMERA_PTR camera);

            void move_camera(CAMERA_PTR camera, 
                             
            float x_pos, float y_pos, float z_pos);

            void move_camera_rel(CAMERA_PTR camera, 
                                 
            float x_add, float y_add, float z_add);

            void rotate_camera(CAMERA_PTR camera, 
                               
            float x_rot, float y_rot, float z_rot);

            void point_camera(CAMERA_PTR camera, 
                              
            float x_eye, float y_eye, float z_eye, 
                              
            float x_at, float y_at, float z_at);

            void start_track_camera(CAMERA_PTR camera);
            void end_track_camera(CAMERA_PTR camera);
            void track_camera(CAMERA_PTR camera, float time_ratio);
            void update_camera_pos(CAMERA_PTR camera);

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

            //----------------------------------------------------------------------------------------
            // Create camera.
            //----------------------------------------------------------------------------------------
            CAMERA_PTR create_camera()
            {
                CAMERA_PTR camera = (CAMERA_PTR) malloc(
            sizeof(CAMERA));

                memset(camera, 0, 
            sizeof(CAMERA));

                
            return camera;
            }

            //----------------------------------------------------------------------------------------
            // Destroy camera.
            //----------------------------------------------------------------------------------------
            void destroy_camera(CAMERA_PTR camera)
            {
                free(camera);
            }

            //----------------------------------------------------------------------------------------
            // move camera to new position.
            //----------------------------------------------------------------------------------------
            void move_camera(CAMERA_PTR camera, 
                             
            float x_pos, float y_pos, float z_pos)
            {    
                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);
            }

            //----------------------------------------------------------------------------------------
            // Move camera to new position relative to current position.
            //----------------------------------------------------------------------------------------
            void move_camera_rel(CAMERA_PTR camera, 
                                 
            float x_add, float y_add, float z_add)
            {
                move_camera(camera, 
                            camera->x_pos + x_add, camera->y_pos + y_add, camera->z_pos + z_add);
            }

            //-------------------------------------------------------------------------
            // rotate camera.
            //-------------------------------------------------------------------------
            void rotate_camera(CAMERA_PTR camera, 
                               
            float x_rot, float y_rot, float z_rot)
            {
                D3DXMATRIX mat_x_rot, mat_y_rot, mat_z_rot;    

                camera->x_rot = x_rot;
                camera->y_rot = y_rot;
                camera->z_rot = z_rot;

                D3DXMatrixRotationX(&mat_x_rot, -x_rot);
                D3DXMatrixRotationY(&mat_y_rot, -y_rot);
                D3DXMatrixRotationZ(&mat_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);
            }

            //-------------------------------------------------------------------------
            // rotate camera.
            //-------------------------------------------------------------------------
            void rotate_camera_rel(CAMERA_PTR camera, 
                                   
            float x_add, float y_add, float z_add)
            {
                rotate_camera(camera, 
                              camera->x_pos + x_add, camera->y_pos + y_add, camera->z_pos + z_add);
            }

            //-------------------------------------------------------------------------
            // move camera to new position and look at new target position.
            //-------------------------------------------------------------------------
            void point_camera(CAMERA_PTR 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
                move_camera(camera, x_eye, y_eye, z_eye);
                rotate_camera(camera, x_rot, y_rot, 0.0);
            }

            //-------------------------------------------------------------------------
            // set camera's start tracking position and rotation.
            //-------------------------------------------------------------------------
            void start_track_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 end_track_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 track_camera(CAMERA_PTR camera, float time_ratio)
            {       
                
            float x_move = (camera->end_x_pos - camera->start_x_pos) * time_ratio;
                
            float y_move = (camera->end_y_pos - camera->start_y_pos) * time_ratio;
                
            float z_move = (camera->end_z_pos - camera->start_z_pos) * time_ratio;

                move_camera(camera, 
                    camera->start_x_pos + x_move, camera->start_y_pos + y_move, camera->start_z_pos + z_move);

                
            float x_rotate = (camera->end_x_rot - camera->start_x_rot) * time_ratio;
                
            float y_rotate = (camera->end_y_rot - camera->start_y_rot) * time_ratio;
                
            float z_rotate = (camera->end_z_rot - camera->start_z_rot) * time_ratio;

                rotate_camera(camera, 
                    camera->start_x_rot + x_rotate, camera->start_y_rot + y_rotate, camera->start_z_rot + z_rotate);
            }

            //-------------------------------------------------------------------------
            // update new camera world transform matrix.
            //-------------------------------------------------------------------------
            void update_camera_pos(CAMERA_PTR camera)
            {
                D3DXMatrixMultiply(&camera->mat_view, &camera->mat_translation, &camera->mat_rotation);
            }

            posted on 2007-10-25 20:25 lovedday 閱讀(180) 評(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)論

            久久成人国产精品一区二区| 久久丫忘忧草产品| 国产福利电影一区二区三区久久久久成人精品综合 | 狠狠色婷婷综合天天久久丁香| 色成年激情久久综合| 免费精品国产日韩热久久| 久久精品国产亚洲av麻豆小说 | …久久精品99久久香蕉国产| 久久婷婷色综合一区二区| 麻豆一区二区99久久久久| 狠狠久久综合伊人不卡| AV无码久久久久不卡网站下载| 日韩影院久久| 91秦先生久久久久久久| 久久精品国产亚洲av高清漫画| 久久久久久一区国产精品| 国产国产成人精品久久| 日韩乱码人妻无码中文字幕久久| 狠狠久久综合| 狠狠精品久久久无码中文字幕 | 无码AV波多野结衣久久| 人人狠狠综合88综合久久| 色综合久久综合网观看| 久久精品一本到99热免费| 一本久久知道综合久久| 婷婷久久综合| 亚洲国产成人精品91久久久 | 久久久不卡国产精品一区二区| 九九久久自然熟的香蕉图片| 亚洲国产精品狼友中文久久久 | 国产69精品久久久久9999APGF| 久久久中文字幕日本| 精品无码人妻久久久久久| 久久精品国产福利国产秒| 丁香狠狠色婷婷久久综合| 久久无码人妻一区二区三区午夜 | 中文字幕精品久久| 久久人人爽人人爽人人片AV麻烦| 久久久久久免费视频| 久久天天躁狠狠躁夜夜躁2014| 久久人妻无码中文字幕|