diff options
author | jjesswan <jessica_wan@brown.edu> | 2024-04-09 22:09:55 -0400 |
---|---|---|
committer | jjesswan <jessica_wan@brown.edu> | 2024-04-09 22:09:55 -0400 |
commit | 33da3ed6ca5d92e9a29935150b01b62daef75a04 (patch) | |
tree | c6c3ecf092310b242d5df5245c43b347ebba242e /src | |
parent | 7a8d0d8bc2572707c9d35006f30ea835c86954b0 (diff) |
add update loop
Diffstat (limited to 'src')
-rw-r--r-- | src/arap.cpp | 26 | ||||
-rw-r--r-- | src/arap.h | 7 | ||||
-rwxr-xr-x | src/glwidget.cpp | 3 | ||||
-rw-r--r-- | src/ocean/ocean.cpp | 36 | ||||
-rw-r--r-- | src/ocean/ocean.h | 18 |
5 files changed, 69 insertions, 21 deletions
diff --git a/src/arap.cpp b/src/arap.cpp index 714a7db..3ebdf64 100644 --- a/src/arap.cpp +++ b/src/arap.cpp @@ -34,9 +34,9 @@ void ARAP::init // m_shape.init(vertices, triangles); // } - ocean o = ocean(); - vertices = o.get_vertices(); - triangles = o.get_faces(); + + vertices = m_ocean.get_vertices(); + triangles = m_ocean.get_faces(); m_shape.init(vertices, triangles); // Students, please don't touch this code: get min and max for viewport stuff @@ -49,7 +49,26 @@ void ARAP::init coeffMax = all_vertices.colwise().maxCoeff(); } +void ARAP::update(double seconds) +{ + // STUDENTS: This method should contain all the time-stepping logic for your simulation. + // Specifically, the code you write here should compute new, updated vertex positions for your + // simulation mesh, and it should then call m_shape.setVertices to update the display with those + // newly-updated vertices. + + // STUDENTS: As currently written, the program will just continually compute simulation timesteps as long + // as the program is running (see View::tick in view.cpp) . You might want to e.g. add a hotkey for pausing + // the simulation, and perhaps start the simulation out in a paused state. + // Note that the "seconds" parameter represents the amount of time that has passed since + // the last update + + m_ocean.updateVertexAmplitudes(m_time); + m_shape.setVertices(m_ocean.get_vertices()); + + m_time += m_timestep; + std::cout << m_time << std::endl; +} // Move an anchored vertex, defined by its index, to targetPosition void ARAP::move @@ -58,6 +77,7 @@ void ARAP::move Vector3f targetPosition ) { + std::cout << "moving vertex: " << vertex << std::endl; // Here are some helpful controls for the application // // - You start in first-person camera mode @@ -3,6 +3,7 @@ #include "graphics/shape.h" #include "Eigen/StdList" #include "Eigen/StdVector" +#include "ocean/ocean.h" #include <Eigen/Core> #include <Eigen/Dense> #include <Eigen/Sparse> @@ -22,6 +23,8 @@ public: void init(Eigen::Vector3f &min, Eigen::Vector3f &max); void move(int vertex, Eigen::Vector3f pos); + void update(double seconds); + // ================== Students, If You Choose To Modify The Code Below, It's On You @@ -65,5 +68,9 @@ public: int m_num_iterations; const char * m_mesh_path; + + ocean m_ocean; + double m_time = 0.00; + double m_timestep = 0.001; }; diff --git a/src/glwidget.cpp b/src/glwidget.cpp index 8c92f5b..2a7a452 100755 --- a/src/glwidget.cpp +++ b/src/glwidget.cpp @@ -70,6 +70,8 @@ void GLWidget::initializeGL() glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); + // glShadeModel(GL_SMOOTH); + // Initialize shaders m_defaultShader = new Shader(":resources/shaders/shader.vert", ":resources/shaders/shader.frag"); @@ -288,6 +290,7 @@ void GLWidget::keyReleaseEvent(QKeyEvent *event) void GLWidget::tick() { float deltaSeconds = m_deltaTimeProvider.restart() / 1000.f; + m_arap.update(deltaSeconds); // Move camera auto look = m_camera.getLook(); diff --git a/src/ocean/ocean.cpp b/src/ocean/ocean.cpp index f70a553..1b0e23c 100644 --- a/src/ocean/ocean.cpp +++ b/src/ocean/ocean.cpp @@ -10,18 +10,24 @@ ocean::ocean() { initial_h = std::vector<std::pair<double, double>>(); + current_h = std::vector<std::pair<double, double>>(); + // initialize the initial height fields for (int i = 0; i < N; i++) { initial_h.push_back(amplitude_0(i)); + current_h.push_back(initial_h[i]); } } +void ocean::updateVertexAmplitudes(double t){ + for (int i = 0; i < N; i++){ + current_h[i] = amplitude_t(t, i); + } +} + /* Maps the 1D k-index into it's 2D waveform vector */ -std::pair<double, double> ocean::k_index_to_k_vector - ( - int k_index - ) +std::pair<double, double> ocean::k_index_to_k_vector(int k_index) { // get the x and z indices int x = k_index % length; @@ -200,11 +206,12 @@ std::pair<double, double> ocean::amplitude_t // add the real and imaginary part together from both h_0 and h_0_conjugate double real = // h+0 real - h_0.first * exp_positive.first - - h_0.second * exp_positive.second + (h_0.first * exp_positive.first) + - (h_0.second * exp_positive.second) // h_0_conjugate real - + h_0_conjugate.first * exp_negative.first - - h_0_conjugate.second * exp_negative.second; + + (h_0_conjugate.first * exp_negative.first) + - (h_0_conjugate.second * exp_negative.second); + double imag = // h_0 imaginary h_0.first * exp_positive.second @@ -225,9 +232,14 @@ std::vector<Eigen::Vector3f> ocean::get_vertices() double k_x = k.first; double k_z = k.second; - double amplitude = initial_h[i].first; + //if (i < length) + double amplitude = current_h[i].first; + if (i < length) amplitude = initial_h[i].first; + + + //if (i==2) std::cout << amplitude << std::endl; - std::cout << "k_x: " << k_x << " k_z: " << k_z << " amplitude: " << amplitude << std::endl; + //std::cout << "k_x: " << k_x << " k_z: " << k_z << " amplitude: " << amplitude << std::endl; vertices.emplace_back(k_x, amplitude, k_z); } @@ -251,8 +263,8 @@ std::vector<Eigen::Vector3i> ocean::get_faces() int i3 = i + length; int i4 = i + length + 1; - faces.emplace_back(i1, i2, i3); - faces.emplace_back(i2, i3, i4); + faces.emplace_back(i2, i1, i3); + faces.emplace_back(i2, i3, i4); } } return faces; diff --git a/src/ocean/ocean.h b/src/ocean/ocean.h index 7e436cf..faa0fb2 100644 --- a/src/ocean/ocean.h +++ b/src/ocean/ocean.h @@ -14,9 +14,17 @@ class ocean { public: ocean(); + void updateVertexAmplitudes(double t); + std::vector<Eigen::Vector3f> get_vertices(); + std::vector<Eigen::Vector3i> get_faces(); - const int length = 16; // length of grid - const int width = 16; // width of grid + + + +private: + + const int length = 32; // length of grid + const int width = 32; // width of grid const int N = length * width; // total number of grid points const double A = 100; // numeric constant for the Phillips spectrum @@ -25,13 +33,11 @@ public: = std::make_pair(1.0, 0.0); // wind direction std::vector<std::pair<double, double>> initial_h; // initial height fields for each K + std::vector<std::pair<double, double>> current_h; // current height fields for each K + const double D = 1; // Depth below mean water level (for dispersion relation) - std::vector<Eigen::Vector3f> get_vertices - (); - std::vector<Eigen::Vector3i> get_faces - (); std::pair<double, double> k_index_to_k_vector ( |