Add sphere.
Move original tracer1 to tracer_blue_sky and create new tracer as tracer_red_dot.
This commit is contained in:
parent
e45bb8ddad
commit
5ca6cc0809
@ -1,9 +1,22 @@
|
|||||||
extern crate rtiow;
|
extern crate rtiow;
|
||||||
|
|
||||||
use rtiow::ray::Ray;
|
use rtiow::ray::Ray;
|
||||||
|
use rtiow::vec3::dot;
|
||||||
use rtiow::vec3::Vec3;
|
use rtiow::vec3::Vec3;
|
||||||
|
|
||||||
|
fn hit_sphere(center: Vec3, radius: f32, r: Ray) -> bool {
|
||||||
|
let oc = r.origin() - center;
|
||||||
|
let a = dot(r.direction(), r.direction());
|
||||||
|
let b = 2. * dot(oc, r.direction());
|
||||||
|
let c = dot(oc, oc) - radius * radius;
|
||||||
|
let discriminant = b * b - 4. * a * c;
|
||||||
|
discriminant > 0.
|
||||||
|
}
|
||||||
|
|
||||||
fn color(r: Ray) -> Vec3 {
|
fn color(r: Ray) -> Vec3 {
|
||||||
|
if hit_sphere(Vec3::new(0., 0., -1.), 0.5, r) {
|
||||||
|
return Vec3::new(1., 0., 0.);
|
||||||
|
}
|
||||||
let unit_direction = r.direction().unit_vector();
|
let unit_direction = r.direction().unit_vector();
|
||||||
let t = 0.5 * (unit_direction.y + 1.);
|
let t = 0.5 * (unit_direction.y + 1.);
|
||||||
Vec3::new(1., 1., 1.) * (1. - t) + Vec3::new(0.5, 0.7, 1.) * t
|
Vec3::new(1., 1., 1.) * (1. - t) + Vec3::new(0.5, 0.7, 1.) * t
|
||||||
46
rtiow/src/bin/tracer_red_dot.rs
Normal file
46
rtiow/src/bin/tracer_red_dot.rs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
extern crate rtiow;
|
||||||
|
|
||||||
|
use rtiow::ray::Ray;
|
||||||
|
use rtiow::vec3::dot;
|
||||||
|
use rtiow::vec3::Vec3;
|
||||||
|
|
||||||
|
fn hit_sphere(center: Vec3, radius: f32, r: Ray) -> bool {
|
||||||
|
let oc = r.origin() - center;
|
||||||
|
let a = dot(r.direction(), r.direction());
|
||||||
|
let b = 2. * dot(oc, r.direction());
|
||||||
|
let c = dot(oc, oc) - radius * radius;
|
||||||
|
let discriminant = b * b - 4. * a * c;
|
||||||
|
discriminant > 0.
|
||||||
|
}
|
||||||
|
|
||||||
|
fn color(r: Ray) -> Vec3 {
|
||||||
|
if hit_sphere(Vec3::new(0., 0., -1.), 0.5, r) {
|
||||||
|
return Vec3::new(1., 0., 0.);
|
||||||
|
}
|
||||||
|
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 nx = 200;
|
||||||
|
let ny = 100;
|
||||||
|
println!("P3\n{} {}\n255", nx, ny);
|
||||||
|
let lower_left_corner = Vec3::new(-2., -1., -1.);
|
||||||
|
let horizontal = Vec3::new(4., 0., 0.);
|
||||||
|
let vertical = Vec3::new(0., 2., 0.);
|
||||||
|
let origin: Vec3 = Default::default();
|
||||||
|
for j in (0..ny).rev() {
|
||||||
|
for i in 0..nx {
|
||||||
|
let u = i as f32 / nx as f32;
|
||||||
|
let v = j as f32 / ny as f32;
|
||||||
|
let r = Ray::new(origin, lower_left_corner + horizontal * u + vertical * v);
|
||||||
|
let col = color(r);
|
||||||
|
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(())
|
||||||
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
use vec3::Vec3;
|
use vec3::Vec3;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Copy, Clone, Default)]
|
||||||
pub struct Ray {
|
pub struct Ray {
|
||||||
a: Vec3,
|
a: Vec3,
|
||||||
b: Vec3,
|
b: Vec3,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user