Skip to content

Instantly share code, notes, and snippets.

@MatthewBlanchard
Created September 10, 2018 01:45
Show Gist options
  • Save MatthewBlanchard/48f4405bf5b1b6c2eae4aabf65c28863 to your computer and use it in GitHub Desktop.
Save MatthewBlanchard/48f4405bf5b1b6c2eae4aabf65c28863 to your computer and use it in GitHub Desktop.
local defaultVert = [[
vec4 position(mat4 transform_projection, vec4 vertex_position)
{
// The order of operations matters when doing matrix multiplication.
return transform_projection * vertex_position;
}
]]
local gbufferPixel = [[
uniform Image MainTex;
extern Image normal;
void effect()
{
love_Canvases[0] = Texel(MainTex, VaryingTexCoord.xy);
love_Canvases[1] = Texel(normal, VaryingTexCoord.xy);
}
]]
local gbufferShader = love.graphics.newShader(defaultVert, gbufferPixel)
local pointLightShader = love.graphics.newShader(defaultVert, [[
extern Image normal;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords)
{
vec3 lightPos = vec3(400, 300, 100);
vec3 lightDir = normalize(lightPos - vec3(screen_coords, 0));
vec3 lightc = vec3(0.5, 0.5, 0.5);
vec3 albedo = vec3(Texel(texture, texture_coords));
vec3 normal = normalize(Texel(normal, texture_coords).xyz * 2.0 - 1.0) ;
vec3 diffuse = max(dot(lightDir, normal), 0.0) * lightc;
return vec4(diffuse * albedo, 1);
}
]])
function love.load()
local w, h = love.graphics.getDimensions()
albedo = love.graphics.newCanvas()
normal = love.graphics.newCanvas()
albedo:setFilter("nearest", "nearest", 0)
normal:setFilter("nearest", "nearest", 0)
amap = love.graphics.newImage("wallalbedo.png")
nmap = love.graphics.newImage("wallnormal.png")
amap:setFilter("nearest", "nearest")
nmap:setFilter("nearest", "nearest")
amap:setWrap("repeat")
nmap:setWrap("repeat")
tiles = love.graphics.newQuad( 0, 0, w, h, amap:getDimensions())
end
function love.draw()
love.graphics.push()
love.graphics.scale(2)
love.graphics.setCanvas(albedo, normal)
love.graphics.setShader(gbufferShader)
gbufferShader:send("normal", nmap)
love.graphics.draw(amap, tiles, 0, 0)
love.graphics.setCanvas()
--love.graphics.setShader(pointLightShader)
--pointLightShader:send("normal", normal)
love.graphics.draw(normal, 0, 0)
love.graphics.pop()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment