Compare commits
3 Commits
3d835bcf63
...
63ccb552fd
Author | SHA1 | Date |
---|---|---|
|
63ccb552fd | |
|
3fbe40c149 | |
|
b89ad6932d |
|
@ -0,0 +1,7 @@
|
||||||
|
use crate::types_defined::Color;
|
||||||
|
|
||||||
|
impl Color {
|
||||||
|
pub fn to_color(self) -> String {
|
||||||
|
return format!("{} {} {}\n", (self.x * 256.0) as u8, (self.y * 256.0) as u8, (self.z * 256.0) as u8);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
use crate::types_defined::{HitRecord, Ray};
|
||||||
|
|
||||||
|
pub trait Hittable {
|
||||||
|
fn hit(self, r: &Ray, t_min: f32, t_max: f32) -> HitRecord;
|
||||||
|
}
|
190
src/image.rs
190
src/image.rs
|
@ -1,14 +1,7 @@
|
||||||
use crate::{point::Point, ray::Ray, vec3::{self, Vec3}};
|
use crate::hittable::Hittable;
|
||||||
|
use crate::types_defined::{Color, Point, Ray, Sphere, Vec3};
|
||||||
|
|
||||||
|
|
||||||
pub type Color = vec3::Vec3;
|
|
||||||
|
|
||||||
|
|
||||||
impl Color {
|
|
||||||
pub fn to_color(self) -> String {
|
|
||||||
return format!("{} {} {}\n", (self.x * 256.0) as u8, (self.y * 256.0) as u8, (self.z * 256.0) as u8);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn gen_ray_sphere_normal_ppm_p3(width: i32, height: i32, camera_center: Vec3, viewport_top_left_pixel_center: Vec3, viewport_u_delta: Vec3, viewport_v_delta: Vec3) -> String {
|
pub fn gen_ray_sphere_normal_ppm_p3(width: i32, height: i32, camera_center: Vec3, viewport_top_left_pixel_center: Vec3, viewport_u_delta: Vec3, viewport_v_delta: Vec3) -> String {
|
||||||
let mut img_content = format!("P3\n{} {}\n255\n", width, height);
|
let mut img_content = format!("P3\n{} {}\n255\n", width, height);
|
||||||
|
@ -26,15 +19,15 @@ pub fn gen_ray_sphere_normal_ppm_p3(width: i32, height: i32, camera_center: Vec3
|
||||||
let ray_direction = pixel_center - camera_center;
|
let ray_direction = pixel_center - camera_center;
|
||||||
// ray
|
// ray
|
||||||
let r = Ray::new(camera_center, ray_direction);
|
let r = Ray::new(camera_center, ray_direction);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
球
|
球
|
||||||
*/
|
*/
|
||||||
let _sphere_center = Point::new(0.0, 0.0, -1.0);
|
let _sphere_center = Point::new(0.0, 0.0, -1.0);
|
||||||
let sphere_radius = 0.5;
|
let sphere_radius = 0.5;
|
||||||
|
let sphere = Sphere::new(_sphere_center, sphere_radius);
|
||||||
|
|
||||||
// determind color
|
// determind color
|
||||||
let color = ray_color_at_sphere_normal(&r, _sphere_center, sphere_radius);
|
let color = ray_color(&r, sphere);
|
||||||
// content
|
// content
|
||||||
img_content.push_str(color.to_color().as_str());
|
img_content.push_str(color.to_color().as_str());
|
||||||
img_content.push('\n');
|
img_content.push('\n');
|
||||||
|
@ -44,184 +37,21 @@ pub fn gen_ray_sphere_normal_ppm_p3(width: i32, height: i32, camera_center: Vec3
|
||||||
img_content
|
img_content
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn gen_ray_sphere_ppm_p3(width: i32, height: i32, camera_center: Vec3, viewport_top_left_pixel_center: Vec3, viewport_u_delta: Vec3, viewport_v_delta: Vec3) -> String {
|
|
||||||
let mut img_content = format!("P3\n{} {}\n255\n", width, height);
|
|
||||||
|
|
||||||
for j in 0..height {
|
|
||||||
|
|
||||||
if j % 10 == 0 {
|
|
||||||
println!("scan line {}/{} ", j + 1, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in 0..width {
|
|
||||||
// every pixcel's position
|
|
||||||
let pixel_center = viewport_top_left_pixel_center + (i as f32 * viewport_u_delta) + (j as f32 * viewport_v_delta);
|
|
||||||
// Vector(camera, pixcel)
|
|
||||||
let ray_direction = pixel_center - camera_center;
|
|
||||||
// ray
|
|
||||||
let r = Ray::new(camera_center, ray_direction);
|
|
||||||
|
|
||||||
/*
|
|
||||||
球
|
|
||||||
*/
|
|
||||||
let _sphere_center = Point::new(0.0, 0.0, -1.0);
|
|
||||||
let sphere_radius = 0.5;
|
|
||||||
|
|
||||||
// determind color
|
|
||||||
let color = ray_color_at_sphere(&r, _sphere_center, sphere_radius);
|
|
||||||
// content
|
|
||||||
img_content.push_str(color.to_color().as_str());
|
|
||||||
img_content.push('\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
img_content
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn gen_ray_ppm_p3(width: i32, height: i32, camera_center: Vec3, viewport_top_left_pixel_center: Vec3, viewport_u_delta: Vec3, viewport_v_delta: Vec3) -> String {
|
|
||||||
let mut img_content = format!("P3\n{} {}\n255\n", width, height);
|
|
||||||
|
|
||||||
for j in 0..height {
|
|
||||||
|
|
||||||
if j % 10 == 0 {
|
|
||||||
println!("scan line {}/{} ", j + 1, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in 0..width {
|
|
||||||
// every pixcel's position
|
|
||||||
let pixel_center = viewport_top_left_pixel_center + (i as f32 * viewport_u_delta) + (j as f32 * viewport_v_delta);
|
|
||||||
// Vector(camera, pixcel)
|
|
||||||
let ray_direction = pixel_center - camera_center;
|
|
||||||
// ray
|
|
||||||
let r = Ray::new(camera_center, ray_direction);
|
|
||||||
// determind color
|
|
||||||
let color = ray_color(&r);
|
|
||||||
// content
|
|
||||||
img_content.push_str(color.to_color().as_str());
|
|
||||||
img_content.push('\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
img_content
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
b^ - 4ac > 0 : 2 交点; = 0:1交点;< 0 : 无交点
|
|
||||||
*/
|
|
||||||
fn hit_sphere(r: &Ray, sphere_center: Point, sphere_radius: f32) -> bool {
|
|
||||||
let a = r.direction.dot(r.direction);
|
|
||||||
let b_sqrt = (-2.0 * (r.direction.dot(sphere_center - r.point))).powi(2);
|
|
||||||
let c = (sphere_center - r.point).dot(sphere_center - r.point) - sphere_radius.powi(2);
|
|
||||||
|
|
||||||
return b_sqrt - 4.0 * a * c >= 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn hit_sphere_normal(r: &Ray, sphere_center: Point, sphere_radius: f32) -> f32 {
|
|
||||||
let a: f32 = r.direction.length_squared();
|
|
||||||
let h = r.direction.dot(sphere_center - r.point);
|
|
||||||
// let b = -2.0 * r.direction.dot(sphere_center - r.point);
|
|
||||||
let c = (sphere_center - r.point).length_squared() - sphere_radius * sphere_radius;
|
|
||||||
|
|
||||||
let discriminant = h*h - a*c;
|
|
||||||
|
|
||||||
// // 两个焦点
|
|
||||||
let disc_sqrt = discriminant.sqrt();
|
|
||||||
let near = (h - disc_sqrt) / a;
|
|
||||||
let far = (h + disc_sqrt) / a;
|
|
||||||
|
|
||||||
// 射线起点可能在球内的情况
|
|
||||||
if near >= 0.0 && far >= 0.0 {
|
|
||||||
return near.min(far);
|
|
||||||
} else if near > 0.0 {
|
|
||||||
return near;
|
|
||||||
} else if far > 0.0 {
|
|
||||||
return far;
|
|
||||||
} else {
|
|
||||||
return -1.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
ray color functions
|
ray color functions
|
||||||
*/
|
*/
|
||||||
fn ray_color_at_sphere_normal(r: &Ray, sphere_center: Point, sphere_radius: f32) -> Color {
|
fn ray_color<H: Hittable>(r: &Ray, h: H) -> Color {
|
||||||
|
let hr = h.hit(&r, 0.0, f32::MAX);
|
||||||
let t = hit_sphere_normal(r, sphere_center, sphere_radius);
|
if hr.t >= 0.0 {
|
||||||
if t >= 0.0 {
|
let inter_point = hr.p;
|
||||||
let inter_point = r.at(t);
|
|
||||||
// 单位向量
|
// 单位向量
|
||||||
let n = inter_point / inter_point.dot(inter_point);
|
let n = inter_point / inter_point.dot(inter_point);
|
||||||
// 法向量也是 -1~1 +1再x0.5让他落到颜色的区间
|
// 法向量也是 -1~1 +1再x0.5让他落到颜色的区间
|
||||||
return 0.5 * (n - sphere_center + Point::new(1.0, 1.0, 1.0))
|
// return 0.5 * (n - sphere_center + Point::new(1.0, 1.0, 1.0))
|
||||||
|
return 0.5 * (hr.normal + Point::new(1.0, 1.0, 1.0))
|
||||||
}
|
}
|
||||||
|
|
||||||
// v / |v|
|
// v / |v|
|
||||||
let unit_direction = r.direction / r.direction.length();
|
let unit_direction = r.direction / r.direction.length();
|
||||||
let a = 0.5*(unit_direction.y + 1.0);
|
let a = 0.5*(unit_direction.y + 1.0);
|
||||||
return (1.0-a)*Color::new(1.0, 1.0, 1.0) + a*Color::new(0.5, 0.7, 1.0);
|
return (1.0-a)*Color::new(1.0, 1.0, 1.0) + a*Color::new(0.5, 0.7, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ray_color_at_sphere(r: &Ray, sphere_center: Point, sphere_radius: f32) -> Color {
|
|
||||||
|
|
||||||
// return RED if hit sphere
|
|
||||||
if hit_sphere(r, sphere_center, sphere_radius) {
|
|
||||||
return Color::new(0.4, 0.4, 0.2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// v / |v|
|
|
||||||
let unit_direction = r.direction / r.direction.length();
|
|
||||||
let a = 0.5*(unit_direction.y + 1.0);
|
|
||||||
return (1.0-a)*Color::new(1.0, 1.0, 1.0) + a*Color::new(0.5, 0.7, 1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ray_color(r: &Ray) -> Color {
|
|
||||||
// v / |v|
|
|
||||||
let unit_direction = r.direction / r.direction.length();
|
|
||||||
let a = 0.5*(unit_direction.y + 1.0);
|
|
||||||
return (1.0-a)*Color::new(1.0, 1.0, 1.0) + a*Color::new(0.5, 0.7, 1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* -------width(i)-------
|
|
||||||
* |
|
|
||||||
* |
|
|
||||||
* height(j)
|
|
||||||
* |
|
|
||||||
* |
|
|
||||||
* ----------------------
|
|
||||||
*
|
|
||||||
* ppm p3 format:
|
|
||||||
*
|
|
||||||
* P3
|
|
||||||
*
|
|
||||||
* [width] [height]
|
|
||||||
* [max color]
|
|
||||||
* R1 G1 B1
|
|
||||||
* R2 G2 B2
|
|
||||||
* R3 G3 B3
|
|
||||||
* ...xN
|
|
||||||
*/
|
|
||||||
pub fn gen_gradient_ppm_p3(width: i32, height: i32) -> String {
|
|
||||||
let mut img_content = format!("P3\n{} {}\n255\n", width, height);
|
|
||||||
|
|
||||||
for j in 0..height {
|
|
||||||
println!("scan line {}/{} ", j + 1, height);
|
|
||||||
for i in 0..width {
|
|
||||||
let r = i as f32 / (width-1) as f32;
|
|
||||||
let g = j as f32 / (height-1) as f32;
|
|
||||||
let b = (i+j) as f32 / (width + height - 2) as f32;
|
|
||||||
|
|
||||||
let color = Color::new(r, g, b);
|
|
||||||
img_content.push_str(color.to_color().as_str());
|
|
||||||
|
|
||||||
img_content.push('\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
img_content
|
|
||||||
}
|
|
112
src/main.rs
112
src/main.rs
|
@ -1,26 +1,18 @@
|
||||||
use write_file_util::{write_image};
|
use write_file_util::{write_image};
|
||||||
use image::{gen_gradient_ppm_p3, gen_ray_ppm_p3, Color};
|
|
||||||
use vec3::{Vec3};
|
|
||||||
use point::{Point};
|
|
||||||
use ray::{Ray};
|
|
||||||
|
|
||||||
use crate::image::{gen_ray_sphere_normal_ppm_p3, gen_ray_sphere_ppm_p3};
|
|
||||||
|
|
||||||
|
use crate::image::{gen_ray_sphere_normal_ppm_p3};
|
||||||
|
use crate::types_defined::{Point, Vec3};
|
||||||
mod image;
|
mod image;
|
||||||
mod write_file_util;
|
mod write_file_util;
|
||||||
mod vec3;
|
mod vec3;
|
||||||
mod point;
|
|
||||||
mod ray;
|
mod ray;
|
||||||
|
mod hittable;
|
||||||
|
mod sphere;
|
||||||
|
mod types_defined;
|
||||||
|
mod color;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
gen_gradient_ppm();
|
|
||||||
println!("==================================");
|
|
||||||
ray_scene_render();
|
|
||||||
println!("==================================");
|
|
||||||
ray_sphere_scene_render();
|
|
||||||
println!("==================================");
|
|
||||||
ray_sphere_normal_scene_render();
|
ray_sphere_normal_scene_render();
|
||||||
println!("==================================");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -59,95 +51,3 @@ fn ray_sphere_normal_scene_render() {
|
||||||
|
|
||||||
write_image(ppm_content, "./target/ray_sphere_normal_scene_render.ppm".to_string())
|
write_image(ppm_content, "./target/ray_sphere_normal_scene_render.ppm".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ray_sphere_scene_render() {
|
|
||||||
let aspect_ratio = 16.0/9.0;
|
|
||||||
let image_width = 400;
|
|
||||||
// aleast 1px
|
|
||||||
let image_height = ((image_width as f32 / aspect_ratio) as i32).max(1);
|
|
||||||
|
|
||||||
let viewport_height = 2.0;
|
|
||||||
let viewport_width = viewport_height * (image_width as f32 / image_height as f32);
|
|
||||||
|
|
||||||
println!("set image({},{}), viewport({},{})", image_width, image_height, viewport_width, viewport_height);
|
|
||||||
let viewport_u = Vec3::new(viewport_width, 0.0, 0.0);
|
|
||||||
// image x--> right
|
|
||||||
// |
|
|
||||||
// y
|
|
||||||
// space: y up , x right , z back, -z front
|
|
||||||
let viewport_v = Vec3::new(0.0, -viewport_height, 0.0);
|
|
||||||
|
|
||||||
// width per pix
|
|
||||||
let viewport_u_delta = viewport_u / (image_width as f32);
|
|
||||||
// height per pix
|
|
||||||
let viewport_v_delta = viewport_v / (image_height as f32);
|
|
||||||
|
|
||||||
// camerea position
|
|
||||||
let camera_center = Point::new(0.0, 0.0, 0.0);
|
|
||||||
// -z 1.0 viewport to camera
|
|
||||||
let focal_length = Vec3::new(0.0, 0.0, -1.0);
|
|
||||||
// camera position --> viewport center ---> top center --> top left
|
|
||||||
let viewport_top_left_pixel = camera_center + focal_length - viewport_u / 2.0 - viewport_v / 2.0;
|
|
||||||
// padding 0.5* delta u/v
|
|
||||||
let viewport_top_left_pixel_center = viewport_top_left_pixel - viewport_u_delta / 2.0 - viewport_v_delta / 2.0;
|
|
||||||
|
|
||||||
let ppm_content = gen_ray_sphere_ppm_p3(image_width, image_height, camera_center, viewport_top_left_pixel_center, viewport_u_delta, viewport_v_delta);
|
|
||||||
|
|
||||||
write_image(ppm_content, "./target/ray_sphere_scene_render.ppm".to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn ray_scene_render() {
|
|
||||||
let aspect_ratio = 16.0/9.0;
|
|
||||||
let image_width = 400;
|
|
||||||
// aleast 1px
|
|
||||||
let image_height = ((image_width as f32 / aspect_ratio) as i32).max(1);
|
|
||||||
|
|
||||||
let viewport_height = 2.0;
|
|
||||||
let viewport_width = viewport_height * (image_width as f32 / image_height as f32);
|
|
||||||
|
|
||||||
println!("set image({},{}), viewport({},{})", image_width, image_height, viewport_width, viewport_height);
|
|
||||||
let viewport_u = Vec3::new(viewport_width, 0.0, 0.0);
|
|
||||||
// image x--> right
|
|
||||||
// |
|
|
||||||
// y
|
|
||||||
// space: y up , x right , z back, -z front
|
|
||||||
let viewport_v = Vec3::new(0.0, -viewport_height, 0.0);
|
|
||||||
|
|
||||||
// width per pix
|
|
||||||
let viewport_u_delta = viewport_u / (image_width as f32);
|
|
||||||
// height per pix
|
|
||||||
let viewport_v_delta = viewport_v / (image_height as f32);
|
|
||||||
|
|
||||||
// camerea position
|
|
||||||
let camera_center = Point::new(0.0, 0.0, 0.0);
|
|
||||||
// -z 1.0 viewport to camera
|
|
||||||
let focal_length = Vec3::new(0.0, 0.0, -1.0);
|
|
||||||
// camera position --> viewport center ---> top center --> top left
|
|
||||||
let viewport_top_left_pixel = camera_center + focal_length - viewport_u / 2.0 - viewport_v / 2.0;
|
|
||||||
// padding 0.5* delta u/v
|
|
||||||
let viewport_top_left_pixel_center = viewport_top_left_pixel - viewport_u_delta / 2.0 - viewport_v_delta / 2.0;
|
|
||||||
|
|
||||||
let ppm_content = gen_ray_ppm_p3(image_width, image_height, camera_center, viewport_top_left_pixel_center, viewport_u_delta, viewport_v_delta);
|
|
||||||
|
|
||||||
write_image(ppm_content, "./target/ray_scene_render.ppm".to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn gen_gradient_ppm() {
|
|
||||||
let ppm_content = gen_gradient_ppm_p3(400, 225);
|
|
||||||
|
|
||||||
let v1 = Vec3::new(1.0, 1.0, 1.0);
|
|
||||||
let v2 = Vec3::new(1.0, 1.0, 1.0);
|
|
||||||
|
|
||||||
println!("v: {:#} + {:#} => {:#}", v1, v2, v1 + v2);
|
|
||||||
println!("v: {:#} + {:#} => {:#}", v1, v2, v1 - v2);
|
|
||||||
println!("v: {:#} + {:#} => {:#}", v1, v2, v1.dot(v2));
|
|
||||||
println!("v: {:#} + {:#} => {:#}", v1, v2, v1.cross(v2));
|
|
||||||
println!("color: {:#}", Color::new(0.3, 0.4, 1.0).to_color());
|
|
||||||
println!("point: {:#}", Point::new(1.0, 1.0, 1.0));
|
|
||||||
let ray = Ray::new(Point::new(1.0, 1.0, 1.0), Vec3 { x: 0.0, y: 1.0, z: 0.0 });
|
|
||||||
println!("Ray: {:#} => {:#}", ray, ray.at(2.0));
|
|
||||||
|
|
||||||
write_image(ppm_content, "./target/gen_gradient_ppm.ppm".to_string())
|
|
||||||
}
|
|
|
@ -1,4 +0,0 @@
|
||||||
use crate::vec3;
|
|
||||||
|
|
||||||
pub type Point = vec3::Vec3;
|
|
||||||
|
|
13
src/ray.rs
13
src/ray.rs
|
@ -1,17 +1,6 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fmt::{Display};
|
use std::fmt::{Display};
|
||||||
|
use crate::types_defined::{Point, Ray, Vec3};
|
||||||
use crate::{point::Point, vec3::Vec3};
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
fn: P(t) = t*b
|
|
||||||
*/
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Ray {
|
|
||||||
pub point: Point,
|
|
||||||
pub direction: Vec3
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ray {
|
impl Ray {
|
||||||
pub fn new(p: Point, direction: Vec3) -> Ray {
|
pub fn new(p: Point, direction: Vec3) -> Ray {
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
use crate::hittable;
|
||||||
|
use crate::types_defined::{HitRecord, Point, Ray, Sphere};
|
||||||
|
|
||||||
|
impl Sphere {
|
||||||
|
pub fn new(c: Point, r: f32) -> Self {
|
||||||
|
Self {
|
||||||
|
center: c,
|
||||||
|
radius: r,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl hittable::Hittable for Sphere {
|
||||||
|
fn hit(self, r: &Ray, t_min: f32, t_max: f32) -> HitRecord {
|
||||||
|
let a: f32 = r.direction.length_squared();
|
||||||
|
let h = r.direction.dot(self.center - r.point);
|
||||||
|
// let b = -2.0 * r.direction.dot(sphere_center - r.point);
|
||||||
|
let c = (self.center - r.point).length_squared() - self.radius * self.radius;
|
||||||
|
|
||||||
|
let discriminant = h * h - a * c;
|
||||||
|
|
||||||
|
// // 两个焦点
|
||||||
|
let disc_sqrt = discriminant.sqrt();
|
||||||
|
let near = (h - disc_sqrt) / a;
|
||||||
|
let far = (h + disc_sqrt) / a;
|
||||||
|
|
||||||
|
let mut root = near;
|
||||||
|
if root <= t_min || root >= t_max {
|
||||||
|
root = far;
|
||||||
|
if root <= t_min || root >= t_max {
|
||||||
|
root = -1.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
HitRecord {
|
||||||
|
t: root,
|
||||||
|
p: r.at(root),
|
||||||
|
normal: r.at(root) - self.center,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
/*
|
||||||
|
* [3]
|
||||||
|
*/
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub struct Vec3 {
|
||||||
|
pub x: f32,
|
||||||
|
pub y: f32,
|
||||||
|
pub z: f32,
|
||||||
|
}
|
||||||
|
pub type Point = Vec3;
|
||||||
|
pub type Color = Vec3;
|
||||||
|
|
||||||
|
/*
|
||||||
|
fn: P(t) = t*b
|
||||||
|
*/
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Ray {
|
||||||
|
pub point: Point,
|
||||||
|
pub direction: Vec3
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct HitRecord {
|
||||||
|
pub t: f32,
|
||||||
|
pub p: Vec3,
|
||||||
|
pub normal: Vec3,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
/////////////
|
||||||
|
// for test
|
||||||
|
/////////////
|
||||||
|
*/
|
||||||
|
pub struct Sphere {
|
||||||
|
pub center: Point,
|
||||||
|
pub radius: f32,
|
||||||
|
}
|
10
src/vec3.rs
10
src/vec3.rs
|
@ -1,15 +1,7 @@
|
||||||
|
|
||||||
use std::{fmt::Display, ops::{Add, AddAssign, Div, Mul, MulAssign, Sub}};
|
use std::{fmt::Display, ops::{Add, AddAssign, Div, Mul, MulAssign, Sub}};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
/*
|
use crate::types_defined::Vec3;
|
||||||
* [3]
|
|
||||||
*/
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
||||||
pub struct Vec3 {
|
|
||||||
pub x: f32,
|
|
||||||
pub y: f32,
|
|
||||||
pub z: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Vec3 {
|
impl Display for Vec3 {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
|
Loading…
Reference in New Issue