XFile网格的应用(2)(转载)
本篇是XFile网格的应用(1)的续篇。 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
Return ValuesIf 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. RemarksThe 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.
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
RemarksA 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.
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
RemarksThe 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;
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> Retrievesapointertothedatainthebuffer. LPVOIDGetBufferPointer(); Parameters None. ReturnValues Returnsapointertothedatainthebuffer. 再来看看GetBufferSize的具体使用信息: Retrievesthetotalsizeofthedatainthebuffer.
Creates a buffer object.
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 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;
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 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).
IDirect3D9*_d3d;
IDirect3DDevice9*_d3d_device;
ID3DXBuffer*_adjacency_buffer;
ID3DXBuffer*_material_buffer;
D3DMATERIAL9*_material_array;
IDirect3DTexture9**_texture_array;
DWORD_num_materials;
ID3DXMesh*_mesh;
//------------------------------------------------------------------------------------
//Load.Xfile
//------------------------------------------------------------------------------------
boolBASIC_XFILE::Load_XFile(char*x_filename)
{
//LoadsameshfromaDirectX.xfile
if(FAILED(D3DXLoadMeshFromX(
x_filename,//Pointertoastringthatspecifiesthefilename
D3DXMESH_MANAGED,//specifiescreationoptionsforthemesh
_d3d_device,//PointertoanIDirect3DDevice9interface
&_adjacency_buffer,//Pointertoabufferthatcontainsadjacencydata
&_material_buffer,//Pointertoabuffercontainingmaterialsdata
NULL,//Pointertoabuffercontaininganarrayofeffectinstances
&_num_materials,//PointertothenumberofD3DXMATERIALstructures
&_mesh)))//AddressofapointertoanID3DXMeshinterface
{
MessageBox(NULL,"Load.Xfilefailed.","ERROR",MB_OK);
returnfalse;
}
//invaliddata
if(_material_buffer==NULL||_num_materials==0)
returnfalse;
//retrievesapointertothedatainthebuffer
D3DXMATERIAL*material=(D3DXMATERIAL*)_material_buffer->GetBufferPointer();
if(material!=NULL)
{
//allocatememoryformaterialarrayandtexturearray
_material_array=newD3DMATERIAL9[_num_materials];
_texture_array=newIDirect3DTexture9*[_num_materials];
for(DWORDi=0;i<_num_materials;i++)
{
//assignmaterialtoarray
_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;
}
}
}
//Generatesameshwithreorderedfacesandverticestooptimizedrawingperformance.
//Thismethodreorderstheexistingmesh.
_mesh->OptimizeInplace(D3DXMESHOPT_COMPACT|D3DXMESHOPT_ATTRSORT|D3DXMESHOPT_VERTEXCACHE,(DWORD*)_adjacency_buffer->GetBufferPointer(),NULL,NULL);
_material_buffer->Release();
_adjacency_buffer->Release();
returntrue;
}
//------------------------------------------------------------------------------------
//Rendermesh.
//------------------------------------------------------------------------------------
voidBASIC_XFILE::Render()
{
//Cleararendertargetandthedepthbuffer
_d3d_device->Clear(0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0),1.0f,0);
_d3d_device->BeginScene();
//drawallfaceinthemesh
for(DWORDi=0;i<_num_materials;i++)
{
//Setsthematerialpropertiesforthedevice
_d3d_device->SetMaterial(&_material_array[i]);
//Assignsatexturetoastageforadevice
_d3d_device->SetTexture(0,_texture_array[i]);
//Drawsasubsetofamesh
_mesh->DrawSubset(i);
}
_d3d_device->EndScene();
//Presentsthecontentsofthenextbufferinthesequenceofbackbuffersownedbythedevice
_d3d_device->Present(NULL,NULL);
}
D3DPRESENT_PARAMETERSpresent_param;
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |