summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjjesswan <jessica_wan@brown.edu>2024-05-06 20:29:14 -0400
committerjjesswan <jessica_wan@brown.edu>2024-05-06 20:29:14 -0400
commiteb8af873097ce73a22139db4924ebd41d766f011 (patch)
tree2ba63f06e8b787b8091cf1ac8a71974191ac57bb
parentd8bf256cf18327f48061ed875c98109200288733 (diff)
foam done
-rw-r--r--resources/images/foam4.pngbin0 -> 10850158 bytes
-rw-r--r--resources/shaders/foam.frag10
-rw-r--r--resources/shaders/foam.vert8
-rwxr-xr-xsrc/glwidget.cpp8
-rw-r--r--src/graphics/shape.cpp51
-rw-r--r--src/graphics/shape.h12
-rw-r--r--src/ocean/ocean_alt.cpp11
7 files changed, 46 insertions, 54 deletions
diff --git a/resources/images/foam4.png b/resources/images/foam4.png
new file mode 100644
index 0000000..0b91836
--- /dev/null
+++ b/resources/images/foam4.png
Binary files differ
diff --git a/resources/shaders/foam.frag b/resources/shaders/foam.frag
index 8f5df85..af0cf4c 100644
--- a/resources/shaders/foam.frag
+++ b/resources/shaders/foam.frag
@@ -32,7 +32,7 @@ float getSaturation(vec2 k, vec2 xzPos, float adjWaveLength, float phaseC){
void main() {
float height = pos.y;
- float saturation = constants[0];//getSaturation(dir, vec2(pos.x, pos.z), 512.f, constants[0]);
+ float saturation = constants[0];//getSaturation(dir, vec2(pos.x, pos.z), 200.f, constants[0]);
vec4 m_uv = texture(halftone_texture, tex);
float m_threshold = m_uv.r * m_uv.g * m_uv.b;
@@ -44,10 +44,10 @@ void main() {
vec4 g = clamp(.5*saturation - m_threshold, 0, 1) * h;
// apply foam texture
- vec4 foam = texture(foam_texture, tex*.3);
- vec4 j = vec4(22, 33, 54, 100)/255;
- if (saturation > m_threshold) j = vec4(vec3(g*foam), 1);
+ vec4 foam = texture(foam_texture, tex*3);
+ vec4 j = vec4(0,0,0,0);
+ if (saturation > m_threshold) j = vec4(g*foam);
- fragColor = vec4(vec3(saturation), 1);
+ fragColor = vec4(vec3(j), saturation);
}
diff --git a/resources/shaders/foam.vert b/resources/shaders/foam.vert
index a33e343..f27c589 100644
--- a/resources/shaders/foam.vert
+++ b/resources/shaders/foam.vert
@@ -1,7 +1,9 @@
#version 330 core
layout(location = 0) in vec3 position; // Position of the vertex
-layout(location = 1) in float wavelength; // wavelenth adjusted for ocean depth
+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 vec2 direction; // wave slope
//layout(location = 3) in vec2 texCoords; // texture coords
//layout(location = 3) in vec3 norm; // texture coords
@@ -43,8 +45,8 @@ vec2 calculateTexCoord(vec3 pos){
}
void main() {
- dir = vec2(0,0);
- constants = vec2(wavelength, phaseC);
+ dir = vec2(wavedirs[0],wavedirs[1]);
+ constants = vec2(wavelength[0], phaseC);
gl_Position = proj * view * model * vec4(position, 1);
pos = vec3(gl_Position);
diff --git a/src/glwidget.cpp b/src/glwidget.cpp
index 5a2d87d..230eb3c 100755
--- a/src/glwidget.cpp
+++ b/src/glwidget.cpp
@@ -86,7 +86,7 @@ void GLWidget::initializeGL()
m_foamShader = new Shader(":resources/shaders/foam.vert", ":resources/shaders/foam.frag");
m_halftone_tex = loadTextureFromFile("/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/halftone.png").textureID;
- m_foam_tex = loadTextureFromFile("/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/foam3.png").textureID;
+ m_foam_tex = loadTextureFromFile("/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/foam4.png").textureID;
@@ -316,9 +316,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());
diff --git a/src/graphics/shape.cpp b/src/graphics/shape.cpp
index 5e4a69d..e0afb83 100644
--- a/src/graphics/shape.cpp
+++ b/src/graphics/shape.cpp
@@ -125,33 +125,20 @@ 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){
-
-// vector<Vector3f> verts;
-// vector<Vector3f> norms;
-
-// vector<Vector2f> tex;
-// vector<Vector3f> colors;
m_vertices.clear();
copy(vertices.begin(), vertices.end(), back_inserter(m_vertices));
- std::vector<Eigen::Vector3f> verts;
- std::vector<Eigen::Vector2f> ks;
- std::vector<float> waves;
-
- updateMeshFoam(m_faces, vertices, waveDirs, wavelengths, verts, ks, waves);
-
- //updateFoam(m_faces, vertices, textureCoords, verts, tex, colors);
-
+ vector<Vector3f> verts;
+ vector<Vector3f> normals;
+ vector<Vector3f> colors;
+ updateMeshFoam(m_faces, vertices, wavelengths, waveDirs, verts, normals, colors);
glBindBuffer(GL_ARRAY_BUFFER, m_surfaceVbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(float) * ((verts.size() * 3) + (waves.size() * 1)), nullptr, GL_DYNAMIC_DRAW);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(float) * ((verts.size() * 3) + (normals.size() * 3) + (colors.size() * 3)), nullptr, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * verts.size() * 3, static_cast<const void *>(verts.data()));
- glBufferSubData(GL_ARRAY_BUFFER, sizeof(float) * verts.size() * 3, sizeof(float) * waves.size() * 1, static_cast<const void *>(waves.data()));
- //glBufferSubData(GL_ARRAY_BUFFER, sizeof(float) * ((verts.size() * 3) + (waves.size() * 1)), sizeof(float) * ks.size() * 2, static_cast<const void *>(ks.data()));
- //glBufferSubData(GL_ARRAY_BUFFER, sizeof(float) * ((verts.size() * 3) + (wavelengths.size() * 1) + (waveDirs.size() * 2)), sizeof(float) * tex.size() * 2, static_cast<const void *>(tex.data()));
- // glBufferSubData(GL_ARRAY_BUFFER, sizeof(float) * ((verts.size() * 3) + (wavelengths.size() * 1) + (waveDirs.size() * 2) + (tex.size() * 2)), sizeof(float) * (norms.size() * 3), static_cast<const void *>(norms.data()));
-
+ glBufferSubData(GL_ARRAY_BUFFER, sizeof(float) * verts.size() * 3, sizeof(float) * normals.size() * 3, static_cast<const void *>(normals.data()));
+ glBufferSubData(GL_ARRAY_BUFFER, sizeof(float) * ((verts.size() * 3) + (normals.size() * 3)), sizeof(float) * colors.size() * 3, static_cast<const void *>(colors.data()));
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
@@ -349,7 +336,7 @@ void Shape::updateMesh(const std::vector<Eigen::Vector3i> &faces,
Vector3f n = getNormal(face);
for (auto& v: {face[0], face[1], face[2]}) {
- normals.push_back(n);
+ normals.push_back(Eigen::Vector3f(1,1,1));
verts.push_back(vertices[v]);
if (m_anchors.find(v) == m_anchors.end()) {
@@ -362,26 +349,24 @@ 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<Eigen::Vector2f> &k_vectors,
- const std::vector<float> &wavelengths,
-
- std::vector<Eigen::Vector3f> &verts,
- std::vector<Eigen::Vector2f> &ks,
- std::vector<float> &waves)
+ const std::vector<Eigen::Vector3f> &vertices,
+ const std::vector<float> &wavelengths,
+ const vector<Vector2f> &waveDirs,
+ std::vector<Eigen::Vector3f>& verts,
+ std::vector<Eigen::Vector3f>& normals,
+ std::vector<Eigen::Vector3f>& colors)
{
verts.reserve(faces.size() * 3);
- ks.reserve(faces.size() * 3);
- waves.reserve(faces.size() * 3);
+ normals.reserve(faces.size() * 3);
+ colors.reserve(faces.size() * 3);
for (const Eigen::Vector3i& face : faces) {
for (auto& v: {face[0], face[1], face[2]}) {
- ks.push_back(k_vectors[v]);
+ normals.push_back(Eigen::Vector3f(wavelengths[v],0,0));
verts.push_back(vertices[v]);
- waves.push_back(wavelengths[v]);
-
+ colors.push_back(Eigen::Vector3f(waveDirs[v][0], waveDirs[v][1], 0));
}
}
}
diff --git a/src/graphics/shape.h b/src/graphics/shape.h
index 71c7bc4..ab3c27a 100644
--- a/src/graphics/shape.h
+++ b/src/graphics/shape.h
@@ -42,13 +42,13 @@ public:
std::vector<Eigen::Vector3f>& colors);
void updateMeshFoam(const std::vector<Eigen::Vector3i> &faces,
- const std::vector<Eigen::Vector3f> &vertices,
- const std::vector<Eigen::Vector2f> &k_vectors,
- const std::vector<float> &wavelengths,
+ const std::vector<Eigen::Vector3f> &vertices,
+ const std::vector<float> &wavelengths,
+ const std::vector<Eigen::Vector2f> &waveDirs,
- std::vector<Eigen::Vector3f> &verts,
- std::vector<Eigen::Vector2f> &ks,
- std::vector<float> &waves);
+ std::vector<Eigen::Vector3f>& verts,
+ std::vector<Eigen::Vector3f>& normals,
+ std::vector<Eigen::Vector3f>& colors);
diff --git a/src/ocean/ocean_alt.cpp b/src/ocean/ocean_alt.cpp
index feed479..5dcfd74 100644
--- a/src/ocean/ocean_alt.cpp
+++ b/src/ocean/ocean_alt.cpp
@@ -280,7 +280,7 @@ Eigen::Vector2d ocean_alt::complex_exp(double exponent){
std::vector<Eigen::Vector3f> ocean_alt::get_vertices()
{
std::vector<Eigen::Vector3f> vertices = std::vector<Eigen::Vector3f>();
- if (iterations < 4){
+ if (iterations < 10){
for (int i = 0; i < N; i++){
Eigen::Vector2d amplitude = m_current_h[i];
float height = amplitude[0];
@@ -331,8 +331,13 @@ std::vector<Eigen::Vector3f> ocean_alt::get_vertices()
Eigen::Vector2i m_n = index_1d_to_2d(i);
- m_foam_constants.wavelengths[i] = 1.f;//(height - 0) / (max -0);//M_PI * m_slopes[i].dot(m_slopes[i]) / ((float) m_n[0] / Lx);
- //std::cout << m_foam_constants.wavelengths[i] << std::endl;
+ // m_foam_constants.wavelengths[i] = 2.f* M_PI * m_slopes[i].dot(m_slopes[i]) / Lx;
+ m_foam_constants.wavelengths[i] = ((height -200 ) / (1000.f -200 ));
+
+// if (i < 5){
+// //std::cout << min << ", " << max << std::endl;
+// std::cout << m_foam_constants.wavelengths[i] << std::endl;
+// }