【发布时间】:2017-05-28 08:01:57
【问题描述】:
我有一个程序用于将模型加载到 C 中的 OpenGL 中。就使用 Assimp 加载模型而言(据我理解),此代码非常简单:
const struct aiScene* scene = aiImportFile(objfile, aiProcessPreset_TargetRealtime_Fast);
unsigned int vbo, ibo, tex;
if(scene == NULL)
{
fprintf(stderr, "Could not load file '%s'\n", objfile);
return 1;
}
int count = 0, size = 0;
int i, j, k;
for(i = 0; i < scene->mNumMeshes; i ++)
size += (3 * scene->mMeshes[i]->mNumFaces);
Vertex* vertices = (Vertex*)malloc(size * sizeof(Vertex));
int* indices = (int*)malloc(size * sizeof(int));
for(i = 0; i < scene->mNumMeshes; i ++)
{
struct aiMesh* mesh = scene->mMeshes[i];
int meshFaces = mesh->mNumFaces;
for(j = 0; j < meshFaces; j ++)
{
struct aiFace* face = &(mesh->mFaces[j]);
for(k = 0; k < face->mNumIndices; k ++)
{
int index = face->mIndices[k];
struct aiVector3D pos = mesh->mVertices[index];
struct aiVector3D uv = mesh->mTextureCoords[0][index];
struct aiVector3D normal = {.x=1.0f,.y=1.0f,.z=1.0f};
if(mesh->mNormals != NULL)
normal = mesh->mNormals[index];
Vertex _vertex = {.x=pos.x * scale,
.y=pos.y * scale,
.z=pos.z * scale,
.u=uv.x, .v=uv.y,
.nx=normal.x * scale,
.ny=normal.y * scale,
.nz=normal.z * scale};
vertices[count] = _vertex;
indices[count] = count;
count ++;
}
}
}
aiReleaseImport(scene);
tex = loadTexture(texfile);
if(tex == 0)
{
fprintf(stderr, "Could not load file '%s'\n", texfile);
return 1;
}
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, size * sizeof(Vertex), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size * sizeof(int), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
我的loadTexture 函数适用于我使用过的所有其他纹理,所以我怀疑这是问题所在。对于我的一些更基本的模型,我根本没有任何问题。但是当我尝试加载更复杂的模型时,比如这个:
Quad Shotgun rendered in Blender
纹理坐标被扔掉了,像这样:Quad Shotgun rendered first person
另外,为了确保不正确加载与.obj 关联的.mtl 文件不是问题,我删除了.mtl 中的所有内容,除了它定义了纹理文件的位置,因此我仍然可以加载它进入搅拌机。结果相同。我已经对 Assimp 进行了研究,我确信这不是我的渲染循环的问题。请帮忙,我不知道我还错过了什么,或者我的程序可能出了什么问题!
【问题讨论】: