diff --git a/alpha/alpha.pro b/alpha/alpha.pro index 1d72d5d..58f1301 100644 --- a/alpha/alpha.pro +++ b/alpha/alpha.pro @@ -1,18 +1,31 @@ TEMPLATE = app -QT += core gui opengl -CONFIG += c++11 release warn_on +QT += opengl +CONFIG += c++11 debug_and_release warn_on + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets DESTDIR += bin INCLUDEPATH += \ $$PWD/../symath \ $$PWD/../symgraph -#HEADERS += \ +HEADERS += \ + alphascene.h \ + sphere.h SOURCES += \ - main.cpp + main.cpp \ + alphascene.cpp \ + sphere.cpp + +win32:LIBS += opengl32.lib + +LIBS += -L../symath/lib/ \ + -L../symgraph/lib/ \ + -lsymath \ + -lsymgraph RESOURCES += \ resource.qrc diff --git a/alpha/alphascene.cpp b/alpha/alphascene.cpp new file mode 100644 index 0000000..32828d9 --- /dev/null +++ b/alpha/alphascene.cpp @@ -0,0 +1,32 @@ +#include "alphascene.h" +#include <iostream> +#include "sphere.h" + +using namespace std; + +AlphaScene::AlphaScene(QWidget *parent) : Scene(parent) +{ + this->sphere = new Sphere(); + + this->addShader(ShaderID::color, fragment_shader); + this->addShader(ShaderID::vertex, vertex_shader); + + + this->addShape(sphere); +} + +AlphaScene::~AlphaScene() +{ + this->rmShape(sphere); + + delete this->sphere; +} + +void AlphaScene::quitEvent(int) +{ + cout << "Closing program!!!" << endl; + +} + +const QString AlphaScene::vertex_shader = ":/vertex_shader.glsl"; +const QString AlphaScene::fragment_shader = ":/fragment_shader.glsl"; diff --git a/alpha/alphascene.h b/alpha/alphascene.h new file mode 100644 index 0000000..70aac13 --- /dev/null +++ b/alpha/alphascene.h @@ -0,0 +1,28 @@ +#ifndef __ALPHA_SCENE_H__ +#define __ALPHA_SCENE_H__ + +#include "scene.h" + +class Sphere; + +class AlphaScene : public Scene +{ + +public: + + AlphaScene(QWidget* parent = 0); + + ~AlphaScene(); + + +private: + + virtual void quitEvent(int exit_code) override; + + Sphere * sphere; + + static const QString vertex_shader; + static const QString fragment_shader; +}; + +#endif diff --git a/alpha/fragment_shader.glsl b/alpha/fragment_shader.glsl index 3999d74..a128cac 100644 --- a/alpha/fragment_shader.glsl +++ b/alpha/fragment_shader.glsl @@ -1,4 +1,6 @@ +varying vec4 frag_color; void main() { + gl_FragColor = frag_color; } diff --git a/alpha/main.cpp b/alpha/main.cpp index 13e3263..d15a27d 100644 --- a/alpha/main.cpp +++ b/alpha/main.cpp @@ -1,17 +1,12 @@ #include <QApplication> -#include <iostream> -#include "scene.h" - -using namespace std; - -//Scene scene; +#include "alphascene.h" int main(int argc, char **argv) { QApplication app(argc, argv); - //scene.show(); - cout << "Funziona!!!" << endl; + AlphaScene scene; + scene.show(); return app.exec(); } diff --git a/alpha/sphere.cpp b/alpha/sphere.cpp new file mode 100644 index 0000000..a444f4b --- /dev/null +++ b/alpha/sphere.cpp @@ -0,0 +1,30 @@ +#include "sphere.h" +#include "models.h" + +const double Sphere::K = 10.0; + +Sphere::Sphere() + + : MaterialPoint(models::sphere(), Particle(1.0)) +{ + // empty +} + +Sphere::~Sphere() +{ + // empty +} + +void Sphere::update(const float&) +{ + /* Elastic force */ + this->particle.forces -= this->position() * K; +} + +#include <QGLShaderProgram> +#include "shaderid.h" + +void Sphere::colorCall(render_s &args) +{ + args.shaders->setAttributeValue(ShaderID::color, 0.7f, 0.5f, 0.2f, 1.0f); +} diff --git a/alpha/sphere.h b/alpha/sphere.h new file mode 100644 index 0000000..0ba475f --- /dev/null +++ b/alpha/sphere.h @@ -0,0 +1,24 @@ +#ifndef SPHERE_H +#define SPHERE_H + +#include "matpoint.h" + +class Sphere : public MaterialPoint +{ + +public: + + Sphere(); + + ~Sphere(); + + virtual void update(const float &dt) override; + + static const double K; + +protected: + + virtual void colorCall(render_s &) override; +}; + +#endif // SPHERE_H diff --git a/alpha/vertex_shader.glsl b/alpha/vertex_shader.glsl index a68d957..877fe22 100644 --- a/alpha/vertex_shader.glsl +++ b/alpha/vertex_shader.glsl @@ -1,9 +1,13 @@ attribute vec3 vertex; +attribute vec4 color; uniform mat4 projection; uniform mat4 modelview; +varying vec4 frag_color; + void main() { gl_Position = projection * modelview * vec4(vertex, 1.0); + frag_color = color; } diff --git a/symath/particle.cpp b/symath/particle.cpp index 7285526..c38bfd5 100644 --- a/symath/particle.cpp +++ b/symath/particle.cpp @@ -1,30 +1,26 @@ #include "particle.h" -Particle::Particle(double mass, const SVector<3>& position, const SVector<3>& speed) +Particle::Particle(const double& mass, const SVector<3>& position, const SVector<3>& speed) + + : mass(mass), position(position), speed(speed), forces(SVector<3>(true)) { - this->mass = mass; - this->position = position; - this->speed = speed; - this->forces = SVector<3>::nullv; + } Particle::Particle(const Particle& particle) { - this->mass = particle.mass; - this->position = particle.position; - this->speed = particle.speed; - this->forces = SVector<3>::nullv; + *this = particle; } Particle::~Particle() { //empty } void Particle::update(const float &dt) { speed += forces * dt / mass; position += speed * dt; - forces = SVector<3>::nullv; + forces.null(); } diff --git a/symath/particle.h b/symath/particle.h index ceff1d7..aa5eb48 100644 --- a/symath/particle.h +++ b/symath/particle.h @@ -1,27 +1,27 @@ #ifndef __PARTICLE_H__ #define __PARTICLE_H__ #include "svector.h" struct Particle { - - SVector<3> position; - SVector<3> speed; - - double mass; - SVector<3> forces; - - Particle( double mass, - const SVector<3>& position = SVector<3>::nullv, - const SVector<3>& speed = SVector<3>::nullv); + Particle( const double& mass = 1.0, + const SVector<3>& position = SVector<3>(true), + const SVector<3>& speed = SVector<3>(true)); Particle(const Particle& particle); ~Particle(); void update(const float &dt); + + double mass; + + SVector<3> position; + SVector<3> speed; + + SVector<3> forces; }; #endif diff --git a/symath/svector.cpp b/symath/svector.cpp index 9f1931e..ecd367a 100644 --- a/symath/svector.cpp +++ b/symath/svector.cpp @@ -1,14 +1,12 @@ #include "svector.h" -#ifndef __linux__ const SVector<3> operator^(const SVector<3>& v, const SVector<3>& w) { SVector<3> u = {0, 0, 0}; u[0] = v[1] * w[2] - v[2] * w[1]; u[1] = v[2] * w[0] - v[0] * w[2]; u[2] = v[0] * w[1] - v[1] * w[0]; return u; } -#endif diff --git a/symath/svector.h b/symath/svector.h index ea31b96..67fc87f 100644 --- a/symath/svector.h +++ b/symath/svector.h @@ -1,265 +1,265 @@ #ifndef __STATIC_VECTOR_H__ #define __STATIC_VECTOR_H__ #include <initializer_list> #include <array> #include <cmath> // needed for sqrt template<std::size_t N> class SVector { public: typedef std::size_t size_t; /* Constructors */ - SVector() + SVector(bool null = false) { - this->components.fill(0); + if (null) + this->null(); } SVector(const std::initializer_list<double> &init) { if (init.size() != N) return; // TODO throw exception /* copy init list without include <algorithm> */ size_t index = 0; for (auto c : init) this->components[index++] = c; } SVector(const SVector<N>& init) { *this = init; } /* For auto implementation */ /* it allows to loop through components * * for (auto comp : v) * { * // loop content * } */ typedef typename std::array<double, N>::iterator iterator; typedef typename std::array<double, N>::const_iterator const_iterator; iterator begin() { return components.begin(); } iterator end() { return components.end(); } const_iterator begin() const { return components.begin(); } const_iterator end() const { return components.end(); } /* Reduce to module = 1 */ SVector<N> unit() const { return *this / this->module(); } size_t size() const { return N; // quite basic } double& operator[](size_t i) { return components[i]; } const double& operator[](size_t i) const { return components[i]; } bool operator==(const SVector<N>& v) const { for (size_t i = 0; i < N; ++i) { if (components[i] != v[i]) return false; } return true; } bool operator!=(const SVector<N>& v) const { return !(*this == v); } /* Plus / minus */ SVector<N>& operator+=(const SVector<N>& v) { for (size_t i = 0; i < N; ++i) components[i] += v[i]; return *this; } SVector<N>& operator-=(const SVector<N>& v) { for (size_t i = 0; i < N; ++i) components[i] -= v[i]; return *this; } /* Scalar multiplication / division */ SVector<N>& operator*=(double k) { for (size_t i = 0; i < N; ++i) components[i] *= k; return *this; } SVector<N>& operator/=(double k) { for (size_t i = 0; i < N; ++i) components[i] /= k; return *this; } /* Dot product */ double operator*(const SVector<N>& v) const { double x = 0; for (size_t i = 0; i < N; ++i) x += this->components[i] * v[i]; return x; } SVector<N> operator-() const { return (-1)*(*this); } SVector<N>& operator~() { return (*this)*=-1; } /* Return the module */ double module() const { return sqrt(this->sq_module()); } double sq_module() const { double x; for (auto c : components) x += c * c; return x; } - - static const SVector<N> nullv; + void null() + { + components.fill(0); + } private: std::array<double, N> components; }; #ifdef STATIC_VECTOR_IO template <std::size_t N> std::ostream& operator<<(std::ostream& os, const SVector<N> &v) { os << "("; for (auto comp : v) { os << comp << ", "; } os << ")"; return os; } #endif template<std::size_t N> const SVector<N> operator+(SVector<N> v, const SVector<N>& w) { return v += w; } template<std::size_t N> const SVector<N> operator-(SVector<N> v, const SVector<N>& w) { return v -= w; } template<std::size_t N> const SVector<N> operator*(SVector<N> v, double k) { return v *= k; } template<std::size_t N> const SVector<N> operator/(SVector<N> v, double k) { return v /= k; } /* * Prototipe for cross product * Implementation in vector.cpp */ -#ifdef __linux__ -const SVector<3> operator^(const SVector<3>& v, const SVector<3>& w) +/*const SVector<3> operator^(const SVector<3>& v, const SVector<3>& w) { SVector<3> u = {0, 0, 0}; u[0] = v[1] * w[2] - v[2] * w[1]; u[1] = v[2] * w[0] - v[0] * w[2]; u[2] = v[0] * w[1] - v[1] * w[0]; return u; -} +}*/ -#else const SVector<3> operator^(const SVector<3>& v, const SVector<3>& w); -#endif template <std::size_t N> SVector<N> apply_matrix(const double (&matrix)[N][N], const SVector<N>& v) { SVector<N> out; for (std::size_t i = 0; i < N; ++i) { for (std::size_t j = 0; j < N; ++j) out[i] += matrix[i][j] * v[j]; } return out; } template <std::size_t N> SVector<N> apply_homo_matrix(const double (&matrix)[N+1][N+1], const SVector<N>& v) { SVector<N> out; for (std::size_t i = 0; i < N; ++i) { for (std::size_t j = 0; j < N; ++j) out[i] += matrix[i][j] * v[j]; /* homogene component */ out[i] += matrix[i][N]; } return out; } #endif diff --git a/symath/symath.pro b/symath/symath.pro index 721deb3..01b0611 100644 --- a/symath/symath.pro +++ b/symath/symath.pro @@ -1,18 +1,18 @@ TEMPLATE = lib QT += core -CONFIG += c++11 staticlib release warn_on +CONFIG += c++11 staticlib debug_and_release warn_on DESTDIR += lib HEADERS += \ vector.h \ svector.h \ particle.h \ iosymkit.h SOURCES += \ vector.cpp \ svector.cpp \ particle.cpp diff --git a/symgraph/camera.cpp b/symgraph/camera.cpp index c6e7a47..8af61e3 100644 --- a/symgraph/camera.cpp +++ b/symgraph/camera.cpp @@ -1,25 +1,35 @@ #include "camera.h" #include "svector.h" #include <QMatrix4x4> +Camera::Camera() +{ + +} + +Camera::~Camera() +{ + +} + void Camera::moveCamera(const SVector<3>& position){ this->position=position; } void Camera::moveView(const SVector<3>& lookat){ this->lookat=lookat; } SVector<3> Camera::getPosition() const{ return position; } SVector<3> Camera::getLookat() const{ return lookat; } QMatrix4x4 Camera::resolve(const QMatrix4x4& matrix) { /* Easy multiplication */ return this->matrix * matrix; } diff --git a/symgraph/matpoint.cpp b/symgraph/matpoint.cpp index 0728e8f..93446a3 100644 --- a/symgraph/matpoint.cpp +++ b/symgraph/matpoint.cpp @@ -1,70 +1,86 @@ #include "matpoint.h" MaterialPoint::MaterialPoint( const GLBuffer& buffer, double mass, const SVector<3>& loc, const QString& label, flags_t flags) : Shape(buffer, label, flags), particle(mass, loc) { // empty } MaterialPoint::MaterialPoint( const GLBuffer& buffer, const Particle& particle, const QString& label, flags_t flags) : Shape(buffer, label, flags), particle(particle) { // empty } MaterialPoint::~MaterialPoint() {} double MaterialPoint::mass() const { return particle.mass; } const SVector<3>& MaterialPoint::speed() const { return particle.speed; } SVector<3> MaterialPoint::position() const { return particle.position; } const Particle& MaterialPoint::getParticle() const { return this->particle; } void MaterialPoint::setPosition(const SVector<3>& pos) { this->particle.position = pos; } void MaterialPoint::move(const SVector<3>& delta) { this->particle.position += delta; } +SVector<3> MaterialPoint::orientation() const +{ + // no orientation settings for a Material point + return SVector<3>(true); +} + +void MaterialPoint::setOrientation(const SVector<3>&) +{ + // empty +} + +void MaterialPoint::rotate(const SVector<3>&) +{ + //empty +} + void MaterialPoint::setSpeed(const SVector<3>& speed) { this->particle.speed = speed; } void MaterialPoint::setMass(double mass) { this->particle.mass = mass; } void MaterialPoint::post_update(const float& dt) { this->particle.update(dt); } diff --git a/symgraph/matpoint.h b/symgraph/matpoint.h index 5b8652d..9b703fb 100644 --- a/symgraph/matpoint.h +++ b/symgraph/matpoint.h @@ -1,58 +1,58 @@ #ifndef __MATPOINT_H__ #define __MATPOINT_H__ #include "shape.h" #include "particle.h" class MaterialPoint : public Shape { public: MaterialPoint( const GLBuffer&, double mass, - const SVector<3>& loc = SVector<3>::nullv, + const SVector<3>& loc = SVector<3>(true), const QString& label = "", flags_t flags = S_UPDATE | S_RENDER | S_FILL_MODE); MaterialPoint( const GLBuffer&, const Particle& particle, const QString& label = "", flags_t flags = S_UPDATE | S_RENDER | S_FILL_MODE); - ~MaterialPoint(); + virtual ~MaterialPoint(); /* Get fields */ double mass() const; const SVector<3>& speed() const; const Particle& getParticle() const; /* Set fields */ void setSpeed(const SVector<3>& speed); void setMass(double mass); virtual SVector<3> position() const override; virtual SVector<3> orientation() const override; virtual void setPosition(const SVector<3>& centre) override; virtual void setOrientation(const SVector<3>& angles) override; virtual void move(const SVector<3>& delta) override; virtual void rotate(const SVector<3>& delta) override; virtual void post_update(const float &dt) override; -private: +protected: Particle particle; }; #endif diff --git a/symgraph/models.h b/symgraph/models.h index 859b55e..3b37273 100644 --- a/symgraph/models.h +++ b/symgraph/models.h @@ -1,13 +1,12 @@ #ifndef __MODELS_H__ #define __MODELS_H__ #include "glbuffer.h" /* Specific built-in models for rendering */ namespace models { - GLBuffer emptyBuffer; - GLBuffer sphere(GLuint slices, GLuint stacks); + GLBuffer sphere(GLuint slices = 25, GLuint stacks = 25); } #endif diff --git a/symgraph/partsystem.cpp b/symgraph/partsystem.cpp index 3513942..588569c 100644 --- a/symgraph/partsystem.cpp +++ b/symgraph/partsystem.cpp @@ -1,72 +1,72 @@ #include "partsystem.h" #include "matpoint.h" #include "models.h" ParticleSystem::ParticleSystem( const std::vector<MaterialPoint*>& particles, const QString& label, flags_t flags) - : Shape(models::emptyBuffer, label, flags) + : Shape(GLBuffer(), label, flags) { this->particles = particles; } ParticleSystem::~ParticleSystem() { } void ParticleSystem::post_update(const float &dt) { for (auto p : particles) p->post_update(dt); } void ParticleSystem::render(render_s &args) { /* Call superclass method */ Shape::render(args); /* Render all particles */ for (auto point : particles) { if (point->isRendering()) point->render(args); } } SVector<3> ParticleSystem::position() const { /* Calculate center of mass */ SVector<3> com = {0, 0, 0}; return com; } SVector<3> ParticleSystem::orientation() const { /* Compute total orientation */ SVector<3> angles = {0, 0, 0}; return angles; } void ParticleSystem::setPosition(const SVector<3>&) { } void ParticleSystem::setOrientation(const SVector<3>&) { } void ParticleSystem::move(const SVector<3>&) { } void ParticleSystem::rotate(const SVector<3>&) { } diff --git a/symgraph/partsystem.h b/symgraph/partsystem.h index fa248ce..6b4a4ad 100644 --- a/symgraph/partsystem.h +++ b/symgraph/partsystem.h @@ -1,47 +1,47 @@ #ifndef __PARTSYSTEM_H__ #define __PARTSYSTEM_H__ #include <vector> #include "shape.h" class MaterialPoint; /* Shape inheritance is made to support graphical adds on in children classes */ class ParticleSystem : public Shape { public: ParticleSystem( const std::vector<MaterialPoint*>& particles = {}, const QString& label = "", flags_t flags = S_UPDATE | S_RENDER); - ~ParticleSystem(); + virtual ~ParticleSystem(); /* Method used to bind interactions between the particles */ virtual void interaction(MaterialPoint*, MaterialPoint*) = 0; virtual void post_update(const float &dt) override; /* Override of render method, it calls render for each MaterialPoint associated */ virtual void render(render_s &) override; /* Center of mass */ virtual SVector<3> position() const override; virtual SVector<3> orientation() const override; virtual void setPosition(const SVector<3>& centre) override; virtual void setOrientation(const SVector<3>& angles) override; virtual void move(const SVector<3>& delta) override; virtual void rotate(const SVector<3>& delta) override; private: std::vector<MaterialPoint*> particles; }; #endif diff --git a/symgraph/scene.cpp b/symgraph/scene.cpp index 0507b7f..d3f27f0 100644 --- a/symgraph/scene.cpp +++ b/symgraph/scene.cpp @@ -1,274 +1,279 @@ #include "scene.h" #include "shape.h" #include "camera.h" #include <string.h> // strcpy /* includes from Qt library */ #include <QGLShaderProgram> #include <QMatrix4x4> /* Scene class implementation */ Scene::Scene(QWidget *parent) : QGLWidget(parent) { /* Default camera initialization */ this->camera = new Camera(); /* Default values initialization */ this->delta_time = 10e-4f; this->initialized = false; this->width = 1024; this->height = 800; strcpy(this->perspective_id, "perspective"); strcpy(this->vertex_id, "vertex"); strcpy(this->color_id, "color"); strcpy(this->modelview_id, "modelview"); /* Shader program initialization */ - this->shaderManager = new QGLShaderProgram(); + //this->shaderManager = new QGLShaderProgram(); /* Bind quit call */ - QObject::connect(this, SIGNAL(Scene::quitCall(int)), this, SLOT(Scene::quit(int))); + QObject::connect(this, SIGNAL(quitCall(int)), this, SLOT(quitEvent(int))); } Scene::~Scene() { /* Class destruction */ /* Camera destruction */ delete camera; /* Shader program destruction */ - delete shaderManager; + //delete shaderManager; } void Scene::initializeGL() { if (initialized) throw "Scene " + this->windowTitle() + " already inizialized"; // build the shader - shaderManager->link(); + shaderManager.link(); // activate the shader - shaderManager->bind(); + shaderManager.bind(); /* Enable superposition control for graphical objects */ glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); this->initialized = true; } void Scene::resizeGL(int width, int height) { /* Set screen variables */ this->width = width; this->height = height; /* Update window viewport */ glViewport(0, 0, width, height); QMatrix4x4 matrix; /* Update perspective function */ matrix.perspective(this->perspective, (float)width / height, 1e-3f, 1e5f); /* Load the matrix to the shader */ - shaderManager->setUniformValue(this->perspective_id, matrix); + shaderManager.setUniformValue(this->perspective_id, matrix); } void Scene::paintGL() { /* clear screen */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Render iter */ QMatrix4x4 matrix; /* Initialize render arguments */ render_s render_args; render_args.camera = this->camera; render_args.matrix = &matrix; render_args.modelview_name = &this->modelview_id[0]; - render_args.shaders = this->shaderManager; + render_args.shaders = &this->shaderManager; render_args.polygonMode = true; /* default polygon mode */ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); /* Render all components */ for (auto shape : shapes) { /* Call shape's render function */ if (shape->isRendering()) shape->render(render_args); } } void Scene::updateComponents(const float &dt) { /* User updates all components */ for (auto shape : shapes) { /* Updating component */ if (shape->isUpdating()) shape->update(dt); } /* Engine updates all components */ for (auto shape : shapes) { /* Updating component */ if (shape->isUpdating()) shape->post_update(dt); } } void Scene::timerEvent(QTimerEvent*) { /* establish the correct delta time */ float dt = timer.restart() / 1000.0f; /* update all shapes */ this->updateComponents(dt); /* update graphics engine */ this->updateGL(); } +void Scene::quit(int exit_code) +{ + emit quitCall(exit_code); +} + Scene::iterator Scene::begin() { return shapes.begin(); } Scene::iterator Scene::end() { return shapes.end(); } Scene::const_iterator Scene::begin() const { return shapes.begin(); } Scene::const_iterator Scene::end() const { return shapes.end(); } void Scene::addShape(Shape *shape) { shapes.push_back(shape); } void Scene::rmShape(Shape *shape) { bool found = false; /* Need to check whether it exists or not */ for (auto s : shapes) { if (s == shape) { found = true; break; } } if (found) { /* remove from the queue */ shapes.remove(shape); } else throw QString("Could not remove shape object, it seems not to exists in the Scene"); } Shape * Scene::findShapeByLabel(const QString& label) const { for (auto p : shapes){ if(p->label()==label) return p; } return 0; } void Scene::removeAllShapes() { shapes.clear(); } Camera * Scene::getCamera() { return this->camera; } void Scene::addShader(ShaderID id, const QString& file) { if (initialized) throw QString("Could not add shader in the Scene, already initialized"); switch(id) { case ShaderID::vertex: - shaderManager->addShaderFromSourceFile(QGLShader::Vertex, file); + shaderManager.addShaderFromSourceFile(QGLShader::Vertex, file); break; case ShaderID::color: - shaderManager->addShaderFromSourceFile(QGLShader::Fragment, file); + shaderManager.addShaderFromSourceFile(QGLShader::Fragment, file); break; default: break; } } void Scene::setPerspectiveID(const char *label) { if (!initialized) strcpy(this->perspective_id, label); } void Scene::setVertexID(const char *label) { if (!initialized) strcpy(this->vertex_id, label); } void Scene::setColorID(const char *label) { if (!initialized) strcpy(this->color_id, label); } void Scene::setModelviewID(const char *label) { if (!initialized) strcpy(this->modelview_id, label); } void Scene::pause() { if (timerId != 0) { killTimer(timerId); timerId = 0; } } void Scene::resume() { if (timerId == 0) { timerId = startTimer((int)(delta_time * 1000)); timer.restart(); } } diff --git a/symgraph/scene.h b/symgraph/scene.h index 90d6c74..4976285 100644 --- a/symgraph/scene.h +++ b/symgraph/scene.h @@ -1,148 +1,147 @@ #ifndef __SCENE_H__ #define __SCENE_H__ /* * This namespace manages all "low level" * rendering state and functions * * Idea: All graphical components * are contained in a "linked" list such that, * for every frame and for each component * their "update" and "render" method are called. * * * */ #include <QGLWidget> +#include <QGLShaderProgram> #include <QTime> #include <list> #include "shaderid.h" /* Forward declarations */ class Shape; class Camera; -class QGLShaderProgram; /* Graphics scene manager */ class Scene : public QGLWidget { /* QObject macro, it expands to most of QWidget functionalities */ Q_OBJECT public: /* Windows constructor */ Scene(QWidget* parent = 0); /* Destructor */ - ~Scene(); + virtual ~Scene(); /* Iterator definition to loop through all shapes */ typedef std::list<Shape*>::iterator iterator; typedef std::list<Shape*>::const_iterator const_iterator; iterator begin(); iterator end(); const_iterator begin() const; const_iterator end() const; /* Add and remove shape functions */ void addShape(Shape *); void rmShape(Shape *); Shape * findShapeByLabel(const QString&) const; void removeAllShapes(); /* Camera settings */ Camera * getCamera(); /* Shader settings */ void addShader(ShaderID id, const QString& file); /* * ID string settings * Binding an id allows you to * reference to the pointed value * in the shader program */ void setPerspectiveID(const char*); void setVertexID(const char*); void setColorID(const char*); void setModelviewID(const char*); /* Timer functions */ void pause(); void resume(); /* Public fields */ /* Default delta time settings */ float delta_time; /* Window and view settings */ int width, height; float perspective; -public slots: - /* Quit function */ void quit(int exit_code); signals: /* Emittion quit callback */ void quitCall(int exit_code); -protected: +protected slots: /* Quit event, to be implemented in children classes */ virtual void quitEvent(int exit_code) = 0; private: /* Shortcuts to reduce a function density */ void updateComponents(const float &dt); /* * Overridden methods from QGLWidget * They implement all OpenGL calls */ virtual void initializeGL() override; virtual void resizeGL(int width, int height) override; virtual void paintGL() override; /* Timer callback */ virtual void timerEvent(QTimerEvent *event) override; /* Shape list */ std::list<Shape*> shapes; /* A camera instance to be used */ Camera * camera; /* Shader manager for detailed colors and vertices settings */ - QGLShaderProgram * shaderManager; + QGLShaderProgram shaderManager; #define LABEL_SIZE 24 /* String identifiers for the shader identities */ - char vertex_id[LABEL_SIZE + 1]; - char color_id[LABEL_SIZE + 1]; - char perspective_id[LABEL_SIZE + 1]; - char modelview_id[LABEL_SIZE + 1]; + + char vertex_id[LABEL_SIZE + 1]; // default "vertex" + char color_id[LABEL_SIZE + 1]; // default "color" + char perspective_id[LABEL_SIZE + 1]; // default "perspective" + char modelview_id[LABEL_SIZE + 1]; // default "modelview" /* Time management */ int timerId; QTime timer; /* Initialized flag */ bool initialized; }; #endif diff --git a/symgraph/shape.h b/symgraph/shape.h index 9daacc6..69c4f81 100644 --- a/symgraph/shape.h +++ b/symgraph/shape.h @@ -1,173 +1,173 @@ #ifndef __SHAPE_H__ #define __SHAPE_H__ #include "svector.h" #include "glbuffer.h" #include <QString> #include <QVector3D> /* basic graphical object class */ /* 8-bit type definition for shape flags */ typedef unsigned char flags_t; #define S_UPDATE 0x1 // first bit 00000001 #define S_RENDER 0x2 // second bit 00000010 #define S_FILL_MODE 0x4 // third bit 00000100 class QMatrix4x4; class QGLShaderProgram; class Camera; /* * This struct is made in order to pass * multiple arguments into the Shape::render function * keeping the references from the Scene class */ struct render_s { QGLShaderProgram *shaders; Camera *camera; QMatrix4x4 *matrix; char * modelview_name; bool polygonMode; }; /* * Renderizable class * Every object inheriting Shape can be renderized by * the Scene motor */ class Shape { public: Shape(const GLBuffer&, const QString &label = "", flags_t flags = S_UPDATE | S_RENDER | S_FILL_MODE); - ~Shape(); + virtual ~Shape(); QString label() const; void setGLBuffer(const GLBuffer&); const GLBuffer& getGLBuffer() const; /* Flags methods */ /* * Check wether to call the update (and post-update) method */ bool isUpdating() const; void toggleUpdate(); void setUpdate(bool value); /* * Check wheter to call the render method */ bool isRendering() const; void toggleRender(); void setRender(bool value); /* * This option is a reference to glPolygonMode * * In runtime, openGL allows you to ignore surfaces and draw * only the borders of a graphic object * * Setting fillmode to true, before rendering, * glPolygonMode will be set to GL_FILL * * Otherwise it will be set to to GL_LINE */ bool isFillMode() const; void toggleFillMode(); void setFillMode(bool value); /* * Set all flags in one using the | operator * and the following macros: * * S_UPDATE * S_RENDER * S_FILL_MODE * */ flags_t getFlags() const; void setFlags(flags_t flags); /* * update call to graphical engine * this method can be overridden * * Parameters: * * QGLShaderProgram* : shader program used in the scene * */ virtual void render(render_s&); /* returns the centre position */ virtual SVector<3> position() const = 0; /* returns the angle orientation in the space (Euler angles) */ virtual SVector<3> orientation() const = 0; /* sets a new centre position */ virtual void setPosition(const SVector<3>& centre) = 0; /* sets the euler angles orientation in the space */ virtual void setOrientation(const SVector<3>& angles) = 0; /* updates the centre position relatively to the old one */ virtual void move(const SVector<3>& delta) = 0; /* rotate from the euler angles */ virtual void rotate(const SVector<3>& delta) = 0; /* * User update component * Override this method to bind force interactions */ virtual void update(const float &dt) = 0; /* * Last time update * This method is called after all components in scene are * updated by the user in order to update relatively to * the engine (position and speed) */ virtual void post_update(const float &dt) = 0; private: /* technical informations */ QString _label; flags_t flags; protected: virtual void colorCall(render_s &) = 0; /* OpenGL graphical components */ GLBuffer buffer; }; /* Vector convertion */ QVector3D qtVector(const SVector<3>&); #endif diff --git a/symgraph/symgraph.pro b/symgraph/symgraph.pro index 0569fd5..d052177 100644 --- a/symgraph/symgraph.pro +++ b/symgraph/symgraph.pro @@ -1,30 +1,32 @@ TEMPLATE = lib QT += core opengl -CONFIG += c++11 staticlib release warn_on +CONFIG += c++11 staticlib debug_and_release warn_on DESTDIR += lib -LIBS += opengl32.lib +win32:LIBS += opengl32.lib + +LIBS += -L../symath/lib/ -lsymath \ INCLUDEPATH += $$PWD/../symath HEADERS += \ scene.h \ shape.h \ matpoint.h \ partsystem.h \ camera.h \ models.h \ glbuffer.h \ shaderid.h SOURCES += \ scene.cpp \ shape.cpp \ matpoint.cpp \ partsystem.cpp \ camera.cpp \ models.cpp diff --git a/symkit.pro b/symkit.pro index 4a5061e..2850650 100644 --- a/symkit.pro +++ b/symkit.pro @@ -1,10 +1,10 @@ TEMPLATE = subdirs SUBDIRS += \ symath \ symgraph \ alpha symgraph.depends = symath -alpha.depends = symgraph +alpha.depends = symgraph symath