Added some movement commands as well as proper index loading for models
Getty Ritter
8 years ago
46 | 46 | None => Err("Unexpected end-of-stream"), |
47 | 47 | } |
48 | 48 | }; |
49 | let mut buf = [ try!(next_byte()), | |
50 | try!(next_byte()), | |
51 | try!(next_byte()), | |
52 | try!(next_byte()) ]; | |
49 | let buf = [ try!(next_byte()), | |
50 | try!(next_byte()), | |
51 | try!(next_byte()), | |
52 | try!(next_byte()) ]; | |
53 | 53 | let _ = next_byte(); |
54 | 54 | let f = unsafe { mem::transmute::<[u8;4], f32>(buf) }; |
55 | 55 | Ok(Bencode::Float(f)) |
135 | 135 | } |
136 | 136 | } |
137 | 137 | |
138 | pub fn as_u16(&self) -> Result<u16, String> { | |
139 | match self { | |
140 | &Bencode::Int(n) => Ok(n as u16), | |
141 | _ => fail("not a number"), | |
142 | } | |
143 | } | |
144 | ||
138 | 145 | pub fn as_i64(&self) -> Result<i64, String> { |
139 | 146 | match self { |
140 | 147 | &Bencode::Int(n) => Ok(n), |
2 | 2 | #[macro_use] |
3 | 3 | extern crate glium; |
4 | 4 | |
5 |
|
|
5 | // mod adnot; | |
6 | 6 | mod bencode; |
7 | 7 | mod model; |
8 | 8 | |
19 | 19 | const VERTEX_SHADER: &'static str = r#" |
20 | 20 | #version 140 |
21 | 21 | in vec3 pos; |
22 | ||
23 | uniform mat4 perspective; | |
24 | uniform mat4 matrix; | |
25 | ||
26 | out vec3 o_pos; | |
27 | ||
22 | 28 | void main() { |
23 |
|
|
29 | o_pos = pos; | |
30 | gl_Position = perspective * matrix * vec4(pos, 1.0); | |
24 | 31 | } |
25 | 32 | "#; |
26 | 33 | |
27 | 34 | const FRAGMENT_SHADER: &'static str = r#" |
28 | 35 | #version 140 |
36 | in vec3 o_pos; | |
29 | 37 | out vec4 color; |
30 | 38 | void main() { |
31 |
color = vec4( |
|
39 | color = vec4(o_pos.z * 2.0, 0.0, 1.0 - (o_pos.z * 2.0), 1.0); | |
32 | 40 | } |
33 | 41 | "#; |
34 | 42 | |
39 | 47 | .build_glium() |
40 | 48 | .unwrap(); |
41 | 49 | let vbuf = md.get_vbuf(&display); |
42 | let idxs = glium::index::NoIndices( | |
43 | glium::index::PrimitiveType::TrianglesList); | |
50 | let idxs = md.get_ibuf(&display); | |
44 | 51 | let program = Program::from_source( |
45 | 52 | &display, |
46 | 53 | VERTEX_SHADER, |
47 | 54 | FRAGMENT_SHADER, |
48 | 55 | None).unwrap(); |
49 | 56 | |
57 | ||
58 | let mut px = 0f32; | |
59 | let mut py = 0f32; | |
60 | let mut t = 0f32; | |
61 | let dx = 0.001; | |
62 | let sx = 0.01f32; | |
50 | 63 | loop { |
64 | t += 0.01; | |
51 | 65 | let mut target = display.draw(); |
66 | ||
67 | let perspective = { | |
68 | let (width, height) = target.get_dimensions(); | |
69 | let aspect_ratio = height as f32 / width as f32; | |
70 | ||
71 | let fov: f32 = 3.141592 / 3.0; | |
72 | let zfar = 1024.0; | |
73 | let znear = 0.1; | |
74 | ||
75 | let f = 1.0 / (fov / 2.0).tan(); | |
76 | ||
77 | [ | |
78 | [f * aspect_ratio , 0.0, 0.0 , 0.0], | |
79 | [ 0.0 , f , 0.0 , 0.0], | |
80 | [ 0.0 , 0.0, (zfar+znear)/(zfar-znear) , 1.0], | |
81 | [ 0.0 , 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0], | |
82 | ] | |
83 | }; | |
84 | let uniforms = uniform! { | |
85 | perspective: perspective, | |
86 | matrix: [ [sx, 0.0, 0.0, 0.0], | |
87 | [0.0, t.cos() * sx, -t.sin() * sx, 0.0], | |
88 | [0.0, t.sin() * sx, t.cos() * sx, 0.0], | |
89 | [px, 0.0, py, sx] | |
90 | ] | |
91 | }; | |
92 | // let params = glium::DrawParameters { | |
93 | // depth: glium::Depth { | |
94 | // test: glium::draw_parameters::DepthTest::IfLess, | |
95 | // write: true, | |
96 | // .. Default::default() | |
97 | // }, | |
98 | // .. Default::default() | |
99 | // }; | |
52 | 100 | target.clear_color(0.0, 0.0, 0.0, 1.0); |
53 | 101 | target.draw(&vbuf, |
54 | 102 | &idxs, |
55 | 103 | &program, |
56 |
& |
|
104 | &uniforms, | |
57 | 105 | &Default::default()).unwrap(); |
58 | 106 | target.finish().unwrap(); |
59 | 107 | for ev in display.poll_events() { |
60 | 108 | match ev { |
61 | 109 | Event::Closed => return, |
62 | 110 | Event::KeyboardInput(_, 9, _) => return, |
63 |
|
|
111 | Event::KeyboardInput(_, 25, _) => { | |
112 | py -= dx; | |
113 | }, | |
114 | Event::KeyboardInput(_, 38, _) => { | |
115 | px -= dx; | |
116 | }, | |
117 | Event::KeyboardInput(_, 39, _) => { | |
118 | py += dx; | |
119 | }, | |
120 | Event::KeyboardInput(_, 40, _) => { | |
121 | px += dx; | |
122 | }, | |
123 | evt => { | |
124 | }, | |
64 | 125 | } |
65 | 126 | } |
66 | 127 | } |
13 | 13 | #[derive(Debug, Clone)] |
14 | 14 | pub struct Model { |
15 | 15 | pub points: Vec<V3D>, |
16 |
pub tris: Vec<u |
|
16 | pub tris: Vec<u16>, | |
17 | 17 | } |
18 | 18 | |
19 | 19 | impl Model { |
31 | 31 | |
32 | 32 | pub fn load(is: &[u8]) -> Result<Model, String> { |
33 | 33 | let bs = try!(Bencode::parse(is)); |
34 | println!("{:?}", bs); | |
34 | 35 | Ok(Model { |
35 | 36 | points: try!(try!(bs.get_field("pts")).map(load_v3d)), |
36 |
tris: try!(try!(bs.get_field("tris")).map(|v| v.as_u |
|
37 | tris: try!(try!(bs.get_field("tris")).map(|v| v.as_u16())), | |
37 | 38 | }) |
38 | 39 | } |
39 | 40 | |
40 | 41 | pub fn get_vbuf(&self, display: &glium::Display) -> glium::VertexBuffer<V3D> { |
41 | 42 | glium::VertexBuffer::new(display, self.points.as_slice()).unwrap() |
42 | 43 | } |
44 | ||
45 | pub fn get_ibuf(&self, display: &glium::Display) -> glium::IndexBuffer<u16> { | |
46 | glium::IndexBuffer::new( | |
47 | display, | |
48 | glium::index::PrimitiveType::TrianglesList, | |
49 | &self.tris).unwrap() | |
50 | } | |
43 | 51 | } |
44 | 52 | |
45 | 53 | fn load_v3d(bs: &Bencode) -> Result<V3D, String> { |
46 | 54 | Ok(V3D { |
47 | pos: [ try!(try!(bs.get_idx(0)).as_float()), | |
48 | try!(try!(bs.get_idx(1)).as_float()), | |
49 |
|
|
55 | pos: [ try!(try!(bs.get_idx(0)).as_float()) * 0.5, | |
56 | try!(try!(bs.get_idx(1)).as_float()) * 0.5, | |
57 | try!(try!(bs.get_idx(2)).as_float()) * 0.5, | |
50 | 58 | ], |
51 | 59 | tex: [ try!(try!(bs.get_idx(3)).as_float()), |
52 | 60 | try!(try!(bs.get_idx(4)).as_float()), |