Implement sub-sampling anti-aliasing.

Move camera to code to separate module.
This commit is contained in:
Bill Thiede 2018-09-09 13:36:33 -07:00
parent 35722b57b1
commit 9698afce43
5 changed files with 164 additions and 0 deletions

82
rtiow/Cargo.lock generated
View File

@ -1,4 +1,86 @@
[[package]]
name = "bitflags"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cloudabi"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fuchsia-zircon-sys"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
"rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand_core"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rtiow"
version = "0.1.0"
dependencies = [
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d"
"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c"
"checksum rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "edecf0f94da5551fc9b492093e30b041a891657db7940ee221f9d2f66e82eef2"
"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -4,3 +4,4 @@ version = "0.1.0"
authors = ["Bill Thiede <rust@xinu.tv>"]
[dependencies]
rand = "0.5.5"

View File

@ -0,0 +1,53 @@
extern crate rand;
extern crate rtiow;
use rand::Rng;
use rtiow::camera::Camera;
use rtiow::hitable::Hit;
use rtiow::hitable_list::HitableList;
use rtiow::ray::Ray;
use rtiow::sphere::Sphere;
use rtiow::vec3::Vec3;
fn color(r: Ray, world: &Hit) -> Vec3 {
if let Some(rec) = world.hit(r, 0., std::f32::MAX) {
return (rec.normal + 1.) * 0.5;
}
// No hit, choose color from background.
let unit_direction = r.direction().unit_vector();
let t = 0.5 * (unit_direction.y + 1.);
Vec3::new(1., 1., 1.) * (1. - t) + Vec3::new(0.5, 0.7, 1.) * t
}
fn main() -> Result<(), std::io::Error> {
let mut rng = rand::thread_rng();
let nx = 200;
let ny = 100;
let ns = 100;
println!("P3\n{} {}\n255", nx, ny);
let objects = vec![
Sphere::new(Vec3::new(0., 0., -1.), 0.5),
Sphere::new(Vec3::new(0., -100.5, -1.), 100.),
];
let cam = Camera::new2x1();
let world = HitableList::new(objects.iter().map(|o| o).collect());
for j in (0..ny).rev() {
for i in 0..nx {
let mut col: Vec3 = Default::default();
for _ in 0..ns {
let u = (rng.gen_range::<f32>(0., 1.) + i as f32) / nx as f32;
let v = (rng.gen_range::<f32>(0., 1.) + j as f32) / ny as f32;
let r = cam.get_ray(u, v);
col = col + color(r, &world);
}
col = col / ns as f32;
let ir = (255.99 * col[0]) as u32;
let ig = (255.99 * col[1]) as u32;
let ib = (255.99 * col[2]) as u32;
println!("{} {} {}", ir, ig, ib);
}
}
Ok(())
}

27
rtiow/src/camera.rs Normal file
View File

@ -0,0 +1,27 @@
use ray::Ray;
use vec3::Vec3;
pub struct Camera {
origin: Vec3,
lower_left_corner: Vec3,
horizontal: Vec3,
vertical: Vec3,
}
impl Camera {
pub fn new2x1() -> Camera {
Camera {
lower_left_corner: Vec3::new(-2., -1., -1.),
horizontal: Vec3::new(4., 0., 0.),
vertical: Vec3::new(0., 2., 0.),
origin: Default::default(),
}
}
pub fn get_ray(&self, u: f32, v: f32) -> Ray {
Ray::new(
self.origin,
self.lower_left_corner + self.horizontal * u + self.vertical * v - self.origin,
)
}
}

View File

@ -1,3 +1,4 @@
pub mod camera;
pub mod hitable;
pub mod hitable_list;
pub mod ray;