From 4e9ed2481a1960a2fc4a31ecd14d1904b76cad2e Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Tue, 1 Jul 2014 15:47:07 +0300 Subject: Fix labels and grid lines changing size when aspect ratio is changed Since shaders needed fixing anyway for surface because of this, also implements the support for object gradients for surface, which was missing. Task-number: QTRD-2666 Task-number: QTRD-3211 Change-Id: I0c5da7fdfef308a96ec0bae4750fd22035da4e82 Reviewed-by: Mika Salmela --- .../doc/src/qtdatavisualization.qdoc | 1 - .../engine/abstract3drenderer.cpp | 25 +++-- .../engine/abstract3drenderer_p.h | 1 + src/datavisualization/engine/scatter3drenderer.cpp | 68 +++++++----- src/datavisualization/engine/scatter3drenderer_p.h | 2 +- src/datavisualization/engine/shaders/surface.frag | 4 +- .../engine/shaders/surfaceFlat.frag | 4 +- .../engine/shaders/surfaceShadowFlat.frag | 6 +- .../engine/shaders/surfaceShadowNoTex.frag | 6 +- .../engine/shaders/surface_ES2.frag | 4 +- src/datavisualization/engine/surface3drenderer.cpp | 68 ++++++++---- src/datavisualization/engine/surface3drenderer_p.h | 1 + src/datavisualization/utils/surfaceobject.cpp | 115 +++++++-------------- src/datavisualization/utils/surfaceobject_p.h | 6 ++ 14 files changed, 163 insertions(+), 148 deletions(-) (limited to 'src') diff --git a/src/datavisualization/doc/src/qtdatavisualization.qdoc b/src/datavisualization/doc/src/qtdatavisualization.qdoc index af419814..3726e735 100644 --- a/src/datavisualization/doc/src/qtdatavisualization.qdoc +++ b/src/datavisualization/doc/src/qtdatavisualization.qdoc @@ -330,7 +330,6 @@ \li Surfaces with non-straight rows and columns do not always render properly. \li Q3DLight class (and Light3D QML item) are currently not usable for anything. \li Changing any of Q3DScene properties affecting subviewports currently has no effect. - \li The color style Q3DTheme::ColorStyleObjectGradient doesn't work for surface graphs. \li Widget based examples layout incorrectly in iOS. \li Reparenting a graph to an item in another QQuickWindow is not supported. \li There is a low-impact binary break between 1.0 and 1.1. The break is due to a QML type diff --git a/src/datavisualization/engine/abstract3drenderer.cpp b/src/datavisualization/engine/abstract3drenderer.cpp index 782b1480..c2ed43da 100644 --- a/src/datavisualization/engine/abstract3drenderer.cpp +++ b/src/datavisualization/engine/abstract3drenderer.cpp @@ -69,6 +69,7 @@ Abstract3DRenderer::Abstract3DRenderer(Abstract3DController *controller) m_graphHorizontalAspectRatio(0.0f), m_polarGraph(false), m_radialLabelOffset(1.0f), + m_polarRadius(2.0f), m_xRightAngleRotation(QQuaternion::fromAxisAndAngle(1.0f, 0.0f, 0.0f, 90.0f)), m_yRightAngleRotation(QQuaternion::fromAxisAndAngle(0.0f, 1.0f, 0.0f, 90.0f)), m_zRightAngleRotation(QQuaternion::fromAxisAndAngle(0.0f, 0.0f, 1.0f, 90.0f)), @@ -313,8 +314,6 @@ void Abstract3DRenderer::updateSelectionMode(QAbstract3DGraph::SelectionFlags mo void Abstract3DRenderer::updateAspectRatio(float ratio) { m_graphAspectRatio = ratio; - calculateZoomLevel(); - m_cachedScene->activeCamera()->d_ptr->updateViewMatrix(m_autoScaleAdjustment); foreach (SeriesRenderCache *cache, m_renderCacheList) cache->setDataDirty(true); updateCustomItemPositions(); @@ -369,9 +368,9 @@ void Abstract3DRenderer::calculateZoomLevel() GLfloat div; GLfloat zoomAdjustment; div = qMin(m_primarySubViewport.width(), m_primarySubViewport.height()); - zoomAdjustment = 2.0f * defaultRatio + zoomAdjustment = defaultRatio * ((m_primarySubViewport.width() / div) - / (m_primarySubViewport.height() / div)) / m_graphAspectRatio; + / (m_primarySubViewport.height() / div)); m_autoScaleAdjustment = qMin(zoomAdjustment, 1.0f); // clamp to 1.0f } @@ -1206,8 +1205,8 @@ void Abstract3DRenderer::calculatePolarXZ(const QVector3D &dataPos, float &x, fl qreal radius = m_axisCacheZ.formatter()->positionAt(dataPos.z()); // Convert angle & radius to X and Z coords - x = float(radius * qSin(angle)) * m_graphAspectRatio; - z = -float(radius * qCos(angle)) * m_graphAspectRatio; + x = float(radius * qSin(angle)) * m_polarRadius; + z = -float(radius * qCos(angle)) * m_polarRadius; } void Abstract3DRenderer::drawRadialGrid(ShaderHelper *shader, float yFloorLinePos, @@ -1234,10 +1233,10 @@ void Abstract3DRenderer::drawRadialGrid(ShaderHelper *shader, float yFloorLinePo for (int i = 0; i < gridLineCount; i++) { float gridPosition = (i >= mainSize) ? subGridPositions.at(i - mainSize) : gridPositions.at(i); - float radiusFraction = m_graphAspectRatio * gridPosition; + float radiusFraction = m_polarRadius * gridPosition; QVector3D gridLineScaler(radiusFraction * float(qSin(polarGridHalfAngle)), gridLineWidth, gridLineWidth); - translateVector.setZ(gridPosition * m_graphAspectRatio); + translateVector.setZ(gridPosition * m_polarRadius); for (int j = 0; j < polarGridRoundness; j++) { QMatrix4x4 modelMatrix; QMatrix4x4 itModelMatrix; @@ -1275,7 +1274,7 @@ void Abstract3DRenderer::drawAngularGrid(ShaderHelper *shader, float yFloorLineP const QMatrix4x4 &projectionViewMatrix, const QMatrix4x4 &depthMatrix) { - float halfRatio((m_graphAspectRatio + (labelMargin / 2.0f)) / 2.0f); + float halfRatio((m_polarRadius + (labelMargin / 2.0f)) / 2.0f); QVector3D gridLineScaler(gridLineWidth, gridLineWidth, halfRatio); int gridLineCount = m_axisCacheX.gridLineCount(); const QVector &gridPositions = m_axisCacheX.formatter()->gridPositions(); @@ -1337,10 +1336,10 @@ float Abstract3DRenderer::calculatePolarBackgroundMargin() float actualLabelWidth = actualLabelHeight / labelSize.height() * labelSize.width(); float labelPosition = labelPositions.at(label); qreal angle = labelPosition * M_PI * 2.0; - float x = qAbs((m_graphAspectRatio + labelMargin) * float(qSin(angle))) - + actualLabelWidth - m_graphAspectRatio + labelMargin; - float z = qAbs(-(m_graphAspectRatio + labelMargin) * float(qCos(angle))) - + actualLabelHeight - m_graphAspectRatio + labelMargin; + float x = qAbs((m_polarRadius + labelMargin) * float(qSin(angle))) + + actualLabelWidth - m_polarRadius + labelMargin; + float z = qAbs(-(m_polarRadius + labelMargin) * float(qCos(angle))) + + actualLabelHeight - m_polarRadius + labelMargin; float neededMargin = qMax(x, z); maxNeededMargin = qMax(maxNeededMargin, neededMargin); } diff --git a/src/datavisualization/engine/abstract3drenderer_p.h b/src/datavisualization/engine/abstract3drenderer_p.h index f7fa748b..40c7db0a 100644 --- a/src/datavisualization/engine/abstract3drenderer_p.h +++ b/src/datavisualization/engine/abstract3drenderer_p.h @@ -260,6 +260,7 @@ protected: float m_graphHorizontalAspectRatio; bool m_polarGraph; float m_radialLabelOffset; + float m_polarRadius; QQuaternion m_xRightAngleRotation; QQuaternion m_yRightAngleRotation; diff --git a/src/datavisualization/engine/scatter3drenderer.cpp b/src/datavisualization/engine/scatter3drenderer.cpp index 56219339..76b955e8 100644 --- a/src/datavisualization/engine/scatter3drenderer.cpp +++ b/src/datavisualization/engine/scatter3drenderer.cpp @@ -57,8 +57,8 @@ Scatter3DRenderer::Scatter3DRenderer(Scatter3DController *controller) m_selectionDepthBuffer(0), m_shadowQualityToShader(100.0f), m_shadowQualityMultiplier(3), - m_heightNormalizer(1.0f), m_scaleX(0.0f), + m_scaleY(0.0f), m_scaleZ(0.0f), m_scaleXWithBackground(0.0f), m_scaleYWithBackground(0.0f), @@ -75,9 +75,6 @@ Scatter3DRenderer::Scatter3DRenderer(Scatter3DController *controller) m_haveUniformColorMeshSeries(false), m_haveGradientMeshSeries(false) { - m_axisCacheY.setScale(2.0f); - m_axisCacheY.setTranslate(-1.0f); - initializeOpenGLFunctions(); initializeOpenGL(); } @@ -707,6 +704,9 @@ void Scatter3DRenderer::drawScene(const GLuint defaultFboHandle) dotShader->bind(); } + float rangeGradientYScaler = 0.5f / m_scaleY; + float rangeGradientYScalerForPoints = rangeGradientYScaler * 100.0f; + foreach (SeriesRenderCache *baseCache, m_renderCacheList) { if (baseCache->isVisible()) { ScatterSeriesRenderCache *cache = @@ -808,7 +808,8 @@ void Scatter3DRenderer::drawScene(const GLuint defaultFboHandle) if (rangeGradientPoints) { // Drawing points with range gradient // Get color from gradient based on items y position converted to percent - int position = int(item.translation().y() * 50.0f) + 50; + int position = int(item.translation().y() * rangeGradientYScalerForPoints) + + 50; dotColor = Utils::vectorFromColor( cache->gradientImage().pixel(0, position)); } else { @@ -825,13 +826,12 @@ void Scatter3DRenderer::drawScene(const GLuint defaultFboHandle) else gradientTexture = cache->singleHighlightGradientTexture(); lightStrength = m_cachedTheme->highlightLightStrength(); - // Insert data to ScatterRenderItem - // We don't have ownership, so don't delete the previous one + // Save the reference to the item to be used in label drawing selectedItem = &item; dotSelectionFound = true; // Save selected item size (adjusted with font size) for selection label // positioning - selectedItemSize = itemSize + (m_cachedTheme->font().pointSizeF() / 500.0f); + selectedItemSize = itemSize + m_drawer->scaledFontSize() - 0.05f; } if (!drawingPoints) { @@ -846,7 +846,8 @@ void Scatter3DRenderer::drawScene(const GLuint defaultFboHandle) dotShader->setUniformValue(dotShader->color(), dotColor); } else if (colorStyle == Q3DTheme::ColorStyleRangeGradient) { dotShader->setUniformValue(dotShader->gradientMin(), - (item.translation().y() + 1.0f) / 2.0f); + (item.translation().y() + m_scaleY) + * rangeGradientYScaler); } #if !defined(QT_OPENGL_ES_2) if (m_cachedShadowQuality > QAbstract3DGraph::ShadowQualityNone) { @@ -925,12 +926,12 @@ void Scatter3DRenderer::drawScene(const GLuint defaultFboHandle) else gradientTexture = cache->singleHighlightGradientTexture(); GLfloat lightStrength = m_cachedTheme->highlightLightStrength(); - // Save the reference to the item to be used on label drawing + // Save the reference to the item to be used in label drawing selectedItem = &item; dotSelectionFound = true; // Save selected item size (adjusted with font size) for selection label // positioning - selectedItemSize = itemSize + (m_cachedTheme->font().pointSizeF() / 500.0f); + selectedItemSize = itemSize + m_drawer->scaledFontSize() - 0.05f; if (!drawingPoints) { // Set shader bindings @@ -944,7 +945,8 @@ void Scatter3DRenderer::drawScene(const GLuint defaultFboHandle) dotShader->setUniformValue(dotShader->color(), dotColor); } else if (colorStyle == Q3DTheme::ColorStyleRangeGradient) { dotShader->setUniformValue(dotShader->gradientMin(), - (item.translation().y() + 1.0f) / 2.0f); + (item.translation().y() + m_scaleY) + * rangeGradientYScaler); } if (!drawingPoints) { @@ -1575,7 +1577,7 @@ void Scatter3DRenderer::drawLabels(bool drawSelection, const Q3DCamera *activeCa if (m_polarGraph) { float direction = m_zFlipped ? -1.0f : 1.0f; labelTrans.setZ((m_axisCacheZ.formatter()->labelPositions().at(label) - * -m_graphAspectRatio + * -m_polarRadius + m_drawer->scaledFontSize() + gridLineWidth) * direction); } else { labelTrans.setZ(m_axisCacheZ.labelPosition(label)); @@ -1596,7 +1598,7 @@ void Scatter3DRenderer::drawLabels(bool drawSelection, const Q3DCamera *activeCa } if (!drawSelection && m_axisCacheZ.isTitleVisible()) { if (m_polarGraph) { - float titleZ = -m_graphAspectRatio / 2.0f; + float titleZ = -m_polarRadius / 2.0f; if (m_zFlipped) titleZ = -titleZ; labelTrans.setZ(titleZ); @@ -1730,8 +1732,8 @@ void Scatter3DRenderer::drawLabels(bool drawSelection, const Q3DCamera *activeCa continue; float labelPosition = labelPositions.at(label); qreal angle = labelPosition * M_PI * 2.0; - labelTrans.setX((m_graphAspectRatio + labelMargin) * float(qSin(angle))); - labelTrans.setZ(-(m_graphAspectRatio + labelMargin) * float(qCos(angle))); + labelTrans.setX((m_polarRadius + labelMargin) * float(qSin(angle))); + labelTrans.setZ(-(m_polarRadius + labelMargin) * float(qCos(angle))); // Alignment depends on label angular position, as well as flips Qt::AlignmentFlag vAlignment = Qt::AlignCenter; Qt::AlignmentFlag hAlignment = Qt::AlignCenter; @@ -1776,7 +1778,7 @@ void Scatter3DRenderer::drawLabels(bool drawSelection, const Q3DCamera *activeCa totalRotation *= m_zRightAngleRotationNeg; if (m_yFlippedForGrid) totalRotation *= QQuaternion::fromAxisAndAngle(0.0f, 0.0f, 1.0f, -180.0f); - labelTrans.setZ(-m_graphAspectRatio); + labelTrans.setZ(-m_polarRadius); radial = true; } drawAxisTitleX(labelRotation, labelTrans, totalRotation, m_dummyRenderItem, @@ -2022,8 +2024,6 @@ void Scatter3DRenderer::calculateSceneScalingFactors() m_hBackgroundMargin = qMax(m_hBackgroundMargin, polarMargin); } - m_heightNormalizer = GLfloat(m_axisCacheY.max() - m_axisCacheY.min()) / 2.0f; - float horizontalAspectRatio; if (m_polarGraph) horizontalAspectRatio = 1.0f; @@ -2039,19 +2039,31 @@ void Scatter3DRenderer::calculateSceneScalingFactors() areaSize.setWidth(horizontalAspectRatio); } + float horizontalMaxDimension; + if (m_graphAspectRatio > 2.0f) { + horizontalMaxDimension = 2.0f; + m_scaleY = 2.0f / m_graphAspectRatio; + } else { + horizontalMaxDimension = m_graphAspectRatio; + m_scaleY = 1.0f; + } + if (m_polarGraph) + m_polarRadius = horizontalMaxDimension; + float scaleFactor = qMax(areaSize.width(), areaSize.height()); - m_scaleX = m_graphAspectRatio * areaSize.width() / scaleFactor; - m_scaleZ = m_graphAspectRatio * areaSize.height() / scaleFactor; + m_scaleX = horizontalMaxDimension * areaSize.width() / scaleFactor; + m_scaleZ = horizontalMaxDimension * areaSize.height() / scaleFactor; m_scaleXWithBackground = m_scaleX + m_hBackgroundMargin; + m_scaleYWithBackground = m_scaleY + m_vBackgroundMargin; m_scaleZWithBackground = m_scaleZ + m_hBackgroundMargin; - m_scaleYWithBackground = m_vBackgroundMargin + 1.0f; - float factorScaler = 2.0f * m_graphAspectRatio / scaleFactor; - m_axisCacheX.setScale(factorScaler * areaSize.width()); - m_axisCacheZ.setScale(-factorScaler * areaSize.height()); - m_axisCacheX.setTranslate(-m_axisCacheX.scale() / 2.0f); - m_axisCacheZ.setTranslate(-m_axisCacheZ.scale() / 2.0f); + m_axisCacheX.setScale(m_scaleX * 2.0f); + m_axisCacheY.setScale(m_scaleY * 2.0f); + m_axisCacheZ.setScale(m_scaleZ * 2.0f); + m_axisCacheX.setTranslate(-m_scaleX); + m_axisCacheY.setTranslate(-m_scaleY); + m_axisCacheZ.setTranslate(-m_scaleZ); } void Scatter3DRenderer::initShaders(const QString &vertexShader, const QString &fragmentShader) @@ -2237,7 +2249,7 @@ QVector3D Scatter3DRenderer::convertPositionToTranslation(const QVector3D &posit yTrans = m_axisCacheY.positionAt(position.y()); } else { xTrans = position.x() * m_scaleX; - yTrans = position.y(); + yTrans = position.y() * m_scaleY; zTrans = position.z() * m_scaleZ; } return QVector3D(xTrans, yTrans, zTrans); diff --git a/src/datavisualization/engine/scatter3drenderer_p.h b/src/datavisualization/engine/scatter3drenderer_p.h index 645a2a8e..9baad16a 100644 --- a/src/datavisualization/engine/scatter3drenderer_p.h +++ b/src/datavisualization/engine/scatter3drenderer_p.h @@ -67,8 +67,8 @@ private: GLuint m_selectionDepthBuffer; GLfloat m_shadowQualityToShader; GLint m_shadowQualityMultiplier; - float m_heightNormalizer; float m_scaleX; + float m_scaleY; float m_scaleZ; float m_scaleXWithBackground; float m_scaleYWithBackground; diff --git a/src/datavisualization/engine/shaders/surface.frag b/src/datavisualization/engine/shaders/surface.frag index f17dd73e..238e5fed 100644 --- a/src/datavisualization/engine/shaders/surface.frag +++ b/src/datavisualization/engine/shaders/surface.frag @@ -11,9 +11,11 @@ uniform highp vec3 lightPosition_wrld; uniform highp float lightStrength; uniform highp float ambientStrength; uniform highp vec4 lightColor; +uniform highp float gradMin; +uniform highp float gradHeight; void main() { - highp vec2 gradientUV = vec2(0.0, (coords_mdl.y + 1.0) / 2.0); + highp vec2 gradientUV = vec2(0.0, gradMin + coords_mdl.y * gradHeight); highp vec3 materialDiffuseColor = texture2D(textureSampler, gradientUV).xyz; highp vec3 materialAmbientColor = lightColor.rgb * ambientStrength * materialDiffuseColor; highp vec3 materialSpecularColor = lightColor.rgb; diff --git a/src/datavisualization/engine/shaders/surfaceFlat.frag b/src/datavisualization/engine/shaders/surfaceFlat.frag index 748fb3dd..1a0bbdeb 100644 --- a/src/datavisualization/engine/shaders/surfaceFlat.frag +++ b/src/datavisualization/engine/shaders/surfaceFlat.frag @@ -13,9 +13,11 @@ uniform highp vec3 lightPosition_wrld; uniform highp float lightStrength; uniform highp float ambientStrength; uniform highp vec4 lightColor; +uniform highp float gradMin; +uniform highp float gradHeight; void main() { - highp vec2 gradientUV = vec2(0.0, (coords_mdl.y + 1.0) / 2.0); + highp vec2 gradientUV = vec2(0.0, gradMin + coords_mdl.y * gradHeight); highp vec3 materialDiffuseColor = texture2D(textureSampler, gradientUV).xyz; highp vec3 materialAmbientColor = lightColor.rgb * ambientStrength * materialDiffuseColor; highp vec3 materialSpecularColor = lightColor.rgb; diff --git a/src/datavisualization/engine/shaders/surfaceShadowFlat.frag b/src/datavisualization/engine/shaders/surfaceShadowFlat.frag index 0613a40c..4b0dfae0 100644 --- a/src/datavisualization/engine/shaders/surfaceShadowFlat.frag +++ b/src/datavisualization/engine/shaders/surfaceShadowFlat.frag @@ -7,15 +7,17 @@ varying highp vec3 position_wrld; flat varying highp vec3 normal_cmr; varying highp vec3 eyeDirection_cmr; varying highp vec3 lightDirection_cmr; +varying highp vec4 shadowCoord; uniform highp sampler2DShadow shadowMap; uniform sampler2D textureSampler; -varying highp vec4 shadowCoord; uniform highp vec3 lightPosition_wrld; uniform highp float lightStrength; uniform highp float ambientStrength; uniform highp float shadowQuality; uniform highp vec4 lightColor; +uniform highp float gradMin; +uniform highp float gradHeight; highp vec2 poissonDisk[16] = vec2[16](vec2(-0.94201624, -0.39906216), vec2(0.94558609, -0.76890725), @@ -35,7 +37,7 @@ highp vec2 poissonDisk[16] = vec2[16](vec2(-0.94201624, -0.39906216), vec2(0.14383161, -0.14100790)); void main() { - highp vec2 gradientUV = vec2(0.0, (coords_mdl.y + 1.0) / 2.0); + highp vec2 gradientUV = vec2(0.0, gradMin + coords_mdl.y * gradHeight); highp vec3 materialDiffuseColor = texture2D(textureSampler, gradientUV).xyz; highp vec3 materialAmbientColor = lightColor.rgb * ambientStrength * materialDiffuseColor; highp vec3 materialSpecularColor = lightColor.rgb; diff --git a/src/datavisualization/engine/shaders/surfaceShadowNoTex.frag b/src/datavisualization/engine/shaders/surfaceShadowNoTex.frag index 1acf8f69..d6195227 100644 --- a/src/datavisualization/engine/shaders/surfaceShadowNoTex.frag +++ b/src/datavisualization/engine/shaders/surfaceShadowNoTex.frag @@ -5,15 +5,17 @@ varying highp vec3 position_wrld; varying highp vec3 normal_cmr; varying highp vec3 eyeDirection_cmr; varying highp vec3 lightDirection_cmr; +varying highp vec4 shadowCoord; uniform highp sampler2DShadow shadowMap; uniform sampler2D textureSampler; -varying highp vec4 shadowCoord; uniform highp vec3 lightPosition_wrld; uniform highp float lightStrength; uniform highp float ambientStrength; uniform highp float shadowQuality; uniform highp vec4 lightColor; +uniform highp float gradMin; +uniform highp float gradHeight; highp vec2 poissonDisk[16] = vec2[16](vec2(-0.94201624, -0.39906216), vec2(0.94558609, -0.76890725), @@ -33,7 +35,7 @@ highp vec2 poissonDisk[16] = vec2[16](vec2(-0.94201624, -0.39906216), vec2(0.14383161, -0.14100790)); void main() { - highp vec2 gradientUV = vec2(0.0, (coords_mdl.y + 1.0) / 2.0); + highp vec2 gradientUV = vec2(0.0, gradMin + coords_mdl.y * gradHeight); highp vec3 materialDiffuseColor = texture2D(textureSampler, gradientUV).xyz; highp vec3 materialAmbientColor = lightColor.rgb * ambientStrength * materialDiffuseColor; highp vec3 materialSpecularColor = lightColor.rgb; diff --git a/src/datavisualization/engine/shaders/surface_ES2.frag b/src/datavisualization/engine/shaders/surface_ES2.frag index 58d13834..1d1bdc3e 100644 --- a/src/datavisualization/engine/shaders/surface_ES2.frag +++ b/src/datavisualization/engine/shaders/surface_ES2.frag @@ -10,9 +10,11 @@ uniform sampler2D textureSampler; uniform highp float lightStrength; uniform highp float ambientStrength; uniform highp vec4 lightColor; +uniform highp float gradMin; +uniform highp float gradHeight; void main() { - highp vec2 gradientUV = vec2(0.0, (coords_mdl.y + 1.0) / 2.0); + highp vec2 gradientUV = vec2(0.0, gradMin + coords_mdl.y * gradHeight); highp vec3 materialDiffuseColor = texture2D(textureSampler, gradientUV).xyz; highp vec3 materialAmbientColor = lightColor.rgb * ambientStrength * materialDiffuseColor; highp vec3 materialSpecularColor = lightColor.rgb; diff --git a/src/datavisualization/engine/surface3drenderer.cpp b/src/datavisualization/engine/surface3drenderer.cpp index c1a23493..f5f05498 100644 --- a/src/datavisualization/engine/surface3drenderer.cpp +++ b/src/datavisualization/engine/surface3drenderer.cpp @@ -50,6 +50,7 @@ Surface3DRenderer::Surface3DRenderer(Surface3DController *controller) m_labelShader(0), m_heightNormalizer(0.0f), m_scaleX(0.0f), + m_scaleY(0.0f), m_scaleZ(0.0f), m_scaleXWithBackground(0.0f), m_scaleYWithBackground(0.0f), @@ -70,9 +71,6 @@ Surface3DRenderer::Surface3DRenderer(Surface3DController *controller) m_selectionTexturesDirty(false), m_noShadowTexture(0) { - m_axisCacheY.setScale(2.0f); - m_axisCacheY.setTranslate(-1.0f); - // Check if flat feature is supported ShaderHelper tester(this, QStringLiteral(":/shaders/vertexSurfaceFlat"), QStringLiteral(":/shaders/fragmentSurfaceFlat")); @@ -834,7 +832,7 @@ void Surface3DRenderer::drawSlicedScene() glCullFace(GL_BACK); // Grid lines - if (m_cachedTheme->isGridEnabled() && m_heightNormalizer) { + if (m_cachedTheme->isGridEnabled()) { #if !(defined QT_OPENGL_ES_2) ShaderHelper *lineShader = m_backgroundShader; #else @@ -1257,7 +1255,7 @@ void Surface3DRenderer::drawScene(GLuint defaultFboHandle) // Draw the surface if (!m_renderCacheList.isEmpty()) { - // For surface we can see climpses from underneath + // For surface we can see glimpses from underneath glDisable(GL_CULL_FACE); bool drawGrid = false; @@ -1303,10 +1301,24 @@ void Surface3DRenderer::drawScene(GLuint defaultFboHandle) shader->setUniformValue(shader->lightColor(), lightColor); GLuint gradientTexture; - if (cache->colorStyle() == Q3DTheme::ColorStyleUniform) + if (cache->colorStyle() == Q3DTheme::ColorStyleUniform) { gradientTexture = cache->baseUniformTexture(); - else + shader->setUniformValue(shader->gradientMin(), 0.0f); + shader->setUniformValue(shader->gradientHeight(), 0.0f); + } else { gradientTexture = cache->baseGradientTexture(); + if (cache->colorStyle() == Q3DTheme::ColorStyleObjectGradient) { + float objMin = cache->surfaceObject()->minYValue(); + float objMax = cache->surfaceObject()->maxYValue(); + float objRange = objMax - objMin; + shader->setUniformValue(shader->gradientMin(), -(objMin / objRange)); + shader->setUniformValue(shader->gradientHeight(), 1.0f / objRange); + } else { + shader->setUniformValue(shader->gradientMin(), 0.5f); + shader->setUniformValue(shader->gradientHeight(), + 1.0f / (m_scaleY * 2.0f)); + } + } #if !defined(QT_OPENGL_ES_2) if (m_cachedShadowQuality > QAbstract3DGraph::ShadowQualityNone) { @@ -1433,7 +1445,7 @@ void Surface3DRenderer::drawScene(GLuint defaultFboHandle) QVector3D gridLineScaleZ(gridLineWidth, gridLineWidth, m_scaleZWithBackground); QVector3D gridLineScaleY(gridLineWidth, m_scaleYWithBackground, gridLineWidth); - if (m_cachedTheme->isGridEnabled() && m_heightNormalizer) { + if (m_cachedTheme->isGridEnabled()) { #if !(defined QT_OPENGL_ES_2) ShaderHelper *lineShader = m_backgroundShader; #else @@ -1947,7 +1959,7 @@ void Surface3DRenderer::drawLabels(bool drawSelection, const Q3DCamera *activeCa if (m_polarGraph) { float direction = m_zFlipped ? -1.0f : 1.0f; labelTrans.setZ((m_axisCacheZ.formatter()->labelPositions().at(label) - * -m_graphAspectRatio + * -m_polarRadius + m_drawer->scaledFontSize() + gridLineWidth) * direction); } else { labelTrans.setZ(m_axisCacheZ.labelPosition(label)); @@ -1968,7 +1980,7 @@ void Surface3DRenderer::drawLabels(bool drawSelection, const Q3DCamera *activeCa } if (!drawSelection && m_axisCacheZ.isTitleVisible()) { if (m_polarGraph) { - float titleZ = -m_graphAspectRatio / 2.0f; + float titleZ = -m_polarRadius / 2.0f; if (m_zFlipped) titleZ = -titleZ; labelTrans.setZ(titleZ); @@ -2106,8 +2118,8 @@ void Surface3DRenderer::drawLabels(bool drawSelection, const Q3DCamera *activeCa continue; float labelPosition = labelPositions.at(label); qreal angle = labelPosition * M_PI * 2.0; - labelTrans.setX((m_graphAspectRatio + labelMargin) * float(qSin(angle))); - labelTrans.setZ(-(m_graphAspectRatio + labelMargin) * float(qCos(angle))); + labelTrans.setX((m_polarRadius + labelMargin) * float(qSin(angle))); + labelTrans.setZ(-(m_polarRadius + labelMargin) * float(qCos(angle))); // Alignment depends on label angular position, as well as flips Qt::AlignmentFlag vAlignment = Qt::AlignCenter; Qt::AlignmentFlag hAlignment = Qt::AlignCenter; @@ -2152,7 +2164,7 @@ void Surface3DRenderer::drawLabels(bool drawSelection, const Q3DCamera *activeCa totalRotation *= m_zRightAngleRotationNeg; if (m_yFlippedForGrid) totalRotation *= QQuaternion::fromAxisAndAngle(0.0f, 0.0f, 1.0f, -180.0f); - labelTrans.setZ(-m_graphAspectRatio); + labelTrans.setZ(-m_polarRadius); radial = true; } drawAxisTitleX(labelRotation, labelTrans, totalRotation, m_dummyRenderItem, @@ -2415,19 +2427,31 @@ void Surface3DRenderer::calculateSceneScalingFactors() areaSize.setWidth(horizontalAspectRatio); } + float horizontalMaxDimension; + if (m_graphAspectRatio > 2.0f) { + horizontalMaxDimension = 2.0f; + m_scaleY = 2.0f / m_graphAspectRatio; + } else { + horizontalMaxDimension = m_graphAspectRatio; + m_scaleY = 1.0f; + } + if (m_polarGraph) + m_polarRadius = horizontalMaxDimension; + float scaleFactor = qMax(areaSize.width(), areaSize.height()); - m_scaleX = m_graphAspectRatio * areaSize.width() / scaleFactor; - m_scaleZ = m_graphAspectRatio * areaSize.height() / scaleFactor; + m_scaleX = horizontalMaxDimension * areaSize.width() / scaleFactor; + m_scaleZ = horizontalMaxDimension * areaSize.height() / scaleFactor; m_scaleXWithBackground = m_scaleX + m_hBackgroundMargin; + m_scaleYWithBackground = m_scaleY + m_vBackgroundMargin; m_scaleZWithBackground = m_scaleZ + m_hBackgroundMargin; - m_scaleYWithBackground = m_vBackgroundMargin + 1.0f; - float factorScaler = 2.0f * m_graphAspectRatio / scaleFactor; - m_axisCacheX.setScale(factorScaler * areaSize.width()); - m_axisCacheZ.setScale(-factorScaler * areaSize.height()); - m_axisCacheX.setTranslate(-m_axisCacheX.scale() / 2.0f); - m_axisCacheZ.setTranslate(-m_axisCacheZ.scale() / 2.0f); + m_axisCacheX.setScale(m_scaleX * 2.0f); + m_axisCacheY.setScale(m_scaleY * 2.0f); + m_axisCacheZ.setScale(m_scaleZ * 2.0f); + m_axisCacheX.setTranslate(-m_scaleX); + m_axisCacheY.setTranslate(-m_scaleY); + m_axisCacheZ.setTranslate(-m_scaleZ); } void Surface3DRenderer::checkFlatSupport(SurfaceSeriesRenderCache *cache) @@ -2848,7 +2872,7 @@ QVector3D Surface3DRenderer::convertPositionToTranslation(const QVector3D &posit yTrans = m_axisCacheY.positionAt(position.y()); } else { xTrans = position.x() * m_scaleX; - yTrans = position.y(); + yTrans = position.y() * m_scaleY; zTrans = position.z() * m_scaleZ; } return QVector3D(xTrans, yTrans, zTrans); diff --git a/src/datavisualization/engine/surface3drenderer_p.h b/src/datavisualization/engine/surface3drenderer_p.h index 7a547042..cb9c49c3 100644 --- a/src/datavisualization/engine/surface3drenderer_p.h +++ b/src/datavisualization/engine/surface3drenderer_p.h @@ -58,6 +58,7 @@ private: ShaderHelper *m_labelShader; float m_heightNormalizer; float m_scaleX; + float m_scaleY; float m_scaleZ; float m_scaleXWithBackground; float m_scaleYWithBackground; diff --git a/src/datavisualization/utils/surfaceobject.cpp b/src/datavisualization/utils/surfaceobject.cpp index f8675912..a98f2f2a 100644 --- a/src/datavisualization/utils/surfaceobject.cpp +++ b/src/datavisualization/utils/surfaceobject.cpp @@ -69,24 +69,14 @@ void SurfaceObject::setUpSmoothData(const QSurfaceDataArray &dataArray, const QR uvs.resize(totalSize); int totalIndex = 0; - AxisRenderCache &xCache = flipXZ ? m_axisCacheZ : m_axisCacheX; - AxisRenderCache &zCache = flipXZ ? m_axisCacheX : m_axisCacheZ; + // Init min and max to ridiculous values + m_minY = 10000000.0; + m_maxY = -10000000.0f; for (int i = 0; i < m_rows; i++) { const QSurfaceDataRow &p = *dataArray.at(i); for (int j = 0; j < m_columns; j++) { - const QSurfaceDataItem &data = p.at(j); - float normalizedX; - float normalizedZ; - if (polar) { - // Slice don't use polar, so don't care about flip - m_renderer->calculatePolarXZ(data.position(), normalizedX, normalizedZ); - } else { - normalizedX = xCache.positionAt(data.x()); - normalizedZ = zCache.positionAt(data.z()); - } - float normalizedY = m_axisCacheY.positionAt(data.y()); - m_vertices[totalIndex] = QVector3D(normalizedX, normalizedY, normalizedZ); + getNormalizedVertex(p.at(j), m_vertices[totalIndex], polar, flipXZ); if (changeGeometry) uvs[totalIndex] = QVector2D(GLfloat(j) * uvX, GLfloat(i) * uvY); totalIndex++; @@ -151,19 +141,8 @@ void SurfaceObject::updateSmoothRow(const QSurfaceDataArray &dataArray, int rowI int p = rowIndex * m_columns; const QSurfaceDataRow &dataRow = *dataArray.at(rowIndex); - for (int j = 0; j < m_columns; j++) { - const QSurfaceDataItem &data = dataRow.at(j); - float normalizedX; - float normalizedZ; - if (polar) { - m_renderer->calculatePolarXZ(data.position(), normalizedX, normalizedZ); - } else { - normalizedX = m_axisCacheX.positionAt(data.x()); - normalizedZ = m_axisCacheZ.positionAt(data.z()); - } - float normalizedY = m_axisCacheY.positionAt(data.y()); - m_vertices[p++] = QVector3D(normalizedX, normalizedY, normalizedZ); - } + for (int j = 0; j < m_columns; j++) + getNormalizedVertex(dataRow.at(j), m_vertices[p++], polar, false); // Create normals int colLimit = m_columns - 1; @@ -214,17 +193,8 @@ void SurfaceObject::updateSmoothItem(const QSurfaceDataArray &dataArray, int row bool polar) { // Update a vertice - const QSurfaceDataItem &data = dataArray.at(row)->at(column); - float normalizedX; - float normalizedZ; - if (polar) { - m_renderer->calculatePolarXZ(data.position(), normalizedX, normalizedZ); - } else { - normalizedX = m_axisCacheX.positionAt(data.x()); - normalizedZ = m_axisCacheZ.positionAt(data.z()); - } - float normalizedY = m_axisCacheY.positionAt(data.y()); - m_vertices[row * m_columns + column] = QVector3D(normalizedX, normalizedY, normalizedZ); + getNormalizedVertex(dataArray.at(row)->at(column), + m_vertices[row * m_columns + column], polar, false); // Create normals int startRow = row; @@ -376,24 +346,14 @@ void SurfaceObject::setUpData(const QSurfaceDataArray &dataArray, const QRect &s int doubleColumns = m_columns * 2 - 2; int rowColLimit = rowLimit * doubleColumns; - AxisRenderCache &xCache = flipXZ ? m_axisCacheZ : m_axisCacheX; - AxisRenderCache &zCache = flipXZ ? m_axisCacheX : m_axisCacheZ; + // Init min and max to ridiculous values + m_minY = 10000000.0; + m_maxY = -10000000.0f; for (int i = 0; i < m_rows; i++) { const QSurfaceDataRow &row = *dataArray.at(i); for (int j = 0; j < m_columns; j++) { - const QSurfaceDataItem &data = row.at(j); - float normalizedX; - float normalizedZ; - if (polar) { - // Slice don't use polar, so don't care about flip - m_renderer->calculatePolarXZ(data.position(), normalizedX, normalizedZ); - } else { - normalizedX = xCache.positionAt(data.x()); - normalizedZ = zCache.positionAt(data.z()); - } - float normalizedY = m_axisCacheY.positionAt(data.y()); - m_vertices[totalIndex] = QVector3D(normalizedX, normalizedY, normalizedZ); + getNormalizedVertex(row.at(j), m_vertices[totalIndex], polar, flipXZ); if (changeGeometry) uvs[totalIndex] = QVector2D(GLfloat(j) * uvX, GLfloat(i) * uvY); @@ -475,18 +435,7 @@ void SurfaceObject::updateCoarseRow(const QSurfaceDataArray &dataArray, int rowI const QSurfaceDataRow &dataRow = *dataArray.at(rowIndex); for (int j = 0; j < m_columns; j++) { - const QSurfaceDataItem &data = dataRow.at(j); - float normalizedX; - float normalizedZ; - if (polar) { - m_renderer->calculatePolarXZ(data.position(), normalizedX, normalizedZ); - } else { - normalizedX = m_axisCacheX.positionAt(data.x()); - normalizedZ = m_axisCacheZ.positionAt(data.z()); - } - float normalizedY = m_axisCacheY.positionAt(data.y()); - m_vertices[p++] = QVector3D(normalizedX, normalizedY, normalizedZ); - + getNormalizedVertex(dataRow.at(j), m_vertices[p++], polar, false); if (j > 0 && j < colLimit) { m_vertices[p] = m_vertices[p - 1]; p++; @@ -528,18 +477,7 @@ void SurfaceObject::updateCoarseItem(const QSurfaceDataArray &dataArray, int row // Update a vertice int p = row * doubleColumns + column * 2 - (column > 0); - const QSurfaceDataItem &data = dataArray.at(row)->at(column); - float normalizedX; - float normalizedZ; - if (polar) { - m_renderer->calculatePolarXZ(data.position(), normalizedX, normalizedZ); - } else { - normalizedX = m_axisCacheX.positionAt(data.x()); - normalizedZ = m_axisCacheZ.positionAt(data.z()); - } - float normalizedY = m_axisCacheY.positionAt(data.y()); - m_vertices[p] = QVector3D(normalizedX, normalizedY, normalizedZ); - p++; + getNormalizedVertex(dataArray.at(row)->at(column), m_vertices[p++], polar, false); if (column > 0 && column < colLimit) m_vertices[p] = m_vertices[p - 1]; @@ -714,6 +652,31 @@ bool SurfaceObject::checkFlipNormal(const QSurfaceDataArray &array) return ascendingX != ascendingZ; } +void SurfaceObject::getNormalizedVertex(const QSurfaceDataItem &data, QVector3D &vertex, + bool polar, bool flipXZ) +{ + float normalizedX; + float normalizedZ; + if (polar) { + // Slice don't use polar, so don't care about flip + m_renderer->calculatePolarXZ(data.position(), normalizedX, normalizedZ); + } else { + if (flipXZ) { + normalizedX = m_axisCacheZ.positionAt(data.x()); + normalizedZ = m_axisCacheX.positionAt(data.z()); + } else { + normalizedX = m_axisCacheX.positionAt(data.x()); + normalizedZ = m_axisCacheZ.positionAt(data.z()); + } + } + float normalizedY = m_axisCacheY.positionAt(data.y()); + m_minY = qMin(normalizedY, m_minY); + m_maxY = qMax(normalizedY, m_maxY); + vertex.setX(normalizedX); + vertex.setY(normalizedY); + vertex.setZ(normalizedZ); +} + GLuint SurfaceObject::gridElementBuf() { if (!m_meshDataLoaded) diff --git a/src/datavisualization/utils/surfaceobject_p.h b/src/datavisualization/utils/surfaceobject_p.h index 54e6dec3..b3ba80e9 100644 --- a/src/datavisualization/utils/surfaceobject_p.h +++ b/src/datavisualization/utils/surfaceobject_p.h @@ -70,6 +70,8 @@ public: GLuint gridIndexCount(); QVector3D vertexAt(int column, int row); void clear(); + float minYValue() const { return m_minY; } + float maxYValue() const { return m_maxY; } private: QVector3D normal(const QVector3D &a, const QVector3D &b, const QVector3D &c, bool flipNormal); @@ -77,6 +79,8 @@ private: const QVector &normals, const GLint *indices, bool changeGeometry); bool checkFlipNormal(const QSurfaceDataArray &array); + inline void getNormalizedVertex(const QSurfaceDataItem &data, QVector3D &vertex, bool polar, + bool flipXZ); private: SurfaceType m_surfaceType; @@ -91,6 +95,8 @@ private: AxisRenderCache &m_axisCacheY; AxisRenderCache &m_axisCacheZ; Surface3DRenderer *m_renderer; + float m_minY; + float m_maxY; }; QT_END_NAMESPACE_DATAVISUALIZATION -- cgit v1.2.3