Add ambient light from point lights
This commit is contained in:
parent
135b075aa1
commit
331056c206
@ -91,6 +91,8 @@ void Shader::UpdatePointLights(PointLightList pointLights) {
|
|||||||
|
|
||||||
glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "radius").c_str()), pointLights[i]->GetRadius());
|
glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "radius").c_str()), pointLights[i]->GetRadius());
|
||||||
glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "ambient").c_str()), pointLights[i]->GetAmbient());
|
glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "ambient").c_str()), pointLights[i]->GetAmbient());
|
||||||
|
glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "diffuse").c_str()), pointLights[i]->GetDiffuse());
|
||||||
|
glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "specular").c_str()), pointLights[i]->GetSpecular());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,7 +185,8 @@ function init_point_lights()
|
|||||||
pointLight:Translate(couch.Vector3(i * -10.0, 0, -10))
|
pointLight:Translate(couch.Vector3(i * -10.0, 0, -10))
|
||||||
pointLight:SetRadius(10.0)
|
pointLight:SetRadius(10.0)
|
||||||
pointLight:SetColor(color)
|
pointLight:SetColor(color)
|
||||||
pointLight:SetAmbient(1.0)
|
pointLight:SetAmbient(0.2)
|
||||||
|
pointLight:SetDiffuse(1.0)
|
||||||
pointLight:SetSpecular(0.1)
|
pointLight:SetSpecular(0.1)
|
||||||
local lightBox = couch.Mesh.FromFile("../resources/cube.obj")
|
local lightBox = couch.Mesh.FromFile("../resources/cube.obj")
|
||||||
lightBox:UniformScale(0.5);
|
lightBox:UniformScale(0.5);
|
||||||
|
@ -55,13 +55,16 @@ struct Material {
|
|||||||
uniform DirectionalLight directionalLight;
|
uniform DirectionalLight directionalLight;
|
||||||
uniform Material material;
|
uniform Material material;
|
||||||
|
|
||||||
|
float calcDiffuseIntensity(in vec3 direction, in vec3 normal) {
|
||||||
|
float diff = dot(normalize(direction), normalize(normal));
|
||||||
|
return max(diff, 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
void calcDirectionalLight(in vec3 normal) {
|
void calcDirectionalLight(in vec3 normal) {
|
||||||
AMBIENT += directionalLight.ambient * directionalLight.color * material.ambient;
|
AMBIENT += directionalLight.ambient * directionalLight.color * material.ambient;
|
||||||
|
|
||||||
vec3 direction = -(VIEW * vec4(directionalLight.direction, 0.0)).xyz;
|
vec3 direction = -(VIEW * vec4(directionalLight.direction, 0.0)).xyz;
|
||||||
float diff = dot(normalize(direction), normalize(normal));
|
DIFFUSE += directionalLight.diffuse * directionalLight.color * material.diffuse * calcDiffuseIntensity(direction, 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 viewDir = normalize((VIEW * MODEL * vec4(pos, 1.0)).xyz);
|
||||||
vec3 reflectionDir = reflect(normalize(direction), normalize(normal));
|
vec3 reflectionDir = reflect(normalize(direction), normalize(normal));
|
||||||
@ -72,10 +75,13 @@ void calcDirectionalLight(in vec3 normal) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void calcPointLight(in vec3 normal, in PointLight pointLight) {
|
void calcPointLight(in vec3 normal, in PointLight pointLight) {
|
||||||
vec4 ray = VIEW * MODEL * vec4(pos, 1.0) - VIEW * vec4(pointLight.pos, 1.0);
|
vec3 direction = vec3(VIEW * vec4(pointLight.pos, 1.0) - VIEW * MODEL * vec4(pos, 1.0)).xyz;
|
||||||
float dist = length(ray);
|
float dist = length(direction);
|
||||||
|
|
||||||
float attenuation = max(1.0 - dist * dist / pointLight.radius / pointLight.radius, 0.0);
|
float attenuation = max(1.0 - dist * dist / pointLight.radius / pointLight.radius, 0.0);
|
||||||
AMBIENT += pointLight.color * material.ambient * pointLight.ambient * attenuation;
|
AMBIENT += pointLight.color * material.ambient * pointLight.ambient * attenuation;
|
||||||
|
|
||||||
|
DIFFUSE += pointLight.color * pointLight.diffuse * material.diffuse * calcDiffuseIntensity(direction, normal);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user