From 9d3741b5bca2a8ab90de2244ca6bbde878c71217 Mon Sep 17 00:00:00 2001 From: Anselme Date: Fri, 3 Jun 2016 10:03:51 +0200 Subject: [PATCH 1/5] added missing memory liberations --- src/pixelpipeline.cpp | 11 ++++++++--- src/simulation.cpp | 7 ++++++- src/simulation.h | 1 + 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/pixelpipeline.cpp b/src/pixelpipeline.cpp index a7d2194..3cc34c6 100644 --- a/src/pixelpipeline.cpp +++ b/src/pixelpipeline.cpp @@ -72,14 +72,19 @@ PixelPipeline::PixelPipeline(MapScene *map) : PixelPipeline::~PixelPipeline() { - delete m_mapFBO; - delete m_mapTex; + // shaders delete m_texMapShader; delete m_renderShader; - delete m_map; delete m_skyboxShader; + // textures + delete m_mapTex; delete m_skyTexBack; delete m_skyTexFront; + // framebuffers + delete m_mapFBO; + // meshes + delete m_toreiller; + delete m_map; } void PixelPipeline::updateChanges() diff --git a/src/simulation.cpp b/src/simulation.cpp index ab83cd2..1a15961 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -23,6 +23,12 @@ Simulation::Simulation(MapScene *_map, std::vector &_behaviors } } +Simulation::~Simulation() +{ + for(Dude* d : m_dudes) + delete d; +} + void Simulation::update() { std::random_shuffle(m_dudes.begin(), m_dudes.end()); @@ -55,7 +61,6 @@ void Simulation::handleAction(Dude *dude) // initialisation Coord currentPos(dude->getPos()); Coord targetPos = p_map->toreillerLoop(currentPos + Coord(action.dir)); - Pixel &source = p_map->getPixel(currentPos); Pixel &target = p_map->getPixel(targetPos); dude->setSuccess(false); diff --git a/src/simulation.h b/src/simulation.h index 804fbd5..75650d8 100644 --- a/src/simulation.h +++ b/src/simulation.h @@ -15,6 +15,7 @@ private: std::vector m_teams; public: Simulation(MapScene *_map, std::vector &_behaviors); + ~Simulation(); MapScene* getMap() { return (MapScene*) p_map; } int getPopulation(){return m_dudes.size();} From 0b8a6293097a20d3d5282200584f0440b092a08a Mon Sep 17 00:00:00 2001 From: HatjigeorgiouDimitri Date: Fri, 3 Jun 2016 10:10:46 +0200 Subject: [PATCH 2/5] now use unordered_map in MapScene for better performance --- src/coord.h | 9 +++++++++ src/mapscene.cpp | 1 + src/mapscene.h | 4 ++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/coord.h b/src/coord.h index 1ae4fca..63b0b2f 100644 --- a/src/coord.h +++ b/src/coord.h @@ -33,6 +33,8 @@ struct Coord default : x= 0; y= 0; break; } } + bool operator==(const Coord &c) const + { return x==c.x&&y==c.y;} Coord operator+(const Coord &c) const { return Coord(x+c.x, y+c.y);} Coord& operator+=(const Coord &c) @@ -69,6 +71,13 @@ struct Coord { return dist(*this, c); } int dist(int x, int y) const { return dist(*this, Coord(x, y)); } + +}; + +struct CoordHash{ + size_t operator()(const Coord& val) const{ + return val.x+10000*val.y; + } }; #endif // COORD_H diff --git a/src/mapscene.cpp b/src/mapscene.cpp index 2cccbc1..6c727e2 100644 --- a/src/mapscene.cpp +++ b/src/mapscene.cpp @@ -104,5 +104,6 @@ void MapScene::draw() // clearing temporary data structures m_updatedCoordMap.clear(); + m_updatedCoordMap.rehash(0); m_pixels.clear(); } diff --git a/src/mapscene.h b/src/mapscene.h index 457f882..e006eec 100644 --- a/src/mapscene.h +++ b/src/mapscene.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include struct MapScene : public Map, public BasicScene { @@ -48,7 +48,7 @@ private: Pix(glm::vec2 p, glm::vec3 c) : pos(p), color(c) {} }; std::vector m_pixels; - std::map m_updatedCoordMap; + std::unordered_map m_updatedCoordMap; }; #endif // MAPSCENE_H From 10a627ddb381fcf743dd9a48997c362453727ba8 Mon Sep 17 00:00:00 2001 From: HatjigeorgiouDimitri Date: Fri, 3 Jun 2016 11:04:27 +0200 Subject: [PATCH 3/5] some compilations errors fixed --- generators/image.h | 2 +- generators/realWorld.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/generators/image.h b/generators/image.h index 2013f2f..3a50da0 100644 --- a/generators/image.h +++ b/generators/image.h @@ -11,7 +11,7 @@ pixel[1] = header_data_cmap[(unsigned char)data[0]][1]; \ pixel[2] = header_data_cmap[(unsigned char)data[0]][2]; \ data ++; } -static char header_data_cmap[256][3] = { +static unsigned char header_data_cmap[256][3] = { { 0, 0, 0}, {255,255,255}, {255,255,255}, diff --git a/generators/realWorld.cpp b/generators/realWorld.cpp index 1df4a96..da61c15 100644 --- a/generators/realWorld.cpp +++ b/generators/realWorld.cpp @@ -3,10 +3,10 @@ #include "image.h" // windows MinGW -// g++ -shared realWorld.cpp -o realWorld.dll -I../include +// g++ -shared realWorld.cpp -o realWorld.dll -I../src -funsigned-char -std=c++11 // linux gcc -// g++ -shared realWorld.cpp -o realWorld.so -I../include -fPIC +// g++ -shared realWorld.cpp -o realWorld.so -I../src -fPIC -funsigned-char -std=c++11 extern "C" void generate(Map *mapPtr) { From 6aa2c5686f56cc89b5de9c5f156737939709068c Mon Sep 17 00:00:00 2001 From: Anselme Date: Fri, 3 Jun 2016 11:27:21 +0200 Subject: [PATCH 4/5] fixed flat / sphere scaling and mouse move input scaling --- shaders/world.frag.glsl | 1 + shaders/world.vert.glsl | 2 +- src/mainwindow.cpp | 7 +++++++ src/mainwindow.h | 1 + src/mainwindow.ui | 19 +++++++++++++------ src/pixelpipeline.cpp | 16 +++++++++++++--- src/pixelpipeline.h | 5 ++++- 7 files changed, 40 insertions(+), 11 deletions(-) diff --git a/shaders/world.frag.glsl b/shaders/world.frag.glsl index d41e1d8..a99feda 100644 --- a/shaders/world.frag.glsl +++ b/shaders/world.frag.glsl @@ -24,6 +24,7 @@ vec3 phongLighting(in vec3 kd, in vec3 ks, in float ns, in vec3 color, in vec3 n void main() { // looping the world in the shape of a toreiller (infinite flat surface) + //float zoomRatio = mix(1, 1/camera.z, flatSphere); vec2 worldCoord = texCoord*worldSize + camera.xy*worldSize.y + (texCoord-vec2(0.25, 0.5))*surfaceRatio*worldSize; ivec2 nbRevolutions = ivec2(floor(worldCoord / worldSize)); if(abs(mod(nbRevolutions.y, 2)) > 0.5) diff --git a/shaders/world.vert.glsl b/shaders/world.vert.glsl index db0eaec..9665a8d 100644 --- a/shaders/world.vert.glsl +++ b/shaders/world.vert.glsl @@ -18,7 +18,7 @@ void main(void) vec2 flatTexCoord = texCoord; texCoord = mix(texCoord, flatTexCoord, vec2(flatSphere)); normal = inNormal.xyz; - vec4 flatPos = vec4(inTexCoord.x*2 -1, inTexCoord.y*2 -1, 0.0, 1.0); + vec4 flatPos = vec4((inTexCoord.x*2 -1)*camera.z, (inTexCoord.y*2 -1)*camera.z, 0.0, 1.0); vec3 position = vec3(inPosition.xy, inPosition.z - 1); vec4 projPos = mvp * vec4(position*camera.z, 1.0); gl_Position = mix(projPos, flatPos, vec4(flatSphere)); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 4f2782f..6ed36f1 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -30,6 +30,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(ui->actionPlayPause, SIGNAL(toggled(bool)), this, SLOT(pauseSimu(bool))); connect(ui->actionAction_step, SIGNAL(triggered(bool)), this, SLOT(stepSimu())); connect(ui->actionStop, SIGNAL(triggered(bool)), this, SLOT(stopSimu())); + connect(ui->resetToDefaultAdvanced, SIGNAL(pressed()), this, SLOT(resetAdvancedToDefault())); changeSimSpeed(ui->simSpeedSlider->value()); ui->advancedGroupBox->hide(); } @@ -100,6 +101,12 @@ void MainWindow::stopSimu() delete p_simu; } +void MainWindow::resetAdvancedToDefault() +{ + ui->flatSphereSlider->setValue(0); + ui->surfaceRatioSlider->setValue(50); +} + void MainWindow::pauseSimu(bool pause) { m_paused = pause; diff --git a/src/mainwindow.h b/src/mainwindow.h index faeccd8..5a94c57 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -32,6 +32,7 @@ private slots: void updateSimu(); void stepSimu(); void stopSimu(); + void resetAdvancedToDefault(); void pauseSimu(bool); }; diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 08b94e0..9ec00bf 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -6,8 +6,8 @@ 0 0 - 744 - 490 + 683 + 499 @@ -190,14 +190,14 @@ 5 - + Spherical / Flat - + true @@ -225,14 +225,14 @@ - + surface ratio - + true @@ -263,6 +263,13 @@ + + + + Reset to default + + + diff --git a/src/pixelpipeline.cpp b/src/pixelpipeline.cpp index 3cc34c6..87f4b40 100644 --- a/src/pixelpipeline.cpp +++ b/src/pixelpipeline.cpp @@ -8,7 +8,6 @@ #include "mapscene.h" #include #include - #define SCROLL_SPEED 0.998f class ToreillerGenerator : public MeshGenerator @@ -150,6 +149,12 @@ void PixelPipeline::resizeGL(int w, int h) m_view = glm::translate(glm::mat4(), glm::vec3(0, 0, -3)); } +void PixelPipeline::cameraMove(int x, int y) +{ + m_camera.x -= 2*x/(m_camera.z*m_width); + m_camera.y += 2*y/(m_camera.z*m_height); +} + void PixelPipeline::cameraZoom(int nbScrolls) { while(nbScrolls != 0) @@ -167,6 +172,11 @@ void PixelPipeline::cameraZoom(int nbScrolls) } if(m_camera.z > m_mapHeight/4) m_camera.z = m_mapHeight/4; - else if(m_camera.z < 0.4f) - m_camera.z = 0.4f; + else if(m_camera.z < 1) + m_camera.z = 1; +} + +Coord PixelPipeline::getToreillerPos(int mouseX, int mouseY) +{ + } diff --git a/src/pixelpipeline.h b/src/pixelpipeline.h index 36bff88..99fb009 100644 --- a/src/pixelpipeline.h +++ b/src/pixelpipeline.h @@ -10,6 +10,7 @@ class Shader; class PixelMesh; class MapScene; class Mesh; +class Coord; class PixelPipeline : public Pipeline { @@ -46,12 +47,14 @@ public: virtual void renderGL(Scene *scene); virtual void resizeGL(int w, int h); - void cameraMove(int x, int y) { m_camera.x -= x/(m_camera.z*m_width); m_camera.y += y/(m_camera.z*m_width); } + void cameraMove(int x, int y); void cameraZoom(int nbScrolls); void setSurfaceRatio(float surfaceRatio) { m_surfaceRatio = surfaceRatio; } void setFlatSphere(float flatSphere) { m_flatSphere = flatSphere; } void setShowToreiller(bool showToreiller) { m_showToreiller = showToreiller; } + + Coord getToreillerPos(int mouseX, int mouseY); }; #endif // PIXELPIPELINE_H From 1d658f89452d5542cc8158ffb27300275862b90f Mon Sep 17 00:00:00 2001 From: Anselme Date: Fri, 3 Jun 2016 16:45:38 +0200 Subject: [PATCH 5/5] implemented picking for flat display, and fixed some bugs --- shaders/skybox.frag.glsl | 1 - shaders/skybox.vert.glsl | 1 - shaders/world.frag.glsl | 2 -- shaders/world.vert.glsl | 1 - src/drawwidget.cpp | 18 +++++++++++++++--- src/drawwidget.h | 3 ++- src/mainwindow.cpp | 2 ++ src/map.h | 4 ++++ src/pixelpipeline.cpp | 10 ++++++---- 9 files changed, 29 insertions(+), 13 deletions(-) diff --git a/shaders/skybox.frag.glsl b/shaders/skybox.frag.glsl index 67fb03f..869ee92 100644 --- a/shaders/skybox.frag.glsl +++ b/shaders/skybox.frag.glsl @@ -5,7 +5,6 @@ uniform sampler2D backMap; uniform vec3 camera; uniform vec2 worldSize; -uniform vec2 screenSize; uniform float flatSphere; uniform float surfaceRatio; diff --git a/shaders/skybox.vert.glsl b/shaders/skybox.vert.glsl index 5bd6a27..f097c24 100644 --- a/shaders/skybox.vert.glsl +++ b/shaders/skybox.vert.glsl @@ -5,7 +5,6 @@ layout(location = 2)in vec2 inTexCoord; layout(location = 1)in vec3 inNormal; uniform float flatSphere; -uniform vec2 screenSize; uniform mat4 mvp; uniform vec3 camera; diff --git a/shaders/world.frag.glsl b/shaders/world.frag.glsl index a99feda..9e2ff42 100644 --- a/shaders/world.frag.glsl +++ b/shaders/world.frag.glsl @@ -4,7 +4,6 @@ uniform sampler2DRect colorMap; uniform vec3 camera; uniform vec2 worldSize; -uniform vec2 screenSize; uniform float flatSphere; uniform float surfaceRatio; @@ -24,7 +23,6 @@ vec3 phongLighting(in vec3 kd, in vec3 ks, in float ns, in vec3 color, in vec3 n void main() { // looping the world in the shape of a toreiller (infinite flat surface) - //float zoomRatio = mix(1, 1/camera.z, flatSphere); vec2 worldCoord = texCoord*worldSize + camera.xy*worldSize.y + (texCoord-vec2(0.25, 0.5))*surfaceRatio*worldSize; ivec2 nbRevolutions = ivec2(floor(worldCoord / worldSize)); if(abs(mod(nbRevolutions.y, 2)) > 0.5) diff --git a/shaders/world.vert.glsl b/shaders/world.vert.glsl index 9665a8d..6208599 100644 --- a/shaders/world.vert.glsl +++ b/shaders/world.vert.glsl @@ -5,7 +5,6 @@ layout(location = 2)in vec2 inTexCoord; layout(location = 1)in vec3 inNormal; uniform float flatSphere; -uniform vec2 screenSize; uniform mat4 mvp; uniform vec3 camera; diff --git a/src/drawwidget.cpp b/src/drawwidget.cpp index 9d53182..acd68c3 100644 --- a/src/drawwidget.cpp +++ b/src/drawwidget.cpp @@ -13,6 +13,8 @@ DrawWidget::DrawWidget(QWidget *parent) : QOpenGLWidget(parent), + m_grabbedMouseLeft(false), + m_grabbedMouseRight(false), m_Qt_fbo(NULL), m_pipeline(NULL) { @@ -94,11 +96,15 @@ void DrawWidget::stopSimulation() void DrawWidget::mouseMoveEvent(QMouseEvent *event) { - if(grabbedMouse && m_pipeline != NULL) + if(m_grabbedMouseLeft && m_pipeline != NULL) { m_pipeline->cameraMove(event->globalX() - lastMousePos.x(), event->globalY() - lastMousePos.y()); repaint(); } + if(m_grabbedMouseRight && m_pipeline != NULL) + { + m_pipeline->getToreillerPos(event->x(), event->y()); + } lastMousePos = event->globalPos(); } @@ -107,7 +113,10 @@ void DrawWidget::mousePressEvent(QMouseEvent* event) switch(event->button()) { case Qt::LeftButton : - grabbedMouse = true; + m_grabbedMouseLeft = true; + break; + case Qt::RightButton : + m_grabbedMouseRight = true; break; default: break; @@ -125,7 +134,10 @@ void DrawWidget::mouseReleaseEvent(QMouseEvent* event) switch(event->button()) { case Qt::LeftButton : - grabbedMouse = false; + m_grabbedMouseLeft = false; + break; + case Qt::RightButton : + m_grabbedMouseRight = false; break; default: break; diff --git a/src/drawwidget.h b/src/drawwidget.h index 7da88d1..f1b524f 100644 --- a/src/drawwidget.h +++ b/src/drawwidget.h @@ -21,7 +21,8 @@ class DrawWidget : public QOpenGLWidget // camera handling variables QPoint lastMousePos; - bool grabbedMouse; + bool m_grabbedMouseLeft; + bool m_grabbedMouseRight; MapScene *m_map; MapScene *m_dummyMap; FrameBuffer *m_Qt_fbo; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 6ed36f1..61cf92d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -76,6 +76,8 @@ void MainWindow::updateSimu() ui->dateLabel->setText(QString::number(++m_date)); ui->populationLabel->setText(QString::number(p_simu->getPopulation())); ui->drawWidget->updateDudesBehavior(); + ui->drawWidget->setFlatSphere(ui->flatSphereSlider->value()); + ui->drawWidget->setSurfaceRatio(ui->surfaceRatioSlider->value()); ui->drawWidget->repaint(); } if(m_simSpeedChanged) diff --git a/src/map.h b/src/map.h index 44f4dc0..edc7a60 100644 --- a/src/map.h +++ b/src/map.h @@ -66,6 +66,10 @@ public: Coord &toreillerLoop(Coord &c) { + while(c.x < 0) + c.x += m_width; + while(c.y < 0) + c.y += m_width; // this is the shader implementation of the toreiller Coord nbRevolutions(c.x/m_width, c.y/m_height); if(std::abs(nbRevolutions.y % 2)) diff --git a/src/pixelpipeline.cpp b/src/pixelpipeline.cpp index 87f4b40..4884bc3 100644 --- a/src/pixelpipeline.cpp +++ b/src/pixelpipeline.cpp @@ -36,7 +36,7 @@ PixelPipeline::PixelPipeline(MapScene *map) : { m_width = 256; m_height = 256; - m_surfaceRatio = 1; + m_surfaceRatio = 0; m_flatSphere = 0; m_map->setPipeline(this); m_targetFBO = FrameBuffer::screen; @@ -112,7 +112,6 @@ void PixelPipeline::renderGL(Scene *scene) m_skyboxShader->bind(); m_skyboxShader->bindVec3(m_skyboxShader->getLocation("camera"), m_camera); m_renderShader->bindVec2(m_skyboxShader->getLocation("worldSize"), glm::vec2(m_mapWidth, m_mapHeight)); - m_skyboxShader->bindVec2(m_skyboxShader->getLocation("screenSize"), glm::vec2(m_width, m_height)); m_skyboxShader->bindFloat(m_skyboxShader->getLocation("surfaceRatio"), m_surfaceRatio); m_skyboxShader->bindFloat(m_skyboxShader->getLocation("flatSphere"), m_flatSphere); m_skyTexFront->bind(0); @@ -129,7 +128,6 @@ void PixelPipeline::renderGL(Scene *scene) m_renderShader->bind(); m_renderShader->bindVec3(m_renderShader->getLocation("camera"), m_camera); m_renderShader->bindVec2(m_renderShader->getLocation("worldSize"), glm::vec2(m_mapWidth, m_mapHeight)); - m_renderShader->bindVec2(m_renderShader->getLocation("screenSize"), glm::vec2(m_width, m_height)); m_renderShader->bindFloat(m_renderShader->getLocation("surfaceRatio"), m_surfaceRatio); m_renderShader->bindFloat(m_renderShader->getLocation("flatSphere"), m_flatSphere); m_mapTex->bind(0); @@ -178,5 +176,9 @@ void PixelPipeline::cameraZoom(int nbScrolls) Coord PixelPipeline::getToreillerPos(int mouseX, int mouseY) { - + glm::vec2 pos(mouseX, mouseY); + pos /= glm::vec2(m_width, m_height); // part that depends on resolution + pos = (pos - glm::vec2(0.5f))*m_mapHeight/m_camera.z; // part that depends on camera zoom + pos += glm::vec2(m_camera.x + 0.5f, -m_camera.y + 0.5f)*m_mapHeight; // part that depends on camera offset + return Coord(pos.x, pos.y); }