load different textures type using assimp

3.6k views Asked by At

By default I load everything like this

for(int g = 0; g < faces.size(); g++)
{

    glMaterialfv(GL_FRONT, GL_SPECULAR, materials[g].Ks);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, materials[g].Kd);
    glMaterialfv(GL_FRONT, GL_AMBIENT, materials[g].Ka);
    glMaterialf(GL_FRONT, GL_SHININESS, materials[g].Ns);

    Texture t;
    getTexture(&t, materials[g].pict);

    glBindTexture(GL_TEXTURE_2D, t.texID);

    glBegin(GL_TRIANGLES);

    for(int f = 0; f < faces[g].size(); f++)
    {
        glNormal3f(normals[faces[g][f].vn1 - 1].x, normals[faces[g][f].vn1 - 1].y, normals[faces[g][f].vn1 - 1].z);
        glTexCoord2f(texCoords[faces[g][f].vt1 - 1].u, texCoords[faces[g][f].vt1 - 1].v);
        glVertex3f(vertices[faces[g][f].v1 - 1].x, vertices[faces[g][f].v1 - 1].y, vertices[faces[g][f].v1 - 1].z);

        glNormal3f(normals[faces[g][f].vn2 - 1].x, normals[faces[g][f].vn2 - 1].y, normals[faces[g][f].vn2 - 1].z);
        glTexCoord2f(texCoords[faces[g][f].vt2 - 1].u, texCoords[faces[g][f].vt2 - 1].v);
        glVertex3f(vertices[faces[g][f].v2 - 1].x, vertices[faces[g][f].v2 - 1].y, vertices[faces[g][f].v2 - 1].z);

        glNormal3f(normals[faces[g][f].vn3 - 1].x, normals[faces[g][f].vn3 - 1].y, normals[faces[g][f].vn3 - 1].z);
        glTexCoord2f(texCoords[faces[g][f].vt3 - 1].u, texCoords[faces[g][f].vt3 - 1].v);
        glVertex3f(vertices[faces[g][f].v3 - 1].x, vertices[faces[g][f].v3 - 1].y, vertices[faces[g][f].v3 - 1].z);
    }

    glEnd();
}

It was very very slow so I decided to use assimp. The model itself is loaded well.But they don't have any textures at all(well something like color exist) enter image description here

I load materials like this

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

    m_Textures[i] = NULL;
    //GL_SHININESS GL_SPECULAR GL_DIFFUSE GL_AMBIENT
    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();
            if (!LoadTGA(m_Textures[i], const_cast<char*>(FullPath.c_str())))
            {
                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());
                glGenTextures(1, &m_Textures[i]->texID);
                glBindTexture(GL_TEXTURE_2D, m_Textures[i]->texID);
                glTexImage2D(GL_TEXTURE_2D, 
                                0, 
                                m_Textures[i]->bpp / 8, 
                                m_Textures[i]->width, 
                                m_Textures[i]->height, 
                                0, 
                                m_Textures[i]->type, 
                                GL_UNSIGNED_BYTE, 
                                m_Textures[i]->imageData);
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            }
        }
    }
}

I set aiTextureType as diffuse, but in my .obj file there are also shininess, specular, ambient materials.

I draw everything like this

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);

for (unsigned int i = 0 ; i < m_Entries.size() ; i++) {
    glBindBuffer(GL_ARRAY_BUFFER, m_Entries[i].VB);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TVertex), 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TVertex), (const GLvoid*)12);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(TVertex), (const GLvoid*)20);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Entries[i].IB);

    const unsigned int MaterialIndex = m_Entries[i].MaterialIndex;

    if (MaterialIndex < m_Textures.size() && m_Textures[MaterialIndex]) {
        glClientActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, m_Textures[MaterialIndex]->texID);
    }

    glDrawElements(GL_TRIANGLES, m_Entries[i].NumIndices, GL_UNSIGNED_INT, 0);
}

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);

So how to load other types of materials?

1

There are 1 answers

0
Thovex On

Since there was no answer here,

http://assimp.sourceforge.net/lib_html/material_8h.html#a7dd415ff703a2cc53d1c22ddbbd7dde0

Enumerator:

aiTextureType_NONE
Dummy value.

No texture, but the value to be used as 'texture semantic' (#aiMaterialProperty::mSemantic) for all material properties not related to textures.

aiTextureType_DIFFUSE
The texture is combined with the result of the diffuse lighting equation.

aiTextureType_SPECULAR
The texture is combined with the result of the specular lighting equation.

aiTextureType_AMBIENT
The texture is combined with the result of the ambient lighting equation.

aiTextureType_EMISSIVE
The texture is added to the result of the lighting calculation.

It isn't influenced by incoming light.

aiTextureType_HEIGHT The texture is a height map.

By convention, higher gray-scale values stand for higher elevations from the base height.

aiTextureType_NORMALS
The texture is a (tangent space) normal-map.

Again, there are several conventions for tangent-space normal maps. Assimp does (intentionally) not distinguish here.

aiTextureType_SHININESS
The texture defines the glossiness of the material.

The glossiness is in fact the exponent of the specular (phong) lighting equation. Usually there is a conversion function defined to map the linear color values in the texture to a suitable exponent. Have fun.

aiTextureType_OPACITY
The texture defines per-pixel opacity.

Usually 'white' means opaque and 'black' means 'transparency'. Or quite the opposite. Have fun.

aiTextureType_DISPLACEMENT
Displacement texture.

The exact purpose and format is application-dependent. Higher color values stand for higher vertex displacements.

aiTextureType_LIGHTMAP
Lightmap texture (aka Ambient Occlusion)

Both 'Lightmaps' and dedicated 'ambient occlusion maps' are covered by this material property. The texture contains a scaling value for the final color value of a pixel. Its intensity is not affected by incoming light.

aiTextureType_REFLECTION
Reflection texture.

Contains the color of a perfect mirror reflection. Rarely used, almost never for real-time applications.

aiTextureType_UNKNOWN
Unknown texture.

A texture reference that does not match any of the definitions above is considered to be 'unknown'. It is still imported, but is excluded from any further postprocessing.