diff --git a/1.py b/1.py new file mode 100644 index 0000000000000000000000000000000000000000..849cbdb3ace4e7223d3cc472f441fe0b65f20573 --- /dev/null +++ b/1.py @@ -0,0 +1,14 @@ +from raytracer import * +from scene import * +from light import * +from camera import * + +class RenderSetup: + def __init__(self): + pass + + def read(self, filename): + f = open(filename, 'r') + string = f.read() + f.close() + return string diff --git a/__pycache__/camera.cpython-34.pyc b/__pycache__/camera.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dd826e474bf65093d2ed713bc690c7f9b6969846 Binary files /dev/null and b/__pycache__/camera.cpython-34.pyc differ diff --git a/__pycache__/intersection.cpython-34.pyc b/__pycache__/intersection.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..09c8a08fc88f9354199cbafb2f76f1706f17932c Binary files /dev/null and b/__pycache__/intersection.cpython-34.pyc differ diff --git a/__pycache__/operation_vector.cpython-34.pyc b/__pycache__/operation_vector.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..483f9b840d48d805517698b88bcdc3b8bf1b2a1b Binary files /dev/null and b/__pycache__/operation_vector.cpython-34.pyc differ diff --git a/__pycache__/raytracer.cpython-34.pyc b/__pycache__/raytracer.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..582256ac81b09f05d7a902c4757bc47e0a6d1368 Binary files /dev/null and b/__pycache__/raytracer.cpython-34.pyc differ diff --git a/__pycache__/scene.cpython-34.pyc b/__pycache__/scene.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..605d82c4b0b82ab45d83dd2e7cee91a0972dadf7 Binary files /dev/null and b/__pycache__/scene.cpython-34.pyc differ diff --git a/camera.py b/camera.py new file mode 100644 index 0000000000000000000000000000000000000000..3074d0d5b4f5188704de9de4e68845169b4f0fca --- /dev/null +++ b/camera.py @@ -0,0 +1,16 @@ +from operation_vector import * + +class Camera: + def __init__(self, image_nrows, image_ncols, distance_focale): + self.image_nrows = image_nrows + self.image_ncols = image_ncols + self.focal_length = distance_focale + + def ray_at(self, row, col): + n = self.image_nrows//2 + x = (row-n)/n + n = self.image_ncols//2 + y = (n-col)/n + return Vector(x, y, self.focal_length) + + diff --git a/intersection.py b/intersection.py new file mode 100644 index 0000000000000000000000000000000000000000..ebed3696b5d6657b3548239861136aa28590fd2f --- /dev/null +++ b/intersection.py @@ -0,0 +1,37 @@ +from operation_vector import * +from scene import * + +class Intersection: + def __init__(self, position, normal, object): + self.position = position + self.normal = normal + self.object = object + +def intersect(objet, ray): + o = ray.starting_point + l = ray.direction + if type(objet) is Sphere: + centre = objet.centre + r = objet.radius + a = l*l + diff = o-centre + b = 2*(l*diff) + c = diff*diff - r**2 + delta = b**2 - 4*a*c + if delta > 0: + intersection = [(-b - delta**0.5)/(2*a), (-b + delta**0.5)/(2*a)] + elif delta == 0: + intersection = [-b/(2*a)] + else: + intersection = [] + if len(intersection) == 0 or max(intersection) < 0: + return None + elif min(intersection) < 0: + return max(intersection) + else: + return min(intersection) + + + + + diff --git a/operation_vector.py b/operation_vector.py new file mode 100644 index 0000000000000000000000000000000000000000..d29e644624ec5fa025f736f42194bb63d6128714 --- /dev/null +++ b/operation_vector.py @@ -0,0 +1,90 @@ +class Vector: + def __init__(self, coord): + x, y, z = coord + self.x = x + self.y = y + self.z = z + + def coord(self): + return (self.x, self.y, self.z) + + def __add__(first, second): + a, b, c = first.coord() + d, e, f = second.coord() + return Vector((a + d, b + e, c + f)) + + def __rmul__(vector, item): + d, e, f = vector.coord() + return Vector((item*d, item*e, item*f)) + + def __mul__(first, second): + if type(second) == Vector: + d, e, f = first.coord() + a, b, c = second.coord() + return a*d + b*e + c*f + else: + return second*first + + def norm(self): + return (self*self)**0.5 + + def __truediv__(vector, scalar): + if scalar == 0: + return float('inf')*vector + else: + return (1/scalar)*vector + + def __sub__(vector1, vector2): + return vector1 + (-1 * vector2) + + def __repr__(self): + return 'Vector' + str(self.coord()) + + def __str__(self): + return '/ ' + str(self.x) + '\n| ' + str(self.y) + '\n\\ ' + str(self.z) + '\n' + + def __xor__(first, second): + a, b, c = first.coord() + d, e, f = second.coord() + return Vector((b*f - e*c, c*d - f*a, a*e - b*d)) + + def __neg__(self): + return -1*self + + def __pos__(self): + return self + + def __eq__(first, second): + if type(second) != Vector: + return False + a, b, c = first.coord() + d, e, f = second.coord() + return a == d and b == e and c == f + + def __iter__(self): + yield self.x + yield self.y + yield self.z + + def __getitem__(self, key): + if key == 0 or key == 'x': + return self.x + elif key == 1 or key == 'y': + return self.y + elif key == 2 or key == 'z': + return self.z + else: + raise IndexError('index out of bounds') + + def __setitem__(self, key, value): + if key == 0 or key == 'x': + self.x = value + elif key == 1 or key == 'y': + self.y = value + elif key == 2 or key == 'z': + self.z = value + else: + raise IndexError('index out of bounds') + +def base(): + return Vector((1,0,0)),Vector((0,1,0)),Vector((0,0,1)) diff --git a/raytracer.py b/raytracer.py new file mode 100644 index 0000000000000000000000000000000000000000..c831502f0b3effc6a2fab6b327d35a35aeb8a62b --- /dev/null +++ b/raytracer.py @@ -0,0 +1,7 @@ +from operation_vector import * + +class Ray: + def __init__(self, starting_point, direction): + self.starting_point = Vector(starting_point) + self.direction = Vector(direction) + diff --git a/scene.py b/scene.py new file mode 100644 index 0000000000000000000000000000000000000000..df42a528803c31caa83cf2618b3f9737206aed97 --- /dev/null +++ b/scene.py @@ -0,0 +1,14 @@ +from operation_vector import * + +class Sphere: + def __init__(self, coord, rayon, material): + self.centre = Vector(coord) + self.radius = rayon + +class Material: + def __init__(color, ambiant, diffuse, specular, shininess): + self.color = Vector(color) + self.ambiant = ambiant + self.diffuse = diffuse + self.specular = specular + self.shininess = shininess diff --git a/script.py b/script.py new file mode 100644 index 0000000000000000000000000000000000000000..4f21b3203da5692ff16f1b11656782df612fdda5 --- /dev/null +++ b/script.py @@ -0,0 +1,7 @@ +from intersection import * +from camera import * +from raytracer import * + +s = Sphere((0,0,0),15) +r = Ray((0,0,0),(0,-1,1)) +i=(intersect(s,r))