Add plane texture mapping

This commit is contained in:
maddiebaka
2023-06-17 01:36:49 -04:00
parent 04b166ac72
commit a9f503fdf4
4 changed files with 30 additions and 6 deletions

View File

@@ -339,9 +339,24 @@ impl Intersectable for Plane {
// TODO: Implement this // TODO: Implement this
fn texture_coords(&self, hit_point: &Vec3<f64>) -> TextureCoords { fn texture_coords(&self, hit_point: &Vec3<f64>) -> 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 { TextureCoords {
x: 0.5, x: hit_vec.dot(&x_axis) as f32,
y: 0.5, y: hit_vec.dot(&y_axis) as f32,
} }
} }
} }

View File

@@ -28,16 +28,24 @@ use std::io::{Write,stdout};
use crossterm::{QueueableCommand,cursor,terminal,ExecutableCommand}; use crossterm::{QueueableCommand,cursor,terminal,ExecutableCommand};
fn initialize_scene(camera: &mut PerspectiveCamera) { 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)); 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 { let back_plane = Plane {
//pos: Vec3::new(0.0, 0.0, 100.0), //pos: Vec3::new(0.0, 0.0, 100.0),
pos: Vec3::new(0.0, 0.0, 1500.0), pos: Vec3::new(0.0, 0.0, 1500.0),
//color: Color::new(20.0, 20.0, 255.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), normal: Vec3::new(0.0, 0.0, 1.0),
}; };
camera.elements.push(Element::Plane(back_plane)); camera.elements.push(Element::Plane(back_plane));
@@ -66,10 +74,11 @@ fn initialize_scene(camera: &mut PerspectiveCamera) {
}; };
camera.elements.push(Element::Sphere(right_sphere)); camera.elements.push(Element::Sphere(right_sphere));
let sphere_color = Color { red: 60.0, green: 60.0, blue: 60.0 };
let center_sphere = Sphere { let center_sphere = Sphere {
pos: Vec3::new(1280.0, 690.0, 1100.0), pos: Vec3::new(1280.0, 690.0, 1100.0),
radius: 300.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)); camera.elements.push(Element::Sphere(center_sphere));
} }

BIN
texture/tiles_base.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 MiB

BIN
texture/tiles_height.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB