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

            天行健 君子當自強而不息

            創建3D圖形引擎(2)【OO改良版】

             

            本篇是創建3D圖形引擎(1)【OO改良版】的續篇,以創建游戲內核【OO改良版】中編寫的代碼為基礎進行開發,細節說明請參閱創建3D圖形引擎(2

             

            接口:

            //==============================================================================
            // This class encapsulate for frustum, judge whether other object is in frustum.
            //==============================================================================
            typedef class FRUSTUM
            {
            public:
                
            // Construct the six planes from current view and projection. 
                // Can override the default depth value.
                BOOL create(float z_distance);

                
            // The following functions check a single point, cube, rectangle, and sphere if 
                // contained in the frustum. A return value of TRUE means visible, FALSE not visible.
                // When checking cubes or rectangles, you can supply a BOOL variable that determines 
                // if all the points are in the frustum.

                BOOL check_point(
            float x_pos, float y_pos, float z_pos);

                BOOL check_cube(
            float x_center, float y_center, float z_center,
                                
            float radius,
                                BOOL* completely_contained);

                BOOL check_rectangle(
            float x_center, float y_center, float z_center,
                                     
            float x_radius, float y_radius, float z_radius,
                                     BOOL* completely_contained);

                BOOL check_sphere(
            float x_center, float y_center, float z_center,
                                  
            float radius);
                
            private:
                D3DXPLANE m_planes[6];       
            // the frustum planes
            } *FRUSTUM_PTR;

            實現:

            /*************************************************************************
            PURPOSE:
                Implement for frustum.
            *************************************************************************/


            #include "core_common.h"
            #include "core_graphics.h"
            #include "frustum.h"

            //----------------------------------------------------------------------------
            // Construct frustum.
            //----------------------------------------------------------------------------
            BOOL FRUSTUM::create(float z_distance)
            {
                D3DXMATRIX matrix, mat_view, mat_proj;

                
            // error checking
                if(g_d3d_device == NULL)
                    
            return FALSE;

                
            // calculate FOV data
                g_d3d_device->GetTransform(D3DTS_PROJECTION, &mat_proj);

                
            if(! float_equal(z_distance, 0.0f))
                {
                    
            // Calculate new projection matrix based on distance provided.
                    //
                    // projection matrix is:
                    // 
                    // | xScale     0          0               0 |
                    // | 0        yScale       0               0 |
                    // | 0          0       zf/(zf-zn)         1 |
                    // | 0          0       -zn*zf/(zf-zn)     0 |
                    //
                    // where:
                    //      yScale = cot(fovY/2)
                    //      xScale = yScale / aspect ratio

                    
            float z_min = -mat_proj._43 / mat_proj._33;
                    
            float q = z_distance / (z_distance - z_min);

                    mat_proj._33 = q;
                    mat_proj._43 = -q * z_min;
                }

                g_d3d_device->GetTransform(D3DTS_VIEW, &mat_view);
                D3DXMatrixMultiply(&matrix, &mat_view, &mat_proj);

                
            // calculate the planes
                m_planes[0].a = matrix._14 + matrix._13; // Near
                m_planes[0].b = matrix._24 + matrix._23;
                m_planes[0].c = matrix._34 + matrix._33;
                m_planes[0].d = matrix._44 + matrix._43;
                D3DXPlaneNormalize(&m_planes[0], &m_planes[0]);

                m_planes[1].a = matrix._14 - matrix._13; 
            // Far
                m_planes[1].b = matrix._24 - matrix._23;
                m_planes[1].c = matrix._34 - matrix._33;
                m_planes[1].d = matrix._44 - matrix._43;
                D3DXPlaneNormalize(&m_planes[1], &m_planes[1]);

                m_planes[2].a = matrix._14 + matrix._11; 
            // Left
                m_planes[2].b = matrix._24 + matrix._21;
                m_planes[2].c = matrix._34 + matrix._31;
                m_planes[2].d = matrix._44 + matrix._41;
                D3DXPlaneNormalize(&m_planes[2], &m_planes[2]);

                m_planes[3].a = matrix._14 - matrix._11; 
            // Right
                m_planes[3].b = matrix._24 - matrix._21;
                m_planes[3].c = matrix._34 - matrix._31;
                m_planes[3].d = matrix._44 - matrix._41;
                D3DXPlaneNormalize(&m_planes[3], &m_planes[3]);

                m_planes[4].a = matrix._14 - matrix._12; 
            // Top
                m_planes[4].b = matrix._24 - matrix._22;
                m_planes[4].c = matrix._34 - matrix._32;
                m_planes[4].d = matrix._44 - matrix._42;
                D3DXPlaneNormalize(&m_planes[4], &m_planes[4]);

                m_planes[5].a = matrix._14 + matrix._12; 
            // Bottom
                m_planes[5].b = matrix._24 + matrix._22;
                m_planes[5].c = matrix._34 + matrix._32;
                m_planes[5].d = matrix._44 + matrix._42;
                D3DXPlaneNormalize(&m_planes[5], &m_planes[5]);

                
            return TRUE;
            }

            //----------------------------------------------------------------------------
            // Check one point whether in frustum.
            //----------------------------------------------------------------------------
            BOOL FRUSTUM::check_point(float x_pos, float y_pos, float z_pos)
            {
                
            // make sure point is in frustum
                for(short i = 0; i < 6; i++)
                {
                    
            if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3(x_pos, y_pos, z_pos)) < 0.0f)
                        
            return FALSE;
                }

                
            return TRUE;
            }

            //----------------------------------------------------------------------------
            // Check whether a cube in frustum, if total cube in frustum then 
            // completely_contained will be set TRUE.
            //----------------------------------------------------------------------------
            BOOL FRUSTUM::check_cube(float x_center, float y_center, float z_center, 
                                     
            float radius, 
                                     BOOL* completely_contained)
            {
                DWORD num_points_in_frustum = 0;

                
            // count the number of points inside the frustum
                for(short i = 0; i < 6; i++)
                {
                    DWORD count = 8;
                    BOOL in_all_planes = TRUE;

                    
            // test all eight points against plane

                    
            if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3(x_center - radius, y_center - radius, z_center - radius)) < 0.0f)
                    {
                        in_all_planes = FALSE;
                        count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3(x_center + radius, y_center - radius, z_center - radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3(x_center - radius, y_center + radius, z_center - radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3(x_center + radius, y_center + radius, z_center - radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3(x_center - radius, y_center - radius, z_center + radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3(x_center + radius, y_center - radius, z_center + radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3(x_center - radius, y_center + radius, z_center + radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3(x_center + radius, y_center + radius, z_center + radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            // if none contained, return FALSE.
                    if(count == 0)
                        
            return FALSE;

                    
            // update counter if they were all in front of plane.
                    if(in_all_planes)
                        ++num_points_in_frustum;
                }

                
            // store BOOL flag if completely contained
                if(completely_contained)
                    *completely_contained = (num_points_in_frustum == 6);

                
            return TRUE;
            }

            //----------------------------------------------------------------------------
            // Check whether a rectangle is in frustum, if total in then completely_contained
            // will be set TRUE.
            //----------------------------------------------------------------------------
            BOOL FRUSTUM::check_rectangle(float x_center, float y_center, float z_center, 
                                          
            float x_radius, float y_radius, float z_radius, 
                                          BOOL* completely_contained)
            {
                DWORD num_points_in_frustum = 0;

                
            // count the number of points inside the frustum
                for(short i = 0; i < 6; i++) 
                {
                    DWORD count = 8;
                    BOOL  in_all_planes = TRUE;
                    
                    
            // Test all eight points against plane

                    
            if(D3DXPlaneDotCoord(&m_planes[i], 
                        &D3DXVECTOR3(x_center - x_radius, y_center - y_radius, z_center - z_radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], 
                        &D3DXVECTOR3(x_center + x_radius, y_center - y_radius, z_center - z_radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], 
                        &D3DXVECTOR3(x_center - x_radius, y_center + y_radius, z_center - z_radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], 
                        &D3DXVECTOR3(x_center + x_radius, y_center + y_radius, z_center - z_radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], 
                        &D3DXVECTOR3(x_center - x_radius, y_center - y_radius, z_center + z_radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], 
                        &D3DXVECTOR3(x_center + x_radius, y_center - y_radius, z_center + z_radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], 
                        &D3DXVECTOR3(x_center - x_radius, y_center + y_radius, z_center + z_radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            if(D3DXPlaneDotCoord(&m_planes[i], 
                        &D3DXVECTOR3(x_center + x_radius, y_center + y_radius, z_center + z_radius)) < 0.0f) 
                    {
                      in_all_planes = FALSE;
                      count--;
                    }

                    
            // If none contained, return FALSE
                    if(count == 0)
                      
            return FALSE;

                    
            // Update counter if they were all in front of plane
                    if(in_all_planes)
                        ++num_points_in_frustum;
                }

                
            // Store BOOL flag if completely contained
                if(completely_contained)
                    *completely_contained = (num_points_in_frustum == 6);

                
            return TRUE;
            }

            //----------------------------------------------------------------------------
            // Check whether a sphere is in frustum. 
            //----------------------------------------------------------------------------
            BOOL FRUSTUM::check_sphere(float x_center, float y_center, float z_center, 
                                       
            float radius)
            {
                
            // make sure radius is in frustum
                for(short i = 0; i < 6; i++)
                {
                    
            if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3(x_center, y_center, z_center)) < -radius)
                        
            return FALSE;
                }

                
            return TRUE;
            }

            測試代碼:
            /************************************************************************************
            PURPOSE:
                frustum test.
            ************************************************************************************/


            #include "core_common.h"
            #include "core_framework.h"
            #include "core_graphics.h"
            #include "core_input.h"
            #include "frustum.h"

            #define MAX_OBJECTS 256

            class APP : public FRAMEWORK
            {
            public:
                BOOL init()
                {              
                    
            if(! create_display(g_hwnd, get_client_width(g_hwnd), get_client_height(g_hwnd), 16, TRUE, TRUE))
                        
            return FALSE;

                    set_perspective(D3DX_PI / 4, 1.3333f, 1.0f, 10000.0f);

                    
            // create font
                    m_font.create("Arial", 16, TRUE, FALSE);

                    
            // initialize input and input device
                    m_input.create(g_hwnd, get_window_inst());        
                    m_mouse.create(&m_input, MOUSE, TRUE);

                    
            // load mesh
                    if(! m_mesh.load("..\\Data\\Yodan.x", "..\\Data\\"))
                        
            return FALSE;

                    
            for(short i = 0; i < MAX_OBJECTS; i++)
                    {
                        m_objects[i].create(&m_mesh);

                        m_objects[i].move((
            float) (rand() % 4000) - 2000.0f, 0.0f, (float) (rand() % 4000) - 2000.0f);
                    }

                    
            return TRUE;
                }

                BOOL frame()
                {
                    
            // read input device data
                    m_mouse.read();

                    
            // position camera and rotate based on mouse position

                    m_camera.move(0.0f, 100.0f, 0.0f);

                    
            // m_mouse.get_y_delta():
                    //      get mouse's relative x movement coordinate.
                    //
                    // m_mouse.get_x_delta():
                    //      get mouse's relative y movement coordinate.

                    m_camera.rotate_rel(m_mouse.get_y_delta() / 200.0f, m_mouse.get_x_delta() / 200.0f, 0.0f);

                    
            // set view transform matrix
                    g_d3d_device->SetTransform(D3DTS_VIEW, m_camera.get_view_matrix());

                    
            // render everything
                    clear_display(D3DCOLOR_RGBA(0, 64, 128, 255), 1.0f);

                    
            // begin render now
                    if(SUCCEEDED(g_d3d_device->BeginScene()))
                    {
                        FRUSTUM frustum;
                        frustum.create(0.0f);

                        
            long num_drawn = 0;

                        
            // render each object in frustum
                        for(short i = 0; i < MAX_OBJECTS; i++)\
                        {
                            
            float radius;

                            m_objects[i].get_bounds(NULL, NULL, NULL, NULL, NULL, NULL, &radius);

                            
            if(frustum.check_sphere(m_objects[i].get_x_pos(), m_objects[i].get_y_pos(), m_objects[i].get_z_pos(),
                                                    radius))
                            {
                                m_objects[i].render();
                                num_drawn++;
                            }      
                        }               

                        
            char stats[128];

                        
            // display statistics
                        sprintf(stats, "%lu of 256 objects drawn.", num_drawn);
                        m_font.draw(stats, 0, 0, 400, 100, 0xFFFFFFFF, DT_LEFT | DT_TOP);

                        g_d3d_device->EndScene();            
                    }

                    present_display();        

                    
            return TRUE;
                }

                BOOL shutdown()
                {
                    
            return TRUE;
                }

            private:
                CAMERA          m_camera;
                FONT            m_font;

                INPUT           m_input;    
                INPUT_DEVICE    m_mouse;

                MESH            m_mesh;
                OBJECT          m_objects[MAX_OBJECTS];
            };

            int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
            {
                DWORD client_width  = 640;
                DWORD client_height = 480;
                DWORD x_pos = (get_screen_width()  - client_width) / 2;
                DWORD y_pos = (get_screen_height() - client_height) / 4;

                
            if(! build_window(inst, "frustum_class", "frustum test", 
                                  WS_BORDER | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
                                  x_pos, y_pos, client_width, client_height))
                {
                    
            return -1;
                }

                APP app;
                app.run();

                
            return 0;
            }
             

            posted on 2007-10-19 20:07 lovedday 閱讀(621) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            日韩久久久久中文字幕人妻| 伊人久久成人成综合网222| 色欲综合久久躁天天躁| 久久天天躁狠狠躁夜夜不卡| 国内精品九九久久精品 | 亚洲国产另类久久久精品小说| 日韩精品久久久久久久电影蜜臀| 亚洲精品美女久久久久99小说| 国产一级持黄大片99久久| 狠狠色丁香久久婷婷综| 国产精品美女久久久久AV福利 | 99国产欧美精品久久久蜜芽| 久久亚洲精品无码播放| 777午夜精品久久av蜜臀| 日本一区精品久久久久影院| 亚洲中文字幕无码久久精品1| 精品少妇人妻av无码久久| 亚洲国产精品无码久久久蜜芽| 久久99热国产这有精品| 欧美精品丝袜久久久中文字幕 | 中文字幕久久精品| 综合网日日天干夜夜久久| 嫩草影院久久国产精品| 伊人久久大香线蕉综合影院首页| 大蕉久久伊人中文字幕| 日本人妻丰满熟妇久久久久久| 亚洲国产日韩欧美综合久久| 99国内精品久久久久久久| 久久丫忘忧草产品| 2021国内精品久久久久久影院| 成人精品一区二区久久| 精品免费tv久久久久久久| 久久国产色AV免费观看| 久久久久高潮综合影院| 久久久久亚洲精品天堂| 亚洲AV无码久久精品色欲| 久久午夜夜伦鲁鲁片免费无码影视 | 久久r热这里有精品视频| 日韩av无码久久精品免费| 色99久久久久高潮综合影院| 青青青青久久精品国产|