# ----------------------------------------------------------------------------- # Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved. # Distributed under the (new) BSD License. # ----------------------------------------------------------------------------- """ Mandelbrot set with pan & zoom """ import numpy as np from glumpy import app, gl, glm, gloo, __version__ from glumpy.transforms import PanZoom, Position vertex = """ attribute vec2 position; attribute vec2 texcoord; varying vec2 v_texcoord; void main() { gl_Position = ; v_texcoord = texcoord; } """ fragment = """ varying vec2 v_texcoord; vec3 hot(float t) { return vec3(smoothstep(0.00,0.33,t), smoothstep(0.33,0.66,t), smoothstep(0.66,1.00,t)); } void main() { const int n = 300; const float log_2 = 0.6931471805599453; vec2 c = 3.0*v_texcoord - vec2(2.0,1.5); float x, y, d; int i; vec2 z = c; for(i = 0; i < n; ++i) { x = (z.x*z.x - z.y*z.y) + c.x; y = (z.y*z.x + z.x*z.y) + c.y; d = x*x + y*y; if (d > 4.0) break; z = vec2(x,y); } if ( i < n ) { float nu = log(log(sqrt(d))/log_2)/log_2; float index = float(i) + 1.0 - nu; float v = pow(index/float(n),0.5); gl_FragColor = vec4(hot(v),1.0); } else { gl_FragColor = vec4(hot(0.0),1.0); } } """ window = app.Window(width=1024, height=1024) @window.event def on_draw(dt): window.clear() program.draw(gl.GL_TRIANGLE_STRIP) @window.event def on_key_press(key, modifiers): if key == app.window.key.SPACE: transform.reset() program = gloo.Program(vertex, fragment, count=4) program['position'] = [(-1,-1), (-1, 1), ( 1,-1), ( 1, 1)] program['texcoord'] = [( 0, 1), ( 0, 0), ( 1, 1), ( 1, 0)] transform = PanZoom(Position("position"),aspect=1) program['transform'] = transform window.attach(transform) app.run()