summaryrefslogtreecommitdiffstatshomepage
path: root/tests/perf_bench/misc_raytrace.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/perf_bench/misc_raytrace.py')
-rw-r--r--tests/perf_bench/misc_raytrace.py24
1 files changed, 20 insertions, 4 deletions
diff --git a/tests/perf_bench/misc_raytrace.py b/tests/perf_bench/misc_raytrace.py
index 76d4194bce..b51acaccac 100644
--- a/tests/perf_bench/misc_raytrace.py
+++ b/tests/perf_bench/misc_raytrace.py
@@ -4,6 +4,7 @@
INF = 1e30
EPS = 1e-6
+
class Vec:
def __init__(self, x, y, z):
self.x, self.y, self.z = x, y, z
@@ -30,12 +31,15 @@ class Vec:
def dot(self, rhs):
return self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
+
RGB = Vec
+
class Ray:
def __init__(self, p, d):
self.p, self.d = p, d
+
class View:
def __init__(self, width, height, depth, pos, xdir, ydir, zdir):
self.width = width
@@ -49,12 +53,14 @@ class View:
def calc_dir(self, dx, dy):
return (self.xdir * dx + self.ydir * dy + self.zdir * self.depth).normalise()
+
class Light:
def __init__(self, pos, colour, casts_shadows):
self.pos = pos
self.colour = colour
self.casts_shadows = casts_shadows
+
class Surface:
def __init__(self, diffuse, specular, spec_idx, reflect, transp, colour):
self.diffuse = diffuse
@@ -76,6 +82,7 @@ class Surface:
def transparent(colour):
return Surface(0.2, 0.9, 32, 0.0, 0.8, colour * 0.3)
+
class Sphere:
def __init__(self, surface, centre, radius):
self.surface = surface
@@ -99,6 +106,7 @@ class Sphere:
def surface_at(self, v):
return self.surface, (v - self.centre).normalise()
+
class Plane:
def __init__(self, surface, centre, normal):
self.surface = surface
@@ -116,12 +124,14 @@ class Plane:
def surface_at(self, p):
return self.surface, self.normal
+
class Scene:
def __init__(self, ambient, light, objs):
self.ambient = ambient
self.light = light
self.objs = objs
+
def trace_scene(canvas, view, scene, max_depth):
for v in range(canvas.height):
y = (-v + 0.5 * (canvas.height - 1)) * view.height / canvas.height
@@ -131,6 +141,7 @@ def trace_scene(canvas, view, scene, max_depth):
c = trace_ray(scene, ray, max_depth)
canvas.put_pix(u, v, c)
+
def trace_ray(scene, ray, depth):
# Find closest intersecting object
hit_t = INF
@@ -181,6 +192,7 @@ def trace_ray(scene, ray, depth):
return col
+
def trace_to_light(scene, ray, light_dist):
col = scene.light.colour
for obj in scene.objs:
@@ -189,6 +201,7 @@ def trace_to_light(scene, ray, light_dist):
col *= obj.surface.transp
return col
+
class Canvas:
def __init__(self, width, height):
self.width = width
@@ -202,10 +215,11 @@ class Canvas:
self.data[off + 2] = min(255, max(0, int(255 * c.z)))
def write_ppm(self, filename):
- with open(filename, 'wb') as f:
- f.write(bytes('P6 %d %d 255\n' % (self.width, self.height), 'ascii'))
+ with open(filename, "wb") as f:
+ f.write(bytes("P6 %d %d 255\n" % (self.width, self.height), "ascii"))
f.write(self.data)
+
def main(w, h, d):
canvas = Canvas(w, h)
view = View(32, 32, 64, Vec(0, 0, 50), Vec(1, 0, 0), Vec(0, 1, 0), Vec(0, 0, -1))
@@ -221,13 +235,14 @@ def main(w, h, d):
Sphere(Surface.shiny(RGB(1, 1, 1)), Vec(-5, -4, 3), 4),
Sphere(Surface.dull(RGB(0, 0, 1)), Vec(4, -5, 0), 4),
Sphere(Surface.transparent(RGB(0.2, 0.2, 0.2)), Vec(6, -1, 8), 4),
- ]
+ ],
)
trace_scene(canvas, view, scene, d)
return canvas
+
# For testing
-#main(256, 256, 4).write_ppm('rt.ppm')
+# main(256, 256, 4).write_ppm('rt.ppm')
###########################################################################
# Benchmark interface
@@ -238,5 +253,6 @@ bm_params = {
(5000, 100): (40, 40, 3),
}
+
def bm_setup(params):
return lambda: main(*params), lambda: (params[0] * params[1] * params[2], None)