Skip to content

Commit 304b851

Browse files
author
Scott Graham Mansell
committedAug 20, 2009
Its been a while since I commited, so who knows what I've done. Anyway AA works now
1 parent 3b67af9 commit 304b851

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed
 

‎camera.py

+46-10
Original file line numberDiff line numberDiff line change
@@ -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

‎out.rgbz+edge.xcf

-60.7 KB
Binary file not shown.

‎rayCaster.py

+27-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from antiAlaising import *
44
import Image
55
import sys
6+
import time
67
from Tkinter import Tk, Canvas, PhotoImage
78
from camera import Camera
89

@@ -39,13 +40,6 @@ def putImageRow(self, row, colours):
3940
self.image.put(rowColourString, to=(0, row))
4041
self.root.update()
4142

42-
def rayColour(self, ray, scene):
43-
44-
hit = scene.intersect(ray)
45-
hit.calcReflections(scene)
46-
hit.calcLights(scene)
47-
return hit.colour()
48-
4943
# Main body. Set up an image then compute colour at each pixel
5044
def trace(self):
5145
camera = definition.camera
@@ -55,6 +49,7 @@ def trace(self):
5549
sys.stdout.flush()
5650

5751
count = 0
52+
t0 = time.clock()
5853
max = float(WIN_SIZE**2)
5954
lastPercentage = 0
6055
for row in range(WIN_SIZE):
@@ -63,15 +58,38 @@ def trace(self):
6358
count += 1
6459

6560
pixel = camera.pixelColour(col, row)
61+
camera.img.putpixel((col, row), pixel.intColour())
62+
ROW.append(pixel.intColour())
63+
percentage = (count / max * 100)
64+
self.putImageRow(row, ROW)
65+
if percentage - lastPercentage > .9:
66+
print "\b\b\b\b\b\b%4.0f%%" % percentage,
67+
sys.stdout.flush()
68+
lastPercentage = percentage
69+
print "\b\b\b\b\b\b Done (%f sec)" % (time.clock() - t0)
70+
71+
print "\tAnti-alasing... 0%",
72+
sys.stdout.flush()
73+
t0 = time.clock()
74+
count = 0
75+
lastPercentage = 0
76+
for row in range(WIN_SIZE):
77+
ROW = []
78+
for col in range(WIN_SIZE):
79+
count += 1
80+
81+
pixel = camera.aa(col, row)
82+
camera.img.putpixel((col, row), pixel)
6683
ROW.append(pixel)
6784
percentage = (count / max * 100)
6885
self.putImageRow(row, ROW)
6986
if percentage - lastPercentage > .9:
7087
print "\b\b\b\b\b\b%4.0f%%" % percentage,
7188
sys.stdout.flush()
7289
lastPercentage = percentage
73-
print "\b\b\b\b\b\b Done"
74-
print camera.maxLength
90+
print "\b\b\b\b\b\b (%f sec)" % (time.clock() - t0)
91+
92+
print camera.pixels
7593

7694
camera.img.save("out.png") # Display image in default image-viewer application
7795

‎scenes/balls2.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from CSG import *
1010
from camera import Camera
1111

12-
WIN_SIZE = 400 # Screen window size (square)
12+
WIN_SIZE = 100 # Screen window size (square)
1313

1414
SHINY_RED = Material(Colour(0.7, 0.1, 0.2), Colour(0.4,0.4,0.4), 100, .2)
1515
SHINY_BLUE = Material(Colour(0.2, 0.3, 0.7), Colour(0.8,0.8,0.8), 200, .3)
@@ -50,5 +50,6 @@
5050
camera = Camera(scene, Point3(0, 0.2, 2),WIN_SIZE)
5151
#camera.lookAt(Point3(0.5,0.2,0.3))
5252
camera.lookAt(Point3(0.5,0.3, 0.5))
53+
#camera.setFoV(90)
5354

5455

0 commit comments

Comments
 (0)
Please sign in to comment.