Fix shadow mapping onto terrain

This commit is contained in:
austin 2022-09-16 21:43:54 -04:00
parent eb9ea36749
commit 32775dc4ee
2 changed files with 10 additions and 6 deletions

View File

@ -100,6 +100,7 @@ vec4 getTextureColor(int index, vec2 coord){
if(index == 4){
return texture(groundTextures[4], coord);
}
// return texture(shadowMap, coord);
// return vec3(1,1,1);
return vec4(0,0,0,1);
}
@ -148,9 +149,9 @@ void main(){
vec3 result = CalcDirLight(norm, viewDir, finalTexColor);
for(int i = 0; i < NR_POINT_LIGHTS; i++){
result += CalcPointLight(i, norm, FragPos, viewDir);
}
// for(int i = 0; i < NR_POINT_LIGHTS; i++){
// result += CalcPointLight(i, norm, FragPos, viewDir);
// }
// result += CalcSpotLight(spotLight, norm, FragPos, viewDir);
FragColor = vec4(result, 1);//texture(ourTexture, TexCoord);//vec4(result, 1.0);
@ -172,7 +173,7 @@ vec3 CalcDirLight(vec3 normal, vec3 viewDir, vec3 texColor){
float shadow = ShadowCalculation(FragPosLightSpace, lightDir, normal);
// return shadow * vec3(1,1,1);
return ( ambient + (1.0-shadow) * diffuse ) * texColor;// + specular);
}
@ -245,7 +246,7 @@ float ShadowCalculation(vec4 fragPosLightSpace, vec3 lightDir, vec3 normal){
float currentDepth = projCoords.z;
//calculate bias
float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005);
float bias = min(0.05 * (1.0 - dot(normal, lightDir)), 0.005);
//calculate shadow value
float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
@ -254,7 +255,7 @@ float ShadowCalculation(vec4 fragPosLightSpace, vec3 lightDir, vec3 normal){
shadow = 0.0;
}
// shadow = currentDepth;
// shadow = currentDepth - closestDepth;
return shadow;
}

View File

@ -15,6 +15,7 @@ uniform mat4 transform;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform mat4 lightSpaceMatrix;
@ -22,6 +23,7 @@ uniform mat4 projection;
out vec3 Normal;
out vec3 FragPos;
out vec2 TexCoord;
out vec4 FragPosLightSpace;
flat out ivec4 groundTexIndices;
@ -42,6 +44,7 @@ void main() {
groundTexIndices = ivec4(int(aGroundTexIndices.x),int(aGroundTexIndices.y),int(aGroundTexIndices.z),int(aGroundTexIndices.w));
// groundTexIndices = vec4(int(aGroundTexIndices.x),int(aGroundTexIndices.y),int(aGroundTexIndices.z),int(aGroundTexIndices.w));
// groundTexIndices = ivec4(0,1,2,3);
FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.0);
//set final position with opengl space
gl_Position = projection * view * model * FinalVertex;