Assimp 对现有 aiVector3D 的访问冲突
Assimp access violation on existing aiVector3D
我正在使用以下代码通过 Assimp 加载 .obj
文件。 TextureCoordinates 出现问题。我尝试访问的 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
的值,但会在之后引发访问冲突。
老实说,我在这里没有想法。 a screenshot 展示了我的解释。
scene->mMeshes[0]->mTextureCoords[i][0];
是集合 i
的第一个纹理坐标。你想要的是获得第一组纹理坐标 - 所以它应该是 scene->mMeshes[0]->mTextureCoords[0][i];
我正在使用以下代码通过 Assimp 加载 .obj
文件。 TextureCoordinates 出现问题。我尝试访问的 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
的值,但会在之后引发访问冲突。
老实说,我在这里没有想法。 a screenshot 展示了我的解释。
scene->mMeshes[0]->mTextureCoords[i][0];
是集合 i
的第一个纹理坐标。你想要的是获得第一组纹理坐标 - 所以它应该是 scene->mMeshes[0]->mTextureCoords[0][i];