-
Notifications
You must be signed in to change notification settings - Fork 398
/
deferred.rs
384 lines (327 loc) · 14.7 KB
/
deferred.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#[macro_use]
extern crate glium;
use glium::{index::PrimitiveType, VertexBuffer, IndexBuffer, Program};
use cgmath::SquareMatrix;
use glium::{Surface, Display};
use glutin::surface::WindowSurface;
use ouroboros::self_referencing;
use support::{ApplicationContext, State};
use std::io::Cursor;
mod support;
pub struct Dt {
depthtexture: glium::texture::DepthTexture2d,
textures: [glium::texture::Texture2d; 4],
light_texture: glium::texture::Texture2d,
}
#[self_referencing]
struct Data {
dt: Dt,
#[borrows(dt)]
#[covariant]
buffs: (glium::framebuffer::MultiOutputFrameBuffer<'this>, glium::framebuffer::SimpleFrameBuffer<'this>, &'this Dt),
}
#[derive(Copy, Clone)]
struct FloorVertex {
position: [f32; 4],
normal: [f32; 4],
texcoord: [f32; 2]
}
implement_vertex!(FloorVertex, position, normal, texcoord);
#[derive(Copy, Clone)]
struct QuadVertex {
position: [f32; 4],
texcoord: [f32; 2]
}
implement_vertex!(QuadVertex, position, texcoord);
struct Light {
position: [f32; 4],
color: [f32; 3],
attenuation: [f32; 3],
radius: f32
}
struct Application {
pub opengl_texture: glium::Texture2d,
pub floor_vertex_buffer: VertexBuffer<FloorVertex>,
pub floor_index_buffer: IndexBuffer<u16>,
pub quad_vertex_buffer: VertexBuffer<QuadVertex>,
pub quad_index_buffer: IndexBuffer<u16>,
pub prepass_program: Program,
pub lighting_program: Program,
pub composition_program: Program,
pub tenants: Data,
pub lights: [Light; 4],
}
impl ApplicationContext for Application {
const WINDOW_TITLE:&'static str = "Glium deferred example";
fn new(display: &Display<WindowSurface>) -> Self {
let image = image::load(Cursor::new(&include_bytes!("../tests/fixture/opengl.png")),
image::ImageFormat::Png).unwrap().to_rgba8();
let image_dimensions = image.dimensions();
let image = glium::texture::RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions);
let opengl_texture = glium::texture::Texture2d::new(display, image).unwrap();
let floor_vertex_buffer = {
glium::VertexBuffer::new(display,
&[
FloorVertex { position: [-1.0, 0.0, -1.0, 1.0], normal: [0.0, 1.0, 0.0, 1.0], texcoord: [1.0, 0.0] },
FloorVertex { position: [1.0, 0.0, -1.0, 1.0], normal: [0.0, 1.0, 0.0, 1.0], texcoord: [0.0, 0.0] },
FloorVertex { position: [1.0, 0.0, 1.0, 1.0], normal: [0.0, 1.0, 0.0, 1.0], texcoord: [0.0, 1.0] },
FloorVertex { position: [-1.0, 0.0, 1.0, 1.0], normal: [0.0, 1.0, 0.0, 1.0], texcoord: [1.0, 1.0] },
]
).unwrap()
};
let floor_index_buffer = glium::IndexBuffer::new(display, PrimitiveType::TrianglesList,
&[0u16, 1, 2, 0, 2, 3]).unwrap();
let quad_vertex_buffer = {
glium::VertexBuffer::new(display,
&[
QuadVertex { position: [0.0, 0.0, 0.0, 1.0], texcoord: [0.0, 0.0] },
QuadVertex { position: [800.0, 0.0, 0.0, 1.0], texcoord: [1.0, 0.0] },
QuadVertex { position: [800.0, 500.0, 0.0, 1.0], texcoord: [1.0, 1.0] },
QuadVertex { position: [0.0, 500.0, 0.0, 1.0], texcoord: [0.0, 1.0] },
]
).unwrap()
};
let quad_index_buffer = glium::IndexBuffer::new(display, PrimitiveType::TrianglesList,
&[0u16, 1, 2, 0, 2, 3]).unwrap();
// compiling shaders and linking them together
let prepass_program = glium::Program::from_source(display,
// vertex shader
"
#version 140
uniform mat4 perspective_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
in vec4 position;
in vec4 normal;
in vec2 texcoord;
smooth out vec4 frag_position;
smooth out vec4 frag_normal;
smooth out vec2 frag_texcoord;
void main() {
frag_position = model_matrix * position;
frag_normal = model_matrix * normal;
frag_texcoord = texcoord;
gl_Position = perspective_matrix * view_matrix * frag_position;
}
",
// fragment shader
"
#version 140
uniform sampler2D tex;
smooth in vec4 frag_position;
smooth in vec4 frag_normal;
smooth in vec2 frag_texcoord;
out vec4 output1;
out vec4 output2;
out vec4 output3;
out vec4 output4;
void main() {
output1 = vec4(frag_position);
output2 = vec4(frag_normal);
output3 = texture(tex, frag_texcoord);
output4 = vec4(1.0, 0.0, 1.0, 1.0);
}
",
// geometry shader
None)
.unwrap();
let lighting_program = glium::Program::from_source(display,
// vertex shader
"
#version 140
uniform mat4 matrix;
in vec4 position;
in vec2 texcoord;
smooth out vec2 frag_texcoord;
void main() {
gl_Position = matrix * position;
frag_texcoord = texcoord;
}
",
// fragment shader
"
#version 140
uniform sampler2D position_texture;
uniform sampler2D normal_texture;
uniform vec4 light_position;
uniform vec3 light_color;
uniform vec3 light_attenuation;
uniform float light_radius;
smooth in vec2 frag_texcoord;
out vec4 frag_output;
void main() {
vec4 position = texture(position_texture, frag_texcoord);
vec4 normal = texture(normal_texture, frag_texcoord);
vec3 light_vector = light_position.xyz - position.xyz;
float light_distance = abs(length(light_vector));
vec3 normal_vector = normalize(normal.xyz);
float diffuse = max(dot(normal_vector, light_vector), 0.0);
if (diffuse > 0.0) {
float attenuation_factor = 1.0 / (
light_attenuation.x +
(light_attenuation.y * light_distance) +
(light_attenuation.z * light_distance * light_distance)
);
attenuation_factor *= (1.0 - pow((light_distance / light_radius), 2.0));
attenuation_factor = max(attenuation_factor, 0.0);
diffuse *= attenuation_factor;
}
frag_output = vec4(light_color * diffuse, 1.0);
}
",
// geometry shader
None)
.unwrap();
// compiling shaders and linking them together
let composition_program = glium::Program::from_source(display,
// vertex shader
"
#version 140
uniform mat4 matrix;
in vec4 position;
in vec2 texcoord;
smooth out vec2 frag_texcoord;
void main() {
frag_texcoord = texcoord;
gl_Position = matrix * position;
}
",
// fragment shader
"
#version 140
uniform sampler2D decal_texture;
uniform sampler2D lighting_texture;
smooth in vec2 frag_texcoord;
out vec4 frag_output;
void main() {
vec4 lighting_value = texture(lighting_texture, frag_texcoord);
frag_output = vec4(texture(decal_texture, frag_texcoord).rgb * lighting_value.rgb, 1.0);
}
",
// geometry shader
None)
.unwrap();
let texture1 = glium::texture::Texture2d::empty_with_format(display, glium::texture::UncompressedFloatFormat::F32F32F32F32, glium::texture::MipmapsOption::NoMipmap, 800, 500).unwrap();
let texture2 = glium::texture::Texture2d::empty_with_format(display, glium::texture::UncompressedFloatFormat::F32F32F32F32, glium::texture::MipmapsOption::NoMipmap, 800, 500).unwrap();
let texture3 = glium::texture::Texture2d::empty_with_format(display, glium::texture::UncompressedFloatFormat::F32F32F32F32, glium::texture::MipmapsOption::NoMipmap, 800, 500).unwrap();
let texture4 = glium::texture::Texture2d::empty_with_format(display, glium::texture::UncompressedFloatFormat::F32F32F32F32, glium::texture::MipmapsOption::NoMipmap, 800, 500).unwrap();
let depthtexture = glium::texture::DepthTexture2d::empty_with_format(display, glium::texture::DepthFormat::F32, glium::texture::MipmapsOption::NoMipmap, 800, 500).unwrap();
let light_texture = glium::texture::Texture2d::empty_with_format(display, glium::texture::UncompressedFloatFormat::F32F32F32F32, glium::texture::MipmapsOption::NoMipmap, 800, 500).unwrap();
let tenants = DataBuilder {
dt: Dt {
depthtexture,
textures: [texture1, texture2, texture3, texture4],
light_texture,
},
buffs_builder: |dt| {
let output = [("output1", &dt.textures[0]), ("output2", &dt.textures[1]), ("output3", &dt.textures[2]), ("output4", &dt.textures[3])];
let framebuffer = glium::framebuffer::MultiOutputFrameBuffer::with_depth_buffer(display, output.iter().cloned(), &dt.depthtexture).unwrap();
let light_buffer = glium::framebuffer::SimpleFrameBuffer::with_depth_buffer(display, &dt.light_texture, &dt.depthtexture).unwrap();
(framebuffer, light_buffer, dt)
}
}.build();
let lights = [
Light {
position: [1.0, 1.0, 1.0, 1.0],
attenuation: [0.8, 0.00125, 0.0000001],
color: [1.0, 0.0, 0.0],
radius: 1.5
},
Light {
position: [0.0, 1.0, 0.0, 1.0],
attenuation: [0.8, 0.00125, 0.0000001],
color: [0.0, 1.0, 0.0],
radius: 1.5
},
Light {
position: [0.0, 1.0, 1.0, 1.0],
attenuation: [0.8, 0.00125, 0.0000001],
color: [0.0, 0.0, 1.0],
radius: 1.5
},
Light {
position: [1.0, 1.0, 0.0, 1.0],
attenuation: [0.8, 0.00125, 0.0000001],
color: [1.0, 1.0, 0.0],
radius: 1.5
}
];
Self {
opengl_texture,
floor_vertex_buffer,
floor_index_buffer,
quad_vertex_buffer,
quad_index_buffer,
prepass_program,
lighting_program,
composition_program,
tenants,
lights,
}
}
fn draw_frame(&mut self, display: &Display<WindowSurface>) {
let ortho_matrix: cgmath::Matrix4<f32> = cgmath::ortho(0.0, 800.0, 0.0, 500.0, -1.0, 1.0);
let perspective_matrix: cgmath::Matrix4<f32> = cgmath::perspective(cgmath::Deg(45.0), 1.333, 0.0001, 100.0);
let view_eye: cgmath::Point3<f32> = cgmath::Point3::new(0.0, 2.0, -2.0);
let view_center: cgmath::Point3<f32> = cgmath::Point3::new(0.0, 0.0, 0.0);
let view_up: cgmath::Vector3<f32> = cgmath::Vector3::new(0.0, 1.0, 0.0);
let view_matrix: cgmath::Matrix4<f32> = cgmath::Matrix4::look_at_rh(view_eye, view_center, view_up);
let model_matrix: cgmath::Matrix4<f32> = cgmath::Matrix4::identity();
self.tenants.with_mut(|fields| {
let framebuffer = &mut fields.buffs.0;
let light_buffer = &mut fields.buffs.1;
let dt = fields.dt;
// prepass
let uniforms = uniform! {
perspective_matrix: Into::<[[f32; 4]; 4]>::into(perspective_matrix),
view_matrix: Into::<[[f32; 4]; 4]>::into(view_matrix),
model_matrix: Into::<[[f32; 4]; 4]>::into(model_matrix),
tex: &self.opengl_texture
};
framebuffer.clear_color(0.0, 0.0, 0.0, 0.0);
framebuffer.draw(&self.floor_vertex_buffer, &self.floor_index_buffer, &self.prepass_program, &uniforms, &Default::default()).unwrap();
// lighting
let draw_params = glium::DrawParameters {
//depth_function: glium::DepthFunction::IfLessOrEqual,
blend: glium::Blend {
color: glium::BlendingFunction::Addition {
source: glium::LinearBlendingFactor::One,
destination: glium::LinearBlendingFactor::One
},
alpha: glium::BlendingFunction::Addition {
source: glium::LinearBlendingFactor::One,
destination: glium::LinearBlendingFactor::One
},
constant_value: (1.0, 1.0, 1.0, 1.0)
},
.. Default::default()
};
light_buffer.clear_color(0.0, 0.0, 0.0, 0.0);
for light in self.lights.iter() {
let uniforms = uniform! {
matrix: Into::<[[f32; 4]; 4]>::into(ortho_matrix),
position_texture: &dt.textures[0],
normal_texture: &dt.textures[1],
light_position: light.position,
light_attenuation: light.attenuation,
light_color: light.color,
light_radius: light.radius
};
light_buffer.draw(&self.quad_vertex_buffer, &self.quad_index_buffer, &self.lighting_program, &uniforms, &draw_params).unwrap();
}
// composition
let uniforms = uniform! {
matrix: Into::<[[f32; 4]; 4]>::into(ortho_matrix),
decal_texture: &dt.textures[2],
lighting_texture: &dt.light_texture
};
let mut target = display.draw();
target.clear_color(0.0, 0.0, 0.0, 0.0);
target.draw(&self.quad_vertex_buffer, &self.quad_index_buffer, &self.composition_program, &uniforms, &Default::default()).unwrap();
target.finish().unwrap();
});
}
}
fn main() {
State::<Application>::run_loop();
}