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

v1

parent 6a2eb0f3
No related branches found
No related tags found
No related merge requests found
1.py 0 → 100644
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
File added
File added
File added
File added
File added
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)
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)
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))
from operation_vector import *
class Ray:
def __init__(self, starting_point, direction):
self.starting_point = Vector(starting_point)
self.direction = Vector(direction)
scene.py 0 → 100644
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
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))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment