4 | 4 |
use glium::glutin::{Event,WindowBuilder};
|
5 | 5 |
use glium::texture::{RawImage2d,Texture2d};
|
6 | 6 |
|
7 | |
use therm_model::model::Model;
|
| 7 |
use therm_model::graph::SceneGraph;
|
8 | 8 |
|
9 | 9 |
const VERTEX_SHADER: &'static str = include_str!("../data/shaders/vertex.glsl");
|
10 | |
|
11 | 10 |
const FRAGMENT_SHADER: &'static str = include_str!("../data/shaders/fragment.glsl");
|
12 | 11 |
|
13 | 12 |
pub struct Graphics {
|
14 | 13 |
disp: Display,
|
15 | 14 |
rt: f32,
|
16 | |
px: f32,
|
17 | |
py: f32,
|
18 | |
pz: f32,
|
19 | 15 |
light: [f32;3],
|
20 | |
model: Model,
|
| 16 |
graph: SceneGraph,
|
21 | 17 |
program: Program,
|
22 | 18 |
tex: Texture2d,
|
23 | 19 |
}
|
24 | 20 |
|
| 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 |
|
25 | 33 |
impl Graphics {
|
26 | |
pub fn new(m: Model, t: RawImage2d<u8>) -> Self {
|
| 34 |
pub fn new(sg: SceneGraph, t: RawImage2d<u8>) -> Self {
|
27 | 35 |
let disp = WindowBuilder::new()
|
28 | 36 |
.with_title(format!("utmyen"))
|
29 | 37 |
.with_depth_buffer(24)
|
|
41 | 49 |
Graphics {
|
42 | 50 |
disp: disp,
|
43 | 51 |
rt: 0.0,
|
44 | |
px: 0.0,
|
45 | |
py: 0.01,
|
46 | |
pz: 0.0,
|
47 | 52 |
light: [-1.0, 0.4, 0.9f32],
|
48 | |
model: m,
|
| 53 |
graph: SceneGraph::Translate(0.0,0.0,1.0,Box::new(sg)),
|
49 | 54 |
program: program,
|
50 | 55 |
tex: tex,
|
51 | 56 |
}
|
|
66 | 71 |
match n {
|
67 | 72 |
9 => return,
|
68 | 73 |
24 => self.rt -= 0.1,
|
69 | |
25 => self.py -= DX,
|
| 74 |
25 => nudge(&mut self.graph,0.0,0.0,-DX),
|
70 | 75 |
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),
|
76 | 81 |
_ => {},
|
77 | 82 |
},
|
78 | 83 |
_ => {},
|
|
82 | 87 |
}
|
83 | 88 |
}
|
84 | 89 |
|
85 | |
const DX: f32 = 0.001;
|
86 | |
const SX: f32 = 0.01;
|
| 90 |
const DX: f32 = 0.01;
|
87 | 91 |
|
88 | 92 |
struct Target<'a> {
|
89 | 93 |
frame: &'a mut Frame,
|
|
101 | 105 |
.. Default::default()
|
102 | 106 |
};
|
103 | 107 |
|
104 | |
let vbuf = g.model.get_vbuf(&g.disp);
|
105 | |
let idxs = g.model.get_ibuf(&g.disp);
|
106 | 108 |
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);
|
112 | 109 |
|
113 | 110 |
let mytex = glium::uniforms::Sampler::new(&g.tex)
|
114 | 111 |
.minify_filter(glium::uniforms::MinifySamplerFilter::Nearest)
|
115 | 112 |
.magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest);
|
116 | 113 |
|
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 | |
¶ms).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 | |
¶ms).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 | |
¶ms).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 |
¶ms).unwrap();
|
| 126 |
});
|
149 | 127 |
|
150 | 128 |
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 | |
]
|
161 | 129 |
}
|
162 | 130 |
|
163 | 131 |
fn perspective(&self) -> [[f32;4];4] {
|