gdritter repos utmyen / dc5a842
Added user control of rotation instead of automatic rotating; now loading model from command-line args instead of hard-coding it; proper depth buffer handling Getty Ritter 8 years ago
2 changed file(s) with 34 addition(s) and 22 deletion(s). Collapse all Expand all
1313 // use bencode::Bencode;
1414
1515 fn sample() -> model::Model {
16 model::Model::load_from_file("data/test/cube.model").unwrap()
16 let file = std::env::args().nth(1)
17 .unwrap_or("data/test/cube.model".to_string());
18 model::Model::load_from_file(&file).unwrap()
1719 }
1820
1921 const VERTEX_SHADER: &'static str = r#"
4446 let md = sample();
4547 let display = WindowBuilder::new()
4648 .with_title(format!("utymen"))
49 .with_depth_buffer(24)
4750 .build_glium()
4851 .unwrap();
4952 let vbuf = md.get_vbuf(&display);
5457 FRAGMENT_SHADER,
5558 None).unwrap();
5659
57
5860 let mut px = 0f32;
59 let mut py = 0f32;
60 let mut t = 0f32;
61 let mut py = 0.02f32;
62 let mut rt = 0f32;
6163 let dx = 0.001;
6264 let sx = 0.01f32;
65
6366 loop {
64 t += 0.01;
6567 let mut target = display.draw();
6668
6769 let perspective = {
7981 [ 0.0 , f , 0.0 , 0.0],
8082 [ 0.0 , 0.0, (zfar+znear)/(zfar-znear) , 1.0],
8183 [ 0.0 , 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0],
82 ]
84 ]
8385 };
86
8487 let uniforms = uniform! {
8588 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]
89 matrix: [ [ rt.cos() * sx, 0.0, -rt.sin() * sx, 0.0 ],
90 [ 0.0, sx, 0.0, 0.0 ],
91 [ rt.sin() * sx, 0.0, rt.cos() * sx, 0.0 ],
92 [ px, 0.0, py, sx ]
9093 ]
9194 };
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 // };
100 target.clear_color(0.0, 0.0, 0.0, 1.0);
95
96 let params = glium::DrawParameters {
97 depth: glium::Depth {
98 test: glium::draw_parameters::DepthTest::IfLess,
99 write: true,
100 .. Default::default()
101 },
102 .. Default::default()
103 };
104
105 target.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
101106 target.draw(&vbuf,
102107 &idxs,
103108 &program,
104109 &uniforms,
105 &Default::default()).unwrap();
110 &params).unwrap();
106111 target.finish().unwrap();
112
107113 for ev in display.poll_events() {
108114 match ev {
109115 Event::Closed => return,
110116 Event::KeyboardInput(_, 9, _) => return,
117 Event::KeyboardInput(_, 24, _) => {
118 rt -= 0.1;
119 },
111120 Event::KeyboardInput(_, 25, _) => {
112121 py -= dx;
122 },
123 Event::KeyboardInput(_, 26, _) => {
124 rt += 0.1;
113125 },
114126 Event::KeyboardInput(_, 38, _) => {
115127 px -= dx;
5252
5353 fn load_v3d(bs: &Bencode) -> Result<V3D, String> {
5454 Ok(V3D {
55 pos: [ try!(try!(bs.get_idx(0)).as_float()) * 0.5,
56 try!(try!(bs.get_idx(1)).as_float()) * 0.5,
55 pos: [ try!(try!(bs.get_idx(1)).as_float()) * 0.5,
5756 try!(try!(bs.get_idx(2)).as_float()) * 0.5,
57 try!(try!(bs.get_idx(0)).as_float()) * 0.5,
5858 ],
5959 tex: [ try!(try!(bs.get_idx(3)).as_float()),
6060 try!(try!(bs.get_idx(4)).as_float()),