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

v2

parent 350227b3
No related branches found
No related tags found
No related merge requests found
No preview for this file type
File added
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -27,9 +27,13 @@ def intersect(objet, ray):
if len(intersection) == 0 or max(intersection) < 0:
return None
elif min(intersection) < 0:
return max(intersection)
position = o + l*max(intersection)
else:
return min(intersection)
position = o + l*min(intersection)
if diff.norm() >= r:
return Intersection(position, (position - centre).normalized(), objet)
else:
return Intersection(position, -(position - centre).normalized(), objet)
......
light.py 0 → 100644
class Spotlight:
def __init__(self, position, color):
self.position = position
self.color = color
def phong_illuminate(light, position, normal, object, viewer):
ks = object.material.specular
kd = object.material.diffuse
alpha = object.material.shininess
L = (light.position - position).normalized()
N = normal.normalized()
R = 2*(L*N)*N - L
V = (viewer - position).normalized()
return kd*(L*N) + ks*(R*V)**alpha
def ambiant_illuminate(position, object):
return object.material.ambient
from random import random
class Vector:
def __init__(self, coord):
x, y, z = coord
......@@ -9,14 +11,19 @@ class Vector:
return (self.x, self.y, self.z)
def __add__(first, second):
if type(second) == Vector:
a, b, c = first.coord()
d, e, f = second.coord()
return Vector((a + d, b + e, c + f))
return first + second*ones()
def __rmul__(vector, item):
d, e, f = vector.coord()
return Vector((item*d, item*e, item*f))
def __radd__(vector, item):
return vector + item
def __mul__(first, second):
if type(second) == Vector:
d, e, f = first.coord()
......@@ -86,5 +93,20 @@ class Vector:
else:
raise IndexError('index out of bounds')
def __pow__(vector1, vector2):
final = []
for x,y in zip(vector1, vector2):
final.append(x*y)
return Vector(final)
def normalized(self):
return self/self.norm()
def base():
return Vector((1,0,0)),Vector((0,1,0)),Vector((0,0,1))
def rand():
return Vector([random(), random(), random()])
def ones():
return Vector((1,1,1))
from operation_vector import *
from light import phong_illuminate, ambiant_illuminate
from intersection import intersect
class Ray:
def __init__(self, starting_point, direction):
self.starting_point = Vector(starting_point)
self.direction = Vector(direction)
self.starting_point = starting_point
self.direction = direction
def trace_ray(ray, scene, camera):
intersect_list = []
distance = float('inf')
for o in scene.object_list:
resultat = intersect(o, ray)
if resultat != None:
d = (resultat.position - ray.starting_point).norm()
distance = min(distance, d)
if distance == d:
class_intersect = resultat
if distance == float('inf'):
return [0,0,0]
I = 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_object = class_intersect.object.material.color
color_ray =
return [I*
......@@ -2,13 +2,25 @@ from operation_vector import *
class Sphere:
def __init__(self, coord, rayon, material):
self.centre = Vector(coord)
self.centre = lightcoord
self.radius = rayon
self.material = material
class Material:
def __init__(color, ambiant, diffuse, specular, shininess):
self.color = Vector(color)
self.color = color
self.ambiant = ambiant
self.diffuse = diffuse
self.specular = specular
self.shininess = shininess
class Scene:
def __init__(self):
self.object_list = []
self.light_list = []
def add_object(self, o):
self.object_list.append(o)
def add_light(self, l):
self.light_list.append(l)
......@@ -2,6 +2,6 @@ from intersection import *
from camera import *
from raytracer import *
s = Sphere((0,0,0),15)
r = Ray((0,0,0),(0,-1,1))
s = Sphere(Vector((0,0,0)),15,1)
r = Ray(Vector((0,0,150)),Vector((0,0,-11)))
i=(intersect(s,r))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment