Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implemented shadows
  • Loading branch information
adusca committed Dec 27, 2014
1 parent 123a522 commit cf251ab
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
11 changes: 4 additions & 7 deletions geometry.py
Expand Up @@ -45,7 +45,7 @@ def intersection_value(self, line):
if a == 0:
return []
t = float(self.cte - line.start[2])/line.slope[2]
if t >= 0:
if t > 0.01:
return [t]
else:
return []
Expand Down Expand Up @@ -75,7 +75,7 @@ def intersection_value(self, line):
A = v.squared_norm()
B = 2*v.scalar_product(q)
C = q.squared_norm() - self.radius**2
return solve_quadradic((A, B, C))
return solve_quadradic(A, B, C)

def color_at_point(self, p):
return self.color
Expand Down Expand Up @@ -118,14 +118,11 @@ def project(self, v2):
n = float(self.scalar_product(v2))/v2.scalar_product(v2)
return v2.mul_by_number(n)

def solve_quadradic(coef):
a = coef[0]
b = coef[1]
c = coef[2]
def solve_quadradic(a, b, c):
delta = b**2 - 4*a*c
if delta < 0:
return []
ans = []
ans.append((-b - sqrt(delta))/(2.0*a))
ans.append((-b + sqrt(delta))/(2.0*a))
return filter(lambda x: x >= 0, ans)
return filter(lambda x: x > 0.01, ans)
44 changes: 28 additions & 16 deletions render.py
Expand Up @@ -14,31 +14,43 @@ def coordinates(num, sz = size):

def create_world():
S1 = Sphere((4, 4, 2), 2, (100, 220, 0))
S2 = Sphere((7, 7, 1), 1, (200, 10, 200))
S2 = Sphere((2, 2, 1), 1, (0, 100, 200))
S3 = Sphere((3, 1, 1), 1, (100, 100, 200))
P = HorizontalPlane(0, (200, 100, 200), (10, 200, 10))
P = HorizontalPlane(0, (200, 100, 200), (0, 20, 200))
return [S1, S2, S3, P]

def first_intersection(scene, line):
ans = []
for S in scene:
ts = S.intersection_value(line)
if ts:
p = line.point_after_t(ts[0])
ans.append((ts[0], S, p.coords))
if not ans:
return []
ans.sort()
return [ans[0]]

def first_intersection_color(scene, line):
ans = first_intersection(scene, line)
if not ans:
return (255, 255, 255)
to_light = first_intersection(scene, Line.through_points(ans[0][2], light))
if to_light and to_light[0][0] < 1:
return (0, 0, 0)
return ans[0][1].color_at_point(ans[0][2])

world = create_world()
camera = (3, -1, 3)
light = (0, 10, 0)
light = (3, -1, 5)

# Colouring each pixel
for i in range(size):
x0 = coordinates(i)
for j in range(size):
y0 = coordinates(j)
for k in range(size):
y0 = coordinates(k)
j = size - 1 - k
l = Line.through_points(camera, (x0, 0, y0))
ans = []
for S in world:
ts = S.intersection_value(l)
if ts:
p = l.point_after_t(ts[0])
ans.append((ts[0], S, p.coords))
if not ans:
rays[i, size - 1 - j] = (255, 255, 255)
else:
ans.sort()
rays[i, size - 1 -j] = ans[0][1].color_at_point(ans[0][2])
rays[i, j] = first_intersection_color(world, l)

tmp.save(path)
10 changes: 6 additions & 4 deletions testing.py
@@ -1,14 +1,16 @@
from geometry import *

# Testing sphere methods

def test_intersect_True():
S = Sphere((0, 0, 0), 1, (10, 200, 20))
l = Line.throughPoints((0, 1, 0), (0, 1, 1))
assert S.intersect(l)
l = Line.through_points((0, 1, 0), (0, 1, 1))
assert S.intersection_value(l) != []

def test_intersect_False():
S = Sphere((100, 100, 100), 0.1, (10, 200, 20))
l = Line.throughPoints((0, 0, 1), (0, 0, 2))
assert S.intersect(l) == False
l = Line.through_points((0, 0, 1), (0, 0, 2))
assert S.intersection_value(l) == []

# Testing vector methods

Expand Down

0 comments on commit cf251ab

Please sign in to comment.