From 9b436e67cdc5ee896c3c2fec90499e400a9e524e Mon Sep 17 00:00:00 2001 From: Sebastian Park Date: Wed, 17 Apr 2024 03:10:27 -0400 Subject: Do realtime refraction kinda. --- resources/shaders/shader.frag | 29 ++++++++++++++++++++++++++-- resources/shaders/shader.vert | 45 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 3 deletions(-) (limited to 'resources/shaders') diff --git a/resources/shaders/shader.frag b/resources/shaders/shader.frag index d2183cc..66a36d0 100755 --- a/resources/shaders/shader.frag +++ b/resources/shaders/shader.frag @@ -5,20 +5,45 @@ in vec3 normal_cameraSpace; in vec3 camera_worldSpace; in vec3 normal_worldSpace; in vec3 pos; +in vec3 refrPos; +in vec2 uv; uniform int wire = 0; uniform float red = 1.0; uniform float green = 1.0; uniform float blue = 1.0; uniform float alpha = 1.0; +uniform sampler2D sampler; +uniform vec2 widthBounds; +uniform vec2 lengthBounds; +//uniform float test = 0; + +vec2 uvFromWorldPoint(vec3 point) { + float u = (point.x - widthBounds[0]) / (widthBounds[1] - widthBounds[0]); + float v = (point.z - lengthBounds[0]) / (lengthBounds[1] - lengthBounds[0]); + return vec2(u, v); +} void main() { // Do lighting in camera space vec3 lightDir = normalize(vec3(0, 0.5, 1)); - float d = clamp(dot(normal_cameraSpace, lightDir), 0, 1); + lightDir = normalize(vec3(0.f, 3.f, 0.f) - pos); +// float d = clamp(dot(normal_cameraSpace, lightDir), 0, 1); + float d = clamp(dot(normal_worldSpace, lightDir), 0, 1); vec3 reflectedLight = lightDir - 2 * dot(lightDir, normal_worldSpace) * normal_worldSpace; vec3 posToCam = normalize(camera_worldSpace - pos); float spec = pow(dot(posToCam, reflectedLight), 2.f); - fragColor = clamp(0.5f * vec4(red * d, green * d, blue * d, 0.5f) + 0.5f * vec4(1, 1, 1, 1) * spec, 0, 1); +// fragColor = texture(sampler, vec2(0.5f, 0.5f)); +// fragColor = vec4(abs(pos.x / 160.f), pos.y, 0.f, 1.f); +// fragColor = vec4(uv.y, uv.y, 0.f, 1.f); +// fragColor = vec4(camera_worldSpace.x - pos[0], camera_worldSpace.y - pos[1], pos[2], 1.f); +// fragColor = vec4(- pos[0], 0.f, 0.f, 1.f); +// fragColor = vec4((pos - vec3(widthBounds[0], 0, lengthBounds[0])) / 5.f, 1.f); +// fragColor = vec4(fragColor.x, 0.f, fragColor.z, 1.f); +// fragColor = vec4(test, test, test, 1.f); + fragColor = vec4(vec3(uvFromWorldPoint(refrPos), 0.f), 1.f); + +// fragColor = clamp(0.5f * vec4(red * d, green * d, blue * d, 0.5f) + 0.5f * vec4(1, 1, 1, 1) * spec, 0, 1); + } diff --git a/resources/shaders/shader.vert b/resources/shaders/shader.vert index 15e6833..90ebdf8 100755 --- a/resources/shaders/shader.vert +++ b/resources/shaders/shader.vert @@ -7,6 +7,8 @@ uniform mat4 proj; uniform mat4 view; uniform mat4 model; uniform mat4 inverseView; +//uniform float width; +//uniform float height; // TODO: Pass in width and height as uniform uniform mat3 inverseTransposeModel; @@ -14,12 +16,53 @@ out vec3 normal_cameraSpace; out vec3 normal_worldSpace; out vec3 camera_worldSpace; out vec3 pos; +out vec3 refrPos; +out vec2 uv; + +vec3 getRefrPos() { + float depth = -1.f; + vec3 w_o = pos - camera_worldSpace; + float cos_theta_i = dot(-w_o, normal_worldSpace); + 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))); + + 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) * normal_worldSpace; +// Ray reflectedRay(i.hit, w_t); +// float attenuation = (!entering && attenuateRefract) ? std::pow(M_E, (-(ray.o - i.hit).norm()) * mat.ior) : 1; +// L += traceRay(reflectedRay, scene, true, n_t, !is_in_refractor) * attenuation / threshold; + float dist = position.y - depth; + float depthScale = dist / w_t.y; + vec3 groundContactPoint = -(w_t * depthScale) + position; + return groundContactPoint; + } else { +// Eigen::Vector3f w_i = w_o - 2 * w_o.dot(intersectNormal) * incidenceNormal; +// Ray reflectedRay(i.hit, w_i); +// L += traceRay(reflectedRay, scene, true, current_ior, false) / threshold; + return vec3(0, 0, 0); + } +} void main() { + float depth = -2.f; + float dist = position.y - depth; + float width = 81.f * 2.f; + float length = 81.f * 2.f; + normal_cameraSpace = normalize(inverse(transpose(mat3(view))) * inverseTransposeModel * normal); camera_worldSpace = vec3(inverseView * vec4(0.f, 0.f, 0.f, 1.f)); normal_worldSpace = normal; - pos = position; + pos = vec3(model * vec4(position, 1.f)); //vec3(model * vec4(objSpacePos, 1.f)); +// pos = position; + + float depthScale = dist / normal.y; + vec3 groundContactPoint = -(normal * depthScale) + position; // carries down to ground + groundContactPoint = vec3(model * vec4(position, 1)); + uv = vec2((position.x + 81.f) / (162.f), groundContactPoint.z); +// uv = vec2(normal); + refrPos = getRefrPos(); gl_Position = proj * view * model * vec4(position, 1); } -- cgit v1.2.3-70-g09d2 From 1d5c17db40e6bddf88c718d498eabbff0efbf4dd Mon Sep 17 00:00:00 2001 From: Sebastian Park Date: Wed, 17 Apr 2024 11:17:35 -0400 Subject: Refraction stuff. Also refractor shading equation. --- resources/shaders/shader.frag | 15 ++++++++++++--- resources/shaders/shader.vert | 16 +++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) (limited to 'resources/shaders') diff --git a/resources/shaders/shader.frag b/resources/shaders/shader.frag index 66a36d0..9668ecb 100755 --- a/resources/shaders/shader.frag +++ b/resources/shaders/shader.frag @@ -6,6 +6,7 @@ in vec3 camera_worldSpace; in vec3 normal_worldSpace; in vec3 pos; in vec3 refrPos; +in float refrProb; in vec2 uv; uniform int wire = 0; @@ -42,8 +43,16 @@ void main() { // fragColor = vec4((pos - vec3(widthBounds[0], 0, lengthBounds[0])) / 5.f, 1.f); // fragColor = vec4(fragColor.x, 0.f, fragColor.z, 1.f); // fragColor = vec4(test, test, test, 1.f); - fragColor = vec4(vec3(uvFromWorldPoint(refrPos), 0.f), 1.f); - -// fragColor = clamp(0.5f * vec4(red * d, green * d, blue * d, 0.5f) + 0.5f * vec4(1, 1, 1, 1) * spec, 0, 1); + vec2 refrUV = uvFromWorldPoint(refrPos); + vec4 transmissive = vec4(vec3(refrUV, 1.f - refrUV.y), 1.f); + fragColor = 0.25f * vec4(red * d, green * d, blue * d, 1.0f); // Diffuse + fragColor += 0.75f * vec4(1, 1, 1, 1) * pow(spec, 10.f); // Specular TODO: Pass multiplications as uniforms. + fragColor = clamp(fragColor, 0.f, 1.f); // Clamp + fragColor *= (1 - (refrProb / 1.f)); + fragColor += (refrProb / 1.5f) * transmissive; + fragColor = vec4(vec3(fragColor), 1.5f); + // Dividing refrProb by 2 just for heuristic. Want more phong to show through. +// fragColor = clamp(fragColor, 0.f, 1.f); +// fragColor = vec4(refrProb, 0.f, 0.f, 1.f); } diff --git a/resources/shaders/shader.vert b/resources/shaders/shader.vert index 90ebdf8..bc6cb2e 100755 --- a/resources/shaders/shader.vert +++ b/resources/shaders/shader.vert @@ -17,16 +17,20 @@ out vec3 normal_worldSpace; out vec3 camera_worldSpace; out vec3 pos; out vec3 refrPos; +out float refrProb; out vec2 uv; -vec3 getRefrPos() { - float depth = -1.f; +vec4 getRefrPos() { + float depth = -1.f; // TODO: Pass as uniform vec3 w_o = pos - camera_worldSpace; float cos_theta_i = dot(-w_o, normal_worldSpace); 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) * normal_worldSpace; @@ -36,12 +40,12 @@ vec3 getRefrPos() { float dist = position.y - depth; float depthScale = dist / w_t.y; vec3 groundContactPoint = -(w_t * depthScale) + position; - return groundContactPoint; + return vec4(groundContactPoint, 1.f - prob_to_refl); } else { // Eigen::Vector3f w_i = w_o - 2 * w_o.dot(intersectNormal) * incidenceNormal; // Ray reflectedRay(i.hit, w_i); // L += traceRay(reflectedRay, scene, true, current_ior, false) / threshold; - return vec3(0, 0, 0); + return vec4(0, 0, 0, 0); } } @@ -62,7 +66,9 @@ void main() { groundContactPoint = vec3(model * vec4(position, 1)); uv = vec2((position.x + 81.f) / (162.f), groundContactPoint.z); // uv = vec2(normal); - refrPos = getRefrPos(); + vec4 refrPos_and_prob = getRefrPos(); + refrPos = vec3(refrPos_and_prob); + refrProb = clamp(refrPos_and_prob.w, 0.f, 1.f); gl_Position = proj * view * model * vec4(position, 1); } -- cgit v1.2.3-70-g09d2 From 28d74097815a8d52b8f47f6eae6464005a6bc552 Mon Sep 17 00:00:00 2001 From: Sebastian Park Date: Mon, 22 Apr 2024 00:52:22 -0400 Subject: normalize direction to camera. --- CMakeLists.txt.user | 2 +- resources/shaders/shader.frag | 1 + resources/shaders/shader.vert | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) (limited to 'resources/shaders') diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user index 5b63c10..24cd5bb 100644 --- a/CMakeLists.txt.user +++ b/CMakeLists.txt.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/resources/shaders/shader.frag b/resources/shaders/shader.frag index 9668ecb..141915e 100755 --- a/resources/shaders/shader.frag +++ b/resources/shaders/shader.frag @@ -51,6 +51,7 @@ void main() { fragColor = clamp(fragColor, 0.f, 1.f); // Clamp fragColor *= (1 - (refrProb / 1.f)); fragColor += (refrProb / 1.5f) * transmissive; +// fragColor = transmissive * refrProb; fragColor = vec4(vec3(fragColor), 1.5f); // Dividing refrProb by 2 just for heuristic. Want more phong to show through. // fragColor = clamp(fragColor, 0.f, 1.f); diff --git a/resources/shaders/shader.vert b/resources/shaders/shader.vert index bc6cb2e..8dda424 100755 --- a/resources/shaders/shader.vert +++ b/resources/shaders/shader.vert @@ -22,7 +22,7 @@ out vec2 uv; vec4 getRefrPos() { float depth = -1.f; // TODO: Pass as uniform - vec3 w_o = pos - camera_worldSpace; + vec3 w_o = normalize(pos - camera_worldSpace); float cos_theta_i = dot(-w_o, normal_worldSpace); float n_i = 1; float n_t = 1.33f; -- cgit v1.2.3-70-g09d2