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

            天行健 君子當自強而不息

            D3D中的網格模型(6)

            11.4 外接體(Bounding Volumes)

            有時我們需要計算mesh的外接體(邊界范圍),常用的有兩種類型:立方體和球。也有使用其它方法的,如圓柱體,橢球體,菱形體,膠囊形。圖11.4演示了對同一個mesh分別使用立方體和球體類型。

            邊界盒/球常常被用來加速可見性測試,碰撞檢測等。假如一個mesh的邊界盒/球不可見,那么我們就說mesh不可見。一個盒/球可見性測試是比分別測試 mesh中的每個三角形要廉價的多。對于一個碰撞檢測例子,如果一枚導彈點火起飛,我們需要檢測它是否擊中了同一場景中的目標。由于這些物體都是由大量三角形構成,我們可以依次檢測每個對象的每個三角形,來測試導彈(可以用射線數學模型)是否碰撞到了這些三角形。這個方法需要進行多次的射線/三角形交點的運算。一個更好的方法是使用邊界盒或邊界球,計算射線與場景中的每個對象的邊界盒/邊界球的交點。如果射線與對象的邊界范圍相交,可以認為該對象被擊中了。這是一個公平的近似方法,如果需要更高的精度,可以用邊界范圍法先去除那些明顯不會相撞的對象,然后用更精確地方法檢測很可能相撞的對象。如果邊界范圍檢測發現相撞,則該對象就很有可能相撞。

            D3DX庫提供了計算mesh的邊界盒和邊界球的函數。這些函數使用頂點數組作為輸入計算邊界盒/球。這些函數本來就是設計的很靈活的,它們可以使用各種頂點格式:

            HRESULT D3DXComputeBoundingSphere(

                   LPD3DXVECTOR3 pFirstPosition,

                   DWORD NumVertices,

                   DWORD dwStride,

                   D3DXVECTOR3* pCenter,

                   FLOAT* pRadius

            );

            pFirstPosition——指向在頂點數組中第一個頂點的向量,描述頂點位置。

            NumVertices——在頂點數組中的的頂點數。

            dwStride——每個頂點的字節大小。這很重要,因為頂點結構可能有一些額外信息如法向量和紋理坐標,函數需要知道應該跳過多少字節來得到下一個頂點的位置。

            pCenter——返回邊界球的中心。

            pRadius——返回邊界球的半徑。

            HRESULT D3DXComputeBoundingBox(

                   LPD3DXVECTOR3 pFirstPosition,

                   DWORD NumVertices,

                   DWORD dwStride,

                   D3DXVECTOR3* pMin,

                   D3DXVECTOR3* pMax

            );

            前三個參數和D3DXComputeBoundingSphere的前三個參數是完全一樣的。最后兩個參數分別用來返回邊界盒的最小和最大點。< /p>

             

            11.4.1一些新的特殊常量

            const float INFINITY = FLT_MAX;
            const float EPSILON = 0.001f;

            常量INFINITY是用來表示一個浮點數所能存儲的最大數。因為我們找不到一個比FLT_MAX還要大的浮點數,我們可以將它視為無窮大。常量 EPSILON是一個很小的值,我們這樣定義它,凡是比它小的數就視為0。這也是很有必要的,因為得到的浮點是不精確的。因此,讓它和0比較相等肯定會失敗。我們因此可以通過把該值與0的差值與EPSILON比較來確定是否相等:

            bool Equals(float lhs, float rhs)

            {

                   // if lhs == rhs their difference should be zero

                   return fabs(lhs - rhs) < EPSILON ? true : false;

            }

             

            11.4.2外接體類型

            為了更容易的使用邊界盒和邊界球,我們將它們分別封裝到兩個類中。

            class cBoundingBox
            {
            public:
                D3DXVECTOR3 m_min, m_max;
                
                cBoundingBox();
                
            bool is_point_inside(D3DXVECTOR3& point);
            };

            class cBoundingSphere
            {
            public:
                D3DXVECTOR3 m_center;
                
            float        m_radius;

                cBoundingSphere();
            };

            cBoundingBox::cBoundingBox()
            {    
                m_min.x 
            = INFINITY;
                m_min.y 
            = INFINITY;
                m_min.z 
            = INFINITY;

                m_max.x 
            = -INFINITY;
                m_max.y 
            = -INFINITY;
                m_max.z 
            = -INFINITY;
            }

            bool cBoundingBox::is_point_inside(D3DXVECTOR3 &point)
            {
                
            return (point.x >= m_min.x && point.y >= m_min.y && point.z >= m_min.z &&
                        point.x 
            <= m_max.x && point.y <= m_max.y && point.z <= m_max.z);
            }

            cBoundingSphere::cBoundingSphere()
            {
                m_radius 
            = 0.0f;
            }

             

            11.4.3實例程序:外接體

            實例程序主要演示使用D3DXComputeBoundingSphere和 D3DXComputeBoundingBox。程序讀取一個X文件并且計算該mesh的邊界球,它創建兩個ID3DXMesh對象,一個用來作為邊界球模型一個用來作為邊界盒模型(如下圖所示)。你能夠通過敲空格鍵在邊界球和邊界盒之間切換。



            主程序:

            /**************************************************************************************
              Demonstrates how to use D3DXComputeBoundingSphere and D3DXComputeBoundingBox.
              The spacebar key switches between rendering the mesh's bounding sphere and box.  
             *************************************************************************************
            */

            #include 
            <vector>
            #include 
            "d3dUtility.h"

            #pragma warning(disable : 
            4100)

            using namespace std;

            const int WIDTH  = 640;
            const int HEIGHT = 480;

            IDirect3DDevice9
            *            g_device;
            ID3DXMesh
            *                    g_mesh;
            ID3DXMesh
            *                    g_sphere_mesh;
            ID3DXMesh
            *                    g_box_mesh;
            vector
            <D3DMATERIAL9>        g_materials;
            vector
            <IDirect3DTexture9*>    g_textures;

            bool g_is_render_bounding_sphere = true;

            bool compute_bounding_sphere(ID3DXMesh* mesh, cBoundingSphere* sphere);
            bool compute_bounding_box(ID3DXMesh* mesh, cBoundingBox* box);

            ////////////////////////////////////////////////////////////////////////////////////////////////////

            bool setup()
            {    
                
            // load the XFile data

                ID3DXBuffer
            *    adjacency_buffer = NULL;
                ID3DXBuffer
            *    material_buffer  = NULL;
                DWORD            num_material     
            = 0;

                HRESULT hr 
            = D3DXLoadMeshFromX("bigship1.x", D3DXMESH_MANAGED, g_device, &adjacency_buffer, &material_buffer,
                                               NULL, 
            &num_material, &g_mesh);

                
            if(FAILED(hr))
                {
                    MessageBox(NULL, 
            "D3DXLoadMeshFromX() - FAILED""ERROR", MB_OK);
                    
            return false;
                }

                
            // extract the materials, and load textures.
                if(material_buffer != NULL && num_material != 0)
                {
                    D3DXMATERIAL
            * materials = (D3DXMATERIAL*) material_buffer->GetBufferPointer();

                    
            for(DWORD i = 0; i < num_material; i++)
                    {
                        
            // the MatD3D property doesn't have an ambient value set when it load, so set it now.
                        materials[i].MatD3D.Ambient = materials[i].MatD3D.Diffuse;

                        
            // save the ith material
                        g_materials.push_back(materials[i].MatD3D);

                        
            // check if the ith material has an associative texture
                        if(materials[i].pTextureFilename != NULL)
                        {
                            
            // yes, load the texture for the ith subset.
                            IDirect3DTexture9* texture;
                            D3DXCreateTextureFromFile(g_device, materials[i].pTextureFilename, 
            &texture);

                            
            // save the loaded texture
                            g_textures.push_back(texture);
                        }
                        
            else
                        {
                            
            // no texture for the ith subset
                            g_textures.push_back(NULL);
                        }
                    }
                }

                safe_release
            <ID3DXBuffer*>(material_buffer);
                
                
            // optimize the mesh
                
                hr 
            = g_mesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE,
                    (DWORD
            *) adjacency_buffer->GetBufferPointer(), NULL, NULL, NULL);

                safe_release
            <ID3DXBuffer*>(adjacency_buffer);

                
            if(FAILED(hr))
                {
                    MessageBox(NULL, 
            "OptimizeInplace() - FAILED""ERROR", MB_OK);
                    
            return false;
                }

                
            // compute bounding sphere and bounding box

                cBoundingSphere bounding_sphere;
                cBoundingBox    bounding_box;

                compute_bounding_sphere(g_mesh, 
            &bounding_sphere);
                compute_bounding_box(g_mesh, 
            &bounding_box);

                D3DXCreateSphere(g_device, bounding_sphere.m_radius, 
            2020&g_sphere_mesh, NULL);

                
            float width  = bounding_box.m_max.x - bounding_box.m_min.x;
                
            float height = bounding_box.m_max.y - bounding_box.m_min.y;
                
            float depth  = bounding_box.m_max.z - bounding_box.m_min.z;

                D3DXCreateBox(g_device, width, height, depth, 
            &g_box_mesh, NULL);

                
            // set texture filters    
                g_device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
                g_device
            ->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
                g_device
            ->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

                
            // set lights

                D3DXVECTOR3 dir(
            1.0f-1.0f1.0f);
                D3DXCOLOR color(
            1.0f1.0f1.0f1.0f);
                D3DLIGHT9 light 
            = init_directional_light(&dir, &color);

                g_device
            ->SetLight(0&light);
                g_device
            ->LightEnable(0, TRUE);
                g_device
            ->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
                g_device
            ->SetRenderState(D3DRS_SPECULARENABLE, TRUE);

                
            // set camera

                D3DXVECTOR3 pos(
            4.0f12.0f-20.0f);
                D3DXVECTOR3 target(
            0.0f0.0f0.0f);
                D3DXVECTOR3 up(
            0.0f1.0f0.0f);

                D3DXMATRIX view_matrix;
                D3DXMatrixLookAtLH(
            &view_matrix, &pos, &target, &up);
                g_device
            ->SetTransform(D3DTS_VIEW, &view_matrix);

                
            // set the projection matrix
                D3DXMATRIX proj;
                D3DXMatrixPerspectiveFovLH(
            &proj, D3DX_PI * 0.5f, (float)WIDTH/HEIGHT, 1.0f1000.0f);
                g_device
            ->SetTransform(D3DTS_PROJECTION, &proj);
                
                
            return true;
            }

            ///////////////////////////////////////////////////////////////////////////////////////////////////////

            void cleanup()
            {    
                safe_release
            <ID3DXMesh*>(g_mesh);
                safe_release
            <ID3DXMesh*>(g_sphere_mesh);
                safe_release
            <ID3DXMesh*>(g_box_mesh);
                
                
            for(DWORD i = 0; i < g_textures.size(); i++)
                    safe_release
            <IDirect3DTexture9*>(g_textures[i]);
            }

            ///////////////////////////////////////////////////////////////////////////////////////////////////////

            bool display(float time_delta)
            {
                
            // update: rotate the mesh

                
            static float y = 0.0f;

                D3DXMATRIX y_rot_matrix;    
                D3DXMatrixRotationY(
            &y_rot_matrix, y);
                g_device
            ->SetTransform(D3DTS_WORLD, &y_rot_matrix);

                y 
            += time_delta;
                
            if(y >= 6.28f)
                    y 
            = 0.0f;

                
            // render now

                g_device
            ->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x000000001.0f0);

                g_device
            ->BeginScene();

                
            for(DWORD i = 0; i < g_materials.size(); i++)
                {
                    g_device
            ->SetMaterial(&g_materials[i]);
                    g_device
            ->SetTexture(0, g_textures[i]);
                    g_mesh
            ->DrawSubset(i);
                }

                
            // draw bounding volume in yellow and at 30% opacity
                D3DMATERIAL9 blue_material = YELLOW_MATERIAL;
                blue_material.Diffuse.a 
            = 0.30f;    // 30% opacity
                g_device->SetMaterial(&blue_material);

                g_device
            ->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
                g_device
            ->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA);
                g_device
            ->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

                
            if(g_is_render_bounding_sphere)
                    g_sphere_mesh
            ->DrawSubset(0);
                
            else
                    g_box_mesh
            ->DrawSubset(0);

                g_device
            ->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);

                g_device
            ->EndScene();

                g_device
            ->Present(NULL, NULL, NULL, NULL);

                
            return true;
            }

            ///////////////////////////////////////////////////////////////////////////////////////////////////////

            LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM word_param, LPARAM long_param)
            {
                
            switch(msg)
                {
                
            case WM_DESTROY:
                    PostQuitMessage(
            0);
                    
            break;

                
            case WM_KEYDOWN:
                    
            if(word_param == VK_ESCAPE)
                        DestroyWindow(hwnd);

                    
            if(word_param == VK_SPACE)
                        g_is_render_bounding_sphere 
            = !g_is_render_bounding_sphere;

                    
            break;
                }

                
            return DefWindowProc(hwnd, msg, word_param, long_param);
            }

            ///////////////////////////////////////////////////////////////////////////////////////////////////////

            int WINAPI WinMain(HINSTANCE inst, HINSTANCE, PSTR cmd_line, int cmd_show)
            {
                
            if(! init_d3d(inst, WIDTH, HEIGHT, true, D3DDEVTYPE_HAL, &g_device))
                {
                    MessageBox(NULL, 
            "init_d3d() - failed."0, MB_OK);
                    
            return 0;
                }

                
            if(! setup())
                {
                    MessageBox(NULL, 
            "Steup() - failed."0, MB_OK);
                    
            return 0;
                }

                enter_msg_loop(display);

                cleanup();
                g_device
            ->Release();

                
            return 0;
            }

            ///////////////////////////////////////////////////////////////////////////////////////////////////////

            bool compute_bounding_sphere(ID3DXMesh* mesh, cBoundingSphere* sphere)
            {
                BYTE
            * v;

                mesh
            ->LockVertexBuffer(0, (void**)&v);

                HRESULT hr 
            = D3DXComputeBoundingSphere((D3DXVECTOR3*)v, mesh->GetNumVertices(),
                    D3DXGetFVFVertexSize(mesh
            ->GetFVF()), &sphere->m_center, &sphere->m_radius);

                mesh
            ->UnlockVertexBuffer();

                
            if(FAILED(hr))
                    
            return false;

                
            return true;
            }

            bool compute_bounding_box(ID3DXMesh* mesh, cBoundingBox* box)
            {
                BYTE
            * v;

                mesh
            ->LockVertexBuffer(0, (void**)&v);

                HRESULT hr 
            = D3DXComputeBoundingBox((D3DXVECTOR3*)v, mesh->GetNumVertices(),
                    D3DXGetFVFVertexSize(mesh
            ->GetFVF()), &box->m_min, &box->m_max);

                mesh
            ->UnlockVertexBuffer();

                
            if(FAILED(hr))
                    
            return false;

                
            return true;
            }

             


            下載源代碼

            posted on 2008-03-29 19:16 lovedday 閱讀(2364) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            香蕉99久久国产综合精品宅男自 | 97久久精品国产精品青草| 久久精品国产精品亚洲毛片| 狠狠色综合久久久久尤物| 国产激情久久久久久熟女老人| 久久精品国产一区| 亚洲国产精品综合久久网络| 99久久伊人精品综合观看| 久久综合鬼色88久久精品综合自在自线噜噜 | 99久久精品日本一区二区免费| 久久www免费人成精品香蕉| 性做久久久久久久| 欧美激情精品久久久久久久九九九| 午夜精品久久影院蜜桃| 日本精品一区二区久久久| 久久99精品国产99久久6| 久久99亚洲综合精品首页| 久久国产精品-国产精品| 77777亚洲午夜久久多人| 国产综合久久久久久鬼色| 久久无码人妻精品一区二区三区| 国产成人精品免费久久久久| 久久久久亚洲av无码专区| 久久精品人人槡人妻人人玩AV| 国产精品美女久久久久网| 久久精品嫩草影院| 国产精品无码久久综合网| 久久影视综合亚洲| 亚洲午夜久久久久久噜噜噜| 99久久99久久| 国产91久久综合| 日韩影院久久| 99久久精品午夜一区二区 | 亚洲狠狠婷婷综合久久蜜芽| 色狠狠久久综合网| 久久久高清免费视频| 久久综合给久久狠狠97色| 久久99毛片免费观看不卡| 99久久免费只有精品国产| 久久99精品国产麻豆蜜芽| 亚洲欧洲日产国码无码久久99|