Ejemplo n.º 1
0
    def _build_programs(self, *args):
        """ Build all filter programs """

        index = 0
        self._programs = []

        # We add an extra do-nothing program for unfiltered rendering because
        # it will be rendered at original viewport size and we want all filters
        # to use the same framebuffer size.
        args = list(args) + ["texture2D(filtered, v_texcoord)"]

        for i, code in enumerate(args):
            if not isinstance(code, gloo.Snippet):
                snippet = gloo.Snippet(code)
            else:
                snippet = code
            program = gloo.Program(vertex, fragment, count=4)
            program['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
            program['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)]
            program['original'] = self._framebuffers[0].color[0]
            program['original'].interpolation = gl.GL_LINEAR
            program['filtered'] = self._framebuffers[index].color[0]
            program['filtered'].interpolation = gl.GL_LINEAR
            program['texsize'] = self.width, self.height
            if i < len(args) - 1:
                original_snippet = snippet
                # Walk through snippet arguments to fill last one
                while len(snippet.args) > 0:
                    if len(snippet.args) > 1:
                        raise ValueError(
                            "Filter snippet cannot have more than 1 argument")
                    elif not isinstance(snippet.args[0], gloo.Snippet):
                        raise ValueError(
                            "Filter snippet argument must be a Snippet")
                    else:
                        snippet = snippet.args[0]
                snippet._args = "original", "filtered", "v_texcoord", "texsize"
                program['filter'] = original_snippet
            else:
                program['filter'] = code
            self._programs.append(program)
            index = (index + 1) % 2
Ejemplo n.º 2
0
# program['u_minor_grid_step'] = np.array([ 0.25, np.pi/60])
# program['u_limits1'] = -5.1, +5.1, -5.1, +5.1
# program['u_limits2'] = 1.0, 5.0, 0*np.pi, 2*np.pi

# Cartesian domains
program['u_major_grid_step'] = np.array([1.00, 1.00])
program['u_minor_grid_step'] = np.array([0.10, 0.10])
program['u_limits1'] = -5.1, +5.1, -5.1, +5.1
program['u_limits2'] = -5.0, +5.0, -5.0, +5.0

# Hammer domains
# program['u_major_grid_step'] = np.array([ 1.00, 0.50]) * np.pi/ 6.0
# program['u_minor_grid_step'] = np.array([ 1.00, 0.50]) * np.pi/30.0
# program['u_limits1'] = -3.0, +3.0, -1.5, +1.5
# program['u_limits2'] = -np.pi, +np.pi, -np.pi/3, +np.pi/3

# program['transform'] = shaders.get("transforms/polar.glsl")
# program['transform'] = shaders.get("transforms/hammer.glsl")
program['transform_forward'] = gloo.Snippet(
    library.get("transforms/identity_forward.glsl"))
program['transform_inverse'] = gloo.Snippet(
    library.get("transforms/identity_inverse.glsl"))
program['trackball'] = Trackball(Position("texcoord"))
program['trackball'].theta = 0
program['trackball'].phi = 0
program['trackball'].zoom = 7.5

window.attach(program['trackball'])

app.run()
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
from glumpy import gloo

transform_1 = gloo.Snippet("""
uniform float scale;
float forward(float x) { return scale*x; }
float inverse(float x) { return scale*x; }
""")

transform_2 = gloo.Snippet("""
uniform float scale;
float forward(float x) { return scale*x; }
float inverse(float x) { return scale*x; }
""")

transform_3 = gloo.Snippet("""
vec2 compose(float x, float y) { return vec2(x,y); }
vec2 compose(vec2 xy) { return xy; }
""")

code = """
uniform float scale;

void main(void)
{
    // ---
    float scale_t1 = <transform_1.scale>;
    float scale_t2 = <transform_6.scale>;
void main()
{
    <clip>;
    float r = texture2D(texture, v_texcoord).r;
    gl_FragColor = vec4(vec3(r),1.0);
}
"""

Grid = gloo.Snippet("""
uniform float rows, cols;
vec4 cell(vec4 position, float index)
{
    float col = mod(index,cols) + 0.5;
    float row = floor(index/cols) + 0.5;
    float x = -1.0 + col * (2.0/cols);
    float y = -1.0 + row * (2.0/rows);
    float width = 0.95 / (1.0*cols);
    float height = 0.95 / (1.0*rows);
    vec4 P = position / position.w;
    P = vec4(x + width*P.x, y + height*P.y, P.z, P.w);
    return P*position.w;
}
""")

Clip = gloo.Snippet("""
uniform vec2 iResolution;
uniform float rows, cols;
void clip(float index)
{
    vec2 P = gl_FragCoord.xy;
Ejemplo n.º 5
0
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
import numpy as np
from glumpy import app, gloo, gl, glm

Position = gloo.Snippet("""
vec4 position2D(vec2 position)
{
    return vec4(position, 0.0, 1.0);
}
""")

XScale = gloo.Snippet("""
uniform float xscale;
vec2 scale(vec2 position)
{
    return vec2(xscale,1.0) * position;
}
""")

Grid = gloo.Snippet("""
uniform float rows, cols;
varying float v_x, v_y, v_index;
vec2 cell(vec2 position, float index)
{
    v_index = index;
    float col = mod(index,cols) + 0.5;
    float row = floor(index/cols) + 0.5;
    float x = -1.0 + col * (2.0/cols);
Ejemplo n.º 6
0
varying vec2 v_texcoord;
void main()
{
    float r = texture2D(texture, v_texcoord).r;
    gl_FragColor = vec4(vec3(r),1.0);
}
"""

window = app.Window(1024, 1024)

# See http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/
VBlur = gloo.Snippet("""
vec4 filter(sampler2D original, sampler2D filtered, vec2 texcoord, vec2 texsize)
{
    return 0.2270270270 *  texture2D( filtered, texcoord)
         + 0.3162162162 * (texture2D( filtered, texcoord + vec2(0.0, 1.3846153846)/texsize) +
                           texture2D( filtered, texcoord - vec2(0.0, 1.3846153846)/texsize) )
         + 0.0702702703 * (texture2D( filtered, texcoord + vec2(0.0, 3.2307692308)/texsize) +
                           texture2D( filtered, texcoord - vec2(0.0, 3.2307692308)/texsize) );
}""")

HBlur = gloo.Snippet("""
vec4 filter(sampler2D original, sampler2D filtered, vec2 texcoord, vec2 texsize)
{
    return 0.2270270270 *  texture2D( filtered, texcoord)
         + 0.3162162162 * (texture2D( filtered, texcoord + vec2(1.3846153846, 0.0)/texsize) +
                           texture2D( filtered, texcoord - vec2(1.3846153846, 0.0)/texsize) )
         + 0.0702702703 * (texture2D( filtered, texcoord + vec2(3.2307692308, 0.0)/texsize) +
                           texture2D( filtered, texcoord - vec2(3.2307692308, 0.0)/texsize) );
}""")
GaussianBlur = Filter(512, 512, VBlur, HBlur)
Ejemplo n.º 7
0
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
import numpy as np
from glumpy import gloo

A = gloo.Snippet("uniform float a;\nvoid function_A(void) {};\n\n",
                 name="Snippet_A")
B = gloo.Snippet("uniform float b;\nvoid function_B(void) {};\n\n",
                 name="Snippet_B")
C = gloo.Snippet("uniform float c;\nvoid function_C(void) {};\n\n",
                 name="Snippet_C")
D = A(B("P")) + C()

print("D:             ", D)
print("D['Snippet_A']:", D["Snippet_A"])
print("D['Snippet_B']:", D["Snippet_B"])
print("D['Snippet_C']:", D["Snippet_C"])

print()
print("D.locals: ", D.locals)
print("D.globals:", D.globals)
print("D['Snippet_A'].locals:", D["Snippet_A"].locals)
print("D['Snippet_B'].locals:", D["Snippet_B"].locals)
print("D['Snippet_C'].locals:", D["Snippet_C"].locals)
print("D.symbols:", D.symbols)
print()

# print D.objects
"""

fragment = """
uniform sampler2D texture;
varying vec2 v_texcoord;
void main()
{
    float r = texture2D(texture, v_texcoord).r;
    gl_FragColor = vec4(vec3(r),1.0);
}
"""

pixelate = gloo.Snippet("""
uniform float level;
vec4 filter(sampler2D original, sampler2D filtered, vec2 texcoord, vec2 texsize)
{
    vec2 uv = (texcoord * level);
    uv = (uv - fract(uv)) / level;
    return texture2D(filtered, uv);
} """)
sepia = gloo.Snippet("""
vec4 filter(vec4 color)
{
    return vec4( dot(color.rgb, vec3(.393, .769, .189)),
                 dot(color.rgb, vec3(.349, .686, .168)),
                 dot(color.rgb, vec3(.272, .534, .131)),
                 color.a );
}
vec4 filter(sampler2D original, sampler2D filtered, vec2 texcoord, vec2 texsize)
{
    return filter( texture2D(filtered, texcoord) );
}