Search code examples
c++openglglslassimpply-file-format

Assimp cannot load vertices color of PLY model


I'm using the assimp3.0 on windows with OpenGL. I tried to load and display a ply model by following lines:

Assimp::Importer importer;
const aiScene* scene = NULL;
scene = importer.ReadFile('filename', 0);

I built the vertex buffer with scene->mMeshes[0]->mVertices and color buffer with scene->mMeshes[0]->mColors However, when I debug the code, only scene->mMeshes[0]->mColors[0] got the colors, scene->mMeshes[0]->mColors[1] and the following elements remains to be NULL following is a short ply example I I wrote:

ply
format ascii 1.0
comment VCGLIB generated
element vertex 3
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
property uchar alpha
element face 1
property list uchar int vertex_indices
end_header
0.0 0.0 0.0 219 227 248 255
1.0 0.0 0.0 220 230 212 255
2.0 0.0 2.0 160 122 221 255
3 0 1 2

I'm currently wondering if it was caused by the wrong pFlags parameter of importer.ReadFile(pFile,pFlags).

Can anyone help me figure out which part went wrong.


Solution

  • I think I understood the problem.

    You are reading colours like this

    for (int i = 0; i < vertexCount; i++)
        vertexColor = scene->mMeshes[0]->mColors[i];
    

    However, AssImp can store more than one vertex colour channel, so you are suppose to read it like this.

    for (int i = 0; i < vertexCount; i++)
        vertexColor = scene->mMeshes[0]->mColors[0][i];
    

    This will read the colours in the 0th vertex colour channel