summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Park <SebPark03@gmail.com>2024-05-08 01:14:11 -0400
committerSebastian Park <SebPark03@gmail.com>2024-05-08 01:14:11 -0400
commit3d9a55316dbcfb2ca1f32f5554d1948fcabb5d74 (patch)
treef2ca9a05d094ab0a85a42f500fb9ea1cc6630240
parentdcab788763ff0af5918ca0c50538daa8bbfc84d8 (diff)
Do some scuffed caustics.
-rw-r--r--CMakeLists.txt.user2
-rw-r--r--resources/shaders/color.frag42
-rw-r--r--resources/shaders/color.vert92
-rwxr-xr-xresources/shaders/shader.frag3
-rwxr-xr-xresources/shaders/shader.vert6
-rwxr-xr-xsrc/glwidget.cpp52
-rw-r--r--src/graphics/shape.cpp6
-rw-r--r--src/ocean/ocean_alt.cpp11
8 files changed, 172 insertions, 42 deletions
diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user
index bc65141..f9d8bd9 100644
--- a/CMakeLists.txt.user
+++ b/CMakeLists.txt.user
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
-<!-- Written by QtCreator 8.0.1, 2024-05-05T03:29:58. -->
+<!-- Written by QtCreator 8.0.1, 2024-05-07T05:26:10. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
diff --git a/resources/shaders/color.frag b/resources/shaders/color.frag
index 4a052a1..fe70983 100644
--- a/resources/shaders/color.frag
+++ b/resources/shaders/color.frag
@@ -1,8 +1,42 @@
-#version 330 core
-
-in vec3 col;
+#version 410 core
out vec4 fragColor;
+in vec3 normal_cameraSpace;
+in vec3 camera_worldSpace;
+in vec3 normal_worldSpace;
+in vec3 pos;
+in vec3 oldPosFlat;
+in float intensity;
+
+//uniform float multiplier = 1.f;
+//uniform float contrast = 10.f;
+//uniform float intExp = 0.5f;
+//uniform float scale = 1.f;
+
+uniform float multiplier = .9f;
+uniform float contrast = 20.f;
+uniform float intExp = 0.f;
+uniform float scale = 1.f;
+
+//uniform float multiplier = .5f;
+//uniform float contrast = 1.5f;
+//uniform float intExp = 0.f;
+//uniform float scale = 1.f;
+
+//uniform vec4 baseColor =vec4();
+
void main() {
- fragColor = vec4(col, 1.0);
+ fragColor = 0.5f * (vec4(normal_worldSpace[0], 0.f, normal_worldSpace[2], 1.f) + 1);
+ float oldArea = length(dFdx(oldPosFlat)) * length(dFdy(oldPosFlat));
+ float newArea = length(dFdx(pos)) * length(dFdy(pos));
+ float areaRatio = oldArea / newArea * multiplier;
+// if(oldArea / newArea > 3) {
+// areaRatio = 0.f;
+// }
+// fragColor = clamp(vec4(intensity, intensity, (1.f - intensity) * .5f, intensity), 0.f, 1.f);
+ fragColor = vec4(pow(areaRatio, contrast)) * pow(intensity, intExp);
+ float finalInt = pow(areaRatio, contrast) * pow(intensity, intExp);
+ finalInt = clamp(finalInt, 0, 1);
+ fragColor = vec4(1, 1, 1, finalInt * scale);
+// fragColor = vec4(vec3(1), pow(oldArea / newArea * .2f, 1.5f));
}
diff --git a/resources/shaders/color.vert b/resources/shaders/color.vert
index 6aa941e..677abf6 100644
--- a/resources/shaders/color.vert
+++ b/resources/shaders/color.vert
@@ -1,10 +1,92 @@
#version 330 core
-layout (location = 0) in vec3 position;
-layout (location = 1) in vec3 normal;
-out vec3 col;
+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 = -3000.f;
+uniform float skyHeight = 500.f;
+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;
+
+out vec3 normal_cameraSpace;
+out vec3 normal_worldSpace;
+out vec3 camera_worldSpace;
+out vec3 pos;
+out float intensity;
+out vec3 oldPosFlat;
+
+uniform vec2 widthBounds;
+uniform vec2 lengthBounds;
+
+vec3 moveToTopDown(vec3 p) {
+ return vec3((p[0] - ((widthBounds[0] + widthBounds[1]) / 2)) / (widthBounds[1] - widthBounds[0]) * 2,
+ -(p[2] - ((lengthBounds[0] + lengthBounds[1]) / 2)) * 2 / (lengthBounds[1] - lengthBounds[0]), 0.f);
+}
+
+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.y - d;
+ float depthScale = dist / w_t.y;
+ vec3 groundContactPoint = -(w_t * depthScale) + p;
+ return vec4(groundContactPoint, 1.f - prob_to_refl);
+ } else {
+ return vec4(0, 0, 0, 0);
+ }
+}
+
+vec3 wrapGL(vec3 p) {
+ float newX = p[0];
+ float newY = p[1];
+
+ newX = mod(newX + 1.f, 2.f) - 1.f;
+
+ newY = mod(newY + 1.f, 2.f) - 1.f;
+
+ return vec3(newX, newY, 0.f);
+}
void main() {
- gl_Position = vec4(position, 1.0);
- col = normal;
+ 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 = vec3(model * vec4(position, 1.f)); //vec3(model * vec4(objSpacePos, 1.f));
+ vec3 lightDir = -normalize(vec3(1, -1, 1));
+
+ gl_Position = proj * view * model * vec4(position, 1);
+
+// gl_Position = vec4((position[0] - ((widthBounds[0] + widthBounds[1]) / 2)) / (widthBounds[1] - widthBounds[0]) * 2,
+// (position[2] - ((lengthBounds[0] + lengthBounds[1]) / 2)) / (lengthBounds[1] - lengthBounds[0]) * 2, 0.f, 1.f);
+ float newX = mod(int(position[0]), 3) - 1;
+ float newY = mod(int(position[2]), 3) - 1;
+
+ vec4 refractedPositionAndProb = refractToFloor(-lightDir, position, normal_worldSpace, depth);
+
+ float waterMurkiness = .002f;
+
+ gl_Position = vec4(newX, newY, 0.f, 1.f);
+
+ intensity = refractedPositionAndProb[3] * clamp(dot(normal_worldSpace, lightDir), 0.f, 1.f) * exp(-length((position - vec3(refractedPositionAndProb))) * waterMurkiness);
+
+ oldPosFlat = moveToTopDown(position);
+ pos = moveToTopDown(vec3(refractedPositionAndProb));
+ gl_Position = vec4(pos, 1.f);
}
diff --git a/resources/shaders/shader.frag b/resources/shaders/shader.frag
index 6d1bc57..88c783d 100755
--- a/resources/shaders/shader.frag
+++ b/resources/shaders/shader.frag
@@ -70,7 +70,8 @@ void main() {
vec2 refrUV = uvFromWorldPoint(refrPos);
vec2 reflUV = uvFromWorldPoint(reflPos);
- float waterMurkiness = 0.002f; // TODO: Make uniform
+// float waterMurkiness = 0.002f; // TODO: Make uniform
+ float waterMurkiness = 0.0005f; // TODO: Make uniform
vec3 waterVolumeColor = vec3(red * 0.1f, green * 0.2f, blue * 0.2f);
float murkDiffuse = 0.3f;
float murkAmbient = 0.8f;
diff --git a/resources/shaders/shader.vert b/resources/shaders/shader.vert
index 1409039..3cd5c17 100755
--- a/resources/shaders/shader.vert
+++ b/resources/shaders/shader.vert
@@ -2,9 +2,9 @@
layout(location = 0) in vec3 position; // Position of the vertex
layout(location = 1) in vec3 normal; // Normal of the vertex
-layout(location = 3) in vec3 texCoords; // Normal of the vertex
+layout(location = 2) in vec3 texCoords; // Normal of the vertex
-uniform float depth = -1500.f;
+uniform float depth = -3000.f;
uniform float skyHeight = 500.f;
uniform mat4 proj;
uniform mat4 view;
@@ -26,7 +26,7 @@ out vec2 uv;
out float matIor;
vec4 getRefrPos() {
-// float depth = -1000.f; // TODO: Pass as uniform
+// float depth = -1500.f; // TODO: Pass as uniform
vec3 w_o = normalize(pos - camera_worldSpace);
float cos_theta_i = dot(-w_o, normal_worldSpace);
float n_i = 1;
diff --git a/src/glwidget.cpp b/src/glwidget.cpp
index 4c8696d..bac695c 100755
--- a/src/glwidget.cpp
+++ b/src/glwidget.cpp
@@ -68,8 +68,8 @@ void GLWidget::initializeGL()
// Enable depth-testing and backface culling
glEnable(GL_DEPTH_TEST);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
+// glEnable(GL_CULL_FACE);
+// glCullFace(GL_BACK);
// glShadeModel(GL_SMOOTH);
@@ -169,30 +169,38 @@ void GLWidget::initializeGL()
void GLWidget::paintCaustics() {
glClearColor(0.68f, 0.58f, 0.38f, 1);
+// glClearColor(0., 0., 0., 1);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
+// glBindFramebuffer(GL_FRAMEBUFFER, m_defaultFBO);
- // Task 15: Clear the screen here
-
- // TA SOLUTION
+ // Clear Screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glEnable( GL_BLEND );
- // Bind the shader
- m_colorShader->bind();
+// // Bind the shader
+// m_colorShader->bind();
- // Task 16: Bind your VAO here
+//// m_colorShader->setUniform("asdf", m_arap.minCorner[0], m_arap.maxCorner[0]);
+//// // Bind VAO
+// glBindVertexArray(m_floor_vao);
- // TA SOLUTION
- glBindVertexArray(m_floor_vao);
-
- // Task 17: Draw your VAO here
+//// // Draw the VAO
+// glDrawArrays(GL_TRIANGLES, 0, 3);
- // TA SOLUTION
- glDrawArrays(GL_TRIANGLES, 0, 3);
-
- // Task 18: Unbind your VAO here
+ m_colorShader->bind();
+ //
+ m_colorShader->setUniform("proj", m_camera.getProjection());
+ m_colorShader->setUniform("view", m_camera.getView());
+ Eigen::Matrix4f inverseView = m_camera.getView().inverse();
+ m_colorShader->setUniform("inverseView", inverseView);
+ //
+ m_colorShader->setUniform("widthBounds", m_arap.minCorner[0], m_arap.maxCorner[0]);
+ m_colorShader->setUniform("lengthBounds", m_arap.minCorner[2], m_arap.maxCorner[2]);
+ m_arap.draw(m_colorShader, GL_TRIANGLES);
- // TA SOLUTION
+ // Unbind the VAO
glBindVertexArray(0);
// Unbind the shader
@@ -309,9 +317,9 @@ void GLWidget::paintGL()
glBindFramebuffer(GL_FRAMEBUFFER, m_defaultFBO);
// return;
// paintTexture(m_ground_texture, false);
-// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-// glEnable( GL_BLEND );
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glEnable( GL_BLEND );
m_defaultShader->bind();
m_defaultShader->setUniform("proj", m_camera.getProjection());
@@ -345,6 +353,10 @@ void GLWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
m_camera.setAspect(static_cast<float>(w) / h);
+ glDeleteTextures(1, &m_fbo_texture);
+ glDeleteRenderbuffers(1, &m_fbo_renderbuffer);
+ glDeleteFramebuffers(1, &m_fbo);
+ makeFBO();
}
// ================== Event Listeners
diff --git a/src/graphics/shape.cpp b/src/graphics/shape.cpp
index 7a33140..4b1d4d4 100644
--- a/src/graphics/shape.cpp
+++ b/src/graphics/shape.cpp
@@ -138,13 +138,13 @@ void Shape::draw(Shader *shader, GLenum mode)
// Not that one texture is overwriting the other, because if we just load sky it doesn't work
// Draws whatever is bound to texture0 no matter what.
// Drawing the ground texture.
- /* // When ground is being rendered dynamically, don't use static ground image.
+/* // When ground is being rendered dynamically, don't use static ground image.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_ground_texture);
// glBindTexture(GL_TEXTURE_2D, 0);
shader->setUniform("groundSampler", 0);
- glUniform1i(glGetUniformLocation(shader->id(), "groundSampler"), 0);
- */
+ glUniform1i(glGetUniformLocation(shader->id(), "groundSampler"), 0);*/
+
// https://stackoverflow.com/questions/67277087/opengl-glsl-multiple-texture-binding-not-working
// FIGURED OUT THE PROBLEM. it was that SAMPLERS WERE DEFAULTING TO SLOT 0 AND SETUNIFORM WASN'T WORKING
diff --git a/src/ocean/ocean_alt.cpp b/src/ocean/ocean_alt.cpp
index 025e89b..ca080cd 100644
--- a/src/ocean/ocean_alt.cpp
+++ b/src/ocean/ocean_alt.cpp
@@ -43,7 +43,8 @@ void ocean_alt::init_wave_index_constants(){
m_waveIndexConstants[i] = wave_const;
// initialize m_current_h to be h0 for now
- m_current_h.push_back(h0_prime);
+// m_current_h.push_back(h0_prime);
+ m_current_h.push_back(Eigen::Vector2d(0.0, 0.0));
m_displacements.push_back(Eigen::Vector2d(0.0, 0.0));
m_slopes.push_back(Eigen::Vector2d(0.0, 0.0));
m_normals.push_back(Eigen::Vector3f(0.0, 1.0, 0.0));
@@ -269,11 +270,11 @@ std::vector<Eigen::Vector3f> ocean_alt::get_vertices()
float ys = 1.f + s[1]*s[1];
float zs = 1.f + s[2]*s[2];
-// Eigen::Vector3f diff = y - s;
-// Eigen::Vector3f norm = Eigen::Vector3f(diff[0]/ sqrt(xs), diff[1]/ sqrt(ys), diff[2]/sqrt(zs));
+ Eigen::Vector3f diff = y - s;
+ Eigen::Vector3f norm = Eigen::Vector3f(diff[0]/ sqrt(xs), diff[1]/ sqrt(ys), diff[2]/sqrt(zs));
// NEW
- Eigen::Vector3f norm = Eigen::Vector3f(-slope[0], 1.0, -slope[1]);
+// Eigen::Vector3f norm = Eigen::Vector3f(-slope[0], 1.0, -slope[1]);
norm.normalize();
//NEW
@@ -450,4 +451,4 @@ std::vector<Eigen::Vector2d> ocean_alt::fast_fft
}
return h;
-} \ No newline at end of file
+}