Skip to content

Commit

Permalink
Renderer checker board
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-Rapious committed Sep 11, 2023
1 parent b4560fe commit 9c6c259
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
Binary file added app/generated_images/23_checker_texture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 7 additions & 8 deletions app/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use lib_ray_tracer::{
camera::{self, Camera},
geometry::Sphere,
material::Material,
texture::Texture,
world::World,
Renderer,
};
Expand All @@ -28,16 +29,14 @@ pub fn render() {
let mut world = World::empty();
let mut rng = thread_rng();

let ground_mat = Material::Lambertian(Vector3::new(0.5, 0.5, 0.5));
/*let checker = CheckerTexture::from_colors(
0.32,
Vector3::new(0.2, 0.3, 0.1),
Vector3::new(0.9, 0.9, 0.9),
);*/
//let ground_mat = Material::Lambertian(Vector3::new(0.5, 0.5, 0.5));
static EVEN: Texture = Texture::SolidColor(Vector3::new(0.2, 0.3, 0.1));
static ODD: Texture = Texture::SolidColor(Vector3::new(0.9, 0.9, 0.9));
let checker = Material::TexturedLambertian(Texture::CheckerTexture(3.0, &EVEN, &ODD));
world.add(Sphere::stationary(
Point3::new(0.0, -1000.0, 0.0),
1000.0,
ground_mat,
checker,
));

for a in -11..11 {
Expand Down Expand Up @@ -111,5 +110,5 @@ pub fn render() {
let renderer = Renderer::new(aspect_ratio, image_width, camera);
let img = renderer.render_parallel_image(&world2);

img.save("generated_images/22_refactor.png").unwrap();
img.save("generated_images/23_checker_texture.png").unwrap();
}
14 changes: 14 additions & 0 deletions src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use nalgebra::Vector3;
use rand::{Rng, RngCore};

use crate::ray::Ray;
use crate::texture::Texture;
use crate::utility::*;
use crate::world::HitRecord;

// TODO: transform Material into a trait
#[derive(Clone, Copy)]
pub enum Material {
Lambertian(Vector3<f64>),
TexturedLambertian(Texture),
Hemisphere(Vector3<f64>),
Metal(Vector3<f64>, f64),
Dielectric(f64),
Expand Down Expand Up @@ -39,6 +41,18 @@ impl Material {
*attenuation = albedo;
true
}
TexturedLambertian(texture) => {
let mut scatter_direction = hit_record.normal + random_unit_vector(rng);

// Catch degenerate scatter direction
if scatter_direction.norm_squared() < 1e-8 {
scatter_direction = hit_record.normal;
}

*scattered_ray = Ray::new(hit_record.hit_point, scatter_direction, ray_in.time());
*attenuation = texture.value(0.0, 0.0, hit_record.hit_point);
true
}
Hemisphere(albedo) => {
let direction = random_on_hemisphere(&hit_record.normal, rng);
*scattered_ray = Ray::new(hit_record.hit_point, direction, ray_in.time());
Expand Down
8 changes: 4 additions & 4 deletions src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ impl Texture {
match *self {
Self::SolidColor(color) => color,
Self::CheckerTexture(inv_scale, color_even, color_odd) => {
let x_int = (inv_scale * p.x) as usize;
let y_int = (inv_scale * p.y) as usize;
let z_int = (inv_scale * p.z) as usize;
let x_int = (inv_scale * p.x) as i32;
let y_int = (inv_scale * p.y) as i32;
let z_int = (inv_scale * p.z) as i32;

if (x_int + y_int + z_int % 2) == 0 {
if (x_int + y_int + z_int) % 2 == 0 {
color_even.value(u, v, p)
} else {
color_odd.value(u, v, p)
Expand Down

0 comments on commit 9c6c259

Please sign in to comment.