Raenir Salazar - 2014-04-12

In blender, I can assign to every face a particular colour/material.

So far following along various tutorials everyone just loads up textures which involves UV's and blah but no incremental tutorial on how to just get the diffuse materials first.

How do I access these materials? The documentation isn't easy to follow and kinda vague.

Secondly, I have a function I got from a tutorial to dynamically read all of the textures from a scene/mesh.

bool Mesh::InitMaterials(const aiScene* pScene, const std::string& Filename)
// Extract the directory part from the file name
std::string::size_type SlashIndex = Filename.find_last_of("/");
std::string Dir;

if (SlashIndex == std::string::npos) {
    Dir = ".";
else if (SlashIndex == 0) {
    Dir = "/";
else {
    Dir = Filename.substr(0, SlashIndex);

bool Ret = true;

// Initialize the materials
for (unsigned int i = 0; i < pScene->mNumMaterials; i++) {
    const aiMaterial* pMaterial = pScene->mMaterials[i];

    m_Textures[i] = NULL;
    if (pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0) {
        aiString Path;

        if (pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
            std::string FullPath = Dir + "/" + Path.data;
            m_Textures[i] = new Texture(GL_TEXTURE_2D, FullPath.c_str());

            if (!m_Textures[i]->Load()) {
                printf("Error loading texture '%s'\n", FullPath.c_str());
                delete m_Textures[i];
                m_Textures[i] = NULL;
                Ret = false;
            else {
                printf("Loaded texture '%s'\n", FullPath.c_str());

    // Load a white texture in case the model does not include its own texture
    if (!m_Textures[i]) {
        m_Textures[i] = new Texture(GL_TEXTURE_2D, "Resources/white.jpg");

        Ret = m_Textures[i]->Load();

return Ret;


(pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) absolutely refuses to work, it always evaluates to false no matter what it sees no texture in my mesh and I get a blank black mesh; the mesh itself otherwise loads fine.

The texture was a bmp kept in the same directory as the obj and mtl files.