Skip to content
Snippets Groups Projects
Commit 14c2402d authored by Benjamin Koltes's avatar Benjamin Koltes
Browse files

fix de light.py et ajout des tests

parent 1fce1a88
Branches
No related tags found
No related merge requests found
Showing with 158 additions and 18 deletions
File added
File added
File added
File added
File added
File added
from operation_vector import Vector
from raytracer import Ray
class Camera:
def __init__(self, image_nrows, image_ncols, distance_focale):
......@@ -11,6 +12,6 @@ class Camera:
x = (n-row)/n
n = self.image_ncols//2
y = (n-col)/n
return Vector((x, y, self.focal_length))
return Ray(Vector([0,0,0]), Vector((x, y, self.focal_length)))
......@@ -13,7 +13,13 @@ def phong_illuminate(light, position, normal, object, viewer):
R = 2*(L*N)*N - L
V = (viewer - position).normalized()
i = ((kd*(L*N) + ks*(R*V)**alpha))*(N*L > 0)
if (N*L) <= 0:
return 0*position
i = ((kd*(L*N) + ks*(R*V)**alpha))
if i < 0:
i = 0
elif i > 1:
i = 1
return i*(light.color ** object.material.color)
......
one_sphere.png

6.02 KiB

......@@ -3,12 +3,12 @@ from random import random
class Vector:
def __init__(self, coord):
x, y, z = coord
self.x = x
self.y = y
self.z = z
self.x = float(x)
self.y = float(y)
self.z = float(z)
def coord(self):
return (self.x, self.y, self.z)
return (float(self.x), float(self.y), float(self.z))
def __add__(first, second):
if type(second) == Vector:
......@@ -19,7 +19,7 @@ class Vector:
def __rmul__(vector, item):
d, e, f = vector.coord()
return Vector((item*d, item*e, item*f))
return Vector((float(item*d), float(item*e), float(item*f)))
def __radd__(vector, item):
return vector + item
......
......@@ -25,7 +25,7 @@ def trace_ray(ray, scene, camera):
color += phong_illuminate(light, class_intersect.position, class_intersect.normal, class_intersect.object, Vector([0,0,0]))
color_object = class_intersect.object.material.color
c_x, c_y, c_z = color.coord()
return Vector([min(c_x,1), min(c_y,1), min(c_z,1)])
return Vector([max(min(c_x, 1), 0), max(0, min(c_y, 1)), max(0, min(c_z,1))])
def raytracer_render(camera, scene):
n_lignes = camera.image_nrows
......@@ -33,8 +33,7 @@ def raytracer_render(camera, scene):
affiche = zeros((n_lignes, n_colonnes,3))
for i in range(n_lignes):
for j in range(n_colonnes):
ray_direction = camera.ray_at(i, j)
ray = Ray(Vector((0,0,0)), ray_direction)
ray = camera.ray_at(i, j)
color = trace_ray(ray, scene, camera)
affiche[i,j,:] = color.coord()
return affiche
......@@ -7,12 +7,13 @@ class Sphere:
self.material = material
class Material:
def __init__(self, color, ambiant, diffuse, specular, shininess):
def __init__(self, color, ambiant, diffuse, specular, shininess, reflection):
self.color = color
self.ambiant = ambiant
self.diffuse = diffuse
self.specular = specular
self.shininess = shininess
self.reflection = reflection
class Scene:
def __init__(self):
......
......@@ -3,13 +3,22 @@ from light import Spotlight
from camera import Camera
from raytracer import raytracer_render
from matplotlib.image import imsave
from math import cos, sin, pi
camera = Camera(200,200,2)
materiau_sphere = Material(Vector((0,0,1)), .1, .3, .8, 20)
materiau_sphere = Material(Vector((0,0,1)), .5, .3, .7, 20, 1)
sphere = Sphere(Vector([0,0,3]), 1, materiau_sphere)
lumiere = Spotlight(Vector((1, 1, 0)), Vector((1,1,1)))
for theta in range(36):
a = 3*cos(theta*pi/18)
b = 3*sin(theta*pi/18)
lumiere = Spotlight(Vector((0, a, 3+b)), Vector((1,1,1)))
lumiere_2 = Spotlight(Vector((0, -a, 3-b)), Vector((1,1,1)))
scene = Scene()
scene.add_object(sphere)
scene.add_light(lumiere)
scene.add_light(lumiere_2)
affiche = raytracer_render(camera, scene)
imsave('one_sphere.png',affiche)
imsave('one_sphere_' + str(theta) + '.png',affiche)
print(theta)
# Show to python where to find the Camera module
import sys
sys.path.append('..')
# Import the Camera module
from camera import *
# Instantiate a Camera object
c = Camera(100, 200, 3)
# Test its members
assert(c.image_nrows == 100)
assert(c.image_ncols == 200)
assert(c.focal_length == 3)
# Show to python where to find the Camera module
import sys
sys.path.append('..')
import numpy as np
from camera import *
from operation_vector import Vector
c = Camera(100, 200, 3)
r_center = c.ray_at(c.image_nrows / 2, c.image_ncols / 2);
assert (abs(r_center.direction*Vector([0,0,1]) - r_center.direction.norm()) < 0.01, "c.ray_at(c.image_nrows / 2, c.image_ncols / 2) should have the direction [0,0,1].")
r_zero = c.ray_at(0,0);
r_zero_ref_dir = Vector([1,1,c.focal_length])
r_zero_ref_dir = r_zero_ref_dir / r_zero_ref_dir.norm()
assert abs(r_zero.direction* (r_zero_ref_dir) -
r_zero.direction.norm()) < 0.01, "c.ray_at(0,0) should have the direction [1,1,camera.focal_length].";
r_one = c.ray_at(c.image_nrows, c.image_ncols);
r_one_ref_dir = Vector([-1,-1,c.focal_length])
r_one_ref_dir = r_one_ref_dir / r_one_ref_dir.norm()
assert abs(r_one.direction*(r_one_ref_dir) -
r_one.direction.norm() < 0.01), "c.ray_at(c.nrows,c.ncols) should have the direction [-1,-1,c.focal_length.";
# Show to python where to find the modules
import sys
sys.path.append('..')
from operation_vector import Vector
from scene import *
from camera import *
from intersection import *
def is_parallel(v1, v2):
return ((v1*v2) / (v1.norm() * v2.norm()) - 1.) < 0.01
s = Sphere(Vector([0,0,3]), 1, Material(Vector([0,0,1.]), 1, 1, 1, 1, 1))
r = Ray(Vector([0,0,0]), Vector([0,0,1]))
i = intersect(s, r)
assert (i.position == Vector([0,0,2.])), "Error with a ray starting outside the sphere."
assert is_parallel(i.normal, Vector([0,0,-1.])), "Error with the normal of a ray starting outside the sphere."
r = Ray(Vector([0,0,10]), Vector([0,0,-1]))
i = intersect(s, r)
assert (i.position == Vector([0,0,4.])), "Error with a ray starting outside the sphere."
assert is_parallel(i.normal, Vector([0,0,1.])), "Error with the normal of a ray starting outside the sphere."
r = Ray(Vector([0,0,3]), Vector([0,0,-1]))
i = intersect(s, r)
assert (i.position == Vector([0,0,2.])), "Error with a ray starting inside the sphere."
assert is_parallel(i.normal, Vector([0,0,1.])), "Error with the normal of a ray starting inside the sphere."
r = Ray(Vector([0,0,5]), Vector([0,0,1]))
i = intersect(s, r)
assert i is None, "Error with a ray not intersecting the sphere: intersect should return None."
import sys
sys.path.append('..')
from operation_vector import Vector
from scene import *
m = Material(Vector([0,0,1]), 0.1, 0.2, 0.3, 0.4, 0.5);
assert((m.color == Vector([0,0,1])))
assert(m.ambiant == 0.1)
assert(m.diffuse == 0.2)
assert(m.specular == 0.3)
assert(m.shininess == 0.4)
assert(m.reflection == 0.5)
# Show to python where to find the Camera module
import sys
sys.path.append('..')
from operation_vector import Vector
# Import the Camera module
from scene import *
from light import *
s = Sphere(Vector([0,0,3]), 1, Material(Vector([0.1,0.2,0.3]), 0.1, 0.2, 0.3, 0.4, 0.5));
n = Vector([0,0,-1])
l = Spotlight(Vector([0,0,0]), Vector([0,0,1]));
p = Vector([0,0,2])
v = Vector([0,0,0])
assert((phong_illuminate(l, p, n, s, v) == Vector([0,0,0.15])))
s = Sphere(Vector([0,0,3]), 1, Material(Vector([0.1,0.2,0.3]), 0.1, 0.2, 0.3, 0.4, 0.5));
n = Vector([0,0,-1])
l = Spotlight(Vector([1,1,0]), Vector([0,0,1]));
p = Vector([0,0,2])
v = Vector([0,0,0])
assert((phong_illuminate(l, p, n, s, v) - Vector([0, 0, 0.13])).norm() < 0.01)
s = Sphere(Vector([0,0,3]), 1, Material(Vector([0.1,0.2,0.3]), 0.1, 0.2, 0.3, 0.4, 0.5));
n = Vector([0,0,-1])
l = Spotlight(Vector([0,0,10]), Vector([0,0,1]));
p = Vector([0,0,2])
v = Vector([0,0,0])
assert((phong_illuminate(l, p, n, s, v) == Vector([0,0,0])))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment