Example #1
0
def render_frame(frame_no, frame_count):
    from handsome.util import save_array_as_image

    time = 2 * frame_no / (frame_count - 1)
    if time > 1.:
        time = 2 - time

    set_time = load_sampler_lib()['set_time']
    set_time(time)

    canvas = make_canvas({
        'extents' : (512, 512),
        'color'   : '#fff',
    })

    surface = generate_surface()
    shader = generate_shader()

    mesh = generate_mesh(surface, shader)
    meshes = subdivide_mesh(mesh)

    for mesh in meshes:
        cache = render_mesh(mesh)
        cache.composite_into(canvas)

    buffer = array_view(canvas.downsample(1))
    buffer = np.clip(buffer, 0., 1.)
    buffer = (255 * buffer).astype(np.uint8)

    frame_path = 'render/008/frame_{:03}.tiff'.format(frame_no)
    save_array_as_image(pixel_view(buffer), frame_path, 'RGBA')

    return frame_path
Example #2
0
def main():
    colors = {
        'red'   : '#f00',
        'white' : '#fff',
    }

    float_colors = { }

    for key, value in colors.items():
        color = parse_color(value)
        colors[key] = np.array([ color ], dtype=Pixel)
        float_colors[key] = np.array(tuple(c / 255. for c in color), dtype=FloatPixel)

    image = Tile((0, 0), (512, 512), sample_rate=4, dtype=FloatPixel)
    image.buffer[:,:] = float_colors['white']

    tile_width, tile_height = image.buffer.shape

    mesh = MicropolygonMesh((1,1))
    mesh_width, mesh_height = mesh.buffer.shape

    margin = 16
    width = 128
    right  = image.shape[0]
    top    = image.shape[1]


    buffer = np.array([
            [
                (right - margin - width, top - margin, 1, 1, 1.5, 0, 0, 1),
                (right - margin        , top - margin, 1, 1, 0  , 0, 0, 1)
            ],
            [
                (margin        , margin, 1, 1, .5, 0, 0, 1),
                (margin + width, margin, 1, 1, 0 , 0, 0, 1)
            ],
        ],
        dtype=np.float32
    )

    mesh.buffer.view(dtype=(np.float32, 8))[:] = buffer

    fill_micropolygon_mesh(
        mesh_width, mesh_height,
        generate_numpy_begin(mesh.buffer),
        tile_width, tile_height,
        generate_numpy_begin(image.coordinate_image),
        generate_numpy_begin(image.buffer)
    )

    buffer = array_view(image.downsample(1))
    buffer = np.clip(buffer, 0., 1.)
    buffer = (255. * buffer).astype(dtype=np.uint8)

    save_array_as_image(pixel_view(buffer), 'render/002_micropolygon.tiff', 'RGBA')
Example #3
0
def generate_texture():
    from handsome.util import save_array_as_image

    scene = generate_texture_scene()
    canvas = render_scene(scene)

    buffer = array_view(canvas.downsample(1))
    buffer = np.clip(buffer, 0., 1.)
    buffer = (255. * buffer).astype(dtype=np.uint8)

    save_array_as_image(pixel_view(buffer), 'render/texture.tiff', 'RGBA')
Example #4
0
def render_image(scene_path, out_path):
    from handsome.util import save_array_as_image

    scene = parse_scene(scene_path)
    canvas = render_scene(scene)

    buffer = array_view(canvas.downsample(1))
    buffer = np.clip(buffer, 0., 1.)
    buffer = (255. * buffer).astype(dtype=np.uint8)

    save_array_as_image(pixel_view(buffer), out_path, 'RGBA')
Example #5
0
def main():
    from handsome.opencl_api import fill_micropolygon_mesh
    from handsome.util import save_array_as_image, parse_color

    mesh = generate_mesh()
    tile = Tile((0, 0), (512, 512), 4, dtype=FloatPixel)

    fill_micropolygon_mesh(mesh, tile)

    buffer = array_view(tile.downsample(1))
    # buffer = array_view(tile.buffer)
    buffer = np.clip(buffer, 0., 1.)
    buffer = (255 * buffer).astype(np.uint8)

    save_array_as_image(pixel_view(buffer), 'render/009_opencl.tiff', 'RGBA')
Example #6
0
def test_tile_intersection_slices():
    master = Tile((0, 0), (640, 400))
    tile = Tile((100, 100), (100, 300))

    blue = np.array([(0, 0, 153, 255)], dtype=Pixel)
    red = np.array([(153, 0, 0, 255)], dtype=Pixel)
    white = np.array([(255, 255, 255, 255)], dtype=Pixel)

    master.buffer[::] = white
    tile.buffer[::] = red

    m, t = master.intersection_slices(tile)
    master.buffer[m] = tile.buffer[t]

    save_array_as_image('test_tile_2.tiff', master.buffer, 'RGBA')
Example #7
0
def render_frame(frame_no):
    print('rendering frame {}'.format(frame_no))

    image = Tile((0, 0), (512,512), 4, dtype=FloatPixel)
    image.buffer[:,:] = palette['white'].view(dtype=FloatPixel)

    center = (256, 256)
    radius = 192

    mesh = star(center, radius)

    cache = render_mesh(mesh)
    cache.composite_into(image)

    buffer = array_view(image.downsample(1))
    buffer = np.clip(buffer, 0., 1.)
    buffer = (255. * buffer).astype(dtype=np.uint8)

    path = 'render/004/frame_{:03}.tiff'.format(frame_no)
    save_array_as_image(pixel_view(buffer), path, 'RGBA')
Example #8
0
def main():
    from handsome.util import save_array_as_image

    canvas = make_canvas({
        'extents' : (512, 512),
        'color'   : '#fff',
    })

    surface = generate_surface()
    shader = generate_texture_shader()

    mesh = generate_mesh(surface, shader)

    cache = render_mesh(mesh)
    cache.composite_into(canvas)

    buffer = array_view(canvas.downsample(1))
    buffer = np.clip(buffer, 0., 1.)
    buffer = (255 * buffer).astype(np.uint8)

    save_array_as_image(pixel_view(buffer), 'render/006_texture.tiff', 'RGBA')
Example #9
0
def render_tile():
    import random

    mode = 'RGBA'
    image_shape = (640, 480)

    image = Tile((0,0), image_shape)

    r = random.SystemRandom()

    colors = {
        'white'  : '#ffff',
        'black'  : '#000f',
        '205A8C' : '205A8C',
        '6596BF' : '6596BF',
        '98F2EC' : '98F2EC',
        'BF6865' : 'BF6865',
        'F4DBD6' : 'F4DBD6',
    }

    for name, color in colors.items():
        colors[name] = parse_color(color)

    image.buffer[:,:] = colors['white']

    tile_colors = [ color for name, color in colors.items() if name not in ('black', 'white') ]
    # tile = Tile((0, 0), (100, 100))
    tile = Tile((0, 0), (16, 16))

    for x in range(0, image_shape[0], 16):
        for y in range(0, image_shape[1], 16):
            # x = int(r.random() * image_shape[0])
            # y = int(r.random() * image_shape[1])
            tile.set_origin((x, y))
            tile.buffer[:] = r.sample(tile_colors, 1)

            i, t = image.intersection_slices(tile)
            image.buffer[i] = tile.buffer[t]

    save_array_as_image('test_tile.gif', image.buffer, 'RGBA')
Example #10
0
def main():
    colors = {
        'white'  : (255 , 255 , 255 , 255), 
        'red'    : (255 , 0   , 0   , 255), 
        'green'  : (0   , 255 , 0   , 255), 
        'blue'   : (0   , 0   , 255 , 255), 
        'yellow' : (255 , 255 , 000 , 255), 
    }

    for key, value in colors.items():
        colors[key] = np.array([value], dtype=Pixel)


    image = Tile((0, 0), (100, 100), dtype=Pixel)
    image.buffer[:,:] = colors['white']

    image.buffer[:50,:50] = colors['red']    # upper left
    image.buffer[50:,:50] = colors['green']  # upper right
    image.buffer[:50,50:] = colors['blue']   # lower left
    image.buffer[50:,50:] = colors['yellow'] # lower right

    save_array_as_image(image.buffer, 'render/001_tile.tiff', 'RGBA')
Example #11
0
def test_fill():
    shape=[640,480]
    p = point(100, 100)

    l = Line(
        point(100, 100),
        point(0, 200)
    )

    coords = make_coordinate_image(shape)
    delta = coords - l.start[:2].reshape(1,1,2)
    direction = (l.end - l.start)[:2].reshape(1,1,2)

    mask = np.inner(delta, direction).reshape(640, 480) >= 0.

    white = np.array([(255, 255, 255, 255)], dtype=Pixel)
    red   = np.array([(180, 0, 0, 255)], dtype=Pixel)

    image = np.empty(shape, dtype=Pixel, order='F')
    image[:,:] = white
    image[mask] = red

    save_array_as_image(image, 'red_white.tiff', 'RGBA')
Example #12
0
def render_frame(frame_no):
    print('rendering frame {}'.format(frame_no))

    from math import sin, pi
    tau = 2. * pi

    image = Tile((0, 0), (512,512), 4, dtype=FloatPixel)
    image.buffer[:,:] = palette['white'].view(dtype=FloatPixel)

    center = (256, 256)
    radius = 100

    surface = circle(center, radius)

    global transform

    transform = np.array(
        [
            [ 1., .0 ],
            [ .5 * sin(tau * frame_no / 60.), 1. ]
        ],
        dtype=np.float32
    )

    meshes = generate_meshes(surface)

    for mesh in meshes:
        cache = render_mesh(mesh)
        cache.composite_into(image)

    buffer = array_view(image.downsample(1))
    buffer = np.clip(buffer, 0., 1.)
    buffer = (255. * buffer).astype(dtype=np.uint8)

    path = 'render/003/frame_{:03}.tiff'.format(frame_no)
    save_array_as_image(pixel_view(buffer), path, 'RGBA')
Example #13
0
def test_line():
    shape = [640,480]

    lines = [
        [ (100, 100), (300, 300) ],
        [ (100, 300), (300, 100) ],
        [ (100, 200), (300, 200) ],
        [ (200, 100), (200, 300) ],
    ]

    width = 10.

    white = np.array([(255, 255, 255, 255)], dtype=Pixel)

    image = np.empty(shape, dtype=Pixel, order='F')
    image[::] = white

    red = np.array([(180, 0, 0, 127)], dtype=Pixel)

    for line in lines:
        line = Line(point(*line[0]), point(*line[1]))
        composite_image(draw_line(line, width, red), image)

    save_array_as_image('test_line.tiff', image, 'RGBA')
Example #14
0
def test_open_cl():
    from handsome.util import save_array_as_image, parse_color
    from handsome.Scene import make_canvas

    canvas = make_canvas({
        'extents' : (640, 480),
        'color'   : '#fff',
    })

    context = cl.create_some_context()
    queue = cl.CommandQueue(context)

    mf = cl.mem_flags

    source = r'''
        __kernel void shade_square(
            __global float4 * result_g
        )
        {
            int y = get_global_id(0),
                height = get_global_size(0);

            int x = get_global_id(1),
                width = get_global_size(1);

            int index = y * width + x;

            const float2  center = (float2)(width / 2, height / 2),
                dimensions = (float2)(height) / 2,
                upper_left = center - dimensions / 2;

            float x_blend = ((float)x - upper_left.x) / dimensions.x,
                y_blend = ((float)y - upper_left.y) / dimensions.y
            ;
            
            float4 upper_left_c = (float4)(1, 1, 1, 1),
                upper_right_c = (float4)(1, 0, 0, 1),
                lower_left_c = (float4)(0, 0, 1, 1),
                lower_right_c = (float4)(0, 0, 0, 1)
            ;

            if (0. <= x_blend && x_blend <= 1. && 0. <= y_blend && y_blend <= 1.) {
                result_g[index]
                    = (1 - x_blend) * (1 - y_blend) * upper_left_c
                    + x_blend * (1 - y_blend)  * upper_right_c
                    + (1 - x_blend) * y_blend * lower_left_c
                    + x_blend * y_blend * lower_right_c
                ;
            }
            else{
                result_g[index] = (float4)(0, 0, 0, 1);
            }
        }
    '''

    program = cl.Program(context, source).build()

    result = cl.Buffer(context, mf.WRITE_ONLY, canvas.buffer.nbytes)
    program.shade_square(queue, canvas.buffer.shape, None, result)

    cl.enqueue_copy(queue, array_view(canvas.buffer), result)

    buffer = array_view(canvas.downsample(1))
    buffer = np.clip(buffer, 0., 1.)
    buffer = (255 * buffer).astype(np.uint8)

    save_array_as_image(pixel_view(buffer), 'render/009_opencl.tiff', 'RGBA')
Example #15
0
def blocktopus():
    import random
    import math
    r = random.SystemRandom()

    mode = 'RGBA'
    image_shape = (1280, 960)
    # image_shape = (640, 480)

    image = Tile((0, 0), image_shape)

    colors = {
        'white'        : '#ffff',
        'black'        : '#000f',
        '205A8C'       : '205A8C',
        '6596BF'       : '6596BF',
        '98F2EC'       : '98F2EC',
        'BF6865'       : 'BF6865',
        'F4DBD6'       : 'F4DBD6',
        'dark_blue'    : '05115B',
        'light_blue'   : '60CAF0',
        'brown_yellow' : 'DAB32B',
        'brown'        : '855E14',
        'yellow'       : 'FFDD06',
    }

    for name, color in colors.items():
        colors[name] = parse_color(color)

    def make_tile(color):
        tile = Tile((0, 0), (16, 16))
        tile.buffer[:] = color
        return tile

    blocktopus_colors = (
        'brown',
        'brown_yellow',
        'dark_blue',
        'light_blue',
        'yellow',
    )

    tiles = { }

    def get_tile(tile_id):
        tile = tiles.get(tile_id)
        if tile is not None:
            return tile
        
        color = r.choice(blocktopus_colors)
        tiles[tile_id] = out = make_tile(color)
        out.buffer[:] = colors[color]
        return out

    spine = Line(
        point(.1 * image_shape[0], .5 * image_shape[1]),
        point(.9 * image_shape[0], .5 * image_shape[1])
    )

    direction = normalize(spine.direction)
    up = normalize(rotate_left(spine.direction))

    steps = 30 
    step_size = 1. / steps
    frequency = 2.5 * 2. * math.pi * step_size

    frames = 10 * 30

    for frame in range(frames):
        image.buffer[:] = colors['white']

        phase = 2 * 2. * math.pi * frame / frames
        phase_offset = .5 - math.cos(phase)

        for i in range(steps):
            width_factor = (1. - float(i) / (steps - 1))
            width = width_factor * .15 * image_shape[1]

            s = math.cos(frequency * i + phase) + phase_offset

            base = spine(i * step_size)

            offsets = [
                s * width * up,
                (s - 1) * width * up,
            ]

            for (j, offset) in enumerate(offsets):
                p = base + offset
                tile = get_tile((i, j))
                tile.set_origin(p[:2])

                intersection = image.intersection_slices(tile)
                if intersection is None: continue

                m, t = intersection
                image.buffer[m] = tile.buffer[t]

        path = 'blocktopus/frame_{frame:03}.tiff'.format(frame=frame)
        save_array_as_image(path, image.buffer, 'RGBA')