fix: apply correct model, view, and projection matrices for camera motion each frame
This commit is contained in:
32
src/main.rs
32
src/main.rs
@@ -308,28 +308,34 @@ fn main() {
|
||||
|
||||
// == // Issue the necessary gl:: commands to draw your scene here
|
||||
simple_shader.activate();
|
||||
// build and send the transform matrix (start from identity)
|
||||
let mut transform: glm::Mat4 = glm::identity();
|
||||
// build Model matrix (start from identity)
|
||||
let mut model: glm::Mat4 = glm::identity();
|
||||
// cycle through each transform parameter (a,b,c,d,e,f) over time
|
||||
let cycle_duration = 2.0;
|
||||
let idx = ((elapsed / cycle_duration).floor() as usize) % 6;
|
||||
let v = elapsed.sin();
|
||||
match idx {
|
||||
0 => transform[(0,0)] = v, // a
|
||||
1 => transform[(0,1)] = v, // b
|
||||
2 => transform[(0,3)] = v, // c
|
||||
3 => transform[(1,0)] = v, // d
|
||||
4 => transform[(1,1)] = v, // e
|
||||
5 => transform[(1,3)] = v, // f
|
||||
0 => model[(0,0)] = v, // a
|
||||
1 => model[(0,1)] = v, // b
|
||||
2 => model[(0,3)] = v, // c
|
||||
3 => model[(1,0)] = v, // d
|
||||
4 => model[(1,1)] = v, // e
|
||||
5 => model[(1,3)] = v, // f
|
||||
_ => {}
|
||||
}
|
||||
// move scene into viewable negative z range
|
||||
let translation: glm::Mat4 = glm::translation(&glm::vec3(0.0, 0.0, -5.0));
|
||||
transform = translation * transform;
|
||||
// apply perspective projection: (aspect, fovy radians, near, far)
|
||||
// model translation to initial view distance
|
||||
let translation_model: glm::Mat4 = glm::translation(&glm::vec3(0.0, 0.0, -5.0));
|
||||
let model = translation_model * model;
|
||||
// build View matrix: translate and rotate world relative to camera
|
||||
let view_translation: glm::Mat4 = glm::translation(&(-cam_pos));
|
||||
let view_rot_y: glm::Mat4 = glm::rotation(-cam_yaw, &glm::vec3(0.0, 1.0, 0.0));
|
||||
let view_rot_x: glm::Mat4 = glm::rotation(-cam_pitch, &glm::vec3(1.0, 0.0, 0.0));
|
||||
let view: glm::Mat4 = view_rot_x * view_rot_y * view_translation;
|
||||
// build Projection matrix
|
||||
let projection: glm::Mat4 =
|
||||
glm::perspective(window_aspect_ratio, std::f32::consts::PI / 4.0, 1.0, 100.0);
|
||||
transform = projection * transform;
|
||||
// final transform: Projection * View * Model
|
||||
let transform: glm::Mat4 = projection * view * model;
|
||||
gl::UniformMatrix4fv(transform_loc, 1, gl::FALSE, transform.as_ptr());
|
||||
gl::BindVertexArray(my_vao);
|
||||
gl::DrawElements(
|
||||
|
||||
Reference in New Issue
Block a user