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. --- .DS_Store | Bin 6148 -> 10244 bytes CMakeLists.txt | 4 ++++ CMakeLists.txt.user | 3 ++- resources/images/hello.png | Bin 0 -> 461146 bytes resources/images/image001.jpg | Bin 0 -> 4627 bytes resources/images/kitten.png | Bin 0 -> 1603868 bytes resources/images/uv1 copy.png | Bin 0 -> 48020 bytes resources/images/uv1.png | Bin 0 -> 86064 bytes resources/images/uvmap.jpg | Bin 0 -> 281828 bytes resources/shaders/shader.frag | 29 +++++++++++++++++++++++++-- resources/shaders/shader.vert | 45 +++++++++++++++++++++++++++++++++++++++++- src/arap.cpp | 40 +++++++++++++++++++++++++++++++++++++ src/arap.h | 8 ++++++++ src/glwidget.cpp | 7 +++++++ src/graphics/shader.cpp | 8 ++++++++ src/graphics/shader.h | 2 ++ src/graphics/shape.cpp | 44 +++++++++++++++++++++++++++++++++++++++++ src/graphics/shape.h | 6 ++++++ src/main.cpp | 2 +- src/ocean/ocean.h | 9 +++++++-- 20 files changed, 200 insertions(+), 7 deletions(-) create mode 100644 resources/images/hello.png create mode 100644 resources/images/image001.jpg create mode 100644 resources/images/kitten.png create mode 100644 resources/images/uv1 copy.png create mode 100644 resources/images/uv1.png create mode 100644 resources/images/uvmap.jpg diff --git a/.DS_Store b/.DS_Store index afabf6e..8e365cf 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/CMakeLists.txt b/CMakeLists.txt index 5872179..468fb1a 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/oceanshape.cpp src/mainwindow.h src/arap.h @@ -46,6 +47,7 @@ add_executable(${PROJECT_NAME} src/graphics/meshloader.h src/graphics/shader.h src/graphics/shape.h + src/graphics/oceanshape.h util/tiny_obj_loader.h util/unsupportedeigenthing/OpenGLSupport @@ -80,6 +82,8 @@ qt6_add_resources(${PROJECT_NAME} "Resources" PREFIX "/" FILES + resources/images/uv1.png + resources/images/kitten.png resources/shaders/shader.frag resources/shaders/shader.vert resources/shaders/anchorPoint.vert diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user index b46887f..5b63c10 100644 --- a/CMakeLists.txt.user +++ b/CMakeLists.txt.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -357,6 +357,7 @@ true false true + /Users/sebastianpark/College/2023-24/CSCI_2240/ocean-simulation/ /Users/sebastianpark/College/2023-24/CSCI_2240/ocean-simulation/build/build-ocean-simulation-Qt_6_2_4_for_macOS-Release 1 diff --git a/resources/images/hello.png b/resources/images/hello.png new file mode 100644 index 0000000..29df26f Binary files /dev/null and b/resources/images/hello.png differ diff --git a/resources/images/image001.jpg b/resources/images/image001.jpg new file mode 100644 index 0000000..9faa90d Binary files /dev/null and b/resources/images/image001.jpg differ diff --git a/resources/images/kitten.png b/resources/images/kitten.png new file mode 100644 index 0000000..3c92001 Binary files /dev/null and b/resources/images/kitten.png differ diff --git a/resources/images/uv1 copy.png b/resources/images/uv1 copy.png new file mode 100644 index 0000000..fcf4825 Binary files /dev/null and b/resources/images/uv1 copy.png differ diff --git a/resources/images/uv1.png b/resources/images/uv1.png new file mode 100644 index 0000000..82ff8ac Binary files /dev/null and b/resources/images/uv1.png differ diff --git a/resources/images/uvmap.jpg b/resources/images/uvmap.jpg new file mode 100644 index 0000000..dae795f Binary files /dev/null and b/resources/images/uvmap.jpg differ 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); } diff --git a/src/arap.cpp b/src/arap.cpp index 79fe475..94f2e3d 100644 --- a/src/arap.cpp +++ b/src/arap.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "ocean/ocean.h" @@ -47,6 +48,45 @@ void ARAP::init } coeffMin = all_vertices.colwise().minCoeff(); coeffMax = all_vertices.colwise().maxCoeff(); + + minCorner = coeffMin; + maxCorner = coeffMax; + + +// m_shape.initGroundPlane("cornell_box_full_lighting.png") +// QImage ocean_floor_image; +// GLuint ocean_floor_texture; +// // Prepare filepath +// QString ocean_floor_filepath = QString(":/resources/images/kitten.png"); + +// // TASK 1: Obtain image from filepath +// ocean_floor_image = QImage(ocean_floor_filepath); + +// // TASK 2: Format image to fit OpenGL +// ocean_floor_image = ocean_floor_image.convertToFormat(QImage::Format_RGBA8888).mirrored(); + +// // TASK 3: Generate kitten texture +// glGenTextures(1, &ocean_floor_texture); + +// // TASK 9: Set the active texture slot to texture slot 0 +// glActiveTexture(GL_TEXTURE0); + +// // TASK 4: Bind kitten texture +// glBindTexture(GL_TEXTURE_2D, ocean_floor_texture); + +// // TASK 5: Load image into kitten texture +// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ocean_floor_image.width(), ocean_floor_image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, ocean_floor_image.bits()); + +// // TASK 6: Set min and mag filters' interpolation mode to linear +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + +// // TASK 7: Unbind kitten texture +// glBindTexture(GL_TEXTURE_2D, 0); + +// // TASK 10: set the texture.frag uniform for our texture +// glUseProgram(m_texture_shader); +// glUniform1i(glGetUniformLocation(m_texture_shader, "sampler"), 0); } void ARAP::update(double seconds) diff --git a/src/arap.h b/src/arap.h index ca8967c..c4a24d7 100644 --- a/src/arap.h +++ b/src/arap.h @@ -1,6 +1,7 @@ #pragma once #include "graphics/shape.h" +#include "graphics/oceanshape.h" #include "Eigen/StdList" #include "Eigen/StdVector" #include "ocean/ocean.h" @@ -17,6 +18,7 @@ class ARAP { private: Shape m_shape; + OceanShape m_oceanShape; public: ARAP(); @@ -38,6 +40,10 @@ public: m_shape.draw(shader, mode); } + void initGroundPlane(std::string texturePath, float depth, Shader* shader) { + m_shape.initGroundPlane(texturePath, depth, shader); + } + SelectMode select(Shader *shader, int vertex) { return m_shape.select(shader, vertex); @@ -72,5 +78,7 @@ public: ocean m_ocean; double m_time = 0.00; double m_timestep = 0.001; + + Eigen::Vector3f minCorner, maxCorner; }; diff --git a/src/glwidget.cpp b/src/glwidget.cpp index 2801251..263b7dd 100755 --- a/src/glwidget.cpp +++ b/src/glwidget.cpp @@ -107,6 +107,8 @@ void GLWidget::initializeGL() m_deltaTimeProvider.start(); m_intervalTimer.start(1000 / 60); + + m_arap.initGroundPlane(":resources/images/kitty.png", 2, m_defaultShader); } void GLWidget::paintGL() @@ -120,9 +122,14 @@ void GLWidget::paintGL() m_defaultShader->setUniform("view", m_camera.getView()); Eigen::Matrix4f inverseView = m_camera.getView().inverse(); m_defaultShader->setUniform("inverseView", inverseView); + m_defaultShader->setUniform("widthBounds", m_arap.minCorner[0], m_arap.maxCorner[0]); + m_defaultShader->setUniform("lengthBounds", m_arap.minCorner[2], m_arap.maxCorner[2]); +// m_defaultShader->setUniform(""); m_arap.draw(m_defaultShader, GL_TRIANGLES); m_defaultShader->unbind(); + glBindTexture(GL_TEXTURE_2D, 0); + glClear(GL_DEPTH_BUFFER_BIT); // m_pointShader->bind(); diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 6ac9949..161eeae 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -83,6 +83,14 @@ void Shader::setUniform(const std::string &name, bool b) { glUniform1i(m_uniforms[name], static_cast(b)); } +void Shader::setUniform(const std::string &name, Eigen::Vector3f v) { + glUniform3f(m_uniforms[name], static_cast(v[0]), static_cast(v[1]), static_cast(v[2])); +} + +void Shader::setUniform(const std::string &name, float a, float b) { + glUniform2f(m_uniforms[name], static_cast(a), static_cast(b)); +} + void Shader::setUniformArrayByIndex(const std::string &name, float f, size_t index) { glUniform1f(m_uniformArrays[std::make_tuple(name, index)], f); } diff --git a/src/graphics/shader.h b/src/graphics/shader.h index bc3c0c1..7e9dad4 100644 --- a/src/graphics/shader.h +++ b/src/graphics/shader.h @@ -32,6 +32,8 @@ public: void setUniform(const std::string &name, float f); void setUniform(const std::string &name, int i); void setUniform(const std::string &name, bool b); + void setUniform(const std::string &name, Eigen::Vector3f v); + void setUniform(const std::string &name, float a, float b); void setUniformArrayByIndex(const std::string &name, float f, size_t index); void setUniformArrayByIndex(const std::string &name, const Eigen::Vector2f &vec2, size_t index); diff --git a/src/graphics/shape.cpp b/src/graphics/shape.cpp index 9459306..c5dde0d 100644 --- a/src/graphics/shape.cpp +++ b/src/graphics/shape.cpp @@ -1,6 +1,7 @@ #include "shape.h" #include +//#include #include "graphics/shader.h" using namespace Eigen; @@ -126,8 +127,12 @@ void Shape::draw(Shader *shader, GLenum mode) shader->setUniform("blue", m_blue); shader->setUniform("alpha", m_alpha); glBindVertexArray(m_surfaceVao); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, ocean_floor_texture); + shader->setUniform("sampler", 1); glDrawElements(mode, m_numSurfaceVertices, GL_UNSIGNED_INT, reinterpret_cast(0)); glBindVertexArray(0); + glBindTexture(GL_TEXTURE_2D, 0); break; } case GL_POINTS: @@ -276,3 +281,42 @@ void Shape::updateMesh(const std::vector &faces, } } +void Shape::initGroundPlane(std::string texturePath, float depth, Shader* shader) { + + // Prepare filepath + QString ocean_floor_filepath = QString(texturePath.c_str()); + + // TASK 1: Obtain image from filepath + this->ocean_floor_image = QImage(ocean_floor_filepath); + + // TASK 2: Format image to fit OpenGL + ocean_floor_image = ocean_floor_image.convertToFormat(QImage::Format_RGBA8888).mirrored(); + auto bits = this->ocean_floor_image.bits(); + auto dat = ocean_floor_image.data_ptr(); + + // TASK 3: Generate texture + glGenTextures(1, &ocean_floor_texture); + + // TASK 9: Set the active texture slot to texture slot 0 + glActiveTexture(GL_TEXTURE0); + + // TASK 4: Bind kitten texture + glBindTexture(GL_TEXTURE_2D, ocean_floor_texture); + + // TASK 5: Load image into kitten texture + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ocean_floor_image.width(), ocean_floor_image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, ocean_floor_image.bits()); + + // TASK 6: Set min and mag filters' interpolation mode to linear + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + // TASK 7: Unbind kitten texture + glBindTexture(GL_TEXTURE_2D, 0); + + // TASK 10: set the texture.frag uniform for our texture + glUseProgram(shader->id()); +// glUniform1i(glGetUniformLocation(shader->id(), "sampler"), 0); + shader->setUniform("sampler", 0); + glUseProgram(0); +} + diff --git a/src/graphics/shape.h b/src/graphics/shape.h index 1451c85..b909606 100644 --- a/src/graphics/shape.h +++ b/src/graphics/shape.h @@ -3,6 +3,8 @@ #include #include #include +#include +#include #define EIGEN_DONT_VECTORIZE #define EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT @@ -33,6 +35,8 @@ public: void setColor(float r, float g, float b); + void initGroundPlane(std::string texturePath, float depth, Shader* shader); + void draw(Shader *shader, GLenum mode); SelectMode select(Shader *shader, int vertex); bool selectWithSpecifiedMode(Shader *shader, int vertex, SelectMode mode); @@ -47,6 +51,8 @@ private: GLuint m_surfaceVao; GLuint m_surfaceVbo; GLuint m_surfaceIbo; + GLuint ocean_floor_texture; + QImage ocean_floor_image; unsigned int m_numSurfaceVertices; unsigned int m_verticesSize; diff --git a/src/main.cpp b/src/main.cpp index a11e7d8..ae74af6 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) // Create a Qt application QApplication a(argc, argv); - QCoreApplication::setApplicationName("ARAP"); + QCoreApplication::setApplicationName("OCEAN"); QCoreApplication::setOrganizationName("CS 2240"); QCoreApplication::setApplicationVersion(QT_VERSION_STR); diff --git a/src/ocean/ocean.h b/src/ocean/ocean.h index 2387d2a..0d2d2c0 100644 --- a/src/ocean/ocean.h +++ b/src/ocean/ocean.h @@ -17,7 +17,12 @@ public: void updateVertexAmplitudes(double t); std::vector get_vertices(); std::vector get_faces(); - + int getLength() { + return this->length; + } + int getWidth() { + return this->width; + } @@ -27,7 +32,7 @@ private: const int width = 81; // width of grid const int N = length * width; // total number of grid points - const double A = 10.0; // numeric constant for the Phillips spectrum + const double A = 50.0; // numeric constant for the Phillips spectrum const double V = .25; // wind speed const std::pair omega_wind = std::make_pair(1.0, 0.0); // wind direction -- cgit v1.2.3-70-g09d2