diff options
author | sotech117 <michael_foiani@brown.edu> | 2024-05-10 16:18:23 -0400 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2024-05-10 16:18:23 -0400 |
commit | 05038becbe8907a1364db05c42fc1bc48f66ce56 (patch) | |
tree | ae4908fbb29fadb44e549af52a09d368387477e6 | |
parent | 2820f2a620340feff973dfea26c1eb1172140ada (diff) |
-rw-r--r-- | resources/shaders/foam.frag | 18 | ||||
-rw-r--r-- | resources/shaders/foam.vert | 8 | ||||
-rw-r--r-- | src/arap.cpp | 6 | ||||
-rw-r--r-- | src/graphics/shape.cpp | 12 | ||||
-rw-r--r-- | src/graphics/shape.h | 4 | ||||
-rw-r--r-- | src/ocean/ocean_alt.cpp | 2 | ||||
-rw-r--r-- | src/ocean/ocean_alt.h | 4 |
7 files changed, 38 insertions, 16 deletions
diff --git a/resources/shaders/foam.frag b/resources/shaders/foam.frag index d6d1cab..53248ac 100644 --- a/resources/shaders/foam.frag +++ b/resources/shaders/foam.frag @@ -4,6 +4,8 @@ in vec2 constants; in vec2 dir; in vec2 tex; in vec3 pos; +in vec3 norm; +in vec3 camera_worldSpace; uniform float time; @@ -15,6 +17,9 @@ uniform sampler2D foam_texture; uniform vec2 widthBounds; uniform vec2 lengthBounds; +uniform vec4 sunColor = vec4(1.5f, .7, .39f, 1.f); +uniform vec3 lightDir = -normalize(vec3(1, 0, -1)); + out vec4 fragColor; float getSaturation(vec2 k, vec2 xzPos, float adjWaveLength, float phaseC){ @@ -45,8 +50,19 @@ void main() { // apply foam texture vec4 foam = texture(foam_texture, tex + time*.0003); + + vec4 lightFromSun = vec4(1.f, 1.f, 1.f, 1.f); + vec4 foamAmbient = vec4(.5f, .5f, .5f, 1.f); + + vec3 reflectedLight = lightDir - 2 * dot(lightDir, norm) * norm; + vec3 posToCam = normalize(camera_worldSpace - pos); + float spec = pow(clamp(dot(posToCam, reflectedLight), 0, 1), 2.f); + + float diffuseComp = dot(normalize(norm), lightDir); + lightFromSun = vec4(vec3(diffuseComp * sunColor), 1.f); + vec4 j = vec4(0,0,0,0); - if (saturation > m_threshold) j = g*foam; + if (saturation > m_threshold) j = g * foam * (lightFromSun + foamAmbient + spec * .6f); fragColor = j; diff --git a/resources/shaders/foam.vert b/resources/shaders/foam.vert index f27c589..4d7f075 100644 --- a/resources/shaders/foam.vert +++ b/resources/shaders/foam.vert @@ -2,7 +2,7 @@ layout(location = 0) in vec3 position; // Position of the vertex layout(location = 1) in vec3 wavelength; // wavelenth adjusted for ocean depth -layout(location = 2) in vec3 wavedirs; // wavelenth adjusted for ocean depth +layout(location = 2) in vec3 normals; // normals //layout(location = 2) in vec2 direction; // wave slope //layout(location = 3) in vec2 texCoords; // texture coords @@ -13,6 +13,8 @@ out vec2 constants; out vec2 dir; out vec2 tex; out vec3 pos; +out vec3 norm; +out vec3 camera_worldSpace; @@ -45,11 +47,13 @@ vec2 calculateTexCoord(vec3 pos){ } void main() { - dir = vec2(wavedirs[0],wavedirs[1]); +// dir = vec2(wavedirs[0],wavedirs[1]); constants = vec2(wavelength[0], phaseC); gl_Position = proj * view * model * vec4(position, 1); pos = vec3(gl_Position); + norm = normalize(normals); + camera_worldSpace = vec3(inverseView * vec4(0.f, 0.f, 0.f, 1.f)); tex = calculateTexCoord(position); diff --git a/src/arap.cpp b/src/arap.cpp index d1af7b5..7db5076 100644 --- a/src/arap.cpp +++ b/src/arap.cpp @@ -84,7 +84,9 @@ void ARAP::update(double seconds) // the last update m_ocean.fft_prime(m_time); m_ocean.update_ocean(); - m_shape.setVertices_and_Normals(m_ocean.get_vertices(), m_ocean.getNormals()); + std::vector<Eigen::Vector3f> normals = m_ocean.getNormals(); + + m_shape.setVertices_and_Normals(m_ocean.get_vertices(), normals ); // m_shape.setVertices(m_ocean.get_vertices()); // auto tmp = m_ocean.get_vertices(); @@ -101,7 +103,7 @@ void ARAP::update(double seconds) FoamConstants foam = m_ocean.getFoamConstants(); std::vector<float> tiled_wavelengths = m_ocean.get_tiled_wavelengths(); std::vector<Eigen::Vector2f> tiled_k_vectors = m_ocean.get_tiled_k_vectors(); - m_foam_shape.setFoamInputs(m_ocean.get_vertices(), tiled_wavelengths, tiled_k_vectors, foam.texCoords); + m_foam_shape.setFoamInputs(m_ocean.get_vertices(), tiled_wavelengths, normals, foam.texCoords); m_time += m_timestep; diff --git a/src/graphics/shape.cpp b/src/graphics/shape.cpp index abe4f7b..b7cee1b 100644 --- a/src/graphics/shape.cpp +++ b/src/graphics/shape.cpp @@ -123,16 +123,16 @@ void Shape::setVertices_and_Normals(const vector<Vector3f> &vertices, const vect /// void Shape::setFoamInputs(const vector<Vector3f> &vertices, const vector<float> &wavelengths, - const vector<Vector2f> &waveDirs, const vector<Vector2f> &textures){ + const vector<Vector3f> &norms, const vector<Vector2f> &textures){ m_vertices.clear(); copy(vertices.begin(), vertices.end(), back_inserter(m_vertices)); vector<Vector3f> verts; - vector<Vector3f> normals; - vector<Vector3f> colors; + vector<Vector3f> normals; // this is actually wavelengths + vector<Vector3f> colors; // this is actually norms - updateMeshFoam(m_faces, vertices, wavelengths, waveDirs, verts, normals, colors); + updateMeshFoam(m_faces, vertices, wavelengths, norms, verts, normals, colors); glBindBuffer(GL_ARRAY_BUFFER, m_surfaceVbo); glBufferData(GL_ARRAY_BUFFER, sizeof(float) * ((verts.size() * 3) + (normals.size() * 3) + (colors.size() * 3)), nullptr, GL_DYNAMIC_DRAW); @@ -351,7 +351,7 @@ void Shape::updateMesh(const std::vector<Eigen::Vector3i> &faces, void Shape::updateMeshFoam(const std::vector<Eigen::Vector3i> &faces, const std::vector<Eigen::Vector3f> &vertices, const std::vector<float> &wavelengths, - const vector<Vector2f> &waveDirs, + const vector<Vector3f> &norms, std::vector<Eigen::Vector3f>& verts, std::vector<Eigen::Vector3f>& normals, std::vector<Eigen::Vector3f>& colors) @@ -363,7 +363,7 @@ void Shape::updateMeshFoam(const std::vector<Eigen::Vector3i> &faces, for (auto& v: {face[0], face[1], face[2]}) { normals.push_back(Eigen::Vector3f(wavelengths[v],0,0)); verts.push_back(vertices[v]); - colors.push_back(Eigen::Vector3f(waveDirs[v][0], waveDirs[v][1], 0)); + colors.push_back(Eigen::Vector3f(norms[v])); } } } diff --git a/src/graphics/shape.h b/src/graphics/shape.h index ab3c27a..88fc33e 100644 --- a/src/graphics/shape.h +++ b/src/graphics/shape.h @@ -32,7 +32,7 @@ public: void setVertices(const std::vector<Eigen::Vector3f> &vertices); void setVertices_and_Normals(const std::vector<Eigen::Vector3f> &vertices, const std::vector<Eigen::Vector3f> &normals); void setFoamInputs(const std::vector<Eigen::Vector3f> &verts, const std::vector<float> &wavelengths, - const std::vector<Eigen::Vector2f> &waveDirs, const std::vector<Eigen::Vector2f> &textureCoords); + const std::vector<Eigen::Vector3f> &norms, const std::vector<Eigen::Vector2f> &textureCoords); void updateFoam(const std::vector<Eigen::Vector3i> &faces, const std::vector<Eigen::Vector3f> &vertices, const std::vector<Eigen::Vector2f> &texCoords, @@ -44,7 +44,7 @@ public: void updateMeshFoam(const std::vector<Eigen::Vector3i> &faces, const std::vector<Eigen::Vector3f> &vertices, const std::vector<float> &wavelengths, - const std::vector<Eigen::Vector2f> &waveDirs, + const std::vector<Eigen::Vector3f> &waveDirs, std::vector<Eigen::Vector3f>& verts, std::vector<Eigen::Vector3f>& normals, diff --git a/src/ocean/ocean_alt.cpp b/src/ocean/ocean_alt.cpp index 0660a14..e0809a3 100644 --- a/src/ocean/ocean_alt.cpp +++ b/src/ocean/ocean_alt.cpp @@ -569,7 +569,7 @@ std::vector<Eigen::Vector2d> ifft_1d // calculate the twiddle factor int index = group * pow(2, stage) + butterfly; - float w = index * 2 * M_PI / pow(2, stage); + float w = -index * 2 * M_PI / pow(2, stage); Eigen::Vector2d twiddle_factor = {cos(w), sin(w)}; if (reading_buffer1) diff --git a/src/ocean/ocean_alt.h b/src/ocean/ocean_alt.h index 5df1eb5..88344d6 100644 --- a/src/ocean/ocean_alt.h +++ b/src/ocean/ocean_alt.h @@ -85,10 +85,10 @@ private: const double vertex_displacement = Lx / 2; const int N = num_rows*num_cols; // total number of grid points - const double lambda =0.0; // how much displacement matters + const double lambda = .5; // how much displacement matters const double spacing = 1.0; // spacing between grid points - const double A = 6.5; // numeric constant for the Phillips spectrum + const double A = 10; // numeric constant for the Phillips spectrum const double V = 500; // wind speed const double gravity = 9.81; const double L = V*V/gravity; |