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

            天行健 君子當自強而不息

            XFile網格的應用(2)


            本篇是XFile網格的應用(1)的續(xù)篇。

            .X文件的數據裝入

            在上一篇的.X文件中,主場景框架 Scene_Root提供了一個變換矩陣和一個子框架Quad。這個Quad具有一個Mesh網格(包括頂點,材質,紋理等數據),其中 Scene_Root框架的變換矩陣(實際為單位矩陣)說明了Quad子框架的Mesh網格數據應做的變換,以正確擺放在父框架的空間中(這里指世界空間)。對于僅含有單一網格的.X文件來說,利用D3DXLoadMeshFromX函數可實現簡單網格數據的加載。

            來看看D3DXLoadMeshFromX的具體使用說明:

            Loads a mesh from a DirectX .x file.

             HRESULT D3DXLoadMeshFromX( 
            LPCTSTR pFilename ,
            DWORD Options ,
            LPDIRECT3DDEVICE9 pD3DDevice ,
            LPD3DXBUFFER * ppAdjacency ,
            LPD3DXBUFFER * ppMaterials ,
            LPD3DXBUFFER * ppEffectInstances ,
            DWORD * pNumMaterials ,
            LPD3DXMESH * ppMesh
            );

            Parameters

            pFilename
            [in] Pointer to a string that specifies the filename. If the compiler settings require Unicode, the data type LPCTSTR resolves to LPCWSTR. Otherwise, the string data type resolves to LPCSTR. See Remarks.
            Options
            [in] Combination of one or more flags from the D3DXMESH enumeration, which specifies creation options for the mesh.
            pD3DDevice
            [in] Pointer to an IDirect3DDevice9 interface, the device object associated with the mesh.
            ppAdjacency
            [out] Pointer to a buffer that contains adjacency data. The adjacency data contains an array of three DWORDs per face that specify the three neighbors for each face in the mesh. For more information about accessing the buffer, see ID3DXBuffer.
            ppMaterials
            [out] Pointer to a buffer containing materials data. The buffer contains an array of D3DXMATERIAL structures, containing information from the DirectX file. For more information about accessing the buffer, see ID3DXBuffer .
            ppEffectInstances
            [out] Pointer to a buffer containing an array of effect instances, one per attribute group in the returned mesh. An effect instance is a particular instance of state information used to initialize an effect. See D3DXEFFECTINSTANCE. For more information about accessing the buffer, see ID3DXBuffer .
            pNumMaterials
            [out] Pointer to the number of D3DXMATERIAL structures in the ppMaterials array, when the method returns.
            ppMesh
            [out] Address of a pointer to an ID3DXMesh interface, representing the loaded mesh.

            Return Values

            If the function succeeds, the return value is D3D_OK. If the function fails, the return value can be one of the following values: D3DERR_INVALIDCALL, E_OUTOFMEMORY.

            Remarks

            The compiler setting also determines the function version. If Unicode is defined, the function call resolves to D3DXLoadMeshFromXW. Otherwise, the function call resolves to D3DXLoadMeshFromXA because ANSI strings are being used.

            All the meshes in the file will be collapsed into one output mesh. If the file contains a frame hierarchy, all the transformations will be applied to the mesh.

            For mesh files that do not contain effect instance information, default effect instances will be generated from the material information in the .x file. A default effect instance will have default values that correspond to the members of the D3DMATERIAL9 structure.

            The default texture name is also filled in, but is handled differently. The name will be Texture0@Name, which corresponds to an effect variable by the name of "Texture0" with an annotation called "Name." This will contain the string file name for the texture.


            來看看參數Option可以指定的網格創(chuàng)建模式:

            Flags used to specify creation options for a mesh.

            typedef enum D3DXMESH
            {
            D3DXMESH_32BIT = 0x001,
            D3DXMESH_DONOTCLIP = 0x002,
            D3DXMESH_POINTS = 0x004,
            D3DXMESH_RTPATCHES = 0x008,
            D3DXMESH_NPATCHES = 0x4000,
            D3DXMESH_VB_SYSTEMMEM = 0x010,
            D3DXMESH_VB_MANAGED = 0x020,
            D3DXMESH_VB_WRITEONLY = 0x040,
            D3DXMESH_VB_DYNAMIC = 0x080,
            D3DXMESH_VB_SOFTWAREPROCESSING = 0x8000,
            D3DXMESH_IB_SYSTEMMEM = 0x100,
            D3DXMESH_IB_MANAGED = 0x200,
            D3DXMESH_IB_WRITEONLY = 0x400,
            D3DXMESH_IB_DYNAMIC = 0x800,
            D3DXMESH_IB_SOFTWAREPROCESSING = 0x10000,
            D3DXMESH_VB_SHARE = 0x1000,
            D3DXMESH_USEHWONLY = 0x2000,
            D3DXMESH_SYSTEMMEM = 0x110,
            D3DXMESH_MANAGED = 0x220,
            D3DXMESH_WRITEONLY = 0x440,
            D3DXMESH_DYNAMIC = 0x880,
            D3DXMESH_SOFTWAREPROCESSING = 0x18000,
            } D3DXMESH, *LPD3DXMESH;

            Constants

            D3DXMESH_32BIT
            The mesh has 32-bit indices instead of 16-bit indices. See Remarks.
            D3DXMESH_DONOTCLIP
            Use the D3DUSAGE_DONOTCLIP usage flag for vertex and index buffers.
            D3DXMESH_POINTS
            Use the D3DUSAGE_POINTS usage flag for vertex and index buffers.
            D3DXMESH_RTPATCHES
            Use the D3DUSAGE_RTPATCHES usage flag for vertex and index buffers.
            D3DXMESH_NPATCHES
            Specifying this flag causes the vertex and index buffer of the mesh to be created with D3DUSAGE_NPATCHES flag. This is required if the mesh object is to be rendered using N-patch enhancement using Direct3D.
            D3DXMESH_VB_SYSTEMMEM
            Use the D3DPOOL_SYSTEMMEM usage flag for vertex buffers.
            D3DXMESH_VB_MANAGED
            Use the D3DPOOL_MANAGED usage flag for vertex buffers.
            D3DXMESH_VB_WRITEONLY
            Use the D3DUSAGE_WRITEONLY usage flag for vertex buffers.
            D3DXMESH_VB_DYNAMIC
            Use the D3DUSAGE_DYNAMIC usage flag for vertex buffers.
            D3DXMESH_VB_SOFTWAREPROCESSING
            Use the D3DUSAGE_SOFTWAREPROCESSING usage flag for vertex buffers.
            D3DXMESH_IB_SYSTEMMEM
            Use the D3DPOOL_SYSTEMMEM usage flag for index buffers.
            D3DXMESH_IB_MANAGED
            Use the D3DPOOL_MANAGED usage flag for index buffers.
            D3DXMESH_IB_WRITEONLY
            Use the D3DUSAGE_WRITEONLY usage flag for index buffers.
            D3DXMESH_IB_DYNAMIC
            Use the D3DUSAGE_DYNAMIC usage flag for index buffers.
            D3DXMESH_IB_SOFTWAREPROCESSING
            Use the D3DUSAGE_SOFTWAREPROCESSING usage flag for index buffers.
            D3DXMESH_VB_SHARE
            Forces the cloned meshes to share vertex buffers.
            D3DXMESH_USEHWONLY
            Use hardware processing only. For mixed-mode device, this flag will cause the system to use hardware (if supported in hardware) or will default to software processing.
            D3DXMESH_SYSTEMMEM
            Equivalent to specifying both D3DXMESH_VB_SYSTEMMEM and D3DXMESH_IB_SYSTEMMEM.
            D3DXMESH_MANAGED
            Equivalent to specifying both D3DXMESH_VB_MANAGED and D3DXMESH_IB_MANAGED.
            D3DXMESH_WRITEONLY
            Equivalent to specifying both D3DXMESH_VB_WRITEONLY and D3DXMESH_IB_WRITEONLY.
            D3DXMESH_DYNAMIC
            Equivalent to specifying both D3DXMESH_VB_DYNAMIC and D3DXMESH_IB_DYNAMIC.
            D3DXMESH_SOFTWAREPROCESSING
            Equivalent to specifying both D3DXMESH_VB_SOFTWAREPROCESSING and D3DXMESH_IB_SOFTWAREPROCESSING.

            Remarks

            A 32-bit mesh (D3DXMESH_32BIT) can theoretically support (2^32)-1 faces and vertices. However, allocating memory for a mesh that large on a 32-bit operating system is not practical.


            參數ppAdjacency, ppMaterials, ppEffectInstances的類型都是LPD3DXBUFFER,它實際上是指向ID3DXBUFFER的指針:

            The ID3DXBuffer interface is used as a data buffer, storing vertex, adjacency, and material information during mesh optimization and loading operations. The buffer object is used to return arbitrary length data. Also, buffer objects are used to return object code and error messages in methods that assemble vertex and pixel shaders.

            ID3DXBuffer Members

            Method Description
            ID3DXBuffer::GetBufferPointer Retrieves a pointer to the data in the buffer.
            ID3DXBuffer::GetBufferSize Retrieves the total size of the data in the buffer.

            Remarks

            The ID3DXBuffer interface is obtained by calling the D3DXCreateBuffer function.

            The LPD3DXBUFFER type is defined as a pointer to the ID3DXBuffer interface.

            typedef interface ID3DXBuffer ID3DXBuffer;
            typedef interface ID3DXBuffer *LPD3DXBUFFER;

            來看看ID3DXBuffer提供的兩個方法,先來看看GetBufferPointer的具體使用信息:
             
            Retrieves a pointer to the data in the buffer.

            LPVOID GetBufferPointer();

            Parameters
            None.

            Return Values
            Returns a pointer to the data in the buffer.

             

            再來看看GetBufferSize的具體使用信息:

            Retrieves the total size of the data in the buffer.

            DWORD GetBufferSize();

            Parameters
            None.

            Return Values
            Returns the total size of the data in the buffer, in bytes.


            ID3DXBuffer可利用DirectX API函數D3DXCreateBuffer來創(chuàng)建,不過,一般不需要直接創(chuàng)建,下面是該函數的具體使用信息:

            Creates a buffer object.

            HRESULT D3DXCreateBuffer(
              DWORD NumBytes,
              LPD3DXBUFFER * ppBuffer
            );

            Parameters

            NumBytes
            [in] Size of the buffer to create, in bytes.

            ppBuffer
            [out] Address of a pointer to an ID3DXBuffer interface, representing the created buffer object.

            Return Values

            If the function succeeds, the return value is D3D_OK. If the function fails, the return value can be one of the following: E_OUTOFMEMORY.


            利用pNumMaterials指針參數可獲得網格mesh所使用的材質的數量,ppMesh參數則是返回的一個ID3DXMESH 接口的指針地址,利用ID3DXMESH 接口提供的方法,可對裝入的網格數據進行讀取,優(yōu)化和渲染等。

            由于利用D3DXLoadMeshFromX函數裝入的材質和紋理貼圖文件名被封裝在一個擴展的D3DXMATERIAL結構體內,因此通常需要將材質取出,放入材質數組中,將紋理貼圖文件名取出,創(chuàng)建相應的紋理對象,再將這些紋理對象放入紋理對象數組中,以方便后續(xù)的紋理網格渲染。

            來看看D3DXMATERIAL的具體信息:
             
            Returns material information saved in Direct3D (.x) files.

            typedef struct D3DXMATERIAL {
                D3DMATERIAL9 MatD3D;
                LPSTR pTextureFilename;
            } D3DXMATERIAL, *LPD3DXMATERIAL;

            Members

            MatD3D
            D3DMATERIAL9 structure that describes the material properties.

            pTextureFilename
            Pointer to a string that specifies the file name of the texture.

            Remarks

            The D3DXLoadMeshFromX and D3DXLoadMeshFromXof functions return an array of D3DXMATERIAL structures that specify the material color and name of the texture for each material in the mesh. The application is then required to load the texture.

            The LPD3DXMATERIAL type is defined as a pointer to the D3DXMATERIAL structure.

            typedef struct D3DXMATERIAL* LPD3DXMATERIAL;

             

            其中用到了結構體D3DMATERIAL9,它的具體定義是:
             
            Specifies material properties.

            typedef struct D3DMATERIAL9 {
                D3DCOLORVALUE Diffuse;
                D3DCOLORVALUE Ambient;
                D3DCOLORVALUE Specular;
                D3DCOLORVALUE Emissive;
                float Power;
            } D3DMATERIAL9, *LPD3DMATERIAL9;

            Members

            Diffuse
            Value specifying the diffuse color of the material. See D3DCOLORVALUE.

            Ambient
            Value specifying the ambient color of the material. See D3DCOLORVALUE.

            Specular
            Value specifying the specular color of the material. See D3DCOLORVALUE.

            Emissive
            Value specifying the emissive color of the material. See D3DCOLORVALUE.

            Power
            Floating-point value specifying the sharpness of specular highlights. The higher the value, the sharper the highlight.

            Remarks

            To turn off specular highlights, set D3DRS_SPECULARENABLE to FALSE, using D3DRENDERSTATETYPE. This is the fastest option because no specular highlights will be calculated.

            For more information about using the lighting engine to calculate specular lighting, see Specular Lighting (Direct3D 9).
             

            下面的代碼演示了怎樣加載.X文件,以及加載后怎樣獲得相應的數據。
             IDirect3D9*         _d3d;
             IDirect3DDevice9*   _d3d_device;
             ID3DXBuffer*        _adjacency_buffer;
             ID3DXBuffer*        _material_buffer;
             D3DMATERIAL9*       _material_array;
             IDirect3DTexture9** _texture_array;
             DWORD               _num_materials;
             ID3DXMesh*          _mesh;
            //------------------------------------------------------------------------------------
            // Load .X file
            //------------------------------------------------------------------------------------
            bool BASIC_XFILE::Load_XFile(char* x_filename)
            {
                // Loads a mesh from a DirectX .x file
                if(FAILED(D3DXLoadMeshFromX(
                    x_filename,             // Pointer to a string that specifies the filename
                    D3DXMESH_MANAGED,       // specifies creation options for the mesh
                    _d3d_device,            // Pointer to an IDirect3DDevice9 interface
                    &_adjacency_buffer,     // Pointer to a buffer that contains adjacency data
                    &_material_buffer,      // Pointer to a buffer containing materials data
                    NULL,                   // Pointer to a buffer containing an array of effect instances
                    &_num_materials,        // Pointer to the number of D3DXMATERIAL structures 
                    &_mesh)))               // Address of a pointer to an ID3DXMesh interface
                {
                    MessageBox(NULL, "Load .X file failed.", "ERROR", MB_OK);
                    return false;
                }
                // invalid data
                if(_material_buffer == NULL || _num_materials == 0)
                    return false;
                // retrieves a pointer to the data in the buffer
                D3DXMATERIAL* material = (D3DXMATERIAL*) _material_buffer->GetBufferPointer();
                if(material != NULL)
                {
                    // allocate memory for material array and texture array
                    _material_array = new D3DMATERIAL9[_num_materials];
                    _texture_array  = new IDirect3DTexture9*[_num_materials];    
                    for(DWORD i = 0; i < _num_materials; i++)
                    {
                        // assign material to array
                        _material_array[i] = material[i].MatD3D;
                        if(material[i].pTextureFilename != NULL)
                        {
                            if(FAILED(D3DXCreateTextureFromFile(_d3d_device, material[i].pTextureFilename, &_texture_array[i])))
                                _texture_array[i] = NULL;
                        }
                    }
                }
                // Generates a mesh with reordered faces and vertices to optimize drawing performance. 
                // This method reorders the existing mesh.
                _mesh->OptimizeInplace(D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
                    (DWORD*) _adjacency_buffer->GetBufferPointer(), NULL, NULL, NULL);
                _material_buffer->Release();
                _adjacency_buffer->Release();
                return true;
            }

            Mesh數據的處理

            執(zhí)行D3DXLoadMeshFromX函數完成.X文件的加載后,Mesh的各種數據將存放在頂點緩沖區(qū),頂點索引緩沖區(qū),材質緩沖區(qū),鄰接頂點緩沖區(qū),特效屬性緩沖區(qū),網格緩沖區(qū)中。其中,材質緩沖區(qū)(包含材質信息和紋理貼圖文件名信息),鄰接頂點緩沖區(qū)以及特效屬性緩沖區(qū)的地址由 D3DXLoadMeshFromX函數成功執(zhí)行后返回,其他的緩沖區(qū)地址可利用ID3DXMesh接口函數來獲得。

            .X網格的渲染和優(yōu)化就是使用上面緩沖區(qū)的數據來進行的。頂點緩沖區(qū)給出了整個網格的頂點坐標列表(包括頂點坐標和頂點紋理坐標),頂點索引緩沖區(qū)給出了每個三角形面的頂點構成信息(三個頂點的索引值),從而間接給出了整個網格的所有三角形面的頂點信息說明,屬性緩沖區(qū)為每個三角形面的一個網格子集編號,即給出了網格的每個三角形面的分組號。具有同一個Subset編號的三角形,將使用相同的材質和紋理對象進行渲染。

            Mesh網格的渲染,可以用一個for循環(huán)來進行,循環(huán)的次數為總的材質數。每一次循環(huán)都將那些具有相同Subset子集號的三角形面渲染出來,即渲染每個子集Subset。不過每次循環(huán)首先要設置渲染管道線的材質和紋理對象,如下所示:
            //------------------------------------------------------------------------------------
            // Render mesh.
            //------------------------------------------------------------------------------------
            void BASIC_XFILE::Render()
            {
                // Clear a render target and the depth buffer
                _d3d_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
                _d3d_device->BeginScene();
                // draw all face in the mesh
                for(DWORD i = 0; i < _num_materials; i++)
                {
                    // Sets the material properties for the device
                    _d3d_device->SetMaterial(&_material_array[i]);
                    // Assigns a texture to a stage for a device
                    _d3d_device->SetTexture(0, _texture_array[i]);
                    // Draws a subset of a mesh
                    _mesh->DrawSubset(i);
                }
                _d3d_device->EndScene();
                // Presents the contents of the next buffer in the sequence of back buffers owned by the device
                _d3d_device->Present(NULL, NULL, NULL, NULL);
            }

            上面的代碼中使用Clear對Z緩沖區(qū)進行了清除,因此必須在初始化D3D設備時,開啟Z緩沖區(qū),否則該函數調用失敗并且屏幕將出現重影。

            D3DPRESENT_PARAMETERS present_param;

            present_param.EnableAutoDepthStencil = TRUE;


            如果沒有加入上面的代碼,則出現重影,如下圖所示:



            Render函數中使用DrawSubset函數來渲染每一個Mesh子集,該函數只需要指定一個子集號,就可將網格中具有該子集號的三角形面渲染出來,下面是該函數的具體使用信息:
             
            Draws a subset of a mesh.

            HRESULT DrawSubset(
              DWORD AttribId
            );

            Parameters

            AttribId
            [in] DWORD that specifies which subset of the mesh to draw. This value is used to differentiate faces in a mesh as belonging to one or more attribute groups.

            Return Values

            If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

            Remarks

            The subset that is specified by AttribId will be rendered by the IDirect3DDevice9::DrawIndexedPrimitive method, using the D3DPT_TRIANGLELIST primitive type, so an index buffer must be properly initialized.

            An attribute table is used to identify areas of the mesh that need to be drawn with different textures, render states, materials, and so on. In addition, the application can use the attribute table to hide portions of a mesh by not drawing a given attribute identifier (AttribId) when drawing the frame.

             
            閱讀下篇:XFile網格的應用(3)

             

            posted on 2007-05-18 20:05 lovedday 閱讀(2051) 評論(4)  編輯 收藏 引用

            評論

            # re: XFile網格的應用(2) 2007-07-26 00:07 小禹

            看了您的文章,感覺寫的很清楚明了。在此感謝。

            有一個問題,我一直沒有解決。就是如何導入多個.X文件。一般的書或文章都是教導入一個文件,可是現實開發(fā)中肯定要導入多個的。怎么辦呢?謝謝了

              回復  更多評論   

            # re: XFile網格的應用(2) 2007-07-26 01:11 lovedday

            我也不知道,DX我也是初學者,邊學邊寫的。 :)  回復  更多評論   

            # re: XFile網格的應用(2) 2008-04-27 12:05 無名劍

            @小禹
            能夠導入一個也就能導入多個嘛,你多創(chuàng)建幾個對象然后一個個導入不就是多個了嗎?  回復  更多評論   

            # re: XFile網格的應用(2) 2008-06-17 00:31 help

            你好 請問下如何 在父空間旋轉模型呢?  回復  更多評論   

            公告

            導航

            統(tǒng)計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            国产精品久久久香蕉| 97久久国产露脸精品国产| 久久精品麻豆日日躁夜夜躁| 久久中文骚妇内射| 国产99精品久久| 狠狠精品干练久久久无码中文字幕| 久久se精品一区二区影院| 欧美久久一区二区三区| 久久人人爽人人爽人人av东京热 | 国色天香久久久久久久小说| 91精品国产高清91久久久久久| 国产精品99久久精品爆乳| 欧美亚洲国产精品久久| 久久综合九色综合精品| 精品综合久久久久久97| 国产精自产拍久久久久久蜜| 久久综合九色综合网站| 伊人久久综合热线大杳蕉下载| 久久妇女高潮几次MBA| 精品久久久久久无码人妻热| 久久丫精品国产亚洲av不卡| 久久亚洲精品无码观看不卡| 久久精品成人免费看| 精品熟女少妇AV免费久久| 久久国产香蕉一区精品| 国产成人精品白浆久久69| 精品久久人人爽天天玩人人妻| 精品国产青草久久久久福利| 国产精品久久久久久久久免费 | 色婷婷久久综合中文久久蜜桃av| 国产 亚洲 欧美 另类 久久| 久久久精品国产sm调教网站| 久久午夜无码鲁丝片秋霞| 久久亚洲2019中文字幕| 热久久这里只有精品| 久久国产精品久久| 国产精品久久久久久福利69堂| 69SEX久久精品国产麻豆| 久久亚洲中文字幕精品有坂深雪| 亚洲欧美国产精品专区久久| 国产精品久久久久久久人人看|