-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy path_6_multiple_lights.rs
331 lines (286 loc) · 15.3 KB
/
_6_multiple_lights.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
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]
extern crate glfw;
use self::glfw::Context;
extern crate gl;
use self::gl::types::*;
use std::ptr;
use std::mem;
use std::os::raw::c_void;
use std::ffi::CStr;
use common::{process_events, processInput, loadTexture};
use shader::Shader;
use camera::Camera;
use cgmath::{Matrix4, Vector3, vec3, Point3, Deg, perspective};
use cgmath::prelude::*;
// settings
const SCR_WIDTH: u32 = 800;
const SCR_HEIGHT: u32 = 600;
pub fn main_2_6() {
let mut camera = Camera {
Position: Point3::new(0.0, 0.0, 3.0),
..Camera::default()
};
let mut firstMouse = true;
let mut lastX: f32 = SCR_WIDTH as f32 / 2.0;
let mut lastY: f32 = SCR_HEIGHT as f32 / 2.0;
// timing
let mut deltaTime: f32; // time between current frame and last frame
let mut lastFrame: f32 = 0.0;
// glfw: initialize and configure
// ------------------------------
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
glfw.window_hint(glfw::WindowHint::ContextVersion(3, 3));
glfw.window_hint(glfw::WindowHint::OpenGlProfile(glfw::OpenGlProfileHint::Core));
#[cfg(target_os = "macos")]
glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true));
// glfw window creation
// --------------------
let (mut window, events) = glfw.create_window(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", glfw::WindowMode::Windowed)
.expect("Failed to create GLFW window");
window.make_current();
window.set_framebuffer_size_polling(true);
window.set_cursor_pos_polling(true);
window.set_scroll_polling(true);
// tell GLFW to capture our mouse
window.set_cursor_mode(glfw::CursorMode::Disabled);
// gl: load all OpenGL function pointers
// ---------------------------------------
gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);
let (lightingShader, lampShader, VBO, cubeVAO, lightVAO, diffuseMap, specularMap, cubePositions, pointLightPositions) = unsafe {
// configure global opengl state
// -----------------------------
gl::Enable(gl::DEPTH_TEST);
// build and compile our shader program
// ------------------------------------
let lightingShader = Shader::new(
"src/_2_lighting/shaders/6.multiple_lights.vs",
"src/_2_lighting/shaders/6.multiple_lights.fs");
let lampShader = Shader::new(
"src/_2_lighting/shaders/6.lamp.vs",
"src/_2_lighting/shaders/6.lamp.fs");
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
let vertices: [f32; 288] = [
// positions // normals // texture coords
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0,
0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 0.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0,
0.5, 0.5, -0.5, 0.0,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0.0, 0.0,
-0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0,
0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0
];
// positions all containers
let cubePositions: [Vector3<f32>; 10] = [
vec3( 0.0, 0.0, 0.0),
vec3( 2.0, 5.0, -15.0),
vec3(-1.5, -2.2, -2.5),
vec3(-3.8, -2.0, -12.3),
vec3( 2.4, -0.4, -3.5),
vec3(-1.7, 3.0, -7.5),
vec3( 1.3, -2.0, -2.5),
vec3( 1.5, 2.0, -2.5),
vec3( 1.5, 0.2, -1.5),
vec3(-1.3, 1.0, -1.5)
];
// positions of the point lights
let pointLightPositions: [Vector3<f32>; 4] = [
vec3( 0.7, 0.2, 2.0),
vec3( 2.3, -3.3, -4.0),
vec3(-4.0, 2.0, -12.0),
vec3( 0.0, 0.0, -3.0)
];
// first, configure the cube's VAO (and VBO)
let (mut VBO, mut cubeVAO) = (0, 0);
gl::GenVertexArrays(1, &mut cubeVAO);
gl::GenBuffers(1, &mut VBO);
gl::BindBuffer(gl::ARRAY_BUFFER, VBO);
gl::BufferData(gl::ARRAY_BUFFER,
(vertices.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
&vertices[0] as *const f32 as *const c_void,
gl::STATIC_DRAW);
gl::BindVertexArray(cubeVAO);
let stride = 8 * mem::size_of::<GLfloat>() as GLsizei;
gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, stride, ptr::null());
gl::EnableVertexAttribArray(0);
gl::VertexAttribPointer(1, 3, gl::FLOAT, gl::FALSE, stride, (3 * mem::size_of::<GLfloat>()) as *const c_void);
gl::EnableVertexAttribArray(1);
gl::VertexAttribPointer(2, 2, gl::FLOAT, gl::FALSE, stride, (6 * mem::size_of::<GLfloat>()) as *const c_void);
gl::EnableVertexAttribArray(2);
// second, configure the light's VAO (VBO stays the same; the vertices are the same for the light object which is also a 3D cube)
let mut lightVAO = 0;
gl::GenVertexArrays(1, &mut lightVAO);
gl::BindVertexArray(lightVAO);
gl::BindBuffer(gl::ARRAY_BUFFER, VBO);
// note that we update the lamp's position attribute's stride to reflect the updated buffer data
gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, stride, ptr::null());
gl::EnableVertexAttribArray(0);
// load textures (we now use a utility function to keep the code more organized)
// -----------------------------------------------------------------------------
let diffuseMap = loadTexture("resources/textures/container2.png");
let specularMap = loadTexture("resources/textures/container2_specular.png");
// shader configuration
// --------------------
lightingShader.useProgram();
lightingShader.setInt(c_str!("material.diffuse"), 0);
lightingShader.setInt(c_str!("material.specular"), 1);
(lightingShader, lampShader, VBO, cubeVAO, lightVAO, diffuseMap, specularMap, cubePositions, pointLightPositions)
};
// render loop
// -----------
while !window.should_close() {
// per-frame time logic
// --------------------
let currentFrame = glfw.get_time() as f32;
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// events
// -----
process_events(&events, &mut firstMouse, &mut lastX, &mut lastY, &mut camera);
// input
// -----
processInput(&mut window, deltaTime, &mut camera);
// render
// ------
unsafe {
gl::ClearColor(0.1, 0.1, 0.1, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
// be sure to activate shader when setting uniforms/drawing objects
lightingShader.useProgram();
lightingShader.setVector3(c_str!("viewPos"), &camera.Position.to_vec());
lightingShader.setFloat(c_str!("material.shininess"), 32.0);
/*
Here we set all the uniforms for the 5/6 types of lights we have. We have to set them manually and index
the proper PointLight struct in the array to set each uniform variable. This can be done more code-friendly
by defining light types as classes and set their values in there, or by using a more efficient uniform approach
by using 'Uniform buffer objects', but that is something we'll discuss in the 'Advanced GLSL' tutorial.
*/
// directional light
lightingShader.setVec3(c_str!("dirLight.direction"), -0.2, -1.0, -0.3);
lightingShader.setVec3(c_str!("dirLight.ambient"), 0.05, 0.05, 0.05);
lightingShader.setVec3(c_str!("dirLight.diffuse"), 0.4, 0.4, 0.4);
lightingShader.setVec3(c_str!("dirLight.specular"), 0.5, 0.5, 0.5);
// point light 1
lightingShader.setVector3(c_str!("pointLights[0].position"), &pointLightPositions[0]);
lightingShader.setVec3(c_str!("pointLights[0].ambient"), 0.05, 0.05, 0.05);
lightingShader.setVec3(c_str!("pointLights[0].diffuse"), 0.8, 0.8, 0.8);
lightingShader.setVec3(c_str!("pointLights[0].specular"), 1.0, 1.0, 1.0);
lightingShader.setFloat(c_str!("pointLights[0].constant"), 1.0);
lightingShader.setFloat(c_str!("pointLights[0].linear"), 0.09);
lightingShader.setFloat(c_str!("pointLights[0].quadratic"), 0.032);
// point light 2
lightingShader.setVector3(c_str!("pointLights[1].position"), &pointLightPositions[1]);
lightingShader.setVec3(c_str!("pointLights[1].ambient"), 0.05, 0.05, 0.05);
lightingShader.setVec3(c_str!("pointLights[1].diffuse"), 0.8, 0.8, 0.8);
lightingShader.setVec3(c_str!("pointLights[1].specular"), 1.0, 1.0, 1.0);
lightingShader.setFloat(c_str!("pointLights[1].constant"), 1.0);
lightingShader.setFloat(c_str!("pointLights[1].linear"), 0.09);
lightingShader.setFloat(c_str!("pointLights[1].quadratic"), 0.032);
// point light 3
lightingShader.setVector3(c_str!("pointLights[2].position"), &pointLightPositions[2]);
lightingShader.setVec3(c_str!("pointLights[2].ambient"), 0.05, 0.05, 0.05);
lightingShader.setVec3(c_str!("pointLights[2].diffuse"), 0.8, 0.8, 0.8);
lightingShader.setVec3(c_str!("pointLights[2].specular"), 1.0, 1.0, 1.0);
lightingShader.setFloat(c_str!("pointLights[2].constant"), 1.0);
lightingShader.setFloat(c_str!("pointLights[2].linear"), 0.09);
lightingShader.setFloat(c_str!("pointLights[2].quadratic"), 0.032);
// point light 4
lightingShader.setVector3(c_str!("pointLights[3].position"), &pointLightPositions[3]);
lightingShader.setVec3(c_str!("pointLights[3].ambient"), 0.05, 0.05, 0.05);
lightingShader.setVec3(c_str!("pointLights[3].diffuse"), 0.8, 0.8, 0.8);
lightingShader.setVec3(c_str!("pointLights[3].specular"), 1.0, 1.0, 1.0);
lightingShader.setFloat(c_str!("pointLights[3].constant"), 1.0);
lightingShader.setFloat(c_str!("pointLights[3].linear"), 0.09);
lightingShader.setFloat(c_str!("pointLights[3].quadratic"), 0.032);
// spotLight
lightingShader.setVector3(c_str!("spotLight.position"), &camera.Position.to_vec());
lightingShader.setVector3(c_str!("spotLight.direction"), &camera.Front);
lightingShader.setVec3(c_str!("spotLight.ambient"), 0.0, 0.0, 0.0);
lightingShader.setVec3(c_str!("spotLight.diffuse"), 1.0, 1.0, 1.0);
lightingShader.setVec3(c_str!("spotLight.specular"), 1.0, 1.0, 1.0);
lightingShader.setFloat(c_str!("spotLight.constant"), 1.0);
lightingShader.setFloat(c_str!("spotLight.linear"), 0.09);
lightingShader.setFloat(c_str!("spotLight.quadratic"), 0.032);
lightingShader.setFloat(c_str!("spotLight.cutOff"), 12.5f32.to_radians().cos());
lightingShader.setFloat(c_str!("spotLight.outerCutOff"), 15.0f32.to_radians().cos());
// view/projection transformations
let projection: Matrix4<f32> = perspective(Deg(camera.Zoom), SCR_WIDTH as f32 / SCR_HEIGHT as f32, 0.1, 100.0);
let view = camera.GetViewMatrix();
lightingShader.setMat4(c_str!("projection"), &projection);
lightingShader.setMat4(c_str!("view"), &view);
// world transformation
let mut model = Matrix4::<f32>::identity();
lightingShader.setMat4(c_str!("model"), &model);
// bind diffuse map
gl::ActiveTexture(gl::TEXTURE0);
gl::BindTexture(gl::TEXTURE_2D, diffuseMap);
// bind specular map
gl::ActiveTexture(gl::TEXTURE1);
gl::BindTexture(gl::TEXTURE_2D, specularMap);
// render containers
gl::BindVertexArray(cubeVAO);
for (i, position) in cubePositions.iter().enumerate() {
// calculate the model matrix for each object and pass it to shader before drawing
let mut model: Matrix4<f32> = Matrix4::from_translation(*position);
let angle = 20.0 * i as f32;
// don't forget to normalize the axis!
model = model * Matrix4::from_axis_angle(vec3(1.0, 0.3, 0.5).normalize(), Deg(angle));
lightingShader.setMat4(c_str!("model"), &model);
gl::DrawArrays(gl::TRIANGLES, 0, 36);
}
// also draw the lamp object(s)
lampShader.useProgram();
lampShader.setMat4(c_str!("projection"), &projection);
lampShader.setMat4(c_str!("view"), &view);
// we now draw as many light bulbs as we have point lights.
gl::BindVertexArray(lightVAO);
for position in &pointLightPositions {
model = Matrix4::from_translation(*position);
model = model * Matrix4::from_scale(0.2); // Make it a smaller cube
lampShader.setMat4(c_str!("model"), &model);
gl::DrawArrays(gl::TRIANGLES, 0, 36);
}
}
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
window.swap_buffers();
glfw.poll_events();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
unsafe {
gl::DeleteVertexArrays(1, &cubeVAO);
gl::DeleteVertexArrays(1, &lightVAO);
gl::DeleteBuffers(1, &VBO);
}
}