Vertex shader:
#version 120
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord;
uniform vec3[64] lights;
uniform vec3[64] lightColors;
uniform int lightingEnabled;
uniform int lightCount;
varying vec4 v_color;
varying vec2 v_texCoords;
float shade(vec3 light) {
float lx = light.x;
float ly = light.y;
float dist = distance(a_position.xy, vec2(lx, ly)) / light.z;
float c = (1 - sqrt(dist * dist * 2) / 300);
return c;
}
vec3 light(vec3 light, vec3 lightColor) {
float c = shade(light);
if(c < 0) c = 0;
vec3 l = vec3(lightColor.r * c, lightColor.g * c, lightColor.b * c);
return l;
}
vec4 calculateLighting() {
vec4 lighting=vec4(0.0, 0.0, 0.0, 1.0);
for(int i=0;i<lightCount;i++) {
lighting.rgb = max(light(lights[i], lightColors[i]), lighting.rgb);
}
return lighting;
}
void main() {
vec4 lighting = vec4(1.0, 1.0, 1.0, 1.0);
if(lightingEnabled == 1) {
lighting = vec4(0.0, 0.0, 0.0, 1.0);
lighting = calculateLighting();
}
vec4 pos = a_position;
pos.x = pos.x;
pos.y = pos.y;
v_color = a_color * lighting;
v_texCoords = a_texCoord;
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * pos;
}
Fragment:
#version 120
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
void main() {
vec4 col = texture2D(u_texture, v_texCoords);
gl_FragColor = col * v_color;
}
This code works properly, but is per vertex so it has some not nice features as one might expect. How do I move lighting calculations to fragment shader? I know I’m asking for quite a lot, but I’m kinda lost here…