Added basic shadows to point lights; Better than directional shadows
This commit is contained in:
1 parent
4b14362a04
commit
1a8320feab
21 files changed
+250
-71
No files matched your search
@@ -24,7 +24,8 @@ struct Light {
|
||||
vec3 pos;
|
||||
vec3 color;
|
||||
float strength;
|
||||
sampler2D depthTex;
|
||||
sampler2D directionalDepthTex;
|
||||
samplerCube pointDepthTex;
|
||||
};
|
||||
|
||||
uniform Camera camera;
|
||||
@@ -32,20 +33,20 @@ uniform Material material;
|
||||
uniform Light light[100];
|
||||
uniform int numberOfLights;
|
||||
uniform bool isContinued;
|
||||
uniform float farPlane;
|
||||
|
||||
out vec4 outColor;
|
||||
|
||||
float calcShadow(int lightNum, vec4 fragPosLight, vec3 no, vec3 lDir) {
|
||||
float calcShadowDirectional(int lightNum, vec4 fragPosLight, vec3 no, vec3 lDir) {
|
||||
vec3 fp = fragPosLight.xyz / fragPosLight.w;
|
||||
fp = fp * 0.5 + 0.5;
|
||||
float current = fp.z;
|
||||
float bias = max(0.05 * (1.0 - dot(no, lDir)), 0.005);
|
||||
float shad = 0.0f;
|
||||
vec2 texSize = 1.0 / textureSize(light[lightNum].depthTex, 0);
|
||||
|
||||
vec2 texSize = 1.0 / textureSize(light[lightNum].directionalDepthTex, 0);
|
||||
for (int x = -1; x <= 1; x++) {
|
||||
for (int y = -1; y <= 1; y++) {
|
||||
float pcfDepth = texture(light[lightNum].depthTex, fp.xy + vec2(x, y) * texSize).r;
|
||||
float pcfDepth = texture(light[lightNum].directionalDepthTex, fp.xy + vec2(x, y) * texSize).r;
|
||||
shad += current - bias > pcfDepth ? 1.0 : 0.0;
|
||||
}
|
||||
}
|
||||
@@ -55,6 +56,33 @@ float calcShadow(int lightNum, vec4 fragPosLight, vec3 no, vec3 lDir) {
|
||||
return shad;
|
||||
}
|
||||
|
||||
vec3 offsets[20] = vec3[](
|
||||
vec3(1, 1, 1), vec3(1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1),
|
||||
vec3(1, 1, -1), vec3(1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1),
|
||||
vec3(1, 1, 0), vec3(1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
|
||||
vec3(1, 0, 1), vec3(-1, 0, 1), vec3(1, 0, -1), vec3(-1, 0, -1),
|
||||
vec3(0, 1, 1), vec3(0, -1, 1), vec3(0, -1, -1), vec3(0, 1, -1)
|
||||
);
|
||||
|
||||
float calcShadowPoint(int lightNum, vec3 no, vec3 lDir) {
|
||||
vec3 fragLight = fragPos - light[lightNum].pos;
|
||||
float current = length(fragLight);
|
||||
float bias = 0.15;
|
||||
float closest = 0.0f;
|
||||
float shad = 0.0f;
|
||||
int samples = 20;
|
||||
float viewDist = length(camera.pos - fragPos);
|
||||
float disk = (1.0 + (viewDist / farPlane)) / 50.0f;
|
||||
for (int i = 0; i < samples; i++) {
|
||||
closest = texture(light[lightNum].pointDepthTex, fragLight + offsets[i] * disk).r;
|
||||
closest *= farPlane;
|
||||
if (current - bias > closest)
|
||||
shad += 1.0f;
|
||||
}
|
||||
shad /= float(samples);
|
||||
return shad;
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec3 color = vec3(0.0);
|
||||
|
||||
@@ -73,9 +101,6 @@ void main() {
|
||||
if (!isContinued)
|
||||
color += ambient;
|
||||
for (int i = 0; i < numberOfLights; i++) {
|
||||
if (i == numberOfLights - 1 && numberOfLights == 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (light[i].type == 1) {
|
||||
vec3 lightDir = normalize(light[i].pos - fragPos);
|
||||
@@ -88,7 +113,7 @@ void main() {
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.specPower);
|
||||
vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.0f);
|
||||
|
||||
float shadow = calcShadow(i, fragPosLightSpace[i], norm, lightDir);
|
||||
float shadow = calcShadowDirectional(i, fragPosLightSpace[i], norm, lightDir);
|
||||
color += (diffuse + specular) * (1.0 - shadow);
|
||||
continue;
|
||||
}
|
||||
@@ -96,17 +121,18 @@ void main() {
|
||||
vec3 lightDir = normalize(light[i].pos - fragPos);
|
||||
vec3 norm = normalize(normals);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = light[i].color * (diff * matDiff) * (light[i].strength / 2.0f);
|
||||
vec3 diffuse = light[i].color * (diff * matDiff);
|
||||
|
||||
vec3 viewDir = normalize(camera.pos - fragPos);
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.specPower);
|
||||
vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.0f);
|
||||
vec3 specular = light[i].color * spec * matSpec;
|
||||
|
||||
float length = length(light[i].pos - fragPos);
|
||||
float att = clamp(10.0 / length, 0.0, 1.0) * light[i].strength;
|
||||
float att = 1.0 / (1.0 + 0.09 * length + 0.032 * (length * length));
|
||||
|
||||
color += ((diffuse + specular) * att);
|
||||
float shadow = calcShadowPoint(i, norm, lightDir);
|
||||
color += (((diffuse + specular) * (1.0 - shadow)) * att) * light[i].strength;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#version 330 core
|
||||
|
||||
in vec4 fragPos;
|
||||
|
||||
uniform vec3 lightPos;
|
||||
uniform float farPlane;
|
||||
|
||||
void main() {
|
||||
float dist = length(fragPos.xyz - lightPos);
|
||||
dist /= farPlane;
|
||||
gl_FragDepth = dist;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#version 330 core
|
||||
|
||||
layout(triangles) in;
|
||||
layout(triangle_strip, max_vertices = 18) out;
|
||||
|
||||
uniform mat4 lightSpaceMat[6];
|
||||
|
||||
out vec4 fragPos;
|
||||
|
||||
void main() {
|
||||
for (int face = 0; face < 6; face++) {
|
||||
gl_Layer = face;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
fragPos = gl_in[i].gl_Position;
|
||||
gl_Position = lightSpaceMat[face] * fragPos;
|
||||
EmitVertex();
|
||||
}
|
||||
EndPrimitive();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec3 pos;
|
||||
|
||||
uniform mat4 model;
|
||||
|
||||
void main() {
|
||||
gl_Position = model * vec4(pos, 1.0);
|
||||
}
|
||||
Reference in new issue
Block a user