# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 <NAME>. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
import numpy as np
from glumpy import app, gloo, gl

vertex = """
    attribute vec2 position;
    attribute vec2 texcoord;
    varying vec2 v_texcoord;
    void main()
    {
        gl_Position = vec4(position, 0.0, 1.0);
        v_texcoord = texcoord;
    }
"""

fragment = """
    uniform sampler3D texture;
    varying vec2 v_texcoord;
    void main()
    {
        float r = texture3D(texture, vec3(v_texcoord, 0.5)).r;
        gl_FragColor = vec4(r,r,r,1);
    }
"""

def checkerboard(grid_num=8, grid_size=32):
    row_even = grid_num // 2 * [0, 1]
    row_odd = grid_num // 2 * [1, 0]
    Z = np.row_stack(grid_num // 2 * (row_even, row_odd, row_even)).astype(np.uint8)
    s1 = grid_num // 2 * row_even
    return 255 * Z.repeat(grid_size, axis=0).repeat(grid_size, axis=1)

window = app.Window(width=512, height=512)

@window.event
def on_draw(dt):
    window.clear()
    quad.draw(gl.GL_TRIANGLE_STRIP)

quad = gloo.Program(vertex, fragment, count=4)
quad['position'] = [(-1,-1), (-1,+1), (+1,-1), (+1,+1)]
quad['texcoord'] = [( 0, 1), ( 0, 0), ( 1, 1), ( 1, 0)]
quad['texture'] = checkerboard().reshape(32, 32, -1)
app.run()

# todo: replace checker board with pet ct 3d volume
