From 560463fdbe2e47d9de422d6ecc6ae1cfbe6cdaf6 Mon Sep 17 00:00:00 2001 From: Marcus Ihde Date: Sun, 9 Mar 2014 18:33:34 +0100 Subject: [PATCH] Improve and add poly/circle glow. --- light.lua | 121 ++++++++++++++++++++++++++++++++++++++++++++---------- main.lua | 41 +++++++++++++----- 2 files changed, 129 insertions(+), 33 deletions(-) diff --git a/light.lua b/light.lua index 55ab919..232760a 100644 --- a/light.lua +++ b/light.lua @@ -29,6 +29,7 @@ function love.light.newWorld() o.normalMap = love.graphics.newCanvas() o.glowMap = love.graphics.newCanvas() o.glowMap2 = love.graphics.newCanvas() + o.glowBlur = 1.0 o.isGlowBlur = false o.pixelShadow = love.graphics.newCanvas() o.pixelShadow2 = love.graphics.newCanvas() @@ -178,10 +179,10 @@ function love.light.newWorld() for i = 1, #o.img do if o.img[i].normal then 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 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 love.graphics.setColor(255, 255, 255) @@ -218,13 +219,31 @@ function love.light.newWorld() if o.changed then o.glowMap:clear(0, 0, 0) love.graphics.setCanvas(o.glowMap) - for i = 1, #o.img do - if o.img[i].glow then - love.graphics.setColor(255, 255, 255) - love.graphics.draw(o.img[i].glow, o.img[i].x - o.img[i].ox2, o.img[i].y - o.img[i].oy2) + for i = 1, #o.circle do + if o.circle[i].glowStrength > 0.0 then + love.graphics.setColor(o.circle[i].glowRed, o.circle[i].glowGreen, o.circle[i].glowBlue) + love.graphics.circle("fill", o.circle[i].x, o.circle[i].y, o.circle[i].radius) else 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 o.isGlowBlur = false @@ -282,16 +301,17 @@ function love.light.newWorld() -- draw glow o.drawGlow = function() 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.setShader() love.graphics.draw(o.glowMap, LOVE_LIGHT_TRANSLATE_X, LOVE_LIGHT_TRANSLATE_Y) love.graphics.setBlendMode("alpha") else - LOVE_LIGHT_BLURV:send("steps", 1.0) - LOVE_LIGHT_BLURH:send("steps", 1.0) + LOVE_LIGHT_BLURV:send("steps", o.glowBlur) + LOVE_LIGHT_BLURH:send("steps", o.glowBlur) 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.setShader(LOVE_LIGHT_BLURV) 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.draw(o.glowMap2, LOVE_LIGHT_TRANSLATE_X, LOVE_LIGHT_TRANSLATE_Y) love.graphics.setCanvas(LOVE_LIGHT_LAST_BUFFER) - love.graphics.setBlendMode("additive") + --love.graphics.setBlendMode("additive") love.graphics.setShader() love.graphics.draw(o.glowMap, LOVE_LIGHT_TRANSLATE_X, LOVE_LIGHT_TRANSLATE_Y) love.graphics.setBlendMode("alpha") @@ -351,6 +371,11 @@ function love.light.newWorld() o.blur = blur o.changed = true end + -- set glow blur + o.setGlowBlur = function(blur) + o.glowBlur = blur + o.changed = true + end -- new rectangle o.newRectangle = function(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.blue = 255 o.glassAlpha = 1.0 + o.glowRed = 255 + o.glowGreen = 255 + o.glowBlue = 255 + o.glowStrength = 0.0 o.type = "rectangle" p.changed = true o.data = { @@ -584,18 +613,30 @@ function love.light.newRectangle(p, x, y, width, height) o.shine = b p.changed = true end - -- set glass color + -- set shadow color o.setColor = function(red, green, blue) o.red = red o.green = green o.blue = blue p.changed = true end - -- set glass alpha + -- set shadow alpha o.setAlpha = function(alpha) o.glassAlpha = alpha p.changed = true 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 o.getX = function() return o.x @@ -637,6 +678,10 @@ function love.light.newCircle(p, x, y, radius) o.green = 255 o.blue = 255 o.glassAlpha = 1.0 + o.glowRed = 255 + o.glowGreen = 255 + o.glowBlue = 255 + o.glowStrength = 0.0 o.type = "circle" p.changed = true -- set position @@ -678,18 +723,30 @@ function love.light.newCircle(p, x, y, radius) o.shine = b p.changed = true end - -- set glass color + -- set shadow color o.setColor = function(red, green, blue) o.red = red o.green = green o.blue = blue p.changed = true end - -- set glass alpha + -- set shadow alpha o.setAlpha = function(alpha) o.glassAlpha = alpha p.changed = true 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 o.getX = function() return o.x @@ -720,6 +777,10 @@ function love.light.newPolygon(p, ...) o.green = 255 o.blue = 255 o.glassAlpha = 1.0 + o.glowRed = 255 + o.glowGreen = 255 + o.glowBlue = 255 + o.glowStrength = 0.0 o.type = "polygon" p.changed = true if ... then @@ -742,18 +803,30 @@ function love.light.newPolygon(p, ...) o.shine = b p.changed = true end - -- set glass color + -- set shadow color o.setColor = function(red, green, blue) o.red = red o.green = green o.blue = blue p.changed = true end - -- set glass alpha + -- set shadow alpha o.setAlpha = function(alpha) o.glassAlpha = alpha p.changed = true 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 o.getPoints = function() return unpack(o.data) @@ -790,6 +863,10 @@ function love.light.newImage(p, img, x, y, width, height, ox, oy) o.green = 255 o.blue = 255 o.glassAlpha = 1.0 + o.glowRed = 255 + o.glowGreen = 255 + o.glowBlue = 255 + o.glowStrength = 0.0 o.type = "image" p.changed = true o.data = { @@ -1106,14 +1183,14 @@ end polyStencil = function() 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) - --end + end end 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)) - --end + end end for i = 1, #LOVE_LIGHT_IMAGE do if LOVE_LIGHT_IMAGE[i].shine then diff --git a/main.lua b/main.lua index 767c4b7..3a29172 100644 --- a/main.lua +++ b/main.lua @@ -67,7 +67,7 @@ function love.load() lightSmooth = 1.0 lightWorld = love.light.newWorld() 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.setSmooth(lightSmooth) @@ -82,6 +82,7 @@ function love.load() bloomOn = true textureOn = true normalOn = false + glowBlur = 1.0 offsetX = 0.0 offsetY = 0.0 @@ -207,7 +208,6 @@ function love.draw() for i = 1, phyCnt do if phyLight[i].getType() == "image" then if not normalOn then - --love.graphics.setColor(255, 255, 255) math.randomseed(i) 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) @@ -267,32 +267,39 @@ function love.draw() end if bloomOn then 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 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 if textureOn then 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 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 if normalOn then 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 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 love.graphics.setColor(255, 0, 255) - love.graphics.print("F11: Clear obj.", 4 + 152 * 3, 4 + 20 * 1) - love.graphics.print("F12: Clear lights", 4 + 152 * 4, 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 * 3) love.graphics.setColor(0, 127, 255) 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("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.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) @@ -378,6 +385,12 @@ function love.keypressed(k, u) textureOn = not textureOn elseif k == "f8" then 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 physicWorld:destroy() lightWorld.clearObjects() @@ -460,11 +473,14 @@ function love.keypressed(k, u) phyCnt = phyCnt + 1 phyLight[phyCnt] = lightWorld.newPolygon() phyLight[phyCnt].setAlpha(0.5) + phyLight[phyCnt].setGlowStrength(1.0) phyBody[phyCnt] = love.physics.newBody(physicWorld, mx, my, "dynamic") 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]:setRestitution(0.5) 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)) elseif k == "9" then -- add circle @@ -472,6 +488,9 @@ function love.keypressed(k, u) phyCnt = phyCnt + 1 phyLight[phyCnt] = lightWorld.newCircle(mx, my, cRadius) 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) phyLight[phyCnt].setColor(math.random(0, 255), math.random(0, 255), math.random(0, 255)) phyBody[phyCnt] = love.physics.newBody(physicWorld, mx, my, "dynamic")