Load Normal map and tangent space

Mesh.cpp

Now we are moving to Mesh.cpp

We need to revise Mesh::loadModel() to create the tangent space and load the tangent/bitangent together with the normal map.

Create the tangent space

Assimp supports creation of the tangent space with the option aiProcess_CalcTangentSpace.

void Mesh::loadModel(std::string path) 
{
    Assimp::Importer importer;
    const aiScene* scene = importer.ReadFile(path, aiProcess_JoinIdenticalVertices 
                                       | aiProcess_FlipUVs 
                                       | aiProcess_CalcTangentSpace );
    
    ...
}

Load the tangent/bitangent

Then we can read the tangents from the data

for (int i = 0; i < scene->mNumMeshes; i++)
    {
        aiMesh* mesh = scene->mMeshes[i];
        
        // read vertex position and normals
        int nVertex = mesh->mNumVertices;

        for (int j = 0; j < nVertex; j++)
        {
            glm::vec3 pos; 
            ... read pos
            v.pos = pos;

            glm::vec3 normal;
            ... read normal
            v.normal = normal;

            // LabA08 read tangents for normal mapping
            glm::vec3 tangent;
            tangent.x = mesh->mTangents[i].x;
            tangent.y = mesh->mTangents[i].y;
            tangent.z = mesh->mTangents[i].z;
            v.tangent = tangent;  

            tangent.x = mesh->mBitangents[i].x;
            tangent.y = mesh->mBitangents[i].y;
            tangent.z = mesh->mBitangents[i].z;
            v.bitangent = tangent; 
            
            ...
            }
    }

Load the normal map

In addition to the diffuse texture map, now we need to load the normal map.

After loading the texture map, add loading of the normal map.

Because the OBJ material does not has a field for normal map, actually we are using the map_Bump option (change of height) for the normal map, as can be seen from Box_normal.mtl.

So the corresponding texture type for the normal map is aiTextureType_HEIGHT.

Full Source Code of Mesh::loadModel()

Last updated