i'm trying to figure out how i can send an array to a pixel shader which size isnt constant. I want to make a lighting shader and want to send two arrays of vec3 to the shader. The first array of vec3 contains the x and y coordinate and the range of the light source, the second array contains the color. Thats what i got until now:
Code: Select all
extern number numlights;
extern vec3 lightposrange[numlights];
extern vec3 lightcolors[numlights];
float lengthSqr(float x, float y){
return x*x + y*y;
}
float length(float x, float y){
return sqrt(lengthSqr(x,y));
}
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords){
vec4 pixel = Texel(texture, texture_coords);
vec2 lightToPixel = pixel_coords - lightposrange.xy;
for (int i = 0; i < numlights; i++){
if (length(lightToPixel.x, lightToPixel.y) <= lightposrange.z) {
pixel.r += lightcolors[i].r;
pixel.g += lightcolors[i].g;
pixel.b += lightcolors[i].b;
}
}
return pixel;
}
It says :
And some more errors that don't play a role for now.Line 4: error: non constant expression for array size
Line 5: error: non constant expression for array size
By setting
Code: Select all
extern number numlights;
Code: Select all
const extern number numlights;
Maybe you will need the love.draw function:Line 2: error: OpenGL requires constants to be initialized
Code: Select all
function love.draw()
-- draw unlighted scene in canvase
love.graphics.setPixelEffect()
love.graphics.setCanvas(unlightedSceneCanvas)
drawScene()
love.graphics.setCanvas()
-- shade everything
love.graphics.setPixelEffect(lightingShader)
-- create lightdata for shader
local lightposrange = {}
local lightcolors = {}
for i=1,#lights do
lightposrange[#lightposrange+1] = {lights[i].x, lights[i].y, lights[i].range}
lightcolors[#lightcolors+1] = lights[i].color
end
-- send lightdata to shader
lightingShader:send("numlights", #lights)
lightingShader:send("lightposrange", unpack(lightposrange))
lightingShader:send("lightcolors", unpack(lightcolors))
-- draw the unlighted scene with activated shader
love.graphics.draw(unlightedSceneCanvas)
end
EDIT:
fixed some general problems, main problem still there