Skip to content

Commit

Permalink
Formattage version 0.8.1 ! A moi le "passing" !
Browse files Browse the repository at this point in the history
  • Loading branch information
Bycob committed Apr 3, 2017
1 parent c6fd80f commit 466957a
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 84 deletions.
4 changes: 2 additions & 2 deletions bench/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ fn write_bench_to_file(name: &str, time: f64) {
.open(&file_name)
.expect(format!("Error : can't open {} for writing benchmarks results",
file_name)
.as_str());
.as_str());
file.write_all(format!("{} was executed in: {} \n", name, time).as_bytes())
.expect(format!("Error while writing the benchmark results into {}",
file_name)
.as_str());
.as_str());


}
Expand Down
3 changes: 2 additions & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ pub fn make_average_color(colors: &[RGBA32]) -> RGBA32 {
// Calcul de la couleur moyenne

let number_of_colors = colors.len();
let squared_colors: Vec<(u64, u64, u64)> = colors.into_iter()
let squared_colors: Vec<(u64, u64, u64)> = colors
.into_iter()
.map(|color| (color.r as u64, color.g as u64, color.b as u64))
.collect();
let mut acc: (u64, u64, u64) = (0, 0, 0);
Expand Down
4 changes: 2 additions & 2 deletions src/geometry/obj3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ impl Object {
format!("Warning, the object {} is not centered in (0,0,0) but in {}",
self.name,
&barycenter)
.yellow()
.dimmed());
.yellow()
.dimmed());
// On centre l'objet à l'origine.
self.position = -barycenter;
self.apply_position();
Expand Down
7 changes: 4 additions & 3 deletions src/geometry/obj_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ pub fn open_obj(file: &str) -> Mesh {
let mut tex: Option<Vec<Vector2f>> = None;

// We clean the reader of all useless lines before iterating over it.
for line in &reader.lines()
.map(|l| l.expect("Error while reading line"))
.collect::<Vec<String>>() {
for line in &reader
.lines()
.map(|l| l.expect("Error while reading line"))
.collect::<Vec<String>>() {
let parsed_line = match parse_line(line) {
Ok(t) => t,
Err(e) => panic!(e),
Expand Down
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ use lib_render::*;
// Generate a template file at the location [path]
fn generate_template(path: &str) {
let mut scene = Scene::new_empty();
scene.world
scene
.world
.add_camera(Vector3::new(0_f32, 0_f32, 5_f32),
Vector3::new(10_f32, 0_f32, 0_f32));

scene.world
scene
.world
.add_object(Vector3::new(10_f32, 0.56_f32, 2_f32),
"models/plane_no_uv.obj".to_string(),
"Example".to_string());
Expand Down Expand Up @@ -82,9 +84,9 @@ fn parse_arg() {
// Handling the template case
if matches.opt_present("g") {
generate_template(match matches.opt_str("g") {
Some(ref path) => path,
None => "template.json",
});
Some(ref path) => path,
None => "template.json",
});
}

// Handling the case where we need to render
Expand Down
10 changes: 6 additions & 4 deletions src/material/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ impl Texture for TextureMap {
texture_registry: Option<&HashMap<String, Image<RGBA8>>>)
-> RGBA32 {

let texture = &texture_registry.unwrap()
.get(self.map_path.as_str())
.unwrap();
texture.get_pixel_at(((u.unwrap() * self.tiling_x * texture.width() as f32) as u32 %
let texture = &texture_registry
.unwrap()
.get(self.map_path.as_str())
.unwrap();
texture
.get_pixel_at(((u.unwrap() * self.tiling_x * texture.width() as f32) as u32 %
texture.width()),
((v.unwrap() * self.tiling_y * texture.height() as f32) as u32 %
texture.height()))
Expand Down
7 changes: 4 additions & 3 deletions src/material/flat_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ impl Material for FlatMaterial {
if !world.is_occluded(light_ray) {
let ray_vect = -light_ray.slope() / light_ray.slope().norm();
//let factor = cmp::max(&0.0, &ray_vect.dot_product(&frag.normal));
let factor = ray_vect.dot_product(&(frag.normal / frag.normal.norm()))
let factor = ray_vect
.dot_product(&(frag.normal / frag.normal.norm()))
.abs();
intensity += factor / light_count as f32;
}
Expand Down Expand Up @@ -126,8 +127,8 @@ impl Material for MatCap {
_: Option<&TextureRegister>)
-> RGBA32 {
let coef = (frag.normal
.dot_product(&(ray.slope() / ray.slope().norm()))
.abs() * 255f32) as u8;
.dot_product(&(ray.slope() / ray.slope().norm()))
.abs() * 255f32) as u8;

RGBA8::new(&coef, &(255u8 - coef), &coef, &255u8).to_rgba32()
}
Expand Down
16 changes: 8 additions & 8 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,15 +589,15 @@ mod tests {
};

assert!((v1 * v2).aeq(&Vector3 {
x: 0_f32,
y: 2_f32,
z: 4_f32,
}));
x: 0_f32,
y: 2_f32,
z: 4_f32,
}));
assert!((&v1 * &v2).aeq(&Vector3 {
x: 0_f32,
y: 2_f32,
z: 4_f32,
}));
x: 0_f32,
y: 2_f32,
z: 4_f32,
}));
}

#[test]
Expand Down
34 changes: 17 additions & 17 deletions src/ray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ mod tests {
});

assert!(match plane.get_intersection_fragment(&mut ray) {
None => true,
_ => false,
});
None => true,
_ => false,
});
}

#[test]
Expand All @@ -264,9 +264,9 @@ mod tests {

let intersection = plane.get_intersection_fragment(&mut ray);
assert!(match intersection {
None => true,
Some(_) => false,
});
None => true,
Some(_) => false,
});
}

#[test]
Expand All @@ -291,17 +291,17 @@ mod tests {

let intersection = plane.get_intersection_fragment(&mut ray);
assert!(match intersection {
None => false,
Some(point) => {
(point.position -
Vector3f {
x: 0.0,
y: -35.0,
z: 0.0,
})
.norm() < 0.00001
}
});
None => false,
Some(point) => {
(point.position -
Vector3f {
x: 0.0,
y: -35.0,
z: 0.0,
})
.norm() < 0.00001
}
});
}

}
33 changes: 18 additions & 15 deletions src/renderer/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ impl Renderer {
for path in texture_paths {
let path_str = String::from(path.as_str());
println!("Ajout de la texture {}", path);
textures.entry(path)
textures
.entry(path)
.or_insert_with(|| Image::<RGBA8>::read_from_file(path_str.as_str()));
}
}
Expand Down Expand Up @@ -124,18 +125,18 @@ impl Renderer {
le canvas passé en paramètres. */
pub fn calculate_rays(&self, world: &scene::World, camera: &scene::Camera, pixel: &mut Pixel) {

let objects = world.objects()
let objects = world
.objects()
.iter()
.filter(|bbox| bbox.is_visible())
.collect::<Vec<&Object>>();

for sample in &mut pixel.samples {
// On récupère le rayon à partir du sample
let mut ray: Ray =
camera.create_ray_from_sample(sample,
self.ratio,
self.res_x as f32,
self.res_y as f32);
let mut ray: Ray = camera.create_ray_from_sample(sample,
self.ratio,
self.res_x as f32,
self.res_y as f32);

// CALCUL DE LA COULEUR DU RAYON (TODO à mettre ailleurs)

Expand Down Expand Up @@ -235,12 +236,12 @@ impl Renderer {

// On passe les blocs aux threads
pool.scoped(|scope| while !blocks.is_empty() {
let block = blocks.pop().unwrap();
scope.execute(|| {
self.render_block(block, world, camera, &shared_image);
progress_bar.lock().unwrap().inc();
});
});
let block = blocks.pop().unwrap();
scope.execute(|| {
self.render_block(block, world, camera, &shared_image);
progress_bar.lock().unwrap().inc();
});
});

progress_bar.lock().unwrap().finish();

Expand All @@ -261,7 +262,8 @@ impl Renderer {
.create_sampler()
.create_samples(&mut block);

let filter = self.filter_factory.create_filter(self.res_x as u32, self.res_y as u32);
let filter = self.filter_factory
.create_filter(self.res_x as u32, self.res_y as u32);

// Emission des rayons
for pixel in block.pixels_mut() {
Expand All @@ -283,7 +285,8 @@ impl Renderer {
}

// Superposition de l'image rendue à l'image finale
shared_image.lock()
shared_image
.lock()
.unwrap()
.deref_mut()
.superpose_sub_image(Image::<RGBA32>::from_vec_vec(&temp_result),
Expand Down
12 changes: 6 additions & 6 deletions src/sampler/samplers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ impl Sampler for DefaultSampler {
for i in 0..self.sample_square_root {
for j in 0..self.sample_square_root {
result.push(Vector2f {
x: (i as f32 + 0.5) / self.sample_square_root as f32,
y: (j as f32 + 0.5) / self.sample_square_root as f32,
});
x: (i as f32 + 0.5) / self.sample_square_root as f32,
y: (j as f32 + 0.5) / self.sample_square_root as f32,
});
if result.len() >= self.sample_rate as usize {
break;
}
Expand Down Expand Up @@ -73,9 +73,9 @@ impl Sampler for HaltonSampler {
let mut result: Vec<Vector2f> = vec![];
for i in 0..self.sample_rate {
result.push(Vector2f {
x: get_halton(i, 2),
y: get_halton(i, 3),
});
x: get_halton(i, 2),
y: get_halton(i, 3),
});
}

result
Expand Down
36 changes: 18 additions & 18 deletions src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,25 +243,25 @@ mod test {
let (origin, vec1, vec2) = cam.get_canvas_base(1.0);

assert!((origin -
Vector3f {
x: 4.0,
y: 2.0,
z: 4.0 + 2.0_f32.sqrt(),
})
.norm() < 0.001);
Vector3f {
x: 4.0,
y: 2.0,
z: 4.0 + 2.0_f32.sqrt(),
})
.norm() < 0.001);
assert!((vec1 -
Vector3f {
x: -2.0,
y: 2.0,
z: 0.0,
})
.norm() < 0.001);
Vector3f {
x: -2.0,
y: 2.0,
z: 0.0,
})
.norm() < 0.001);
assert!((vec2 -
Vector3f {
x: 0.0,
y: 0.0,
z: -2.0 * 2.0_f32.sqrt(),
})
.norm() < 0.001);
Vector3f {
x: 0.0,
y: 0.0,
z: -2.0 * 2.0_f32.sqrt(),
})
.norm() < 0.001);
}
}

0 comments on commit 466957a

Please sign in to comment.