Commit 408b5ca7 authored by bbguimaraes's avatar bbguimaraes
Browse files

sms: simulation class to wrap systems

parent dcc6a9eb
......@@ -8,6 +8,7 @@
#include "sms/vector.h"
#include "camera.h"
#include "simulation.h"
class GLUquadric;
......@@ -21,21 +22,14 @@ class Display : public QGLWidget {
Display(QWidget * parent = nullptr);
~Display();
float update_rate() const {return this->m_update_rate;}
Camera * camera() {return &this->m_camera;}
bool paused() const {return this->m_paused;}
bool textured() const {return this->m_textured;}
void set_systems(const std::vector<SpringMassSystem *> & v)
{this->m_systems = v;}
void set_update_rate(float r) {this->m_update_rate = r;}
void set_simulations(const std::vector<Simulation *> & v)
{this->m_simulations = v;}
void set_texture(GLuint t) {this->m_texture = t;}
void set_paused(bool p) {this->m_paused = p;}
void set_draw_grid(bool d) {this->m_draw_grid = d;}
void set_grid_size(int s) {this->m_grid_size = s;}
void set_draw_axes(bool d) {this->m_draw_axes = d;}
void set_draw_masses(bool d) {this->m_draw_masses = d;}
void set_textured(bool t) {this->m_textured = t;}
public slots:
void update_systems();
......@@ -59,7 +53,7 @@ class Display : public QGLWidget {
void draw_axes();
void draw_axis(float height);
void draw_circle(float radius);
void draw_system(SpringMassSystem * system);
void draw_simulation(Simulation * s);
void draw_springs_non_textured(const SpringMassSystem * system);
void draw_springs_textured(SpringMassSystem * system);
void draw_masses(const SpringMassSystem * system, GLenum mode);
......@@ -67,9 +61,8 @@ class Display : public QGLWidget {
void draw_number(unsigned int n);
void update_fps();
std::vector<SpringMassSystem *> m_systems;
std::vector<Simulation *> m_simulations;
Mass * m_selected;
float m_update_rate;
Camera m_camera;
GLuint m_texture;
GLUquadric * m_quadric;
......@@ -82,11 +75,8 @@ class Display : public QGLWidget {
Vector m_click_position;
Vector m_last_position;
bool m_paused;
bool m_draw_grid;
bool m_draw_axes;
bool m_draw_masses;
bool m_textured;
int m_grid_size;
};
......
#ifndef SIMULATION_H
#define SIMULATION_H
#include <vector>
class SpringMassSystem;
class Simulation {
std::vector<SpringMassSystem *> m_systems;
double m_update_rate;
bool m_paused;
bool m_draw_masses;
bool m_textured;
public:
Simulation() : m_paused(false), m_draw_masses(true), m_textured(false)
{}
const std::vector<SpringMassSystem *> * systems() const
{return &this->m_systems;}
double update_rate() const {return this->m_update_rate;}
bool paused() const {return this->m_paused;}
bool draw_masses() const {return this->m_draw_masses;}
bool textured() const {return this->m_textured;}
void set_systems(const std::vector<SpringMassSystem *> & v)
{this->m_systems = v;}
void set_update_rate(double r) {this->m_update_rate = r;}
void set_paused(bool p) {this->m_paused = p;}
void set_draw_masses(bool d) {this->m_draw_masses = d;}
void set_textured(bool t) {this->m_textured = t;}
void update();
};
#endif // SIMULATION_H
......@@ -14,16 +14,12 @@
Display::Display(QWidget * parent) :
QGLWidget(parent),
m_selected(nullptr),
m_update_rate(1.0f),
m_texture(0),
m_quadric(nullptr),
m_last_frame(QTime::currentTime()),
m_ctrl_key_down(false),
m_paused(false),
m_draw_grid(false),
m_draw_axes(false),
m_draw_masses(true),
m_textured(false),
m_grid_size(100) {
this->setFocusPolicy(Qt::StrongFocus);
}
......@@ -58,8 +54,8 @@ void Display::paintGL() {
this->draw_grid();
if(this->m_draw_axes)
this->draw_axes();
for(auto x : this->m_systems)
this->draw_system(x);
for(auto x : this->m_simulations)
this->draw_simulation(x);
this->update_fps();
this->draw_hud();
}
......@@ -98,16 +94,17 @@ void Display::select(Vector click) {
glLoadIdentity();
this->set_camera();
Mass * selected = nullptr;
for(auto system : this->m_systems) {
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
this->draw_masses(system, GL_SELECT);
if(glRenderMode(GL_RENDER) != 0) {
selected = &(*system->masses())[select_buffer[3]];
break;
for(auto simulation : this->m_simulations)
for(auto system : *simulation->systems()) {
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
this->draw_masses(system, GL_SELECT);
if(glRenderMode(GL_RENDER) != 0) {
selected = &(*system->masses())[select_buffer[3]];
break;
}
}
}
glMatrixMode(GL_PROJECTION);
glPopMatrix();
this->m_selected = selected;
......@@ -115,10 +112,8 @@ void Display::select(Vector click) {
/*slot*/
void Display::update_systems() {
if(this->paused())
return;
for(auto x : this->m_systems)
x->update(this->m_update_rate);
for(auto x : this->m_simulations)
x->update();
}
void Display::draw_grid() {
......@@ -180,13 +175,15 @@ void Display::draw_circle(float radius) {
glEnd();
}
void Display::draw_system(SpringMassSystem * system) {
if(!this->textured())
this->draw_springs_non_textured(system);
else
this->draw_springs_textured(system);
if(this->m_draw_masses)
this->draw_masses(system, GL_RENDER);
void Display::draw_simulation(Simulation * s) {
for(auto system : *s->systems()) {
if(!s->textured())
this->draw_springs_non_textured(system);
else
this->draw_springs_textured(system);
if(s->draw_masses())
this->draw_masses(system, GL_RENDER);
}
}
void Display::draw_springs_non_textured(const SpringMassSystem * system) {
......@@ -320,13 +317,16 @@ void Display::keyPressEvent(QKeyEvent * event) {
this->m_draw_grid = !this->m_draw_grid;
break;
case 'M':
this->m_draw_masses = !this->m_draw_masses;
for(auto x : this->m_simulations)
x->set_draw_masses(!x->draw_masses());
break;
case 'P':
this->m_paused = !this->m_paused;
for(auto x : this->m_simulations)
x->set_paused(!x->paused());
break;
case 'T':
this->m_textured = !this->m_textured;
for(auto x : this->m_simulations)
x->set_textured(!x->textured());
break;
case Qt::Key_Control:
this->m_ctrl_key_down = true;
......
......@@ -15,10 +15,18 @@ void init_sm_system3(SpringMassSystem * system);
GLuint load_texture(const std::string & filename);
int main(int argc, char ** argv) {
const double UPDATE_RATE = 1.0f / 5000.0f;
QApplication app(argc, argv);
SpringMassSystem sms0, sms1;
init_sm_system3(&sms0);
init_sm_system3(&sms1);
init_sm_system2(&sms1);
Simulation simulations[2];
simulations[0].set_systems({&sms0});
simulations[1].set_systems({&sms1});
simulations[0].set_paused(true);
simulations[1].set_paused(true);
simulations[0].set_update_rate(UPDATE_RATE);
simulations[1].set_update_rate(UPDATE_RATE);
for(auto & mass : *sms0.masses())
mass.set_position(mass.position() + Vector(-10.0f));
for(auto & mass : *sms1.masses())
......@@ -26,23 +34,20 @@ int main(int argc, char ** argv) {
Display display;
display.makeCurrent();
display.set_texture(load_texture("texture.jpg"));
display.set_systems({&sms0, &sms1});
display.set_update_rate(1.0f / 5000.0f);
display.set_paused(true);
display.set_textured(false);
display.set_simulations({&simulations[0], &simulations[1]});
display.camera()->set_distance(30.0f);
display.setMouseTracking(true);
display.resize(800, 600);
display.show();
QTimer display_timer, simulation_timer;
QObject::connect(
&display_timer, SIGNAL(timeout()),
&display, SLOT(update_systems()));
&display_timer, &QTimer::timeout,
[&simulations](){for(auto & x : simulations) x.update();});
QObject::connect(
&simulation_timer, SIGNAL(timeout()),
&display, SLOT(update()));
&simulation_timer, &QTimer::timeout,
&display, static_cast<void (Display::*)()>(&Display::update));
display_timer.start(1.0f / 60.0f);
simulation_timer.start(display.update_rate());
simulation_timer.start(UPDATE_RATE);
return app.exec();
}
......
#include "simulation.h"
#include "sms/springmasssystem.h"
void Simulation::update() {
if(this->paused())
return;
for(auto x : this->m_systems)
x->update(this->update_rate());
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment