-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
186 lines (171 loc) · 6.96 KB
/
main.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
use glfw::{Action, Key, Modifiers, WindowEvent};
use gloam::{
app::init_default_opengl_3_3,
camera::{Camera, FreeCamera},
context::ClearMask,
error::Result,
uniform::Uniform,
};
use nalgebra_glm as glm;
use std::f32::consts::PI;
mod enclosure;
use enclosure::init_enclosure;
mod light_source;
use light_source::init_light_source;
const COLOR_INCREMENT: f32 = 0.05;
const POSITION_INCREMENT: f32 = 0.1;
fn main() -> Result<()> {
env_logger::init();
let (mut window, mut ctx) = init_default_opengl_3_3("HelloPhongIllumination")?;
window.set_key_polling(true);
window.set_framebuffer_size_polling(true);
ctx.try_enable_depth_test(None)?;
let camera = FreeCamera::new(
glm::vec3(0.0, 0.0, 2.0),
glm::vec3(0.0, 0.0, 0.0),
glm::vec3(0.0, 1.0, 0.0),
0.0,
0.0,
);
let projection_matrix = glm::perspective(window.get_aspect_ratio(), PI / 4.0, 0.1, 100.0);
let mut light_translation_vector = glm::vec3(0.0, 0.0, -20.0);
let mut light_color = glm::vec3(1.0, 1.0, 1.0);
let ambient_light_intensity = 0.2;
let specular_light_intensity = 0.5;
let mut light_source = init_light_source(&mut ctx, light_color)?;
let light_source_base_model_matrix = glm::scale(&glm::identity(), &glm::vec3(0.1, 0.1, 0.1));
light_source.set_uniform_on_cpu(Uniform::new_mat4fv(
"projectionMatrix",
projection_matrix,
false,
))?;
light_source.set_uniform_on_cpu(Uniform::new_mat4fv(
"viewMatrix",
camera.get_view_matrix(),
false,
))?;
let enclosure_shininess = 128.0;
let enclosure_position = glm::vec3(0.0, 0.0, 0.0);
let mut enclosure = init_enclosure(&mut ctx, glm::vec3(0.2, 0.2, 0.2))?;
let enclosure_model_matrix = glm::translate(
&glm::scale(&glm::identity(), &glm::vec3(3.0, 1.5, 7.0)),
&enclosure_position,
);
let enclosure_normal_matrix = enclosure_model_matrix
.fixed_view::<3, 3>(0, 0)
.try_inverse()
.unwrap()
.transpose();
enclosure.set_uniform_on_cpu(Uniform::new_mat4fv(
"modelMatrix",
enclosure_model_matrix,
false,
))?;
enclosure.set_uniform_on_cpu(Uniform::new_mat3fv(
"normalMatrix",
enclosure_normal_matrix,
false,
))?;
enclosure.set_uniform_on_cpu(Uniform::new_1f(
"ambientLightIntensity",
ambient_light_intensity,
))?;
enclosure.set_uniform_on_cpu(Uniform::new_1f(
"specularLightIntensity",
specular_light_intensity,
))?;
enclosure.set_uniform_on_cpu(Uniform::new_1f(
"shininess",
enclosure_shininess,
))?;
enclosure.set_uniform_on_cpu(Uniform::new_mat4fv(
"projectionMatrix",
projection_matrix,
false,
))?;
enclosure.set_uniform_on_cpu(Uniform::new_mat4fv(
"viewMatrix",
camera.get_view_matrix(),
false,
))?;
enclosure.set_uniform_on_cpu(Uniform::new_3f("cameraPosition", camera.position.clone()))?;
window.run_event_loop(|win, event| {
ctx.clear(&[ClearMask::Color(0.0, 0.0, 0.0, 0.0), ClearMask::DepthBuffer]);
match event {
Some(ev) => match ev {
WindowEvent::FramebufferSize(width, height) => {
ctx.viewport(0, 0, width, height);
}
WindowEvent::Key(key, scan_code, action, modifier) => {
match (key, scan_code, action, modifier) {
(Key::W, _, Action::Press, Modifiers::Super) => win.set_should_close(true),
(Key::R, _, Action::Press | Action::Repeat, Modifiers::Shift) => {
light_color.x = (light_color.x - COLOR_INCREMENT).max(0.0);
}
(Key::R, _, Action::Press | Action::Repeat, _) => {
light_color.x = (light_color.x + COLOR_INCREMENT).min(1.0);
}
(Key::G, _, Action::Press | Action::Repeat, Modifiers::Shift) => {
light_color.y = (light_color.y - COLOR_INCREMENT).max(0.0);
}
(Key::G, _, Action::Press | Action::Repeat, _) => {
light_color.y = (light_color.y + COLOR_INCREMENT).min(1.0);
}
(Key::B, _, Action::Press | Action::Repeat, Modifiers::Shift) => {
light_color.z = (light_color.z - COLOR_INCREMENT).max(0.0);
}
(Key::B, _, Action::Press | Action::Repeat, _) => {
light_color.z = (light_color.z + COLOR_INCREMENT).min(1.0);
}
(Key::Up, _, Action::Press | Action::Repeat, Modifiers::Shift) => {
light_translation_vector.y += POSITION_INCREMENT;
}
(Key::Up, _, Action::Press | Action::Repeat, _) => {
light_translation_vector.z -= POSITION_INCREMENT;
}
(Key::Down, _, Action::Press | Action::Repeat, Modifiers::Shift) => {
light_translation_vector.y -= POSITION_INCREMENT;
}
(Key::Down, _, Action::Press | Action::Repeat, _) => {
light_translation_vector.z += POSITION_INCREMENT;
}
(Key::Right, _, Action::Press | Action::Repeat, _) => {
light_translation_vector.x += POSITION_INCREMENT;
}
(Key::Left, _, Action::Press | Action::Repeat, _) => {
light_translation_vector.x -= POSITION_INCREMENT;
}
_ => (),
}
}
_ => (),
},
_ => (),
}
let light_model_matrix =
glm::translate(&light_source_base_model_matrix, &light_translation_vector);
light_source.try_set_uniforms_and_render(
&mut ctx,
vec![
Uniform::new_mat4fv("modelMatrix", light_model_matrix, false),
Uniform::new_1f("red", light_color.x),
Uniform::new_1f("green", light_color.y),
Uniform::new_1f("blue", light_color.z),
],
)?;
light_source.deactivate(&mut ctx);
let light_position = light_model_matrix.fixed_view::<3, 1>(0, 3).into_owned();
log::info!("light source position: {light_position:?}");
log::info!("light source color: {light_color:?}");
enclosure.try_set_uniforms_and_render(
&mut ctx,
vec![
Uniform::new_3f("lightPosition", light_position),
Uniform::new_3f("lightColor", light_color),
],
)?;
enclosure.deactivate(&mut ctx);
win.draw();
Ok(())
})
}