From c89fefe27b5be7424df44afac853686f04bc49fe Mon Sep 17 00:00:00 2001 From: Tom Hargreaves Date: Sun, 5 Oct 2014 02:42:39 +0100 Subject: [PATCH] supersampling --- cube.py | 10 +++++++++- scaledcube.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 scaledcube.py diff --git a/cube.py b/cube.py index 7ad216f..0fbe5f4 100755 --- a/cube.py +++ b/cube.py @@ -124,8 +124,11 @@ def sigterm_handler(_signo, _stack_frame): help="Display framerate") ap.add_argument('-n', '--noloop', action='store_true', default=False, help="Run selected pattern(s) only once, don't loop through them") +ap.add_argument('-S', '--scale', type=int, default=1, + help="Supersampling factor for full-scene antialiasing") +ap.add_argument('-g', '--gamma', type=float, default=2.0, + help="Gamma of output device (for supersampling)") args = ap.parse_args() - debug_frames = args.frames if args.port is None: import glcube @@ -134,6 +137,11 @@ def sigterm_handler(_signo, _stack_frame): import serialcube c = serialcube.Cube(args) +if args.scale != 1: + args.realcube = c + import scaledcube + c = scaledcube.Cube(args) + if c.color: c.plasma = cubehelper.color_plasma else: diff --git a/scaledcube.py b/scaledcube.py new file mode 100644 index 0000000..265e51c --- /dev/null +++ b/scaledcube.py @@ -0,0 +1,51 @@ +# Supersampled virtual cube with gamma correction +# Tom Hargreaves +import numpy +import cubehelper + +class Cube(object): + def __init__(self, args): + self.realcube = args.realcube + self.color = self.realcube.color + size = args.size + self.realsize = size + sc = args.scale + self.scale = sc + self.size = size*sc + self.gamma = args.gamma + self.pixels = numpy.zeros((size*sc, size*sc, size*sc, 3), 'f') + + def set_pixel(self, xyz, rgb): + rgb = cubehelper.color_to_float(rgb) + self.pixels[tuple(xyz)] = rgb + + def clear(self): + self.pixels.fill(0.0) + + def single_buffer(self): + pass + + def swap(self): + pass + + def render(self): + sc = self.scale + gamma = self.gamma + igamma = 1/self.gamma + sc3 = sc * sc * sc + for x in range(0, self.realsize): + for y in range(0, self.realsize): + for z in range(0, self.realsize): + (r, g, b) = (0, 0, 0) + for xx in range(0, sc): + for yy in range(0, sc): + for zz in range(0, sc): + (rr,gg,bb) = self.pixels[x*sc+xx, y*sc+yy, z*sc+zz] + r += rr**gamma + g += gg**gamma + b += bb**gamma + self.realcube.set_pixel((x,y,z), + ((r/sc3)**igamma, + (g/sc3)**igamma, + (b/sc3)**igamma)) + self.realcube.render()