Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Assimp and Direct3D 9


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 VitaliBR   Members   -  Reputation: 147

Like
0Likes
Like

Posted 27 July 2012 - 03:16 PM

Hi guys!

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

Sponsor:

#2 beans222   Members   -  Reputation: 1135

Like
2Likes
Like

Posted 27 July 2012 - 09:31 PM

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, 28 July 2012 - 07:45 PM.

New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

 


#3 VitaliBR   Members   -  Reputation: 147

Like
0Likes
Like

Posted 30 July 2012 - 08:31 AM

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.

I changed the memcpy (I think)

But now it shows nothing on the screen. :(

Loader:
// 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;

Render:
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);


#4 beans222   Members   -  Reputation: 1135

Like
1Likes
Like

Posted 30 July 2012 - 01:06 PM

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.

New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

 


#5 VitaliBR   Members   -  Reputation: 147

Like
0Likes
Like

Posted 31 July 2012 - 05:32 AM

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.

yes, the SDX_CUSTOMVERTEX and VERTEX is the same, VERTEX is my old structure.

I tried to redo the structure was as follows:
// 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;

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

#6 VitaliBR   Members   -  Reputation: 147

Like
0Likes
Like

Posted 01 August 2012 - 01:37 PM

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

But the strange thing is that my
// 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;

}

is

iNumVert = 1217
iNumFaces = 1368

and the same model in assimp view is
iNumVert = 1284
iNumFaces = 1340

Posted Image

I'm using the model Assimp/test/models/OBJ/spider.obj

#7 VitaliBR   Members   -  Reputation: 147

Like
0Likes
Like

Posted 02 August 2012 - 12:54 PM

I did it! Missing only the textures Posted Image

Edited by VitaliBR, 02 August 2012 - 12:54 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS