eoc: Updates to work with new Float abstraction.

This commit is contained in:
Bill Thiede 2021-07-18 17:27:52 -07:00
parent 1c06833658
commit ecf7cd7bdc
6 changed files with 21 additions and 17 deletions

View File

@ -3,6 +3,7 @@ use anyhow::{bail, Result};
use rtchallenge::{
canvas::Canvas,
tuples::{Color, Tuple},
Float,
};
#[derive(Debug)]
@ -47,8 +48,8 @@ fn main() -> Result<()> {
let bg = Color::new(0.2, 0.2, 0.2);
let mut c = Canvas::new(w, h, bg);
let mut i = 0;
let w = w as f32;
let h = h as f32;
let w = w as Float;
let h = h as Float;
while p.position.y > 0. {
p = tick(&e, &p);
println!("tick {}: proj {:?}", i, p);

View File

@ -1,11 +1,11 @@
use std::f32::consts::PI;
use anyhow::Result;
use rtchallenge::{
canvas::Canvas,
float::consts::PI,
matrices::Matrix4x4,
tuples::{Color, Tuple},
Float,
};
fn draw_dot(c: &mut Canvas, x: usize, y: usize) {
@ -30,13 +30,13 @@ fn main() -> Result<()> {
let rot_hour = Matrix4x4::rotation_z(-PI / 6.);
let mut p = t * p;
let w = w as f32;
let h = h as f32;
let w = w as Float;
let h = h as Float;
// The 'world' exists between -0.5 - 0.5 in X-Y plane.
// To convert to screen space, we translate by 0.5, scale to canvas size,
// and invert the Y-axis.
let world_to_screen =
Matrix4x4::scaling(w as f32, -h as f32, 1.0) * Matrix4x4::translation(0.5, -0.5, 0.);
Matrix4x4::scaling(w as Float, -h as Float, 1.0) * Matrix4x4::translation(0.5, -0.5, 0.);
for _ in 0..12 {
let canvas_pixel = world_to_screen * p;
draw_dot(&mut c, canvas_pixel.x as usize, canvas_pixel.y as usize);

View File

@ -6,6 +6,7 @@ use rtchallenge::{
rays::Ray,
spheres::{intersect, Sphere},
tuples::{Color, Tuple},
Float,
};
fn main() -> Result<()> {
@ -16,7 +17,7 @@ fn main() -> Result<()> {
let ray_origin = Tuple::point(0., 0., -5.);
let wall_z = 10.;
let wall_size = 7.;
let pixel_size = wall_size / w as f32;
let pixel_size = wall_size / w as Float;
let half = wall_size / 2.;
let color = Color::new(1., 0., 0.);
let mut shape = Sphere::default();
@ -25,9 +26,9 @@ fn main() -> Result<()> {
);
for y in 0..h {
let world_y = half - pixel_size * y as f32;
let world_y = half - pixel_size * y as Float;
for x in 0..w {
let world_x = -half + pixel_size * x as f32;
let world_x = -half + pixel_size * x as Float;
let position = Tuple::point(world_x, world_y, wall_z);
let r = Ray::new(ray_origin, (position - ray_origin).normalize());
let xs = intersect(&shape, &r);

View File

@ -7,7 +7,7 @@ use rtchallenge::{
rays::Ray,
spheres::{intersect, Sphere},
tuples::{Color, Tuple},
WHITE,
Float, WHITE,
};
fn main() -> Result<()> {
@ -18,7 +18,7 @@ fn main() -> Result<()> {
let ray_origin = Tuple::point(0., 0., -5.);
let wall_z = 10.;
let wall_size = 7.;
let pixel_size = wall_size / w as f32;
let pixel_size = wall_size / w as Float;
let half = wall_size / 2.;
let mut shape = Sphere::default();
shape.material = Material {
@ -33,9 +33,9 @@ fn main() -> Result<()> {
let light = PointLight::new(light_position, light_color);
let in_shadow = false;
for y in 0..h {
let world_y = half - pixel_size * y as f32;
let world_y = half - pixel_size * y as Float;
for x in 0..w {
let world_x = -half + pixel_size * x as f32;
let world_x = -half + pixel_size * x as Float;
let position = Tuple::point(world_x, world_y, wall_z);
let direction = (position - ray_origin).normalize();
let r = Ray::new(ray_origin, direction);

View File

@ -1,10 +1,11 @@
use std::{f32::consts::PI, time::Instant};
use std::time::Instant;
use anyhow::Result;
use structopt::StructOpt;
use rtchallenge::{
camera::{Camera, RenderStrategy},
float::consts::PI,
lights::PointLight,
materials::Material,
matrices::Matrix4x4,

View File

@ -1,10 +1,11 @@
use std::{f32::consts::PI, time::Instant};
use std::time::Instant;
use anyhow::Result;
use structopt::StructOpt;
use rtchallenge::{
camera::{Camera, RenderStrategy},
float::consts::PI,
lights::PointLight,
materials::Material,
matrices::Matrix4x4,
@ -31,7 +32,7 @@ fn main() -> Result<()> {
let light_position = Tuple::point(-10., 10., -10.);
let light_color = WHITE;
let light = PointLight::new(light_position, light_color);
let mut camera = Camera::new(width, height, PI / 4.);
let mut camera = Camera::new(width, height, PI / 2.);
let from = Tuple::point(0., 1.5, -5.);
let to = Tuple::point(0., 1., 0.);
let up = Tuple::point(0., 1., 0.);