diff --git a/__pycache__/camera.cpython-34.pyc b/__pycache__/camera.cpython-34.pyc index dd826e474bf65093d2ed713bc690c7f9b6969846..792f368305503a061e7064d9fe99759a111bce47 100644 Binary files a/__pycache__/camera.cpython-34.pyc and b/__pycache__/camera.cpython-34.pyc differ diff --git a/__pycache__/intersection.cpython-34.pyc b/__pycache__/intersection.cpython-34.pyc index ce45d81abd4216de5f6724547e34233a307f722a..48d1d03c1c4b208c1a94e28030de92106bd258d7 100644 Binary files a/__pycache__/intersection.cpython-34.pyc and b/__pycache__/intersection.cpython-34.pyc differ diff --git a/__pycache__/light.cpython-34.pyc b/__pycache__/light.cpython-34.pyc index b02808540f9d300cadbe77fbc522f8dab99b25c5..2c5c24287247609d3dab3d207689a77f08f299d3 100644 Binary files a/__pycache__/light.cpython-34.pyc and b/__pycache__/light.cpython-34.pyc differ diff --git a/__pycache__/operation_vector.cpython-34.pyc b/__pycache__/operation_vector.cpython-34.pyc index 0a79b5b1543fbe633d1c9a6b7a13dfe5b2dd286f..b4fddc08c6eb3ea624590739f481c8628ba886bb 100644 Binary files a/__pycache__/operation_vector.cpython-34.pyc and b/__pycache__/operation_vector.cpython-34.pyc differ diff --git a/__pycache__/raytracer.cpython-34.pyc b/__pycache__/raytracer.cpython-34.pyc index a55b6635259f896d52b898063ac511914cad7b30..541ae3bafce63d5351a9328de18fffded84bcccf 100644 Binary files a/__pycache__/raytracer.cpython-34.pyc and b/__pycache__/raytracer.cpython-34.pyc differ diff --git a/__pycache__/scene.cpython-34.pyc b/__pycache__/scene.cpython-34.pyc index 5ab218016de0b0e358a757fb5370f11787373659..958548fa5bf8dc8b725d4a0da95a25b99990d1b9 100644 Binary files a/__pycache__/scene.cpython-34.pyc and b/__pycache__/scene.cpython-34.pyc differ diff --git a/camera.py b/camera.py index 3074d0d5b4f5188704de9de4e68845169b4f0fca..a4e5e2347fbc09707b6870d2133024208fc041d9 100644 --- a/camera.py +++ b/camera.py @@ -1,4 +1,4 @@ -from operation_vector import * +from operation_vector import Vector class Camera: def __init__(self, image_nrows, image_ncols, distance_focale): @@ -8,9 +8,9 @@ class Camera: def ray_at(self, row, col): n = self.image_nrows//2 - x = (row-n)/n + x = (n-row)/n n = self.image_ncols//2 y = (n-col)/n - return Vector(x, y, self.focal_length) + return Vector((x, y, self.focal_length)) diff --git a/light.py b/light.py index cacff891e754c20068a63cbbbe47a15c0a22139a..38106c2596341c6d294c46b0c2b2ba5c593f16ce 100644 --- a/light.py +++ b/light.py @@ -13,7 +13,10 @@ def phong_illuminate(light, position, normal, object, viewer): R = 2*(L*N)*N - L V = (viewer - position).normalized() - return kd*(L*N) + ks*(R*V)**alpha + i = kd*(L*N) + ks*(R*V)**alpha + + return i*(light.color ** object.material.color) def ambiant_illuminate(position, object): - return object.material.ambient + return object.material.ambiant*object.material.color + diff --git a/one_sphere.png b/one_sphere.png new file mode 100644 index 0000000000000000000000000000000000000000..7aed7ee4f5582aad31f2be17bef7755f16ff7f32 Binary files /dev/null and b/one_sphere.png differ diff --git a/one_sphere.py b/one_sphere.py new file mode 100644 index 0000000000000000000000000000000000000000..3925b79c8a50eb44b9a594f2ae229c5b2b4e5218 --- /dev/null +++ b/one_sphere.py @@ -0,0 +1,6 @@ +from scene import * +from light import Spotlight + +materiau_sphere = Material(Vector((0,0,1)), .2, .2, .2, .2) +sphere = Sphere(Vector([0,0,3]), 1, materiau_sphere) +lumiere = Spotlight(Vector((1,1,0)), Vector((1,1,1))) diff --git a/raytracer.py b/raytracer.py index 0f1a436da8ca87c5212b3a5c19cfa1341d516c05..4b746b31408141b2d1830ff7ddbcb2636311af38 100644 --- a/raytracer.py +++ b/raytracer.py @@ -1,5 +1,7 @@ from light import phong_illuminate, ambiant_illuminate from intersection import intersect +from operation_vector import Vector +from numpy import array, zeros class Ray: def __init__(self, starting_point, direction): @@ -17,12 +19,22 @@ def trace_ray(ray, scene, camera): if distance == d: class_intersect = resultat if distance == float('inf'): - return [0,0,0] - I = ambiant_illuminate(class_intersect.position, class_intersect.object) + return Vector([0,0,0]) + color = ambiant_illuminate(class_intersect.position, class_intersect.object) for light in scene.light_list: - I += phong_illuminate(light, class_intersect.position, class_intersect.normal, class_intersect.object, camera.position) + color += phong_illuminate(light, class_intersect.position, class_intersect.normal, class_intersect.object, Vector([0,0,0])) color_object = class_intersect.object.material.color - color_ray = - return [I* - - + c_x, c_y, c_z = color.coord() + return Vector([min(c_x,1), min(c_y,1), min(c_z,1)]) + +def raytracer_render(camera, scene): + n_lignes = camera.image_nrows + n_colonnes = camera.image_ncols + 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) + color = trace_ray(ray, scene, camera) + affiche[i,j,:] = color.coord() + return affiche diff --git a/scene.py b/scene.py index fffa1edf07670d6a22625b9670519f37dd18dc82..ff44e7be4a0772fe083cf3b3c28253869ae5b527 100644 --- a/scene.py +++ b/scene.py @@ -2,12 +2,12 @@ from operation_vector import * class Sphere: def __init__(self, coord, rayon, material): - self.centre = lightcoord + self.centre = coord self.radius = rayon self.material = material class Material: - def __init__(color, ambiant, diffuse, specular, shininess): + def __init__(self, color, ambiant, diffuse, specular, shininess): self.color = color self.ambiant = ambiant self.diffuse = diffuse diff --git a/script.py b/script.py index eb610274d7f49b5f4868d20c1de966b80331ad9d..a15d386ef67930661098fcba0107b7e4b37e2e51 100644 --- a/script.py +++ b/script.py @@ -1,7 +1,15 @@ -from intersection import * -from camera import * -from raytracer import * +from scene import * +from light import Spotlight +from camera import Camera +from raytracer import raytracer_render +from matplotlib.image import imsave -s = Sphere(Vector((0,0,0)),15,1) -r = Ray(Vector((0,0,150)),Vector((0,0,-11))) -i=(intersect(s,r)) +camera = Camera(200,200,2) +materiau_sphere = Material(Vector((0,0,1)), .01, .5, .01, 1) +sphere = Sphere(Vector([0,0,3]), 1, materiau_sphere) +lumiere = Spotlight(Vector((1,1,0)), Vector((1,1,1))) +scene = Scene() +scene.add_object(sphere) +scene.add_light(lumiere) +affiche = raytracer_render(camera, scene) +imsave('one_sphere.png',affiche)