This one written for use with Ultrano's oop wrapper..
Code has been tested and is working.
Written for use in ParticleSystem demo, for loading and rending a mesh used as a full SkySphere (Night and Day SkyDomes)...
Note that since we can't use params in the Constructor Method, a secondary Constructor (the Load Method) was required. This must be manually called after instancing the class object. Sorry about the messy coding :tongue:
Note also, you must set the FVF in your Render proc before calling this Render Method.

;This simple Class contains functionality for Loading and Rendering a Mesh

MESH_D3DFVF_CUSTOMVERTEX equ D3DFVF_XYZ or D3DFVF_NORMAL or D3DFVF_TEX1

class CMesh
virtual $CMesh
virtual Load
virtual Render
long pMesh
long pMeshMaterials
long pMeshTextures
long dwNumMaterials
endclass

CMesh_CMesh proc
return ecx
CMesh_CMesh endp

CMesh_$CMesh proc
local i:DWORD
local pTextures:DWORD
local me:DWORD
mov me,ecx
free .CMesh.pMeshMaterials
.if .CMesh.pMeshTextures != NULL
m2m pTextures,.CMesh.pMeshTextures
xor eax,eax
mov i,eax
.while eax < .CMesh.dwNumMaterials
mov ebx,i
shl ebx,2
.if pTextures
mcall pTextures,IUnknown_Release
.endif
inc i
mov ecx,me
mov eax,i
.endw
.endif
free .CMesh.pMeshTextures
mcall .CMesh.pMesh,IUnknown_Release,
Debug CTEXT("Mesh destroyed OK",13,10)
ret
CMesh_$CMesh endp

CMesh_Load proc pD3DDevice, pFilename
local pMaterialsBuffer:LPD3DXBUFFER
local pMesh :LPD3DXMESH
local pmatMaterials:ptr D3DXMATERIAL
local me:DWORD
local i:DWORD
local ErrBuf[256]:BYTE
mov me,ecx ;Preserve pThis

mov pMesh,NULL
mov pMaterialsBuffer,0
invoke D3DXLoadMeshFromX,pFilename, D3DXMESH_SYSTEMMEM, pD3DDevice, NULL, addr pMaterialsBuffer, addr .CMesh.dwNumMaterials, addr pMesh
.if eax!=D3D_OK
invoke MessageBox,0,CTEXT("grrrr"),CTEXT("grrrr"),MB_OK
mov ecx,me
mov eax,NULL
mov .CMesh.pMesh , eax
mov .CMesh.pMeshMaterials ,eax
mov .CMesh.pMeshTextures ,eax
DebugValue addr ErrBuf, CTEXT("Mesh '%s' failed to load",13,10), pFilename
ret
.endif
DebugValue addr ErrBuf,CTEXT("pMeshMaterials=%lX",13,10),.CMesh.pMeshMaterials
DebugValue addr ErrBuf,CTEXT("pMeshTextures =%lX",13,10),.CMesh.pMeshTextures

mcall pMaterialsBuffer,ID3DXBuffer_GetBufferPointer
mov pmatMaterials,eax


; //Create two arrays. One to hold the materials and only to hold the textures
mov ecx,me
mov eax, .CMesh.dwNumMaterials
imul eax,sizeof D3DMATERIAL8
mov .CMesh.pMeshMaterials , malloc (eax)
mov eax, .CMesh.dwNumMaterials
shl eax,2
mov .CMesh.pMeshTextures,malloc (eax)

xor eax,eax
mov i,eax
.while eax < .CMesh.dwNumMaterials
mov ecx,me
mov ebx,.CMesh.pMeshMaterials
mov eax,i
imul eax,sizeof D3DMATERIAL8
add ebx,eax
mov ecx,pmatMaterials
invoke RtlMoveMemory,ebx,ecx,sizeof D3DMATERIAL8 ; //Copy the material

; //Set the ambient color for the material (D3DX does not do this)
mov ecx,me
mov ebx,.CMesh.pMeshMaterials
mov eax,i
imul eax,sizeof D3DMATERIAL8
add ebx,eax
invoke RtlMoveMemory,addr .D3DMATERIAL8.Ambient ,addr .D3DMATERIAL8.Diffuse,sizeof D3DMATERIAL8.Diffuse

; //Create the texture
mov ebx,pmatMaterials
mov ecx,me
mov ecx, .CMesh.pMeshTextures
mov eax,i
shl eax,2
add ecx,eax
invoke D3DXCreateTextureFromFile,pD3DDevice, .D3DXMATERIAL.pTextureFilename, ecx
.if eax!=D3D_OK
mov ecx,me
mov ecx, .CMesh.pMeshTextures
mov eax,i
shl eax,2
add ecx,eax
mov dword ptr,NULL
DebugValue addr ErrBuf, CTEXT("Texture '%s' Failed to Load",13,10), .D3DXMATERIAL.pTextureFilename
.endif
add pmatMaterials,sizeof D3DXMATERIAL
inc i
mov ecx,me
mov eax,i
.endw

; //We've finished with the material buffer, so release it
mcall pMaterialsBuffer,IUnknown_Release

; //Make sure that the normals are setup for our mesh
mov ecx,me
mcall pMesh, ID3DXMesh_CloneMeshFVF,D3DXMESH_MANAGED, MESH_D3DFVF_CUSTOMVERTEX, pD3DDevice, addr .CMesh.pMesh
mcall pMesh,IUnknown_Release
mov ecx,me
invoke D3DXComputeNormals,.CMesh.pMesh, NULL
DebugValue addr ErrBuf, CTEXT("Mesh '%s' loaded OK",13,10), pFilename
return S_OK
CMesh_Load endp

CMesh_Render proc pd3dDevice:DWORD
local me:DWORD
local i:DWORD
local ErrBuf[256]:BYTE
local pMaterials:DWORD
local pTextures:DWORD
local dwNumMaterials:DWORD

mov me,ecx
assume ecx:nothing
.if .CMesh.pMesh != NULL
xor eax,eax
mov i,eax
m2m pMaterials,.CMesh.pMeshMaterials
m2m pTextures,.CMesh.pMeshTextures
m2m dwNumMaterials,.CMesh.dwNumMaterials
.while eax < dwNumMaterials
mcall pd3dDevice,IDirect3DDevice8_SetMaterial, pMaterials
mov ecx,me
mov eax,i
shl eax,2
mov eax, pTextures
mcall pd3dDevice,IDirect3DDevice8_SetTexture,0, dword ptr
mov ecx,me
mcall .CMesh.pMesh,ID3DXMesh_DrawSubset,i
add pMaterials,sizeof D3DMATERIAL8
add pTextures,4
inc i
mov eax,i
mov ecx,me
.endw
; mcall .CMesh.pMesh,ID3DXMesh_GetNumFaces
ret
.endif
return NULL
CMesh_Render endp
Posted on 2003-12-21 22:32:08 by Homer