def noise1d(cfg, ndim=4, nb_layers=6, lacunarity=2.0, gain=0.5): random.seed(0) random_dim = 1<<ndim shader_header = '#version 100\nprecision highp float;\n' def get_rand(): return array.array('f', [random.uniform(0, 1) for x in range(random_dim)]) random_buf = BufferFloat(data=get_rand()) random_tex = Texture2D(data_src=random_buf, width=random_dim, height=1) quad = Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) prog = Program(fragment=shader_header + get_myfrag('noise-common') + get_myfrag('noise1d'), vertex=shader_header + get_myvert('noise')) utime_animkf = [AnimKeyFrameFloat(0, 0), AnimKeyFrameFloat(cfg.duration, 1)] utime = UniformFloat(anim=AnimatedFloat(utime_animkf)) render = Render(quad, prog) render.update_textures(tex0=random_tex) render.update_uniforms(dim=UniformInt(random_dim)) render.update_uniforms(nb_layers=UniformInt(nb_layers)) render.update_uniforms(time=utime) #render.update_uniforms(profile=UniformInt(1)) render.update_uniforms(lacunarity=UniformFloat(lacunarity)) render.update_uniforms(gain=UniformFloat(gain)) return render
def noise2d(cfg, ndim=4, nb_layers=6, lacunarity=2.0, gain=0.5): random.seed(0) random_dim = (1<<ndim) + 1 shader_header = '#version 100\nprecision highp float;\n' nb_gradients = random_dim**2 #print 'nb_gradients', nb_gradients #print 'dim', random_dim random_vals = _permuted_2d_gradients(nb_gradients) random_data = array.array('f', random_vals) random_buf = BufferVec2(data=random_data) random_tex = Texture2D(data_src=random_buf, width=random_dim, height=random_dim) quad = Quad((-1, 1, 0), (2, 0, 0), (0, -2, 0)) prog = Program(fragment=shader_header + get_myfrag('noise-common') + get_myfrag('noise2d')) utime_animkf = [AnimKeyFrameFloat(0, 0), AnimKeyFrameFloat(cfg.duration, 1)] utime = UniformFloat(anim=AnimatedFloat(utime_animkf)) render = Render(quad, prog) render.update_textures(tex0=random_tex) render.update_uniforms(dim=UniformInt(random_dim)) render.update_uniforms(nb_layers=UniformInt(nb_layers)) render.update_uniforms(time=utime) render.update_uniforms(profile=UniformInt(0)) render.update_uniforms(lacunarity=UniformFloat(lacunarity)) render.update_uniforms(gain=UniformFloat(gain)) return render
def noise3d(cfg, ndim=4, nb_layers=1, lacunarity=2.0, gain=0.5): random.seed(0) random_dim = (1<<ndim) + 1 #shader_header = '#version 100\nprecision highp float;\n' shader_header = '#version 130\n' if cfg.glbackend != 'gles' else '#version 310 es\n' nb_gradients = random_dim**2 grad_set = ( ( 0, 1, 1), ( 0, 1,-1), ( 0,-1, 1), ( 0,-1,-1), ( 1, 0, 1), ( 1, 0,-1), (-1, 0, 1), (-1, 0,-1), ( 1, 1, 0), ( 1,-1, 0), (-1, 1, 0), (-1,-1, 0), ) def get_rand(): #return array.array('f', _get_rand(random_dim**2, nb_comp=3)) r = [] for i in range(nb_gradients): r += random.choice(grad_set) return array.array('f', r) random_buffer = BufferVec3(data=get_rand()) random_tex = Texture2D(data_src=random_buffer, width=random_dim, height=random_dim) quad = Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) prog = Program(fragment=shader_header + get_myfrag('noise-common') + get_myfrag('noise3d'), vertex=shader_header + get_myvert('noise3d')) utime_animkf = [AnimKeyFrameFloat(0, 0), AnimKeyFrameFloat(cfg.duration/2.0, 1), AnimKeyFrameFloat(cfg.duration, 0)] utime = UniformFloat(anim=AnimatedFloat(utime_animkf)) render = Render(quad, prog) render.update_textures(tex0=random_tex) render.update_uniforms(dim=UniformInt(random_dim)) render.update_uniforms(nb_layers=UniformInt(nb_layers)) render.update_uniforms(time=utime) render.update_uniforms(lacunarity=UniformFloat(lacunarity)) render.update_uniforms(gain=UniformFloat(gain)) return render
def segment_debug(cfg, ndim=2): dim = 1 << ndim random.seed(0) # generate NxN RGB colors colors = [random.random() for x in range(dim**2 * 3)] c_data = array.array('f', colors) c_buf = BufferVec3(data=c_data) c_tex = Texture2D(data_src=c_buf, width=dim, height=dim) #quad = Quad((-1, 1, 0), (2, 0, 0), (0, -2, 0)) # origin in bottom left quad = Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) prog = Program(fragment=get_myfrag('segment')) render = Render(quad, prog) render.update_textures(tex0=c_tex) render.update_uniforms(dim=UniformInt(dim)) return render
def godtree(cfg, variance_split=35, variance_cont=5, birth_step=0.1, max_w=0.15, min_h=0.15, max_h=0.45, twist=0, proba_split=0.5, proba_cont=0.9, seed=0, debug_color=False, green=(0.29, 0.46, 0.29, 1.0), brown=(0.22, 0.21, 0.21, 1.0)): random.seed(seed) assert birth_step > 0 and birth_step <= 1 nb_generations = int(1. / birth_step) # one new generation every birth step tree_specs = _get_tree_specs(nb_generations, variance_split, variance_cont, proba_split, proba_cont, min_h, max_h) for t in range(nb_generations + 1): _add_tree_coords(tree_specs, t/float(nb_generations), birth_step, max_w, twist*pi/2.) if debug_color: fragment = get_myfrag('godtree-dbgcolor') else: ugreen = UniformVec4(green) ubrown = UniformVec4(brown) fragment = get_myfrag('godtree') program = Program(fragment=fragment) grow_time = cfg.duration / 2.0 # grow only during half the demo time def _get_renders(branch, level=0): renders = [] for _, child_branch in branch.children.items(): # Branch vertices and normals vertices_animkf = [] normals_animkf = [] for i, coords in enumerate(zip(branch.coords, child_branch.coords)): time = i * grow_time / float(nb_generations) vertices_data = array.array('f') normals_data = array.array('f') bottom_coords, top_coords = coords bottom_top_coords = zip(bottom_coords, top_coords) prev = bottom_top_coords[-1] for bottom1, top1 in bottom_top_coords: bottom0, top0 = prev prev = bottom1, top1 norm = vec3_normal(top0, bottom1) normals_data.extend(norm * 6) vertices_data.extend(bottom0 + bottom1 + top0 + bottom1 + top0 + top1) vertices_animkf.append(AnimKeyFrameBuffer(time, vertices_data)) normals_animkf.append(AnimKeyFrameBuffer(time, normals_data)) vertices = AnimatedBufferVec3(vertices_animkf) normals = AnimatedBufferVec3(normals_animkf) shape = Geometry(vertices, normals=normals, draw_mode=GL.GL_TRIANGLES) render = Render(shape, program) # Colors if debug_color: ucolor = UniformVec3([random.uniform(0.5, 1) for i in range(3)]) render.update_uniforms(color=ucolor) else: render.update_uniforms(green=ugreen, brown=ubrown) # Age time_birth = level * birth_step time_old = time_birth + 1.0 age_animkf = [AnimKeyFrameFloat(time_birth * grow_time, 0), AnimKeyFrameFloat(time_old * grow_time, 1)] uage = UniformFloat(anim=AnimatedFloat(age_animkf)) render.update_uniforms(age=uage) renders.append(render) renders += _get_renders(child_branch, level + 1) return renders renders = _get_renders(tree_specs) node = Group(children=renders) node = GraphicConfig(node, depth=ConfigDepth(GL.GL_TRUE)) rot_animkf = [AnimKeyFrameFloat(0, 0), AnimKeyFrameFloat(cfg.duration, 360*2)] node = Rotate(node, axis=(0,1,0), anim=AnimatedFloat(rot_animkf)) node = Translate(node, (0, -1, 0)) camera = Camera(node) return camera