Add basic ray casting
This commit is contained in:
65
src/main.rs
65
src/main.rs
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user