gdritter repos utmyen / f4cf520
Added some movement commands as well as proper index loading for models Getty Ritter 8 years ago
3 changed file(s) with 92 addition(s) and 16 deletion(s). Collapse all Expand all
4646 None => Err("Unexpected end-of-stream"),
4747 }
4848 };
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()) ];
5353 let _ = next_byte();
5454 let f = unsafe { mem::transmute::<[u8;4], f32>(buf) };
5555 Ok(Bencode::Float(f))
135135 }
136136 }
137137
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
138145 pub fn as_i64(&self) -> Result<i64, String> {
139146 match self {
140147 &Bencode::Int(n) => Ok(n),
22 #[macro_use]
33 extern crate glium;
44
5 mod adnot;
5 // mod adnot;
66 mod bencode;
77 mod model;
88
1919 const VERTEX_SHADER: &'static str = r#"
2020 #version 140
2121 in vec3 pos;
22
23 uniform mat4 perspective;
24 uniform mat4 matrix;
25
26 out vec3 o_pos;
27
2228 void main() {
23 gl_Position = vec4(pos, 1.0);
29 o_pos = pos;
30 gl_Position = perspective * matrix * vec4(pos, 1.0);
2431 }
2532 "#;
2633
2734 const FRAGMENT_SHADER: &'static str = r#"
2835 #version 140
36 in vec3 o_pos;
2937 out vec4 color;
3038 void main() {
31 color = vec4(1.0, 0.0, 0.0, 1.0);
39 color = vec4(o_pos.z * 2.0, 0.0, 1.0 - (o_pos.z * 2.0), 1.0);
3240 }
3341 "#;
3442
3947 .build_glium()
4048 .unwrap();
4149 let vbuf = md.get_vbuf(&display);
42 let idxs = glium::index::NoIndices(
43 glium::index::PrimitiveType::TrianglesList);
50 let idxs = md.get_ibuf(&display);
4451 let program = Program::from_source(
4552 &display,
4653 VERTEX_SHADER,
4754 FRAGMENT_SHADER,
4855 None).unwrap();
4956
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;
5063 loop {
64 t += 0.01;
5165 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 // };
52100 target.clear_color(0.0, 0.0, 0.0, 1.0);
53101 target.draw(&vbuf,
54102 &idxs,
55103 &program,
56 &glium::uniforms::EmptyUniforms,
104 &uniforms,
57105 &Default::default()).unwrap();
58106 target.finish().unwrap();
59107 for ev in display.poll_events() {
60108 match ev {
61109 Event::Closed => return,
62110 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 },
64125 }
65126 }
66127 }
1313 #[derive(Debug, Clone)]
1414 pub struct Model {
1515 pub points: Vec<V3D>,
16 pub tris: Vec<usize>,
16 pub tris: Vec<u16>,
1717 }
1818
1919 impl Model {
3131
3232 pub fn load(is: &[u8]) -> Result<Model, String> {
3333 let bs = try!(Bencode::parse(is));
34 println!("{:?}", bs);
3435 Ok(Model {
3536 points: try!(try!(bs.get_field("pts")).map(load_v3d)),
36 tris: try!(try!(bs.get_field("tris")).map(|v| v.as_usize())),
37 tris: try!(try!(bs.get_field("tris")).map(|v| v.as_u16())),
3738 })
3839 }
3940
4041 pub fn get_vbuf(&self, display: &glium::Display) -> glium::VertexBuffer<V3D> {
4142 glium::VertexBuffer::new(display, self.points.as_slice()).unwrap()
4243 }
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 }
4351 }
4452
4553 fn load_v3d(bs: &Bencode) -> Result<V3D, String> {
4654 Ok(V3D {
47 pos: [ try!(try!(bs.get_idx(0)).as_float()),
48 try!(try!(bs.get_idx(1)).as_float()),
49 try!(try!(bs.get_idx(2)).as_float()),
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,
5058 ],
5159 tex: [ try!(try!(bs.get_idx(3)).as_float()),
5260 try!(try!(bs.get_idx(4)).as_float()),