From 45be0c56fa95f42ecc32ce1ba136fc3c8d3fc0ff Mon Sep 17 00:00:00 2001 From: Tim Anema Date: Mon, 29 Sep 2014 10:03:15 -0400 Subject: [PATCH] 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 --- lib/body.lua | 18 +-- lib/light.lua | 55 ++++---- lib/light_world.lua | 123 +++++++----------- lib/postshader.lua | 18 +-- {shader => lib/shaders}/blurh.glsl | 0 {shader => lib/shaders}/blurv.glsl | 0 .../shaders}/chromatic_aberration.glsl | 0 {shader => lib/shaders}/contrast.glsl | 0 {shader => lib/shaders}/four_colors.glsl | 0 {shader => lib/shaders}/glow.glsl | 0 {shader => lib/shaders}/material.glsl | 0 {shader => lib/shaders}/monochrom.glsl | 0 {shader => lib/shaders}/normal.glsl | 0 {shader => lib/shaders}/normal_invert.glsl | 0 {shader => lib/shaders}/poly_shadow.glsl | 0 {shader => lib/shaders}/reflection.glsl | 0 {shader => lib/shaders}/refraction.glsl | 0 {shader => lib/shaders}/scanlines.glsl | 0 {shader => lib/shaders}/tilt_shift.glsl | 0 19 files changed, 93 insertions(+), 121 deletions(-) rename {shader => lib/shaders}/blurh.glsl (100%) rename {shader => lib/shaders}/blurv.glsl (100%) rename {shader => lib/shaders}/chromatic_aberration.glsl (100%) rename {shader => lib/shaders}/contrast.glsl (100%) rename {shader => lib/shaders}/four_colors.glsl (100%) rename {shader => lib/shaders}/glow.glsl (100%) rename {shader => lib/shaders}/material.glsl (100%) rename {shader => lib/shaders}/monochrom.glsl (100%) rename {shader => lib/shaders}/normal.glsl (100%) rename {shader => lib/shaders}/normal_invert.glsl (100%) rename {shader => lib/shaders}/poly_shadow.glsl (100%) rename {shader => lib/shaders}/reflection.glsl (100%) rename {shader => lib/shaders}/refraction.glsl (100%) rename {shader => lib/shaders}/scanlines.glsl (100%) rename {shader => lib/shaders}/tilt_shift.glsl (100%) diff --git a/lib/body.lua b/lib/body.lua index e569c91..97cd4b9 100644 --- a/lib/body.lua +++ b/lib/body.lua @@ -4,6 +4,9 @@ local height_map_conv = require(_PACKAGE..'/height_map_conv') 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, ...) local args = {...} self.id = id @@ -13,11 +16,6 @@ function body:init(world, id, type, ...) self.glow = nil self.world = world - self.reflection = false - self.reflective = false - self.refraction = false - self.refractive = false - self.shine = true self.red = 0 self.green = 0 @@ -600,9 +598,9 @@ function body:drawGlow() love.graphics.polygon("fill", unpack(self.data)) elseif self.type == "image" and self.img then if self.glowStrength > 0.0 and self.glow then - love.graphics.setShader(self.world.glowShader) - self.world.glowShader:send("glowImage", self.glow) - self.world.glowShader:send("glowTime", love.timer.getTime() * 0.5) + love.graphics.setShader(self.glowShader) + self.glowShader:send("glowImage", self.glow) + self.glowShader:send("glowTime", love.timer.getTime() * 0.5) love.graphics.setColor(255, 255, 255) else love.graphics.setShader() @@ -657,9 +655,11 @@ end function body:drawMaterial() if self.material and self.normal then + love.graphics.setShader(self.materialShader) 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.setShader() end end diff --git a/lib/light.lua b/lib/light.lua index 2f6bda5..485022f 100644 --- a/lib/light.lua +++ b/lib/light.lua @@ -5,6 +5,10 @@ local vector = require(_PACKAGE..'/vector') 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) self.world = world self.direction = 0 @@ -115,18 +119,18 @@ function light:setGlowStrength(strength) end 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 and self.y + self.range > self.world.translate_y and self.y - self.range < love.graphics.getHeight() + self.world.translate_y then local lightposrange = {self.x, love.graphics.getHeight() - self.y, self.range} - self.world.direction = self.world.direction + 0.002 - self.world.shader:send("lightPosition", {self.x - self.world.translate_x, love.graphics.getHeight() - (self.y - self.world.translate_y), self.z}) - self.world.shader:send("lightRange", self.range) - self.world.shader:send("lightColor", {self.red / 255.0, self.green / 255.0, self.blue / 255.0}) - self.world.shader:send("lightSmooth", self.smooth) - self.world.shader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength}) - self.world.shader:send("lightAngle", math.pi - self.angle / 2.0) - self.world.shader:send("lightDirection", self.direction) + self.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.shader:send("lightColor", {self.red / 255.0, self.green / 255.0, self.blue / 255.0}) + self.shader:send("lightSmooth", self.smooth) + self.shader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength}) + self.shader:send("lightAngle", math.pi - self.angle / 2.0) + self.shader:send("lightDirection", self.direction) love.graphics.setCanvas(self.shadow) love.graphics.clear() @@ -158,7 +162,7 @@ function light:updateShadow() self.world.body[k]:drawShadow(self) end - love.graphics.setShader(self.world.shader) + love.graphics.setShader(self.shader) -- draw shine love.graphics.setCanvas(self.shine) @@ -172,6 +176,7 @@ function light:updateShadow() else self.visible = false end + love.graphics.setShader() end function light:drawShadow() @@ -301,23 +306,23 @@ end function light:drawPixelShadow() if self.visible then if self.normalInvert then - self.world.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.world.normalInvertShader:send('lightPosition',{self.x, love.graphics.getHeight() - self.y, self.z / 255.0}) - self.world.normalInvertShader:send('lightRange',{self.range}) - self.world.normalInvertShader:send("lightSmooth", self.smooth) - self.world.normalInvertShader:send("lightAngle", math.pi - self.angle / 2.0) - self.world.normalInvertShader:send("lightDirection", self.direction) - love.graphics.setShader(self.world.normalInvertShader) + self.normalInvertShader:send('screenResolution', {love.graphics.getWidth(), love.graphics.getHeight()}) + self.normalInvertShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0}) + self.normalInvertShader:send('lightPosition',{self.x, love.graphics.getHeight() - self.y, self.z / 255.0}) + self.normalInvertShader:send('lightRange',{self.range}) + self.normalInvertShader:send("lightSmooth", self.smooth) + self.normalInvertShader:send("lightAngle", math.pi - self.angle / 2.0) + self.normalInvertShader:send("lightDirection", self.direction) + love.graphics.setShader(self.normalInvertShader) else - self.world.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.world.normalShader:send('lightPosition',{self.x, love.graphics.getHeight() - self.y, self.z / 255.0}) - self.world.normalShader:send('lightRange',{self.range}) - self.world.normalShader:send("lightSmooth", self.smooth) - self.world.normalShader:send("lightAngle", math.pi - self.angle / 2.0) - self.world.normalShader:send("lightDirection", self.direction) - love.graphics.setShader(self.world.normalShader) + self.normalShader:send('screenResolution', {love.graphics.getWidth(), love.graphics.getHeight()}) + self.normalShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0}) + self.normalShader:send('lightPosition',{self.x, love.graphics.getHeight() - self.y, self.z / 255.0}) + self.normalShader:send('lightRange',{self.range}) + self.normalShader:send("lightSmooth", self.smooth) + self.normalShader:send("lightAngle", math.pi - self.angle / 2.0) + self.normalShader:send("lightDirection", self.direction) + love.graphics.setShader(self.normalShader) end love.graphics.draw(self.world.normalMap, self.world.translate_x, self.world.translate_y) end diff --git a/lib/light_world.lua b/lib/light_world.lua index 630bd62..c9e9b66 100644 --- a/lib/light_world.lua +++ b/lib/light_world.lua @@ -29,82 +29,49 @@ local height_map_conv = require(_PACKAGE..'/height_map_conv') 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() self.translate_x = 0 self.translate_y = 0 - self.direction = 0 - self.last_buffer = nil self.lights = {} - self.ambient = {0, 0, 0} self.body = {} - self.refraction = {} - self.shadow = love.graphics.newCanvas() - 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.glowBlur = 1.0 - self.glowTimer = 0.0 - self.glowDown = false - 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.ambient = {0, 0, 0} + self.normalInvert = false + self.glowBlur = 1.0 + self.glowTimer = 0.0 + self.glowDown = false + self.refractionStrength = 8.0 + self.reflectionStrength = 16.0 self.reflectionVisibility = 1.0 - self.blur = 2.0 - self.optionShadows = true - self.optionPixelShadows = true - 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 + self.blur = 2.0 + + self:refreshScreenSize() end 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 end self.last_buffer = love.graphics.getCanvas() - love.graphics.setShader(self.shader) for i = 1, #self.lights do self.lights[i]:updateShadow() end -- update shadow - love.graphics.setShader() love.graphics.setCanvas(self.shadow) love.graphics.setStencil() love.graphics.setColor(unpack(self.ambient)) @@ -132,7 +99,7 @@ function light_world:updateShadows() end function light_world:updatePixelShadows() - if not self.optionPixelShadows or not self.isPixelShadows then + if not self.isPixelShadows then return end @@ -173,7 +140,7 @@ function light_world:updatePixelShadows() end function light_world:updateGlow() - if not self.optionGlow or not self.isGlow then + if not self.isGlow then return end @@ -201,7 +168,7 @@ function light_world:updateGlow() end function light_world:updateRefraction() - if not self.optionRefraction or not self.isRefraction then + if not self.isRefraction then return end @@ -217,7 +184,7 @@ function light_world:updateRefraction() end function light_world:updateRelfection() - if not self.optionReflection or not self.isReflection then + if not self.isReflection then return end @@ -232,24 +199,24 @@ function light_world:updateRelfection() end function light_world:refreshScreenSize() - self.shadow = love.graphics.newCanvas() - 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.pixelShadow = love.graphics.newCanvas() - self.pixelShadow2 = love.graphics.newCanvas() + self.shadow = love.graphics.newCanvas() + self.shadow2 = love.graphics.newCanvas() + self.pixelShadow = love.graphics.newCanvas() + self.pixelShadow2 = 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() end -- draw shadow 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 end @@ -281,7 +248,7 @@ end -- draw shine function light_world:drawShine() - if not self.optionShadows or not self.isShadows then + if not self.isShadows then return end love.graphics.setColor(255, 255, 255) @@ -311,7 +278,7 @@ end -- draw pixel shadow function light_world:drawPixelShadow() - if not self.optionPixelShadows or not self.isPixelShadows then + if not self.isPixelShadows then return end self:updatePixelShadows() @@ -324,16 +291,14 @@ end -- draw material function light_world:drawMaterial() - love.graphics.setShader(self.materialShader) for i = 1, #self.body do self.body[i]:drawMaterial() end - love.graphics.setShader() end -- draw glow function light_world:drawGlow() - if not self.optionGlow or not self.isGlow then + if not self.isGlow then return end @@ -364,7 +329,7 @@ function light_world:drawGlow() end -- draw refraction function light_world:drawRefraction() - if not self.optionRefraction or not self.isRefraction then + if not self.isRefraction then return end @@ -386,7 +351,7 @@ end -- draw reflection function light_world:drawReflection() - if not self.optionReflection or not self.isReflection then + if not self.isReflection then return end diff --git a/lib/postshader.lua b/lib/postshader.lua index 5e59a18..dd8b033 100644 --- a/lib/postshader.lua +++ b/lib/postshader.lua @@ -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 SOFTWARE. ]] +local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or "" +local class = require(_PACKAGE..'/class') LOVE_POSTSHADER_BUFFER_RENDER = love.graphics.newCanvas() LOVE_POSTSHADER_BUFFER_BACK = love.graphics.newCanvas() LOVE_POSTSHADER_LAST_BUFFER = nil -LOVE_POSTSHADER_BLURV = love.graphics.newShader("shader/blurv.glsl") -LOVE_POSTSHADER_BLURH = love.graphics.newShader("shader/blurh.glsl") -LOVE_POSTSHADER_CONTRAST = love.graphics.newShader("shader/contrast.glsl") -LOVE_POSTSHADER_CHROMATIC_ABERRATION = love.graphics.newShader("shader/chromatic_aberration.glsl") -LOVE_POSTSHADER_FOUR_COLOR = love.graphics.newShader("shader/four_colors.glsl") -LOVE_POSTSHADER_MONOCHROM = love.graphics.newShader("shader/monochrom.glsl") -LOVE_POSTSHADER_SCANLINES = love.graphics.newShader("shader/scanlines.glsl") -LOVE_POSTSHADER_TILT_SHIFT = love.graphics.newShader("shader/tilt_shift.glsl") +LOVE_POSTSHADER_BLURV = love.graphics.newShader(_PACKAGE.."/shaders/blurv.glsl") +LOVE_POSTSHADER_BLURH = love.graphics.newShader(_PACKAGE.."/shaders/blurh.glsl") +LOVE_POSTSHADER_CONTRAST = love.graphics.newShader(_PACKAGE.."/shaders/contrast.glsl") +LOVE_POSTSHADER_CHROMATIC_ABERRATION = love.graphics.newShader(_PACKAGE.."/shaders/chromatic_aberration.glsl") +LOVE_POSTSHADER_FOUR_COLOR = love.graphics.newShader(_PACKAGE.."/shaders/four_colors.glsl") +LOVE_POSTSHADER_MONOCHROM = love.graphics.newShader(_PACKAGE.."/shaders/monochrom.glsl") +LOVE_POSTSHADER_SCANLINES = love.graphics.newShader(_PACKAGE.."/shaders/scanlines.glsl") +LOVE_POSTSHADER_TILT_SHIFT = love.graphics.newShader(_PACKAGE.."/shaders/tilt_shift.glsl") love.postshader = {} diff --git a/shader/blurh.glsl b/lib/shaders/blurh.glsl similarity index 100% rename from shader/blurh.glsl rename to lib/shaders/blurh.glsl diff --git a/shader/blurv.glsl b/lib/shaders/blurv.glsl similarity index 100% rename from shader/blurv.glsl rename to lib/shaders/blurv.glsl diff --git a/shader/chromatic_aberration.glsl b/lib/shaders/chromatic_aberration.glsl similarity index 100% rename from shader/chromatic_aberration.glsl rename to lib/shaders/chromatic_aberration.glsl diff --git a/shader/contrast.glsl b/lib/shaders/contrast.glsl similarity index 100% rename from shader/contrast.glsl rename to lib/shaders/contrast.glsl diff --git a/shader/four_colors.glsl b/lib/shaders/four_colors.glsl similarity index 100% rename from shader/four_colors.glsl rename to lib/shaders/four_colors.glsl diff --git a/shader/glow.glsl b/lib/shaders/glow.glsl similarity index 100% rename from shader/glow.glsl rename to lib/shaders/glow.glsl diff --git a/shader/material.glsl b/lib/shaders/material.glsl similarity index 100% rename from shader/material.glsl rename to lib/shaders/material.glsl diff --git a/shader/monochrom.glsl b/lib/shaders/monochrom.glsl similarity index 100% rename from shader/monochrom.glsl rename to lib/shaders/monochrom.glsl diff --git a/shader/normal.glsl b/lib/shaders/normal.glsl similarity index 100% rename from shader/normal.glsl rename to lib/shaders/normal.glsl diff --git a/shader/normal_invert.glsl b/lib/shaders/normal_invert.glsl similarity index 100% rename from shader/normal_invert.glsl rename to lib/shaders/normal_invert.glsl diff --git a/shader/poly_shadow.glsl b/lib/shaders/poly_shadow.glsl similarity index 100% rename from shader/poly_shadow.glsl rename to lib/shaders/poly_shadow.glsl diff --git a/shader/reflection.glsl b/lib/shaders/reflection.glsl similarity index 100% rename from shader/reflection.glsl rename to lib/shaders/reflection.glsl diff --git a/shader/refraction.glsl b/lib/shaders/refraction.glsl similarity index 100% rename from shader/refraction.glsl rename to lib/shaders/refraction.glsl diff --git a/shader/scanlines.glsl b/lib/shaders/scanlines.glsl similarity index 100% rename from shader/scanlines.glsl rename to lib/shaders/scanlines.glsl diff --git a/shader/tilt_shift.glsl b/lib/shaders/tilt_shift.glsl similarity index 100% rename from shader/tilt_shift.glsl rename to lib/shaders/tilt_shift.glsl