没有做出任何改变,还是用了OpenMesh::IO::read_texture函数,请使用OpenMesh8.0版本中有的函数
时间: 2024-03-20 19:45:31 浏览: 103
非常抱歉之前的回答有误,确实 OpenMesh 库中没有 `OpenMesh::IO::read_texture` 函数。以下是使用 OpenMesh 8.0 版本中的函数来读取带纹理的网格文件的示例代码:
```c++
#include <iostream>
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
struct MyTraits : public OpenMesh::DefaultTraits {
typedef OpenMesh::Vec3d Point;
typedef OpenMesh::Vec3d Normal;
typedef OpenMesh::Vec2d TexCoord2D;
};
typedef OpenMesh::TriMesh_ArrayKernelT<MyTraits> MyMesh;
int main(int argc, char **argv) {
MyMesh mesh;
if (!OpenMesh::IO::read_mesh(mesh, "my_mesh.obj")) {
std::cerr << "Failed to read mesh file!" << std::endl;
return 1;
}
// Check if the mesh has texture coordinates
if (!mesh.has_vertex_texcoords2D()) {
std::cerr << "Mesh doesn't have texture coordinates!" << std::endl;
return 1;
}
// Read texture coordinates
OpenMesh::MPropHandleT<MyMesh::TexCoord2D> texcoordProp;
if (!mesh.get_property_handle(texcoordProp, "v:texcoord")) {
std::cerr << "Failed to get texture coordinate property handle!" << std::endl;
return 1;
}
if (!OpenMesh::IO::read_tex(mesh, texcoordProp, "my_texture.png")) {
std::cerr << "Failed to read texture file!" << std::endl;
return 1;
}
// Access texture coordinates
for (MyMesh::VertexIter v_it = mesh.vertices_begin(); v_it != mesh.vertices_end(); ++v_it) {
MyMesh::TexCoord2D texCoord = mesh.property(texcoordProp, *v_it);
std::cout << "Vertex " << *v_it << " has texture coordinates (" << texCoord[0] << ", " << texCoord[1] << ")" << std::endl;
}
return 0;
}
```
这个示例使用了 `OpenMesh::IO::read_mesh` 函数来读取网格文件,使用了 `OpenMesh::IO::read_tex` 函数来读取纹理文件,并且使用了 `OpenMesh::MPropHandleT` 类型来存储纹理坐标属性。请注意,纹理文件仍然需要是 PNG 格式。另外请注意,如果您在使用自定义 traits 类型时需要读取不同的属性,请相应地修改 traits 类型的定义。
阅读全文