GLSL not explained well...

General discussion about LÖVE, Lua, game development, puns, and unicorns.
metzyn
Prole
Posts: 6
Joined: Wed Mar 27, 2013 10:51 pm
Location: TN, USA

GLSL not explained well...

Post by metzyn » Thu Mar 28, 2013 1:39 am

I do not understand how to implement GLSL code into my LOVE scripts. The wiki doesn't show much info at all so I am not sure how it works. I researched this forum and Google, yet there are no good tutorials as how to get started. Please help!

Kyle
Party member
Posts: 146
Joined: Sat Mar 16, 2013 9:46 pm

Re: GLSL not explained well...

Post by Kyle » Thu Mar 28, 2013 4:21 am

There's a lot of GLSL documentation on the internet if that's what you're after. As for using it in LOVE, here's some sample code:

Code: Select all

local effect = nil

function love.load()
effect = love.graphics.newPixelEffect(love.filesystem.read("shader.fs"))
end

function love.draw()
love.graphics.setPixelEffect(effect)
love.graphics.rectangle(0, 0, 100, 100)
love.graphics.setPixelEffect(nil)
end
The most important thing to remember when writing your shader is that the entry point is not the same as in pure GL. It's modified in LOVE.

Code: Select all

vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords )
So instead of setting gl_FragCoord, you'd return the vec4 for the pixel color. Your texture coordinates are supplied for you, as well as the current texture, the system-wide current color setting, and the screen coordinate position of the current pixel. In pure GL, you'd supply your own uniform values for that stuff.

You can also send data to the shader in the form of uniform variables, like I mentioned above:

Code: Select all

effect:send(name, value[s])
The value can be a number or a table of numbers for a vec2/3/4. Or more numbers for a matrix. Full list here.

If you're looking for a big repository of shaders already written for LOVE, you're out of luck as far as I know. There is, however, a "share a shader" thread. That's the closest you can get right now.

If you don't know how shaders work, you can think of them as a small program that the graphics processor runs for each pixel. When you draw a rectangle, OpenGL transforms those coordinates into pixels on the screen (rasterization) and then runs those pixels through your shader. It's a very fast and efficient way to make modifications to images on the fly, and modern photorealistic or otherwise graphics would be nearly impossible (extremely impractical) without them.

The effect function in your shader returns a vec4 that represents the color you want to choose for the current pixel. There are a many functions built-in for math and for reading from textures. Here's a sample effect that'd simply draw a black and white texture.

Code: Select all

vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords )
{
vec4 color = Texel(texture, texture_coords); //This reads a color from our texture at the coordinates LOVE gave us (0-1, 0-1)
return vec4(vec3(1.0, 1.0, 1.0) * (max(color.r, max(color.g, color.b))), 1.0); //This just returns a white color that's modulated by the brightest color channel at the given pixel in the texture. Nothing too complex, and not exactly the prettiest way to do B&W :P
}
That ended up longer than what I had originally intended to write. Hope it helps, and good luck with your foray into the wonderful territory of shaders!

User avatar
master both
Party member
Posts: 259
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: GLSL not explained well...

Post by master both » Thu Mar 28, 2013 4:37 am

^ this should be added to the wiki, its a really good explanation.

Kyle
Party member
Posts: 146
Joined: Sat Mar 16, 2013 9:46 pm

Re: GLSL not explained well...

Post by Kyle » Thu Mar 28, 2013 4:42 am

Ah, that's nice of you. :P

I might also add that pixel effects are called "fragment shaders" outside of LOVE, in case you go Googling for it. Most GLSL is compatible with LOVE's implementation, but keep in mind that LOVE limits its GLSL to version 1.2 (#version 120 or less would show at the top of any shader you might find). The wiki has a list of things that are renamed in LOVE's GLSL, as well, but when I looked through the source code, these renamings were made so that you didn't have to follow them.

User avatar
monsieur_h
Citizen
Posts: 65
Joined: Tue Oct 30, 2012 4:43 pm

Re: GLSL not explained well...

Post by monsieur_h » Thu Mar 28, 2013 12:35 pm

Neat explanation. You definetly should write on the blog about it. Would be of great use.

metzyn
Prole
Posts: 6
Joined: Wed Mar 27, 2013 10:51 pm
Location: TN, USA

Re: GLSL not explained well...

Post by metzyn » Fri Mar 29, 2013 1:26 am

That really helps! Thank you for taking the time to explain so much. I'm going to start messing with some of it now...

agwblack
Prole
Posts: 1
Joined: Thu Dec 19, 2013 11:48 am

Re: GLSL not explained well...

Post by agwblack » Thu Dec 19, 2013 11:52 am

I made an account just to say what a great explanation this is. I haven't much previous experience with shaders and trying to piece bits together from other (non-Love2D) orientated GLSL tutorials and apply it to a Love2D pixeleffect (shader, whatever - I'm still using 0.80) was taking a long time. This post brought everything into sudden focus. I agree with the other posters who suggest that a version of this is added to Wiki.

User avatar
slime
Solid Snayke
Posts: 2858
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: GLSL not explained well...

Post by slime » Tue Dec 24, 2013 12:38 am

master both wrote:^ this should be added to the wiki, its a really good explanation.
Anyone with a forum account can add to the wiki.

User avatar
master both
Party member
Posts: 259
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: GLSL not explained well...

Post by master both » Tue Dec 24, 2013 2:32 am

slime wrote:
master both wrote:^ this should be added to the wiki, its a really good explanation.
Anyone with a forum account can add to the wiki.
I tried to add the post as a tutorial (Introducction to Shaders), but I can't manage to do so, and in the discussion section there are people complaining that they nither can.

User avatar
slime
Solid Snayke
Posts: 2858
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: GLSL not explained well...

Post by slime » Tue Dec 24, 2013 2:41 am

Are you sure you're logged in to the wiki when you try? Sometimes I have issues where I have to log in multiple times for it to "stick".

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests