gdritter repos utmyen / b9aac1b
Switched graphics type to render a scene graph (instead of just a plain model) Getty Ritter 8 years ago
2 changed file(s) with 45 addition(s) and 70 deletion(s). Collapse all Expand all
44 use glium::glutin::{Event,WindowBuilder};
55 use glium::texture::{RawImage2d,Texture2d};
66
7 use therm_model::model::Model;
7 use therm_model::graph::SceneGraph;
88
99 const VERTEX_SHADER: &'static str = include_str!("../data/shaders/vertex.glsl");
10
1110 const FRAGMENT_SHADER: &'static str = include_str!("../data/shaders/fragment.glsl");
1211
1312 pub struct Graphics {
1413 disp: Display,
1514 rt: f32,
16 px: f32,
17 py: f32,
18 pz: f32,
1915 light: [f32;3],
20 model: Model,
16 graph: SceneGraph,
2117 program: Program,
2218 tex: Texture2d,
2319 }
2420
21 fn nudge(g: &mut SceneGraph, dx:f32, dy:f32, dz:f32) {
22 match g {
23 &mut SceneGraph::Translate(ref mut x, ref mut y, ref mut z, _) => {
24 *x += dx;
25 *y += dy;
26 *z += dz;
27 },
28 _ => {},
29 }
30 }
31
32
2533 impl Graphics {
26 pub fn new(m: Model, t: RawImage2d<u8>) -> Self {
34 pub fn new(sg: SceneGraph, t: RawImage2d<u8>) -> Self {
2735 let disp = WindowBuilder::new()
2836 .with_title(format!("utmyen"))
2937 .with_depth_buffer(24)
4149 Graphics {
4250 disp: disp,
4351 rt: 0.0,
44 px: 0.0,
45 py: 0.01,
46 pz: 0.0,
4752 light: [-1.0, 0.4, 0.9f32],
48 model: m,
53 graph: SceneGraph::Translate(0.0,0.0,1.0,Box::new(sg)),
4954 program: program,
5055 tex: tex,
5156 }
6671 match n {
6772 9 => return,
6873 24 => self.rt -= 0.1,
69 25 => self.py -= DX,
74 25 => nudge(&mut self.graph,0.0,0.0,-DX),
7075 26 => self.rt += 0.1,
71 38 => self.px += DX,
72 39 => self.py += DX,
73 40 => self.px -= DX,
74 44 => self.pz -= DX,
75 45 => self.pz += DX,
76 38 => nudge(&mut self.graph,DX,0.0,0.0),
77 39 => nudge(&mut self.graph,0.0,0.0,DX),
78 40 => nudge(&mut self.graph,-DX,0.0,0.0),
79 44 => nudge(&mut self.graph,0.0,-DX,0.0),
80 45 => nudge(&mut self.graph,0.0,DX,0.0),
7681 _ => {},
7782 },
7883 _ => {},
8287 }
8388 }
8489
85 const DX: f32 = 0.001;
86 const SX: f32 = 0.01;
90 const DX: f32 = 0.01;
8791
8892 struct Target<'a> {
8993 frame: &'a mut Frame,
101105 .. Default::default()
102106 };
103107
104 let vbuf = g.model.get_vbuf(&g.disp);
105 let idxs = g.model.get_ibuf(&g.disp);
106108 self.frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
107
108 let per = self.perspective();
109 let m1 = self.matrix(g, g.px, g.py);
110 let m2 = self.matrix(g, g.px+0.01, g.py);
111 let m3 = self.matrix(g, g.px-0.01, g.py);
112109
113110 let mytex = glium::uniforms::Sampler::new(&g.tex)
114111 .minify_filter(glium::uniforms::MinifySamplerFilter::Nearest)
115112 .magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest);
116113
117 self.frame.draw(&vbuf,
118 &idxs,
119 &g.program,
120 &uniform! {
121 perspective: per,
122 u_light: g.light,
123 matrix: m1,
124 tx: mytex,
125 },
126 &params).unwrap();
127
128 self.frame.draw(&vbuf,
129 &idxs,
130 &g.program,
131 &uniform! {
132 perspective: per,
133 u_light: g.light,
134 matrix: m2,
135 tx: mytex,
136 },
137 &params).unwrap();
138
139 self.frame.draw(&vbuf,
140 &idxs,
141 &g.program,
142 &uniform! {
143 perspective: per,
144 u_light: g.light,
145 matrix: m3,
146 tx: mytex,
147 },
148 &params).unwrap();
114 let per = self.perspective();
115 g.graph.traverse(&mut |model, matrix| {
116 self.frame.draw(&model.get_vbuf(&g.disp),
117 &model.get_ibuf(&g.disp),
118 &g.program,
119 &uniform! {
120 perspective: per,
121 u_light: g.light,
122 matrix: *matrix,
123 tx: mytex,
124 },
125 &params).unwrap();
126 });
149127
150128 return true;
151 }
152
153 fn matrix(&self, g: &Graphics, px: f32, py: f32) -> [[f32;4];4] {
154 let rt = g.rt;
155 let pz = g.pz;
156 [ [ rt.cos() * SX, 0.0, -rt.sin() * SX, 0.0 ],
157 [ 0.0, SX, 0.0, 0.0 ],
158 [ rt.sin() * SX, 0.0, rt.cos() * SX, 0.0 ],
159 [ px, pz, py, SX ]
160 ]
161129 }
162130
163131 fn perspective(&self) -> [[f32;4];4] {
1010
1111 use glium::texture::{RawImage2d};
1212 use therm_model::model;
13 use therm_model::graph::SceneGraph;
1314
1415 fn sample() -> model::Model {
1516 let file = std::env::args().nth(1)
2021 fn sample_texture<'a>() -> RawImage2d<'a, u8> {
2122 use std::io::Cursor;
2223 let img = image::load(
23 Cursor::new(&include_bytes!("/home/gdritter/documents/pixel-chest.png")[..]),
24 Cursor::new(&include_bytes!("../data/test/chest.png")[..]),
2425 image::PNG).unwrap().to_rgba();
2526 let dims = img.dimensions();
2627 let img = glium::texture::RawImage2d::from_raw_rgba_reversed(img.into_raw(), dims);
3132 let md = sample();
3233 let tex = sample_texture();
3334
34 let mut g = graphics::Graphics::new(md, tex);
35 let graph = SceneGraph::Many(vec![
36 SceneGraph::Model(md.clone()),
37 SceneGraph::Translate(-0.3,0.0,-0.3, Box::new(SceneGraph::Model(md.clone())))
38 ]);
39
40 let mut g = graphics::Graphics::new(graph, tex);
41
3542 g.run();
3643 }