Assimp loading model

Started by
13 comments, last by kolarz3 10 years, 9 months ago

I try to load with assimp simple box. But assimp return wrong number of vertex. I try several formats(didnt help). This is my model :


xof 0303txt 0032

Frame Root {
  FrameTransformMatrix {
     1.000000, 0.000000, 0.000000, 0.000000,
     0.000000,-0.000000, 1.000000, 0.000000,
     0.000000, 1.000000, 0.000000, 0.000000,
     0.000000, 0.000000, 0.000000, 1.000000;;
  }
  Frame Cube {
    FrameTransformMatrix {
       1.000000, 0.000000, 0.000000, 0.000000,
       0.000000, 1.000000, 0.000000, 0.000000,
       0.000000, 0.000000, 1.000000, 0.000000,
       0.000000, 0.000000, 0.000000, 1.000000;;
    }
    Mesh { // Cube mesh
      8;
       1.000000; 1.000000;-1.000000;,
       1.000000;-1.000000;-1.000000;,
      -1.000000;-1.000000;-1.000000;,
      -1.000000; 1.000000;-1.000000;,
       1.000000; 0.999999; 1.000000;,
       0.999999;-1.000001; 1.000000;,
      -1.000000;-1.000000; 1.000000;,
      -1.000000; 1.000000; 1.000000;;
      12;
      3;3,1,0,,
      3;5,7,4,,
      3;1,4,0,,
      3;6,5,1,,
      3;7,6,2,,
      3;3,0,4,,
      3;3,2,1,,
      3;5,6,7,,
      3;6,1,2,,
      3;7,2,3,,
      3;3,4,7,,
      3;1,5,4,;
      MeshNormals { // Cube normals
        12;
         0.000000; 0.000000;-1.000000;,
        -0.000000;-0.000000; 1.000000;,
         1.000000; 0.000000;-0.000000;,
        -0.000000;-1.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
         0.000000; 1.000000; 0.000000;,
         0.000000;-0.000000;-1.000000;,
         0.000000;-0.000000; 1.000000;,
        -0.000000;-1.000000; 0.000000;,
        -1.000000; 0.000000;-0.000000;,
         0.000000; 1.000000; 0.000000;,
         1.000000;-0.000001; 0.000000;;
        12;
        3;0,0,0,,
        3;1,1,1,,
        3;2,2,2,,
        3;3,3,3,,
        3;4,4,4,,
        3;5,5,5,,
        3;6,6,6,,
        3;7,7,7,,
        3;8,8,8,,
        3;9,9,9,,
        3;10,10,10,,
        3;11,11,11,;
      } // End of Cube normals
      MeshMaterialList { // Cube material list
        1;
        12;
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0;;
        Material Material {
           0.640000; 0.640000; 0.640000; 1.000000;;
           96.078431;
           0.500000; 0.500000; 0.500000;;
           0.000000; 0.000000; 0.000000;;
        }
      } // End of Cube material list
    } // End of Cube mesh
  } // End of Cube
} // End of Root

Model have 8 Vertex, when I call mesh->mNumVertices I get 24. This code loading vertex :


	for (unsigned int i = 0 ; i < mesh->mNumVertices ; i++) 
	{
Vert.push_back(Vertex( mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z,mesh->mTextureCoords[0][i].x,mesh->mTextureCoords[0][i].y,mesh->mNormals[i].x,mesh->mNormals[i].y,mesh->mNormals[i].z));
	}

7b4c677ec2005.png

Advertisement

Does MeshNormals define normals for each triangle? If yes, then you simply have vertices that have same position, but different normal. Therefore 6 faces, each 4 vertices = 24.

How can i get real number of vertex ?

What do you mean? You actually have 24 vertices.

If you want to have only 8 you need to define normals for vertices, not triangles, however your model won't be shaded correctly (in this case).

Now I have model with 2676 vertex and 892 indi. Normals are per vertex. Assimp return that i have only 554 vertex.

Did you count in all vertices? They might be split into several meshes.

There's also chance of duplicate vertices (depends on your exporter), Assimp removes duplicate vertices.

Maybe i dont known how to generate corectly normals in blender. Now I generate normals in assimp. But I have still problem. This is my model http://pastebin.com/MQ5pWACz . This have 176 vertex and 348 indi. I noticed strange behaviour of assimp. He split my model into 2 meshes. First have 488 vert and 54 indi, second have 120 vert and 294 indi. Sum of indi is 348 and is correct, but sum of vertex is 608. Still I have strange mix of triangle. This is my full code of loading mesh :


#include "Model.h"
int Model::loadFromFileModelAndInit(char *fileName)
{
	Assimp::Importer importer;
	const aiScene *scene = importer.ReadFile(fileName,aiProcessPreset_TargetRealtime_Fast|aiProcess_GenNormals);
	aiMesh *mesh = scene->mMeshes[0];
	for(unsigned int i=0;i<mesh->mNumFaces;i++)
	{
					const aiFace& face = mesh->mFaces[i];
					Indi.push_back(face.mIndices[0]);
					Indi.push_back(face.mIndices[1]);
					Indi.push_back(face.mIndices[2]);
	}
	numIndi=mesh->mNumFaces;
	for (unsigned int i = 0 ; i < mesh->mNumVertices ; i++) 
	{
		Vert.push_back(Vertex( mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z,1.0,1.0,mesh->mNormals[i].x,mesh->mNormals[i].y,mesh->mNormals[i].z));
	}
	numVert=mesh->mNumVertices;
	sizeVert=sizeof(Vertex);
	return 1;
}
I'm not familiar with that particular mesh format but it seems to contain at least two different materials. In general that would force Assimp to split the mesh into two submeshes.

Furthermore, if you use Assimp to generate normals, how exactly do you do it? What does the documentation tell you about it? The simplest way to generate normals would be to do it per-face. Of course that will increase the number of vertices.

Edit: According to the documentation:


Generates normals for all faces of all meshes.

This is ignored if normals are already there at the time this flag is evaluated. Model importers try to load them from the source file, so they're usually already there. Face normals are shared between all points of a single face, so a single point can have multiple normals, which forces the library to duplicate vertices in some cases. #aiProcess_JoinIdenticalVertices is *senseless* then.

This flag may not be specified together with #aiProcess_GenSmoothNormals.

So having a much higher vertex count than the number of vertex positions stored in the file would be the expected case.

Edit 2: and have you tried aiProcess_GenSmoothNormals?

For this flag i get

mmf5.png

Edit:

With aiProcess_GenNormals and when I load 2 meshes I have :

mc60.png

Model in blender :

kyb.png

Model formats favour file size over what the actual runtime data looks like in the editor you are using. Runtime data is often bigger then the data in the file itself you can't compare the two to make sure the model is loaded correctly. The only thing you can do is check that the data in all of the vertices is the same as the vertex data in the file.

I found that Assimp doesn't read the .blend format well so export to .dea (collada format) and it generally works a lot better in assimp.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement