Last active
August 29, 2015 13:57
-
-
Save ASxa86/9766448 to your computer and use it in GitHub Desktop.
Mass Spring Bezier Patch for cloth simulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Timer.h" | |
#include "Mass.h" | |
#include <glut.h> | |
#include <iostream> | |
#include <vector> | |
Timer timer; | |
std::vector<Mass> ctrlPoints; | |
void display() | |
{ | |
const auto dt = timer.restart(); | |
const auto fps = 1.0f / dt; | |
std::cout << "FPS: " << fps << std::endl; | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glLoadIdentity(); | |
for(const auto& i : ctrlPoints) | |
{ | |
i.draw(); | |
} | |
glutSwapBuffers(); | |
glFlush(); | |
} | |
void reshape(int w, int h) | |
{ | |
glViewport(0, 0, w, h); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glOrtho(0, w, h, 0, -1, 1); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
} | |
void keyboard(unsigned char key, int x, int y) | |
{ | |
switch(key) | |
{ | |
case 27: | |
exit(0); | |
default: | |
break; | |
} | |
} | |
void idle() | |
{ | |
glutPostRedisplay(); | |
} | |
auto buttonPressed = false; | |
void mouse(int button, int state, int x, int y) | |
{ | |
if(button == GLUT_LEFT_BUTTON) | |
{ | |
if(state == GLUT_DOWN) | |
{ | |
buttonPressed = true; | |
} | |
else | |
{ | |
buttonPressed = false; | |
} | |
} | |
} | |
void motion(int x, int y) | |
{ | |
if(buttonPressed == true) | |
{ | |
for(auto& i : ctrlPoints) | |
{ | |
if(i.bb().contains(x, y) == true) | |
{ | |
i.setPosition(x, y, 0); | |
std::cout << "Moved (" << x << ", " << y << ")" << std::endl; | |
} | |
} | |
} | |
} | |
void buildCtrlPoints() | |
{ | |
Mass m; | |
m.setColor(1, 1, 0); | |
m.setPointSize(10); | |
for(auto i = 1; i < 5; i++) | |
{ | |
for(auto j = 1; j < 5; j++) | |
{ | |
m.setPosition(j * 100, i * 100, 0); | |
ctrlPoints.push_back(m); | |
} | |
} | |
} | |
int main(int argc, char** argv) | |
{ | |
buildCtrlPoints(); | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); | |
glutInitWindowSize(800, 600); | |
glutCreateWindow("Assignment3"); | |
glEnable(GL_DEPTH_TEST); | |
glClearColor(0, 0, 0, 0); | |
glutDisplayFunc(display); | |
glutReshapeFunc(reshape); | |
glutKeyboardFunc(keyboard); | |
glutIdleFunc(idle); | |
glutMouseFunc(mouse); | |
glutMotionFunc(motion); | |
glutMainLoop(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Mass.h" | |
#include <glut.h> | |
Mass::Mass() : | |
m_color(1, 0, 0), | |
m_position(0, 0, 0), | |
m_size(1), | |
m_weight(0.5), | |
m_bb(m_position, m_size) | |
{ | |
} | |
void Mass::draw() const | |
{ | |
glPushMatrix(); | |
glPointSize(this->m_size); | |
glColor3f(this->m_color.x, this->m_color.y, this->m_color.z); | |
glBegin(GL_POINTS); | |
glVertex3f(this->m_position.x, this->m_position.y, this->m_position.z); | |
glEnd(); | |
glPopMatrix(); | |
} | |
void Mass::setWeight(const float x) | |
{ | |
this->m_weight = x; | |
} | |
float Mass::getWeight() const | |
{ | |
return this->m_weight; | |
} | |
void Mass::setPosition(const float x, const float y, const float z) | |
{ | |
this->setPosition(Vector3f(x, y, z)); | |
} | |
void Mass::setPosition(const Vector3f& x) | |
{ | |
this->m_position = x; | |
this->m_bb.setCenter(x); | |
} | |
Vector3f Mass::getPosition() const | |
{ | |
return this->m_position; | |
} | |
void Mass::setColor(const float x, const float y, const float z) | |
{ | |
this->setColor(Vector3f(x, y, z)); | |
} | |
void Mass::setColor(const Vector3f& x) | |
{ | |
this->m_color = x; | |
} | |
void Mass::setPointSize(const float x) | |
{ | |
this->m_size = x; | |
this->m_bb.setSize(x); | |
} | |
AABB Mass::bb() const | |
{ | |
return this->m_bb; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment