Add basic ray casting

This commit is contained in:
Madeline Pace
2021-12-17 11:06:23 -05:00
parent f67fbd5151
commit fde5b22063
3 changed files with 135 additions and 139 deletions

View File

@@ -2,44 +2,38 @@
extern crate bmp;
extern crate nalgebra;
use nalgebra::Pnt3;
use nalgebra::Vec3;
use bmp::Image;
use bmp::Pixel;
struct Camera {
x: f64,
y: f64,
z: f64,
pitch: f64,
yaw: f64,
plane: bmp::Image
struct Ray {
pos: nalgebra::Pnt3<f64>,
dir: nalgebra::Vec3<f64>
}
struct OrthoCamera {
x: f64,
y: f64,
z: f64,
plane: bmp::Image
}
impl Camera {
fn new(x: f64, y: f64, z: f64, pitch: f64, yaw: f64) -> Camera {
Camera {
x: x,
y: y,
z: z,
pitch: pitch,
yaw: yaw,
plane: Image::new(256,256)
impl Ray {
fn new(pos: nalgebra::Pnt3<f64>, dir: nalgebra::Vec3<f64>) -> Ray {
Ray {
pos: pos,
dir: dir
}
}
fn at(&self, t: f64) -> Pnt3<f64> {
self.pos + t * self.dir
}
}
struct OrthoCamera {
pos: nalgebra::Pnt3<f64>,
plane: bmp::Image
}
impl OrthoCamera {
fn new(x: f64, y: f64, z: f64) -> OrthoCamera {
fn new(pos: nalgebra::Pnt3<f64>) -> OrthoCamera {
OrthoCamera {
x: x,
y: y,
z: z,
pos: pos,
plane: Image::new(256,256)
}
}
@@ -53,7 +47,7 @@ struct Sphere {
}
fn main() {
let mut camera = OrthoCamera::new(0.0, 0.0, 0.0);
let mut camera = OrthoCamera::new(Pnt3::new(0.0, 0.0, 0.0));
let mut spheres = Vec::new();
spheres.push(Sphere {
x: 0.0,
@@ -67,11 +61,12 @@ fn main() {
}
let _ = camera.plane.save("img.bmp");
// let mut img = Image::new(256,256);
//
// for (x, y) in img.coordinates() {
// img.set_pixel(x, y, px!(x, y, 20));
// }
//
// let _ = img.save("img.bmp");
// Testing rays
let ray = Ray::new(Pnt3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.25, 0.8));
let result = ray.at(5.0);
println!("Result: {}", result);
}