Search code examples
c++assimp

OpenGL Application crashes when accessing Assimp Texture Coordinate Data


I am trying to access the Texture Coordinate of a Cube model made in blender.

for (int i = 0; i < mMesh->mNumVertices; i++) {
    std::cout << mMesh->mTextureCoords[i][0].x << " " << mMesh->mTextureCoords[i][0].y << std::endl;
}

Why is this happening.
The application window launches but the red color background doesnt display.
And the first coords also get printed
0 1
How do i solve this.
Removing this code doesn't lead to a crash.


Solution

  • This should be:

    for (int i = 0; i < mMesh->mNumVertices; i++)
    {
        std::cout << mMesh->mTextureCoords[0][i].x << " " << mMesh->mTextureCoords[0][i].y << std::endl;
    }
    

    Looks like you messed up the first and second array arguments. Also, it is good practice to check if the mesh has texture coordinates or not. Then the code becomes

    if (mMesh->HasTextureCoords(0)) //HasTextureCoords is an assimp function
    {
        for (int i = 0; i < mMesh->mNumVertices; i++)
        {
            std::cout << mMesh->mTextureCoords[0][i].x << " " << mMesh- >mTextureCoords[0][i].y << std::endl;
        }
    }