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 | |
parent | f1230ea24dc87a3bb5c9c709d4571e3298d7d07f (diff) |
"raytraced" caustics.
-rw-r--r-- | .DS_Store | bin | 10244 -> 10244 bytes | |||
-rw-r--r-- | CMakeLists.txt | 6 | ||||
-rw-r--r-- | resources/shaders/caustics.frag | 20 | ||||
-rw-r--r-- | resources/shaders/caustics.vert | 53 | ||||
-rwxr-xr-x | resources/shaders/shader.frag | 3 | ||||
-rw-r--r-- | src/arap.cpp | 63 | ||||
-rw-r--r-- | src/arap.h | 6 | ||||
-rwxr-xr-x | src/glwidget.cpp | 61 | ||||
-rwxr-xr-x | src/glwidget.h | 7 |
9 files changed, 211 insertions, 8 deletions
Binary files differ diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d35a8d..81eca1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ add_executable(${PROJECT_NAME} src/graphics/meshloader.cpp src/graphics/shader.cpp src/graphics/shape.cpp + src/graphics/simpleshape.cpp # src/graphics/oceanshape.cpp src/mainwindow.h @@ -47,6 +48,7 @@ add_executable(${PROJECT_NAME} src/graphics/meshloader.h src/graphics/shader.h src/graphics/shape.h + src/graphics/simpleshape.h # src/graphics/oceanshape.h util/tiny_obj_loader.h @@ -102,8 +104,8 @@ qt6_add_resources(${PROJECT_NAME} "Resources" resources/shaders/texture.frag resources/shaders/color.vert resources/shaders/color.frag - - + resources/shaders/caustics.vert + resources/shaders/caustics.frag ) # GLEW: this provides support for Windows (including 64-bit) 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; diff --git a/src/arap.cpp b/src/arap.cpp index e1ab1ca..ae7240d 100644 --- a/src/arap.cpp +++ b/src/arap.cpp @@ -51,6 +51,8 @@ void ARAP::init minCorner = coeffMin; maxCorner = coeffMax; + + initCausticsShape(10); } void ARAP::update(double seconds) @@ -98,3 +100,64 @@ void ARAP::move // - Minus and equal keys (click repeatedly) to change the size of the vertices } +void ARAP::initCausticsShape(int res) { +// std::vector<Eigen::Vector3f> gridPoints; +// float step = 2.f / ((float) res); + +// for (int i = 0; i < res; ++i) { +// for (int j = 0; j < res; ++j) { +// float x = -1.f + i * step; // calculate x coordinate +// float y = -1.f + j * step; // calculate y coordinate +// gridPoints.push_back(Eigen::Vector3f(x, y, 0.f)); // add point to grid +// } +// } +// std::vector<Eigen::Vector3f> verts; +// float step = 2.f / ((float) res); + +// for (int i = 0; i < res; ++i) { +// for (int j = 0; j < res; ++j) { +// float x = -1.f + i * step; // calculate x coordinate +// float y = -1.f + j * step; // calculate y coordinate +// Eigen::Vector3f bottomLeft = Eigen::Vector3f(x, y, 0.f); +// Eigen::Vector3f bottomRight = Eigen::Vector3f(x + step, y, 0.f); +// Eigen::Vector3f topRight = Eigen::Vector3f(x + step, y + step, 0.f); +// Eigen::Vector3f topLeft = Eigen::Vector3f(x, y + step, 0.f); +// verts.push_back(topLeft); +// verts.push_back(bottomLeft); +// verts.push_back(bottomRight); +// verts.push_back(topLeft); +// verts.push_back(bottomRight); +// verts.push_back(topRight); +// } +// } +// m_causticsShape.setVertices(verts); + std::vector<Eigen::Vector3f> verts; + std::vector<Eigen::Vector3i> faces; + float size = 2.f; + float step = size / ((float) res); + + for (int i = 0; i <= res; ++i) { + for (int j = 0; j <= res; ++j) { + float x = -(size / 2.f) + i * step; // calculate x coordinate + float y = -(size / 2.f) + j * step; // calculate y coordinate + Eigen::Vector3f bottomLeft = Eigen::Vector3f(x, y, 0.f); + verts.push_back(bottomLeft); + } + } + + for (int i = 0; i < res; ++i) { + for (int j = 0; j < res; ++j) { + int bottomLeft = i * (res + 1) + j; + int bottomRight = i * (res + 1) + j + 1; + int topRight = (i + 1) * (res + 1) + j + 1; + int topLeft = (i + 1) * (res + 1) + j; + faces.push_back(Eigen::Vector3i(topLeft, bottomLeft, bottomRight)); + faces.push_back(Eigen::Vector3i(topLeft, bottomRight, topRight)); + faces.push_back(Eigen::Vector3i(bottomRight, bottomLeft, topLeft)); + faces.push_back(Eigen::Vector3i(topRight, bottomRight, topLeft)); + } + } + m_causticsShape.init(verts, faces); + m_causticsShape.setColor(0.27f, .803f, .96f); +} + @@ -1,6 +1,7 @@ #pragma once #include "graphics/shape.h" +//#include "graphics/simpleshape.h" //#include "graphics/oceanshape.h" #include "Eigen/StdList" #include "Eigen/StdVector" @@ -20,7 +21,7 @@ class ARAP private: Shape m_shape; // OceanShape m_oceanShape; - + void initCausticsShape(int res); public: ARAP(); @@ -38,6 +39,7 @@ public: void draw(Shader *shader, GLenum mode) { +// m_causticsShape.draw(shader, mode); m_shape.draw(shader, mode); } @@ -85,5 +87,7 @@ public: double m_timestep = 0.1; Eigen::Vector3f minCorner, maxCorner; + + Shape m_causticsShape; }; diff --git a/src/glwidget.cpp b/src/glwidget.cpp index bac695c..62d8482 100755 --- a/src/glwidget.cpp +++ b/src/glwidget.cpp @@ -67,7 +67,7 @@ void GLWidget::initializeGL() glClearColor(1, 0.98f, 0.85f, 1); // Enable depth-testing and backface culling - glEnable(GL_DEPTH_TEST); +// glEnable(GL_DEPTH_TEST); // glEnable(GL_CULL_FACE); // glCullFace(GL_BACK); // glShadeModel(GL_SMOOTH); @@ -78,7 +78,7 @@ void GLWidget::initializeGL() m_pointShader = new Shader(":resources/shaders/anchorPoint.vert", ":resources/shaders/anchorPoint.geom", ":resources/shaders/anchorPoint.frag"); // m_texture_shader = new Shader(":/resources/shaders/texture.vert", ":/resources/shaders/texture.frag"); m_colorShader = new Shader(":resources/shaders/color.vert", ":resources/shaders/color.frag"); - + m_causticsShader = new Shader(":resources/shaders/caustics.vert", ":resources/shaders/caustics.frag"); initCaustics(); // INITIALIZE TEXTURE STUFF @@ -103,11 +103,12 @@ void GLWidget::initializeGL() m_devicePixelRatio = this->devicePixelRatio(); - m_defaultFBO = 2; + m_defaultFBO = 3; m_fbo_width = size().width() * m_devicePixelRatio; m_fbo_height = size().height() * m_devicePixelRatio; makeFBO(); + makeFBO1(); // FBO STUFF END @@ -171,7 +172,7 @@ 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_fbo1); // glBindFramebuffer(GL_FRAMEBUFFER, m_defaultFBO); // Clear Screen @@ -189,6 +190,7 @@ void GLWidget::paintCaustics() { //// // Draw the VAO // glDrawArrays(GL_TRIANGLES, 0, 3); + m_colorShader->bind(); // m_colorShader->setUniform("proj", m_camera.getProjection()); @@ -205,6 +207,23 @@ void GLWidget::paintCaustics() { // Unbind the shader glUseProgram(0); + + m_causticsShader->bind(); + +// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glBindFramebuffer(GL_FRAMEBUFFER, m_fbo_texture); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable( GL_BLEND ); + + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, m_fbo_texture1); + glUniform1i(glGetUniformLocation(m_causticsShader ->id(), "normSamp"), 2); + + m_arap.m_causticsShape.draw(m_causticsShader, GL_TRIANGLES); + glBindVertexArray(0); + glUseProgram(0); } void GLWidget::initCaustics() { @@ -307,7 +326,37 @@ void GLWidget::makeFBO() { // Task 22 glBindFramebuffer(GL_FRAMEBUFFER, m_defaultFBO); +} +void GLWidget::makeFBO1() { + // Task 19 + glActiveTexture(GL_TEXTURE2); + glGenTextures(1, &m_fbo_texture1); + glBindTexture(GL_TEXTURE_2D, m_fbo_texture1); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_fbo_width, m_fbo_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glBindTexture(GL_TEXTURE_2D, 0); + + // Task 20 + glGenRenderbuffers(1, &m_fbo_renderbuffer1); + glBindRenderbuffer(GL_RENDERBUFFER, m_fbo_renderbuffer1); + glRenderbufferStorage(GL_RENDERBUFFER, + GL_DEPTH24_STENCIL8, + m_fbo_width, + m_fbo_height); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + + // Task 18 + glGenFramebuffers(1, &m_fbo1); + glBindFramebuffer(GL_FRAMEBUFFER, m_fbo1); + + // Task 21 + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fbo_texture1, 0); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_fbo_renderbuffer1); + + // Task 22 + glBindFramebuffer(GL_FRAMEBUFFER, m_defaultFBO); } void GLWidget::paintGL() @@ -356,7 +405,11 @@ void GLWidget::resizeGL(int w, int h) glDeleteTextures(1, &m_fbo_texture); glDeleteRenderbuffers(1, &m_fbo_renderbuffer); glDeleteFramebuffers(1, &m_fbo); + glDeleteTextures(1, &m_fbo_texture1); + glDeleteRenderbuffers(1, &m_fbo_renderbuffer1); + glDeleteFramebuffers(1, &m_fbo1); makeFBO(); + makeFBO1(); } // ================== Event Listeners diff --git a/src/glwidget.h b/src/glwidget.h index 810829a..f77c4ee 100755 --- a/src/glwidget.h +++ b/src/glwidget.h @@ -41,6 +41,7 @@ private: void keyReleaseEvent (QKeyEvent *event) override; void makeFBO(); + void makeFBO1(); void initCaustics(); void paintCaustics(); @@ -55,6 +56,7 @@ private: Shader *m_defaultShader; Shader *m_pointShader; Shader *m_texture_shader; + Shader *m_causticsShader; Shader *m_colorShader; @@ -70,6 +72,11 @@ private: GLuint m_fbo; GLuint m_fbo_texture; GLuint m_fbo_renderbuffer; + + GLuint m_fbo1; + GLuint m_fbo_texture1; + GLuint m_fbo_renderbuffer1; + GLuint m_defaultFBO; GLuint m_floor_vbo; |