Here's the shader in question:
Code: Select all
varying vec4 worldPosition;
varying vec4 viewPosition;
varying vec4 screenPosition;
varying vec3 vertexNormal;
varying vec4 vertexColor;
#define MAX_LIGHTS 16
struct lightSource {
vec3 position;
vec3 color;
};
extern lightSource lights[MAX_LIGHTS];
float specularStrength = 0.5;
vec3 ambient = vec3(0.1, 0.1, 0.1);
vec4 effect(vec4 color, Image tex, vec2 tc, vec2 sc) {
vec3 fragPosition = vec3(worldPosition[0], worldPosition[1], worldPosition[2]);
vec3 viewPos = vec3(viewPosition[0], viewPosition[1], viewPosition[2]);
vec3 norm = normalize(vertexNormal);
vec3 viewDirection = normalize(viewPos - fragPosition);
vec3 diffuse = ambient;
vec3 specular = ambient;
for (int i = 0; i < MAX_LIGHTS; i++) {
lightSource l = lights[i];
vec3 lightDirection = normalize(l.position - fragPosition);
vec3 reflectDirection = reflect(-lightDirection, norm);
float spec = pow(max(dot(viewDirection, reflectDirection), 0.0), 32.0);
specular += specularStrength * spec * l.color;
float diff = max(dot(norm, lightDirection), 0.0);
diffuse += diff * l.color;
};
diffuse = clamp(diffuse, 0.0, 1.0);
return vec4(ambient + diffuse + specular, 1.0);
}
Here's the rest of the code:
Code: Select all
-- GLOBALS
lg = love.graphics
fs = love.filesystem
kb = love.keyboard
lm = love.mouse
random = math.random
noise = love.math.noise
sin = math.sin
cos = math.cos
f = string.format
function love.load()
g3d = require("g3d")
cubes = {}
count = 16
radius = 10
for i=1, count do
local a = (math.pi * 2) / count * i
x = radius * cos(a)
y = radius * sin(a)
cubes[i] = g3d.newModel("cube.obj", nil, {x, y, 0}, {random(), random(), random()})
end
lights = {
{
position = {0, 0, 0},
color = {1, 1, 1}
},
{
position = {10, 0, 0},
color = {1, 0, 0}
},
{
position = {0, 10, 0},
color = {0, 0, 1}
},
}
g3d.shader:send("lights", lights)
time = 0
end
function love.update(dt)
time = time + dt
if time > math.pi * 2 then time = 0 end
g3d.camera.firstPersonMovement(dt)
end
function love.mousemoved(x, y, dx, dy)
g3d.camera.firstPersonLook(dx, dy)
end
function love.draw()
for i,v in ipairs(cubes) do
local ox, oy, oz = unpack(v.translation)
local rx, ry, rz = unpack(v.rotation)
v:setTransform({ox, oy, sin(time + i) * 5}, {time, time, time})
v:draw()
end
end
function love.keypressed(key)
if key == "escape" then love.event.push("quit") end
end