Skip to content

Commit 545871d

Browse files
committed
added triangle support
1 parent 7447278 commit 545871d

File tree

18 files changed

+957
-734
lines changed

18 files changed

+957
-734
lines changed

src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn parse_spheres(data: &[f32]) -> Vec<Sphere> {
2222
}
2323

2424
fn parse_triangles(data: &[f32]) -> Vec<Triangle> {
25-
data.chunks_exact(9)
25+
data.chunks_exact(12)
2626
.map(|c| {
2727
Triangle::new(
2828
Vec3::new(c[0], c[1], c[2]),
@@ -40,9 +40,16 @@ fn parse_lights(data: &[f32]) -> Vec<Light> {
4040
.collect()
4141
}
4242

43+
#[wasm_bindgen(start)]
44+
pub fn init() {
45+
#[cfg(feature = "console_error_panic_hook")]
46+
console_error_panic_hook::set_once();
47+
}
48+
4349
#[wasm_bindgen]
4450
pub fn render_rows(
4551
width: u32,
52+
height: u32,
4653
start_row: u32,
4754
end_row: u32,
4855
sphere_data: &[f32],
@@ -59,7 +66,8 @@ pub fn render_rows(
5966

6067
for y in start_row..end_row {
6168
for x in 0..width {
62-
let point = Vec3::new(x as f32, y as f32, 0.0);
69+
let flipped_y = height as f32 - y as f32 - 1.0;
70+
let point = Vec3::new(x as f32, flipped_y, 0.0);
6371
let color = get_pixel(&point, &spheres, &triangles, &lights, diffuse_intensity);
6472
pixels.push((color.r * 255.0) as u8);
6573
pixels.push((color.g * 255.0) as u8);

src/scene/scene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn get_pixel(
1212
ambient_intensity: f32,
1313
) -> Color {
1414
let mut color = Color::new(0.0, 0.0, 0.0);
15-
let ray = Ray::new(*point, Vec3::new(0.0, 0.0, 1.0));
15+
let ray = Ray::new(*point, Vec3::new(0.0, 0.0, -1.0));
1616
let mut lambda = f32::INFINITY;
1717
let mut nearest_hit: Option<Hit> = None;
1818

src/scene/triangle.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@ impl Triangle {
2929
}
3030

3131
pub fn get_hit(&self, ray: &Ray) -> Option<Hit> {
32-
let a = Matrix3x3::fromVectors([ray.direction, -self.v, -self.w]);
32+
let a = Matrix3x3::from_vectors([ray.direction, -self.v, -self.w]);
3333
let b = self.point_a - ray.origin;
3434

35-
let hit_offsets = a * b;
36-
if !(hit_offsets.x >= 0.0
37-
&& hit_offsets.y >= 0.0
38-
&& hit_offsets.y <= 1.0
39-
&& hit_offsets.z >= 0.0
40-
&& hit_offsets.z <= 1.0)
41-
{
35+
let hit_offsets = a.inverse() * b;
36+
let lambda = hit_offsets.x;
37+
let mu = hit_offsets.y;
38+
let tao = hit_offsets.z;
39+
40+
if lambda <= 0.0 || mu < 0.0 || tao < 0.0 || mu + tao > 1.0 {
4241
return None;
4342
}
4443

src/util/matrices.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub struct Matrix3x3 {
77
}
88

99
impl Matrix3x3 {
10-
pub fn fromVectors(vectors: [Vec3; 3]) -> Self {
10+
pub fn from_vectors(vectors: [Vec3; 3]) -> Self {
1111
Self {
1212
matrix: [
1313
vectors[0].x,

0 commit comments

Comments
 (0)