Skip to content

Commit

Permalink
Texture the sphere
Browse files Browse the repository at this point in the history
  • Loading branch information
athaeryn committed May 19, 2017
1 parent 55bcef2 commit 9e06f5b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,7 +1,7 @@
satcaster
=========

![render](old_renders/1449064301.bmp)
![render](renders/texture.png)


```
Expand Down
Binary file added renders/texture.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/main.rs
Expand Up @@ -18,9 +18,9 @@ use cgmath::Vector3;

fn main() {
let sphere1 = Sphere::new(2f32, (0f32, 0f32, -5f32));
let sphere2 = Sphere::new(0.1f32, (2f32, 1f32, -5f32));
let sphere2 = Sphere::new(0.25f32, (-2.75f32, -0.5f32, -6f32));
let camera = Camera::new(0f32, 0f32, 0f32);
let light = Vector3 { x: 4f32, y: 13f32, z: 10f32 };
let light = Vector3 { x: 10f32, y: 10f32, z: 5f32 };

let scene = Scene {
camera: camera,
Expand All @@ -31,7 +31,7 @@ fn main() {
// render
let mut pixels = PixelBuffer::new(500, 500);
renderer::render(&scene, &mut pixels);
// ditherer::dither(&mut pixels);
ditherer::dither(&mut pixels);

// pixels.print_pbm();
pixels.print_pgm();
Expand Down
17 changes: 16 additions & 1 deletion src/renderer.rs
Expand Up @@ -45,6 +45,7 @@ pub fn render(scene: &Scene, pixels: &mut PixelBuffer) {
if let Some(intersection) = intersections.first() {
let light_dir = scene.light.sub(intersection.pos).normalize();
let light_ray = Ray { pos: intersection.pos, dir: light_dir };

let mut shadowed = false;
for sphere in scene.spheres.iter() {
if let Some(_) = get_intersection(&light_ray, &sphere) {
Expand All @@ -56,8 +57,20 @@ pub fn render(scene: &Scene, pixels: &mut PixelBuffer) {
pixels.set(x, y, 0);
continue;
}

let angle_to_light = light_dir.dot(intersection.normal);
let mut value = 255f32 * angle_to_light;

let from_sphere = intersection.pos.sub(intersection.sphere_pos).normalize();
let foo = from_sphere.mul(16f32);
let val: f32 = perlin.get([from_sphere.x, from_sphere.y, from_sphere.z]);
let val2: f32 = perlin.get([foo.x, foo.y, foo.z]);

let mut base = ((val + val2) * 155f32) + 200f32;
if base > 255f32 { base = 255f32 }
if base < 0f32 { base = 0f32 }

let mut value = base * angle_to_light;
if value > 255f32 { value = 255f32 }
if value < 0f32 { value = 0f32 }
pixels.set(x, y, value as i32);
} else {
Expand All @@ -78,6 +91,7 @@ fn get_intersection (ray: &Ray, sphere: &Sphere) -> Option<Intersection> {
let intersection = Intersection {
pos: hit,
normal: normal,
sphere_pos: sphere.pos,
z: t0
};

Expand Down Expand Up @@ -122,5 +136,6 @@ struct Ray {
struct Intersection {
pos: Vector3<f32>,
normal: Vector3<f32>,
sphere_pos: Vector3<f32>,
z: f32
}

0 comments on commit 9e06f5b

Please sign in to comment.