b71f50d7c50a2f84c18b4bfb2702771087623f82
[mandelwow.git] / support / mod.rs
1 #![allow(dead_code)]
2
3 extern crate genmesh;
4 extern crate obj;
5
6 use glium::{self, Display};
7 use glium::vertex::VertexBufferAny;
8
9 pub mod camera;
10 pub mod vec3;
11
12 pub enum Action {
13     Stop,
14     Continue,
15 }
16
17 pub fn start_loop<F>(mut callback: F) where F: FnMut() -> Action {
18     loop {
19         match callback() {
20             Action::Stop => break,
21             Action::Continue => ()
22         };
23     }
24 }
25
26 /// Returns a vertex buffer that should be rendered as `TrianglesList`.
27 pub fn load_wavefront(display: &Display, data: &[u8]) -> VertexBufferAny {
28     #[derive(Copy, Clone)]
29     struct Vertex {
30         position: [f32; 3],
31         normal: [f32; 3],
32         texture: [f32; 2],
33     }
34
35     implement_vertex!(Vertex, position, normal, texture);
36
37     let mut data = ::std::io::BufReader::new(data);
38     let data = obj::Obj::load(&mut data);
39
40     let mut vertex_data = Vec::new();
41
42     for object in data.object_iter() {
43         for shape in object.group_iter().flat_map(|g| g.indices().iter()) {
44             match shape {
45                 &genmesh::Polygon::PolyTri(genmesh::Triangle { x: v1, y: v2, z: v3 }) => {
46                     for v in [v1, v2, v3].iter() {
47                         let position = data.position()[v.0];
48                         let texture = v.1.map(|index| data.texture()[index]);
49                         let normal = v.2.map(|index| data.normal()[index]);
50
51                         let texture = texture.unwrap_or([0.0, 0.0]);
52                         let normal = normal.unwrap_or([0.0, 0.0, 0.0]);
53
54                         vertex_data.push(Vertex {
55                             position: position,
56                             normal: normal,
57                             texture: texture,
58                         })
59                     }
60                 },
61                 _ => unimplemented!()
62             }
63         }
64     }
65
66     glium::vertex::VertexBuffer::new(display, &vertex_data).unwrap().into_vertex_buffer_any()
67 }