我正在尝试使用assimp加载一些ply文件。我正在获取顶点位置并且法线也正确加载(我看到它们出现在屏幕上所以一切都很好)然而似乎纹理坐标永远不会被加载。这会导致纹理不会出现在模型和我的漫反射颜色上出现在他们而不是纹理上。
我查看了我的代码,发现问题出在我所做的assimp loader类中。 mTextureCoords [0] [index]完全为空。
我已经尝试更改纹理坐标分配的if,但我相信这是正确的?
这是一个loader方法的片段,其中包含获取纹理坐标的代码
if (scene->HasMeshes()) { //Check to see if the filename was valid
aiMesh* aiMesh = *scene->mMeshes;
theMesh.numberOfVertices = aiMesh->mNumVertices;
//Create the number of vertices that this mesh has.
theMesh.pVertices = new sVertex_xyz_rgba_n_uv2_bt[theMesh.numberOfVertices];
theMesh.minXYZ.x = aiMesh->mVertices[0].x;
theMesh.minXYZ.y = aiMesh->mVertices[0].y;
theMesh.minXYZ.z = aiMesh->mVertices[0].z;
theMesh.maxXYZ = theMesh.minXYZ;
for (unsigned int index = 0; index != theMesh.numberOfVertices; index++) {
theMesh.pVertices[index].x = aiMesh->mVertices[index].x;
theMesh.pVertices[index].y = aiMesh->mVertices[index].y;
theMesh.pVertices[index].z = aiMesh->mVertices[index].z;
if (theMesh.pVertices[index].x < theMesh.minXYZ.x) { theMesh.minXYZ.x = theMesh.pVertices[index].x; }
if (theMesh.pVertices[index].y < theMesh.minXYZ.y) { theMesh.minXYZ.y = theMesh.pVertices[index].y; }
if (theMesh.pVertices[index].z < theMesh.minXYZ.z) { theMesh.minXYZ.z = theMesh.pVertices[index].z; }
if (theMesh.pVertices[index].x > theMesh.maxXYZ.x) { theMesh.maxXYZ.x = theMesh.pVertices[index].x; }
if (theMesh.pVertices[index].y > theMesh.maxXYZ.y) { theMesh.maxXYZ.y = theMesh.pVertices[index].y; }
if (theMesh.pVertices[index].z > theMesh.maxXYZ.z) { theMesh.maxXYZ.z = theMesh.pVertices[index].z; }
theMesh.pVertices[index].nx = aiMesh->mNormals[index].x;
theMesh.pVertices[index].ny = aiMesh->mNormals[index].y;
theMesh.pVertices[index].nz = aiMesh->mNormals[index].z;
int numUVComponents = aiMesh->mNumUVComponents[0];
if (numUVComponents == 2) {
theMesh.pVertices[index].u1 = aiMesh->mTextureCoords[0][index].x;
theMesh.pVertices[index].v1 = aiMesh->mTextureCoords[0][index].y;
}
}
theMesh.numberOfTriangles = scene->mMeshes[0]->mNumFaces;
theMesh.pTriangles = new cTriangle[theMesh.numberOfTriangles];
//Copy the triangle vertex information (indices)
for (int triIndex = 0; triIndex != theMesh.numberOfTriangles; triIndex++) {
theMesh.pTriangles[triIndex].vertex_ID_0 = aiMesh->mFaces[triIndex].mIndices[0];
theMesh.pTriangles[triIndex].vertex_ID_1 = aiMesh->mFaces[triIndex].mIndices[1];
theMesh.pTriangles[triIndex].vertex_ID_2 = aiMesh->mFaces[triIndex].mIndices[2];
}
}
这里是模型文件的片段,我试图用标题和顶点,法线和紫外线的几行加载assimp。
ply
format ascii 1.0
comment VCGLIB generated
element vertex 289
property float x
property float y
property float z
property float nx
property float ny
property float nz
property float texture_u
property float texture_v
element face 512
property list uchar int vertex_indices
end_header
-400 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.000000
-350 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.062500
-300 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.125000
-250 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.187500
-200 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.250000
-150 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.312500
-100 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.375000
-50 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.437500
希望有人知道我在哪里弄乱了=)在此先感谢!