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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use glium::{ Display, DrawParameters, DisplayBuild, Surface, Depth };
use glium::index::{ NoIndices, PrimitiveType };
use glium::DepthTest::IfLess;
use glium::vertex::VertexBuffer;
use glium::glutin::{ WindowBuilder, get_primary_monitor };
use glium::glutin::CursorState::Hide;
use glium::draw_parameters::BackfaceCullingMode::CullClockwise;
use glium_text;
use glium_text::{ TextSystem, FontTexture, TextDisplay };
use time;
use shader::Shaders;
use std::default::Default;
use std::f32::consts::PI;
use imgui::{ ImGui, Ui };
use imgui::glium_renderer::Renderer as ImGuiRenderer;
use utils::*;
use posteffect::*;
use types::*;
pub const FIXED_TIME_STAMP: u64 = 16666667;
pub struct Renderer {
pub display: Display,
pub text_system: TextSystem,
default_font: FontTexture,
imgui: ImGui,
imgui_rend: ImGuiRenderer,
pub post_effect: PostEffect,
pub start_time: f64,
pub shaders: Shaders,
}
impl Renderer {
pub fn new(title:String) -> Renderer {
let display = WindowBuilder::new()
.with_depth_buffer(24)
.with_title(title)
.with_vsync()
.with_fullscreen(get_primary_monitor())
.build_glium()
.unwrap();
let text_system = TextSystem::new(&display);
let font = FontTexture::new(&display, &include_bytes!("./resources/font.otf")[..], 100).unwrap();
let mut imgui = ImGui::init();
let imgui_rend = ImGuiRenderer::init(&mut imgui, &display).unwrap();
let shaders = Shaders::new(&display);
let post_fx = PostEffect::new(&display);
let renderer = Renderer {
display: display,
text_system: text_system,
default_font: font,
imgui: imgui,
imgui_rend: imgui_rend,
post_effect: post_fx,
start_time: time::precise_time_s(),
shaders: shaders,
};
renderer.setup();
renderer
}
pub fn setup(&self) {
let window = self.display.get_window().unwrap();
window.set_cursor_state(Hide).ok();
}
pub fn update_imgui_input(&mut self, pos: (i32, i32), btns: (bool, bool, bool)) {
self.imgui.set_mouse_pos(pos.0 as f32, pos.1 as f32);
self.imgui.set_mouse_down(&[btns.0, btns.1, btns.2, false, false]);
}
pub fn draw<'ui, 'a: 'ui, F: FnMut(&Ui<'ui>)>(&'a mut self,
cam_state: &CamState,
render_items: &Vec<RenderItem>,
text_items: &Vec<TextItem>,
mut f: F) {
let (width, height) = self.display.get_framebuffer_dimensions();
let params = DrawParameters {
depth: Depth {
test: IfLess,
write: true,
.. Default::default()
},
backface_culling: CullClockwise,
.. Default::default()
};
let uniforms = uniform! {
projection_matrix: Renderer::build_persp_proj_mat(60f32, width as f32/height as f32, 0.01f32, 1000f32),
modelview_matrix: Renderer::build_fp_view_matrix(cam_state),
cam_pos: cam_state.cam_pos,
time: (time::precise_time_s() - self.start_time) as f32,
};
let mut target = self.display.draw();
render_post(&self.post_effect,
&self.shaders.post_shaders.get(self.post_effect.current_shader).unwrap(),
&mut target,
|target| {
target.clear_color_and_depth((1.0, 1.0, 1.0, 1.0), 1.0);
for item in render_items.iter().filter(|r| r.active) {
let vertex_buffer = VertexBuffer::new(&self.display, &item.vertices).unwrap();
let per_instance = {
let data = item.instance_transforms.iter().filter(|t| t.active).map(|t| {
Attr {
world_position: t.pos,
world_rotation: t.rot,
world_scale: t.scale
}
}).collect::<Vec<_>>();
VertexBuffer::dynamic(&self.display, &data).unwrap()
};
target.draw(
(&vertex_buffer, per_instance.per_instance().unwrap()),
&NoIndices(PrimitiveType::Patches { vertices_per_patch: 3 }),
&self.shaders.shaders.get(item.shader_name).unwrap(),
&uniforms,
¶ms).unwrap();
}
});
for text_item in text_items.iter().filter(|r| r.active) {
let matrix = [[0.02 * text_item.scale.0, 0.0, 0.0, 0.0],
[0.0, 0.02 * text_item.scale.1 * (width as f32) / (height as f32), 0.0, 0.0],
[0.0, 0.0, 0.02 * text_item.scale.2, 0.0],
[text_item.pos.0, text_item.pos.1, text_item.pos.2, 1.0f32]];
let text = TextDisplay::new(&self.text_system, &self.default_font,
text_item.text.as_str());
glium_text::draw(&text,
&self.text_system,
&mut target,
matrix,
text_item.color);
}
let ui = self.imgui.frame((width, height), (width, height), 0.1);
f(&ui);
self.imgui_rend.render(&mut target, ui).unwrap();
match target.finish() {
Ok(_) => {},
Err(e) => println!("{:?}", e),
};
}
pub fn build_persp_proj_mat(fov:f32,aspect:f32,znear:f32,zfar:f32) -> [[f32; 4]; 4] {
let ymax = znear * (fov * (PI/360.0)).tan();
let ymin = -ymax;
let xmax = ymax * aspect;
let xmin = ymin * aspect;
let width = xmax - xmin;
let height = ymax - ymin;
let depth = zfar - znear;
let q = -(zfar + znear) / depth;
let qn = -2.0 * (zfar * znear) / depth;
let w = 2.0 * znear / width;
let h = 2.0 * znear / height;
[[w, 0.0f32, 0.0f32, 0.0f32],
[0.0f32, h, 0.0f32, 0.0f32],
[0.0f32, 0.0f32, q, -1.0f32],
[0.0f32, 0.0f32, qn, 0.0f32]]
}
pub fn build_fp_view_matrix(cam_state: &CamState) -> [[f32; 4]; 4] {
let (sin_yaw, cos_yaw, sin_pitch, cos_pitch) = (
cam_state.cam_rot.1.sin(),
cam_state.cam_rot.1.cos(),
cam_state.cam_rot.0.sin(),
cam_state.cam_rot.0.cos());
let xaxis = [cos_yaw, 0.0, -sin_yaw];
let yaxis = [sin_yaw * sin_pitch, cos_pitch, cos_yaw * sin_pitch];
let zaxis = [sin_yaw * cos_pitch, -sin_pitch, cos_pitch * cos_yaw];
let cam_arr = [cam_state.cam_pos.0, cam_state.cam_pos.1, cam_state.cam_pos.2];
[[ xaxis[0], yaxis[0], zaxis[0], 0.0],
[ xaxis[1], yaxis[1], zaxis[1], 0.0],
[ xaxis[2], yaxis[2], zaxis[2], 0.0],
[ -dotp(&xaxis, &cam_arr), -dotp(&yaxis, &cam_arr), -dotp(&zaxis, &cam_arr), 1.0f32]]
}
}