feat: animate helicopter rotors spinning and draw scene via scene graph
This commit is contained in:
55
src/main.rs
55
src/main.rs
@@ -244,7 +244,7 @@ fn main() {
|
||||
let mut heli_root = SceneNode::new();
|
||||
let body_node = SceneNode::from_vao(heli_body_vao, helicopter.body.index_count);
|
||||
let door_node = SceneNode::from_vao(heli_door_vao, helicopter.door.index_count);
|
||||
let main_rotor_node = SceneNode::from_vao(heli_main_rotor_vao, helicopter.main_rotor.index_count);
|
||||
let mut main_rotor_node = SceneNode::from_vao(heli_main_rotor_vao, helicopter.main_rotor.index_count);
|
||||
let mut tail_rotor_node = SceneNode::from_vao(heli_tail_rotor_vao, helicopter.tail_rotor.index_count);
|
||||
// Set reference point for tail rotor (pivot around its hub)
|
||||
let mut tail_rotor_pin = tail_rotor_node.as_mut();
|
||||
@@ -438,50 +438,17 @@ fn main() {
|
||||
let projection: glm::Mat4 =
|
||||
glm::perspective(window_aspect_ratio, std::f32::consts::PI / 4.0, 1.0, 1000.0);
|
||||
|
||||
// Draw the lunar terrain
|
||||
let model_terrain: glm::Mat4 = glm::identity();
|
||||
let transform_terrain: glm::Mat4 = projection * view * model_terrain;
|
||||
gl::UniformMatrix4fv(transform_loc, 1, gl::FALSE, transform_terrain.as_ptr());
|
||||
gl::BindVertexArray(terrain_vao);
|
||||
gl::DrawElements(
|
||||
gl::TRIANGLES,
|
||||
terrain_mesh.index_count,
|
||||
gl::UNSIGNED_INT,
|
||||
std::ptr::null(),
|
||||
);
|
||||
// Draw helicopter parts
|
||||
let model_heli: glm::Mat4 = glm::identity();
|
||||
let transform_heli = projection * view * model_heli;
|
||||
gl::UniformMatrix4fv(transform_loc, 1, gl::FALSE, transform_heli.as_ptr());
|
||||
gl::BindVertexArray(heli_body_vao);
|
||||
gl::DrawElements(gl::TRIANGLES, helicopter.body.index_count, gl::UNSIGNED_INT, std::ptr::null());
|
||||
gl::BindVertexArray(heli_door_vao);
|
||||
gl::DrawElements(gl::TRIANGLES, helicopter.door.index_count, gl::UNSIGNED_INT, std::ptr::null());
|
||||
gl::BindVertexArray(heli_main_rotor_vao);
|
||||
gl::DrawElements(gl::TRIANGLES, helicopter.main_rotor.index_count, gl::UNSIGNED_INT, std::ptr::null());
|
||||
gl::BindVertexArray(heli_tail_rotor_vao);
|
||||
gl::DrawElements(gl::TRIANGLES, helicopter.tail_rotor.index_count, gl::UNSIGNED_INT, std::ptr::null());
|
||||
// Update rotor rotations
|
||||
{
|
||||
let mut mr_pin = main_rotor_node.as_mut();
|
||||
unsafe { mr_pin.get_unchecked_mut().rotation.y = elapsed * 10.0; }
|
||||
let mut tr_pin = tail_rotor_node.as_mut();
|
||||
unsafe { tr_pin.get_unchecked_mut().rotation.y = elapsed * 10.0; }
|
||||
}
|
||||
|
||||
// Draw point‐particles
|
||||
let transform_particles = projection * view * glm::identity();
|
||||
gl::UniformMatrix4fv(transform_loc, 1, gl::FALSE, transform_particles.as_ptr());
|
||||
gl::BindVertexArray(particle_vao);
|
||||
gl::DrawArrays(gl::POINTS, 0, particle_count);
|
||||
|
||||
// Draw billboard (camera-facing quad)
|
||||
let translation_bill = glm::translation(&glm::vec3(0.0, 0.0, -2.0));
|
||||
let inv_rot_y = glm::rotation(cam_yaw, &glm::vec3(0.0, 1.0, 0.0));
|
||||
let inv_rot_x = glm::rotation(cam_pitch, &glm::vec3(1.0, 0.0, 0.0));
|
||||
let model_billboard = translation_bill * inv_rot_y * inv_rot_x;
|
||||
let transform_bill = projection * view * model_billboard;
|
||||
gl::UniformMatrix4fv(transform_loc, 1, gl::FALSE, transform_bill.as_ptr());
|
||||
gl::BindVertexArray(billboard_vao);
|
||||
gl::DrawElements(
|
||||
gl::TRIANGLES,
|
||||
billboard_indices.len() as i32,
|
||||
gl::UNSIGNED_INT,
|
||||
std::ptr::null(),
|
||||
);
|
||||
// Draw scene via scene graph
|
||||
let view_proj = projection * view;
|
||||
draw_scene(&*root_node, transform_loc, &view_proj, &glm::identity());
|
||||
}
|
||||
|
||||
// Display the new color buffer on the display
|
||||
|
||||
Reference in New Issue
Block a user