hi,
i am looking for a way to convert my d3dxmesh to smeshbuffer. I created this dx mesh from 3dfont.. any way to do it easily?
convert LPD3DXMESH to SMeshBuffer
well I wrote it
here's the code:
Code: Select all
DWORD fvf = mesh->GetFVF();
if (fvf == (D3DFVF_XYZ | D3DFVF_NORMAL)) // 3dtext returns this FVF
{
D3DVERTEXELEMENT9 dec[MAX_FVF_DECL_SIZE];
ZeroMemory(dec, sizeof(D3DVERTEXELEMENT9) * MAX_FVF_DECL_SIZE);
if (FAILED(mesh->GetDeclaration(dec)))
{
MessageBoxA(NULL, "Cannot get declaration of mesh!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}
D3DVERTEXELEMENT9 pos, normal;
for (int i = 0; i < 2; i++) // only 2 type defined
{
if (dec[i].Usage == D3DDECLUSAGE_POSITION && dec[i].Type != D3DDECLTYPE_UNUSED)
{
pos = dec[i];
}
else if (dec[i].Usage == D3DDECLUSAGE_NORMAL && dec[i].Type != D3DDECLTYPE_UNUSED)
{
normal = dec[i];
}
}
if (pos.Type != D3DDECLTYPE_FLOAT3)
{
MessageBoxA(NULL, "Vertex position is not float3 type!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}
if (normal.Type != D3DDECLTYPE_FLOAT3)
{
MessageBoxA(NULL, "Vertex normal is not float3 type!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}
SMeshBuffer* buffer = new SMeshBuffer();
DWORD vertex_count = mesh->GetNumVertices();
DWORD vertex_size = mesh->GetNumBytesPerVertex();
LPDIRECT3DVERTEXBUFFER9 vb;
if (FAILED(mesh->GetVertexBuffer(&vb)))
{
MessageBoxA(NULL, "Cannot get vertex buffer of mesh!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}
char* pVertices;
if (FAILED(vb->Lock(0, 0, (void**)&pVertices, D3DLOCK_READONLY)))
{
MessageBoxA(NULL, "Cannot lock vertex buffer of mesh!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}
buffer->Vertices.reallocate(vertex_count);
for (DWORD i = 0; i < vertex_count; i++)
{
S3DVertex v;
memcpy(&v.Pos, &pVertices[pos.Offset], sizeof(float) * 3);
memcpy(&v.Normal, &pVertices[normal.Offset], sizeof(float) * 3);
v.Color = SColor(255,255,255,255);
buffer->Vertices.push_back(v);
pVertices += vertex_size;
}
if (FAILED(vb->Unlock()))
{
MessageBoxA(NULL, "Cannot unlock vertex buffer of mesh!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}
LPDIRECT3DINDEXBUFFER9 ib;
if (FAILED(mesh->GetIndexBuffer(&ib)))
{
MessageBoxA(NULL, "Cannot get index buffer of mesh!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}
D3DINDEXBUFFER_DESC index_desc;
if (FAILED(ib->GetDesc(&index_desc)))
{
MessageBoxA(NULL, "Cannot get index buffer description of mesh!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}
if (index_desc.Format != D3DFMT_INDEX16)
{
MessageBoxA(NULL, "Index buffer is not 16bit!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}
char* pIndices;
if (FAILED(ib->Lock(0, 0, (void**)&pIndices, D3DLOCK_READONLY)))
{
MessageBoxA(NULL, "Cannot lock index buffer of mesh!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}
void* indices_buffer = malloc(index_desc.Size);
if (!indices_buffer)
{
MessageBoxA(NULL, "Cannot allocate memory for new index buffer!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}
memcpy(indices_buffer, pIndices, index_desc.Size);
buffer->Indices.set_pointer((u16*)indices_buffer, index_desc.Size>>1);
if (FAILED(ib->Unlock()))
{
MessageBoxA(NULL, "Cannot unlock index buffer of mesh!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}
mesh->Release();
return buffer;
}
else
{
MessageBoxA(NULL, "Cannot recognize FVF format!", "3D Text", MB_OK|MB_ICONERROR|MB_APPLMODAL);
exit(0);
}