Add blue underwater fog to shaders

This commit is contained in:
Mateusz Drewniak 2022-01-23 14:22:04 +01:00
parent f20ed91d6f
commit bfa4483f76
5 changed files with 46 additions and 4 deletions

View File

@ -2,12 +2,15 @@
uniform vec3 objectColor;
uniform vec3 lightDir;
uniform vec3 skyColor;
in vec3 interpNormal;
in float visibility;
void main()
{
vec3 normal = normalize(interpNormal);
float diffuse = max(dot(normal, -lightDir), 0.0);
gl_FragColor = vec4(objectColor * diffuse, 1.0);
gl_FragColor = mix(vec4(skyColor, 1.0), gl_FragColor, visibility);
}

View File

@ -6,11 +6,22 @@ layout(location = 2) in vec2 vertexTexCoord;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelMatrix;
uniform mat4 cameraMatrix;
uniform float fogDensity = 0.007;
uniform float fogGradient = 1.5;
out vec3 interpNormal;
out float visibility;
void main()
{
gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);
vec4 worldPosition = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);
gl_Position = worldPosition;
interpNormal = (modelMatrix * vec4(vertexNormal, 0.0)).xyz;
vec4 positionRelativeToCamera = cameraMatrix * worldPosition;
float distanceFromCamera = length(positionRelativeToCamera.xyz);
visibility = exp(-pow((distanceFromCamera * fogDensity), fogGradient));
visibility = clamp(visibility, 0.0, 1.0);
}

View File

@ -2,9 +2,11 @@
uniform sampler2D textureSampler;
uniform vec3 lightDir;
uniform vec3 skyColor;
in vec3 interpNormal;
in vec2 interpTexCoord;
in float visibility;
void main()
{
@ -13,4 +15,5 @@ void main()
vec3 normal = normalize(interpNormal);
float diffuse = max(dot(normal, -lightDir), 0.0);
gl_FragColor = vec4(color * diffuse, 1.0);
gl_FragColor = mix(vec4(skyColor, 1.0), gl_FragColor, visibility);
}

View File

@ -2,17 +2,29 @@
layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 vertexNormal;
layout(location = 2) in vec2 vertexTexCoord;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelMatrix;
uniform mat4 cameraMatrix;
uniform float fogDensity = 0.007;
uniform float fogGradient = 1.5;
out vec3 interpNormal;
out vec2 interpTexCoord;
out float visibility;
void main()
{
gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);
vec4 worldPosition = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);
gl_Position = worldPosition;
interpNormal = (modelMatrix * vec4(vertexNormal, 0.0)).xyz;
interpTexCoord = vertexTexCoord;
vec4 positionRelativeToCamera = cameraMatrix * worldPosition;
float distanceFromCamera = length(positionRelativeToCamera.xyz);
visibility = exp(-pow((distanceFromCamera * fogDensity), fogGradient));
visibility = clamp(visibility, 0.0, 1.0);
}

View File

@ -41,6 +41,9 @@ float cameraAngle = 0;
glm::mat4 cameraMatrix, perspectiveMatrix;
glm::vec3 lightDir = glm::normalize(glm::vec3(0.0f, -1.0f, 0.0f));
glm::vec3 skyColor = glm::vec3(0.0705882353f, 0.4862745098f, 0.7568627451f); // Star Command Blue https://coolors.co/127cc1-f7f9f9-6daedb-bed8d4-93b7be
float fogDensity = 0.03;
float fogGradient = 0.5;
glm::quat rotation = glm::quat(1, 0, 0, 0);
@ -81,7 +84,7 @@ glm::mat4 createCameraMatrix()
glm::quat xMouseRotation = glm::angleAxis(mousePositionDifference[0] / 120, glm::vec3(0, 1, 0));
glm::quat yMouseRotation = glm::angleAxis(mousePositionDifference[1] / 120, glm::vec3(1, 0, 0));
glm::quat mouseRotation = xMouseRotation * yMouseRotation;
glm::quat mouseRotation = glm::normalize(xMouseRotation * yMouseRotation);
mousePositionDifference = {0, 0};
glm::quat invRotation = glm::inverse(mouseRotation);
@ -98,11 +101,16 @@ void drawObjectColor(Core::RenderContext context, glm::mat4 modelMatrix, glm::ve
glUseProgram(program);
glUniform1f(glGetUniformLocation(program, "fogDensity"), fogDensity);
glUniform1f(glGetUniformLocation(program, "fogGradient"), fogGradient);
glUniform3f(glGetUniformLocation(program, "objectColor"), color.x, color.y, color.z);
glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z);
glUniform3f(glGetUniformLocation(program, "skyColor"), skyColor.x, skyColor.y, skyColor.z);
glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "modelViewProjectionMatrix"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "cameraMatrix"), 1, GL_FALSE, (float*)&perspectiveMatrix);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
Core::DrawContext(context);
@ -116,11 +124,16 @@ void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuin
glUseProgram(program);
glUniform1f(glGetUniformLocation(program, "fogDensity"), fogDensity);
glUniform1f(glGetUniformLocation(program, "fogGradient"), fogGradient);
glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z);
glUniform3f(glGetUniformLocation(program, "skyColor"), skyColor.x, skyColor.y, skyColor.z);
Core::SetActiveTexture(textureId, "textureSampler", program, 0);
glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "modelViewProjectionMatrix"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "cameraMatrix"), 1, GL_FALSE, (float*)&perspectiveMatrix);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
Core::DrawContext(context);