Skip to content

Commit ce62de7

Browse files
committed
added face
1 parent f36518b commit ce62de7

File tree

6 files changed

+63
-33
lines changed

6 files changed

+63
-33
lines changed

src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
pub mod color;
2-
pub mod ray;
1+
mod color;
32
mod scene;
4-
pub mod sphere;
5-
pub mod vector;
3+
mod sphere;
4+
mod util;
65

76
use wasm_bindgen::prelude::*;
87
use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement, ImageData};
98

109
use scene::get_pixel;
1110

12-
use crate::vector::Vec3;
11+
use crate::util::vector::Vec3;
1312

1413
#[wasm_bindgen]
1514
pub fn render(canvas: HtmlCanvasElement) -> Result<(), JsValue> {

src/scene.rs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,59 @@
11
use crate::color::Color;
2-
use crate::ray::Ray;
32
use crate::sphere::Sphere;
4-
use crate::vector::Vec3;
3+
use crate::util::{ray::Ray, vector::Vec3};
54

65
pub fn circles() -> Vec<Sphere> {
76
vec![
7+
// Blue
88
Sphere {
9-
center: Vec3::new(600.0, 300.0, 500.0),
10-
radius: 150.0,
9+
center: Vec3::new(800.0, 400.0, 400.0),
10+
radius: 250.0,
11+
color: Color::new(0.0, 0.0, 1.0),
12+
},
13+
// Red
14+
Sphere {
15+
center: Vec3::new(800.0, 400.0, 150.0),
16+
radius: 50.0,
1117
color: Color::new(1.0, 0.0, 0.0),
1218
},
13-
// Circle {
14-
// center: Vec3::new(500.0, 500.0, 500.0),
15-
// radius: 150.0,
16-
// color: Color::new(0.0, 1.0, 0.0),
17-
// },
18-
// Circle {
19-
// center: Vec3::new(700.0, 500.0, 500.0),
20-
// radius: 150.0,
21-
// color: Color::new(0.0, 0.0, 1.0),
22-
// },
19+
// Light Blue left
20+
Sphere {
21+
center: Vec3::new(710.0, 310.0, 330.0),
22+
radius: 150.0,
23+
color: Color::new(0.0, 1.0, 1.0),
24+
},
25+
// Light Blue right
26+
Sphere {
27+
center: Vec3::new(890.0, 310.0, 330.0),
28+
radius: 150.0,
29+
color: Color::new(0.0, 1.0, 1.0),
30+
},
31+
// Lime left
32+
Sphere {
33+
center: Vec3::new(650.0, 250.0, 150.0),
34+
radius: 50.0,
35+
color: Color::new(0.0, 1.0, 0.0),
36+
},
37+
// Lime right
38+
Sphere {
39+
center: Vec3::new(950.0, 250.0, 150.0),
40+
radius: 50.0,
41+
color: Color::new(0.0, 1.0, 0.0),
42+
},
2343
]
2444
}
2545

2646
pub fn get_pixel(point: &Vec3) -> Color {
27-
let mut color = Color::new(1.0, 1.0, 1.0);
47+
let mut color = Color::new(0.0, 0.0, 0.0);
2848
let ray = Ray::new(*point, Vec3::new(0.0, 0.0, 1.0));
49+
let mut lambda = f32::INFINITY;
2950

3051
for sphere in &circles() {
31-
if sphere.contains(point) {
32-
color = color + sphere.color;
52+
if let Some(l) = sphere.get_lambda(&ray) {
53+
if l < lambda {
54+
lambda = l;
55+
color = sphere.color;
56+
}
3357
}
3458
}
3559

src/sphere.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::{color::Color, vector::Vec3};
1+
use crate::{
2+
color::Color,
3+
util::{ray::Ray, vector::Vec3},
4+
};
25

36
#[derive(Debug, Clone, Copy)]
47
pub struct Sphere {
@@ -8,15 +11,16 @@ pub struct Sphere {
811
}
912

1013
impl Sphere {
11-
pub fn new(center: Vec3, radius: f32, color: Color) -> Self {
12-
Self {
13-
center,
14-
radius,
15-
color,
14+
pub fn get_lambda(&self, ray: &Ray) -> Option<f32> {
15+
let v = ray.origin - self.center;
16+
let u = ray.direction;
17+
let a = u.dot(&u);
18+
let b = 2.0 * v.dot(&u);
19+
let c = v.dot(&v) - self.radius * self.radius;
20+
let root_content = b * b - 4.0 * a * c;
21+
if root_content < 0.0 {
22+
return None;
1623
}
17-
}
18-
19-
pub fn contains(&self, point: &Vec3) -> bool {
20-
point.distance2(&self.center) <= self.radius * self.radius
24+
Some((-b - (root_content).sqrt()) / (2.0 * a))
2125
}
2226
}

src/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod ray;
2+
pub mod vector;

src/ray.rs renamed to src/util/ray.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::vector::Vec3;
1+
use crate::util::vector::Vec3;
22

33
pub struct Ray {
44
pub origin: Vec3,

src/vector.rs renamed to src/util/vector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl Vec3 {
3636
pub fn distance(&self, other: &Self) -> f32 {
3737
(*self - *other).length()
3838
}
39+
3940
pub fn distance2(&self, other: &Self) -> f32 {
4041
(*self - *other).length2()
4142
}

0 commit comments

Comments
 (0)