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

            D3D中的網(wǎng)格模型(6)

            11.4 外接體(Bounding Volumes)

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

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

            D3DX庫(kù)提供了計(jì)算mesh的邊界盒和邊界球的函數(shù)。這些函數(shù)使用頂點(diǎn)數(shù)組作為輸入計(jì)算邊界盒/球。這些函數(shù)本來(lái)就是設(shè)計(jì)的很靈活的,它們可以使用各種頂點(diǎn)格式:

            HRESULT D3DXComputeBoundingSphere(

                   LPD3DXVECTOR3 pFirstPosition,

                   DWORD NumVertices,

                   DWORD dwStride,

                   D3DXVECTOR3* pCenter,

                   FLOAT* pRadius

            );

            pFirstPosition——指向在頂點(diǎn)數(shù)組中第一個(gè)頂點(diǎn)的向量,描述頂點(diǎn)位置。

            NumVertices——在頂點(diǎn)數(shù)組中的的頂點(diǎn)數(shù)。

            dwStride——每個(gè)頂點(diǎn)的字節(jié)大小。這很重要,因?yàn)轫旤c(diǎn)結(jié)構(gòu)可能有一些額外信息如法向量和紋理坐標(biāo),函數(shù)需要知道應(yīng)該跳過(guò)多少字節(jié)來(lái)得到下一個(gè)頂點(diǎn)的位置。

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

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

            HRESULT D3DXComputeBoundingBox(

                   LPD3DXVECTOR3 pFirstPosition,

                   DWORD NumVertices,

                   DWORD dwStride,

                   D3DXVECTOR3* pMin,

                   D3DXVECTOR3* pMax

            );

            前三個(gè)參數(shù)和D3DXComputeBoundingSphere的前三個(gè)參數(shù)是完全一樣的。最后兩個(gè)參數(shù)分別用來(lái)返回邊界盒的最小和最大點(diǎn)。< /p>

             

            11.4.1一些新的特殊常量

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

            常量INFINITY是用來(lái)表示一個(gè)浮點(diǎn)數(shù)所能存儲(chǔ)的最大數(shù)。因?yàn)槲覀冋也坏揭粋€(gè)比FLT_MAX還要大的浮點(diǎn)數(shù),我們可以將它視為無(wú)窮大。常量 EPSILON是一個(gè)很小的值,我們這樣定義它,凡是比它小的數(shù)就視為0。這也是很有必要的,因?yàn)榈玫降母↑c(diǎn)是不精確的。因此,讓它和0比較相等肯定會(huì)失敗。我們因此可以通過(guò)把該值與0的差值與EPSILON比較來(lái)確定是否相等:

            bool Equals(float lhs, float rhs)

            {

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

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

            }

             

            11.4.2外接體類型

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

            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實(shí)例程序:外接體

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



            主程序:

            /**************************************************************************************
              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) 評(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无码久久精品成人 | 亚洲国产精品成人AV无码久久综合影院| 久久99久久99小草精品免视看| 久久99精品国产99久久6| 日韩久久无码免费毛片软件 | 久久综合久久综合九色| 久久久人妻精品无码一区| 亚洲精品高清国产一线久久| 亚洲综合精品香蕉久久网97| 久久毛片一区二区| 狠狠狠色丁香婷婷综合久久五月| 人妻无码久久精品| 91精品国产91久久综合| 久久夜色精品国产| 亚洲狠狠久久综合一区77777| 精产国品久久一二三产区区别| 亚洲综合精品香蕉久久网97| 三上悠亚久久精品| 久久精品中文字幕大胸| 久久精品三级视频| 7国产欧美日韩综合天堂中文久久久久 | 欧美亚洲国产精品久久| 久久精品国产只有精品66| 精品久久久久久久| 久久久免费精品re6| 99久久国产综合精品女同图片| 久久久久久亚洲精品不卡| 中文字幕亚洲综合久久2| www性久久久com| 久久精品国产清高在天天线| 一本色道久久88—综合亚洲精品| 人妻少妇精品久久| 久久夜色精品国产| 免费一级欧美大片久久网 | AV无码久久久久不卡蜜桃| 97精品依人久久久大香线蕉97 | 久久久久久国产a免费观看黄色大片| 久久99精品久久久久久不卡 | 久久超乳爆乳中文字幕| 久久久精品国产sm调教网站| 97久久久精品综合88久久|