Add texture support.

This commit is contained in:
maddiebaka
2023-06-15 11:23:32 -04:00
parent 0a9a799cb3
commit e4d92dd3e1
8 changed files with 227 additions and 131 deletions

View File

@@ -2,12 +2,10 @@
use std::f32;
use nalgebra::*;
use std::ops::{Add,Mul};
use crate::camera::*;
use crate::elements::{Element,Intersectable};
use crate::materials::SurfaceType;
use crate::elements::*;
const BLACK: Color = Color {
red: 0.0,
@@ -33,70 +31,6 @@ impl Ray {
}
}
#[derive(Copy, Clone)]
pub struct Color {
pub red: f32,
pub green: f32,
pub blue: f32,
}
impl Color {
pub fn new(red: f32, green: f32, blue: f32) -> Color {
Color {
red: red,
green: green,
blue: blue
}
}
}
impl Mul for Color {
type Output = Color;
fn mul(self, other: Color) -> Color {
Color {
red: self.red * other.red,
green: self.green * other.green,
blue: self.blue * other.blue,
}
}
}
impl Mul<f32> for Color {
type Output = Color;
fn mul(self, other: f32) -> Color {
Color {
red: self.red * other,
green: self.green * other,
blue: self.blue * other,
}
}
}
impl Add for Color {
type Output = Color;
fn add(self, other: Color) -> Color {
Color {
red: self.red + other.red,
green: self.green + other.green,
blue: self.blue + other.blue,
}
}
}
impl Mul<Color> for f32 {
type Output = Color;
fn mul(self, other: Color) -> Color {
other * self
}
}
pub struct Intersection<'a> {
pub distance: f64,
pub object: &'a Element
@@ -111,15 +45,6 @@ impl<'a> Intersection<'a> {
}
}
impl Intersectable for Element {
fn intersect(&self, ray: &Ray) -> Option<f64> {
match *self {
Element::Sphere(ref s) => s.intersect(ray),
Element::Plane(ref p) => p.intersect(ray),
}
}
}
fn create_reflection(normal: Vec3<f64>, incident: Vec3<f64>, hit_point: Vec3<f64>, bias: f64) -> Ray {
Ray {
@@ -155,6 +80,7 @@ fn shade_diffuse(camera: &PerspectiveCamera, object: &Element, hit_point: Vec3<f
let material = object.material();
// TODO: Change light intensity to take hit_point for some reason (read source)
// https://github.com/bheisler/raytracer/blob/7130556181de7fc59eaa29346f5d4134db3e720e/src/rendering.rs#L195
let texture_coords = object.texture_coords(&hit_point);
// Shadow stuff
let shadow_ray = Ray {
@@ -171,7 +97,7 @@ fn shade_diffuse(camera: &PerspectiveCamera, object: &Element, hit_point: Vec3<f
let light_reflected = material.albedo / f32::consts::PI;
let light_color = light_intensity * light_power * light_reflected;
color = color + (material.coloration * light_color);
color = color + (material.coloration.color(&texture_coords) * light_color);
}
return color;