Compare commits
8 commits
pixel-canv
...
main
Author | SHA1 | Date | |
---|---|---|---|
|
8468b8f80a | ||
|
a3644701b5 | ||
|
6292881294 | ||
|
fd9cbea2f6 | ||
|
637abb06ad | ||
|
e3cb37817c | ||
|
bbc31ca172 | ||
|
3f225bde7f |
6 changed files with 122 additions and 90 deletions
63
Cargo.lock
generated
63
Cargo.lock
generated
|
@ -8,12 +8,6 @@ version = "1.0.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "adler32"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"
|
||||
|
||||
[[package]]
|
||||
name = "ahash"
|
||||
version = "0.7.6"
|
||||
|
@ -39,9 +33,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
|||
|
||||
[[package]]
|
||||
name = "bytemuck"
|
||||
version = "1.11.0"
|
||||
version = "1.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5377c8865e74a160d21f29c2d40669f53286db6eab59b88540cbb12ffc8b835"
|
||||
checksum = "2f5715e491b5a1598fc2bef5a606847b5dc1d48ea625bd3c02c00de8285591da"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
|
@ -129,20 +123,11 @@ dependencies = [
|
|||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deflate"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c86f7e25f518f4b81808a2cf1c50996a61f5c2eb394b2393bd87f2a4780a432f"
|
||||
dependencies = [
|
||||
"adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.7.0"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f107b87b6afc2a64fd13cac55fe06d6c8859f12d4b14cbcdd2c67d0976781be"
|
||||
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
|
||||
|
||||
[[package]]
|
||||
name = "encode_unicode"
|
||||
|
@ -150,6 +135,16 @@ version = "0.3.6"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f82b0f4c27ad9f8bfd1f3208d882da2b09c301bc1c828fd3a00d0216d2fbbff6"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.7"
|
||||
|
@ -187,9 +182,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "indicatif"
|
||||
version = "0.17.0"
|
||||
version = "0.17.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fcc42b206e70d86ec03285b123e65a5458c92027d1fb2ae3555878b8113b3ddf"
|
||||
checksum = "bfddc9561e8baf264e0e45e197fd7696320026eb10a8180340debc27b18f535b"
|
||||
dependencies = [
|
||||
"console",
|
||||
"number_prefix",
|
||||
|
@ -204,9 +199,9 @@ checksum = "9478aa10f73e7528198d75109c8be5cd7d15fb530238040148d5f9a22d4c5b3b"
|
|||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.126"
|
||||
version = "0.2.133"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
|
||||
checksum = "c0f80d65747a3e43d1596c7c5492d95d5edddaabd45a7fcdb02b95f644164966"
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
|
@ -219,9 +214,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.5.3"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f5c75688da582b8ffc1f1799e9db273f32133c49e048f614d22ec3256773ccc"
|
||||
checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34"
|
||||
dependencies = [
|
||||
"adler",
|
||||
]
|
||||
|
@ -274,19 +269,19 @@ checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
|
|||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.13.0"
|
||||
version = "1.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1"
|
||||
checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0"
|
||||
|
||||
[[package]]
|
||||
name = "png"
|
||||
version = "0.17.5"
|
||||
version = "0.17.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc38c0ad57efb786dd57b9864e5b18bae478c00c824dc55a38bbc9da95dde3ba"
|
||||
checksum = "8f0e7f4c94ec26ff209cee506314212639d6c91b80afb82984819fafce9df01c"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"crc32fast",
|
||||
"deflate",
|
||||
"flate2",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
|
@ -319,9 +314,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.3"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
@ -388,9 +383,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.9"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
|
||||
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
# Blender v2.82 (sub 7) OBJ File: ''
|
||||
# www.blender.org
|
||||
mtllib viking_room.mtl
|
||||
o mesh_all1_Texture1_0
|
||||
|
||||
v -0.573651 0.001530 0.713748
|
||||
v -0.573651 0.151382 -0.000154
|
||||
v -0.573651 0.164474 0.619081
|
||||
|
|
|
@ -5,10 +5,10 @@ pub fn put_color(img: &mut RgbImage, pixel_color: &Color, x: u32, y: u32, sample
|
|||
let mut g = pixel_color.y();
|
||||
let mut b = pixel_color.z();
|
||||
|
||||
//let scale = 1.0 / samples_per_pixel as f64;
|
||||
//r = r/samples_per_pixel as f64; //(scale * r).sqrt();
|
||||
//g = g/samples_per_pixel as f64; //(scale * g).sqrt();
|
||||
//b = b/samples_per_pixel as f64; //(scale * b).sqrt();
|
||||
let scale = 1.0;// / samples_per_pixel as f64;
|
||||
r = (scale * r).sqrt();
|
||||
g = (scale * g).sqrt();
|
||||
b = (scale * b).sqrt();
|
||||
|
||||
img.put_pixel(
|
||||
x,
|
||||
|
|
|
@ -29,7 +29,8 @@ impl HittableList {
|
|||
|
||||
for obj in &self.objects {
|
||||
let mut temp_rec = HitRecord::empty();
|
||||
if obj.hit(&r, t_min, closest_so_far, &mut temp_rec) {
|
||||
if obj.hit(&r, t_min, closest_so_far, &mut temp_rec)
|
||||
&& closest_so_far > temp_rec.t {
|
||||
hit_anything = true;
|
||||
closest_so_far = temp_rec.t;
|
||||
*rec = temp_rec;
|
||||
|
|
123
src/main.rs
123
src/main.rs
|
@ -39,7 +39,7 @@ fn ray_color(r: &Ray, world: &HittableList, depth: u32) -> Color {
|
|||
{
|
||||
return attenuation * ray_color(&scattered, world, depth - 1);
|
||||
}
|
||||
return Color::null();
|
||||
return Color::new(255.0, 0.0, 0.0);//Color::null();
|
||||
}
|
||||
|
||||
let unit_direction = r.direction();
|
||||
|
@ -67,17 +67,17 @@ fn main() {
|
|||
|
||||
// Image
|
||||
let aspect_ratio = 10.0 / 7.5; //16.0 / 9.0;
|
||||
let image_width = 400;
|
||||
let image_width = 1200;
|
||||
let image_height = (image_width as f64 / aspect_ratio) as u32;
|
||||
let samples_per_pixel = 100_u32;
|
||||
let samples_per_pixel = 1_u32;
|
||||
let max_depth = 50;
|
||||
let antialiasing_threshold = 0.2; // at what diff between two colors will a pixel be antialiased
|
||||
|
||||
let vfov = 40.0;
|
||||
let lookfrom = Point3::new(2.0, 0.8, 3.0);
|
||||
let lookat = Point3::new(0.0, 0.0, 0.0);
|
||||
let vfov = 43.0;
|
||||
let lookfrom = Point3::new(2.0, 1.0, 1.0);
|
||||
let lookat = Point3::new(0.0, 0.2, 0.0);
|
||||
let vup = Vec3::new(0.0, 1.0, 0.0);
|
||||
let dist_to_focus = 3.0;
|
||||
let dist_to_focus = 1.0;
|
||||
let aperture = 0.0; // disable depth of field
|
||||
|
||||
// limit rayon multithreading thread count
|
||||
|
@ -88,7 +88,7 @@ fn main() {
|
|||
|
||||
// World
|
||||
eprintln!("[1/4] Loading meshes from file...");
|
||||
let world = from_obj("obj/suzanne.obj");
|
||||
let world = from_obj("obj/viking_room.obj");
|
||||
// let world = random_world();
|
||||
|
||||
// Camera
|
||||
|
@ -137,6 +137,26 @@ fn main() {
|
|||
.collect();
|
||||
bar.finish_and_clear();
|
||||
|
||||
// no antialiasing
|
||||
if samples_per_pixel == 1_u32 {
|
||||
eprintln!("[4/4] Exporting image to disk...");
|
||||
(0..image_height).into_iter().rev().for_each(|j| {
|
||||
(0..image_width).into_iter().for_each(|i| {
|
||||
color::put_color(
|
||||
&mut image,
|
||||
&color_lines[(image_height - j - 1) as usize][i as usize],
|
||||
i,
|
||||
image_height - j - 1,
|
||||
1,
|
||||
);
|
||||
})
|
||||
});
|
||||
|
||||
image.save(default_file).unwrap();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
eprintln!("[3/4] Antialiasing image...");
|
||||
let mut antialiasing: Vec<Vec<bool>> = Vec::new();
|
||||
let mut antialiasing_counter = 0;
|
||||
|
@ -237,8 +257,7 @@ fn main() {
|
|||
|
||||
/********************
|
||||
* WORLD GENERATION *
|
||||
*********************
|
||||
*/
|
||||
********************/
|
||||
|
||||
|
||||
/*
|
||||
|
@ -262,33 +281,47 @@ fn from_obj(path: &str) -> HittableList {
|
|||
)));
|
||||
*/
|
||||
|
||||
let material = Arc::new(Lambertian::new(&Color::new(
|
||||
77.0 / 255.0,
|
||||
77.0 / 255.0,
|
||||
118.0 / 255.0,
|
||||
)));
|
||||
//let material = Arc::new(Lambertian::new(&Color::new(
|
||||
// 77.0 / 255.0,
|
||||
// 77.0 / 255.0,
|
||||
// 118.0 / 255.0,
|
||||
//)));
|
||||
//let material = Arc::new(Dielectric::new(2.0));
|
||||
//let material = Arc::new(Metal::new(&Color::new(0.9, 0.9, 0.7), 1.0));
|
||||
let material = Arc::new(Metal::new(&Color::new(0.5, 0.55, 0.7), 0.0));
|
||||
//let material = Arc::new(Rainbow::new());
|
||||
|
||||
let rotate_obj = 1; // rotate clockwise, 0: 0, 1: 90, 2: 180, 3: 270
|
||||
|
||||
let cornell_box = tobj::load_obj(path, &tobj::OFFLINE_RENDERING_LOAD_OPTIONS);
|
||||
let (models, materials) = cornell_box.expect("Failed to load OBJ file");
|
||||
let materials = materials.expect("Failed to load MTL file");
|
||||
|
||||
let mut new_x = 0;
|
||||
let mut new_y = 1;
|
||||
let mut new_z = 2;
|
||||
|
||||
match rotate_obj {
|
||||
1 => {
|
||||
new_x = 0;
|
||||
new_y = 2;
|
||||
new_z = 1;
|
||||
},
|
||||
2 => {
|
||||
new_x = 0;
|
||||
new_y = 2;
|
||||
new_z = 1;
|
||||
},
|
||||
3 => {
|
||||
new_x = 0;
|
||||
new_y = 2;
|
||||
new_z = 1;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
|
||||
for (i, m) in models.iter().enumerate() {
|
||||
let mesh = &m.mesh;
|
||||
|
||||
/* Mesh vector lengths
|
||||
println!("positions: {}", mesh.positions.len());
|
||||
println!("vertex_color: {}", mesh.vertex_color.len());
|
||||
println!("normals: {}", mesh.normals.len());
|
||||
println!("texcoords: {}", mesh.texcoords.len());
|
||||
println!("indices: {}", mesh.indices.len());
|
||||
println!("face_arities: {}", mesh.face_arities.len());
|
||||
println!("texcoord_indices: {}", mesh.texcoord_indices.len());
|
||||
println!("normal_indices: {}", mesh.normal_indices.len());
|
||||
*/
|
||||
|
||||
let mut next_face = 0;
|
||||
for f in 0..mesh.face_arities.len() {
|
||||
let end = next_face + mesh.face_arities[f] as usize;
|
||||
|
@ -307,17 +340,17 @@ fn from_obj(path: &str) -> HittableList {
|
|||
|
||||
let normal_avg = Vec3::unit_vector(
|
||||
Vec3::new(
|
||||
mesh.positions[3 * index_normal_a] as f64,
|
||||
mesh.positions[3 * index_normal_a + 1] as f64,
|
||||
mesh.positions[3 * index_normal_a + 2] as f64,
|
||||
mesh.positions[3 * index_normal_a + new_x] as f64,
|
||||
mesh.positions[3 * index_normal_a + new_y] as f64,
|
||||
mesh.positions[3 * index_normal_a + new_z] as f64,
|
||||
) + Vec3::new(
|
||||
mesh.positions[3 * index_normal_b] as f64,
|
||||
mesh.positions[3 * index_normal_b + 1] as f64,
|
||||
mesh.positions[3 * index_normal_b + 2] as f64,
|
||||
mesh.positions[3 * index_normal_b + new_x] as f64,
|
||||
mesh.positions[3 * index_normal_b + new_y] as f64,
|
||||
mesh.positions[3 * index_normal_b + new_z] as f64,
|
||||
) + Vec3::new(
|
||||
mesh.positions[3 * index_normal_c] as f64,
|
||||
mesh.positions[3 * index_normal_c + 1] as f64,
|
||||
mesh.positions[3 * index_normal_c + 2] as f64,
|
||||
mesh.positions[3 * index_normal_c + new_x] as f64,
|
||||
mesh.positions[3 * index_normal_c + new_y] as f64,
|
||||
mesh.positions[3 * index_normal_c + new_z] as f64,
|
||||
),
|
||||
);
|
||||
|
||||
|
@ -336,19 +369,19 @@ fn from_obj(path: &str) -> HittableList {
|
|||
|
||||
world.add(Box::<Triangle>::new(Triangle::new(
|
||||
Point3::new(
|
||||
mesh.positions[3 * index_a] as f64,
|
||||
mesh.positions[3 * index_a + 1] as f64,
|
||||
mesh.positions[3 * index_a + 2] as f64,
|
||||
mesh.positions[3 * index_a + new_x] as f64,
|
||||
mesh.positions[3 * index_a + new_y] as f64,
|
||||
mesh.positions[3 * index_a + new_z] as f64,
|
||||
),
|
||||
Point3::new(
|
||||
mesh.positions[3 * index_b] as f64,
|
||||
mesh.positions[3 * index_b + 1] as f64,
|
||||
mesh.positions[3 * index_b + 2] as f64,
|
||||
mesh.positions[3 * index_b + new_x] as f64,
|
||||
mesh.positions[3 * index_b + new_y] as f64,
|
||||
mesh.positions[3 * index_b + new_z] as f64,
|
||||
),
|
||||
Point3::new(
|
||||
mesh.positions[3 * index_c] as f64,
|
||||
mesh.positions[3 * index_c + 1] as f64,
|
||||
mesh.positions[3 * index_c + 2] as f64,
|
||||
mesh.positions[3 * index_c + new_x] as f64,
|
||||
mesh.positions[3 * index_c + new_y] as f64,
|
||||
mesh.positions[3 * index_c + new_z] as f64,
|
||||
),
|
||||
normal_avg,
|
||||
material.clone(),
|
||||
|
|
|
@ -77,7 +77,13 @@ impl Material for Metal {
|
|||
*scattered = Ray::new(rec.p, reflected + self.fuzz * Vec3::random_in_unit_sphere());
|
||||
*attenuation = self.albedo.clone();
|
||||
|
||||
return Vec3::dot(scattered.direction(), rec.normal) > 0.0;
|
||||
let reflect = Vec3::dot(r_in.direction(), rec.normal);
|
||||
//dbg!(reflect);
|
||||
|
||||
/*if reflect > 0.0 {
|
||||
*attenuation = Color::new(100.0, 0.0, 0.0);
|
||||
}*/
|
||||
return true;//reflect > 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,7 +103,7 @@ impl Material for Mirror {
|
|||
) -> bool {
|
||||
if utility::random_f64() > 0.8 {
|
||||
// Reflektiert
|
||||
let reflected = Vec3::reflect(&Vec3::unit_vector(r_in.direction()), &rec.normal);
|
||||
let reflected = Vec3::reflect(&r_in.direction(), &rec.normal);
|
||||
*scattered = Ray::new(rec.p, reflected);
|
||||
*attenuation = self.albedo.clone();
|
||||
|
||||
|
|
Loading…
Reference in a new issue