Stub program to generate a simple ppm image and define basic types.
This commit is contained in:
129
src/main.rs
Normal file
129
src/main.rs
Normal file
@@ -0,0 +1,129 @@
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate stderrlog;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
|
||||
struct Vector {
|
||||
x: f64,
|
||||
y: f64,
|
||||
z: f64,
|
||||
}
|
||||
|
||||
struct ISect {
|
||||
t: f64,
|
||||
p: Vector,
|
||||
n: Vector,
|
||||
hit: bool,
|
||||
}
|
||||
|
||||
struct Sphere {
|
||||
center: Vector,
|
||||
radius: f64,
|
||||
}
|
||||
|
||||
struct Plane {
|
||||
p: Vector,
|
||||
n: Vector,
|
||||
}
|
||||
|
||||
struct Ray {
|
||||
org: Vector,
|
||||
dir: Vector,
|
||||
}
|
||||
|
||||
struct World {
|
||||
spheres: Vec<Sphere>,
|
||||
plane: Plane,
|
||||
|
||||
width: usize,
|
||||
height: usize,
|
||||
n_subsamples: u32,
|
||||
n_aosamples: u32,
|
||||
|
||||
img: Vec<u8>,
|
||||
}
|
||||
|
||||
impl World {
|
||||
fn render(&mut self) {
|
||||
info!(
|
||||
"Rendering world {}x{} w/ {} subsampling and {} ambient occlusion sampling",
|
||||
self.width, self.height, self.n_subsamples, self.n_aosamples
|
||||
);
|
||||
for y in 0..self.height {
|
||||
for x in 0..self.width {
|
||||
self.img[(x + y * self.width) * 3 + 0] = (x % 256) as u8;
|
||||
self.img[(x + y * self.width) * 3 + 1] = (y % 256) as u8;
|
||||
self.img[(x + y * self.width) * 3 + 2] = ((x * y) % 256) as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn save(&self, path: &str) -> std::io::Result<()> {
|
||||
info!("Saving scene to {}", path);
|
||||
let mut buf = File::create(path)?;
|
||||
writeln!(&buf, "P6\n{} {}\n255", self.width, self.height)?;
|
||||
buf.write_all(&self.img)
|
||||
}
|
||||
}
|
||||
|
||||
fn init_scene() -> World {
|
||||
let w: usize = 512;
|
||||
let h: usize = 512;
|
||||
|
||||
World {
|
||||
spheres: vec![
|
||||
Sphere {
|
||||
center: Vector {
|
||||
x: -2.0,
|
||||
y: 0.0,
|
||||
z: -3.5,
|
||||
},
|
||||
radius: 0.5,
|
||||
},
|
||||
Sphere {
|
||||
center: Vector {
|
||||
x: -0.5,
|
||||
y: 0.0,
|
||||
z: -3.0,
|
||||
},
|
||||
radius: 0.5,
|
||||
},
|
||||
Sphere {
|
||||
center: Vector {
|
||||
x: 1.0,
|
||||
y: 0.0,
|
||||
z: -2.2,
|
||||
},
|
||||
radius: 0.5,
|
||||
},
|
||||
],
|
||||
plane: Plane {
|
||||
p: Vector {
|
||||
x: 0.0,
|
||||
y: -0.5,
|
||||
z: 0.0,
|
||||
},
|
||||
n: Vector {
|
||||
x: 0.0,
|
||||
y: 1.0,
|
||||
z: 0.0,
|
||||
},
|
||||
},
|
||||
width: w,
|
||||
height: h,
|
||||
n_subsamples: 2,
|
||||
n_aosamples: 8,
|
||||
|
||||
img: vec![0; w * h * 3],
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
stderrlog::new().verbosity(3).init().unwrap();
|
||||
|
||||
let mut w = init_scene();
|
||||
w.render();
|
||||
w.save("ao.ppm")
|
||||
}
|
||||
Reference in New Issue
Block a user