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)
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?
Since there was no answer here,
http://assimp.sourceforge.net/lib_html/material_8h.html#a7dd415ff703a2cc53d1c22ddbbd7dde0