Add fog to shader

This commit is contained in:
barmal4 2022-01-17 18:49:26 +01:00
parent 3f9711c675
commit 4a26686c86
10 changed files with 35 additions and 4 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -6,15 +6,29 @@ uniform vec3 cameraPos;
in vec3 interpNormal;
in vec3 fragPos;
float near = 0.15f;
float far = 100.0f;
float linearizeDepth(float depth)
{
return (2.0 * near * far) / (far + near - (depth * 2.0 - 1.0) * (far - near));
}
float logisticDepth(float depth, float steepness = 0.5f, float offset = 5.0f)
{
float zVal = linearizeDepth(depth);
return (1 / (1 + exp(-steepness * (zVal - offset))));
}
void main()
{
float depth = logisticDepth(gl_FragCoord.z);
vec3 lightDir = normalize(lightPos-fragPos);
vec3 V = normalize(cameraPos-fragPos);
vec3 normal = normalize(interpNormal);
vec3 R = reflect(-normalize(lightDir),normal);
float specular = pow(max(0,dot(R,V)),10);
float diffuse = max(0,dot(normal,normalize(lightDir)));
gl_FragColor = vec4(mix(objectColor,objectColor*diffuse+vec3(1)*specular,0.9), 1.0);
float diffuse = max(0,dot(normal,lightDir));
gl_FragColor = vec4(mix(objectColor,objectColor*diffuse+vec3(1)*specular,0.9), 1.0) * (1.0f - depth) + vec4(depth * vec3(5.0f/255.0f, 35.0f/255.0f, 95.0f/255.0f), 1.0f);
}

View File

@ -8,8 +8,25 @@ uniform vec3 cameraPos;
in vec3 interpNormal;
in vec2 interpTexCoord;
in vec3 fragPos;
float near = 0.15f;
float far = 100.0f;
float linearizeDepth(float depth)
{
return (2.0 * near * far) / (far + near - (depth * 2.0 - 1.0) * (far - near));
}
float logisticDepth(float depth, float steepness = 0.5f, float offset = 5.0f)
{
float zVal = linearizeDepth(depth);
return (1 / (1 + exp(-steepness * (zVal - offset))));
}
void main()
{
float depth = logisticDepth(gl_FragCoord.z);
vec2 modifiedTexCoord = vec2(interpTexCoord.x, 1.0 - interpTexCoord.y); // Poprawka dla tekstur Ziemi, ktore bez tego wyswietlaja sie 'do gory nogami'
vec3 color = texture2D(textureSampler, modifiedTexCoord).rgb;
vec3 lightDir = normalize(lightPos-fragPos);
@ -17,7 +34,7 @@ void main()
vec3 normal = normalize(interpNormal);
vec3 R = reflect(-normalize(lightDir),normal);
float specular = pow(max(0,dot(R,V)),10);
float specular = pow(max(0,dot(R,V)),100);
float diffuse = max(0,dot(normal,normalize(lightDir)));
gl_FragColor = vec4(mix(color,color*diffuse+vec3(1)*specular,0.9), 1.0);
gl_FragColor = vec4(mix(color,color*diffuse+vec3(1)*specular,0.9), 1.0) * (1.0f - depth) + vec4(depth * vec3(5.0f/255.0f, 35.0f/255.0f, 95.0f/255.0f), 1.0f);
}