Post by baanish on Apr 26, 2023 3:35:53 GMT
I 'animated' this post chapter 8. Runs surprising well for CPU rendering. I'm generating around 45FPS on my 13900k.
the animation code if anyone is interested
output.mp4 (21.86 KB)
the animation code if anyone is interested
#[test]
#[ignore]
fn test_animation() {
use ray_tracing::camera::{Camera, render};
use ray_tracing::lights::PointLight;
use ray_tracing::matrix::matrix::Matrix;
use ray_tracing::sphere::sphere::Sphere;
use ray_tracing::tuple::color::Color;
use ray_tracing::world::World;
//setup world for a 'solar eclipse'
let mut world = World::new();
//sun at 0,0,0
world.lights = vec![PointLight::new(ray_tracing::tuple::point::Point::new(-0.0, 0.0, -0.0), Color::new(1.0, 1.0, 1.0))];
//earth at 5,0,0
let mut earth = Sphere::new();
earth.transform = Matrix::translation(5.0, 0.0, 0.0) * Matrix::scale(2.0, 2.0, 2.0);
earth.material.color = Color::new(0.0, 0.0, 1.0);
earth.material.diffuse = 0.7;
earth.material.specular = 0.3;
//moon at 0,0,-4
let mut moon = Sphere::new();
//moon is 1/3 the size of earth
moon.transform = Matrix::translation(8.0, 0.0, 0.0) * Matrix::scale(0.5, 0.5, 0.5);
moon.material.color = Color::new(1.0, 1.0, 1.0);
moon.material.diffuse = 0.7;
moon.material.specular = 0.3;
world.objects = vec![earth, moon];
//setup camera
let mut camera = Camera::new(1000, 500, std::f64::consts::PI / 3.0);
camera.transform = Matrix::view_transform(
ray_tracing::tuple::point::Point::new(0.0, 0.0, -15.0),
ray_tracing::tuple::point::Point::new(0.0, 0.0, 0.0),
ray_tracing::tuple::vector::Vector::new(0.0, 1.0, 0.0));
let mut moon_position = (8.0, 0.0, 0.0);
// generate 24*5 frames and animate the moon through a full orbit around the earth
let num_frames = 24*5;
let angle_step = 2.0 * std::f64::consts::PI / (num_frames as f64 - 1.0);
for i in 0..24*5 {
//render
let image = render(&camera, &world);
//save image
image.save_image(&format!("animation/eclipse{:03}.png", i));
// translate the moon without rotating it
let angle = angle_step * i as f64;
moon_position.0 = 5.0 + (3.0 * angle.cos());
moon_position.1 = 0.0 + (3.0 * angle.sin());
println!("moon position: {:?}", moon_position);
world.objects[1].transform = Matrix::translation(moon_position.0, moon_position.1, moon_position.2) * Matrix::scale(0.5, 0.5, 0.5)
}
}
output.mp4 (21.86 KB)