• <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文件中,主場景框架 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可以指定的網格創建模式:

            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來創建,不過,一般不需要直接創建,下面是該函數的具體使用信息:

            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 接口提供的方法,可對裝入的網格數據進行讀取,優化和渲染等。

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

            來看看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數據的處理

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

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

            Mesh網格的渲染,可以用一個for循環來進行,循環的次數為總的材質數。每一次循環都將那些具有相同Subset子集號的三角形面渲染出來,即渲染每個子集Subset。不過每次循環首先要設置渲染管道線的材質和紋理對象,如下所示:
            //------------------------------------------------------------------------------------
            // 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緩沖區進行了清除,因此必須在初始化D3D設備時,開啟Z緩沖區,否則該函數調用失敗并且屏幕將出現重影。

            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 閱讀(2045) 評論(4)  編輯 收藏 引用

            評論

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

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

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

              回復  更多評論   

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

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

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

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

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

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

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            久久精品水蜜桃av综合天堂 | 精品久久8x国产免费观看| 婷婷国产天堂久久综合五月| 午夜精品久久久久| 99久久精品日本一区二区免费| 精品国产乱码久久久久久郑州公司| 久久精品国产影库免费看| 久久精品国产亚洲Aⅴ蜜臀色欲 | 久久国产精品无码一区二区三区| 久久99久久99精品免视看动漫| 国产精品熟女福利久久AV| 亚洲精品国产第一综合99久久| 久久无码人妻一区二区三区午夜| 久久精品国产一区| 中文精品久久久久人妻不卡| 国产成人久久久精品二区三区 | 高清免费久久午夜精品| 欧美国产成人久久精品| 国产精品久久久久久| 久久亚洲国产精品成人AV秋霞| 日本免费一区二区久久人人澡| 久久久久亚洲AV片无码下载蜜桃| 国产精品亚洲美女久久久| 久久久久久国产精品无码超碰 | 色妞色综合久久夜夜| 久久久国产亚洲精品| 精品国产91久久久久久久a| MM131亚洲国产美女久久| 中文成人久久久久影院免费观看 | 精品久久久久久久无码 | 国产亚洲精品美女久久久| 超级碰碰碰碰97久久久久| 久久免费视频一区| 久久久久亚洲AV无码去区首| 久久精品国产91久久综合麻豆自制| 日韩av无码久久精品免费| 无码人妻精品一区二区三区久久| 2019久久久高清456| 久久无码专区国产精品发布| 久久久久久免费视频| 新狼窝色AV性久久久久久|