我有一個程序我在C.用於測試加載模型轉換成OpenGL該代碼,這是儘可能與Assimp模型加載進入非常簡單(我的理解):與模型紋理COORDS問題的OpenGL
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
此外,以確保它不是一個問題與不加載.mtl
文件正確地與.obj
相關聯,我擺脫了.mtl
中的所有內容,除非它定義了紋理文件,所以我仍然可以將它加載到Blender中。相同的結果。我已經完成了關於Assimp的研究,並且我確信這不是我的渲染循環的問題。請幫忙,我不知道我在這裏錯過了什麼,或者我的程序會出現什麼問題!