first gradient
This commit is contained in:
54
src/main.rs
54
src/main.rs
@@ -1,19 +1,57 @@
|
||||
use std::{io::{BufWriter, Write}, fs::File};
|
||||
pub mod vec3;
|
||||
pub mod color;
|
||||
pub mod ray;
|
||||
|
||||
const IMAGE_WIDTH: usize = 512;
|
||||
const IMAGE_HEIGHT: usize = 512;
|
||||
use std::{io::{BufWriter, Write}, fs::File};
|
||||
use crate::{
|
||||
vec3::*,
|
||||
color::*,
|
||||
ray::*
|
||||
};
|
||||
|
||||
fn ray_color(r: &Ray) -> Color {
|
||||
let unit_direction: Vec3 = r.direction().unit_vector();
|
||||
let t = 0.5 * (unit_direction.y() + 1.0);
|
||||
//println!("{}", t);
|
||||
((1.0 - t) * Color::new(1.0, 1.0, 1.0)) + (t * Color::new(0.5, 0.7, 1.0))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Image
|
||||
let aspect_ratio: f64 = 1.0 / 1.0;
|
||||
let image_width: i32 = 400;
|
||||
let image_height: i32 = (image_width as f64 / aspect_ratio as f64) as i32;
|
||||
|
||||
println!();
|
||||
// Camera
|
||||
|
||||
let viewport_height: f64 = 2.0;
|
||||
let viewport_width: f64 = aspect_ratio * viewport_height;
|
||||
let focal_length: f64 = 1.0;
|
||||
|
||||
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);
|
||||
|
||||
// Render
|
||||
|
||||
let mut file = BufWriter::new(File::create("img.ppm").expect("File creation failed"));
|
||||
|
||||
writeln!(file, "P3\n{IMAGE_WIDTH} {IMAGE_HEIGHT}\n255").expect("Error while writing Magicbyte");
|
||||
writeln!(file, "P3\n{image_width} {image_height}\n255").expect("Error while writing Magic Byte");
|
||||
|
||||
for _i in 0..IMAGE_WIDTH {
|
||||
for _j in 0..IMAGE_HEIGHT {
|
||||
writeln!(file, "234 100 255").expect("Error while writing Pixel X: {_i} Y: {_j}");
|
||||
for j in (0..image_width).rev() {
|
||||
//println!("{}", j);
|
||||
//println!("Rows remaining: {j}");
|
||||
write!(file, "\n").expect("grr");
|
||||
for i in (0..image_height).rev() {
|
||||
//println!("{}", i);
|
||||
let u: f64 = i as f64 / image_width as f64;
|
||||
let v: f64 = j as f64 / image_height as f64;
|
||||
let r = Ray::new(origin, lower_left_corner + u * horizontal + v * vertical - origin);
|
||||
|
||||
let pixel_color = ray_color(&r);
|
||||
|
||||
write_color(&mut file, pixel_color);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user