我一直在尝试使用ASSIMP加载Wavefront obj模型。但是,我无法让 mtl 材料颜色起作用。我知道如何加载,但是,我不知道如何为每个顶点获取相应的颜色。(Kd rgb)
usemtl material_11
f 7//1 8//1 9//2
f 10//1 11//1 12//2
例如,上面的 Wavefront obj 片段意味着这些顶点使用 material_11。
Q:那么如何获取每个顶点对应的材质呢?
错误
Wavefront obj材质不在正确的顶点中:
原始模型(使用 ASSIMP 模型查看器渲染):
用我的代码渲染的模型:
代码:
我用于加载 mtl 材料颜色的代码:
std::vector<color4<float>> colors = std::vector<color4<float>>();
...
for (unsigned int i = 0; i < scene->mNumMeshes; i++)
{
const aiMesh* model = scene->mMeshes[i];
const aiMaterial *mtl = scene->mMaterials[model->mMaterialIndex];
color4<float> color = color4<float>(1.0f, 1.0f, 1.0f, 1.0f);
aiColor4D diffuse;
if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse))
color = color4<float>(diffuse.r, diffuse.g, diffuse.b, diffuse.a);
colors.push_back(color);
...
}
创建顶点的代码:
vertex* vertices_arr = new vertex[positions.size()];
for (unsigned int i = 0; i < positions.size(); i++)
{
vertices_arr[i].SetPosition(positions.at(i));
vertices_arr[i].SetTextureCoordinate(texcoords.at(i));
}
// Code for setting vertices colors (I'm just setting it in ASSIMP vertices order, since I don't know how to set it in the correct order).
for (unsigned int i = 0; i < scene->mNumMeshes; i++)
{
const unsigned int vertices_size = scene->mMeshes[i]->mNumVertices;
for (unsigned int k = 0; k < vertices_size; k++)
{
vertices_arr[k].SetColor(colors.at(i));
}
}
编辑:
看起来模型顶点位置也没有正确加载。即使我禁用面部剔除并更改背景颜色。