From e2178df9ead828c2f653c281bdeedd768125f4b3 Mon Sep 17 00:00:00 2001 From: Anselme Date: Fri, 27 May 2016 17:24:06 +0200 Subject: [PATCH] added limits to the zoom scrolling, linked the slides to their features --- shaders/world.frag.glsl | 6 ++++-- shaders/world.vert.glsl | 8 +++++++- src/drawwidget.cpp | 13 +++++++++++++ src/drawwidget.h | 2 ++ src/mainwindow.cpp | 4 ++++ src/mainwindow.ui | 42 ++++++++++++++++++++++++++++++++++++++--- src/pixelpipeline.cpp | 28 +++++++++++++++++---------- src/pixelpipeline.h | 6 +++++- 8 files changed, 92 insertions(+), 17 deletions(-) diff --git a/shaders/world.frag.glsl b/shaders/world.frag.glsl index 84b6ef7..dc94763 100644 --- a/shaders/world.frag.glsl +++ b/shaders/world.frag.glsl @@ -5,6 +5,8 @@ uniform sampler2DRect colorMap; uniform vec3 camera; uniform vec2 worldSize; uniform vec2 screenSize; +uniform float flatSphere; +uniform float surfaceRatio; in vec2 texCoord; in vec3 normal; @@ -22,7 +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) - vec2 worldCoord = texCoord*worldSize + camera.xy*worldSize.y; + 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) { @@ -34,5 +36,5 @@ void main() // computing lighting vec3 lighting = phongLighting(texColor, vec3(0.5), 50, vec3(1), normalize(normal), lightDir, normalize(lightDir+vec3(0, 0, -1))); - outColor = vec4(texColor*0.2 + 0.8*lighting, 1.0); + outColor = vec4(mix(lighting, texColor, flatSphere*0.8 + 0.2), 1.0); } diff --git a/shaders/world.vert.glsl b/shaders/world.vert.glsl index 771c0c4..9699638 100644 --- a/shaders/world.vert.glsl +++ b/shaders/world.vert.glsl @@ -4,6 +4,8 @@ layout(location = 0)in vec3 inPosition; layout(location = 2)in vec2 inTexCoord; layout(location = 1)in vec3 inNormal; +uniform float flatSphere; +uniform vec2 screenSize; uniform mat4 mvp; uniform vec3 camera; @@ -13,6 +15,10 @@ out vec3 normal; void main(void) { texCoord = vec2(inTexCoord.x/2, inTexCoord.y); + vec2 flatTexCoord = texCoord; + texCoord = mix(texCoord, flatTexCoord, vec2(flatSphere)); normal = inNormal.xyz; - gl_Position = mvp * vec4(inPosition*camera.z, 1.0); + vec4 flatPos = vec4(inTexCoord.x*2 -1, inTexCoord.y*2 -1, 0.0, 1.0); + vec4 projPos = mvp * vec4(inPosition*camera.z, 1.0); + gl_Position = mix(projPos, flatPos, vec4(flatSphere)); } diff --git a/src/drawwidget.cpp b/src/drawwidget.cpp index 44c02bd..3bc8376 100644 --- a/src/drawwidget.cpp +++ b/src/drawwidget.cpp @@ -130,3 +130,16 @@ void DrawWidget::wheelEvent(QWheelEvent *event) repaint(); } } + +void DrawWidget::setFlatSphere(int value) +{ + if(m_pipeline != NULL) + m_pipeline->setFlatSphere(float(value)/100); +} + +void DrawWidget::setSurfaceRatio(int value) +{ + float fVal = float(value-50); + if(m_pipeline != NULL) + m_pipeline->setSurfaceRatio(fVal > 0 ? fVal/25 : fVal/60); +} diff --git a/src/drawwidget.h b/src/drawwidget.h index bc07924..c43ef2b 100644 --- a/src/drawwidget.h +++ b/src/drawwidget.h @@ -51,6 +51,8 @@ class DrawWidget : public QOpenGLWidget public slots: void updateDudesBehavior(); + void setFlatSphere(int); + void setSurfaceRatio(int); }; #endif // DRAWWIDGET_H diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index e3b99a0..dd27c10 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -21,6 +21,10 @@ MainWindow::MainWindow(QWidget *parent) : m_simuTimer->start(m_simSpeed); connect(ui->startButton, SIGNAL(pressed()), this, SLOT(openSimuDialog())); connect(ui->simSpeedSlider, SIGNAL(valueChanged(int)), this, SLOT(changeSimSpeed(int))); + connect(ui->flatSphereSlider, SIGNAL(valueChanged(int)), ui->drawWidget, SLOT(setFlatSphere(int))); + connect(ui->surfaceRatioSlider, SIGNAL(valueChanged(int)), ui->drawWidget, SLOT(setSurfaceRatio(int))); + connect(ui->flatSphereSlider, SIGNAL(valueChanged(int)), ui->drawWidget, SLOT(repaint())); + connect(ui->surfaceRatioSlider, SIGNAL(valueChanged(int)), ui->drawWidget, SLOT(repaint())); connect(ui->pauseButton, SIGNAL(toggled(bool)), this, SLOT(pauseSimu(bool))); } diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 5f678de..62c0f3b 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -78,7 +78,7 @@ - 1 + 0 @@ -103,11 +103,26 @@ - false + true + + + 100 + + + 2 + + + 2 Qt::Horizontal + + QSlider::TicksBelow + + + 10 + @@ -120,11 +135,32 @@ - false + true + + + 0 + + + 100 + + + 10 + + + 10 + + + 50 Qt::Horizontal + + QSlider::TicksBelow + + + 10 + diff --git a/src/pixelpipeline.cpp b/src/pixelpipeline.cpp index 516e082..045fa5a 100644 --- a/src/pixelpipeline.cpp +++ b/src/pixelpipeline.cpp @@ -36,6 +36,8 @@ PixelPipeline::PixelPipeline(MapScene *map) : { m_width = 256; m_height = 256; + m_surfaceRatio = 1; + m_flatSphere = 0; m_map->setPipeline(this); m_targetFBO = FrameBuffer::screen; m_mapFBO = new FrameBuffer(); @@ -94,6 +96,8 @@ void PixelPipeline::renderGL(Scene *scene) 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); m_renderShader->bindInteger(m_renderShader->getLocation("colorMap"), 0); @@ -114,16 +118,20 @@ void PixelPipeline::resizeGL(int w, int h) void PixelPipeline::cameraZoom(int nbScrolls) { while(nbScrolls != 0) + { + if(nbScrolls > 0) { - if(nbScrolls > 0) - { - m_camera.z *= SCROLL_SPEED; - --nbScrolls; - } - else - { - m_camera.z /= SCROLL_SPEED; - ++nbScrolls; - } + m_camera.z *= SCROLL_SPEED; + --nbScrolls; } + else + { + m_camera.z /= SCROLL_SPEED; + ++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; } diff --git a/src/pixelpipeline.h b/src/pixelpipeline.h index 8eb623a..e523d1c 100644 --- a/src/pixelpipeline.h +++ b/src/pixelpipeline.h @@ -24,11 +24,12 @@ private: Shader *m_renderShader; int m_width; int m_height; + float m_surfaceRatio; + float m_flatSphere; glm::vec3 m_camera; Mesh *m_toreiller; glm::mat4 m_mvp; - //unsigned int m_vao, m_vbo; public: PixelPipeline(MapScene *map); @@ -42,6 +43,9 @@ public: 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 cameraZoom(int nbScrolls); + + void setSurfaceRatio(float surfaceRatio) { m_surfaceRatio = surfaceRatio; } + void setFlatSphere(float flatSphere) { m_flatSphere = flatSphere; } }; #endif // PIXELPIPELINE_H