Add texture support.
This commit is contained in:
47
src/main.rs
47
src/main.rs
@@ -4,25 +4,26 @@ mod camera;
|
||||
use crate::camera::PerspectiveCamera;
|
||||
|
||||
mod renderer;
|
||||
use crate::renderer::{Color,cast_ray};
|
||||
use crate::renderer::cast_ray;
|
||||
|
||||
mod materials;
|
||||
use crate::materials::{Material,SurfaceType};
|
||||
//use crate::materials::{Material,SurfaceType};
|
||||
|
||||
mod elements;
|
||||
use crate::elements::{Plane,Sphere,Element,LightSrc};
|
||||
use crate::elements::*;
|
||||
|
||||
#[macro_use]
|
||||
extern crate bmp;
|
||||
extern crate rand;
|
||||
extern crate nalgebra;
|
||||
|
||||
use std::fs::File;
|
||||
use std::path::*;
|
||||
|
||||
use rand::Rng;
|
||||
use nalgebra::*;
|
||||
use bmp::Image;
|
||||
use bmp::Pixel;
|
||||
|
||||
use std::{thread,time};
|
||||
use std::io::{Write,stdout};
|
||||
use crossterm::{QueueableCommand,cursor,terminal,ExecutableCommand};
|
||||
|
||||
@@ -39,49 +40,60 @@ fn initialize_scene(camera: &mut PerspectiveCamera) {
|
||||
let red: f32 = rng.gen::<f32>() * 100.0;
|
||||
let green: f32 = rng.gen::<f32>() * 100.0;
|
||||
let blue: f32 = rng.gen::<f32>() * 100.0;
|
||||
|
||||
let color = Color { red, green, blue };
|
||||
|
||||
let sphere = Sphere {
|
||||
pos: Vec3::new(x, y, 100.0),
|
||||
radius: radius,
|
||||
material: Material::new(Color::new(red, green, blue), 2.0, SurfaceType::Reflective { reflectivity: rng.gen::<f32>() }),
|
||||
material: Material::new(Coloration::Color(color), 2.0, SurfaceType::Reflective { reflectivity: rng.gen::<f32>() }),
|
||||
};
|
||||
camera.elements.push(Element::Sphere(sphere));
|
||||
//camera.elements.push(Element::Sphere(sphere));
|
||||
}
|
||||
|
||||
let color = Color { red: 30.0, green: 30.0, blue: 30.0 };
|
||||
|
||||
let back_plane = Plane {
|
||||
//pos: Vec3::new(0.0, 0.0, 100.0),
|
||||
pos: Vec3::new(0.0, 0.0, 1500.0),
|
||||
//color: Color::new(20.0, 20.0, 255.0),
|
||||
material: Material::new(Color::new(20.0, 20.0, 255.0), 2.0, SurfaceType::Diffuse),
|
||||
material: Material::new(Coloration::Color(color), 2.0, SurfaceType::Diffuse),
|
||||
normal: Vec3::new(0.0, 0.0, 1.0),
|
||||
};
|
||||
camera.elements.push(Element::Plane(back_plane));
|
||||
|
||||
let bottom_plane = Plane {
|
||||
pos: Vec3::new(2500.0, 0.0, 1500.0),
|
||||
//color: Color::new(20.0, 20.0, 80.0),
|
||||
material: Material::new(Color::new(20.0, 20.0, 255.0), 2.0, SurfaceType::Diffuse),
|
||||
normal: Vec3::new(0.0, 0.2, 1.0),
|
||||
};
|
||||
// let bottom_plane = Plane {
|
||||
// pos: Vec3::new(2500.0, 0.0, 1500.0),
|
||||
// //color: Coloration::Texture(texture),
|
||||
// //material: Material::new(Coloration::Texture(dummy_texture.clone()), 2.0, SurfaceType::Diffuse),
|
||||
// normal: Vec3::new(0.0, 0.2, 1.0),
|
||||
// };
|
||||
//camera.elements.push(Element::Plane(bottom_plane));
|
||||
|
||||
let path = Path::new("texture/granite_base.png");
|
||||
|
||||
let texture_image = image::open(&path).unwrap();
|
||||
|
||||
let base_texture = Texture { texture: texture_image };
|
||||
|
||||
let center_sphere = Sphere {
|
||||
pos: Vec3::new(1280.0, 1290.0, 1000.0),
|
||||
radius: 300.0,
|
||||
material: Material::new(Color::new(255.0, 255.0, 255.0), 2.0, SurfaceType::Reflective { reflectivity: 0.8 }),
|
||||
material: Material::new(Coloration::Texture(base_texture.clone()), 2.0, SurfaceType::Reflective { reflectivity: 0.1 }),
|
||||
};
|
||||
camera.elements.push(Element::Sphere(center_sphere));
|
||||
|
||||
let left_sphere = Sphere {
|
||||
pos: Vec3::new(200.0, 1800.0, 500.0),
|
||||
radius: 200.0,
|
||||
material: Material::new(Color::new(255.0, 20.0, 20.0), 2.0, SurfaceType::Reflective { reflectivity: 0.1 }),
|
||||
material: Material::new(Coloration::Texture(base_texture.clone()), 2.0, SurfaceType::Reflective { reflectivity: 0.1 }),
|
||||
};
|
||||
camera.elements.push(Element::Sphere(left_sphere));
|
||||
|
||||
let top_sphere = Sphere {
|
||||
pos: Vec3::new(1080.0, 700.0, 500.0),
|
||||
radius: 200.0,
|
||||
material: Material::new(Color::new(255.0, 20.0, 20.0), 2.0, SurfaceType::Diffuse),
|
||||
material: Material::new(Coloration::Texture(base_texture.clone()), 2.0, SurfaceType::Diffuse),
|
||||
};
|
||||
camera.elements.push(Element::Sphere(top_sphere));
|
||||
|
||||
@@ -109,7 +121,6 @@ fn main() {
|
||||
stdout.execute(cursor::Hide).unwrap();
|
||||
|
||||
stdout.write_all(format!("Progress: ").as_bytes()).unwrap();
|
||||
// TODO: Uncomment
|
||||
for (x, y) in camera.output_img.coordinates() {
|
||||
stdout.queue(cursor::SavePosition).unwrap();
|
||||
stdout.write_all(format!("{:.1}%", camera.percent_complete(y)).as_bytes()).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user