Improve and add poly/circle glow.

This commit is contained in:
Marcus Ihde 2014-03-09 18:33:34 +01:00
parent 3a3512083c
commit 560463fdbe
2 changed files with 129 additions and 33 deletions

121
light.lua
View File

@ -29,6 +29,7 @@ function love.light.newWorld()
o.normalMap = love.graphics.newCanvas() o.normalMap = love.graphics.newCanvas()
o.glowMap = love.graphics.newCanvas() o.glowMap = love.graphics.newCanvas()
o.glowMap2 = love.graphics.newCanvas() o.glowMap2 = love.graphics.newCanvas()
o.glowBlur = 1.0
o.isGlowBlur = false o.isGlowBlur = false
o.pixelShadow = love.graphics.newCanvas() o.pixelShadow = love.graphics.newCanvas()
o.pixelShadow2 = love.graphics.newCanvas() o.pixelShadow2 = love.graphics.newCanvas()
@ -178,10 +179,10 @@ function love.light.newWorld()
for i = 1, #o.img do for i = 1, #o.img do
if o.img[i].normal then if o.img[i].normal then
love.graphics.setColor(255, 255, 255) love.graphics.setColor(255, 255, 255)
love.graphics.draw(o.img[i].normal, o.img[i].x - o.img[i].ox2, o.img[i].y - o.img[i].oy2) love.graphics.draw(o.img[i].normal, o.img[i].x - o.img[i].ox2 + LOVE_LIGHT_TRANSLATE_X, o.img[i].y - o.img[i].oy2 + LOVE_LIGHT_TRANSLATE_Y)
else else
love.graphics.setColor(0, 0, 0, 0) love.graphics.setColor(0, 0, 0, 0)
love.graphics.rectangle("fill", o.img[i].x - o.img[i].ox2, o.img[i].y - o.img[i].oy2, o.img[i].imgWidth, o.img[i].imgHeight) love.graphics.rectangle("fill", o.img[i].x - o.img[i].ox2 + LOVE_LIGHT_TRANSLATE_X, o.img[i].y - o.img[i].oy2, o.img[i].imgWidth, o.img[i].imgHeight + LOVE_LIGHT_TRANSLATE_Y)
end end
end end
love.graphics.setColor(255, 255, 255) love.graphics.setColor(255, 255, 255)
@ -218,13 +219,31 @@ function love.light.newWorld()
if o.changed then if o.changed then
o.glowMap:clear(0, 0, 0) o.glowMap:clear(0, 0, 0)
love.graphics.setCanvas(o.glowMap) love.graphics.setCanvas(o.glowMap)
for i = 1, #o.img do for i = 1, #o.circle do
if o.img[i].glow then if o.circle[i].glowStrength > 0.0 then
love.graphics.setColor(255, 255, 255) love.graphics.setColor(o.circle[i].glowRed, o.circle[i].glowGreen, o.circle[i].glowBlue)
love.graphics.draw(o.img[i].glow, o.img[i].x - o.img[i].ox2, o.img[i].y - o.img[i].oy2) love.graphics.circle("fill", o.circle[i].x, o.circle[i].y, o.circle[i].radius)
else else
love.graphics.setColor(0, 0, 0) love.graphics.setColor(0, 0, 0)
love.graphics.draw(o.img[i].img, o.img[i].x - o.img[i].ox2, o.img[i].y - o.img[i].oy2) love.graphics.circle("fill", o.circle[i].x, o.circle[i].y, o.circle[i].radius)
end
end
for i = 1, #o.poly do
if o.poly[i].glowStrength > 0.0 then
love.graphics.setColor(o.poly[i].glowRed, o.poly[i].glowGreen, o.poly[i].glowBlue)
love.graphics.polygon("fill", unpack(o.poly[i].data))
else
love.graphics.setColor(0, 0, 0)
love.graphics.polygon("fill", unpack(o.poly[i].data))
end
end
for i = 1, #o.img do
if o.img[i].glow then
love.graphics.setColor(o.img[i].glowRed, o.img[i].glowGreen, o.img[i].glowBlue)
love.graphics.draw(o.img[i].glow, o.img[i].x - o.img[i].ox2 + LOVE_LIGHT_TRANSLATE_X, o.img[i].y - o.img[i].oy2 + LOVE_LIGHT_TRANSLATE_X)
else
love.graphics.setColor(0, 0, 0)
love.graphics.draw(o.img[i].img, o.img[i].x - o.img[i].ox2 + LOVE_LIGHT_TRANSLATE_X, o.img[i].y - o.img[i].oy2 + LOVE_LIGHT_TRANSLATE_X)
end end
end end
o.isGlowBlur = false o.isGlowBlur = false
@ -282,16 +301,17 @@ function love.light.newWorld()
-- draw glow -- draw glow
o.drawGlow = function() o.drawGlow = function()
love.graphics.setColor(255, 255, 255) love.graphics.setColor(255, 255, 255)
if o.isGlowBlur then if o.isGlowBlur or o.glowBlur == 0.0 then
love.graphics.setBlendMode("additive") love.graphics.setBlendMode("additive")
love.graphics.setShader() love.graphics.setShader()
love.graphics.draw(o.glowMap, LOVE_LIGHT_TRANSLATE_X, LOVE_LIGHT_TRANSLATE_Y) love.graphics.draw(o.glowMap, LOVE_LIGHT_TRANSLATE_X, LOVE_LIGHT_TRANSLATE_Y)
love.graphics.setBlendMode("alpha") love.graphics.setBlendMode("alpha")
else else
LOVE_LIGHT_BLURV:send("steps", 1.0) LOVE_LIGHT_BLURV:send("steps", o.glowBlur)
LOVE_LIGHT_BLURH:send("steps", 1.0) LOVE_LIGHT_BLURH:send("steps", o.glowBlur)
LOVE_LIGHT_LAST_BUFFER = love.graphics.getCanvas() LOVE_LIGHT_LAST_BUFFER = love.graphics.getCanvas()
love.graphics.setBlendMode("alpha") love.graphics.setBlendMode("additive")
o.glowMap2:clear()
love.graphics.setCanvas(o.glowMap2) love.graphics.setCanvas(o.glowMap2)
love.graphics.setShader(LOVE_LIGHT_BLURV) love.graphics.setShader(LOVE_LIGHT_BLURV)
love.graphics.draw(o.glowMap, LOVE_LIGHT_TRANSLATE_X, LOVE_LIGHT_TRANSLATE_Y) love.graphics.draw(o.glowMap, LOVE_LIGHT_TRANSLATE_X, LOVE_LIGHT_TRANSLATE_Y)
@ -299,7 +319,7 @@ function love.light.newWorld()
love.graphics.setShader(LOVE_LIGHT_BLURH) love.graphics.setShader(LOVE_LIGHT_BLURH)
love.graphics.draw(o.glowMap2, LOVE_LIGHT_TRANSLATE_X, LOVE_LIGHT_TRANSLATE_Y) love.graphics.draw(o.glowMap2, LOVE_LIGHT_TRANSLATE_X, LOVE_LIGHT_TRANSLATE_Y)
love.graphics.setCanvas(LOVE_LIGHT_LAST_BUFFER) love.graphics.setCanvas(LOVE_LIGHT_LAST_BUFFER)
love.graphics.setBlendMode("additive") --love.graphics.setBlendMode("additive")
love.graphics.setShader() love.graphics.setShader()
love.graphics.draw(o.glowMap, LOVE_LIGHT_TRANSLATE_X, LOVE_LIGHT_TRANSLATE_Y) love.graphics.draw(o.glowMap, LOVE_LIGHT_TRANSLATE_X, LOVE_LIGHT_TRANSLATE_Y)
love.graphics.setBlendMode("alpha") love.graphics.setBlendMode("alpha")
@ -351,6 +371,11 @@ function love.light.newWorld()
o.blur = blur o.blur = blur
o.changed = true o.changed = true
end end
-- set glow blur
o.setGlowBlur = function(blur)
o.glowBlur = blur
o.changed = true
end
-- new rectangle -- new rectangle
o.newRectangle = function(x, y, w, h) o.newRectangle = function(x, y, w, h)
return love.light.newRectangle(o, x, y, w, h) return love.light.newRectangle(o, x, y, w, h)
@ -519,6 +544,10 @@ function love.light.newRectangle(p, x, y, width, height)
o.green = 255 o.green = 255
o.blue = 255 o.blue = 255
o.glassAlpha = 1.0 o.glassAlpha = 1.0
o.glowRed = 255
o.glowGreen = 255
o.glowBlue = 255
o.glowStrength = 0.0
o.type = "rectangle" o.type = "rectangle"
p.changed = true p.changed = true
o.data = { o.data = {
@ -584,18 +613,30 @@ function love.light.newRectangle(p, x, y, width, height)
o.shine = b o.shine = b
p.changed = true p.changed = true
end end
-- set glass color -- set shadow color
o.setColor = function(red, green, blue) o.setColor = function(red, green, blue)
o.red = red o.red = red
o.green = green o.green = green
o.blue = blue o.blue = blue
p.changed = true p.changed = true
end end
-- set glass alpha -- set shadow alpha
o.setAlpha = function(alpha) o.setAlpha = function(alpha)
o.glassAlpha = alpha o.glassAlpha = alpha
p.changed = true p.changed = true
end end
-- set glow color
o.setGlowColor = function(red, green, blue)
o.glowRed = red
o.glowGreen = green
o.glowBlue = blue
p.changed = true
end
-- set glow alpha
o.setGlowStrength = function(strength)
o.glowStrength = strength
p.changed = true
end
-- get x -- get x
o.getX = function() o.getX = function()
return o.x return o.x
@ -637,6 +678,10 @@ function love.light.newCircle(p, x, y, radius)
o.green = 255 o.green = 255
o.blue = 255 o.blue = 255
o.glassAlpha = 1.0 o.glassAlpha = 1.0
o.glowRed = 255
o.glowGreen = 255
o.glowBlue = 255
o.glowStrength = 0.0
o.type = "circle" o.type = "circle"
p.changed = true p.changed = true
-- set position -- set position
@ -678,18 +723,30 @@ function love.light.newCircle(p, x, y, radius)
o.shine = b o.shine = b
p.changed = true p.changed = true
end end
-- set glass color -- set shadow color
o.setColor = function(red, green, blue) o.setColor = function(red, green, blue)
o.red = red o.red = red
o.green = green o.green = green
o.blue = blue o.blue = blue
p.changed = true p.changed = true
end end
-- set glass alpha -- set shadow alpha
o.setAlpha = function(alpha) o.setAlpha = function(alpha)
o.glassAlpha = alpha o.glassAlpha = alpha
p.changed = true p.changed = true
end end
-- set glow color
o.setGlowColor = function(red, green, blue)
o.glowRed = red
o.glowGreen = green
o.glowBlue = blue
p.changed = true
end
-- set glow alpha
o.setGlowStrength = function(strength)
o.glowStrength = strength
p.changed = true
end
-- get x -- get x
o.getX = function() o.getX = function()
return o.x return o.x
@ -720,6 +777,10 @@ function love.light.newPolygon(p, ...)
o.green = 255 o.green = 255
o.blue = 255 o.blue = 255
o.glassAlpha = 1.0 o.glassAlpha = 1.0
o.glowRed = 255
o.glowGreen = 255
o.glowBlue = 255
o.glowStrength = 0.0
o.type = "polygon" o.type = "polygon"
p.changed = true p.changed = true
if ... then if ... then
@ -742,18 +803,30 @@ function love.light.newPolygon(p, ...)
o.shine = b o.shine = b
p.changed = true p.changed = true
end end
-- set glass color -- set shadow color
o.setColor = function(red, green, blue) o.setColor = function(red, green, blue)
o.red = red o.red = red
o.green = green o.green = green
o.blue = blue o.blue = blue
p.changed = true p.changed = true
end end
-- set glass alpha -- set shadow alpha
o.setAlpha = function(alpha) o.setAlpha = function(alpha)
o.glassAlpha = alpha o.glassAlpha = alpha
p.changed = true p.changed = true
end end
-- set glow color
o.setGlowColor = function(red, green, blue)
o.glowRed = red
o.glowGreen = green
o.glowBlue = blue
p.changed = true
end
-- set glow alpha
o.setGlowStrength = function(strength)
o.glowStrength = strength
p.changed = true
end
-- get polygon data -- get polygon data
o.getPoints = function() o.getPoints = function()
return unpack(o.data) return unpack(o.data)
@ -790,6 +863,10 @@ function love.light.newImage(p, img, x, y, width, height, ox, oy)
o.green = 255 o.green = 255
o.blue = 255 o.blue = 255
o.glassAlpha = 1.0 o.glassAlpha = 1.0
o.glowRed = 255
o.glowGreen = 255
o.glowBlue = 255
o.glowStrength = 0.0
o.type = "image" o.type = "image"
p.changed = true p.changed = true
o.data = { o.data = {
@ -1106,14 +1183,14 @@ end
polyStencil = function() polyStencil = function()
for i = 1, #LOVE_LIGHT_CIRCLE do for i = 1, #LOVE_LIGHT_CIRCLE do
--if LOVE_LIGHT_CIRCLE[i].shine and LOVE_LIGHT_CIRCLE[i].glassAlpha == 1.0 then if LOVE_LIGHT_CIRCLE[i].shine and LOVE_LIGHT_CIRCLE[i].glowStrength == 0.0 then
love.graphics.circle("fill", LOVE_LIGHT_CIRCLE[i].x, LOVE_LIGHT_CIRCLE[i].y, LOVE_LIGHT_CIRCLE[i].radius) love.graphics.circle("fill", LOVE_LIGHT_CIRCLE[i].x, LOVE_LIGHT_CIRCLE[i].y, LOVE_LIGHT_CIRCLE[i].radius)
--end end
end end
for i = 1, #LOVE_LIGHT_POLY do for i = 1, #LOVE_LIGHT_POLY do
--if LOVE_LIGHT_POLY[i].shine and LOVE_LIGHT_POLY[i].glassAlpha == 1.0 then if LOVE_LIGHT_POLY[i].shine and LOVE_LIGHT_POLY[i].glowStrength == 0.0 then
love.graphics.polygon("fill", unpack(LOVE_LIGHT_POLY[i].data)) love.graphics.polygon("fill", unpack(LOVE_LIGHT_POLY[i].data))
--end end
end end
for i = 1, #LOVE_LIGHT_IMAGE do for i = 1, #LOVE_LIGHT_IMAGE do
if LOVE_LIGHT_IMAGE[i].shine then if LOVE_LIGHT_IMAGE[i].shine then

View File

@ -67,7 +67,7 @@ function love.load()
lightSmooth = 1.0 lightSmooth = 1.0
lightWorld = love.light.newWorld() lightWorld = love.light.newWorld()
lightWorld.setAmbientColor(15, 15, 31) lightWorld.setAmbientColor(15, 15, 31)
mouseLight = lightWorld.newLight(0, 0, 255, 191, 127, lightRange) mouseLight = lightWorld.newLight(0, 0, 255, 127, 63, lightRange)
mouseLight.setGlowStrength(0.3) mouseLight.setGlowStrength(0.3)
mouseLight.setSmooth(lightSmooth) mouseLight.setSmooth(lightSmooth)
@ -82,6 +82,7 @@ function love.load()
bloomOn = true bloomOn = true
textureOn = true textureOn = true
normalOn = false normalOn = false
glowBlur = 1.0
offsetX = 0.0 offsetX = 0.0
offsetY = 0.0 offsetY = 0.0
@ -207,7 +208,6 @@ function love.draw()
for i = 1, phyCnt do for i = 1, phyCnt do
if phyLight[i].getType() == "image" then if phyLight[i].getType() == "image" then
if not normalOn then if not normalOn then
--love.graphics.setColor(255, 255, 255)
math.randomseed(i) math.randomseed(i)
love.graphics.setColor(math.random(127, 255), math.random(127, 255), math.random(127, 255)) love.graphics.setColor(math.random(127, 255), math.random(127, 255), math.random(127, 255))
love.graphics.draw(phyLight[i].img, phyLight[i].x - phyLight[i].ox2, phyLight[i].y - phyLight[i].oy2) love.graphics.draw(phyLight[i].img, phyLight[i].x - phyLight[i].ox2, phyLight[i].y - phyLight[i].oy2)
@ -267,32 +267,39 @@ function love.draw()
end end
if bloomOn then if bloomOn then
love.graphics.setColor(0, 255, 0) love.graphics.setColor(0, 255, 0)
love.graphics.print("F6: Bloom on", 4 + 152 * 0, 4 + 20 * 1) love.graphics.print("F6: Bloom (on)", 4 + 152 * 0, 4 + 20 * 1)
else else
love.graphics.setColor(255, 0, 0) love.graphics.setColor(255, 0, 0)
love.graphics.print("F6: Bloom off", 4 + 152 * 0, 4 + 20 * 1) love.graphics.print("F6: Bloom (off)", 4 + 152 * 0, 4 + 20 * 1)
end end
if textureOn then if textureOn then
love.graphics.setColor(0, 255, 0) love.graphics.setColor(0, 255, 0)
love.graphics.print("F7: Texture on", 4 + 152 * 1, 4 + 20 * 1) love.graphics.print("F7: Texture (on)", 4 + 152 * 1, 4 + 20 * 1)
else else
love.graphics.setColor(255, 0, 0) love.graphics.setColor(255, 0, 0)
love.graphics.print("F7: Texture off", 4 + 152 * 1, 4 + 20 * 1) love.graphics.print("F7: Texture (off)", 4 + 152 * 1, 4 + 20 * 1)
end end
if normalOn then if normalOn then
love.graphics.setColor(0, 255, 0) love.graphics.setColor(0, 255, 0)
love.graphics.print("F8: Normal on", 4 + 152 * 2, 4 + 20 * 1) love.graphics.print("F8: Normal (on)", 4 + 152 * 2, 4 + 20 * 1)
else else
love.graphics.setColor(255, 0, 0) love.graphics.setColor(255, 0, 0)
love.graphics.print("F8: Normal off", 4 + 152 * 2, 4 + 20 * 1) love.graphics.print("F8: Normal (off)", 4 + 152 * 2, 4 + 20 * 1)
end
if glowBlur >= 1.0 then
love.graphics.setColor(0, 255, 0)
love.graphics.print("F9: Glow Blur (" .. glowBlur .. ")", 4 + 152 * 3, 4 + 20 * 1)
else
love.graphics.setColor(255, 0, 0)
love.graphics.print("F9: Glow Blur (off)", 4 + 152 * 3, 4 + 20 * 1)
end end
love.graphics.setColor(255, 0, 255) love.graphics.setColor(255, 0, 255)
love.graphics.print("F11: Clear obj.", 4 + 152 * 3, 4 + 20 * 1) love.graphics.print("F11: Clear obj.", 4 + 152 * 4, 4 + 20 * 2)
love.graphics.print("F12: Clear lights", 4 + 152 * 4, 4 + 20 * 1) love.graphics.print("F12: Clear lights", 4 + 152 * 4, 4 + 20 * 3)
love.graphics.setColor(0, 127, 255) love.graphics.setColor(0, 127, 255)
love.graphics.print("WASD Keys: Move objects", 4, love.graphics.getHeight() - 20 * 3) love.graphics.print("WASD Keys: Move objects", 4, love.graphics.getHeight() - 20 * 3)
love.graphics.print("Arrow Keys: Move map", 4, love.graphics.getHeight() - 20 * 2) love.graphics.print("Arrow Keys: Move map", 4, love.graphics.getHeight() - 20 * 2)
love.graphics.print("1-5 Keys: Add image", 4, love.graphics.getHeight() - 20 * 1) love.graphics.print("0-9 Keys: Add object", 4, love.graphics.getHeight() - 20 * 1)
love.graphics.setColor(255, 127, 0) love.graphics.setColor(255, 127, 0)
love.graphics.print("M.left: Add cube", love.graphics.getWidth() - 240, love.graphics.getHeight() - 20 * 4) love.graphics.print("M.left: Add cube", love.graphics.getWidth() - 240, love.graphics.getHeight() - 20 * 4)
love.graphics.print("M.middle: Add light", love.graphics.getWidth() - 240, love.graphics.getHeight() - 20 * 3) love.graphics.print("M.middle: Add light", love.graphics.getWidth() - 240, love.graphics.getHeight() - 20 * 3)
@ -378,6 +385,12 @@ function love.keypressed(k, u)
textureOn = not textureOn textureOn = not textureOn
elseif k == "f8" then elseif k == "f8" then
normalOn = not normalOn normalOn = not normalOn
elseif k == "f9" then
glowBlur = glowBlur + 1.0
if glowBlur > 8.0 then
glowBlur = 0.0
end
lightWorld.setGlowBlur(glowBlur)
elseif k == "f11" then elseif k == "f11" then
physicWorld:destroy() physicWorld:destroy()
lightWorld.clearObjects() lightWorld.clearObjects()
@ -460,11 +473,14 @@ function love.keypressed(k, u)
phyCnt = phyCnt + 1 phyCnt = phyCnt + 1
phyLight[phyCnt] = lightWorld.newPolygon() phyLight[phyCnt] = lightWorld.newPolygon()
phyLight[phyCnt].setAlpha(0.5) phyLight[phyCnt].setAlpha(0.5)
phyLight[phyCnt].setGlowStrength(1.0)
phyBody[phyCnt] = love.physics.newBody(physicWorld, mx, my, "dynamic") phyBody[phyCnt] = love.physics.newBody(physicWorld, mx, my, "dynamic")
phyShape[phyCnt] = love.physics.newRectangleShape(0, 0, math.random(32, 64), math.random(32, 64)) phyShape[phyCnt] = love.physics.newRectangleShape(0, 0, math.random(32, 64), math.random(32, 64))
phyFixture[phyCnt] = love.physics.newFixture(phyBody[phyCnt], phyShape[phyCnt]) phyFixture[phyCnt] = love.physics.newFixture(phyBody[phyCnt], phyShape[phyCnt])
phyFixture[phyCnt]:setRestitution(0.5) phyFixture[phyCnt]:setRestitution(0.5)
math.randomseed(phyCnt) math.randomseed(phyCnt)
phyLight[phyCnt].setGlowColor(math.random(0, 255), math.random(0, 255), math.random(0, 255))
math.randomseed(phyCnt)
phyLight[phyCnt].setColor(math.random(0, 255), math.random(0, 255), math.random(0, 255)) phyLight[phyCnt].setColor(math.random(0, 255), math.random(0, 255), math.random(0, 255))
elseif k == "9" then elseif k == "9" then
-- add circle -- add circle
@ -472,6 +488,9 @@ function love.keypressed(k, u)
phyCnt = phyCnt + 1 phyCnt = phyCnt + 1
phyLight[phyCnt] = lightWorld.newCircle(mx, my, cRadius) phyLight[phyCnt] = lightWorld.newCircle(mx, my, cRadius)
phyLight[phyCnt].setAlpha(0.5) phyLight[phyCnt].setAlpha(0.5)
phyLight[phyCnt].setGlowStrength(1.0)
math.randomseed(phyCnt)
phyLight[phyCnt].setGlowColor(math.random(0, 255), math.random(0, 255), math.random(0, 255))
math.randomseed(phyCnt) math.randomseed(phyCnt)
phyLight[phyCnt].setColor(math.random(0, 255), math.random(0, 255), math.random(0, 255)) phyLight[phyCnt].setColor(math.random(0, 255), math.random(0, 255), math.random(0, 255))
phyBody[phyCnt] = love.physics.newBody(physicWorld, mx, my, "dynamic") phyBody[phyCnt] = love.physics.newBody(physicWorld, mx, my, "dynamic")