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

v2

parent afac9ba7
Branches
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
from operation_vector import * from operation_vector import Vector
class Camera: class Camera:
def __init__(self, image_nrows, image_ncols, distance_focale): def __init__(self, image_nrows, image_ncols, distance_focale):
...@@ -8,9 +8,9 @@ class Camera: ...@@ -8,9 +8,9 @@ class Camera:
def ray_at(self, row, col): def ray_at(self, row, col):
n = self.image_nrows//2 n = self.image_nrows//2
x = (row-n)/n x = (n-row)/n
n = self.image_ncols//2 n = self.image_ncols//2
y = (n-col)/n y = (n-col)/n
return Vector(x, y, self.focal_length) return Vector((x, y, self.focal_length))
...@@ -13,7 +13,10 @@ def phong_illuminate(light, position, normal, object, viewer): ...@@ -13,7 +13,10 @@ def phong_illuminate(light, position, normal, object, viewer):
R = 2*(L*N)*N - L R = 2*(L*N)*N - L
V = (viewer - position).normalized() 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): def ambiant_illuminate(position, object):
return object.material.ambient return object.material.ambiant*object.material.color
one_sphere.png

6.21 KiB

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)))
from light import phong_illuminate, ambiant_illuminate from light import phong_illuminate, ambiant_illuminate
from intersection import intersect from intersection import intersect
from operation_vector import Vector
from numpy import array, zeros
class Ray: class Ray:
def __init__(self, starting_point, direction): def __init__(self, starting_point, direction):
...@@ -17,12 +19,22 @@ def trace_ray(ray, scene, camera): ...@@ -17,12 +19,22 @@ def trace_ray(ray, scene, camera):
if distance == d: if distance == d:
class_intersect = resultat class_intersect = resultat
if distance == float('inf'): if distance == float('inf'):
return [0,0,0] return Vector([0,0,0])
I = ambiant_illuminate(class_intersect.position, class_intersect.object) color = ambiant_illuminate(class_intersect.position, class_intersect.object)
for light in scene.light_list: 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_object = class_intersect.object.material.color
color_ray = c_x, c_y, c_z = color.coord()
return [I* 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
...@@ -2,12 +2,12 @@ from operation_vector import * ...@@ -2,12 +2,12 @@ from operation_vector import *
class Sphere: class Sphere:
def __init__(self, coord, rayon, material): def __init__(self, coord, rayon, material):
self.centre = lightcoord self.centre = coord
self.radius = rayon self.radius = rayon
self.material = material self.material = material
class Material: class Material:
def __init__(color, ambiant, diffuse, specular, shininess): def __init__(self, color, ambiant, diffuse, specular, shininess):
self.color = color self.color = color
self.ambiant = ambiant self.ambiant = ambiant
self.diffuse = diffuse self.diffuse = diffuse
......
from intersection import * from scene import *
from camera import * from light import Spotlight
from raytracer import * from camera import Camera
from raytracer import raytracer_render
from matplotlib.image import imsave
s = Sphere(Vector((0,0,0)),15,1) camera = Camera(200,200,2)
r = Ray(Vector((0,0,150)),Vector((0,0,-11))) materiau_sphere = Material(Vector((0,0,1)), .01, .5, .01, 1)
i=(intersect(s,r)) 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment