I'm trying to render a model obj uploaded by assimp.
I doing as follows:
variables
LPDIRECT3DVERTEXBUFFER9 m_pVB;
LPDIRECT3DINDEXBUFFER9 m_pIB;
UINT numVertices;
UINT numFaces;
UINT inx_vertex;
UINT inx_faces;
UINT startIndex;
VERTEX *vertices;
USHORT *indices;
load the model
#define MeshFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
// Create an instance of the Importer class
Assimp::Importer importer;
// And have it read the given file with some example postprocessing
// Usually - if speed is not the most important aspect for you - you'll
// propably to request more postprocessing than we do in this example.
const aiScene* scene = importer.ReadFile(pFile,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType |
aiProcess_CalcTangentSpace |
aiProcess_JoinIdenticalVertices |
aiProcess_GenSmoothNormals |
aiProcess_LimitBoneWeights |
aiProcess_RemoveRedundantMaterials |
aiProcess_OptimizeMeshes);
// If the import failed, report it
if( !scene)
{
std::string s = "Error in load the " + pFile;
MessageBoxA (NULL, s.c_str(), "ERROR", MB_OK);
return false;
}
aiMesh** meshes = scene->mMeshes;
numVertices = 0;
numFaces = 0;
inx_vertex = 0;
inx_faces = 0;
startIndex = 0;
for(int nMeshes = 0; nMeshes < scene->mNumMeshes; nMeshes++)
{
numVertices += meshes[nMeshes]->mNumVertices;
numFaces += meshes[nMeshes]->mNumFaces;
}
vertices = new VERTEX[numVertices];
indices = new USHORT[numFaces*3];
for(int nMeshes = 0; nMeshes < scene->mNumMeshes; nMeshes++)
{
startIndex = inx_vertex;
for(int nVertex = 0; nVertex < meshes[nMeshes]->mNumVertices; nVertex++)
{
vertices[inx_vertex].X = meshes[nMeshes]->mVertices[nVertex].x;
vertices[inx_vertex].Y = meshes[nMeshes]->mVertices[nVertex].y;
vertices[inx_vertex].Z = meshes[nMeshes]->mVertices[nVertex].z;
vertices[inx_vertex].Nx = meshes[nMeshes]->mNormals[nVertex].x;
vertices[inx_vertex].Ny = meshes[nMeshes]->mNormals[nVertex].y;
vertices[inx_vertex].Nz = meshes[nMeshes]->mNormals[nVertex].z;
vertices[inx_vertex].Color = D3DCOLOR_XRGB(255,255,255);
inx_vertex++;
}
for(int nFaces = 0; nFaces < meshes[nMeshes]->mNumFaces; nFaces++)
{
indices[inx_faces++] = meshes[nMeshes]->mFaces[nFaces].mIndices[0] + startIndex;
indices[inx_faces++] = meshes[nMeshes]->mFaces[nFaces].mIndices[1] + startIndex;
indices[inx_faces++] = meshes[nMeshes]->mFaces[nFaces].mIndices[2] + startIndex;
}
}
// create a vertex buffer interface called v_buffer
devices.d3ddev->CreateVertexBuffer(8*sizeof(SDX_CUSTOMVERTEX),
0,
MeshFVF,
D3DPOOL_MANAGED,
&m_pVB,
NULL);
VOID* pVoid; // a void pointer
// lock v_buffer and load the vertices into it
m_pVB->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, 8*sizeof(SDX_CUSTOMVERTEX));
m_pVB->Unlock();
// create an index buffer interface called i_buffer
devices.d3ddev->CreateIndexBuffer(36*sizeof(short),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&m_pIB,
NULL);
// lock i_buffer and load the indices into it
m_pIB->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, sizeof(indices));
m_pIB->Unlock();
render the model
devices.d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
devices.d3ddev->SetRenderState(D3DRS_ZENABLE, FALSE);
devices.d3ddev->SetFVF(MeshFVF);
devices.d3ddev->SetStreamSource(0, m_pVB, 0, sizeof(VERTEX));
devices.d3ddev->SetIndices(m_pIB);
devices.d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, numVertices, 0, numFaces);
devices.d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);
devices.d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);
But the model is rendered completely wrong, is formless, very bizarre.
Where am I wrong? makes three days I have been analyzing and trying to find the error.