more new stuff
This commit is contained in:
		
							parent
							
								
									87867df2d8
								
							
						
					
					
						commit
						91876b66dd
					
				
					 7 changed files with 48246 additions and 498238 deletions
				
			
		
							
								
								
									
										90004
									
								
								image2.ppm
									
									
									
									
									
								
							
							
						
						
									
										90004
									
								
								image2.ppm
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										11
									
								
								src/color.rs
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								src/color.rs
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -1,6 +1,13 @@
 | 
			
		|||
use std::io::Write;
 | 
			
		||||
use super::Color;
 | 
			
		||||
use std::io::Write;
 | 
			
		||||
 | 
			
		||||
pub fn write_color(out: &mut impl Write, pixel_color: Color) {
 | 
			
		||||
    write!(out, "{} {} {}\n", (255.999*pixel_color.x()) as u32, (255.999*pixel_color.y()) as u32, (255.999*pixel_color.z()) as u32).unwrap();
 | 
			
		||||
    write!(
 | 
			
		||||
        out,
 | 
			
		||||
        "{} {} {}\n",
 | 
			
		||||
        (255.999 * pixel_color.x()) as u32,
 | 
			
		||||
        (255.999 * pixel_color.y()) as u32,
 | 
			
		||||
        (255.999 * pixel_color.z()) as u32
 | 
			
		||||
    )
 | 
			
		||||
    .unwrap();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								src/main.rs
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -1,14 +1,14 @@
 | 
			
		|||
mod vec3;
 | 
			
		||||
mod color;
 | 
			
		||||
mod ray;
 | 
			
		||||
use vec3::Vec3;
 | 
			
		||||
use vec3::Color;
 | 
			
		||||
use vec3::Point3;
 | 
			
		||||
mod vec3;
 | 
			
		||||
use ray::Ray;
 | 
			
		||||
use std::env;
 | 
			
		||||
use std::fs::File;
 | 
			
		||||
use std::io::BufWriter;
 | 
			
		||||
use std::io::Write;
 | 
			
		||||
use vec3::Color;
 | 
			
		||||
use vec3::Point3;
 | 
			
		||||
use vec3::Vec3;
 | 
			
		||||
 | 
			
		||||
fn hit_sphere(center: &Point3, radius: f64, r: &Ray) -> f64 {
 | 
			
		||||
    let oc = r.origin() - *center;
 | 
			
		||||
| 
						 | 
				
			
			@ -37,13 +37,12 @@ fn ray_color(r: &Ray) -> Color{
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
 | 
			
		||||
    // File
 | 
			
		||||
    let mut default_file = "image.ppm";
 | 
			
		||||
 | 
			
		||||
    // Image
 | 
			
		||||
    let aspect_ratio = 16.0 / 9.0;
 | 
			
		||||
    let image_width = 800;
 | 
			
		||||
    let image_width = 400;
 | 
			
		||||
    let image_height = (image_width as f64 / aspect_ratio) as u64;
 | 
			
		||||
 | 
			
		||||
    // Camera
 | 
			
		||||
| 
						 | 
				
			
			@ -54,7 +53,8 @@ fn main() {
 | 
			
		|||
    let origin = Point3::new(0.0, 0.0, 0.0);
 | 
			
		||||
    let horizontal = Vec3::new(viewport_width, 0.0, 0.0);
 | 
			
		||||
    let vertical = Vec3::new(0.0, viewport_height, 0.0);
 | 
			
		||||
    let lower_left_corner = origin - horizontal/2.0 - vertical/2.0 - Vec3::new(0.0, 0.0, focal_length);
 | 
			
		||||
    let lower_left_corner =
 | 
			
		||||
        origin - horizontal / 2.0 - vertical / 2.0 - Vec3::new(0.0, 0.0, focal_length);
 | 
			
		||||
 | 
			
		||||
    // Render
 | 
			
		||||
    let args: Vec<String> = env::args().collect();
 | 
			
		||||
| 
						 | 
				
			
			@ -69,7 +69,10 @@ fn main() {
 | 
			
		|||
        for i in 0..image_width {
 | 
			
		||||
            let u = i as f64 / (image_width - 1) as f64;
 | 
			
		||||
            let v = j as f64 / (image_height - 1) as f64;
 | 
			
		||||
            let r = Ray::new(origin, lower_left_corner + u*horizontal + v*vertical - origin);
 | 
			
		||||
            let r = Ray::new(
 | 
			
		||||
                origin,
 | 
			
		||||
                lower_left_corner + u * horizontal + v * vertical - origin,
 | 
			
		||||
            );
 | 
			
		||||
            let pixel_color = ray_color(&r);
 | 
			
		||||
 | 
			
		||||
            color::write_color(&mut file, pixel_color);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,7 @@ use super::Vec3;
 | 
			
		|||
 | 
			
		||||
pub struct Ray {
 | 
			
		||||
    pub orig: Point3,
 | 
			
		||||
    pub dir: Vec3
 | 
			
		||||
    pub dir: Vec3,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Ray {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										22
									
								
								src/vec3.rs
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								src/vec3.rs
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -18,15 +18,15 @@ impl Vec3 {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn length_squared(&self) -> f64 {
 | 
			
		||||
        self.e[0]*self.e[0]
 | 
			
		||||
        + self.e[1]*self.e[1]
 | 
			
		||||
        + self.e[2]*self.e[2]
 | 
			
		||||
        self.e[0] * self.e[0] + self.e[1] * self.e[1] + self.e[2] * self.e[2]
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn cross(a: Vec3, b: Vec3) -> Vec3 {
 | 
			
		||||
        Vec3::new(a.y()*b.z() - a.z()*b.y(),
 | 
			
		||||
        Vec3::new(
 | 
			
		||||
            a.y() * b.z() - a.z() * b.y(),
 | 
			
		||||
            a.z() * b.x() - a.x() * b.z(),
 | 
			
		||||
            a.x()*b.y() - a.y()*b.x())
 | 
			
		||||
            a.x() * b.y() - a.y() * b.x(),
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn dot(a: Vec3, b: Vec3) -> f64 {
 | 
			
		||||
| 
						 | 
				
			
			@ -37,9 +37,15 @@ impl Vec3 {
 | 
			
		|||
        a / a.length()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn x(&self) -> f64 { self.e[0] }
 | 
			
		||||
    pub fn y(&self) -> f64 { self.e[1] }
 | 
			
		||||
    pub fn z(&self) -> f64 { self.e[2] }
 | 
			
		||||
    pub fn x(&self) -> f64 {
 | 
			
		||||
        self.e[0]
 | 
			
		||||
    }
 | 
			
		||||
    pub fn y(&self) -> f64 {
 | 
			
		||||
        self.e[1]
 | 
			
		||||
    }
 | 
			
		||||
    pub fn z(&self) -> f64 {
 | 
			
		||||
        self.e[2]
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Add for Vec3 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue