@@ -13,6 +13,7 @@ def __init__(self, scene, eyePoint, size):
1313 self .at = Point3 (.5 ,.5 ,.5 )
1414 self .scene = scene
1515 self .maxLength = 0
16+ self .pixels = [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ]
1617
1718 self .Update ()
1819
@@ -51,15 +52,50 @@ def processRay(self, ray):
5152 hit .calcLights (self .scene )
5253 return hit
5354
54- def pixelColour (self , x , y ):
55- ray = self .getRay (x , y );
56- # full scene anti alising goes here
57- hit = self .processRay (ray )
55+ def pixelColour (self , x , y , samples = 1 ):
56+ pitch = .5 / samples
57+ colour = Colour (0 ,0 ,0 )
58+ count = 0
59+ for subX in range (samples ):
60+ for subY in range (samples ):
61+ count += 1
62+ ray = self .getRay (x - .5 + (subX + 1 ) * pitch + subX * pitch ,
63+ y - .5 + (subY + 1 ) * pitch + subY * pitch )
64+ hit = self .processRay (ray )
65+ colour = colour + hit .colour ()
66+ #depth = math.log((hit.length() + 0.1), 10)
67+ #colour = colour + Colour(depth, depth, depth)
68+
69+ self .pixels [samples ] += 1
70+ return colour / count
71+
72+ def aa (self , x , y ):
73+ """Detect if the pixel x,y is at an edge, then anti-alais it"""
74+
75+ #This code is ugly, I can't work out a more sane way to do it
76+ M = self .size
77+ (p1 , p2 , p3 , p4 , p6 , p7 , p8 , p9 ) = [(0 ,0 ,0 )] * 8
78+ if x > 1 and y > 1 : p1 = self .img .getpixel ((x - 1 , y - 1 ))
79+ if y > 2 : p2 = self .img .getpixel ((x , y - 1 ))
80+ if x < M - 1 and y > 1 : p3 = self .img .getpixel ((x + 1 , y - 1 ))
81+ if x > 1 : p4 = self .img .getpixel ((x - 1 , y ))
82+ p5 = self .img .getpixel ((x , y ))
83+ if x < M - 1 : p6 = self .img .getpixel ((x + 1 , y ))
84+ if x > 1 and y > M : p7 = self .img .getpixel ((x - 1 , y + 1 ))
85+ if y > M : p8 = self .img .getpixel ((x , y + 1 ))
86+ if x > M and y > M : p9 = self .img .getpixel ((x + 1 , y + 1 ))
5887
59- depth = math .log ((hit .length () + 0.1 ), 10 )
60- colour = Colour (depth , depth , depth ).intColour ()
61- self .img .putpixel ((x , y ), colour )
62- if hit .length () > self .maxLength :
63- self .maxLength = hit .length ()
64- return colour
88+ r = abs ((p1 [0 ] + 2 * p2 [0 ] + p3 [0 ]) - (p7 [0 ] + 2 * p8 [0 ] + p9 [0 ])) + abs ((p2 [0 ] + 2 * p6 [0 ] + p9 [0 ]) - (p1 [0 ] + 2 * p4 [0 ] + p7 [0 ]))
89+ g = abs ((p1 [1 ] + 2 * p2 [1 ] + p3 [1 ]) - (p7 [1 ] + 2 * p8 [1 ] + p9 [1 ])) + abs ((p2 [1 ] + 2 * p6 [1 ] + p9 [1 ]) - (p1 [1 ] + 2 * p4 [1 ] + p7 [1 ]))
90+ b = abs ((p1 [2 ] + 2 * p2 [2 ] + p3 [2 ]) - (p7 [2 ] + 2 * p8 [2 ] + p9 [2 ])) + abs ( (p2 [2 ] + 2 * p6 [2 ] + p9 [2 ]) - (p1 [2 ] + 2 * p4 [2 ] + p7 [2 ]) )
91+
92+ sum = r + g + b
93+
94+ if sum > 300 : # We do 2 levels of AA
95+ colour = self .pixelColour (x , y , 4 ) # 16 samples per pixel
96+ return colour .intColour ()
97+ elif sum > 150 :
98+ colour = self .pixelColour (x , y , 2 ) # 4 samples per pixel
99+ return colour .intColour ()
100+ return p5
65101
0 commit comments