From 03bd02facc7766a944cf4f339f5170dba80b03ef Mon Sep 17 00:00:00 2001 From: Hugo Tunius Date: Sun, 3 Dec 2017 22:04:53 +0000 Subject: [PATCH] [All] Use f32 over f64 values --- scenes/forest.json | 26 +++++++++++++++++ src/camera.rs | 26 ++++++++--------- src/color.rs | 38 ++++++++++++------------- src/config/camera.rs | 16 +++++------ src/config/light.rs | 6 ++-- src/config/material.rs | 12 ++++---- src/config/object.rs | 4 +-- src/config/scene.rs | 2 +- src/config/transform.rs | 4 +-- src/geometry/sphere.rs | 6 ++-- src/geometry/triangle.rs | 2 +- src/intersection.rs | 4 +-- src/lights/point_light.rs | 6 ++-- src/material.rs | 24 ++++++++-------- src/math/matrix4.rs | 28 +++++++++---------- src/math/mod.rs | 2 +- src/math/vector3.rs | 34 +++++++++++----------- src/mesh_loader.rs | 59 +++++++++++++++++++-------------------- src/ray.rs | 4 +-- src/renderer.rs | 36 ++++++++++++------------ src/scene.rs | 2 +- 21 files changed, 182 insertions(+), 159 deletions(-) create mode 100644 scenes/forest.json diff --git a/scenes/forest.json b/scenes/forest.json new file mode 100644 index 0000000..a40c742 --- /dev/null +++ b/scenes/forest.json @@ -0,0 +1,26 @@ +{ + "max_depth": 5, + "super_sampling": "Off", + "cameras": [{ + "fov": 1, + "width": 640, + "height": 480, + "position": [12.0, 4.0, 0.0], + "look_at": [0.0, 0.0, 0.0], + "up": [0.0, 1.0, 0.0] + }], + "scenes": [{ + "clear_color": [0.05, 0.18, 0.27], + "objects": [{ + "type": "Mesh", + "path": "forest.obj" + }], + "lights": [{ + "type": "PointLight", + "origin": [20.0, 7.0, 0.0], + "color": [0.83, 0.4, 0.04], + "intensity": 1000.0 + }] + }], + "materials": [] +} diff --git a/src/camera.rs b/src/camera.rs index cf14668..fd47af1 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -6,22 +6,22 @@ use config; pub struct Camera { pub width: u32, pub height: u32, - widthf: f64, - heightf: f64, - scale: f64, - aspect_ratio: f64, + widthf: f32, + heightf: f32, + scale: f32, + aspect_ratio: f32, camera_to_world: Matrix4, } impl Camera { - pub fn new(fov: f64, + pub fn new(fov: f32, width: u32, height: u32, position: Point3, look_at: Point3, tmp_up: Vector3) -> Camera { - let aspect_ratio = (width as f64) / (height as f64); + let aspect_ratio = (width as f32) / (height as f32); let scale = (fov * 0.5).tan(); let direction = (position - look_at).normalize(); let right = tmp_up.normalize().cross(&direction); @@ -30,8 +30,8 @@ impl Camera { Camera { width: width, height: height, - widthf: (width as f64), - heightf: (height as f64), + widthf: (width as f32), + heightf: (height as f32), scale: scale, aspect_ratio: aspect_ratio, camera_to_world: Self::camera_to_world_matrix(right.normalize(), @@ -51,21 +51,21 @@ impl Camera { } pub fn create_ray(&self, x: u32, y: u32, x_sample: u32, y_sample: u32, samples: u32) -> Ray { - let samplesf = samples as f64; + let samplesf = samples as f32; let sample_width = self.widthf * samplesf; let sample_height = self.heightf * samplesf; - let mut x_sample_offset = x_sample as f64; - let mut y_sample_offset = y_sample as f64; + let mut x_sample_offset = x_sample as f32; + let mut y_sample_offset = y_sample as f32; if samples == 1 { x_sample_offset = 0.5; y_sample_offset = 0.5; } - let px = ((2.0 * (((x * samples) as f64) + x_sample_offset) / sample_width) - 1.0) * + let px = ((2.0 * (((x * samples) as f32) + x_sample_offset) / sample_width) - 1.0) * self.aspect_ratio * self.scale; - let py = ((2.0 * (((y * samples) as f64) + y_sample_offset) / sample_height) - 1.0) * + let py = ((2.0 * (((y * samples) as f32) + y_sample_offset) / sample_height) - 1.0) * self.scale; let direction = Vector3::new(px, py, -1.0) * self.camera_to_world; diff --git a/src/color.rs b/src/color.rs index eafc43d..be557c9 100644 --- a/src/color.rs +++ b/src/color.rs @@ -51,14 +51,14 @@ impl Color { } } - pub fn new_f64(r: f64, g: f64, b: f64) -> Color { + pub fn new_f32(r: f32, g: f32, b: f32) -> Color { Color::new(Color::clamp((r * 255.0) as i32), Color::clamp((g * 255.0) as i32), Color::clamp((b * 255.0) as i32)) } - pub fn new_from_slice(slice: [f64; 3]) -> Color { - Self::new_f64(slice[0], slice[1], slice[2]) + pub fn new_from_slice(slice: [f32; 3]) -> Color { + Self::new_f32(slice[0], slice[1], slice[2]) } #[inline(always)] @@ -67,8 +67,8 @@ impl Color { } #[inline(always)] - pub fn r_f64(&self) -> f64 { - self.r() as f64 / 255.0 + pub fn r_f32(&self) -> f32 { + self.r() as f32 / 255.0 } #[inline(always)] @@ -77,8 +77,8 @@ impl Color { } #[inline(always)] - pub fn g_f64(&self) -> f64 { - self.g() as f64 / 255.0 + pub fn g_f32(&self) -> f32 { + self.g() as f32 / 255.0 } #[inline(always)] @@ -87,8 +87,8 @@ impl Color { } #[inline(always)] - pub fn b_f64(&self) -> f64 { - self.b() as f64 / 255.0 + pub fn b_f32(&self) -> f32 { + self.b() as f32 / 255.0 } #[inline(always)] @@ -139,23 +139,23 @@ impl Mul for Color { type Output = Color; fn mul(self, other: Color) -> Color { - let r = self.r_f64() * other.r_f64(); - let g = self.g_f64() * other.g_f64(); - let b = self.b_f64() * other.b_f64(); + let r = self.r_f32() * other.r_f32(); + let g = self.g_f32() * other.g_f32(); + let b = self.b_f32() * other.b_f32(); - Color::new_f64(r, g, b) + Color::new_f32(r, g, b) } } -impl Mul for Color { +impl Mul for Color { type Output = Color; - fn mul(self, other: f64) -> Color { - let r = self.r_f64() * other; - let g = self.g_f64() * other; - let b = self.b_f64() * other; + fn mul(self, other: f32) -> Color { + let r = self.r_f32() * other; + let g = self.g_f32() * other; + let b = self.b_f32() * other; - Color::new_f64(r, g, b) + Color::new_f32(r, g, b) } } diff --git a/src/config/camera.rs b/src/config/camera.rs index 57ce123..2bab9c3 100644 --- a/src/config/camera.rs +++ b/src/config/camera.rs @@ -1,20 +1,20 @@ #[derive(Deserialize, Debug)] pub struct Camera { - pub fov: f64, + pub fov: f32, pub width: u32, pub height: u32, - pub position: [f64; 3], - pub look_at: [f64; 3], - pub up: [f64; 3], + pub position: [f32; 3], + pub look_at: [f32; 3], + pub up: [f32; 3], } impl Camera { - pub fn new(fov: f64, + pub fn new(fov: f32, width: u32, height: u32, - position: [f64; 3], - look_at: [f64; 3], - up: [f64; 3]) + position: [f32; 3], + look_at: [f32; 3], + up: [f32; 3]) -> Camera { Camera { fov: fov, diff --git a/src/config/light.rs b/src/config/light.rs index 6aded59..085ad1c 100644 --- a/src/config/light.rs +++ b/src/config/light.rs @@ -2,8 +2,8 @@ #[serde(tag = "type")] pub enum Light { PointLight { - origin: [f64; 3], - color: [f64; 3], - intensity: f64, + origin: [f32; 3], + color: [f32; 3], + intensity: f32, }, } diff --git a/src/config/material.rs b/src/config/material.rs index ddd0e63..dbfeab4 100644 --- a/src/config/material.rs +++ b/src/config/material.rs @@ -2,11 +2,11 @@ use material::IllumninationModel; #[derive(Deserialize, Debug, Copy, Clone)] pub struct Material { - pub ambient_color: [f64; 3], - pub diffuse_color: [f64; 3], - pub specular_color: [f64; 3], - pub specular_exponent: f64, + pub ambient_color: [f32; 3], + pub diffuse_color: [f32; 3], + pub specular_color: [f32; 3], + pub specular_exponent: f32, pub illumination_model: IllumninationModel, - pub reflection_coefficient: Option, - pub refraction_coefficient: Option, + pub reflection_coefficient: Option, + pub refraction_coefficient: Option, } diff --git a/src/config/object.rs b/src/config/object.rs index 39edb1d..76a4559 100644 --- a/src/config/object.rs +++ b/src/config/object.rs @@ -4,12 +4,12 @@ use config::Transform; #[serde(tag = "type")] pub enum Object { Sphere { - radius: f64, + radius: f32, transforms: Option>, material_id: Option, }, Plane { - normal: [f64; 3], + normal: [f32; 3], transforms: Option>, material_id: Option, }, diff --git a/src/config/scene.rs b/src/config/scene.rs index ff25926..fb0dbbd 100644 --- a/src/config/scene.rs +++ b/src/config/scene.rs @@ -3,7 +3,7 @@ use super::light::Light; #[derive(Deserialize, Debug)] pub struct Scene { - pub clear_color: [f64; 3], + pub clear_color: [f32; 3], pub objects: Vec, pub lights: Vec, } diff --git a/src/config/transform.rs b/src/config/transform.rs index 12107c0..8789f4d 100644 --- a/src/config/transform.rs +++ b/src/config/transform.rs @@ -4,8 +4,8 @@ use math; #[derive(Deserialize, Debug)] #[serde(tag = "type")] pub enum Transform { - Translate { value: [f64; 3] }, - Scale { value: [f64; 3] }, + Translate { value: [f32; 3] }, + Scale { value: [f32; 3] }, } impl Transform { diff --git a/src/geometry/sphere.rs b/src/geometry/sphere.rs index 0cbe57d..7b98794 100644 --- a/src/geometry/sphere.rs +++ b/src/geometry/sphere.rs @@ -7,13 +7,13 @@ use material::Material; #[derive(Debug)] pub struct Sphere { pub origin: Point3, - pub radius: f64, + pub radius: f32, material: Material, } impl Sphere { - pub fn new(origin: Point3, radius: f64, material: Material) -> Sphere { + pub fn new(origin: Point3, radius: f32, material: Material) -> Sphere { Sphere { origin: origin, radius: radius, @@ -42,7 +42,7 @@ impl Intersectable for Sphere { let t1 = b + c.sqrt(); let t2 = b - c.sqrt(); - let mut t: Option = None; + let mut t: Option = None; let mut hit = false; let mut inside = false; diff --git a/src/geometry/triangle.rs b/src/geometry/triangle.rs index 6c8e821..134b6d9 100644 --- a/src/geometry/triangle.rs +++ b/src/geometry/triangle.rs @@ -89,7 +89,7 @@ impl Intersectable for Triangle { } impl Triangle { - fn normal_at_intersection(&self, u: f64, v: f64) -> Vector3 { + fn normal_at_intersection(&self, u: f32, v: f32) -> Vector3 { match self.normal { Normal::Face(normal) => normal, Normal::Vertex(n0, n1, n2) => (n0 * (1.0 - u - v) + n1 * u + n2 * v).normalize(), diff --git a/src/intersection.rs b/src/intersection.rs index 5eb127f..076f547 100644 --- a/src/intersection.rs +++ b/src/intersection.rs @@ -4,7 +4,7 @@ use geometry::Shape; #[derive(Copy, Clone)] pub struct Intersection<'a> { - pub t: f64, + pub t: f32, pub shape: &'a Shape, pub point: Point3, pub ray: Ray, @@ -13,7 +13,7 @@ pub struct Intersection<'a> { } impl<'a> Intersection<'a> { - pub fn new(t: f64, + pub fn new(t: f32, shape: &'a Shape, point: Point3, ray: Ray, diff --git a/src/lights/point_light.rs b/src/lights/point_light.rs index b76694f..18918bb 100644 --- a/src/lights/point_light.rs +++ b/src/lights/point_light.rs @@ -4,11 +4,11 @@ use color::Color; pub struct PointLight { pub origin: Point3, pub color: Color, - intensity: f64, + intensity: f32, } impl PointLight { - pub fn new(origin: Point3, color: Color, intensity: f64) -> PointLight { + pub fn new(origin: Point3, color: Color, intensity: f32) -> PointLight { PointLight { origin: origin, color: color, @@ -16,7 +16,7 @@ impl PointLight { } } - pub fn intensity(&self, distance_to_light: f64) -> f64 { + pub fn intensity(&self, distance_to_light: f32) -> f32 { 1.0 / (distance_to_light * distance_to_light) * self.intensity } } diff --git a/src/material.rs b/src/material.rs index 6d01f12..9e00fb2 100644 --- a/src/material.rs +++ b/src/material.rs @@ -51,10 +51,10 @@ pub struct Material { pub ambient_color: Color, pub diffuse_color: Color, pub specular_color: Color, - pub specular_exponent: f64, + pub specular_exponent: f32, pub illumination_model: IllumninationModel, - pub reflection_coefficient: Option, - pub refraction_coefficient: Option, + pub reflection_coefficient: Option, + pub refraction_coefficient: Option, } impl Material { @@ -74,10 +74,10 @@ impl Material { pub fn new(ambient_color: Color, diffuse_color: Color, specular_color: Color, - specular_exponent: f64, + specular_exponent: f32, illumination_model: IllumninationModel, - reflection_coefficient: Option, - refraction_coefficient: Option) + reflection_coefficient: Option, + refraction_coefficient: Option) -> Material { Material { ambient_color: ambient_color, @@ -105,10 +105,10 @@ pub struct MaterialTemplate { ambient_color: Color, diffuse_color: Color, specular_color: Color, - specular_exponent: f64, + specular_exponent: f32, illumination_model: IllumninationModel, - reflection_coefficient: Option, - refraction_coefficient: Option, + reflection_coefficient: Option, + refraction_coefficient: Option, } impl MaterialTemplate { @@ -132,10 +132,10 @@ impl MaterialTemplate { pub fn new(ambient_color: Color, diffuse_color: Color, specular_color: Color, - specular_exponent: f64, + specular_exponent: f32, illumination_model: IllumninationModel, - reflection_coefficient: Option, - refraction_coefficient: Option) + reflection_coefficient: Option, + refraction_coefficient: Option) -> MaterialTemplate { MaterialTemplate { ambient_color: ambient_color, diff --git a/src/math/matrix4.rs b/src/math/matrix4.rs index 523fdf4..d65088c 100644 --- a/src/math/matrix4.rs +++ b/src/math/matrix4.rs @@ -4,11 +4,11 @@ use math::EPSILON; #[derive(Debug, Copy, Clone)] pub struct Matrix4 { // Rows in the outer slice and columns in the inner - data: [[f64; 4]; 4], + data: [[f32; 4]; 4], } impl Matrix4 { - pub fn new(data: [[f64; 4]; 4]) -> Matrix4 { + pub fn new(data: [[f32; 4]; 4]) -> Matrix4 { Matrix4 { data: data } } @@ -75,36 +75,36 @@ impl Matrix4 { [0.0, 0.0, 0.0, 1.0]]) } - pub fn translate(x: f64, y: f64, z: f64) -> Matrix4 { + pub fn translate(x: f32, y: f32, z: f32) -> Matrix4 { Self::new([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [x, y, z, 1.0]]) } - pub fn scale(x: f64, y: f64, z: f64) -> Matrix4 { + pub fn scale(x: f32, y: f32, z: f32) -> Matrix4 { Self::new([[x, 0.0, 0.0, 0.0], [0.0, y, 0.0, 0.], [0.0, 0.0, z, 0.0], [0.0, 0.0, 0.0, 1.0]]) } - pub fn scale_uniform(scale: f64) -> Matrix4 { + pub fn scale_uniform(scale: f32) -> Matrix4 { Self::scale(scale, scale, scale) } - pub fn rot_x(theta: f64) -> Matrix4 { + pub fn rot_x(theta: f32) -> Matrix4 { Self::new([[1.0, 0.0, 0.0, 0.0], [0.0, theta.cos(), theta.sin(), 0.0], [0.0, -theta.sin(), theta.cos(), 0.0], [0.0, 0.0, 0.0, 1.0]]) } - pub fn rot_y(theta: f64) -> Matrix4 { + pub fn rot_y(theta: f32) -> Matrix4 { Self::new([[theta.cos(), 0.0, -theta.sin(), 0.0], [0.0, 1.0, 0.0, 0.0], [theta.sin(), 0.0, theta.cos(), 0.0], [0.0, 0.0, 0.0, 1.0]]) } - pub fn rot_z(theta: f64) -> Matrix4 { + pub fn rot_z(theta: f32) -> Matrix4 { Self::new([[theta.cos(), theta.sin(), 0.0, 0.0], [-theta.sin(), theta.cos(), 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], @@ -113,23 +113,23 @@ impl Matrix4 { } impl Index for Matrix4 { - type Output = [f64; 4]; + type Output = [f32; 4]; - fn index(&self, index: usize) -> &[f64; 4] { + fn index(&self, index: usize) -> &[f32; 4] { &self.data[index] } } impl Index<(usize, usize)> for Matrix4 { - type Output = f64; + type Output = f32; - fn index(&self, index: (usize, usize)) -> &f64 { + fn index(&self, index: (usize, usize)) -> &f32 { &self.data[index.0][index.1] } } impl IndexMut<(usize, usize)> for Matrix4 { - fn index_mut(&mut self, index: (usize, usize)) -> &mut f64 { + fn index_mut(&mut self, index: (usize, usize)) -> &mut f32 { &mut self.data[index.0][index.1] } } @@ -174,7 +174,7 @@ macro_rules! assert_eq_matrix4 { mod tests { use math::EPSILON; use super::Matrix4; - use std::f64::consts::PI; + use std::f32::consts::PI; #[test] fn test_identity() { diff --git a/src/math/mod.rs b/src/math/mod.rs index 226913c..71f6753 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -1,4 +1,4 @@ -pub const EPSILON: f64 = 1e-9; +pub const EPSILON: f32 = 1e-5; macro_rules! assert_eq_within_bound { ($x:expr, $y: expr, $bound: expr) => ( diff --git a/src/math/vector3.rs b/src/math/vector3.rs index b68c895..9a2111e 100644 --- a/src/math/vector3.rs +++ b/src/math/vector3.rs @@ -6,9 +6,9 @@ macro_rules! define_struct { ($T: ident) => ( #[derive(Debug, Copy, Clone, Deserialize)] pub struct $T { - pub x: f64, - pub y: f64, - pub z: f64, + pub x: f32, + pub y: f32, + pub z: f32, } ) } @@ -19,11 +19,11 @@ define_struct!(Point3); macro_rules! define_impl { ($T: ident) => ( impl $T { - pub fn new(x: f64, y: f64, z: f64) -> $T { + pub fn new(x: f32, y: f32, z: f32) -> $T { $T { x: x, y: y, z: z } } - pub fn new_from_slice(slice: [f64; 3]) -> $T { + pub fn new_from_slice(slice: [f32; 3]) -> $T { $T { x: slice[0], y: slice[1], z: slice[2] } } } @@ -35,7 +35,7 @@ define_impl!(Point3); // Vector 3 specific impl Vector3 { - pub fn dot(&self, other: &Self) -> f64 { + pub fn dot(&self, other: &Self) -> f32 { self.x * other.x + self.y * other.y + self.z * other.z } @@ -55,7 +55,7 @@ impl Vector3 { *self - *normal * (2.0 * self.dot(&normal)) } - pub fn length(&self) -> f64 { + pub fn length(&self) -> f32 { self.dot(&self).sqrt() } @@ -103,9 +103,9 @@ macro_rules! define_scalar_add { fn add(self, other: $T) -> Vector3 { Vector3 { - x: self.x + (other as f64), - y: self.y + (other as f64), - z: self.z + (other as f64), + x: self.x + (other as f32), + y: self.y + (other as f32), + z: self.z + (other as f32), } } } @@ -150,9 +150,9 @@ macro_rules! define_scalar_sub { fn sub(self, other: $T) -> Vector3 { Vector3 { - x: self.x - (other as f64), - y: self.y - (other as f64), - z: self.z - (other as f64), + x: self.x - (other as f32), + y: self.y - (other as f32), + z: self.z - (other as f32), } } } @@ -209,9 +209,9 @@ macro_rules! define_scalar_mul { fn mul(self, other: $T) -> Vector3 { Vector3 { - x: self.x * (other as f64), - y: self.y * (other as f64), - z: self.z * (other as f64), + x: self.x * (other as f32), + y: self.y * (other as f32), + z: self.z * (other as f32), } } } @@ -284,7 +284,7 @@ impl Mul for Point3 { mod tests { use math::{Matrix4, EPSILON}; use super::{Point3, Vector3}; - use std::f64::consts::PI; + use std::f32::consts::PI; #[test] diff --git a/src/mesh_loader.rs b/src/mesh_loader.rs index bd965ce..ff486d3 100644 --- a/src/mesh_loader.rs +++ b/src/mesh_loader.rs @@ -37,19 +37,16 @@ impl MeshLoader { None => IllumninationModel::DiffuseSpecular, }; - let mat = Box::new(Material::new(Color::new_f64(m.ambient[0] as f64, - m.ambient[1] as f64, - m.ambient[2] as f64), - Color::new_f64(m.diffuse[0] as f64, - m.diffuse[1] as f64, - m.diffuse[2] as f64), - Color::new_f64(m.specular[0] as f64, - m.specular[1] as f64, - m.specular[2] as f64), - m.shininess as f64, - illumination_model, - None, - Some(m.optical_density as f64))); + let mat = + Box::new(Material::new(Color::new_f32(m.ambient[0], m.ambient[1], m.ambient[2]), + Color::new_f32(m.diffuse[0], m.diffuse[1], m.diffuse[2]), + Color::new_f32(m.specular[0], + m.specular[1], + m.specular[2]), + m.shininess, + illumination_model, + None, + Some(m.optical_density))); material_cache.insert(i, mat); } @@ -78,27 +75,27 @@ impl MeshLoader { let i2 = mesh.indices[f * 3 + 2] as usize; - let p0 = Point3::new((mesh.positions[i0 * 3 + 0]) as f64, - (mesh.positions[i0 * 3 + 1]) as f64, - (mesh.positions[i0 * 3 + 2]) as f64); - let p1 = Point3::new((mesh.positions[i1 * 3 + 0]) as f64, - (mesh.positions[i1 * 3 + 1]) as f64, - (mesh.positions[i1 * 3 + 2]) as f64); - let p2 = Point3::new((mesh.positions[i2 * 3 + 0]) as f64, - (mesh.positions[i2 * 3 + 1]) as f64, - (mesh.positions[i2 * 3 + 2]) as f64); + let p0 = Point3::new(mesh.positions[i0 * 3 + 0], + mesh.positions[i0 * 3 + 1], + mesh.positions[i0 * 3 + 2]); + let p1 = Point3::new(mesh.positions[i1 * 3 + 0], + mesh.positions[i1 * 3 + 1], + mesh.positions[i1 * 3 + 2]); + let p2 = Point3::new(mesh.positions[i2 * 3 + 0], + mesh.positions[i2 * 3 + 1], + mesh.positions[i2 * 3 + 2]); let normal; if use_vertex_normals { - let n0 = Vector3::new(mesh.normals[i0 * 3 + 0] as f64, - mesh.normals[i0 * 3 + 1] as f64, - mesh.normals[i0 * 3 + 2] as f64); - let n1 = Vector3::new(mesh.normals[i1 * 3 + 0] as f64, - mesh.normals[i1 * 3 + 1] as f64, - mesh.normals[i1 * 3 + 2] as f64); - let n2 = Vector3::new(mesh.normals[i2 * 3 + 0] as f64, - mesh.normals[i2 * 3 + 1] as f64, - mesh.normals[i2 * 3 + 2] as f64); + let n0 = Vector3::new(mesh.normals[i0 * 3 + 0], + mesh.normals[i0 * 3 + 1], + mesh.normals[i0 * 3 + 2]); + let n1 = Vector3::new(mesh.normals[i1 * 3 + 0], + mesh.normals[i1 * 3 + 1], + mesh.normals[i1 * 3 + 2]); + let n2 = Vector3::new(mesh.normals[i2 * 3 + 0], + mesh.normals[i2 * 3 + 1], + mesh.normals[i2 * 3 + 2]); normal = Some(Normal::Vertex(n0, n1, n2)); } else { diff --git a/src/ray.rs b/src/ray.rs index 729e1bc..0afebd0 100644 --- a/src/ray.rs +++ b/src/ray.rs @@ -6,11 +6,11 @@ pub struct Ray { pub direction: Vector3, pub inv_direction: Vector3, pub sign: [usize; 3], - pub medium_refraction: f64, + pub medium_refraction: f32, } impl Ray { - pub fn new(origin: Point3, direction: Vector3, medium_refraction: Option) -> Ray { + pub fn new(origin: Point3, direction: Vector3, medium_refraction: Option) -> Ray { let inv_dir = Vector3::new(1.0 / direction.x, 1.0 / direction.y, 1.0 / direction.z); Ray { origin: origin, diff --git a/src/renderer.rs b/src/renderer.rs index 70dd738..72d0a57 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -22,11 +22,11 @@ pub struct Renderer<'a> { } pub struct RefractionProperties { - n1: f64, - n2: f64, - n: f64, - cos_i: f64, - c2: f64, + n1: f32, + n2: f32, + n: f32, + cos_i: f32, + c2: f32, normal: Vector3, } @@ -118,19 +118,19 @@ impl<'a> Renderer<'a> { } - let mut sum_r: f64 = 0.0; - let mut sum_g: f64 = 0.0; - let mut sum_b: f64 = 0.0; + let mut sum_r: f32 = 0.0; + let mut sum_g: f32 = 0.0; + let mut sum_b: f32 = 0.0; for color in &sample_colors { - sum_r += color.r_f64(); - sum_g += color.g_f64(); - sum_b += color.b_f64(); + sum_r += color.r_f32(); + sum_g += color.g_f32(); + sum_b += color.b_f32(); } - Color::new_f64(sum_r / sample_colors.len() as f64, - sum_g / sample_colors.len() as f64, - sum_b / sample_colors.len() as f64) + Color::new_f32(sum_r / sample_colors.len() as f32, + sum_g / sample_colors.len() as f32, + sum_b / sample_colors.len() as f32) } fn trace(&self, ray: Ray, depth: u32, cull: bool) -> Color { @@ -267,9 +267,9 @@ impl<'a> Renderer<'a> { let refraction_color = self.trace(new_ray, current_depth - 1, false); let absorbance = intersection.shape.material().ambient_color * 0.15 * -intersection.t; - let transparency = Color::new_f64(absorbance.r_f64().exp(), - absorbance.g_f64().exp(), - absorbance.b_f64().exp()); + let transparency = Color::new_f32(absorbance.r_f32().exp(), + absorbance.g_f32().exp(), + absorbance.b_f32().exp()); return refraction_color * transparency; } @@ -277,7 +277,7 @@ impl<'a> Renderer<'a> { return Color::black(); } - fn fresnel(&self, refraction_properties: &RefractionProperties) -> f64 { + fn fresnel(&self, refraction_properties: &RefractionProperties) -> f32 { let (n1, n2, cos_i) = (refraction_properties.n1, refraction_properties.n2, refraction_properties.cos_i); let sin_t = n1 / n2 * (1.0 - cos_i * cos_i).max(0.0).sqrt(); diff --git a/src/scene.rs b/src/scene.rs index 4dc39a4..5b289da 100644 --- a/src/scene.rs +++ b/src/scene.rs @@ -143,7 +143,7 @@ impl Scene { return closest_intersection; } - pub fn first_intersection(&self, ray: Ray, cull: bool, distance: f64) -> Option { + pub fn first_intersection(&self, ray: Ray, cull: bool, distance: f32) -> Option { for object in self.objects.iter() { if let Some(hit) = object.intersect(ray, cull) { if hit.t < distance {