默认情况下,我会像这样加载所有内容
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();
}
它非常非常慢,所以我决定使用 assimp。模型本身加载得很好。但它们根本没有任何纹理(存在诸如颜色之类的东西)
我加载这样的材料
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);
}
}
}
}
我设置aiTextureType
为漫反射,但在我的 .obj 文件中也有shininess, specular, ambient
材质。
我画的都是这样
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);
那么如何加载其他类型的材料呢?