def _imposter_gen(res, vs, fs, n_row=9, dist=10): context = mg.create_standalone_context() vs = _read(vs) fs = _read(fs) program = context.program(vertex_shader=vs, fragment_shader=fs) vao = _screen_quad(program, context) u_campos = program['u_campos'] if "u_drawbg" in program: program["u_drawbg"].value = False atlas = Image.new("RGBA", (res, res)) winw, winh = int(res / n_row), int(res / n_row) window_tex = context.texture((winw, winh), 4, dtype="f4") frame = context.framebuffer([window_tex]) frame.use() for pos, uv in _rotate_around(n_row, dist): u_campos.value = pos vao.render() u = uv[0] * winw v = uv[1] * winh data = np.frombuffer(window_tex.read(), dtype="f4") data = data.reshape((winw, winh, 4)) data = _flatten_array(data) img = Image.fromarray(data) atlas.paste(img, (u, v)) return atlas
def recompile_shaders(self, path): print("recompiling shaders..", path) try: vs = _read("./_gl/simple.vs") fs = _read(path) program = self.context.program(vertex_shader=vs, fragment_shader=fs) self.u_time = program["u_time"] self.u_campos = program["u_campos"] self.u_campos.value = (0.0, 5.0, -10.0) self.u_focus = program["u_focus"] self.u_focus.value = (0.0, 2.0, 0.0) self.vao = _screen_quad(program, self.context) kju_data = ii.imread("./_tex/kju_sq.jpg") tex_w = kju_data.shape[0] tex_h = kju_data.shape[1] tex_c = kju_data.shape[2] _tex = self.context.texture((tex_w, tex_h), tex_c, kju_data.tobytes()) _tex.use(0) except Exception as e: print("failed to compile shaders, {}".format(e)) return print("recompiled shaders!")
def _compute_driven_generation(width, height, cs_path): x, y, z = 1024, 1, 1 cs_args = { 'X': x, 'Y': y, 'Z': z, 'WIDTH': width, 'HEIGHT': height, } cs = _read(cs_path, cs_args) context = mg.create_standalone_context() compute_shader = context.compute_shader(cs) in_data = np.random.uniform(0.0, 1.0, (width, height, 4)) out_data = np.zeros((width, height, 4)) in_buffer = context.buffer(in_data.astype('f4')) in_buffer.bind_to_storage_buffer(0) out_buffer = context.buffer(out_data.astype('f4')) out_buffer.bind_to_storage_buffer(1) compute_shader.run(x, y, z) data = np.frombuffer(out_buffer.read(), dtype='f4') data = data.reshape((height, width, 4)) return _flatten_array(data)
def _screenspace_generation(width, height, vspath, fspath, start_time=0.0, end_time=1.0, frames=1, **uniforms): vs = _read(vspath) fs = _read(fspath) context = mg.create_standalone_context() program = context.program(vertex_shader=vs, fragment_shader=fs) vao = _screen_quad(program, context) for k, v in uniforms.items(): if k not in program: continue program[k].value = v test_texture = context.texture((width, height), 4) test_texture.use(0) frame_tex = context.texture((width, height), 4, dtype='f4') frame = context.framebuffer([frame_tex]) frame.use() u_time = {"value": 0.0} if "u_time" in program: u_time = program["u_time"] span = end_time - start_time step = span / max(float(frames), 0.0) for t in range(int(frames)): u_time.value = start_time + step * t vao.render() result_bytes = frame_tex.read() data = np.frombuffer(result_bytes, dtype='f4') data = data.reshape((height, width, 4)) yield _flatten_array(data)