Sign in to follow this  

Assimp and Direct3D 9

This topic is 1961 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys!

I'm trying to render a model obj uploaded by assimp.
I doing as follows:

variables
[CODE]LPDIRECT3DVERTEXBUFFER9 m_pVB;
LPDIRECT3DINDEXBUFFER9 m_pIB;
UINT numVertices;
UINT numFaces;
UINT inx_vertex;
UINT inx_faces;
UINT startIndex;
VERTEX *vertices;
USHORT *indices;[/CODE]

load the model
[CODE]
#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();
[/CODE]

render the model
[CODE]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);[/CODE]

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. :(

Share this post


Link to post
Share on other sites
About that memcpy call for the indices, it looks like you are passing the size of the pointer (which will be 4 or 8) rather than the size of the array. Edited by beans222

Share this post


Link to post
Share on other sites
[quote name='beans222' timestamp='1343446266' post='4963849']
About that memcpy call for the indices, it looks like you are passing the size of the pointer (which will be 4 or 8) rather than the size of the array.
[/quote]
I changed the memcpy (I think)

But now it shows nothing on the screen. :(

Loader:
[CODE]// 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 mesh", 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
if(FAILED(devices.d3ddev->CreateVertexBuffer(numVertices * sizeof(SDX_CUSTOMVERTEX),
0,
MeshFVF,
D3DPOOL_MANAGED,
&m_pVB,
NULL)))
{
MessageBoxA(NULL, "Error in CreateVertexBuffer", "Error mesh", MB_OK | MB_ICONEXCLAMATION);
return false;
}
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, sizeof(SDX_CUSTOMVERTEX));
m_pVB->Unlock();
// create an index buffer interface called i_buffer
if(FAILED(devices.d3ddev->CreateIndexBuffer(numFaces * 3 * sizeof(unsigned int),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&m_pIB,
NULL)))
{
MessageBoxA(NULL, "Error in CreateIndexBuffer", "Error mesh", MB_OK | MB_ICONEXCLAMATION);
return false;
}
// lock i_buffer and load the indices into it
m_pIB->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, sizeof(unsigned int));
m_pIB->Unlock();
return true;[/CODE]

Render:
[CODE]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);
if(FAILED(devices.d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, numVertices, 0, numFaces)))
{
MessageBoxA(NULL, "Error in render model", "Error mesh", MB_OK | MB_ICONEXCLAMATION);
return;
}
devices.d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);
devices.d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);[/CODE]

Share this post


Link to post
Share on other sites
The final argument to memcpy should be the same as the length passed to create the buffer (as it's the total bytes to copy), and the sizeof in the index buffer one should be short, not int. ("numFaces * 3 * sizeof(short)" "numVertices * sizeof(SDX_CUSTOMVERTEX)"). Is type VERTEX (the type of array 'vertices') the same thing as SDX_CUSTOMVERTEX?

I think there's also a problem with the counters in the code that gets the data from Assimp. The index counter is based on the number of vertices which will almost never match the number of vertices. It would be better to use completely separate counters for vertex and index data and just add the mesh's mNumVertices and mNumFaces*3 to the respective counters at the end of the outer loop.

Share this post


Link to post
Share on other sites
[quote name='beans222' timestamp='1343675163' post='4964580']
The final argument to memcpy should be the same as the length passed to create the buffer (as it's the total bytes to copy), and the sizeof in the index buffer one should be short, not int. ("numFaces * 3 * sizeof(short)" "numVertices * sizeof(SDX_CUSTOMVERTEX)"). Is type VERTEX (the type of array 'vertices') the same thing as SDX_CUSTOMVERTEX?

I think there's also a problem with the counters in the code that gets the data from Assimp. The index counter is based on the number of vertices which will almost never match the number of vertices. It would be better to use completely separate counters for vertex and index data and just add the mesh's mNumVertices and mNumFaces*3 to the respective counters at the end of the outer loop.
[/quote]
yes, the SDX_CUSTOMVERTEX and VERTEX is the same, VERTEX is my old structure.

I tried to redo the structure was as follows:
[CODE]// 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_FlipUVs);
/*
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 mesh", MB_OK);
return false;
}
//retrieves the meshes from the main scene;
aiMesh** meshes = scene->mMeshes;
aiMaterial** materials = scene->mMaterials;
aiNode* sceneRoot = scene->mRootNode;
aiNode** children = sceneRoot->mChildren;
aiNode* child;
UINT* meshID;
UINT materialID;
UINT numVertices = 0;
UINT numTriangles = 0;
Vertex* vertices;
DWORD* indices;
UINT* attributeBuffer;
UINT i = 0;
bool rootNode = true;
while(i < sceneRoot->mNumChildren)
{
if(rootNode)
{
child = sceneRoot;
rootNode = false;
} else
{
child = children[i];
i++;
}
if(!(child->mNumMeshes > 0))
continue;
meshID = child->mMeshes;
for(UINT x = 0; x < child->mNumMeshes; x++)
{
numVertices += meshes[meshID[x]]->mNumVertices;
numTriangles += meshes[meshID[x]]->mNumFaces;
}
vertices = new Vertex[numVertices];
indices = new DWORD[numTriangles*3];
attributeBuffer = new UINT[numTriangles];
UINT nextVertex = 0;
UINT nextIndex = 0;
for(UINT x = 0; x < child->mNumMeshes; x++)
{
UINT meshStartIndex = nextVertex;
for(UINT j = 0; j < meshes[meshID[x]]->mNumVertices; j++)
{
vertices[nextVertex].pos = aiVec3ToSDXVector3(meshes[meshID[x]]->mVertices[j]);
vertices[nextVertex].tangent = aiVec3ToSDXVector3(meshes[meshID[x]]->mTangents[j]);
vertices[nextVertex].normal = aiVec3ToSDXVector3(meshes[meshID[x]]->mNormals[j]);
vertices[nextVertex].texC = aiVec3ToSDXVector2(meshes[meshID[x]]->mTextureCoords[0][j]);
nextVertex++;
}
for(UINT j = 0; j < meshes[meshID[x]]->mNumFaces; j++)
{
UINT n = 0;
if(x != 0)
{
n = j + meshes[meshID[x-1]]->mNumFaces;
} else
{
n = j;
}
indices[nextIndex++] = meshes[meshID[x]]->mFaces[j].mIndices[0] + meshStartIndex;
indices[nextIndex++] = meshes[meshID[x]]->mFaces[j].mIndices[1] + meshStartIndex;
indices[nextIndex++] = meshes[meshID[x]]->mFaces[j].mIndices[2] + meshStartIndex;
attributeBuffer[n] = meshes[meshID[x]]->mMaterialIndex;
}
}
}
return true;[/CODE]

but I'm still trying to find a way to create the mesh, I could use the D3DXCreateMesh?

Share this post


Link to post
Share on other sites
I tried this (with the help of the assimp view source):
http://pastebin.com/vz51v8E9

helpers:
http://pastebin.com/aiuzbAGa

and render model:
http://pastebin.com/M2fdH36T

but does not render anything on the screen :(

[size=4][b]But the strange thing is that my [/b][/size]
[size=4][b]// get the number of vertices/faces in the model
unsigned int iNumVert = 0;
unsigned int iNumFaces = 0;
for (unsigned int i = 0; i < scene->mNumMeshes;++i)
{
iNumVert += scene->mMeshes[i]->mNumVertices;
iNumFaces += scene->mMeshes[i]->mNumFaces;[/b][/size]
[size=4][b]}[/b][/size]

[size=4][b]is [/b][/size]

[size=4][b]iNumVert = 1217[/b][/size]
[size=4][b]iNumFaces = 1368[/b][/size]

[size=4][b]and the same model in assimp view is [/b][/size]
[size=4][b]iNumVert = 1284
iNumFaces = 1340[/b][/size]
[img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img]

[size=4][b]I'm using the model [/b][/size][b]Assimp/test/models/OBJ/spider.obj[/b]

Share this post


Link to post
Share on other sites
I did it! Missing only the textures [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by VitaliBR

Share this post


Link to post
Share on other sites

This topic is 1961 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this