Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Mandelbrot ESSL: an experiment with fragment shaders (Codea 1.5)

edited October 2017 in Code Sharing Posts: 489

The code below illustrates the performance benefits that can come with shaders, by comparison (for example) with the original Codea example project Mandelbrot:

image

--
-- Mandelbrot ESSL
--
-- With acknowledgements to:
-- Mandelbrot set explorer, by Dr. Phillip Alvelda
--
supportedOrientations(LANDSCAPE_ANY)
function setup()
    re, im, s = -0.65, 0, 1.35
    aspect = WIDTH / HEIGHT
    m = mesh()
    m:addRect(WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
    m.shader = shader("Patterns:Mandelbrot")
    img = image(WIDTH, HEIGHT)
    render()
    print("Tap Viewer to zoom in, Replay to restart.")
end

function draw()
    sprite(img, WIDTH/2, HEIGHT/2)
end

function touched(touch)
    if touch.state ~= ENDED then return end
    re = minRe + touch.x/WIDTH * s * 2 * aspect
    im = minIm + touch.y/HEIGHT * s * 2
    s = s/3
    render()
end

function render()
    minRe = re - s * aspect
    minIm = im - s
    m.shader.minRe = minRe
    m.shader.minIm = minIm
    m.shader.fRe = s * 2 * aspect
    m.shader.fIm = s * 2
    m.shader.maxIter = 255
    setContext(img)
    m:draw()
    setContext()
end
Tagged:
Sign In or Register to comment.