Issues

Issue #99 resolved

GLSL shaders

vrld
created an issue

I would like to be able to program shaders with love to make things like blur, gloom and normal mapping possible. A starting point is here: http://bitbucket.org/vrld/love-glsl

Comments (3)

  1. vrld reporter

    Interface proposal:

    Query capabilities:

    status  = love.graphics.hasShaders()
    version = love.graphics.getGLSLVersion()
    

    Creation:

    shader = love.graphics.newShader()  -- create new shader object
    shader:addCode(type, glsl_code)     -- add and compile code. type is "fragment" or "vertex"
    shader:compile()                    -- link shader. may throw an error.
    

    To combine these steps, deploy a helper:

    -- in scripts/graphics.lua
    local __newShader = love.graphics.newShader
    function love.graphics.newShader(sources)
        assert( type(sources) == "table", "Invalid argument" )
    
        local shader = __newShader()
        for i,s in pairs(sources) do
            shader:addCode(unpack(s))
        end
        shader:compile()
        return shader
    end
    -- usage: shader = love.graphics.newShader{
    --                        {'fragment', lib_code},
    --                        {'fragment', main_code},
    --                        {'vertex', more_code} }
    

    Get compiler/linker warnings:

    log = shader:infolog()
    

    Attach/detach shader:

    love.graphics.setShader(shader) -- use shader on the following commands
    love.graphics.setShader()       -- don't use any shader
    

    Uniform and sampler access:

    shader:setUniform(varname, [type], u1, u2, u3, u4) -- type is optional, defaults to float.
                                                       -- if u2 to u4 are omitted, the uniform
                                                       -- is a float/int, else a vec2, vec3 or vec4
    
    shader:setMatrix(varname, rows, cols, m1, ..., mk) -- k = rows * cols
    
    shader:setSampler(varname, image, [texture-unit])  -- texture-unit defaults to 1 to not
                                                       -- overwrite the currently active texture
    
    u1, ..., u16 = shader:getUniform(varname, [type])  -- type defaults to float
    

    In addition to that, some shaders could be bundled with love, e.g.:

    • blur
    • bloom
    • desaturate/hsv color shift
    • colorize
    • edge detection
  2. Log in to comment