Add point lights

This commit is contained in:
Dane Johnson
2021-03-04 14:29:48 -06:00
parent 0a2f9ac7bf
commit 55f293137f
9 changed files with 174 additions and 13 deletions

View File

@@ -27,6 +27,17 @@ struct DirectionalLight {
float specular;
};
struct PointLight {
vec3 pos;
float radius;
vec3 color;
float ambient;
float diffuse;
float specular;
};
#define NUM_POINT_LIGHTS 4
uniform PointLight pointLights[NUM_POINT_LIGHTS];
struct Material {
sampler2D tex;
bool usesTex;
@@ -44,6 +55,29 @@ struct Material {
uniform DirectionalLight directionalLight;
uniform Material material;
void calcDirectionalLight(in vec3 normal) {
AMBIENT += directionalLight.ambient * directionalLight.color * material.ambient;
vec3 direction = -(VIEW * vec4(directionalLight.direction, 0.0)).xyz;
float diff = dot(normalize(direction), normalize(normal));
diff = max(diff, 0.0);
DIFFUSE += directionalLight.diffuse * diff * directionalLight.color * material.diffuse;
vec3 viewDir = normalize((VIEW * MODEL * vec4(pos, 1.0)).xyz);
vec3 reflectionDir = reflect(normalize(direction), normalize(normal));
float spec = dot(viewDir, reflectionDir);
spec = max(spec, 0.0);
spec = pow(spec, material.shininess);
SPECULAR += directionalLight.color * (spec * material.specular);
}
void calcPointLight(in vec3 normal, in PointLight pointLight) {
vec4 ray = VIEW * MODEL * vec4(pos, 1.0) - VIEW * vec4(pointLight.pos, 1.0);
float dist = length(ray);
float attenuation = max(1.0 - dist * dist / pointLight.radius / pointLight.radius, 0.0);
AMBIENT += pointLight.color * material.ambient * pointLight.ambient * attenuation;
}
void main() {
vec4 vertex = PROJECTION * VIEW * MODEL * vec4(pos, 1.0);
gl_Position = vertex;
@@ -58,17 +92,14 @@ void main() {
vec3 my_normal = (VIEW * mat4(NORMAL) * vec4(normal, 0.0)).xyz;
// Flat shading, we compute light per vertex
AMBIENT = directionalLight.ambient * directionalLight.color * material.ambient;
AMBIENT = vec3(0.0);
DIFFUSE = vec3(0.0);
SPECULAR = vec3(0.0);
vec3 direction = -(VIEW * vec4(directionalLight.direction, 0.0)).xyz;
float diff = dot(normalize(direction), normalize(my_normal));
diff = max(diff, 0.0);
DIFFUSE = directionalLight.diffuse * diff * directionalLight.color * material.diffuse;
calcDirectionalLight(my_normal);
vec3 viewDir = normalize((VIEW * MODEL * vec4(pos, 1.0)).xyz);
vec3 reflectionDir = reflect(normalize(direction), normalize(my_normal));
float spec = dot(viewDir, reflectionDir);
spec = max(spec, 0.0);
spec = pow(spec, material.shininess);
SPECULAR = directionalLight.color * (spec * material.specular);
// PointLights
for(int i = 0; i < NUM_POINT_LIGHTS; i++) {
calcPointLight(my_normal, pointLights[i]);
}
}