示例#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
示例#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')
示例#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')
示例#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')
示例#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')
示例#6
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')
示例#7
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')
示例#8
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')
示例#9
0
        return (1 - v) * low + v * high

    return surface


def generate_shader():
    dir_path, base_path = os.path.split(__file__)
    texture_path = os.path.abspath(os.path.join(dir_path, 'render', 'texture.tiff'))
    texture = read_texture(texture_path)

    def shader(u, v):
        return sample_texture(texture, u, v)

    return shader

black = pixel_view(np.array([ 0., 0., 0., 0. ], dtype=np.float32))

def sample_texture(texture, s, t):
    from math import floor

    t = 1. - t

    if not (0. <= s < 1.):
        return black

    if not (0. <= t < 1.):
        return black

    shape = texture.shape

    s = s * (shape[0] - 1)
示例#10
0
            column_end = column_start + column_stride

            mesh_slice = buffer[row_start:row_end + 1, column_start:column_end + 1]

            r, c = mesh_slice.shape

            mesh = MicropolygonMesh((r - 1, c - 1))
            mesh.buffer[:,:] = mesh_slice

            yield mesh

            column_start = column_end

        row_start = row_end

red = pixel_view(np.array([ 1., 0., 0., 1. ], dtype=np.float32))
clear = pixel_view(np.array([ 0., 0., 0., 0. ], dtype=np.float32))

def sample_texture(s, t, texture):
    from math import floor

    t = 1. - t
    s, t = t, s

    if not (0. <= s < 1.):
        return clear

    if not (0. <= t < 1.):
        return clear

    shape = texture.shape
示例#11
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')