summaryrefslogtreecommitdiff
path: root/resources/shaders/caustics.vert
diff options
context:
space:
mode:
authorSebastian Park <SebPark03@gmail.com>2024-05-09 23:18:46 -0400
committerSebastian Park <SebPark03@gmail.com>2024-05-09 23:18:46 -0400
commitf9819ae7f8b11b298c16eb14d929a9c09038a877 (patch)
treecef7f0c104d49d41a1e2c76bec2343f875ca8dff /resources/shaders/caustics.vert
parentac3f1e423e696392ea0a50c2652b829e5c5734b6 (diff)
parent99c588d4da2ca044df8276b7efbd9d27c19c8352 (diff)
Merge branch 'shaders'
Diffstat (limited to 'resources/shaders/caustics.vert')
-rw-r--r--resources/shaders/caustics.vert53
1 files changed, 53 insertions, 0 deletions
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);
+}