Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment8 #6

Merged
merged 15 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ int
*.vcxproj
*.vcxproj.filters
*.vcxproj.user

!Frame/Source/ThirdParty/glew/build
Binary file added Document/FinalProject.pdf
Binary file not shown.
107 changes: 107 additions & 0 deletions Frame/Source/Assignment/Assignment8/application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <iostream>

#include "application.h"
#include "rope.h"

namespace CGL {

Application::Application(AppConfig config) { this->config = config; }

Application::~Application() {}

void Application::init() {
// Enable anti-aliasing and circular points.
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_POINT_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);

glPointSize(8);
glLineWidth(4);

glColor3f(1.0, 1.0, 1.0);
// Create two ropes
ropeEuler = new Rope(Vector2D(0, 200), Vector2D(-400, 200), 3, config.mass,
config.ks, {0});
ropeVerlet = new Rope(Vector2D(0, 200), Vector2D(-400, 200), 3, config.mass,
config.ks, {0});
}

void Application::render() {
//Simulation loops
for (int i = 0; i < config.steps_per_frame; i++) {
ropeEuler->simulateEuler(1 / config.steps_per_frame, config.gravity);
ropeVerlet->simulateVerlet(1 / config.steps_per_frame, config.gravity);
}
// Rendering ropes
Rope *rope;

for (int i = 0; i < 2; i++) {
if (i == 0) {
glColor3f(0.0, 0.0, 1.0);
rope = ropeEuler;
} else {
glColor3f(0.0, 1.0, 0.0);
rope = ropeVerlet;
}

glBegin(GL_POINTS);

for (auto &m : rope->masses) {
Vector2D p = m->position;
glVertex2d(p.x, p.y);
}

glEnd();

glBegin(GL_LINES);

for (auto &s : rope->springs) {
Vector2D p1 = s->m1->position;
Vector2D p2 = s->m2->position;
glVertex2d(p1.x, p1.y);
glVertex2d(p2.x, p2.y);
}

glEnd();

glFlush();
}
}

void Application::resize(size_t w, size_t h) {
screen_width = w;
screen_height = h;

float half_width = (float)screen_width / 2;
float half_height = (float)screen_height / 2;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-half_width, half_width, -half_height, half_height, 1, 0);
}

void Application::keyboard_event(int key, int event, unsigned char mods) {
switch (key) {
case '-':
if (config.steps_per_frame > 1) {
config.steps_per_frame /= 2;
}
break;
case '=':
config.steps_per_frame *= 2;
break;
}
}

string Application::name() { return "Rope Simulator"; }

string Application::info() {
ostringstream steps;
steps << "Steps per frame: " << config.steps_per_frame;

return steps.str();
}
}
70 changes: 70 additions & 0 deletions Frame/Source/Assignment/Assignment8/application.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef CGL_APPLICATION_H
#define CGL_APPLICATION_H

// STL
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

// libCGL
#include "CGL/CGL.h"
#include "CGL/osdtext.h"
#include "CGL/renderer.h"

#include "rope.h"

using namespace std;

namespace CGL {

struct AppConfig {
AppConfig() {
// Rope config variables
mass = 1;
ks = 100;

// Environment variables
gravity = Vector2D(0, -1);
steps_per_frame = 64;
}

float mass;
float ks;

float steps_per_frame;
Vector2D gravity;
};

class Application : public Renderer {
public:
Application(AppConfig config);
~Application();

void init();
void render();
void resize(size_t w, size_t h);

std::string name();
std::string info();

void keyboard_event(int key, int event, unsigned char mods);
// void cursor_event(float x, float y);
// void scroll_event(float offset_x, float offset_y);
// void mouse_event(int key, int event, unsigned char mods);

private:
AppConfig config;

Rope *ropeEuler;
Rope *ropeVerlet;

size_t screen_width;
size_t screen_height;

}; // class Application

} // namespace CGL

#endif // CGL_APPLICATION_H
36 changes: 36 additions & 0 deletions Frame/Source/Assignment/Assignment8/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//#pragma comment(lib, "opengl32.lib")

#include "GL/glew.h"
#include "GLFW/glfw3.h"

#include "CGL/CGL.h"
#include "CGL/viewer.h"

#include "application.h"
typedef uint32_t gid_t;

#include <iostream>

using namespace std;
using namespace CGL;

int main(int argc, char **argv) {
AppConfig config;

// create application
Application *app = new Application(config);

// create viewer
Viewer viewer = Viewer();

// set renderer
viewer.set_renderer(app);

// init viewer
viewer.init();

// start viewer
viewer.start();

return 0;
}
30 changes: 30 additions & 0 deletions Frame/Source/Assignment/Assignment8/mass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef MASS_H
#define MASS_H

#include "CGL/CGL.h"
#include "CGL/vector2D.h"

using namespace CGL;

struct Mass {
Mass(Vector2D position, float mass, bool pinned)
: start_position(position), position(position), last_position(position),
mass(mass), pinned(pinned) {}

float mass;
bool pinned;

Vector2D start_position;
Vector2D position;

// explicit Verlet integration

Vector2D last_position;

// explicit Euler integration

Vector2D velocity;
Vector2D forces;
};

#endif /* MASS_H */
61 changes: 61 additions & 0 deletions Frame/Source/Assignment/Assignment8/rope.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
#include <vector>

#include "CGL/vector2D.h"

#include "mass.h"
#include "rope.h"
#include "spring.h"

namespace CGL {

Rope::Rope(Vector2D start, Vector2D end, int num_nodes, float node_mass, float k, vector<int> pinned_nodes)
{
// TODO (Part 1): Create a rope starting at `start`, ending at `end`, and containing `num_nodes` nodes.

// Comment-in this part when you implement the constructor
// for (auto &i : pinned_nodes) {
// masses[i]->pinned = true;
// }
}

void Rope::simulateEuler(float delta_t, Vector2D gravity)
{
for (auto &s : springs)
{
// TODO (Part 2): Use Hooke's law to calculate the force on a node
}

for (auto &m : masses)
{
if (!m->pinned)
{
// TODO (Part 2): Add the force due to gravity, then compute the new velocity and position

// TODO (Part 2): Add global damping
}

// Reset all forces on each mass
m->forces = Vector2D(0, 0);
}
}

void Rope::simulateVerlet(float delta_t, Vector2D gravity)
{
for (auto &s : springs)
{
// TODO (Part 3): Simulate one timestep of the rope using explicit Verlet (solving constraints)
}

for (auto &m : masses)
{
if (!m->pinned)
{
Vector2D temp_position = m->position;
// TODO (Part 3.1): Set the new position of the rope mass

// TODO (Part 4): Add global Verlet damping
}
}
}
}
26 changes: 26 additions & 0 deletions Frame/Source/Assignment/Assignment8/rope.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef ROPE_H
#define ROPE_H

#include "CGL/CGL.h"
#include "mass.h"
#include "spring.h"

using namespace std;

namespace CGL {

class Rope {
public:
Rope(vector<Mass *> &masses, vector<Spring *> &springs)
: masses(masses), springs(springs) {}
Rope(Vector2D start, Vector2D end, int num_nodes, float node_mass, float k,
vector<int> pinned_nodes);

void simulateVerlet(float delta_t, Vector2D gravity);
void simulateEuler(float delta_t, Vector2D gravity);

vector<Mass *> masses;
vector<Spring *> springs;
}; // struct Rope
}
#endif /* ROPE_H */
22 changes: 22 additions & 0 deletions Frame/Source/Assignment/Assignment8/spring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef SPRING_H
#define SPRING_H

#include "CGL/CGL.h"
#include "mass.h"

using namespace std;

namespace CGL {

struct Spring {
Spring(Mass *a, Mass *b, float k)
: m1(a), m2(b), k(k), rest_length((a->position - b->position).norm()) {}

float k;
double rest_length;

Mass *m1;
Mass *m2;
}; // struct Spring
}
#endif /* SPRING_H */
22 changes: 22 additions & 0 deletions Frame/Source/Assignment/CGL/CGL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "GL/glew.h"
#include "GLFW/glfw3.h"

#include "vector2D.h"
#include "complex.h"

#include "vector3D.h"
#include "matrix3x3.h"

#include "vector4D.h"
#include "matrix4x4.h"

// quaternions seem pretty cool.
#include "quaternion.h"


#include "color.h"
#include "renderer.h"
#include "viewer.h"

#include "base64.h"
#include "tinyxml2.h"
Loading