首页 > 解决方案 > Rust/OpenGL - 加载 OBJ 文件时缺少顶点索引

问题描述

我正在尝试使用模块将 .obj 文件中的模型加载到 OpenGL 中gliumwavefront_obj我可以很好地获取顶点和法线,但似乎这个库没有读取索引缓冲区的索引(https:/ /docs.rs/wavefront_obj/10.0.0/wavefront_obj/obj/struct.Object.html)。

我加载模型的代码如下:

...
let tp_input: String = std::fs::read_to_string("assets/models/teapot.obj").unwrap().parse().unwrap();
let tp_object = wavefront_obj::obj::parse(tp_input).unwrap();

let mut tp_vertexes: Vec<teapot::Vertex> = Vec::new();
let mut tp_normals: Vec<teapot::Normal> = Vec::new();

for object in tp_object.objects.iter() {
    for (vertex, normal) in object.vertices.iter().zip(object.normals.iter()) {
        tp_vertexes.push(teapot::Vertex { position: (vertex.x as f32, vertex.y as f32, vertex.z as f32) });
        tp_normals.push(teapot::Normal { normal: (normal.x as f32, normal.y as f32, normal.z as f32) });
    }
}

let positions = glium::VertexBuffer::new(&display, &tp_vertexes).unwrap();
let normals = glium::VertexBuffer::new(&display, &tp_normals).unwrap();
let indices = glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip);

let program_teapot = glium::Program::from_source(&display, shaders::vertex_teapot_src(), shaders::fragment_teapot_src(), None).unwrap();

...

target.draw((&positions, &normals), &indices, &program_teapot, &uniforms_teapot, &params).unwrap();

我得到的结果是这样的: OpenGL 茶壶

我还没有找到通过图书馆文档阅读的解决方案,有没有更好的图书馆或者我可以应用任何方法来获取这些索引?

标签: openglrust3dindex-bufferglium

解决方案


推荐阅读