-
Notifications
You must be signed in to change notification settings - Fork 4
/
render.rs
94 lines (75 loc) · 2.31 KB
/
render.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use glium::{Display, Program, DrawParameters, Frame, Surface};
use glium::uniforms::Uniforms;
use mesh::Mesh;
pub use math::Mat;
pub trait Renderable {
fn draw(&self, renderer: &Renderer, target: &mut Frame, parent: &Mat);
}
impl<'a> Renderable for Vec<&'a Renderable> {
fn draw(&self, renderer: &Renderer, target: &mut Frame, parent: &Mat) {
for x in self {
x.draw(renderer, target, parent);
}
}
}
pub struct Renderer<'display> {
pub display: &'display Display,
program: Program,
params: DrawParameters<'display>,
}
impl<'display> Renderer<'display> {
pub fn new(display: &'display Display) -> Renderer<'display> {
Renderer::with_shader(display,
include_str!("shader/140/default.vert"),
include_str!("shader/140/default.frag"))
}
pub fn with_shader(display: &'display Display, vertex: &str, fragment: &str)
-> Renderer<'display>
{
let program = program!(display,
140 => {
vertex: vertex,
fragment: fragment,
},
).unwrap();
Renderer {
display: display,
program: program,
params: Renderer::build_params(),
}
}
pub fn draw<U>(&self, target: &mut Frame, mesh: &Mesh, uniforms: &U)
where U: Uniforms {
target.draw(
&mesh.vertex_buffer,
&mesh.index_buffer,
&self.program,
uniforms,
&self.params
).unwrap();
}
pub fn render<T: Renderable>(&self, target: &mut Frame, renderable: &T, matrix: &Mat) {
renderable.draw(self, target, matrix);
}
fn build_params<'a>() -> DrawParameters<'a> {
use glium::BlendingFunction::Addition;
use glium::LinearBlendingFactor::*;
use std::default::Default;
let blending_function = Addition {
source: SourceAlpha,
destination: OneMinusSourceAlpha
};
DrawParameters {
blending_function: Some (blending_function),
.. Default::default()
}
}
}
pub trait DisplayExt {
fn hidpi_factor(&self) -> f32;
}
impl DisplayExt for Display {
fn hidpi_factor(&self) -> f32 {
self.get_window().unwrap().hidpi_factor()
}
}