From 1d658f89452d5542cc8158ffb27300275862b90f Mon Sep 17 00:00:00 2001 From: Anselme Date: Fri, 3 Jun 2016 16:45:38 +0200 Subject: [PATCH] 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); }