diff --git a/src/elements.rs b/src/elements.rs index d8d6a7b..edb3a9f 100644 --- a/src/elements.rs +++ b/src/elements.rs @@ -339,9 +339,24 @@ impl Intersectable for Plane { // TODO: Implement this fn texture_coords(&self, hit_point: &Vec3) -> TextureCoords { + let mut x_axis = self.normal.cross(&Vec3 { + x: 0.0, + y: 0.0, + z: 1.0, + }); + if x_axis.norm() == 0.0 { + x_axis = self.normal.cross(&Vec3 { + x: 0.0, + y: 1.0, + z: 0.0, + }); + } + let y_axis = self.normal.cross(&x_axis); + let hit_vec = *hit_point - self.pos; + TextureCoords { - x: 0.5, - y: 0.5, + x: hit_vec.dot(&x_axis) as f32, + y: hit_vec.dot(&y_axis) as f32, } } } diff --git a/src/main.rs b/src/main.rs index 607722c..222e498 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,16 +28,24 @@ use std::io::{Write,stdout}; use crossterm::{QueueableCommand,cursor,terminal,ExecutableCommand}; fn initialize_scene(camera: &mut PerspectiveCamera) { - camera.lights.push(LightSrc::new(Vec3::new(180000.0, 900.0, 300.0), 2.0)); + camera.lights.push(LightSrc::new(Vec3::new(1800.0, 900.0, 300.0), 2.0)); camera.lights.push(LightSrc::new(Vec3::new(1100.0, 800.0, 300.0), 2.0)); - let color = Color { red: 30.0, green: 30.0, blue: 30.0 }; + //let color = Color { red: 30.0, green: 30.0, blue: 30.0 }; + let back_texture_path = Path::new("texture/tiles_base.png"); + let back_height_path = Path::new("texture/tiles_height.png"); + + let back_texture_image = image::open(&back_texture_path).unwrap(); + let back_height_image = image::open(&back_height_path).unwrap(); + + let back_texture = Texture { texture: back_texture_image.clone(), heightmap: back_height_image.clone() }; + 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(Coloration::Color(color), 2.0, SurfaceType::Diffuse), + material: Material::new(Coloration::Texture(back_texture), 2.0, SurfaceType::Diffuse), normal: Vec3::new(0.0, 0.0, 1.0), }; camera.elements.push(Element::Plane(back_plane)); @@ -66,10 +74,11 @@ fn initialize_scene(camera: &mut PerspectiveCamera) { }; camera.elements.push(Element::Sphere(right_sphere)); + let sphere_color = Color { red: 60.0, green: 60.0, blue: 60.0 }; let center_sphere = Sphere { pos: Vec3::new(1280.0, 690.0, 1100.0), radius: 300.0, - material: Material::new(Coloration::Texture(base_texture.clone()), 2.0, SurfaceType::Reflective { reflectivity: 0.2 }), + material: Material::new(Coloration::Color(sphere_color), 2.0, SurfaceType::Reflective { reflectivity: 0.2 }), }; camera.elements.push(Element::Sphere(center_sphere)); } diff --git a/texture/tiles_base.png b/texture/tiles_base.png new file mode 100644 index 0000000..fe82a32 Binary files /dev/null and b/texture/tiles_base.png differ diff --git a/texture/tiles_height.png b/texture/tiles_height.png new file mode 100644 index 0000000..56ab249 Binary files /dev/null and b/texture/tiles_height.png differ