我正在使用以下代码.obj
使用 Assimp 加载文件。纹理坐标出现问题。我尝试访问的 aiVector3D 存在,但是一旦我将值存储在临时变量中,它就会使我的应用程序崩溃。
这是我使用的代码:
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(filename,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);
if (!scene)
{
printf("[ASSIMP] ");
printf(importer.GetErrorString());
printf("\n");
return nullptr;
}
Mesh* newMesh = new Mesh();
unsigned int vertexCount = scene->mMeshes[0]->mNumVertices;
unsigned int triangleCount = scene->mMeshes[0]->mNumFaces;
bool hasUv = scene->mMeshes[0]->HasTextureCoords(0);
newMesh->vertexCount = vertexCount;
newMesh->triangleCount = triangleCount;
newMesh->m_Vertices = new Vertex[vertexCount];
newMesh->m_Triangles = new Triangle[triangleCount];
for (unsigned int i = 0; i < vertexCount; i++) {
aiVector3D vertexPosition = scene->mMeshes[0]->mVertices[i];
aiVector3D vertexNormal = scene->mMeshes[0]->mNormals[i];
newMesh->m_Vertices[i].pos = glm::vec3(vertexPosition.x, vertexPosition.y, vertexPosition.z);
newMesh->m_Vertices[i].normal = glm::vec3(vertexNormal.x, vertexNormal.y, vertexNormal.z);
if (hasUv) {
aiVector3D uvCoordinates = scene->mMeshes[0]->mTextureCoords[i][0];
printf("uvCoordinates: %f %f %f\n", uvCoordinates.x, uvCoordinates.y, uvCoordinates.z);
newMesh->m_Vertices[i].u = uvCoordinates.x;
newMesh->m_Vertices[i].v = uvCoordinates.y;
}
}
for (unsigned int i = 0; i < triangleCount; i++) {
aiFace face = scene->mMeshes[0]->mFaces[i];
for (int j = 0; j < 3; j++) {
Triangle* tri = &newMesh->m_Triangles[i];
tri->vertex[j] = &newMesh->m_Vertices[face.mIndices[j]];
if (tri->vertex[0]->normal.y == 1.0f || tri->vertex[0]->normal.y == -1.0f) {
tri->color = glm::vec3(0.2f, 0.2f, 0.2f);
}
else
{
tri->color = glm::vec3(1.0f, 0.0f, 0.0f);
}
}
}
它在该行崩溃printf("uvCoordinates: %f %f %f\n", uvCoordinates.x, uvCoordinates.y, uvCoordinates.z);
,但如果我删除该行,它在newMesh->m_Vertices[i].u = uvCoordinates.x;
. 如果我注释掉,将顶点的和printf()
都设置为并且根本不使用,它仍然在线崩溃。如果我不加注释,它会打印 的值,但之后会引发访问冲突。u
v
0.0f
uvCoordinates
newMesh->m_Vertices[i].u = uvCoordinates.x;
printf()
uvCoordinates
老实说,我在这里没有想法。这是一个屏幕截图,显示了我解释的内容。