Love2d GLSL Shaders

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
xXxMoNkEyMaNxXx
Party member
Posts: 206
Joined: Thu Jan 10, 2013 6:16 am
Location: Canada

Re: Love2d GLSL Shaders

Post by xXxMoNkEyMaNxXx »

What does "uniform sampler2D tex0;" do?
User avatar
spynaz
Party member
Posts: 152
Joined: Thu Feb 28, 2013 5:49 am

Re: Love2d GLSL Shaders

Post by spynaz »

xXxMoNkEyMaNxXx wrote:What does "uniform sampler2D tex0;" do?
I don't know. :/
User avatar
retrotails
Party member
Posts: 212
Joined: Wed Apr 18, 2012 12:37 am

Re: Love2d GLSL Shaders

Post by retrotails »

xXxMoNkEyMaNxXx wrote:What does "uniform sampler2D tex0;" do?
It stops the compiler from complaining. So it's been at the top of all my shaders.
User avatar
spynaz
Party member
Posts: 152
Joined: Thu Feb 28, 2013 5:49 am

Re: Love2d GLSL Shaders

Post by spynaz »

Retrotails, the GLSL code you told me didn't do anything to the rectangle. It only made it invisible and no matter what I change the color values to, it still was invisible.
User avatar
retrotails
Party member
Posts: 212
Joined: Wed Apr 18, 2012 12:37 am

Re: Love2d GLSL Shaders

Post by retrotails »

spynaz wrote:That didn't do anything except just the rectangle was invisible.
Well, that's what happens when I don't test code.

Code: Select all

function love.load()
    t = 0
    shader = love.graphics.newPixelEffect [[
        extern number t      = 0.0;
        uniform sampler2D tex0;
        vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
        {
            vec4 col = vec4(1, 1, 1, cos(abs(mod(t,1) - .5)*3.14));
            return vec4( col);
        }
    ]]
end
function love.update(dt)
    t = t + dt
    if love.keyboard.isDown('escape') then love.event.quit() end
    shader:send('t', t)
end
function love.draw()
    love.graphics.setPixelEffect(shader)
    love.graphics.rectangle('fill', love.mouse.getX(), love.mouse.getY(), 64, 64)
    love.graphics.setPixelEffect()
end
User avatar
spynaz
Party member
Posts: 152
Joined: Thu Feb 28, 2013 5:49 am

Re: Love2d GLSL Shaders

Post by spynaz »

Alright that works but what does "math.mod" and "mod" do?
User avatar
retrotails
Party member
Posts: 212
Joined: Wed Apr 18, 2012 12:37 am

Re: Love2d GLSL Shaders

Post by retrotails »

spynaz wrote:Alright that works but what does "math.mod" and "mod" do?
The same thing, you just need 'math.' in front of it for Lua. It takes the remainder of something (or something like that, I don't know the wording.)
It basically subtracts the second number from the first until it's smaller then the 2nd number.
"mod(x, 1)" will just take a number and give you the decimal, so mod(69.2017, 1) = 0.2017
I like wolfram for playing with things
http://www.wolframalpha.com/input/?i=mod%2869.1%2C+1%29

http://www.wolframalpha.com/input/?i=mod%28x%2C+1%29
User avatar
spynaz
Party member
Posts: 152
Joined: Thu Feb 28, 2013 5:49 am

Re: Love2d GLSL Shaders

Post by spynaz »

Oh now I know, mod is modulus. But there are 2 ways to use it in Lua:

math.mod(10, 3) = 1
10 % 3 = 1

And modulus doesn't give you the decimal, it gives you the remainder of what is needed to make the second number go into the first. As you can see in the example above, 3 does not go into 10 but it goes into 9 which is only 1 number away from 10 so the value is 1.
Last edited by spynaz on Sat Mar 30, 2013 5:10 am, edited 1 time in total.
User avatar
xXxMoNkEyMaNxXx
Party member
Posts: 206
Joined: Thu Jan 10, 2013 6:16 am
Location: Canada

Re: Love2d GLSL Shaders

Post by xXxMoNkEyMaNxXx »

I can describe mod in one word:
clock

What's 14 o' clock?
14 mod 12 = 2

This gives you the idea of how it works, but there's one difference:
12 mod 12 is 0, not still 12 o' clock.
11.999 mod 12 = 11.999
12.000 mod 12 = 0.000
12.001 mod 12 = 0.001

As a side note:
You can, however, make it work like a clock like this:
(n-1)%12+1
Where % means mod. (It actually does in Lua, C++, and I suspect many other programming languages, but you can't do % on vectors in GLSL, you have to use mod(a,b))

EDIT: Dangit, you already knew about it.
User avatar
spynaz
Party member
Posts: 152
Joined: Thu Feb 28, 2013 5:49 am

Re: Love2d GLSL Shaders

Post by spynaz »

Yea, I already knew it. Lol. But I like what you said that it works kind of like a clock.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 7 guests