Пример #1
0
def test_example1():
    """ Just a few simple compositions.
    """

    # Get function objects. Generate random name for transforms
    code = Function(vert_template)
    t1 = Function(transformScale)
    t2 = Function(transformZOffset)
    t3 = Function(transformScale)

    # We need to create a variable in order to use it in two places
    pos = Variable('attribute vec4 a_position')

    # Compose everything together
    code['position'] = t1(t2(pos))
    code['correction'] = t1(pos)  # Look, we use t1 again, different sig
    code['endtransform'] = t3  # function pointer rather than function call
    code['nlights'] = '4'
    t1['scale'] = t2
    t3['scale'] = (3.0, 4.0, 5.0)
    t2['offset'] = '1.0'

    code2 = Function(frag_template)
    code2['color'] = Varying('v_position')

    code['gl_PointSize'] = '3.0'
    code[code2['color']] = pos
    print(code)
Пример #2
0
    def __init__(self, texture, pos):
        self.vshader = Function("""
            void line_of_sight() {
                vec4 polar_pos = $transform(vec4($pos, 1));
                if( polar_pos.x > 0.999 ) {
                    polar_pos.x = 0.001;
                }
                vec4 c = texture2D($texture, vec2(polar_pos.x, 0.5));
                float depth = c.r;
                if( polar_pos.y > depth+0.5 ) {
                    $mask = vec3(0.5, 0.5, 1);  // out-of-sight objects turn blue
                }
                else {
                    $mask = vec3(1, 1, 1);
                }
            }
        """)
        self.fshader = Function("""
            void apply_texture_mask() {
                gl_FragColor *= vec4($mask,1);
            }
        """)
        self.center = STTransform()
        self.transform = STTransform(
            scale=(0.5 / np.pi, 1, 0),
            translate=(0.5, 0, 0)) * PolarTransform().inverse * self.center

        self.vshader['pos'] = pos
        self.vshader['transform'] = self.transform
        self.vshader['texture'] = texture
        self.vshader['mask'] = Varying('mask', dtype='vec3')
        self.fshader['mask'] = self.vshader['mask']
Пример #3
0
    def __init__(self, opacity, pos):
        self.opacity = opacity
        self.vshader = Function("""
            void line_of_sight() {
                vec2 diff = $player_pos.xy - $pos.xy;
                float dist = length(diff);
                float s = 0.5;
                vec2 step = s * diff / dist;
                int steps = int(dist/s);
                $visible = 1;
                vec2 sample_pos = $pos.xy + step/s;
                for( int i=1; i<steps; i++ ) {
                    float op = texture2D($opacity, (sample_pos+0.5) / $opacity_size).r;
                    if( op > 0 ) {
                        $visible = 0;
                        break;
                    }
                    sample_pos += step;
                }
            }
        """)

        self.fshader = Function("""
            void line_of_sight() {
                gl_FragColor = gl_FragColor * $visible;
                //gl_FragColor.r = $visible;
            }
        """)

        self.vshader['opacity'] = opacity
        self.vshader['opacity_size'] = opacity.shape[:2][::-1]
        self.vshader['pos'] = pos
        self.vshader['visible'] = Varying('visible', dtype='float')
        self.fshader['visible'] = self.vshader['visible']
Пример #4
0
    def __init__(self, texture, texcoords, enabled=True):
        """Apply a texture on a mesh.

        Parameters
        ----------
        texture : (M, N) or (M, N, C) array
            The 2D texture image.
        texcoords : (N, 2) array
            The texture coordinates.
        enabled : bool
            Whether the display of the texture is enabled.
        """
        vfunc = Function("""
            void pass_coords() {
                $v_texcoords = $texcoords;
            }
        """)
        ffunc = Function("""
            void apply_texture() {
                if ($enabled == 1) {
                    gl_FragColor *= texture2D($u_texture, $texcoords);
                }
            }
        """)
        self._texcoord_varying = Varying('v_texcoord', 'vec2')
        vfunc['v_texcoords'] = self._texcoord_varying
        ffunc['texcoords'] = self._texcoord_varying
        self._texcoords_buffer = VertexBuffer(
            np.zeros((0, 2), dtype=np.float32))
        vfunc['texcoords'] = self._texcoords_buffer
        super().__init__(vcode=vfunc, vhook='pre', fcode=ffunc)

        self.enabled = enabled
        self.texture = texture
        self.texcoords = texcoords
Пример #5
0
    def __init__(self, texture, texcoords, enabled=True):
        """Apply a texture on a mesh."""
        vfunc = Function("""
            void pass_coords() {
                $v_texcoords = $texcoords;
            }
        """)
        ffunc = Function("""
            void apply_texture() {
                if ($enabled == 1) {
                    gl_FragColor *= texture2D($u_texture, $texcoords);
                }
            }
        """)
        self._texcoord_varying = Varying('v_texcoord', 'vec2')
        vfunc['v_texcoords'] = self._texcoord_varying
        ffunc['texcoords'] = self._texcoord_varying
        self._texcoords_buffer = VertexBuffer(
            np.zeros((0, 2), dtype=np.float32)
        )
        vfunc['texcoords'] = self._texcoords_buffer
        super().__init__(vcode=vfunc, vhook='pre', fcode=ffunc)

        self.enabled = enabled
        self.texture = texture
        self.texcoords = texcoords
Пример #6
0
 def __init__(self, cmap, zrange=(0, 1)):
     if isinstance(cmap, str):
         cmap = colormap.get_colormap(cmap)
     self.cmap = Function(cmap.glsl_map)
     self.fshader['cmap'] = self.cmap
     self.fshader['zrange'] = zrange
     self.vshader['zval'] = Varying('v_zval', dtype='float')
     self.fshader['zval'] = self.vshader['zval']