1
1
from geometry import *
2
2
from PIL import Image
3
3
4
- path = "./rays.png"
5
- size = 300
6
- tmp = Image .new ('RGB' , (size , size ), "white" )
7
- rays = tmp .load ()
8
-
9
- def coordinates (num , sz = size ):
4
+ def coordinates (num , sz ):
10
5
"""
11
6
Take a number in range(size) and returns the proportional value between 1 and 5
12
7
"""
13
8
return 4.0 * num / (sz - 1 ) + 1
14
9
15
10
def create_world ():
16
- S1 = Sphere ((4 , 4 , 2 ), 2 , (100 , 220 , 0 ))
11
+ S1 = Sphere ((1 , 4 , 2 ), 2 , (100 , 220 , 0 ))
17
12
S2 = Sphere ((2 , 2 , 1 ), 1 , (0 , 100 , 200 ))
18
13
S3 = Sphere ((3 , 1 , 1 ), 1 , (100 , 100 , 200 ))
19
14
P = HorizontalPlane (0 , (200 , 100 , 200 ), (0 , 20 , 200 ))
@@ -31,26 +26,36 @@ def first_intersection(scene, line):
31
26
ans .sort ()
32
27
return [ans [0 ]]
33
28
34
- def first_intersection_color (scene , line ):
29
+ def first_intersection_color (scene , line , light ):
35
30
ans = first_intersection (scene , line )
36
31
if not ans :
37
32
return (255 , 255 , 255 )
38
- to_light = first_intersection (scene , Line .through_points (ans [0 ][2 ], light ))
33
+ l = Line .through_points (ans [0 ][2 ], light )
34
+ to_light = first_intersection (scene , l )
39
35
if to_light and to_light [0 ][0 ] < 1 :
40
36
return (0 , 0 , 0 )
41
- return ans [0 ][1 ].color_at_point (ans [0 ][2 ])
37
+ a , b , c = ans [0 ][1 ].color_at_point (ans [0 ][2 ])
38
+ vector1 = Vector (l .slope ).normalize ()
39
+ vector2 = ans [0 ][1 ].normal (ans [0 ][2 ])
40
+ cos = vector1 .scalar_product (vector2 )
41
+ return (int (a * cos ), int (b * cos ), int (c * cos ))
42
+
42
43
43
- world = create_world ()
44
- camera = (3 , - 1 , 3 )
45
- light = (3 , - 1 , 5 )
44
+ def render (size ):
45
+ path = "./rays.png"
46
+ tmp = Image .new ('RGBA' , (size , size ), "white" )
47
+ rays = tmp .load ()
48
+ world = create_world ()
49
+ camera = (2 , - 1 , 3 )
50
+ light = (3 , - 1 , 10 )
51
+ # Colouring each pixel
52
+ for i in range (size ):
53
+ x0 = coordinates (i , size )
54
+ for k in range (size ):
55
+ y0 = coordinates (k , size )
56
+ j = size - 1 - k
57
+ l = Line .through_points (camera , (x0 , 0 , y0 ))
58
+ rays [i , j ] = first_intersection_color (world , l , light )
59
+ tmp .save (path )
46
60
47
- # Colouring each pixel
48
- for i in range (size ):
49
- x0 = coordinates (i )
50
- for k in range (size ):
51
- y0 = coordinates (k )
52
- j = size - 1 - k
53
- l = Line .through_points (camera , (x0 , 0 , y0 ))
54
- rays [i , j ] = first_intersection_color (world , l )
55
-
56
- tmp .save (path )
61
+ render (300 )
0 commit comments