def __init__(self):
        self.ctx = mgl.create_context()

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform vec3 Color;
                uniform bool UseTexture;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    if (UseTexture) {
                        f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                    } else {
                        f_color = vec4(Color * lum, 1.0);
                    }
                }
            ''',
        )

        self.objects = {}

        for name in ['ground', 'grass', 'billboard', 'billboard-holder', 'billboard-image']:
            obj = Obj.open(data.find('scene-1-%s.obj' % name))
            vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
            vao = self.ctx.simple_vertex_array(self.prog, vbo, 'in_vert', 'in_norm', 'in_text')
            self.objects[name] = vao

        img = Image.open(data.find('infographic-1.jpg')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.sampler = self.ctx.sampler(self.texture)
예제 #2
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                }
            ''',
        )

        self.mvp = self.prog['Mvp']
        self.light = self.prog['Light']

        obj = Obj.open(data.find('crate.obj'))
        img = Image.open(data.find('crate.png')).transpose(
            Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.texture.use()

        self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert',
                                                'in_norm', 'in_text')
예제 #3
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.ctx = mgl.create_context()

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                }
            ''',
        )

        obj = Obj.open(data.find('crate.obj'))
        img = Image.open(data.find('crate.png')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.sampler = self.ctx.sampler(self.texture)

        self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm', 'in_text')
        self.vao.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler, 0)])
예제 #4
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.ctx = mgl.create_context()

        # import gltraces
        # mglprocs = mgl.glprocs(self.ctx)
        # gltraces.glprocs[:] = mglprocs
        # mglprocs[:] = gltraces.gltraces

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec3 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec3 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform samplerCube Sampler;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec3 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(texture(Sampler, normalize(v_text)).rgb * lum, 1.0);
                }
            ''',
        )

        obj = Obj.open(data.find('texture-test-cube.obj'))
        self.texture = self.ctx.texture_cube(
            (4, 4), 3,
            np.random.randint(128, 255, (4, 4, 3, 6), 'u1').tobytes())
        self.sampler = self.ctx.sampler(self.texture, filter=mgl.LINEAR)

        self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty tz'))
        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert',
                                                'in_norm', 'in_text')
        self.vao.scope = self.ctx.scope(mgl.DEPTH_TEST,
                                        samplers=[(self.sampler, 0)])
예제 #5
0
    def __add__(self, data):
        """
        Returns a new location offset by
        the specified string.
        """

        if data == '':
            return self

        skiplines = data.count('\n')
        line = self.line + skiplines
        if skiplines:
            lastnl = data.rfind('\n')
            assert lastnl != -1
            data = data[lastnl + 1:]
            column = 0
        else:
            column = self.column

        i = 0
        while True:
            j = data.find('\t', i)
            if j == -1:
                column += len(data) - i
                break

            column += j - i
            column += _tabwidth
            column -= column % _tabwidth
            i = j + 1

        return Location(self.path, line, column)
예제 #6
0
    def __add__(self, data):
        """
        Returns a new location offset by
        the specified string.
        """

        if data == '':
            return self
        
        skiplines = data.count('\n')
        line = self.line + skiplines
        if skiplines:
            lastnl = data.rfind('\n')
            assert lastnl != -1
            data = data[lastnl + 1:]
            column = 0
        else:
            column = self.column

        i = 0
        while True:
            j = data.find('\t', i)
            if j == -1:
                column += len(data) - i
                break

            column += j - i
            column += _tabwidth
            column -= column % _tabwidth
            i = j + 1

        return Location(self.path, line, column)
예제 #7
0
    def __init__(self):
        self.ctx = mgl.create_context()

        self.prog = self.ctx.program(vertex_shader='''
                #version 330

                in vec2 in_vert;
                out vec2 v_text;

                void main() {
                    gl_Position = vec4(in_vert, 0.0, 1.0);
                    v_text = in_vert;
                }
            ''',
                                     fragment_shader='''
                #version 330

                in vec2 v_text;
                out vec4 f_color;

                uniform sampler2D Texture;
                uniform vec2 Center;
                uniform float Scale;
                uniform float Ratio;
                uniform int Iter;

                void main() {
                    vec2 c;
                    int i;

                    c.x = Ratio * v_text.x * Scale - Center.x;
                    c.y = v_text.y * Scale - Center.y;

                    vec2 z = c;

                    for (i = 0; i < Iter; i++) {
                        float x = (z.x * z.x - z.y * z.y) + c.x;
                        float y = (z.y * z.x + z.x * z.y) + c.y;

                        if ((x * x + y * y) > 4.0) {
                            break;
                        }

                        z.x = x;
                        z.y = y;
                    }

                    f_color = texture(Texture, vec2((i == Iter ? 0.0 : float(i)) / 100.0, 0.0));
                }
            ''')

        img = Image.open(data.find('pal.png')).convert('RGB')
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.sampler = self.ctx.sampler(self.texture)
        self.sampler.use()

        vertices = np.array([-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0])

        self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes())
        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert')
예제 #8
0
def process_articles_from_db():
    articles = db.find('articles',{})

    locs = {}
    count = 0
    for article in articles:
        print article

        print country_hack.article_to_country_codes(article)
        risk_freqs = get_risks_for_article(article)

        a_locs = code.get_geocodes_from_text(article['headline']) + code.get_geocodes_from_text(article['snippet'])
        for al in a_locs:
            try:
                if not al['placename'] in locs:
                    locs[al['placename']] = risk_freqs
                else:
                    for k,v in risk_freqs.iteritems():
                        locs[al['placename']][k] += v
            except:
                pass
        count += 1
    print locs

    loc_list = [locs[key] for key in locs]
    loc_list.sort(sorter)
    
    return loc_list
예제 #9
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                in vec2 in_vert;
                out vec2 v_text;

                void main() {
                    gl_Position = vec4(in_vert, 0.0, 1.0);
                    v_text = in_vert;
                }
            ''',
            fragment_shader='''
                #version 330

                in vec2 v_text;
                out vec4 f_color;

                uniform sampler2D Texture;
                uniform vec2 Seed;
                uniform int Iter;

                void main() {
                    vec2 c = Seed;
                    int i;

                    vec2 z = vec2(3.0 * v_text.x, 2.0 * v_text.y);

                    for (i = 0; i < Iter; i++) {
                        float x = (z.x * z.x - z.y * z.y) + c.x;
                        float y = (z.y * z.x + z.x * z.y) + c.y;

                        if ((x * x + y * y) > 4.0) {
                            break;
                        }

                        z.x = x;
                        z.y = y;
                    }

                    f_color = texture(Texture, vec2((i == Iter ? 0.0 : float(i)) / 100.0, 0.0));
                }
            ''',
        )

        self.seed = self.prog['Seed']
        self.iter = self.prog['Iter']

        img = Image.open(data.find('pal.png')).convert('RGB')
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.texture.use()

        vertices = np.array([-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0])

        self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes())
        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert')
예제 #10
0
파일: main.py 프로젝트: jjinux/tictactoe3
def main():

    """This is the entry point to the application."""

    # This is for text mode.

    if len(sys.argv) == 2 and sys.argv[1] == '-t':
        model.main()
        sys.exit(0)

    # Do initialization.

    pygame.init()
    screen = pygame.display.set_mode(DISPLAY_MODE)
    pygame.display.set_caption(TITLE)
    clock = pygame.time.Clock()
    background = pygame.Surface(screen.get_size()).convert()
    background.fill(BACKGROUND)
    pygame.display.flip()

    game_model = model.Game()
    board_view = view.Board(game_model)
    score_board = view.ScoreBoard(game_model)
    rendering_groups = [board_view, score_board]

    while True:

        clock.tick(FRAMES_PER_SEC)
        scheduler.tick()

        # Handle user input.

        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key in (K_ESCAPE, K_q) or event.type == QUIT:
                    sys.exit(0)
                elif event.key == K_h:
                    url = "file://" + os.path.abspath(data.find("help.html"))
                    webbrowser.open(url, new=True)
                elif event.key == K_r:
                    game_model.reset()
            elif event.type == MOUSEBUTTONDOWN:
                for square_view in board_view:
                    if square_view.rect.collidepoint(*pygame.mouse.get_pos()):
                        xyz = square_view.square_model.xyz
                        try:
                            game_model.move(xyz)
                        except ValueError:
                            pass
                        break

        # Provide the simulation and render it.

        for i in rendering_groups:
            i.update()
            i.clear(screen, background)
            pygame.display.update(i.draw(screen))
예제 #11
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.ctx = mgl.create_context()

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec3 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec3 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform sampler3D Sampler;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec3 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(texture(Sampler, v_text).rgb * lum, 1.0);
                }
            ''',
        )

        obj = Obj.open(data.find('texture-test-cube.obj'))
        self.texture = self.ctx.texture(
            (2, 2, 2), 3, np.random.randint(128, 255, (2, 2, 2, 3), 'u1'))
        self.sampler = self.ctx.sampler(self.texture,
                                        wrap=mgl.REPEAT_X | mgl.REPEAT_Y
                                        | mgl.REPEAT_Z)

        self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty tz'))
        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert',
                                                'in_norm', 'in_text')
        self.vao.scope = self.ctx.scope(mgl.DEPTH_TEST,
                                        samplers=[(self.sampler, 0)])
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.ctx = mgl.create_context()

        # import gltraces
        # mglprocs = mgl.glprocs(self.ctx)
        # gltraces.glprocs[:] = mglprocs
        # mglprocs[:] = gltraces.gltraces

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec3 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec3 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform samplerCube Sampler;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec3 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(texture(Sampler, normalize(v_text)).rgb * lum, 1.0);
                }
            ''',
        )

        obj = Obj.open(data.find('texture-test-cube.obj'))
        self.texture = self.ctx.texture_cube((4, 4), 3, np.random.randint(128, 255, (4, 4, 3, 6), 'u1').tobytes())
        self.sampler = self.ctx.sampler(self.texture, filter=mgl.LINEAR)

        self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty tz'))
        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm', 'in_text')
        self.vao.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler, 0)])
예제 #13
0
파일: main.py 프로젝트: jjinux/tictactoe3
def main():
    """This is the entry point to the application."""

    # This is for text mode.

    if len(sys.argv) == 2 and sys.argv[1] == '-t':
        model.main()
        sys.exit(0)

    # Do initialization.

    pygame.init()
    screen = pygame.display.set_mode(DISPLAY_MODE)
    pygame.display.set_caption(TITLE)
    clock = pygame.time.Clock()
    background = pygame.Surface(screen.get_size()).convert()
    background.fill(BACKGROUND)
    pygame.display.flip()

    game_model = model.Game()
    board_view = view.Board(game_model)
    score_board = view.ScoreBoard(game_model)
    rendering_groups = [board_view, score_board]

    while True:

        clock.tick(FRAMES_PER_SEC)
        scheduler.tick()

        # Handle user input.

        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key in (K_ESCAPE, K_q) or event.type == QUIT:
                    sys.exit(0)
                elif event.key == K_h:
                    url = "file://" + os.path.abspath(data.find("help.html"))
                    webbrowser.open(url, new=True)
                elif event.key == K_r:
                    game_model.reset()
            elif event.type == MOUSEBUTTONDOWN:
                for square_view in board_view:
                    if square_view.rect.collidepoint(*pygame.mouse.get_pos()):
                        xyz = square_view.square_model.xyz
                        try:
                            game_model.move(xyz)
                        except ValueError:
                            pass
                        break

        # Provide the simulation and render it.

        for i in rendering_groups:
            i.update()
            i.clear(screen, background)
            pygame.display.update(i.draw(screen))
예제 #14
0
class SamplerBasicExample(Example):
    gl_version = (3, 3)
    aspect_ratio = 1.0
    title = "Sampler Basic Example"

    img = Image.open(data.find('color_arrows.png'))
    texrect = (-2.0, -2.0, 3.0, 3.0)

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.renderer = SimpleObjectRenderer(self.ctx)

        self.vertices = np.array([
            (-0.9, -0.9),
            (-0.9, 0.9),
            (0.9, -0.9),
            (0.9, 0.9),
        ],
                                 dtype='f4')

        left, bottom, right, top = self.texrect
        self.texcoords = np.array([
            (left, bottom),
            (left, top),
            (right, bottom),
            (right, top),
        ],
                                  dtype='f4')

        self.texture = self.ctx.texture(
            self.img.transpose(Image.FLIP_TOP_BOTTOM).convert('RGB'))
        self.sampler = self.ctx.sampler(self.texture)

    def render(self, time: float, frame_time: float):
        # clear screen
        self.ctx.clear(1.0, 1.0, 1.0)

        # render texture
        self.sampler.use()
        self.renderer.render(mgl.TRIANGLE_STRIP,
                             vertices=self.vertices,
                             texcoords=self.texcoords)

        # render grid
        self.renderer.render(mgl.LINES,
                             vertices=np.array([(i * 0.18, -0.9, i * 0.18, 0.9,
                                                 -0.9, i * 0.18, 0.9, i * 0.18)
                                                for i in range(-5, 7, 2)],
                                               'f4').reshape(-1, 2),
                             texture=False,
                             color=(0.0, 0.0, 0.0, 1.0))
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.ctx = mgl.create_context()

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec3 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec3 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform sampler3D Sampler;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec3 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(texture(Sampler, v_text).rgb * lum, 1.0);
                }
            ''',
        )

        obj = Obj.open(data.find('texture-test-cube.obj'))
        self.texture = self.ctx.texture((2, 2, 2), 3, np.random.randint(128, 255, (2, 2, 2, 3), 'u1'))
        self.sampler = self.ctx.sampler(self.texture, wrap=mgl.REPEAT_X | mgl.REPEAT_Y | mgl.REPEAT_Z)

        self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty tz'))
        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm', 'in_text')
        self.vao.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler, 0)])
예제 #16
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;
                uniform sampler2D Heightmap;

                in vec2 in_vert;
                in vec3 in_color;

                out vec3 v_color;

                void main() {
                    v_color = in_color;
                    float height = texture(Heightmap, in_vert.xy).r * 0.5;
                    gl_Position = Mvp * vec4(in_vert.xy - 0.5, height, 1.0);
                }
            ''',
            fragment_shader='''
                #version 330

                in vec3 v_color;
                out vec4 f_color;

                void main() {
                    f_color = vec4(v_color, 1.0);
                }
            ''',
        )

        self.mvp = self.prog['Mvp']

        vertices, index = terrain(32)

        self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes())
        self.ibo = self.ctx.buffer(index.astype('i4').tobytes())

        vao_content = [
            (self.vbo, '2f', 'in_vert'),
        ]

        self.vao = self.ctx.vertex_array(self.prog, vao_content, self.ibo)

        self.img = Image.open(data.find('noise.jpg')).convert('L')
        texture = self.ctx.texture(self.img.size, 1, self.img.tobytes())
        texture.use()
예제 #17
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;
                uniform sampler2D Heightmap;

                in vec2 in_vert;
                in vec3 in_color;

                out vec3 v_color;

                void main() {
                    v_color = in_color;
                    float height = texture(Heightmap, in_vert.xy).r * 0.5;
                    gl_Position = Mvp * vec4(in_vert.xy - 0.5, height, 1.0);
                }
            ''',
            fragment_shader='''
                #version 330

                in vec3 v_color;
                out vec4 f_color;

                void main() {
                    f_color = vec4(v_color, 1.0);
                }
            ''',
        )

        self.mvp = self.prog['Mvp']

        vertices, index = terrain(32)

        self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes())
        self.ibo = self.ctx.buffer(index.astype('i4').tobytes())

        vao_content = [
            (self.vbo, '2f', 'in_vert'),
        ]

        self.vao = self.ctx.vertex_array(self.prog, vao_content, self.ibo)

        self.img = Image.open(data.find('noise.jpg')).convert('L')
        texture = self.ctx.texture(self.img.size, 1, self.img.tobytes())
        texture.use()
예제 #18
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        # import gltraces
        # mglprocs = mgl.glprocs(self.ctx)
        # gltraces.glprocs[:] = mglprocs
        # mglprocs[:] = gltraces.gltraces

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;

                out vec3 v_vert;
                out vec3 v_norm;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform samplerCube Sampler;
                uniform vec3 Eye;

                in vec3 v_vert;
                in vec3 v_norm;

                out vec4 f_color;

                void main() {
                    f_color = texture(Sampler, reflect(v_vert - Eye, v_norm));
                }
            ''',
        )

        img1 = Image.new('RGB', (200, 200), '#fee')
        img2 = Image.new('RGB', (200, 200), '#efe')
        img3 = Image.new('RGB', (200, 200), '#eef')
        img4 = Image.new('RGB', (200, 200), '#ffe')
        img5 = Image.new('RGB', (200, 200), '#fef')
        img6 = Image.new('RGB', (200, 200), '#eff')

        ImageDraw.ImageDraw(img1).text((50, 30), 'i1', '#000',
                                       ImageFont.truetype('arial', 128))
        ImageDraw.ImageDraw(img2).text((50, 30), 'i2', '#000',
                                       ImageFont.truetype('arial', 128))
        ImageDraw.ImageDraw(img3).text((50, 30), 'i3', '#000',
                                       ImageFont.truetype('arial', 128))
        ImageDraw.ImageDraw(img4).text((50, 30), 'i4', '#000',
                                       ImageFont.truetype('arial', 128))
        ImageDraw.ImageDraw(img5).text((50, 30), 'i5', '#000',
                                       ImageFont.truetype('arial', 128))
        ImageDraw.ImageDraw(img6).text((50, 30), 'i6', '#000',
                                       ImageFont.truetype('arial', 128))

        def join(*images):
            return b''.join(img.tobytes('raw', 'RGB', 0, -1) for img in images)

        self.texture = self.ctx.texture_cube((200, 200), 3,
                                             join(img1, img2, img3, img4, img5,
                                                  img6))
        self.sampler = self.ctx.sampler(self.texture)
        self.sampler.filter = mgl.LINEAR
        self.sampler.use()

        obj = Obj.open(data.find('sitting_dummy.obj'))
        self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz'))
        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert',
                                                'in_norm')
        self.scope = self.ctx.scope(mgl.DEPTH_TEST)
예제 #19
0
    def __init__(self):
        self.ctx = mgl.create_context()

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;
                uniform sampler2D Heightmap;

                in vec2 in_vert;
                out vec2 v_text;

                void main() {
                    vec4 vertex = vec4(in_vert - 0.5, texture(Heightmap, in_vert).r * 0.2, 1.0);
                    gl_Position = Mvp * vertex;
                    v_text = in_vert;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform sampler2D Heightmap;

                uniform sampler2D Color1;
                uniform sampler2D Color2;

                uniform sampler2D Cracks;
                uniform sampler2D Darken;

                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float height = texture(Heightmap, v_text).r;
                    float border = smoothstep(0.5, 0.7, height);

                    vec3 color1 = texture(Color1, v_text * 7.0).rgb;
                    vec3 color2 = texture(Color2, v_text * 6.0).rgb;

                    vec3 color = color1 * (1.0 - border) + color2 * border;

                    color *= 0.8 + 0.2 * texture(Darken, v_text * 3.0).r;
                    color *= 0.5 + 0.5 * texture(Cracks, v_text * 5.0).r;
                    color *= 0.5 + 0.5 * height;

                    f_color = vec4(color, 1.0);
                }
            ''',
        )

        vertices, index = terrain(32)

        self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes())
        self.ibo = self.ctx.buffer(index.astype('i4').tobytes())

        vao_content = [
            (self.vbo, '2f', 'in_vert'),
        ]

        self.vao = self.ctx.vertex_array(self.prog, vao_content, self.ibo)

        img0 = Image.open(data.find('heightmap.jpg')).convert('L').transpose(
            Image.FLIP_TOP_BOTTOM)
        img1 = Image.open(data.find('grass.jpg')).convert('RGB').transpose(
            Image.FLIP_TOP_BOTTOM)
        img2 = Image.open(data.find('rock.jpg')).convert('RGB').transpose(
            Image.FLIP_TOP_BOTTOM)
        img3 = Image.open(data.find('cracks.jpg')).convert('L').transpose(
            Image.FLIP_TOP_BOTTOM)
        img4 = Image.open(data.find('checked.jpg')).convert('L').transpose(
            Image.FLIP_TOP_BOTTOM)

        tex0 = self.ctx.texture(img0.size, 1, img0.tobytes())
        tex1 = self.ctx.texture(img1.size, 3, img1.tobytes())
        tex2 = self.ctx.texture(img2.size, 3, img2.tobytes())
        tex3 = self.ctx.texture(img3.size, 1, img3.tobytes())
        tex4 = self.ctx.texture(img4.size, 1, img4.tobytes())

        sampler0 = self.ctx.sampler(tex0)
        sampler1 = self.ctx.sampler(tex1)
        sampler2 = self.ctx.sampler(tex2)
        sampler3 = self.ctx.sampler(tex3)
        sampler4 = self.ctx.sampler(tex4)

        # tex0.build_mipmaps()
        # tex1.build_mipmaps()
        # tex2.build_mipmaps()
        # tex3.build_mipmaps()
        # tex4.build_mipmaps()

        sampler0.use(0)
        sampler1.use(1)
        sampler2.use(2)
        sampler3.use(3)
        sampler4.use(4)

        self.prog['Heightmap'] = 0
        self.prog['Color1'] = 1
        self.prog['Color2'] = 2
        self.prog['Cracks'] = 3
        self.prog['Darken'] = 4
예제 #20
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform vec3 Color;
                uniform bool UseTexture;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    if (UseTexture) {
                        f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                    } else {
                        f_color = vec4(Color * lum, 1.0);
                    }
                }
            ''',
        )

        self.objects = {}

        for name in [
                'ground', 'grass', 'billboard', 'billboard-holder',
                'billboard-image'
        ]:
            obj = Obj.open(data.find('scene-1-%s.obj' % name))
            vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
            vao = self.ctx.simple_vertex_array(self.prog, vbo, 'in_vert',
                                               'in_norm', 'in_text')
            self.objects[name] = vao

        img = Image.open(data.find('infographic-1.jpg')).transpose(
            Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.texture1 = self.ctx.texture(img.size, 3, img.tobytes())
        # self.texture1.build_mipmaps()

        self.texture2 = self.ctx.texture(self.wnd.size, 3)
        depth_attachment = self.ctx.depth_renderbuffer(self.wnd.size)
        self.fbo = self.ctx.framebuffer(self.texture2, depth_attachment)

        self.sampler1 = self.ctx.sampler(self.texture1)
        self.sampler2 = self.ctx.sampler(self.texture2)

        self.scope1 = self.ctx.scope(mgl.DEPTH_TEST,
                                     self.fbo,
                                     samplers=[(self.sampler1, 0)])
        self.scope2 = self.ctx.scope(mgl.DEPTH_TEST,
                                     self.ctx.screen,
                                     samplers=[(self.sampler2, 0)])
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;
                uniform sampler2D Heightmap;

                in vec2 in_vert;
                out vec2 v_text;

                void main() {
                    vec4 vertex = vec4(in_vert - 0.5, texture(Heightmap, in_vert).r * 0.2, 1.0);
                    gl_Position = Mvp * vertex;
                    v_text = in_vert;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform sampler2D Heightmap;

                uniform sampler2D Color1;
                uniform sampler2D Color2;

                uniform sampler2D Cracks;
                uniform sampler2D Darken;

                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float height = texture(Heightmap, v_text).r;
                    float border = smoothstep(0.5, 0.7, height);

                    vec3 color1 = texture(Color1, v_text * 7.0).rgb;
                    vec3 color2 = texture(Color2, v_text * 6.0).rgb;

                    vec3 color = color1 * (1.0 - border) + color2 * border;

                    color *= 0.8 + 0.2 * texture(Darken, v_text * 3.0).r;
                    color *= 0.5 + 0.5 * texture(Cracks, v_text * 5.0).r;
                    color *= 0.5 + 0.5 * height;

                    f_color = vec4(color, 1.0);
                }
            ''',
        )

        self.mvp = self.prog['Mvp']

        vertices, index = terrain(32)

        self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes())
        self.ibo = self.ctx.buffer(index.astype('i4').tobytes())

        vao_content = [
            (self.vbo, '2f', 'in_vert'),
        ]

        self.vao = self.ctx.vertex_array(self.prog, vao_content, self.ibo)

        img0 = Image.open(data.find('heightmap.jpg')).convert('L').transpose(Image.FLIP_TOP_BOTTOM)
        img1 = Image.open(data.find('grass.jpg')).convert('RGB').transpose(Image.FLIP_TOP_BOTTOM)
        img2 = Image.open(data.find('rock.jpg')).convert('RGB').transpose(Image.FLIP_TOP_BOTTOM)
        img3 = Image.open(data.find('cracks.jpg')).convert('L').transpose(Image.FLIP_TOP_BOTTOM)
        img4 = Image.open(data.find('checked.jpg')).convert('L').transpose(Image.FLIP_TOP_BOTTOM)

        tex0 = self.ctx.texture(img0.size, 1, img0.tobytes())
        tex1 = self.ctx.texture(img1.size, 3, img1.tobytes())
        tex2 = self.ctx.texture(img2.size, 3, img2.tobytes())
        tex3 = self.ctx.texture(img3.size, 1, img3.tobytes())
        tex4 = self.ctx.texture(img4.size, 1, img4.tobytes())

        tex0.build_mipmaps()
        tex1.build_mipmaps()
        tex2.build_mipmaps()
        tex3.build_mipmaps()
        tex4.build_mipmaps()

        tex0.use(0)
        tex1.use(1)
        tex2.use(2)
        tex3.use(3)
        tex4.use(4)

        self.prog['Heightmap'].value = 0
        self.prog['Color1'].value = 1
        self.prog['Color2'].value = 2
        self.prog['Cracks'].value = 3
        self.prog['Darken'].value = 4
예제 #22
0
    def __init__(self):
        self.ctx = mgl.create_context()
        # import gltraces
        # mglprocs = mgl.glprocs(self.ctx)
        # gltraces.glprocs[:] = mglprocs
        # mglprocs[:] = gltraces.gltraces

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                }
            ''',
        )

        obj1 = Obj.open(data.find('crate_left.obj'))
        obj2 = Obj.open(data.find('crate.obj'))
        obj3 = Obj.open(data.find('crate_right.obj'))

        img1 = Image.open(data.find('crate.png')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB')
        img2 = Image.open(data.find('rock.jpg')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB')

        self.texture1 = self.ctx.texture(img1)
        self.texture2 = self.ctx.texture(img2)

        self.sampler1 = self.ctx.sampler(self.texture1)
        self.sampler2 = self.ctx.sampler(self.texture2)

        self.vbo1 = self.ctx.buffer(obj1.pack('vx vy vz nx ny nz tx ty'))
        self.vbo2 = self.ctx.buffer(obj2.pack('vx vy vz nx ny nz tx ty'))
        self.vbo3 = self.ctx.buffer(obj3.pack('vx vy vz nx ny nz tx ty'))

        self.vao1 = self.ctx.simple_vertex_array(self.prog, self.vbo1, 'in_vert', 'in_norm', 'in_text')
        self.vao1.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler2, 0)])

        self.vao2 = self.ctx.simple_vertex_array(self.prog, self.vbo2, 'in_vert', 'in_norm', 'in_text')
        self.vao2.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler1, 0)])

        self.vao3 = self.ctx.simple_vertex_array(self.prog, self.vbo3, 'in_vert', 'in_norm', 'in_text')
        self.vao3.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler2, 0)])

        with self.ctx.recorder:
            self.ctx.clear(1.0, 1.0, 1.0)
            self.vao1.render()
            self.vao2.render()
            self.vao3.render()

        self.bytecode = self.ctx.recorder.dump()
        print(self.bytecode)
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform vec3 Color;
                uniform bool UseTexture;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    if (UseTexture) {
                        f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                    } else {
                        f_color = vec4(Color * lum, 1.0);
                    }
                }
            ''',
        )

        self.objects = {}

        for name in ['ground', 'grass', 'billboard', 'billboard-holder', 'billboard-image']:
            obj = Obj.open(data.find('scene-1-%s.obj' % name))
            vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
            vao = self.ctx.simple_vertex_array(self.prog, vbo, 'in_vert', 'in_norm', 'in_text')
            self.objects[name] = vao

        img = Image.open(data.find('infographic-1.jpg')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.texture1 = self.ctx.texture(img.size, 3, img.tobytes())
        # self.texture1.build_mipmaps()

        self.texture2 = self.ctx.texture(self.wnd.size, 3)
        depth_attachment = self.ctx.depth_renderbuffer(self.wnd.size)
        self.fbo = self.ctx.framebuffer(self.texture2, depth_attachment)

        self.sampler1 = self.ctx.sampler(self.texture1)
        self.sampler2 = self.ctx.sampler(self.texture2)

        self.scope1 = self.ctx.scope(mgl.DEPTH_TEST, self.fbo, samplers=[(self.sampler1, 0)])
        self.scope2 = self.ctx.scope(mgl.DEPTH_TEST, self.ctx.screen, samplers=[(self.sampler2, 0)])
예제 #24
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_move;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert + in_move, 1.0);
                    v_vert = in_vert + in_move;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                }
            ''',
        )

        self.mvp = self.prog['Mvp']
        self.light = self.prog['Light']

        obj = Obj.open(data.find('crate.obj'))
        img = Image.open(data.find('crate.png')).transpose(
            Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.texture.build_mipmaps()
        self.texture.use()

        self.vbo1 = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
        self.vbo2 = self.ctx.buffer(reserve=12 * 1024)
        self.vao = self.ctx.vertex_array(self.prog, [
            (self.vbo1, '3f 3f 2f', 'in_vert', 'in_norm', 'in_text'),
            (self.vbo2, '3f/i', 'in_move'),
        ])

        self.crate_a = np.random.uniform(0.7, 0.8, 32 * 32)
        self.crate_b = np.random.uniform(0.0, 6.3, 32 * 32)
        self.crate_x = (np.tile(np.arange(32), 32) - 16) * 1.5
        self.crate_y = (np.repeat(np.arange(32), 32) - 16) * 1.5
        self.crate_x += np.random.uniform(-0.2, 0.2, 32 * 32)
        self.crate_y += np.random.uniform(-0.2, 0.2, 32 * 32)
    def __init__(self):
        self.ctx = mgl.create_context()

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;

                in vec3 in_color;
                in vec3 in_origin;
                in mat3 in_basis;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec3 v_color;

                void main() {
                    v_vert = in_origin + in_basis * in_vert;
                    v_norm = in_basis * in_norm;
                    v_color = in_color;
                    gl_Position = Mvp * vec4(v_vert, 1.0);
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec3 v_color;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(v_color * lum, 1.0);
                }
            ''',
        )

        obj = Obj.open(data.find('lowpoly_toy_car.obj'))

        self.vbo1 = self.ctx.buffer(obj.pack('vx vy vz nx ny nz'))
        self.vbo2 = self.ctx.buffer(struct.pack(
            '15f',
            1.0, 1.0, 1.0,
            0.0, 0.0, 0.0,
            1.0, 0.0, 0.0,
            0.0, 1.0, 0.0,
            0.0, 0.0, 1.0,
        ) * len(cars))
        self.vao = self.ctx.vertex_array(self.prog, [
            (self.vbo1, '3f 3f', 'in_vert', 'in_norm'),
            (self.vbo2, '3f 3f 9f/i', 'in_color', 'in_origin', 'in_basis'),
        ])
예제 #26
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.ctx = mgl.create_context()

        # import gltraces
        # mglprocs = mgl.glprocs(self.ctx)
        # gltraces.glprocs[:] = mglprocs
        # mglprocs[:] = gltraces.gltraces

        self.prog1 = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec3 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec3 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform samplerCube Sampler;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec3 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(texture(Sampler, normalize(v_text)).rgb * lum, 1.0);
                }
            ''',
        )

        obj = Obj.open(data.find('texture-test-cube.obj'))
        self.texture1 = self.ctx.texture_cube((512, 512), 3, np.random.randint(128, 255, (512, 512, 3, 6), 'u1').tobytes())
        self.depth1 = self.ctx.texture_cube((512, 512), 1, None, dtype='d3')
        self.sampler1 = self.ctx.sampler(self.texture1, filter=mgl.LINEAR)
        self.fbo1 = self.ctx.framebuffer(self.texture1, self.depth1)
        # exit()

        self.vbo1 = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty tz'))
        self.vao1 = self.ctx.simple_vertex_array(self.prog1, self.vbo1, 'in_vert', 'in_norm', 'in_text')
        self.vao1.scope = self.ctx.scope(mgl.DEPTH_TEST, self.ctx.screen, samplers=[(self.sampler1, 0)])

        self.prog2 = self.ctx.program(
            vertex_shader='''
                #version 330

                in vec3 in_vert;
                in vec3 in_norm;
                in vec3 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec3 v_text;

                void main() {
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            geometry_shader='''
                #version 330
                layout(triangles) in;
                layout(triangle_strip, max_vertices=18) out;

                in vec3 v_vert[3];
                in vec3 v_norm[3];
                in vec3 v_text[3];

                out vec3 g_vert;
                out vec3 g_norm;
                out vec3 g_text;

                uniform mat4 Mvp[6];

                void main() {
                    for (int layer = 0; layer < 6; ++layer) {
                        gl_Layer = layer;
                        for (int i = 0; i < 3; ++i) {
                            g_vert = v_vert[i];
                            g_norm = v_norm[i];
                            g_text = v_text[i];
                            gl_Position = Mvp[layer] * vec4(g_vert, 1.0);
                            EmitVertex();
                        }
                        EndPrimitive();
                    }
                }
            ''',
            fragment_shader='''
                #version 330

                uniform sampler3D Sampler;
                uniform vec3 Light;

                in vec3 g_vert;
                in vec3 g_norm;
                in vec3 g_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - g_vert), normalize(g_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(texture(Sampler, g_text).rgb * lum, 1.0);
                }
            ''',
        )

        obj = Obj.open(data.find('test_scene.obj'))
        self.texture2 = self.ctx.texture((4, 4, 4), 3, np.repeat(np.random.randint(180, 220, (4, 4, 4), 'u1'), 3))
        self.sampler2 = self.ctx.sampler(self.texture2, wrap=mgl.REPEAT_X | mgl.REPEAT_Y | mgl.REPEAT_Z, filter=mgl.LINEAR)

        self.vbo2 = self.ctx.buffer(obj.pack('vx vy vz nx ny nz vx vy vz'))
        self.vao2 = self.ctx.simple_vertex_array(self.prog2, self.vbo2, 'in_vert', 'in_norm', 'in_text')
        self.vao2.scope = self.ctx.scope(mgl.DEPTH_TEST, self.fbo1, samplers=[(self.sampler2, 0)])
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # import gltraces
        # mglprocs = mgl.glprocs(self.ctx)
        # gltraces.glprocs[:] = mglprocs
        # mglprocs[:] = gltraces.gltraces

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform vec3 Color;
                uniform bool UseTexture;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    if (UseTexture) {
                        f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                    } else {
                        f_color = vec4(Color * lum, 1.0);
                    }
                }
            ''',
        )

        self.compute = self.ctx.compute_shader('''
            #version 430
            layout (local_size_x = 16, local_size_y = 16) in;
            layout(rg32f,location=0) writeonly uniform image2D destTex;
            uniform float time;
            void main() {
                ivec2 ij = ivec2(gl_GlobalInvocationID.xy);
                float localCoef = length(vec2(ivec2(gl_LocalInvocationID.xy)-8)/8.0);
                float globalCoef = sin(float(gl_WorkGroupID.x+gl_WorkGroupID.y)*0.1 + time)*0.5;
                imageStore(destTex, ij, vec4(1.0-globalCoef*localCoef, 0.0, 0.0, 0.0));
            }
        ''')

        self.objects = {}

        for name in [
                'ground', 'grass', 'billboard', 'billboard-holder',
                'billboard-image'
        ]:
            obj = Obj.open(data.find('scene-1-%s.obj' % name))
            vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
            vao = self.ctx.simple_vertex_array(self.prog, vbo, 'in_vert',
                                               'in_norm', 'in_text')
            self.objects[name] = vao

        img = Image.new('RGB', (512, 512))
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.sampler = self.ctx.sampler(self.texture)
        self.scope = self.ctx.scope(mgl.DEPTH_TEST,
                                    self.ctx.screen,
                                    samplers=[(self.sampler, 0)])
예제 #28
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        # import gltraces
        # mglprocs = mgl.glprocs(self.ctx)
        # gltraces.glprocs[:] = mglprocs
        # mglprocs[:] = gltraces.gltraces

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;

                out vec3 v_vert;
                out vec3 v_norm;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform samplerCube Sampler;
                uniform vec3 Eye;

                in vec3 v_vert;
                in vec3 v_norm;

                out vec4 f_color;

                void main() {
                    f_color = texture(Sampler, reflect(v_vert - Eye, v_norm));
                }
            ''',
        )

        img1 = Image.new('RGB', (200, 200), '#fee')
        img2 = Image.new('RGB', (200, 200), '#efe')
        img3 = Image.new('RGB', (200, 200), '#eef')
        img4 = Image.new('RGB', (200, 200), '#ffe')
        img5 = Image.new('RGB', (200, 200), '#fef')
        img6 = Image.new('RGB', (200, 200), '#eff')

        ImageDraw.ImageDraw(img1).text((50, 30), 'i1', '#000', ImageFont.truetype('arial', 128))
        ImageDraw.ImageDraw(img2).text((50, 30), 'i2', '#000', ImageFont.truetype('arial', 128))
        ImageDraw.ImageDraw(img3).text((50, 30), 'i3', '#000', ImageFont.truetype('arial', 128))
        ImageDraw.ImageDraw(img4).text((50, 30), 'i4', '#000', ImageFont.truetype('arial', 128))
        ImageDraw.ImageDraw(img5).text((50, 30), 'i5', '#000', ImageFont.truetype('arial', 128))
        ImageDraw.ImageDraw(img6).text((50, 30), 'i6', '#000', ImageFont.truetype('arial', 128))

        def join(*images):
            return b''.join(img.tobytes('raw', 'RGB', 0, -1) for img in images)

        self.texture = self.ctx.texture_cube((200, 200), 3, join(img1, img2, img3, img4, img5, img6))
        self.sampler = self.ctx.sampler(self.texture)
        self.sampler.filter = mgl.LINEAR
        self.sampler.use()

        obj = Obj.open(data.find('sitting_dummy.obj'))
        self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz'))
        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm')
        self.scope = self.ctx.scope(mgl.DEPTH_TEST)
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform vec3 Color;
                uniform bool UseTexture;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    if (UseTexture) {
                        f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                    } else {
                        f_color = vec4(Color * lum, 1.0);
                    }
                }
            ''',
        )

        self.mvp = self.prog['Mvp']
        self.light = self.prog['Light']
        self.color = self.prog['Color']
        self.use_texture = self.prog['UseTexture']

        self.objects = {}

        for name in ['ground', 'grass', 'billboard', 'billboard-holder', 'billboard-image']:
            obj = Obj.open(data.find('scene-1-%s.obj' % name))
            vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
            vao = self.ctx.simple_vertex_array(self.prog, vbo, 'in_vert', 'in_norm', 'in_text')
            self.objects[name] = vao

        figure_size = (640, 360)

        temp = io.BytesIO()
        plt.figure(0, figsize=(figure_size[0] / 72, figure_size[1] / 72))

        mu, sigma = 100, 15
        x = mu + sigma * np.random.randn(10000)
        n, bins, patches = plt.hist(x, 50, normed=1, facecolor='r', alpha=0.75)

        plt.axis([40, 160, 0, 0.03])
        plt.grid(True)
        plt.show()

        plt.savefig(temp, format='raw', dpi=72)
        temp.seek(0)

        img = Image.frombytes('RGBA', figure_size, temp.read()).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.texture.build_mipmaps()
예제 #30
0
    def __init__(self):
        self.ctx = mgl.create_context()

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform vec3 Color;
                uniform bool UseTexture;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    if (UseTexture) {
                        f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                    } else {
                        f_color = vec4(Color * lum, 1.0);
                    }
                }
            ''',
        )

        self.objects = {}

        for name in [
                'ground', 'grass', 'billboard', 'billboard-holder',
                'billboard-image'
        ]:
            obj = Obj.open(data.find('scene-1-%s.obj' % name))
            vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
            vao = self.ctx.simple_vertex_array(self.prog, vbo, 'in_vert',
                                               'in_norm', 'in_text')
            self.objects[name] = vao

        img = Image.open(data.find('infographic-1.jpg')).transpose(
            Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.sampler = self.ctx.sampler(self.texture)
예제 #31
0
    def __init__(self):
        self.ctx = mgl.create_context()

        self.canvas_prog = self.ctx.program(
            vertex_shader='''
                #version 330

                in vec2 in_vert;
                out vec2 v_vert;

                void main() {
                    gl_Position = vec4(in_vert * 2.0 - 1.0, 0.0, 1.0);
                    v_vert = in_vert;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform sampler2D Texture;

                in vec2 v_vert;

                out vec4 f_color;

                void main() {
                    f_color = texture(Texture, v_vert);
                }
            ''',
        )

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    vec3 base = vec3(0.5, 0.5, 0.5) * lum;
                    vec3 spec = vec3(1.0, 1.0, 1.0) * pow(lum, 5.7);
                    vec4 tex = texture(Texture, v_text);
                    f_color = vec4(base * 0.1 + tex.rgb * lum + spec, tex.a);
                }
            ''',
        )

        self.canvas_vbo = self.ctx.buffer(
            np.array([0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0],
                     dtype='f4').tobytes())
        self.canvas_vao = self.ctx.simple_vertex_array(self.canvas_prog,
                                                       self.canvas_vbo,
                                                       'in_vert')

        bg_img = Image.open(data.find('mug-background.jpg')).transpose(
            Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.bg_texture = self.ctx.texture(bg_img.size, 3, bg_img.tobytes())
        self.bg_sampler = self.ctx.sampler(self.bg_texture)

        sticker_img = Image.open(data.find('mug-pymet-logo.png')).transpose(
            Image.FLIP_TOP_BOTTOM).convert('RGBA')
        self.sticker_texture = self.ctx.texture(sticker_img.size, 4,
                                                sticker_img.tobytes())
        # self.sticker_texture.build_mipmaps(0, 2)
        self.sticker_sampler = self.ctx.sampler(self.sticker_texture)

        self.mug_texture = self.ctx.texture((1, 1), 3)
        self.mug_texture.write(struct.pack('3B', 10, 10, 10))
        self.mug_sampler = self.ctx.sampler(self.mug_texture)

        obj = Obj.open(data.find('mug.obj'))
        self.mug_vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
        self.mug_vao = self.ctx.simple_vertex_array(self.prog, self.mug_vbo,
                                                    'in_vert', 'in_norm',
                                                    'in_text')

        segs = 32
        radius = 29.94
        bottom = 6.601
        top = 57.856
        left = -163.12 * np.pi / 180.0
        right = 11.25 * np.pi / 180.0

        lin = np.linspace(left, right, segs)
        sticker_vertices = np.array([
            np.repeat(np.cos(lin) * radius, 2),
            np.repeat(np.sin(lin) * radius, 2),
            np.tile([bottom, top], segs),
            np.repeat(np.cos(lin), 2),
            np.repeat(np.sin(lin), 2),
            np.tile([0.0, 0.0], segs),
            np.repeat(np.linspace(0.0, 1.0, segs), 2),
            np.tile([0.0, 1.0], segs),
        ])

        self.sticker_vbo = self.ctx.buffer(
            sticker_vertices.T.astype('f4').tobytes())
        self.sticker_vao = self.ctx.simple_vertex_array(
            self.prog, self.sticker_vbo, 'in_vert', 'in_norm', 'in_text')
예제 #32
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.canvas_prog = self.ctx.program(
            vertex_shader='''
                #version 330

                in vec2 in_vert;
                out vec2 v_vert;

                void main() {
                    gl_Position = vec4(in_vert * 2.0 - 1.0, 0.0, 1.0);
                    v_vert = in_vert;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform sampler2D Texture;

                in vec2 v_vert;

                out vec4 f_color;

                void main() {
                    f_color = texture(Texture, v_vert);
                }
            ''',
        )

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    vec3 base = vec3(0.5, 0.5, 0.5) * lum;
                    vec3 spec = vec3(1.0, 1.0, 1.0) * pow(lum, 5.7);
                    vec4 tex = texture(Texture, v_text);
                    f_color = vec4(base * 0.1 + tex.rgb * lum + spec, tex.a);
                }
            ''',
        )

        self.canvas_vbo = self.ctx.buffer(np.array([0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0], dtype='f4').tobytes())
        self.canvas_vao = self.ctx.simple_vertex_array(self.canvas_prog, self.canvas_vbo, 'in_vert')

        bg_img = Image.open(data.find('mug-background.jpg')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.bg_texture = self.ctx.texture(bg_img.size, 3, bg_img.tobytes())

        self.mvp = self.prog['Mvp']
        self.light = self.prog['Light']

        sticker_img = Image.open(data.find('mug-pymet-logo.png')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGBA')
        self.sticker_texture = self.ctx.texture(sticker_img.size, 4, sticker_img.tobytes())
        self.sticker_texture.build_mipmaps(0, 2)

        self.mug_texture = self.ctx.texture((1, 1), 3)
        self.mug_texture.write(struct.pack('3B', 10, 10, 10))

        obj = Obj.open(data.find('mug.obj'))
        self.mug_vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
        self.mug_vao = self.ctx.simple_vertex_array(self.prog, self.mug_vbo, 'in_vert', 'in_norm', 'in_text')

        segs = 32
        radius = 29.94
        bottom = 6.601
        top = 57.856
        left = -163.12 * np.pi / 180.0
        right = 11.25 * np.pi / 180.0

        lin = np.linspace(left, right, segs)
        sticker_vertices = np.array([
            np.repeat(np.cos(lin) * radius, 2),
            np.repeat(np.sin(lin) * radius, 2),
            np.tile([bottom, top], segs),
            np.repeat(np.cos(lin), 2),
            np.repeat(np.sin(lin), 2),
            np.tile([0.0, 0.0], segs),
            np.repeat(np.linspace(0.0, 1.0, segs), 2),
            np.tile([0.0, 1.0], segs),
        ])

        self.sticker_vbo = self.ctx.buffer(sticker_vertices.T.astype('f4').tobytes())
        self.sticker_vao = self.ctx.simple_vertex_array(self.prog, self.sticker_vbo, 'in_vert', 'in_norm', 'in_text')
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # import gltraces
        # mglprocs = mgl.glprocs(self.ctx)
        # gltraces.glprocs[:] = mglprocs
        # mglprocs[:] = gltraces.gltraces

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform vec3 Color;
                uniform bool UseTexture;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    if (UseTexture) {
                        f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                    } else {
                        f_color = vec4(Color * lum, 1.0);
                    }
                }
            ''',
        )

        self.compute = self.ctx.compute_shader('''
            #version 430
            layout (local_size_x = 16, local_size_y = 16) in;
            layout(rg32f,location=0) writeonly uniform image2D destTex;
            uniform float time;
            void main() {
                ivec2 ij = ivec2(gl_GlobalInvocationID.xy);
                float localCoef = length(vec2(ivec2(gl_LocalInvocationID.xy)-8)/8.0);
                float globalCoef = sin(float(gl_WorkGroupID.x+gl_WorkGroupID.y)*0.1 + time)*0.5;
                imageStore(destTex, ij, vec4(1.0-globalCoef*localCoef, 0.0, 0.0, 0.0));
            }
        ''')

        self.objects = {}

        for name in ['ground', 'grass', 'billboard', 'billboard-holder', 'billboard-image']:
            obj = Obj.open(data.find('scene-1-%s.obj' % name))
            vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
            vao = self.ctx.simple_vertex_array(self.prog, vbo, 'in_vert', 'in_norm', 'in_text')
            self.objects[name] = vao

        img = Image.new('RGB', (512, 512))
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.sampler = self.ctx.sampler(self.texture)
        self.scope = self.ctx.scope(mgl.DEPTH_TEST, self.ctx.screen, samplers=[(self.sampler, 0)])
예제 #34
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform vec4 Camera;

                // Per vertex
                in vec2 in_vert;
                in vec2 in_texture;

                // Per instance
                in vec3 in_pos;
                in vec2 in_size;
                in vec4 in_tint;

                out vec2 v_vert;
                out vec2 v_texture;
                out vec4 v_tint;

                void main() {
                    mat2 rotate = mat2(
                        cos(in_pos.z), sin(in_pos.z),
                        -sin(in_pos.z), cos(in_pos.z)
                    );
                    v_vert = rotate * (in_vert * in_size) + in_pos.xy;
                    gl_Position = vec4((v_vert - Camera.xy) / Camera.zw, 0.0, 1.0);
                    v_texture = in_texture;
                    v_tint = in_tint;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform sampler2D Texture;

                in vec2 v_vert;
                in vec2 v_texture;
                in vec4 v_tint;

                out vec4 f_color;

                void main() {
                    vec4 tex = texture(Texture, v_texture);
                    vec3 color = tex.rgb * (1.0 - v_tint.a) + v_tint.rgb * v_tint.a;
                    f_color = vec4(color, tex.a);
            }
            ''',
        )

        img = Image.open(data.find('crate.png')).convert('RGBA')
        self.tex1 = self.ctx.texture(img.size, 4, img.tobytes())
        self.tex1.use(0)

        img = Image.open(data.find('ball.png')).convert('RGBA')
        self.tex2 = self.ctx.texture(img.size, 4, img.tobytes())
        self.tex2.use(1)

        vertices = np.array([
            -1.0,
            -1.0,
            0.0,
            0.0,
            -1.0,
            1.0,
            0.0,
            1.0,
            1.0,
            -1.0,
            1.0,
            0.0,
            1.0,
            1.0,
            1.0,
            1.0,
        ])

        vbo1 = self.ctx.buffer(vertices.astype('f4').tobytes())

        self.vbo2 = self.ctx.buffer(reserve=1024 * 1024)

        vao_content = [
            (vbo1, '2f 2f', 'in_vert', 'in_texture'),
            (self.vbo2, '3f 2f 4f/i', 'in_pos', 'in_size', 'in_tint'),
        ]

        self.vao = self.ctx.vertex_array(self.prog, vao_content)

        self.space = pymunk.Space()
        self.space.gravity = (0.0, -900.0)

        shape = pymunk.Segment(self.space.static_body, (5, 100), (595, 100),
                               1.0)
        shape.friction = 1.0
        self.space.add(shape)

        self.bodies = []
        self.balls = []

        for x in range(5):
            for y in range(10):
                size = 20
                mass = 10.0
                moment = pymunk.moment_for_box(mass, (size, size))
                body = pymunk.Body(mass, moment)
                body.position = Vec2d(300 + x * 50, 105 + y * (size + 0.1))
                shape = pymunk.Poly.create_box(body, (size, size))
                shape.friction = 0.3
                self.space.add(body, shape)
                self.bodies.append(body)
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_move;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert + in_move, 1.0);
                    v_vert = in_vert + in_move;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                }
            ''',
        )

        self.mvp = self.prog['Mvp']
        self.light = self.prog['Light']

        obj = Obj.open(data.find('crate.obj'))
        img = Image.open(data.find('crate.png')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.texture.build_mipmaps()
        self.texture.use()

        self.vbo1 = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
        self.vbo2 = self.ctx.buffer(reserve=12 * 1024)
        self.vao = self.ctx.vertex_array(self.prog, [
            (self.vbo1, '3f 3f 2f', 'in_vert', 'in_norm', 'in_text'),
            (self.vbo2, '3f/i', 'in_move'),
        ])

        self.crate_a = np.random.uniform(0.7, 0.8, 32 * 32)
        self.crate_b = np.random.uniform(0.0, 6.3, 32 * 32)
        self.crate_x = (np.tile(np.arange(32), 32) - 16) * 1.5
        self.crate_y = (np.repeat(np.arange(32), 32) - 16) * 1.5
        self.crate_x += np.random.uniform(-0.2, 0.2, 32 * 32)
        self.crate_y += np.random.uniform(-0.2, 0.2, 32 * 32)
예제 #36
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;

                in vec3 in_color;
                in vec3 in_origin;
                in mat3 in_basis;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec3 v_color;

                void main() {
                    v_vert = in_origin + in_basis * in_vert;
                    v_norm = in_basis * in_norm;
                    v_color = in_color;
                    gl_Position = Mvp * vec4(v_vert, 1.0);
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec3 v_color;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    f_color = vec4(v_color * lum, 1.0);
                }
            ''',
        )

        self.mvp = self.prog['Mvp']
        self.light = self.prog['Light']

        obj = Obj.open(data.find('lowpoly_toy_car.obj'))

        self.vbo1 = self.ctx.buffer(obj.pack('vx vy vz nx ny nz'))
        self.vbo2 = self.ctx.buffer(
            struct.pack(
                '15f',
                1.0,
                1.0,
                1.0,
                0.0,
                0.0,
                0.0,
                1.0,
                0.0,
                0.0,
                0.0,
                1.0,
                0.0,
                0.0,
                0.0,
                1.0,
            ) * len(cars))
        self.vao = self.ctx.vertex_array(self.prog, [
            (self.vbo1, '3f 3f', 'in_vert', 'in_norm'),
            (self.vbo2, '3f 3f 9f/i', 'in_color', 'in_origin', 'in_basis'),
        ])
예제 #37
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform vec4 Camera;

                // Per vertex
                in vec2 in_vert;
                in vec2 in_texture;

                // Per instance
                in vec3 in_pos;
                in vec2 in_size;
                in vec4 in_tint;

                out vec2 v_vert;
                out vec2 v_texture;
                out vec4 v_tint;

                void main() {
                    mat2 rotate = mat2(
                        cos(in_pos.z), sin(in_pos.z),
                        -sin(in_pos.z), cos(in_pos.z)
                    );
                    v_vert = rotate * (in_vert * in_size) + in_pos.xy;
                    gl_Position = vec4((v_vert - Camera.xy) / Camera.zw, 0.0, 1.0);
                    v_texture = in_texture;
                    v_tint = in_tint;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform sampler2D Texture;

                in vec2 v_vert;
                in vec2 v_texture;
                in vec4 v_tint;

                out vec4 f_color;

                void main() {
                    vec4 tex = texture(Texture, v_texture);
                    vec3 color = tex.rgb * (1.0 - v_tint.a) + v_tint.rgb * v_tint.a;
                    f_color = vec4(color, tex.a);
            }
            ''',
        )

        img = Image.open(data.find('crate.png')).convert('RGBA')
        self.tex1 = self.ctx.texture(img.size, 4, img.tobytes())
        self.tex1.use(0)

        img = Image.open(data.find('ball.png')).convert('RGBA')
        self.tex2 = self.ctx.texture(img.size, 4, img.tobytes())
        self.tex2.use(1)

        vertices = np.array([
            -1.0, -1.0, 0.0, 0.0,
            -1.0, 1.0, 0.0, 1.0,
            1.0, -1.0, 1.0, 0.0,
            1.0, 1.0, 1.0, 1.0,
        ])

        vbo1 = self.ctx.buffer(vertices.astype('f4').tobytes())

        self.vbo2 = self.ctx.buffer(reserve=1024 * 1024)

        vao_content = [
            (vbo1, '2f 2f', 'in_vert', 'in_texture'),
            (self.vbo2, '3f 2f 4f/i', 'in_pos', 'in_size', 'in_tint'),
        ]

        self.vao = self.ctx.vertex_array(self.prog, vao_content)

        self.space = pymunk.Space()
        self.space.gravity = (0.0, -900.0)

        shape = pymunk.Segment(self.space.static_body, (5, 100), (595, 100), 1.0)
        shape.friction = 1.0
        self.space.add(shape)

        self.bodies = []
        self.balls = []

        for x in range(5):
            for y in range(10):
                size = 20
                mass = 10.0
                moment = pymunk.moment_for_box(mass, (size, size))
                body = pymunk.Body(mass, moment)
                body.position = Vec2d(300 + x * 50, 105 + y * (size + 0.1))
                shape = pymunk.Poly.create_box(body, (size, size))
                shape.friction = 0.3
                self.space.add(body, shape)
                self.bodies.append(body)
예제 #38
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    gl_Position = Mvp * vec4(in_vert, 1.0);
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                }
            ''',
            fragment_shader='''
                #version 330

                uniform vec3 Light;
                uniform vec3 Color;
                uniform bool UseTexture;
                uniform sampler2D Texture;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
                    if (UseTexture) {
                        f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
                    } else {
                        f_color = vec4(Color * lum, 1.0);
                    }
                }
            ''',
        )

        self.mvp = self.prog['Mvp']
        self.light = self.prog['Light']
        self.color = self.prog['Color']
        self.use_texture = self.prog['UseTexture']

        self.objects = {}

        for name in [
                'ground', 'grass', 'billboard', 'billboard-holder',
                'billboard-image'
        ]:
            obj = Obj.open(data.find('scene-1-%s.obj' % name))
            vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
            vao = self.ctx.simple_vertex_array(self.prog, vbo, 'in_vert',
                                               'in_norm', 'in_text')
            self.objects[name] = vao

        figure_size = (640, 360)

        temp = io.BytesIO()
        plt.figure(0, figsize=(figure_size[0] / 72, figure_size[1] / 72))

        mu, sigma = 100, 15
        x = mu + sigma * np.random.randn(10000)
        n, bins, patches = plt.hist(x, 50, normed=1, facecolor='r', alpha=0.75)

        plt.axis([40, 160, 0, 0.03])
        plt.grid(True)
        plt.show()

        plt.savefig(temp, format='raw', dpi=72)
        temp.seek(0)

        img = Image.frombytes('RGBA', figure_size, temp.read()).transpose(
            Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.texture.build_mipmaps()