diff options
author | Sebastian Park <SebPark03@gmail.com> | 2024-05-09 22:02:12 -0400 |
---|---|---|
committer | Sebastian Park <SebPark03@gmail.com> | 2024-05-09 22:02:12 -0400 |
commit | 481e582be02385271f87524bfe46d6c41654e23c (patch) | |
tree | ddb9a0b98de7e5b40aeea55464deb75cc753a15d /resources | |
parent | f1230ea24dc87a3bb5c9c709d4571e3298d7d07f (diff) |
"raytraced" caustics.
Diffstat (limited to 'resources')
-rw-r--r-- | resources/shaders/caustics.frag | 20 | ||||
-rw-r--r-- | resources/shaders/caustics.vert | 53 | ||||
-rwxr-xr-x | resources/shaders/shader.frag | 3 |
3 files changed, 75 insertions, 1 deletions
diff --git a/resources/shaders/caustics.frag b/resources/shaders/caustics.frag new file mode 100644 index 0000000..f746896 --- /dev/null +++ b/resources/shaders/caustics.frag @@ -0,0 +1,20 @@ +#version 410 core +out vec4 fragColor; + +in vec3 normal_worldSpace; +in vec3 pos; +in vec3 newPos; +in vec4 col; +in float refractProb; + +uniform sampler2D normSamp; + +void main() { +// fragColor = vec4(vec3((pos[0] + 1) / 2, (pos[1] + 1) / 2, 0.f), 1.f); + float oldArea = length(dFdx(vec3(pos[0], pos[2], pos[1]))) * length(dFdy(vec3(pos[0], pos[2], pos[1]))); + float newArea = length(dFdx(vec3(newPos[0], newPos[2], newPos[1]))) * length(dFdy(vec3(newPos[0], newPos[2], newPos[1]))); + float areaRatio = oldArea / newArea; + float intensity = pow(areaRatio * .3f, 1.5f); + fragColor = vec4(0.98, 1, .78, intensity * refractProb); +// fragColor = col; +} diff --git a/resources/shaders/caustics.vert b/resources/shaders/caustics.vert new file mode 100644 index 0000000..298f5e1 --- /dev/null +++ b/resources/shaders/caustics.vert @@ -0,0 +1,53 @@ +#version 330 core + +layout(location = 0) in vec3 position; // Position of the vertex +layout(location = 1) in vec3 normal; // Normal of the vertex +layout(location = 2) in vec3 texCoords; // Normal of the vertex + +uniform float depth = -1000.f; +uniform float skyHeight = 500.f; + +uniform sampler2D normSamp; + +out vec3 normal_worldSpace; +out vec3 pos; +out vec3 newPos; +out vec4 col; +out float refractProb; + +vec4 refractToFloor(vec3 l, vec3 p, vec3 n, float d) { + // Refracts incoming light direction l through normal n at point p until hits floor at depth d + vec3 w_o = normalize(l); + float cos_theta_i = dot(-w_o, n); + float n_i = 1; + float n_t = 1.33f; + float determinant = 1.f - (pow((n_i / n_t), 2.f) * (1.f - pow(cos_theta_i, 2.f))); + + float r0 = pow((n_i - n_t) / (n_i + n_t), 2.f); // variable required to calculate probability of reflection + float prob_to_refl = r0 + ((1 - r0) * pow((1 - cos_theta_i), 5.f)); + + if (determinant >= 0) { + float cos_theta_t = sqrt(determinant); + vec3 w_t = (n_i / n_t) * w_o + ((n_i / n_t) * cos_theta_i - cos_theta_t) * n; + float dist = p.z - d; + float depthScale = dist / w_t.z; +// vec3 groundContactPoint = -(w_t * depthScale) + p; + vec3 groundContactPoint = (w_t * depthScale) + p; + return vec4(groundContactPoint, 1.f - prob_to_refl); + } else { + return vec4(0, 0, 0, 0); + } +} + +void main() { + normal_worldSpace = normal; + pos = position; + vec4 sampledNormal = texture(normSamp, vec2((pos + 1) / 2)); + sampledNormal = (sampledNormal * 2.f) - 1.f; + col = sampledNormal; + vec4 newPosAndProb = refractToFloor(vec3(0, 0, 1), pos, normalize(vec3(sampledNormal)), 0.01f); + newPos = vec3(newPosAndProb[0], newPosAndProb[1], 0.f); + refractProb = newPosAndProb[3]; +// newPos = pos; + gl_Position = vec4(newPos, 1.f); +} diff --git a/resources/shaders/shader.frag b/resources/shaders/shader.frag index 88c783d..2bc8043 100755 --- a/resources/shaders/shader.frag +++ b/resources/shaders/shader.frag @@ -71,7 +71,8 @@ void main() { vec2 reflUV = uvFromWorldPoint(reflPos); // float waterMurkiness = 0.002f; // TODO: Make uniform - float waterMurkiness = 0.0005f; // TODO: Make uniform +// float waterMurkiness = 0.0005f; // TODO: Make uniform + float waterMurkiness = 0.f; // TODO: Make uniform vec3 waterVolumeColor = vec3(red * 0.1f, green * 0.2f, blue * 0.2f); float murkDiffuse = 0.3f; float murkAmbient = 0.8f; |