Difference between revisions of "Tutorial:Introduction to Shaders"

(Created page with "This is a simple and basic tutorial explaining the basics of shaders. Here is a small example: <source lang="lua"> local effect = nil functi...")
 
Line 18: Line 18:
 
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.
 
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.
  
<source lang="glsl"">
+
<source lang="glsl">
 
vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords )
 
vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords )
 
</source>
 
</source>
Line 32: Line 32:
 
The value can be a number or a table of numbers for a vec2/3/4. Or more numbers for a matrix. [[Shader:send|Full list here]].
 
The value can be a number or a table of numbers for a vec2/3/4. Or more numbers for a matrix. [[Shader:send|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 * [http://love2d.org/forums/viewtopic.php?f=4&t=3733 "share a shader"] thread. That's the closest you can get right now.
+
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 [http://love2d.org/forums/viewtopic.php?f=4&t=3733 "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.
 
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.

Revision as of 03:06, 24 December 2013

This is a simple and basic tutorial explaining the basics of shaders. Here is a small example:

local effect = nil

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

function love.draw()
    love.graphics.setShader(shader)
    love.graphics.rectangle(0, 0, 100, 100)
    love.graphics.setShader(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.

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:

shader: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.

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
}

-- Kyle