implemented picking for flat display, and fixed some bugs

This commit is contained in:
Anselme 2016-06-03 16:45:38 +02:00
parent 6aa2c5686f
commit 1d658f8945
9 changed files with 29 additions and 13 deletions

View File

@ -5,7 +5,6 @@ uniform sampler2D backMap;
uniform vec3 camera; uniform vec3 camera;
uniform vec2 worldSize; uniform vec2 worldSize;
uniform vec2 screenSize;
uniform float flatSphere; uniform float flatSphere;
uniform float surfaceRatio; uniform float surfaceRatio;

View File

@ -5,7 +5,6 @@ layout(location = 2)in vec2 inTexCoord;
layout(location = 1)in vec3 inNormal; layout(location = 1)in vec3 inNormal;
uniform float flatSphere; uniform float flatSphere;
uniform vec2 screenSize;
uniform mat4 mvp; uniform mat4 mvp;
uniform vec3 camera; uniform vec3 camera;

View File

@ -4,7 +4,6 @@ uniform sampler2DRect colorMap;
uniform vec3 camera; uniform vec3 camera;
uniform vec2 worldSize; uniform vec2 worldSize;
uniform vec2 screenSize;
uniform float flatSphere; uniform float flatSphere;
uniform float surfaceRatio; 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() void main()
{ {
// looping the world in the shape of a toreiller (infinite flat surface) // 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; vec2 worldCoord = texCoord*worldSize + camera.xy*worldSize.y + (texCoord-vec2(0.25, 0.5))*surfaceRatio*worldSize;
ivec2 nbRevolutions = ivec2(floor(worldCoord / worldSize)); ivec2 nbRevolutions = ivec2(floor(worldCoord / worldSize));
if(abs(mod(nbRevolutions.y, 2)) > 0.5) if(abs(mod(nbRevolutions.y, 2)) > 0.5)

View File

@ -5,7 +5,6 @@ layout(location = 2)in vec2 inTexCoord;
layout(location = 1)in vec3 inNormal; layout(location = 1)in vec3 inNormal;
uniform float flatSphere; uniform float flatSphere;
uniform vec2 screenSize;
uniform mat4 mvp; uniform mat4 mvp;
uniform vec3 camera; uniform vec3 camera;

View File

@ -13,6 +13,8 @@
DrawWidget::DrawWidget(QWidget *parent) : DrawWidget::DrawWidget(QWidget *parent) :
QOpenGLWidget(parent), QOpenGLWidget(parent),
m_grabbedMouseLeft(false),
m_grabbedMouseRight(false),
m_Qt_fbo(NULL), m_Qt_fbo(NULL),
m_pipeline(NULL) m_pipeline(NULL)
{ {
@ -94,11 +96,15 @@ void DrawWidget::stopSimulation()
void DrawWidget::mouseMoveEvent(QMouseEvent *event) 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()); m_pipeline->cameraMove(event->globalX() - lastMousePos.x(), event->globalY() - lastMousePos.y());
repaint(); repaint();
} }
if(m_grabbedMouseRight && m_pipeline != NULL)
{
m_pipeline->getToreillerPos(event->x(), event->y());
}
lastMousePos = event->globalPos(); lastMousePos = event->globalPos();
} }
@ -107,7 +113,10 @@ void DrawWidget::mousePressEvent(QMouseEvent* event)
switch(event->button()) switch(event->button())
{ {
case Qt::LeftButton : case Qt::LeftButton :
grabbedMouse = true; m_grabbedMouseLeft = true;
break;
case Qt::RightButton :
m_grabbedMouseRight = true;
break; break;
default: default:
break; break;
@ -125,7 +134,10 @@ void DrawWidget::mouseReleaseEvent(QMouseEvent* event)
switch(event->button()) switch(event->button())
{ {
case Qt::LeftButton : case Qt::LeftButton :
grabbedMouse = false; m_grabbedMouseLeft = false;
break;
case Qt::RightButton :
m_grabbedMouseRight = false;
break; break;
default: default:
break; break;

View File

@ -21,7 +21,8 @@ class DrawWidget : public QOpenGLWidget
// camera handling variables // camera handling variables
QPoint lastMousePos; QPoint lastMousePos;
bool grabbedMouse; bool m_grabbedMouseLeft;
bool m_grabbedMouseRight;
MapScene *m_map; MapScene *m_map;
MapScene *m_dummyMap; MapScene *m_dummyMap;
FrameBuffer *m_Qt_fbo; FrameBuffer *m_Qt_fbo;

View File

@ -76,6 +76,8 @@ void MainWindow::updateSimu()
ui->dateLabel->setText(QString::number(++m_date)); ui->dateLabel->setText(QString::number(++m_date));
ui->populationLabel->setText(QString::number(p_simu->getPopulation())); ui->populationLabel->setText(QString::number(p_simu->getPopulation()));
ui->drawWidget->updateDudesBehavior(); ui->drawWidget->updateDudesBehavior();
ui->drawWidget->setFlatSphere(ui->flatSphereSlider->value());
ui->drawWidget->setSurfaceRatio(ui->surfaceRatioSlider->value());
ui->drawWidget->repaint(); ui->drawWidget->repaint();
} }
if(m_simSpeedChanged) if(m_simSpeedChanged)

View File

@ -66,6 +66,10 @@ public:
Coord &toreillerLoop(Coord &c) 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 // this is the shader implementation of the toreiller
Coord nbRevolutions(c.x/m_width, c.y/m_height); Coord nbRevolutions(c.x/m_width, c.y/m_height);
if(std::abs(nbRevolutions.y % 2)) if(std::abs(nbRevolutions.y % 2))

View File

@ -36,7 +36,7 @@ PixelPipeline::PixelPipeline(MapScene *map) :
{ {
m_width = 256; m_width = 256;
m_height = 256; m_height = 256;
m_surfaceRatio = 1; m_surfaceRatio = 0;
m_flatSphere = 0; m_flatSphere = 0;
m_map->setPipeline(this); m_map->setPipeline(this);
m_targetFBO = FrameBuffer::screen; m_targetFBO = FrameBuffer::screen;
@ -112,7 +112,6 @@ void PixelPipeline::renderGL(Scene *scene)
m_skyboxShader->bind(); m_skyboxShader->bind();
m_skyboxShader->bindVec3(m_skyboxShader->getLocation("camera"), m_camera); m_skyboxShader->bindVec3(m_skyboxShader->getLocation("camera"), m_camera);
m_renderShader->bindVec2(m_skyboxShader->getLocation("worldSize"), glm::vec2(m_mapWidth, m_mapHeight)); 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("surfaceRatio"), m_surfaceRatio);
m_skyboxShader->bindFloat(m_skyboxShader->getLocation("flatSphere"), m_flatSphere); m_skyboxShader->bindFloat(m_skyboxShader->getLocation("flatSphere"), m_flatSphere);
m_skyTexFront->bind(0); m_skyTexFront->bind(0);
@ -129,7 +128,6 @@ void PixelPipeline::renderGL(Scene *scene)
m_renderShader->bind(); m_renderShader->bind();
m_renderShader->bindVec3(m_renderShader->getLocation("camera"), m_camera); 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("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("surfaceRatio"), m_surfaceRatio);
m_renderShader->bindFloat(m_renderShader->getLocation("flatSphere"), m_flatSphere); m_renderShader->bindFloat(m_renderShader->getLocation("flatSphere"), m_flatSphere);
m_mapTex->bind(0); m_mapTex->bind(0);
@ -178,5 +176,9 @@ void PixelPipeline::cameraZoom(int nbScrolls)
Coord PixelPipeline::getToreillerPos(int mouseX, int mouseY) 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);
} }