Establish your own 3D static model file

xiaoxiao2021-03-06  52

Follow me step by step (my ideas and procedures).

I have a project that can draw Tiger.x in Microsoft examples, where the creator code segment is as follows (some variables in the code are defined in .h document):

HRESULT CMYMESH :: Create (LPDirect3DDevice9 PDevice, String Meshfile)

{

IF (pdevice == null)

Return E_FAIL;

LPD3DXBUFFER pD3DXMtrlBuffer; if (FAILED (D3DXLoadMeshFromX (MeshFile.c_str (), D3DXMESH_SYSTEMMEM, pDevice, NULL, & pD3DXMtrlBuffer, NULL, & NumOfMaterials, & Mesh))) return E_FAIL; D3DXMATERIAL * d3dxMaterials = (D3DXMATERIAL *) pD3DXMtrlBuffer-> GetBufferPointer (); MeshMaterials = New D3DMATERIAL9 [Numofmaterials]; Meshtextures = New LPDIRECT3DTEXTURE9 [Numofmaterials];

For (dword i = 0; i ) {meshmaterials [i] = d3dxmaterials [i] .matd3d; meshmaterials [i] .Ambient = meshmaterials [i] .diffuse; meshtextures [i] = null; if (! d3dxMaterials [i] .pTextureFilename = NULL && lstrlen (d3dxMaterials [i] .pTextureFilename)> 0) {if (FAILED (D3DXCreateTextureFromFile (pDevice, d3dxMaterials [i] .pTextureFilename, & MeshTextures [i]))) return E_FAIL;} }

PD3DXMTRLBUFFER-> Release (); Return S_OK;}

The code drawing this Mesh is as follows: void cMymesh :: Render (lpdirect3ddevice9 pdevice) {for (dword i = 0; i setMaterial (& meshmaterials [i]); pdevice-> settexture (0 , Meshtextures [I]);

Mesh-> DrawSubset (i);}} We first try to replace DrawSubset function, as follows: void CMyMesh :: Render (LPDIRECT3DDEVICE9 pDevice) {LPDIRECT3DVERTEXBUFFER9 pVertexBuffer; LPDIRECT3DINDEXBUFFER9 pIndexBuffer; DWORD dwNumBytesPerVertex; DWORD dwFVF; DWORD dwNumVertex; DWORD dwFaces; Mesh -> GetVertexBuffer (& pVertexBuffer); Mesh-> GetIndexBuffer (& pIndexBuffer); dwNumBytesPerVertex = Mesh-> GetNumBytesPerVertex (); dwFVF = Mesh-> GetFVF (); dwNumVertex = Mesh-> GetNumVertices (); dwFaces = Mesh-> GetNumFaces () ; pDevice-> SetStreamSource (0, pVertexBuffer, 0, dwNumBytesPerVertex); pDevice-> SetFVF (dwFVF); pDevice-> SetTexture (0, MeshTextures [0]); pDevice-> SetMaterial (& MeshMaterials [0]); pDevice-> Setindices (Pindexbuffer); PDEvice-> DrawIndexedprimitive (D3DPT_TRIANGLIST, 0, 0, DWNUMVERTEX, 0, DWFACES);} Although Meshtextures and Meshmaterials are a bit problem, we know what to draw Tiger.x: VB, IB , NumBytespervertex, FVF, NumVertex, Faces, Texture, Material, then we save these things into the file, not the same .x file destination, and can bypass D3DxMesh. So how do these things get: I modified the code of the Create section:

HRESULT CMYMESH :: Create (lpdirect3ddevice9 pdevice, string Meshfile) {if (pdevice == null) Return E_FAIL;

LPD3DXBUFFER pD3DXMtrlBuffer; if (FAILED (D3DXLoadMeshFromX (MeshFile.c_str (), D3DXMESH_SYSTEMMEM, pDevice, NULL, & pD3DXMtrlBuffer, NULL, & NumOfMaterials, & Mesh))) return E_FAIL; D3DXMATERIAL * d3dxMaterials = (D3DXMATERIAL *) pD3DXMtrlBuffer-> GetBufferPointer (); MeshMaterials = New D3DMATERIAL9 [Numofmaterials]; Meshtextures = New LPDIRECT3DTEXTURE9 [Numofmaterials];

For (DWORD I = 0; I 0) {FILE * fp; int nLen = 0; char * pBuf; if (fp = fopen (d3dxMaterials [i] .pTextureFilename, "RB")) {fseek (fp, 0, seek_ek; nlen = ftell (fp); fseek (fp, 0, seek_set); PBUF = new char [Nlen]; FREAD (PBUF, 1, Nlen, FP); fclose (fp);} HRESULT hr = D3DXCreateTextureFromFileInMemory (D3DDevice, pBuf, nLen, & MeshTextures [i]); delete [] pBuf; // HRESULT hr = D3DXCreateTextureFromFile (D3DDevice_, d3dxMaterials [i] .pTextureFilename, & MeshTextures_ [i]); IF (Failed (HR)) Return E_FAIL;}}

// The following are additions FILE * fp; if (! (Fp = fopen ( "tiger.o", "w")) = NULL) {LPDIRECT3DVERTEXBUFFER9 pVertexBuffer; LPDIRECT3DINDEXBUFFER9 pIndexBuffer; D3DVERTEXBUFFER_DESC desc; D3DINDEXBUFFER_DESC idesc; Mesh-> GetVertexBuffer (& pVertexBuffer); Mesh-> GetIndexBuffer (& pIndexBuffer); pVertexBuffer-> getDesc (& desc); pIndexBuffer-> getDesc (& idesc); VOID * pVBData = NULL; VOID * pIBData = NULL; pVertexBuffer-> Lock (0, 0, ( Void **) & pvbdata, 0); PindexBuffer-> Lock (0, 0, (void **) & pibdata, 0); DWORD DWFVF = MESH-> getFVF (); dword dwnumbytespervertex = mesh-> getnumBytesperTex (); dWord Dwnumfaces = Mesh-> getnumface (); // How? There should be it, how to save it, look at your // This code is obviously imperfect, you need to go to improve it // Someone said that you have considered mesh, there are more animationSets, etc. ? // This is just a static model here, of course, there is no AnimationSet. Animated part I am behind writing. PvertexBuffer-> unlock (); pindexbuffer-> unlock (); fclose (fp);} // increase end PD3DXMTRLBUFFER-> Release (); returnif;}

转载请注明原文地址:https://www.9cbs.com/read-93601.html

New Post(0)