Skip to content
Permalink
Browse files

[All] Use f32 over f64 values

  • Loading branch information...
k0nserv committed Dec 3, 2017
1 parent a2a8ff2 commit 03bd02facc7766a944cf4f339f5170dba80b03ef
@@ -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": []
}
@@ -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;
@@ -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<f64> for Color {
impl Mul<f32> 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)
}
}

@@ -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,
@@ -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,
},
}
@@ -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<f64>,
pub refraction_coefficient: Option<f64>,
pub reflection_coefficient: Option<f32>,
pub refraction_coefficient: Option<f32>,
}
@@ -4,12 +4,12 @@ use config::Transform;
#[serde(tag = "type")]
pub enum Object {
Sphere {
radius: f64,
radius: f32,
transforms: Option<Vec<Transform>>,
material_id: Option<usize>,
},
Plane {
normal: [f64; 3],
normal: [f32; 3],
transforms: Option<Vec<Transform>>,
material_id: Option<usize>,
},
@@ -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<Object>,
pub lights: Vec<Light>,
}
@@ -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 {
@@ -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<f64> = None;
let mut t: Option<f32> = None;
let mut hit = false;
let mut inside = false;

@@ -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(),
@@ -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,
@@ -4,19 +4,19 @@ 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,
intensity: intensity,
}
}

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
}
}

0 comments on commit 03bd02f

Please sign in to comment.
You can’t perform that action at this time.