refactoring,

taking out unused variables and redundant code, also took out debug variables. also moved the shaders in to the lib folder
for better portablility of the library
This commit is contained in:
Tim Anema 2014-09-29 10:03:15 -04:00
parent 9579dc0b15
commit 45be0c56fa
19 changed files with 93 additions and 121 deletions

View File

@ -4,6 +4,9 @@ local height_map_conv = require(_PACKAGE..'/height_map_conv')
local body = class() local body = class()
body.glowShader = love.graphics.newShader(_PACKAGE.."/shaders/glow.glsl")
body.materialShader = love.graphics.newShader(_PACKAGE.."/shaders/material.glsl")
function body:init(world, id, type, ...) function body:init(world, id, type, ...)
local args = {...} local args = {...}
self.id = id self.id = id
@ -13,11 +16,6 @@ function body:init(world, id, type, ...)
self.glow = nil self.glow = nil
self.world = world self.world = world
self.reflection = false
self.reflective = false
self.refraction = false
self.refractive = false
self.shine = true self.shine = true
self.red = 0 self.red = 0
self.green = 0 self.green = 0
@ -600,9 +598,9 @@ function body:drawGlow()
love.graphics.polygon("fill", unpack(self.data)) love.graphics.polygon("fill", unpack(self.data))
elseif self.type == "image" and self.img then elseif self.type == "image" and self.img then
if self.glowStrength > 0.0 and self.glow then if self.glowStrength > 0.0 and self.glow then
love.graphics.setShader(self.world.glowShader) love.graphics.setShader(self.glowShader)
self.world.glowShader:send("glowImage", self.glow) self.glowShader:send("glowImage", self.glow)
self.world.glowShader:send("glowTime", love.timer.getTime() * 0.5) self.glowShader:send("glowTime", love.timer.getTime() * 0.5)
love.graphics.setColor(255, 255, 255) love.graphics.setColor(255, 255, 255)
else else
love.graphics.setShader() love.graphics.setShader()
@ -657,9 +655,11 @@ end
function body:drawMaterial() function body:drawMaterial()
if self.material and self.normal then if self.material and self.normal then
love.graphics.setShader(self.materialShader)
love.graphics.setColor(255, 255, 255) love.graphics.setColor(255, 255, 255)
self.world.materialShader:send("material", self.material) self.materialShader:send("material", self.material)
love.graphics.draw(self.normal, self.x - self.nx + self.world.translate_x, self.y - self.ny + self.world.translate_y) love.graphics.draw(self.normal, self.x - self.nx + self.world.translate_x, self.y - self.ny + self.world.translate_y)
love.graphics.setShader()
end end
end end

View File

@ -5,6 +5,10 @@ local vector = require(_PACKAGE..'/vector')
local light = class() local light = class()
light.shader = love.graphics.newShader(_PACKAGE.."/shaders/poly_shadow.glsl")
light.normalShader = love.graphics.newShader(_PACKAGE.."/shaders/normal.glsl")
light.normalInvertShader = love.graphics.newShader(_PACKAGE.."/shaders/normal_invert.glsl")
function light:init(world, x, y, r, g, b, range) function light:init(world, x, y, r, g, b, range)
self.world = world self.world = world
self.direction = 0 self.direction = 0
@ -115,18 +119,18 @@ function light:setGlowStrength(strength)
end end
function light:updateShadow() function light:updateShadow()
love.graphics.setShader(self.shader)
if self.x + self.range > self.world.translate_x and self.x - self.range < love.graphics.getWidth() + self.world.translate_x if self.x + self.range > self.world.translate_x and self.x - self.range < love.graphics.getWidth() + self.world.translate_x
and self.y + self.range > self.world.translate_y and self.y - self.range < love.graphics.getHeight() + self.world.translate_y and self.y + self.range > self.world.translate_y and self.y - self.range < love.graphics.getHeight() + self.world.translate_y
then then
local lightposrange = {self.x, love.graphics.getHeight() - self.y, self.range} local lightposrange = {self.x, love.graphics.getHeight() - self.y, self.range}
self.world.direction = self.world.direction + 0.002 self.shader:send("lightPosition", {self.x - self.world.translate_x, love.graphics.getHeight() - (self.y - self.world.translate_y), self.z})
self.world.shader:send("lightPosition", {self.x - self.world.translate_x, love.graphics.getHeight() - (self.y - self.world.translate_y), self.z}) self.shader:send("lightRange", self.range)
self.world.shader:send("lightRange", self.range) self.shader:send("lightColor", {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
self.world.shader:send("lightColor", {self.red / 255.0, self.green / 255.0, self.blue / 255.0}) self.shader:send("lightSmooth", self.smooth)
self.world.shader:send("lightSmooth", self.smooth) self.shader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength})
self.world.shader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength}) self.shader:send("lightAngle", math.pi - self.angle / 2.0)
self.world.shader:send("lightAngle", math.pi - self.angle / 2.0) self.shader:send("lightDirection", self.direction)
self.world.shader:send("lightDirection", self.direction)
love.graphics.setCanvas(self.shadow) love.graphics.setCanvas(self.shadow)
love.graphics.clear() love.graphics.clear()
@ -158,7 +162,7 @@ function light:updateShadow()
self.world.body[k]:drawShadow(self) self.world.body[k]:drawShadow(self)
end end
love.graphics.setShader(self.world.shader) love.graphics.setShader(self.shader)
-- draw shine -- draw shine
love.graphics.setCanvas(self.shine) love.graphics.setCanvas(self.shine)
@ -172,6 +176,7 @@ function light:updateShadow()
else else
self.visible = false self.visible = false
end end
love.graphics.setShader()
end end
function light:drawShadow() function light:drawShadow()
@ -301,23 +306,23 @@ end
function light:drawPixelShadow() function light:drawPixelShadow()
if self.visible then if self.visible then
if self.normalInvert then if self.normalInvert then
self.world.normalInvertShader:send('screenResolution', {love.graphics.getWidth(), love.graphics.getHeight()}) self.normalInvertShader:send('screenResolution', {love.graphics.getWidth(), love.graphics.getHeight()})
self.world.normalInvertShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0}) self.normalInvertShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
self.world.normalInvertShader:send('lightPosition',{self.x, love.graphics.getHeight() - self.y, self.z / 255.0}) self.normalInvertShader:send('lightPosition',{self.x, love.graphics.getHeight() - self.y, self.z / 255.0})
self.world.normalInvertShader:send('lightRange',{self.range}) self.normalInvertShader:send('lightRange',{self.range})
self.world.normalInvertShader:send("lightSmooth", self.smooth) self.normalInvertShader:send("lightSmooth", self.smooth)
self.world.normalInvertShader:send("lightAngle", math.pi - self.angle / 2.0) self.normalInvertShader:send("lightAngle", math.pi - self.angle / 2.0)
self.world.normalInvertShader:send("lightDirection", self.direction) self.normalInvertShader:send("lightDirection", self.direction)
love.graphics.setShader(self.world.normalInvertShader) love.graphics.setShader(self.normalInvertShader)
else else
self.world.normalShader:send('screenResolution', {love.graphics.getWidth(), love.graphics.getHeight()}) self.normalShader:send('screenResolution', {love.graphics.getWidth(), love.graphics.getHeight()})
self.world.normalShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0}) self.normalShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
self.world.normalShader:send('lightPosition',{self.x, love.graphics.getHeight() - self.y, self.z / 255.0}) self.normalShader:send('lightPosition',{self.x, love.graphics.getHeight() - self.y, self.z / 255.0})
self.world.normalShader:send('lightRange',{self.range}) self.normalShader:send('lightRange',{self.range})
self.world.normalShader:send("lightSmooth", self.smooth) self.normalShader:send("lightSmooth", self.smooth)
self.world.normalShader:send("lightAngle", math.pi - self.angle / 2.0) self.normalShader:send("lightAngle", math.pi - self.angle / 2.0)
self.world.normalShader:send("lightDirection", self.direction) self.normalShader:send("lightDirection", self.direction)
love.graphics.setShader(self.world.normalShader) love.graphics.setShader(self.normalShader)
end end
love.graphics.draw(self.world.normalMap, self.world.translate_x, self.world.translate_y) love.graphics.draw(self.world.normalMap, self.world.translate_x, self.world.translate_y)
end end

View File

@ -29,82 +29,49 @@ local height_map_conv = require(_PACKAGE..'/height_map_conv')
local light_world = class() local light_world = class()
light_world.blurv = love.graphics.newShader(_PACKAGE.."/shaders/blurv.glsl")
light_world.blurh = love.graphics.newShader(_PACKAGE.."/shaders/blurh.glsl")
light_world.refractionShader = love.graphics.newShader(_PACKAGE.."/shaders/refraction.glsl")
light_world.reflectionShader = love.graphics.newShader(_PACKAGE.."/shaders/reflection.glsl")
light_world.blurv:send("screen", {love.window.getWidth(), love.window.getHeight()})
light_world.blurh:send("screen", {love.window.getWidth(), love.window.getHeight()})
light_world.refractionShader:send("screen", {love.window.getWidth(), love.window.getHeight()})
light_world.reflectionShader:send("screen", {love.window.getWidth(), love.window.getHeight()})
function light_world:init() function light_world:init()
self.translate_x = 0 self.translate_x = 0
self.translate_y = 0 self.translate_y = 0
self.direction = 0
self.last_buffer = nil self.last_buffer = nil
self.lights = {} self.lights = {}
self.ambient = {0, 0, 0}
self.body = {} self.body = {}
self.refraction = {}
self.shadow = love.graphics.newCanvas() self.ambient = {0, 0, 0}
self.shadow2 = love.graphics.newCanvas()
self.shine = love.graphics.newCanvas()
self.shine2 = love.graphics.newCanvas()
self.normalMap = love.graphics.newCanvas()
self.glowMap = love.graphics.newCanvas()
self.glowMap2 = love.graphics.newCanvas()
self.refractionMap = love.graphics.newCanvas()
self.refractionMap2 = love.graphics.newCanvas()
self.reflectionMap = love.graphics.newCanvas()
self.reflectionMap2 = love.graphics.newCanvas()
self.normalInvert = false self.normalInvert = false
self.glowBlur = 1.0 self.glowBlur = 1.0
self.glowTimer = 0.0 self.glowTimer = 0.0
self.glowDown = false self.glowDown = false
self.refractionStrength = 8.0 self.refractionStrength = 8.0
self.pixelShadow = love.graphics.newCanvas()
self.pixelShadow2 = love.graphics.newCanvas()
self.blurv = love.graphics.newShader("shader/blurv.glsl")
self.blurh = love.graphics.newShader("shader/blurh.glsl")
self.blurv:send("screen", {love.window.getWidth(), love.window.getHeight()})
self.blurh:send("screen", {love.window.getWidth(), love.window.getHeight()})
self.shader = love.graphics.newShader("shader/poly_shadow.glsl")
self.glowShader = love.graphics.newShader("shader/glow.glsl")
self.normalShader = love.graphics.newShader("shader/normal.glsl")
self.normalInvertShader = love.graphics.newShader("shader/normal_invert.glsl")
self.materialShader = love.graphics.newShader("shader/material.glsl")
self.refractionShader = love.graphics.newShader("shader/refraction.glsl")
self.refractionShader:send("screen", {love.window.getWidth(), love.window.getHeight()})
self.reflectionShader = love.graphics.newShader("shader/reflection.glsl")
self.reflectionShader:send("screen", {love.window.getWidth(), love.window.getHeight()})
self.reflectionStrength = 16.0 self.reflectionStrength = 16.0
self.reflectionVisibility = 1.0 self.reflectionVisibility = 1.0
self.blur = 2.0 self.blur = 2.0
self.optionShadows = true
self.optionPixelShadows = true self:refreshScreenSize()
self.optionGlow = true
self.optionRefraction = true
self.optionReflection = true
self.isShadows = false
self.isLight = false
self.isPixelShadows = false
self.isGlow = false
self.isRefraction = false
self.isReflection = false
end end
function light_world:updateShadows() function light_world:updateShadows()
if not self.optionShadows or not (self.isShadows or self.isLight) then if not self.isShadows and not self.isLight then
return return
end end
self.last_buffer = love.graphics.getCanvas() self.last_buffer = love.graphics.getCanvas()
love.graphics.setShader(self.shader)
for i = 1, #self.lights do for i = 1, #self.lights do
self.lights[i]:updateShadow() self.lights[i]:updateShadow()
end end
-- update shadow -- update shadow
love.graphics.setShader()
love.graphics.setCanvas(self.shadow) love.graphics.setCanvas(self.shadow)
love.graphics.setStencil() love.graphics.setStencil()
love.graphics.setColor(unpack(self.ambient)) love.graphics.setColor(unpack(self.ambient))
@ -132,7 +99,7 @@ function light_world:updateShadows()
end end
function light_world:updatePixelShadows() function light_world:updatePixelShadows()
if not self.optionPixelShadows or not self.isPixelShadows then if not self.isPixelShadows then
return return
end end
@ -173,7 +140,7 @@ function light_world:updatePixelShadows()
end end
function light_world:updateGlow() function light_world:updateGlow()
if not self.optionGlow or not self.isGlow then if not self.isGlow then
return return
end end
@ -201,7 +168,7 @@ function light_world:updateGlow()
end end
function light_world:updateRefraction() function light_world:updateRefraction()
if not self.optionRefraction or not self.isRefraction then if not self.isRefraction then
return return
end end
@ -217,7 +184,7 @@ function light_world:updateRefraction()
end end
function light_world:updateRelfection() function light_world:updateRelfection()
if not self.optionReflection or not self.isReflection then if not self.isReflection then
return return
end end
@ -234,6 +201,8 @@ end
function light_world:refreshScreenSize() function light_world:refreshScreenSize()
self.shadow = love.graphics.newCanvas() self.shadow = love.graphics.newCanvas()
self.shadow2 = love.graphics.newCanvas() self.shadow2 = love.graphics.newCanvas()
self.pixelShadow = love.graphics.newCanvas()
self.pixelShadow2 = love.graphics.newCanvas()
self.shine = love.graphics.newCanvas() self.shine = love.graphics.newCanvas()
self.shine2 = love.graphics.newCanvas() self.shine2 = love.graphics.newCanvas()
self.normalMap = love.graphics.newCanvas() self.normalMap = love.graphics.newCanvas()
@ -243,13 +212,11 @@ function light_world:refreshScreenSize()
self.refractionMap2 = love.graphics.newCanvas() self.refractionMap2 = love.graphics.newCanvas()
self.reflectionMap = love.graphics.newCanvas() self.reflectionMap = love.graphics.newCanvas()
self.reflectionMap2 = love.graphics.newCanvas() self.reflectionMap2 = love.graphics.newCanvas()
self.pixelShadow = love.graphics.newCanvas()
self.pixelShadow2 = love.graphics.newCanvas()
end end
-- draw shadow -- draw shadow
function light_world:drawShadow() function light_world:drawShadow()
if not self.optionShadows or not (self.isShadows or self.isLight) then if not self.isShadows and not self.isLight then
return return
end end
@ -281,7 +248,7 @@ end
-- draw shine -- draw shine
function light_world:drawShine() function light_world:drawShine()
if not self.optionShadows or not self.isShadows then if not self.isShadows then
return return
end end
love.graphics.setColor(255, 255, 255) love.graphics.setColor(255, 255, 255)
@ -311,7 +278,7 @@ end
-- draw pixel shadow -- draw pixel shadow
function light_world:drawPixelShadow() function light_world:drawPixelShadow()
if not self.optionPixelShadows or not self.isPixelShadows then if not self.isPixelShadows then
return return
end end
self:updatePixelShadows() self:updatePixelShadows()
@ -324,16 +291,14 @@ end
-- draw material -- draw material
function light_world:drawMaterial() function light_world:drawMaterial()
love.graphics.setShader(self.materialShader)
for i = 1, #self.body do for i = 1, #self.body do
self.body[i]:drawMaterial() self.body[i]:drawMaterial()
end end
love.graphics.setShader()
end end
-- draw glow -- draw glow
function light_world:drawGlow() function light_world:drawGlow()
if not self.optionGlow or not self.isGlow then if not self.isGlow then
return return
end end
@ -364,7 +329,7 @@ function light_world:drawGlow()
end end
-- draw refraction -- draw refraction
function light_world:drawRefraction() function light_world:drawRefraction()
if not self.optionRefraction or not self.isRefraction then if not self.isRefraction then
return return
end end
@ -386,7 +351,7 @@ end
-- draw reflection -- draw reflection
function light_world:drawReflection() function light_world:drawReflection()
if not self.optionReflection or not self.isReflection then if not self.isReflection then
return return
end end

View File

@ -21,19 +21,21 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
]] ]]
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
local class = require(_PACKAGE..'/class')
LOVE_POSTSHADER_BUFFER_RENDER = love.graphics.newCanvas() LOVE_POSTSHADER_BUFFER_RENDER = love.graphics.newCanvas()
LOVE_POSTSHADER_BUFFER_BACK = love.graphics.newCanvas() LOVE_POSTSHADER_BUFFER_BACK = love.graphics.newCanvas()
LOVE_POSTSHADER_LAST_BUFFER = nil LOVE_POSTSHADER_LAST_BUFFER = nil
LOVE_POSTSHADER_BLURV = love.graphics.newShader("shader/blurv.glsl") LOVE_POSTSHADER_BLURV = love.graphics.newShader(_PACKAGE.."/shaders/blurv.glsl")
LOVE_POSTSHADER_BLURH = love.graphics.newShader("shader/blurh.glsl") LOVE_POSTSHADER_BLURH = love.graphics.newShader(_PACKAGE.."/shaders/blurh.glsl")
LOVE_POSTSHADER_CONTRAST = love.graphics.newShader("shader/contrast.glsl") LOVE_POSTSHADER_CONTRAST = love.graphics.newShader(_PACKAGE.."/shaders/contrast.glsl")
LOVE_POSTSHADER_CHROMATIC_ABERRATION = love.graphics.newShader("shader/chromatic_aberration.glsl") LOVE_POSTSHADER_CHROMATIC_ABERRATION = love.graphics.newShader(_PACKAGE.."/shaders/chromatic_aberration.glsl")
LOVE_POSTSHADER_FOUR_COLOR = love.graphics.newShader("shader/four_colors.glsl") LOVE_POSTSHADER_FOUR_COLOR = love.graphics.newShader(_PACKAGE.."/shaders/four_colors.glsl")
LOVE_POSTSHADER_MONOCHROM = love.graphics.newShader("shader/monochrom.glsl") LOVE_POSTSHADER_MONOCHROM = love.graphics.newShader(_PACKAGE.."/shaders/monochrom.glsl")
LOVE_POSTSHADER_SCANLINES = love.graphics.newShader("shader/scanlines.glsl") LOVE_POSTSHADER_SCANLINES = love.graphics.newShader(_PACKAGE.."/shaders/scanlines.glsl")
LOVE_POSTSHADER_TILT_SHIFT = love.graphics.newShader("shader/tilt_shift.glsl") LOVE_POSTSHADER_TILT_SHIFT = love.graphics.newShader(_PACKAGE.."/shaders/tilt_shift.glsl")
love.postshader = {} love.postshader = {}