From 92b8b37bd9f53a6d1003473e6c568ac962936394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 16 Dec 2019 11:54:03 -0300 Subject: [PATCH 01/20] waterfallplot: Move to an opengl approach MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/waterfall/waterfallplot.cpp | 39 ++++++++++++--------------------- src/waterfall/waterfallplot.h | 21 ++++++++++++++++++ 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/waterfall/waterfallplot.cpp b/src/waterfall/waterfallplot.cpp index e526d2b36..2c081b306 100644 --- a/src/waterfall/waterfallplot.cpp +++ b/src/waterfall/waterfallplot.cpp @@ -15,8 +15,8 @@ uint16_t WaterfallPlot::_displayWidth = 500; WaterfallPlot::WaterfallPlot(QQuickItem* parent) : Waterfall(parent) - , _currentDrawIndex(_displayWidth) - , _image(2048, 2500, QImage::Format_RGBA8888) + , _currentDrawIndex(0) + , _image(_displayWidth, 2000, QImage::Format_RGBA8888) , _maxDepthToDrawInPixels(0) , _minDepthToDrawInPixels(0) , _mouseDepth(0) @@ -50,20 +50,13 @@ void WaterfallPlot::paint(QPainter* painter) _painter = painter; } - static uint16_t first; - - if (_currentDrawIndex < _displayWidth) { - first = 0; - } else { - first = _currentDrawIndex - _displayWidth; - } - // http://blog.qt.io/blog/2006/05/13/fast-transformed-pixmapimage-drawing/ pix = QPixmap::fromImage(_image, Qt::NoFormatConversion); // Code for debug, draw the entire waterfall - //_painter->drawPixmap(_painter->viewport(), pix, QRect(0, 0, _image.width(), _image.height())); - _painter->drawPixmap(QRect(0, 0, width(), height()), pix, - QRect(first, _minDepthToDrawInPixels, _displayWidth, _maxDepthToDrawInPixels)); + _painter->drawPixmap(_painter->viewport(), pix, QRect(0, 0, _image.width(), _image.height())); + + emit drawHorizontalRatioChanged(); + emit drawVerticalRatioChanged(); } void WaterfallPlot::setImage(const QImage& image) @@ -137,6 +130,10 @@ void WaterfallPlot::draw(const QVector& points, float confidence, float // This ring vector will store variables of the last n samples for user access _DCRing.append({initPoint, length, confidence, distance}); + // Limit currentDrawIndex to be inside our image max width + _currentDrawIndex++; + _currentDrawIndex %= _displayWidth; + /** * @brief Get lastMaxDepth from the last n samples */ @@ -183,7 +180,6 @@ void WaterfallPlot::draw(const QVector& points, float confidence, float QPainter painter(&_image); // Clean everything and start from zero painter.fillRect(_image.rect(), Qt::transparent); - // QRect(0, 0, _image.width(), _image.height()*dynamicPixelsPerMeterScalar), old painter.drawImage(dst, old, src); painter.end(); }; @@ -217,16 +213,6 @@ void WaterfallPlot::draw(const QVector& points, float confidence, float int virtualFloor = initPoint * _minPixelsPerMeter; int virtualHeight = length * _minPixelsPerMeter * dynamicPixelsPerMeterScalar; - // Copy tail to head - // TODO: can we get even better and allocate just once at initialization? ie circular buffering - if (_currentDrawIndex >= _image.width()) { - redrawImage(QRect(0, 0, _displayWidth, _image.height()), - QRect(old.width() - _displayWidth, 0, _displayWidth, old.height())); - - // Start painting from the beginning - _currentDrawIndex = _displayWidth; - } - // Do up/downsampling float factor = points.length() / ((float)(virtualHeight)); @@ -273,7 +259,10 @@ void WaterfallPlot::draw(const QVector& points, float confidence, float _image.setPixelColor(_currentDrawIndex, i + virtualFloor, valueToRGB(points[factor * i])); } } - _currentDrawIndex++; // This can get to be an issue at very fast update rates from ping + + for (int i = virtualHeight; i < _image.height(); i++) { + _image.setPixelColor(_currentDrawIndex, i, Qt::transparent); + } // Fix max update in 20Hz at max if (!_updateTimer->isActive()) { diff --git a/src/waterfall/waterfallplot.h b/src/waterfall/waterfallplot.h index b05c80e60..f365732af 100644 --- a/src/waterfall/waterfallplot.h +++ b/src/waterfall/waterfallplot.h @@ -111,7 +111,28 @@ class WaterfallPlot : public Waterfall { Q_INVOKABLE float getMinDepthToDraw() { return _minDepthToDraw; } Q_PROPERTY(float minDepthToDraw READ getMinDepthToDraw NOTIFY minDepthToDrawChanged) + /** + * @brief Return the necessary horizontal ratio value to perform the correct waterfall draw + * + * @return double + */ + Q_INVOKABLE double drawHorizontalRatio() { return static_cast(_currentDrawIndex + 1) / _image.width(); } + Q_PROPERTY(double drawHorizontalRatio READ drawHorizontalRatio NOTIFY drawHorizontalRatioChanged) + + /** + * @brief Return the necessary vertical ratio value to perform the correct waterfall draw + * + * @return double + */ + Q_INVOKABLE double drawVerticalRatio() + { + return (_maxDepthToDraw - _minDepthToDraw) * _minPixelsPerMeter / _image.height(); + } + Q_PROPERTY(double drawVerticalRatio READ drawVerticalRatio NOTIFY drawVerticalRatioChanged) + signals: + void drawHorizontalRatioChanged(); + void drawVerticalRatioChanged(); void imageChanged(); void maxDepthToDrawChanged(); void minDepthToDrawChanged(); From b8b4b37df66a35814e3e59d30580303d66280834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 16 Dec 2019 11:55:24 -0300 Subject: [PATCH 02/20] opengl: Add waterfallplot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- qml/opengl/waterfallplot/fragment.glsl | 11 +++++++++++ qml/opengl/waterfallplot/vertex.glsl | 8 ++++++++ resources.qrc | 2 ++ 3 files changed, 21 insertions(+) create mode 100644 qml/opengl/waterfallplot/fragment.glsl create mode 100644 qml/opengl/waterfallplot/vertex.glsl diff --git a/qml/opengl/waterfallplot/fragment.glsl b/qml/opengl/waterfallplot/fragment.glsl new file mode 100644 index 000000000..9c4f8517c --- /dev/null +++ b/qml/opengl/waterfallplot/fragment.glsl @@ -0,0 +1,11 @@ +varying vec2 coord; +uniform float horizontalRatio; +uniform float verticalRatio; +uniform sampler2D src; + +void main() { + // Mod is used to wrap around the 0-1 scaled x axis + vec2 shiftedCoord = mod(coord + vec2(horizontalRatio, 0), 1.0); + shiftedCoord.y = shiftedCoord.y * verticalRatio; + gl_FragColor = texture2D(src, shiftedCoord); +} diff --git a/qml/opengl/waterfallplot/vertex.glsl b/qml/opengl/waterfallplot/vertex.glsl new file mode 100644 index 000000000..47c80206f --- /dev/null +++ b/qml/opengl/waterfallplot/vertex.glsl @@ -0,0 +1,8 @@ +uniform highp mat4 qt_Matrix; +attribute highp vec4 qt_Vertex; +attribute highp vec2 qt_MultiTexCoord0; +varying highp vec2 coord; +void main() { + coord = qt_MultiTexCoord0; + gl_Position = qt_Matrix * qt_Vertex; +} diff --git a/resources.qrc b/resources.qrc index 3911b1137..9a07717b4 100644 --- a/resources.qrc +++ b/resources.qrc @@ -69,5 +69,7 @@ qml/opengl/polarplot/fragment.glsl qml/opengl/polarplot/vertex.glsl + qml/opengl/waterfallplot/fragment.glsl + qml/opengl/waterfallplot/vertex.glsl From 7a329f4ebdb3b4e9ea8fa5fc5c0e50fe170d0f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 16 Dec 2019 11:55:47 -0300 Subject: [PATCH 03/20] Ping1DVisualizer: Use new WaterfallPlot with opengl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- qml/Ping1DVisualizer.qml | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/qml/Ping1DVisualizer.qml b/qml/Ping1DVisualizer.qml index bf2afcd4e..9c005449a 100644 --- a/qml/Ping1DVisualizer.qml +++ b/qml/Ping1DVisualizer.qml @@ -62,8 +62,21 @@ Item { orientation: Qt.Horizontal anchors.fill: parent - WaterfallPlot { - id: waterfall + ShaderEffect { + id: shader + + WaterfallPlot { + id: waterfall + visible: false + anchors.fill: parent + } + + property variant src: waterfall + property variant horizontalRatio: waterfall.drawHorizontalRatio + property variant verticalRatio: waterfall.drawVerticalRatio + vertexShader: "qrc:/opengl/waterfallplot/vertex.glsl" + fragmentShader: "qrc:/opengl/waterfallplot/fragment.glsl" + Layout.fillHeight: true Layout.fillWidth: true Layout.preferredWidth: 350 From 7d36686a3f81d5933c3dfd9c954bdfda88ce99db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 17 Dec 2019 16:11:43 -0300 Subject: [PATCH 04/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- pingviewer.pro | 2 +- qml/Ping360Visualizer.qml | 45 ++++++++++++------------------ qml/opengl/polarplot/fragment.glsl | 8 ++++-- qml/opengl/polarplot/vertex.glsl | 10 +++---- src/CMakeLists.txt | 1 + src/main.cpp | 2 ++ src/src.pri | 1 + src/waterfall/polarplot.cpp | 32 +++++++++++++++------ src/waterfall/polarplot.h | 16 ++++++++--- 9 files changed, 68 insertions(+), 49 deletions(-) diff --git a/pingviewer.pro b/pingviewer.pro index 7238e2859..7b05458ca 100644 --- a/pingviewer.pro +++ b/pingviewer.pro @@ -24,7 +24,7 @@ RESOURCES += \ # Warning as error *-g++ | *-clang { - QMAKE_CXXFLAGS += -Werror + #QMAKE_CXXFLAGS += -Werror } *msvc { QMAKE_CXXFLAGS += /WX \ diff --git a/qml/Ping360Visualizer.qml b/qml/Ping360Visualizer.qml index e9a2b01c8..efa026197 100644 --- a/qml/Ping360Visualizer.qml +++ b/qml/Ping360Visualizer.qml @@ -60,7 +60,7 @@ Item { onWidthChanged: { if(chart.Layout.minimumWidth === chart.width) { - shader.parent.width = width - chart.width + waterfall.parent.width = width - chart.width } } @@ -73,25 +73,22 @@ Item { Layout.fillHeight: true Layout.fillWidth: true - /** TODO: The shader logic should be inside done inside of PolarPlot c++ Object - * Or better encapsulated - */ - ShaderEffect { - id: shader + PolarPlot { + id: waterfall height: Math.min(ping.sectorSize > 180 ? parent.height : parent.height*2, parent.width*scale) width: height anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: ping.sectorSize > 180 ? parent.verticalCenter : parent.bottom property var scale: ping.sectorSize >= 180 ? 1 : 0.8/Math.sin(ping.sectorSize*Math.PI/360) - property variant src: waterfall + //property variant src: waterfall property var angle: 1 property bool verticalFlip: false property bool horizontalFlip: false - vertexShader: "qrc:/opengl/polarplot/vertex.glsl" - fragmentShader: "qrc:/opengl/polarplot/fragment.glsl" + //vertexShader: "qrc:/opengl/polarplot/vertex.glsl" + //fragmentShader: "qrc:/opengl/polarplot/fragment.glsl" Text { id: northText @@ -158,7 +155,7 @@ Item { } Shape { - visible: waterfall.containsMouse + //visible: waterfall.containsMouse anchors.centerIn: parent opacity: 0.5 ShapePath { @@ -168,7 +165,7 @@ Item { startY: 0 //TODO: This need to be updated in sensor integration PathLine { - property real angle: -Math.atan2(waterfall.mousePos.x - waterfall.width/2, waterfall.mousePos.y - waterfall.height/2) + Math.PI/2 + property real angle: 0//-Math.atan2(waterfall.mousePos.x - waterfall.width/2, waterfall.mousePos.y - waterfall.height/2) + Math.PI/2 x: waterfall.width*Math.cos(angle)/2 y: waterfall.height*Math.sin(angle)/2 } @@ -176,22 +173,16 @@ Item { } transform: Rotation { - origin.x: shader.width/2 - origin.y: shader.height/2 - axis { x: shader.verticalFlip; y: shader.horizontalFlip; z: ping.sectorSize > 180} + origin.x: waterfall.width/2 + origin.y: waterfall.height/2 + axis { x: waterfall.verticalFlip; y: waterfall.horizontalFlip; z: ping.sectorSize > 180} angle: -ping.heading*180/200 } } - PolarPlot { - id: waterfall - anchors.fill: shader - visible: false - } - Text { id: mouseReadout - visible: waterfall.containsMouse + //visible: waterfall.containsMouse anchors.right: parent.right anchors.top: parent.top anchors.margins: 5 @@ -214,17 +205,17 @@ Item { function calcAngleFromFlips(angle) { var value = waterfall.mouseSampleAngle - if(shader.verticalFlip && shader.horizontalFlip) { + if(waterfall.verticalFlip && waterfall.horizontalFlip) { value = (360 + 270 - value) % 360 return transformValue(value) } - if(shader.verticalFlip) { + if(waterfall.verticalFlip) { value = (360 + 180 - value) % 360 return transformValue(value) } - if(shader.horizontalFlip) { + if(waterfall.horizontalFlip) { value = 360 - value return transformValue(value) } @@ -236,7 +227,7 @@ Item { PolarGrid { id: polarGrid - anchors.fill: shader + anchors.fill: waterfall angle: ping.sectorSize maxDistance: waterfall.maxDistance } @@ -246,7 +237,7 @@ Item { height: 10 anchors.bottom: parent.bottom anchors.right: parent.right - waterfallGradient: waterfall.waterfallGradient + //waterfallGradient: waterfall.waterfallGradient } } @@ -311,7 +302,7 @@ Item { Layout.columnSpan: 5 Layout.fillWidth: true onCheckStateChanged: { - shader.horizontalFlip = checkState + waterfall.horizontalFlip = checkState } } diff --git a/qml/opengl/polarplot/fragment.glsl b/qml/opengl/polarplot/fragment.glsl index d4c74503f..9f1caead5 100644 --- a/qml/opengl/polarplot/fragment.glsl +++ b/qml/opengl/polarplot/fragment.glsl @@ -3,10 +3,12 @@ * Tilman Schmidt [KeyMaster-] example */ uniform sampler2D src; -uniform float angle; varying vec2 coord; void main() { + gl_FragColor = vec4(0, coord.x, coord.y, 1.0); + return; + vec2 sizeOverRadius = vec2(2.0, 2.0); float sampleOffset = 0.0; float polarFactor = 1.0; @@ -25,8 +27,8 @@ void main() { polar.y = sqrt(relPos.x * relPos.x + relPos.y * relPos.y); //Any radius over 1 would go beyond the source texture size, this simply outputs black for those fragments - if(polar.y > 1.0){ - gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); + if(polar.y > 0.5){ + gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0); return; } diff --git a/qml/opengl/polarplot/vertex.glsl b/qml/opengl/polarplot/vertex.glsl index 47c80206f..2a4669d01 100644 --- a/qml/opengl/polarplot/vertex.glsl +++ b/qml/opengl/polarplot/vertex.glsl @@ -1,8 +1,6 @@ -uniform highp mat4 qt_Matrix; -attribute highp vec4 qt_Vertex; -attribute highp vec2 qt_MultiTexCoord0; -varying highp vec2 coord; +attribute vec4 vertices; +varying vec2 coords; void main() { - coord = qt_MultiTexCoord0; - gl_Position = qt_Matrix * qt_Vertex; + gl_Position = vertices; + coords = vertices.xy; } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2668cc6bd..ff291d384 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,7 @@ set(INCLUDE_DIRS mavlink network notification + qquickcomponents sensor settings style diff --git a/src/main.cpp b/src/main.cpp index 031bee4da..c0e61cdd2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,6 +27,8 @@ #include "util.h" #include "waterfallplot.h" +#include "qquickopengl.h" + Q_DECLARE_LOGGING_CATEGORY(mainCategory) PING_LOGGING_CATEGORY(mainCategory, "ping.main") diff --git a/src/src.pri b/src/src.pri index e88c1fcb7..950b57ea2 100644 --- a/src/src.pri +++ b/src/src.pri @@ -24,6 +24,7 @@ include($$PWD/logger/logger.pri) include($$PWD/mavlink/mavlink.pri) include($$PWD/network/network.pri) include($$PWD/notification/notification.pri) +include($$PWD/qquickcomponents/qquickcomponents.pri) include($$PWD/sensor/sensor.pri) include($$PWD/settings/settings.pri) include($$PWD/style/style.pri) diff --git a/src/waterfall/polarplot.cpp b/src/waterfall/polarplot.cpp index ccaa9e120..95f5cec6d 100644 --- a/src/waterfall/polarplot.cpp +++ b/src/waterfall/polarplot.cpp @@ -3,6 +3,7 @@ #include +#include #include #include #include @@ -14,7 +15,7 @@ PING_LOGGING_CATEGORY(polarplot, "ping.polarplot") uint16_t PolarPlot::_angularResolution = 400; PolarPlot::PolarPlot(QQuickItem* parent) - : Waterfall(parent) + : QQuickOpenGL(parent) , _distances(_angularResolution, 0) , _image(400, 1200, QImage::Format_RGBA8888) , _maxDistance(0) @@ -25,14 +26,18 @@ PolarPlot::PolarPlot(QQuickItem* parent) setAcceptHoverEvents(true); _image.fill(QColor(Qt::transparent)); - connect(&_updateTimer, &QTimer::timeout, this, [&] { update(); }); - _updateTimer.setSingleShot(true); - _updateTimer.start(50); + setVertex(":/opengl/polarplot/vertex.glsl"); + setFragment(":/opengl/polarplot/fragment.glsl"); - connect(this, &Waterfall::mousePosChanged, this, &PolarPlot::updateMouseColumnData); - connect(this, &Waterfall::themeChanged, this, &PolarPlot::clear); + //connect(&_updateTimer, &QTimer::timeout, this, [&] { update(); }); + //_updateTimer.setSingleShot(true); + //_updateTimer.start(50); + + //connect(this, &Waterfall::mousePosChanged, this, &PolarPlot::updateMouseColumnData); + //connect(this, &Waterfall::themeChanged, this, &PolarPlot::clear); } +/* void PolarPlot::clear() { qCDebug(polarplot) << "Cleaning waterfall and restarting internal variables"; @@ -40,6 +45,14 @@ void PolarPlot::clear() _distances.fill(0, _angularResolution); _maxDistance = 0; } +*/ + +/* +void setValue(QOpenGLShaderProgram *shaderProgram) +{ + printf(""); + //shaderProgram->setUniformValue("coord", static_cast((_shift%100)/100.0f)); +}*/ void PolarPlot::paint(QPainter* painter) { @@ -102,18 +115,20 @@ void PolarPlot::draw( } for (int index = 0; index < _image.height(); index++) { - _image.setPixelColor(static_cast(newAngle), index, valueToRGB(points[index * scale])); + //_image.setPixelColor(static_cast(newAngle), index, valueToRGB(points[index * scale])); } } // Fix max update in 20Hz at max + /* if (!_updateTimer.isActive()) { _updateTimer.start(50); - } + }*/ emit imageChanged(); } +#if 0 void PolarPlot::updateMouseColumnData() { static const float rad2grad = 200.0f / M_PI; @@ -146,3 +161,4 @@ void PolarPlot::updateMouseColumnData() emit mouseSampleAngleChanged(); emit mouseSampleDistanceChanged(); } +#endif diff --git a/src/waterfall/polarplot.h b/src/waterfall/polarplot.h index 2bfff0b55..32aa3494b 100644 --- a/src/waterfall/polarplot.h +++ b/src/waterfall/polarplot.h @@ -5,6 +5,7 @@ #include #include "logger.h" +#include "qquickopengl.h" #include "ringvector.h" #include "waterfall.h" #include "waterfallgradient.h" @@ -15,7 +16,7 @@ Q_DECLARE_LOGGING_CATEGORY(waterfall) * @brief Polar widget * */ -class PolarPlot : public Waterfall { +class PolarPlot : public QQuickOpenGL { Q_OBJECT public: @@ -26,13 +27,20 @@ class PolarPlot : public Waterfall { */ PolarPlot(QQuickItem* parent = nullptr); + /** + * @brief Define setValue + * + * @param shaderProgram + */ + //void setValue(QOpenGLShaderProgram *shaderProgram) override final; + /** * @brief This is used by the qml paint event * This paint the a polar waterfall in qml * * @param painter */ - void paint(QPainter* painter) final override; + void paint(QPainter* painter); /** * @brief Set the polar Image @@ -58,7 +66,7 @@ class PolarPlot : public Waterfall { * @brief Clear waterfall and restart all parameters * */ - Q_INVOKABLE void clear() final override; + //Q_INVOKABLE void clear() final override; /** * @brief Return waterfall image @@ -106,7 +114,7 @@ class PolarPlot : public Waterfall { * @brief Update mouse column information * */ - void updateMouseColumnData(); + //void updateMouseColumnData(); QVector _distances; QImage _image; From 81674f29a98c4c1470b812d03672234d60f3e419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 7 Jan 2020 11:49:59 -0300 Subject: [PATCH 05/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- qml/MainPage.qml | 11 +++++++++++ qml/Ping360Visualizer.qml | 3 +++ src/main.cpp | 2 ++ src/waterfall/polarplot.cpp | 4 ++-- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/qml/MainPage.qml b/qml/MainPage.qml index 097a8d68a..a2295111d 100644 --- a/qml/MainPage.qml +++ b/qml/MainPage.qml @@ -15,6 +15,8 @@ import PingEnumNamespace 1.0 import SettingsManager 1.0 import StyleManager 1.0 +import QQuickOpenGL 1.0 + Item { id: root visible: true @@ -260,6 +262,15 @@ Item { radialGradient : sensorVisualizer.toString().includes("Ping360") ? radialGradient : linearGradient } + QQuickOpenGL { + id: potato + width: 400 + height: 300 + anchors.top: parent.top + visible: true + z: 10000000 + } + PingStatus { // We need to be over the mainvisualizer z: sensorVisualizer ? sensorVisualizer.z + 1 : 0 diff --git a/qml/Ping360Visualizer.qml b/qml/Ping360Visualizer.qml index efa026197..05eabfd97 100644 --- a/qml/Ping360Visualizer.qml +++ b/qml/Ping360Visualizer.qml @@ -75,10 +75,13 @@ Item { PolarPlot { id: waterfall + /* height: Math.min(ping.sectorSize > 180 ? parent.height : parent.height*2, parent.width*scale) width: height anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: ping.sectorSize > 180 ? parent.verticalCenter : parent.bottom + */ + anchors.fill: parent property var scale: ping.sectorSize >= 180 ? 1 : 0.8/Math.sin(ping.sectorSize*Math.PI/360) //property variant src: waterfall diff --git a/src/main.cpp b/src/main.cpp index c0e61cdd2..97a0925bf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,6 +22,7 @@ #include "ping.h" #include "ping360.h" #include "polarplot.h" +#include "qquickopengl.h" #include "settingsmanager.h" #include "stylemanager.h" #include "util.h" @@ -70,6 +71,7 @@ int main(int argc, char *argv[]) qmlRegisterType("Ping", 1, 0, "Ping"); qmlRegisterType("Ping360", 1, 0, "Ping360"); qmlRegisterType("PolarPlot", 1, 0, "PolarPlot"); + qmlRegisterType("QQuickOpenGL", 1, 0, "QQuickOpenGL"); qmlRegisterType("WaterfallPlot", 1, 0, "WaterfallPlot"); qmlRegisterUncreatableMetaObject( diff --git a/src/waterfall/polarplot.cpp b/src/waterfall/polarplot.cpp index 95f5cec6d..ba266d678 100644 --- a/src/waterfall/polarplot.cpp +++ b/src/waterfall/polarplot.cpp @@ -26,8 +26,8 @@ PolarPlot::PolarPlot(QQuickItem* parent) setAcceptHoverEvents(true); _image.fill(QColor(Qt::transparent)); - setVertex(":/opengl/polarplot/vertex.glsl"); - setFragment(":/opengl/polarplot/fragment.glsl"); + //setVertex(":/opengl/polarplot/vertex.glsl"); + //setFragment(":/opengl/polarplot/fragment.glsl"); //connect(&_updateTimer, &QTimer::timeout, this, [&] { update(); }); //_updateTimer.setSingleShot(true); From 33294d84f6b55c5c007e48af3ffe23403c380ed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Fri, 3 Jan 2020 12:51:12 -0300 Subject: [PATCH 06/20] qquickcomponents MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/qquickcomponents/qquickopengl.cpp | 110 ++++++++++++++++++++++++++ src/qquickcomponents/qquickopengl.h | 35 ++++++++ 2 files changed, 145 insertions(+) create mode 100644 src/qquickcomponents/qquickopengl.cpp create mode 100644 src/qquickcomponents/qquickopengl.h diff --git a/src/qquickcomponents/qquickopengl.cpp b/src/qquickcomponents/qquickopengl.cpp new file mode 100644 index 000000000..6a2dabadc --- /dev/null +++ b/src/qquickcomponents/qquickopengl.cpp @@ -0,0 +1,110 @@ +#include "qquickopengl.h" + +#include +#include +#include +#include + +#include + +#include +#include + +QQuickOpenGL::QQuickOpenGL(QQuickItem* parent) + : QQuickItem(parent) + , _shaderProgram(nullptr) + , _shift(0) + , _window(nullptr) +{ + // We are going to fill our own content with OpenGL + setFlag(QQuickItem::ItemHasContents); + + connect(this, &QQuickItem::windowChanged, this, &QQuickOpenGL::handleWindowChanged); + auto timer = new QTimer(this); + // connect(timer, &QTimer::timeout, this, [this]{ if (_window) _window->update(); }); + connect(this, &QQuickOpenGL::shiftChanged, this, [this] { + if (_window) + _window->update(); + }); +} + +void QQuickOpenGL::handleWindowChanged(QQuickWindow* window) +{ + if (!window) { + return; + } + + _window = window; + window->setClearBeforeRendering(false); + // window->setPersistentOpenGLContext(true); + + connect(window, &QQuickWindow::beforeRendering, this, &QQuickOpenGL::paint, Qt::DirectConnection); +} + +void QQuickOpenGL::paint() +{ + qDebug() << _window; + qDebug() << parentItem(); + + // Check if we are rendering with OpenGL + QSGRendererInterface* rif = window()->rendererInterface(); + Q_ASSERT(rif->graphicsApi() == QSGRendererInterface::OpenGL); + // QSGDefaultRenderContext *rc = static_cast(d->context); + // QRhiCommandBuffer *cb = rc->currentFrameCommandBuffer(); + + initializeOpenGLFunctions(); + if (!_shaderProgram) { + + _shaderProgram.reset(new QOpenGLShaderProgram()); + _shaderProgram->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, + R"( + attribute vec4 vertices; + varying vec2 coords; + void main() { + gl_Position = vertices; + coords = vertices.xy; + } + )"); + + _shaderProgram->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, + R"( + varying vec2 coords; + uniform float shift; + + void main() { + gl_FragColor = vec4(1.0, 0.0, 0.5, 1.0); + } + )"); + + _shaderProgram->bindAttributeLocation("vertices", 0); + _shaderProgram->link(); + } + + float values[] = {1, 1, -1, 1, 1, -1, -1, -1}; + + //_window->beginExternalCommands(); + + // Bind with the actual OpenGL thread + _shaderProgram->bind(); + _shaderProgram->enableAttributeArray(0); + _shaderProgram->setAttributeArray(0, GL_FLOAT, values, 2); + _shaderProgram->setUniformValue("shift", static_cast((_shift % 100) / 100.0f)); + + // We need to fix the opengl origin with our actual item position + auto point = parentItem()->mapFromItem(this, {0, 0}); + point += QPointF {0, static_cast(parentItem()->height() - height())}; + qDebug() << width() << height(); + // glInitDisplayMode(GL_RGBA | GL_ALPHA); + glViewport(point.x(), point.y(), width(), height()); + glDisable(GL_DEPTH_TEST); + glClearColor(0, 0, 0, 0); + glClear(GL_COLOR_BUFFER_BIT); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + _shaderProgram->disableAttributeArray(0); + _shaderProgram->release(); + + _window->resetOpenGLState(); +} diff --git a/src/qquickcomponents/qquickopengl.h b/src/qquickcomponents/qquickopengl.h new file mode 100644 index 000000000..83f8b40e0 --- /dev/null +++ b/src/qquickcomponents/qquickopengl.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +class QOpenGLShaderProgram; + +class QQuickOpenGL : public QQuickItem, protected QOpenGLFunctions { + Q_OBJECT + Q_PROPERTY(int shift READ shift WRITE setShift NOTIFY shiftChanged) + +public: + QQuickOpenGL(QQuickItem* parent = nullptr); + void paint(); + + void handleWindowChanged(QQuickWindow* window); + + int shift() { return _shift; } + void setShift(int shift) + { + if (_shift != shift) { + _shift = shift; + emit shiftChanged(); + } + } + +signals: + void shiftChanged(); + +private: + QSharedPointer _shaderProgram; + int _shift; + QQuickWindow* _window; +}; From 7b525b9f144bafe4c1c6634fe85d58d4cfbe9171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Fri, 3 Jan 2020 14:31:14 -0300 Subject: [PATCH 07/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/qquickcomponents/qquickopengl.cpp | 156 +++++++++++++------------- src/qquickcomponents/qquickopengl.h | 78 +++++++++++-- 2 files changed, 149 insertions(+), 85 deletions(-) diff --git a/src/qquickcomponents/qquickopengl.cpp b/src/qquickcomponents/qquickopengl.cpp index 6a2dabadc..d2676aff6 100644 --- a/src/qquickcomponents/qquickopengl.cpp +++ b/src/qquickcomponents/qquickopengl.cpp @@ -1,31 +1,89 @@ #include "qquickopengl.h" -#include #include -#include -#include #include #include #include -QQuickOpenGL::QQuickOpenGL(QQuickItem* parent) + +class Shader : public QSGMaterialShader +{ +public: + Shader() + { + setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/opengl/waterfallplot/fragment.glsl")); + setShaderSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/opengl/waterfallplot/vertex.glsl")); + } + + /** + * @brief Return attribute names + * The last element should be zero, from documentation + * + * @return char const* const* + */ + char const *const *attributeNames() const + { + static char const *const names[] = {"qt_Vertex", 0 }; + return names; + } + + void initialize() + { + QSGMaterialShader::initialize(); + m_id_matrix = program()->uniformLocation("qt_Matrix"); + m_id_opacity = program()->uniformLocation("qt_Opacity"); + m_id_shift = program()->uniformLocation("shift"); + + auto timer = new QTimer(); + QObject::connect(timer, &QTimer::timeout, [&] { shift += 0.01; } ); + timer->start(10); + } + + void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) + { + qDebug() << newMaterial << oldMaterial; + Q_ASSERT(program()->isLinked()); + if (state.isMatrixDirty()) + program()->setUniformValue(m_id_matrix, state.combinedMatrix()); + if (state.isOpacityDirty()) + program()->setUniformValue(m_id_opacity, state.opacity()); + qDebug() << "update"; + program()->setUniformValue(m_id_shift, shift); + } + +private: + int m_id_matrix; + int m_id_opacity; + int m_id_shift; + float shift = 0; + + QSGMaterial* material; +}; + +class Material : public QSGMaterial +{ +public: + QSGMaterialType *type() const { static QSGMaterialType type; return &type; } + QSGMaterialShader *createShader() const { return new Shader; } +}; + +QQuickOpenGL::QQuickOpenGL(QQuickItem *parent) : QQuickItem(parent) , _shaderProgram(nullptr) , _shift(0) , _window(nullptr) { // We are going to fill our own content with OpenGL - setFlag(QQuickItem::ItemHasContents); + QQuickItem::setFlag(QQuickItem::ItemHasContents); connect(this, &QQuickItem::windowChanged, this, &QQuickOpenGL::handleWindowChanged); - auto timer = new QTimer(this); - // connect(timer, &QTimer::timeout, this, [this]{ if (_window) _window->update(); }); - connect(this, &QQuickOpenGL::shiftChanged, this, [this] { - if (_window) - _window->update(); - }); + connect(this, &QQuickOpenGL::shiftChanged, this, [this]{ if (_window) _window->update(); }); + + auto timer = new QTimer(); + QObject::connect(timer, &QTimer::timeout, this, &QQuickOpenGL::shiftChanged); + timer->start(100); } void QQuickOpenGL::handleWindowChanged(QQuickWindow* window) @@ -36,75 +94,17 @@ void QQuickOpenGL::handleWindowChanged(QQuickWindow* window) _window = window; window->setClearBeforeRendering(false); - // window->setPersistentOpenGLContext(true); - - connect(window, &QQuickWindow::beforeRendering, this, &QQuickOpenGL::paint, Qt::DirectConnection); } -void QQuickOpenGL::paint() + +QSGNode *QQuickOpenGL::updatePaintNode(QSGNode *node, UpdatePaintNodeData *) { - qDebug() << _window; - qDebug() << parentItem(); - - // Check if we are rendering with OpenGL - QSGRendererInterface* rif = window()->rendererInterface(); - Q_ASSERT(rif->graphicsApi() == QSGRendererInterface::OpenGL); - // QSGDefaultRenderContext *rc = static_cast(d->context); - // QRhiCommandBuffer *cb = rc->currentFrameCommandBuffer(); - - initializeOpenGLFunctions(); - if (!_shaderProgram) { - - _shaderProgram.reset(new QOpenGLShaderProgram()); - _shaderProgram->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, - R"( - attribute vec4 vertices; - varying vec2 coords; - void main() { - gl_Position = vertices; - coords = vertices.xy; - } - )"); - - _shaderProgram->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, - R"( - varying vec2 coords; - uniform float shift; - - void main() { - gl_FragColor = vec4(1.0, 0.0, 0.5, 1.0); - } - )"); - - _shaderProgram->bindAttributeLocation("vertices", 0); - _shaderProgram->link(); + QSGSimpleRectNode *n = static_cast(node); + if (!n) { + n = new QSGSimpleRectNode(); + n->setColor(Qt::red); + n->setMaterial(new Material()); } - - float values[] = {1, 1, -1, 1, 1, -1, -1, -1}; - - //_window->beginExternalCommands(); - - // Bind with the actual OpenGL thread - _shaderProgram->bind(); - _shaderProgram->enableAttributeArray(0); - _shaderProgram->setAttributeArray(0, GL_FLOAT, values, 2); - _shaderProgram->setUniformValue("shift", static_cast((_shift % 100) / 100.0f)); - - // We need to fix the opengl origin with our actual item position - auto point = parentItem()->mapFromItem(this, {0, 0}); - point += QPointF {0, static_cast(parentItem()->height() - height())}; - qDebug() << width() << height(); - // glInitDisplayMode(GL_RGBA | GL_ALPHA); - glViewport(point.x(), point.y(), width(), height()); - glDisable(GL_DEPTH_TEST); - glClearColor(0, 0, 0, 0); - glClear(GL_COLOR_BUFFER_BIT); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE); - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - - _shaderProgram->disableAttributeArray(0); - _shaderProgram->release(); - - _window->resetOpenGLState(); + n->setRect(boundingRect()); + return n; } diff --git a/src/qquickcomponents/qquickopengl.h b/src/qquickcomponents/qquickopengl.h index 83f8b40e0..0068ac05b 100644 --- a/src/qquickcomponents/qquickopengl.h +++ b/src/qquickcomponents/qquickopengl.h @@ -4,27 +4,31 @@ #include #include -class QOpenGLShaderProgram; +#include +#include +#include -class QQuickOpenGL : public QQuickItem, protected QOpenGLFunctions { +class QQuickOpenGL : public QQuickItem/*, QSGMaterial, QSGMaterialShader*/ +{ Q_OBJECT Q_PROPERTY(int shift READ shift WRITE setShift NOTIFY shiftChanged) public: - QQuickOpenGL(QQuickItem* parent = nullptr); + QQuickOpenGL(QQuickItem *parent = nullptr); void paint(); - void handleWindowChanged(QQuickWindow* window); + void handleWindowChanged(QQuickWindow *window); int shift() { return _shift; } - void setShift(int shift) - { - if (_shift != shift) { + void setShift(int shift) { + if(_shift != shift) { _shift = shift; emit shiftChanged(); } } + QSGNode* updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override; + signals: void shiftChanged(); @@ -32,4 +36,64 @@ class QQuickOpenGL : public QQuickItem, protected QOpenGLFunctions { QSharedPointer _shaderProgram; int _shift; QQuickWindow* _window; + +/* + +public: + const char *vertexShader() const { + return + R"( + uniform highp mat4 matrix; + attribute highp vec4 vertex; + attribute highp vec2 qt_MultiTexCoord0; + varying highp vec2 coord; + void main() { + coord = qt_MultiTexCoord0; + gl_Position = matrix * vertex; + } + + )"; + } + + const char *fragmentShader() const { + return + R"( + uniform lowp float opacity; + uniform vec2 coord; + void main() { + gl_FragColor = vec4(1, coord.x, 0, 1) * opacity; + } + )"; + } + + char const *const *attributeNames() const + { + static char const *const names[] = { "vertex", 0 }; + return names; + } + + void initialize() + { + QSGMaterialShader::initialize(); + m_id_matrix = program()->uniformLocation("matrix"); + m_id_opacity = program()->uniformLocation("opacity"); + } + + void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) + { + program()->enableAttributeArray(0); + Q_ASSERT(program()->isLinked()); + if (state.isMatrixDirty()) + program()->setUniformValue(m_id_matrix, state.combinedMatrix()); + if (state.isOpacityDirty()) + program()->setUniformValue(m_id_opacity, state.opacity()); + } + + QSGMaterialType *type() const { static QSGMaterialType type; return &type; } + QSGMaterialShader *createShader() const { return const_cast(this); } + +private: + int m_id_matrix; + int m_id_opacity; + */ }; From 94bbb3037268e4b07f2682806791f739428e257b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 7 Jan 2020 11:57:05 -0300 Subject: [PATCH 08/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- resources.qrc | 2 ++ src/qquickcomponents/qquickopengl.cpp | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/resources.qrc b/resources.qrc index 9a07717b4..7a254f0e6 100644 --- a/resources.qrc +++ b/resources.qrc @@ -69,6 +69,8 @@ qml/opengl/polarplot/fragment.glsl qml/opengl/polarplot/vertex.glsl + qml/opengl/qquickopengl/fragment.glsl + qml/opengl/qquickopengl/vertex.glsl qml/opengl/waterfallplot/fragment.glsl qml/opengl/waterfallplot/vertex.glsl diff --git a/src/qquickcomponents/qquickopengl.cpp b/src/qquickcomponents/qquickopengl.cpp index d2676aff6..d4f247910 100644 --- a/src/qquickcomponents/qquickopengl.cpp +++ b/src/qquickcomponents/qquickopengl.cpp @@ -13,8 +13,8 @@ class Shader : public QSGMaterialShader public: Shader() { - setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/opengl/waterfallplot/fragment.glsl")); - setShaderSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/opengl/waterfallplot/vertex.glsl")); + setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/opengl/qquickopengl/fragment.glsl")); + setShaderSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/opengl/qquickopengl/vertex.glsl")); } /** @@ -83,7 +83,7 @@ QQuickOpenGL::QQuickOpenGL(QQuickItem *parent) auto timer = new QTimer(); QObject::connect(timer, &QTimer::timeout, this, &QQuickOpenGL::shiftChanged); - timer->start(100); + timer->start(1000/60); } void QQuickOpenGL::handleWindowChanged(QQuickWindow* window) From 0102e0787100ea41b2f0ddec1e81a3ef253498b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 6 Jan 2020 10:50:38 -0300 Subject: [PATCH 09/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/qquickcomponents/qquickopengl.cpp | 4 +- src/qquickcomponents/qquickopengl.h | 63 --------------------------- 2 files changed, 2 insertions(+), 65 deletions(-) diff --git a/src/qquickcomponents/qquickopengl.cpp b/src/qquickcomponents/qquickopengl.cpp index d4f247910..3fe25345c 100644 --- a/src/qquickcomponents/qquickopengl.cpp +++ b/src/qquickcomponents/qquickopengl.cpp @@ -7,6 +7,7 @@ #include #include +#include class Shader : public QSGMaterialShader { @@ -32,7 +33,6 @@ class Shader : public QSGMaterialShader void initialize() { QSGMaterialShader::initialize(); - m_id_matrix = program()->uniformLocation("qt_Matrix"); m_id_opacity = program()->uniformLocation("qt_Opacity"); m_id_shift = program()->uniformLocation("shift"); @@ -59,7 +59,7 @@ class Shader : public QSGMaterialShader int m_id_shift; float shift = 0; - QSGMaterial* material; + //QSGMaterial* material; }; class Material : public QSGMaterial diff --git a/src/qquickcomponents/qquickopengl.h b/src/qquickcomponents/qquickopengl.h index 0068ac05b..1e52beb69 100644 --- a/src/qquickcomponents/qquickopengl.h +++ b/src/qquickcomponents/qquickopengl.h @@ -1,12 +1,9 @@ #pragma once -#include #include #include #include -#include -#include class QQuickOpenGL : public QQuickItem/*, QSGMaterial, QSGMaterialShader*/ { @@ -36,64 +33,4 @@ class QQuickOpenGL : public QQuickItem/*, QSGMaterial, QSGMaterialShader*/ QSharedPointer _shaderProgram; int _shift; QQuickWindow* _window; - -/* - -public: - const char *vertexShader() const { - return - R"( - uniform highp mat4 matrix; - attribute highp vec4 vertex; - attribute highp vec2 qt_MultiTexCoord0; - varying highp vec2 coord; - void main() { - coord = qt_MultiTexCoord0; - gl_Position = matrix * vertex; - } - - )"; - } - - const char *fragmentShader() const { - return - R"( - uniform lowp float opacity; - uniform vec2 coord; - void main() { - gl_FragColor = vec4(1, coord.x, 0, 1) * opacity; - } - )"; - } - - char const *const *attributeNames() const - { - static char const *const names[] = { "vertex", 0 }; - return names; - } - - void initialize() - { - QSGMaterialShader::initialize(); - m_id_matrix = program()->uniformLocation("matrix"); - m_id_opacity = program()->uniformLocation("opacity"); - } - - void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) - { - program()->enableAttributeArray(0); - Q_ASSERT(program()->isLinked()); - if (state.isMatrixDirty()) - program()->setUniformValue(m_id_matrix, state.combinedMatrix()); - if (state.isOpacityDirty()) - program()->setUniformValue(m_id_opacity, state.opacity()); - } - - QSGMaterialType *type() const { static QSGMaterialType type; return &type; } - QSGMaterialShader *createShader() const { return const_cast(this); } - -private: - int m_id_matrix; - int m_id_opacity; - */ }; From 2a7846e1a10c7edbc99b4132c6231a2baef9650f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 6 Jan 2020 11:45:26 -0300 Subject: [PATCH 10/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/qquickcomponents/qquickopengl.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/qquickcomponents/qquickopengl.cpp b/src/qquickcomponents/qquickopengl.cpp index 3fe25345c..08a360b12 100644 --- a/src/qquickcomponents/qquickopengl.cpp +++ b/src/qquickcomponents/qquickopengl.cpp @@ -58,8 +58,6 @@ class Shader : public QSGMaterialShader int m_id_opacity; int m_id_shift; float shift = 0; - - //QSGMaterial* material; }; class Material : public QSGMaterial From 346ed04af8e2d5d98aaa849d8b8b79c2e8bc573a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 6 Jan 2020 11:56:05 -0300 Subject: [PATCH 11/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- qml/MainPage.qml | 4 ++-- src/qquickcomponents/qquickopengl.cpp | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/qml/MainPage.qml b/qml/MainPage.qml index a2295111d..fdbc33483 100644 --- a/qml/MainPage.qml +++ b/qml/MainPage.qml @@ -264,8 +264,8 @@ Item { QQuickOpenGL { id: potato - width: 400 - height: 300 + width: 50 + height: 50 anchors.top: parent.top visible: true z: 10000000 diff --git a/src/qquickcomponents/qquickopengl.cpp b/src/qquickcomponents/qquickopengl.cpp index 08a360b12..bf0b99e75 100644 --- a/src/qquickcomponents/qquickopengl.cpp +++ b/src/qquickcomponents/qquickopengl.cpp @@ -34,30 +34,32 @@ class Shader : public QSGMaterialShader { QSGMaterialShader::initialize(); m_id_opacity = program()->uniformLocation("qt_Opacity"); + /* m_id_shift = program()->uniformLocation("shift"); auto timer = new QTimer(); QObject::connect(timer, &QTimer::timeout, [&] { shift += 0.01; } ); timer->start(10); + */ } void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) { qDebug() << newMaterial << oldMaterial; Q_ASSERT(program()->isLinked()); - if (state.isMatrixDirty()) - program()->setUniformValue(m_id_matrix, state.combinedMatrix()); if (state.isOpacityDirty()) program()->setUniformValue(m_id_opacity, state.opacity()); + /* qDebug() << "update"; program()->setUniformValue(m_id_shift, shift); + */ } private: int m_id_matrix; int m_id_opacity; int m_id_shift; - float shift = 0; + //float shift = 0; }; class Material : public QSGMaterial @@ -100,7 +102,6 @@ QSGNode *QQuickOpenGL::updatePaintNode(QSGNode *node, UpdatePaintNodeData *) QSGSimpleRectNode *n = static_cast(node); if (!n) { n = new QSGSimpleRectNode(); - n->setColor(Qt::red); n->setMaterial(new Material()); } n->setRect(boundingRect()); From b9ae3e31177dc20f2412b9dd5d3a646ce9c53641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 7 Jan 2020 11:14:42 -0300 Subject: [PATCH 12/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- qml/DeviceManagerViewer.qml | 2 +- qml/MainPage.qml | 13 +++++--- qml/NoControlPanel.qml | 2 +- qml/main.qml | 1 + src/qquickcomponents/qquickopengl.cpp | 47 ++++++++++++++++++++++++--- 5 files changed, 55 insertions(+), 10 deletions(-) diff --git a/qml/DeviceManagerViewer.qml b/qml/DeviceManagerViewer.qml index 21bdcbf4a..3ebbecf67 100644 --- a/qml/DeviceManagerViewer.qml +++ b/qml/DeviceManagerViewer.qml @@ -17,7 +17,7 @@ PingPopup { Component.onCompleted: { if(!DeviceManager.primarySensor) { - open() + //open() } } diff --git a/qml/MainPage.qml b/qml/MainPage.qml index fdbc33483..87d770bb9 100644 --- a/qml/MainPage.qml +++ b/qml/MainPage.qml @@ -65,7 +65,7 @@ Item { onClickedChanged: { if(clicked) { - deviceManagerViewer.open() + //deviceManagerViewer.open() settingsMenu.hideItem = true } } @@ -264,9 +264,14 @@ Item { QQuickOpenGL { id: potato - width: 50 - height: 50 - anchors.top: parent.top + //width: 200 + //height: 200 + anchors.centerIn: parent + //anchors.fill: parent + width: Math.min(parent.height, parent.width) + height: width + //width: 200 + //height: 200 visible: true z: 10000000 } diff --git a/qml/NoControlPanel.qml b/qml/NoControlPanel.qml index 0bd6e0978..1bc116476 100644 --- a/qml/NoControlPanel.qml +++ b/qml/NoControlPanel.qml @@ -14,7 +14,7 @@ PingGroupBox { text: "Device Manager" Layout.fillWidth: true onClicked: { - deviceManagerViewer.open() + //deviceManagerViewer.open() root.parent.hideItem = true } } diff --git a/qml/main.qml b/qml/main.qml index ef7f73e6b..ccdbcdadc 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -29,6 +29,7 @@ ApplicationWindow { width: parent.width/2 height: parent.height*0.9 anchors.centerIn: parent + visible: false } } diff --git a/src/qquickcomponents/qquickopengl.cpp b/src/qquickcomponents/qquickopengl.cpp index bf0b99e75..4d601f411 100644 --- a/src/qquickcomponents/qquickopengl.cpp +++ b/src/qquickcomponents/qquickopengl.cpp @@ -26,13 +26,14 @@ class Shader : public QSGMaterialShader */ char const *const *attributeNames() const { - static char const *const names[] = {"qt_Vertex", 0 }; + static char const *const names[] = {"qt_Vertex", "qt_MultiTexCoord0", 0 }; return names; } void initialize() { QSGMaterialShader::initialize(); + m_id_matrix = program()->uniformLocation("qt_Matrix"); m_id_opacity = program()->uniformLocation("qt_Opacity"); /* m_id_shift = program()->uniformLocation("shift"); @@ -47,8 +48,21 @@ class Shader : public QSGMaterialShader { qDebug() << newMaterial << oldMaterial; Q_ASSERT(program()->isLinked()); - if (state.isOpacityDirty()) + if (state.isOpacityDirty()) { program()->setUniformValue(m_id_opacity, state.opacity()); + } + if (state.isMatrixDirty()) { + program()->setUniformValue(m_id_matrix, state.combinedMatrix()); + } + + qDebug() << "MATRIX combined:" << state.combinedMatrix(); + qDebug() << "MATRIX modelview:" << state.modelViewMatrix(); + qDebug() << "MATRIX projection:" << state.projectionMatrix(); + //qt_ModelViewProjectionMatrix + qDebug() << "RECT1:" << state.deviceRect(); + qDebug() << "RECT:" << state.viewportRect(); + qDebug() << "PIXEL:" << state.devicePixelRatio(); + /* qDebug() << "update"; program()->setUniformValue(m_id_shift, shift); @@ -79,11 +93,26 @@ QQuickOpenGL::QQuickOpenGL(QQuickItem *parent) QQuickItem::setFlag(QQuickItem::ItemHasContents); connect(this, &QQuickItem::windowChanged, this, &QQuickOpenGL::handleWindowChanged); - connect(this, &QQuickOpenGL::shiftChanged, this, [this]{ if (_window) _window->update(); }); + connect(this, &QQuickOpenGL::shiftChanged, this, [this]{ + if (_window) { + _window->update(); + } + //qDebug() << boundingRect(); + }); auto timer = new QTimer(); - QObject::connect(timer, &QTimer::timeout, this, &QQuickOpenGL::shiftChanged); + connect(timer, &QTimer::timeout, this, &QQuickOpenGL::shiftChanged); timer->start(1000/60); + + connect(this, &QQuickItem::childrenRectChanged, this, [this](auto rect) { + /* + if(!node) { + return; + } + QSGGeometry::updateTexturedRectGeometry(node->geometry(), boundingRect(), QRectF(0, 0, 1, 1)); + node->markDirty(QSGNode::DirtyGeometry); + */ + }); } void QQuickOpenGL::handleWindowChanged(QQuickWindow* window) @@ -99,10 +128,20 @@ void QQuickOpenGL::handleWindowChanged(QQuickWindow* window) QSGNode *QQuickOpenGL::updatePaintNode(QSGNode *node, UpdatePaintNodeData *) { + qDebug() << __PRETTY_FUNCTION__; QSGSimpleRectNode *n = static_cast(node); if (!n) { n = new QSGSimpleRectNode(); n->setMaterial(new Material()); + n->material()->setFlag(QSGMaterial::Blending); + n->setFlag(QSGNode::OwnsMaterial, true); + QSGGeometry *g = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4); + QSGGeometry::updateTexturedRectGeometry(g, boundingRect(), QRectF(0, 0, 1, 1)); + n->setGeometry(g); + n->setFlag(QSGNode::OwnsGeometry, true); + } else { + QSGGeometry::updateTexturedRectGeometry(n->geometry(), boundingRect(), QRectF(0, 0, 1, 1)); + n->markDirty(QSGNode::DirtyGeometry); } n->setRect(boundingRect()); return n; From d698362f607df1fed9038ff11ddfa814a85fb371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 7 Jan 2020 11:18:23 -0300 Subject: [PATCH 13/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/qquickcomponents/qquickopengl.cpp | 29 +++++++++++++-------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/qquickcomponents/qquickopengl.cpp b/src/qquickcomponents/qquickopengl.cpp index 4d601f411..d048d17f2 100644 --- a/src/qquickcomponents/qquickopengl.cpp +++ b/src/qquickcomponents/qquickopengl.cpp @@ -128,21 +128,20 @@ void QQuickOpenGL::handleWindowChanged(QQuickWindow* window) QSGNode *QQuickOpenGL::updatePaintNode(QSGNode *node, UpdatePaintNodeData *) { - qDebug() << __PRETTY_FUNCTION__; - QSGSimpleRectNode *n = static_cast(node); - if (!n) { - n = new QSGSimpleRectNode(); - n->setMaterial(new Material()); - n->material()->setFlag(QSGMaterial::Blending); - n->setFlag(QSGNode::OwnsMaterial, true); - QSGGeometry *g = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4); - QSGGeometry::updateTexturedRectGeometry(g, boundingRect(), QRectF(0, 0, 1, 1)); - n->setGeometry(g); - n->setFlag(QSGNode::OwnsGeometry, true); + QSGSimpleRectNode *simpleRectNode = static_cast(node); + if (!simpleRectNode) { + simpleRectNode = new QSGSimpleRectNode(); + simpleRectNode->setMaterial(new Material()); + simpleRectNode->material()->setFlag(QSGMaterial::Blending); + simpleRectNode->setFlag(QSGNode::OwnsMaterial, true); + QSGGeometry *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4); + QSGGeometry::updateTexturedRectGeometry(geometry, boundingRect(), QRectF(0, 0, 1, 1)); + simpleRectNode->setGeometry(geometry); + simpleRectNode->setFlag(QSGNode::OwnsGeometry, true); } else { - QSGGeometry::updateTexturedRectGeometry(n->geometry(), boundingRect(), QRectF(0, 0, 1, 1)); - n->markDirty(QSGNode::DirtyGeometry); + QSGGeometry::updateTexturedRectGeometry(simpleRectNode->geometry(), boundingRect(), QRectF(0, 0, 1, 1)); + simpleRectNode->markDirty(QSGNode::DirtyGeometry); } - n->setRect(boundingRect()); - return n; + simpleRectNode->setRect(boundingRect()); + return simpleRectNode; } From 2603bbf24be77f274e48f6bb076b31eddc5d0f07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 7 Jan 2020 11:25:31 -0300 Subject: [PATCH 14/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/qquickcomponents/qquickopengl.cpp | 36 ++------------------------- src/qquickcomponents/qquickopengl.h | 2 +- 2 files changed, 3 insertions(+), 35 deletions(-) diff --git a/src/qquickcomponents/qquickopengl.cpp b/src/qquickcomponents/qquickopengl.cpp index d048d17f2..384712f7a 100644 --- a/src/qquickcomponents/qquickopengl.cpp +++ b/src/qquickcomponents/qquickopengl.cpp @@ -35,18 +35,10 @@ class Shader : public QSGMaterialShader QSGMaterialShader::initialize(); m_id_matrix = program()->uniformLocation("qt_Matrix"); m_id_opacity = program()->uniformLocation("qt_Opacity"); - /* - m_id_shift = program()->uniformLocation("shift"); - - auto timer = new QTimer(); - QObject::connect(timer, &QTimer::timeout, [&] { shift += 0.01; } ); - timer->start(10); - */ } void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) { - qDebug() << newMaterial << oldMaterial; Q_ASSERT(program()->isLinked()); if (state.isOpacityDirty()) { program()->setUniformValue(m_id_opacity, state.opacity()); @@ -54,26 +46,11 @@ class Shader : public QSGMaterialShader if (state.isMatrixDirty()) { program()->setUniformValue(m_id_matrix, state.combinedMatrix()); } - - qDebug() << "MATRIX combined:" << state.combinedMatrix(); - qDebug() << "MATRIX modelview:" << state.modelViewMatrix(); - qDebug() << "MATRIX projection:" << state.projectionMatrix(); - //qt_ModelViewProjectionMatrix - qDebug() << "RECT1:" << state.deviceRect(); - qDebug() << "RECT:" << state.viewportRect(); - qDebug() << "PIXEL:" << state.devicePixelRatio(); - - /* - qDebug() << "update"; - program()->setUniformValue(m_id_shift, shift); - */ } private: int m_id_matrix; int m_id_opacity; - int m_id_shift; - //float shift = 0; }; class Material : public QSGMaterial @@ -93,26 +70,17 @@ QQuickOpenGL::QQuickOpenGL(QQuickItem *parent) QQuickItem::setFlag(QQuickItem::ItemHasContents); connect(this, &QQuickItem::windowChanged, this, &QQuickOpenGL::handleWindowChanged); + /* connect(this, &QQuickOpenGL::shiftChanged, this, [this]{ if (_window) { _window->update(); } - //qDebug() << boundingRect(); }); auto timer = new QTimer(); connect(timer, &QTimer::timeout, this, &QQuickOpenGL::shiftChanged); timer->start(1000/60); - - connect(this, &QQuickItem::childrenRectChanged, this, [this](auto rect) { - /* - if(!node) { - return; - } - QSGGeometry::updateTexturedRectGeometry(node->geometry(), boundingRect(), QRectF(0, 0, 1, 1)); - node->markDirty(QSGNode::DirtyGeometry); - */ - }); + */ } void QQuickOpenGL::handleWindowChanged(QQuickWindow* window) diff --git a/src/qquickcomponents/qquickopengl.h b/src/qquickcomponents/qquickopengl.h index 1e52beb69..150851e52 100644 --- a/src/qquickcomponents/qquickopengl.h +++ b/src/qquickcomponents/qquickopengl.h @@ -5,7 +5,7 @@ #include -class QQuickOpenGL : public QQuickItem/*, QSGMaterial, QSGMaterialShader*/ +class QQuickOpenGL : public QQuickItem { Q_OBJECT Q_PROPERTY(int shift READ shift WRITE setShift NOTIFY shiftChanged) From c32e9e9681b182a197059c80064186189066d953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 7 Jan 2020 11:45:50 -0300 Subject: [PATCH 15/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- qml/DeviceManagerViewer.qml | 2 +- qml/MainPage.qml | 5 +++-- qml/NoControlPanel.qml | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/qml/DeviceManagerViewer.qml b/qml/DeviceManagerViewer.qml index 3ebbecf67..21bdcbf4a 100644 --- a/qml/DeviceManagerViewer.qml +++ b/qml/DeviceManagerViewer.qml @@ -17,7 +17,7 @@ PingPopup { Component.onCompleted: { if(!DeviceManager.primarySensor) { - //open() + open() } } diff --git a/qml/MainPage.qml b/qml/MainPage.qml index 87d770bb9..ef113743a 100644 --- a/qml/MainPage.qml +++ b/qml/MainPage.qml @@ -65,7 +65,7 @@ Item { onClickedChanged: { if(clicked) { - //deviceManagerViewer.open() + deviceManagerViewer.open() settingsMenu.hideItem = true } } @@ -262,6 +262,7 @@ Item { radialGradient : sensorVisualizer.toString().includes("Ping360") ? radialGradient : linearGradient } + /* QQuickOpenGL { id: potato //width: 200 @@ -274,7 +275,7 @@ Item { //height: 200 visible: true z: 10000000 - } + }*/ PingStatus { // We need to be over the mainvisualizer diff --git a/qml/NoControlPanel.qml b/qml/NoControlPanel.qml index 1bc116476..0bd6e0978 100644 --- a/qml/NoControlPanel.qml +++ b/qml/NoControlPanel.qml @@ -14,7 +14,7 @@ PingGroupBox { text: "Device Manager" Layout.fillWidth: true onClicked: { - //deviceManagerViewer.open() + deviceManagerViewer.open() root.parent.hideItem = true } } From b1ad64ce98b9e174ca203d90741dd7d171819c95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 8 Jan 2020 09:38:10 -0300 Subject: [PATCH 16/20] qquickcomponents MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/qquickcomponents/CMakeLists.txt | 11 +++++++++++ src/qquickcomponents/qquickcomponents.pri | 7 +++++++ 2 files changed, 18 insertions(+) create mode 100644 src/qquickcomponents/CMakeLists.txt create mode 100644 src/qquickcomponents/qquickcomponents.pri diff --git a/src/qquickcomponents/CMakeLists.txt b/src/qquickcomponents/CMakeLists.txt new file mode 100644 index 000000000..acc49ea52 --- /dev/null +++ b/src/qquickcomponents/CMakeLists.txt @@ -0,0 +1,11 @@ +add_library( + qquickcomponents +STATIC + qquickopengl.cpp +) + +target_link_libraries(qquickcomponents +PRIVATE + Qt5::Core + Qt5::Quick +) diff --git a/src/qquickcomponents/qquickcomponents.pri b/src/qquickcomponents/qquickcomponents.pri new file mode 100644 index 000000000..f1d7a6eae --- /dev/null +++ b/src/qquickcomponents/qquickcomponents.pri @@ -0,0 +1,7 @@ +INCLUDEPATH += $$PWD + +HEADERS += \ + $$PWD/*.h + +SOURCES += \ + $$PWD/*.cpp From ee2650696cfc0e40a17bc4c7ebca02b93a442b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 8 Jan 2020 10:21:37 -0300 Subject: [PATCH 17/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- qml/opengl/qquickopengl/fragment.glsl | 10 ++++++++++ qml/opengl/qquickopengl/vertex.glsl | 11 +++++++++++ 2 files changed, 21 insertions(+) create mode 100644 qml/opengl/qquickopengl/fragment.glsl create mode 100644 qml/opengl/qquickopengl/vertex.glsl diff --git a/qml/opengl/qquickopengl/fragment.glsl b/qml/opengl/qquickopengl/fragment.glsl new file mode 100644 index 000000000..17af87ff4 --- /dev/null +++ b/qml/opengl/qquickopengl/fragment.glsl @@ -0,0 +1,10 @@ +varying highp vec2 coord; +uniform lowp float qt_Opacity; +void main() { + float d = distance(coord, vec2(0.5, 0.5)); + if ( d > 0.5) { + gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0) * qt_Opacity; + return; + } + gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) * qt_Opacity; +} diff --git a/qml/opengl/qquickopengl/vertex.glsl b/qml/opengl/qquickopengl/vertex.glsl new file mode 100644 index 000000000..4ba495da2 --- /dev/null +++ b/qml/opengl/qquickopengl/vertex.glsl @@ -0,0 +1,11 @@ +uniform highp mat4 qt_Matrix; + +attribute highp vec4 qt_Vertex; +attribute highp vec2 qt_MultiTexCoord0; + +varying highp vec2 coord; + +void main() { + gl_Position = qt_Matrix * qt_Vertex; + coord = qt_MultiTexCoord0;// + vec2(1.0, 1.0)) / 2.0; +} From f8c7e23f24e06f69ec375ad5db4944158ed3af5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 8 Jan 2020 10:49:32 -0300 Subject: [PATCH 18/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- qml/MainPage.qml | 3 +- src/qquickcomponents/qquickopengl.cpp | 80 ++++++++++++++++++--------- src/qquickcomponents/qquickopengl.h | 32 +++++++++++ 3 files changed, 88 insertions(+), 27 deletions(-) diff --git a/qml/MainPage.qml b/qml/MainPage.qml index ef113743a..a46aef649 100644 --- a/qml/MainPage.qml +++ b/qml/MainPage.qml @@ -262,7 +262,6 @@ Item { radialGradient : sensorVisualizer.toString().includes("Ping360") ? radialGradient : linearGradient } - /* QQuickOpenGL { id: potato //width: 200 @@ -275,7 +274,7 @@ Item { //height: 200 visible: true z: 10000000 - }*/ + } PingStatus { // We need to be over the mainvisualizer diff --git a/src/qquickcomponents/qquickopengl.cpp b/src/qquickcomponents/qquickopengl.cpp index 384712f7a..fea2b1814 100644 --- a/src/qquickcomponents/qquickopengl.cpp +++ b/src/qquickcomponents/qquickopengl.cpp @@ -7,34 +7,25 @@ #include #include -#include +/* +struct MaterialState +{ + QSGTexture *source; +}; -class Shader : public QSGMaterialShader +class MaterialShader : public QSGSimpleMaterialShader { -public: - Shader() + QSG_DECLARE_SIMPLE_SHADER(MaterialShader, MaterialState) + + MaterialShader() { setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/opengl/qquickopengl/fragment.glsl")); setShaderSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/opengl/qquickopengl/vertex.glsl")); } - /** - * @brief Return attribute names - * The last element should be zero, from documentation - * - * @return char const* const* - */ - char const *const *attributeNames() const + QList attributes() const { - static char const *const names[] = {"qt_Vertex", "qt_MultiTexCoord0", 0 }; - return names; - } - - void initialize() - { - QSGMaterialShader::initialize(); - m_id_matrix = program()->uniformLocation("qt_Matrix"); - m_id_opacity = program()->uniformLocation("qt_Opacity"); + return QList() << "qt_Vertex" << "qt_MultiTexCoord0"; } void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) @@ -47,19 +38,58 @@ class Shader : public QSGMaterialShader program()->setUniformValue(m_id_matrix, state.combinedMatrix()); } } - -private: - int m_id_matrix; - int m_id_opacity; -}; +}*/ class Material : public QSGMaterial { public: QSGMaterialType *type() const { static QSGMaterialType type; return &type; } QSGMaterialShader *createShader() const { return new Shader; } + int toto() { return i++; } +private: + int i; }; +char const *const *Shader::attributeNames() const +{ + static char const *const names[] = {"qt_Vertex", "qt_MultiTexCoord0", 0 }; + return names; +} + +void Shader::loadDefaultShader() +{ + setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/opengl/qquickopengl/fragment.glsl")); + setShaderSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/opengl/qquickopengl/vertex.glsl")); +} + +void Shader::initialize() +{ + QSGMaterialShader::initialize(); + _matrixId = program()->uniformLocation("qt_Matrix"); + _opacityId = program()->uniformLocation("qt_Opacity"); +} + +void Shader::updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) +{ + Q_ASSERT(program()->isLinked()); + if (state.isMatrixDirty()) { + program()->setUniformValue(_matrixId, state.combinedMatrix()); + } + if (state.isOpacityDirty()) { + program()->setUniformValue(_opacityId, state.opacity()); + } + + // Let the child class to receive and do the necessary updates with the new state + update(state, newMaterial, oldMaterial); +} + +void Shader::update(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) +{ + Q_UNUSED(state) + Q_UNUSED(newMaterial) + Q_UNUSED(oldMaterial) +} + QQuickOpenGL::QQuickOpenGL(QQuickItem *parent) : QQuickItem(parent) , _shaderProgram(nullptr) diff --git a/src/qquickcomponents/qquickopengl.h b/src/qquickcomponents/qquickopengl.h index 150851e52..e8fd1043e 100644 --- a/src/qquickcomponents/qquickopengl.h +++ b/src/qquickcomponents/qquickopengl.h @@ -5,6 +5,38 @@ #include +#include + +#include + +class Shader : public QSGMaterialShader +{ +public: + Shader() + { + loadDefaultShader(); + } + + /** + * @brief Return attribute names + * The last element should be zero, from documentation + * + * @return char const* const* + */ + char const *const *attributeNames() const; + + void loadDefaultShader(); + + void initialize(); + + void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial); + virtual void update(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial); + +private: + int _matrixId; + int _opacityId; +}; + class QQuickOpenGL : public QQuickItem { Q_OBJECT From 7e4f02095f582ea8c19f3e42f88b0f3a2f7d9bf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 8 Jan 2020 10:57:55 -0300 Subject: [PATCH 19/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/qquickcomponents/qquickopengl.cpp | 7 ------- src/qquickcomponents/qquickopengl.h | 8 +++++++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/qquickcomponents/qquickopengl.cpp b/src/qquickcomponents/qquickopengl.cpp index fea2b1814..ad8d6e27f 100644 --- a/src/qquickcomponents/qquickopengl.cpp +++ b/src/qquickcomponents/qquickopengl.cpp @@ -83,13 +83,6 @@ void Shader::updateState(const RenderState &state, QSGMaterial *newMaterial, QSG update(state, newMaterial, oldMaterial); } -void Shader::update(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) -{ - Q_UNUSED(state) - Q_UNUSED(newMaterial) - Q_UNUSED(oldMaterial) -} - QQuickOpenGL::QQuickOpenGL(QQuickItem *parent) : QQuickItem(parent) , _shaderProgram(nullptr) diff --git a/src/qquickcomponents/qquickopengl.h b/src/qquickcomponents/qquickopengl.h index e8fd1043e..3cd281e72 100644 --- a/src/qquickcomponents/qquickopengl.h +++ b/src/qquickcomponents/qquickopengl.h @@ -30,7 +30,13 @@ class Shader : public QSGMaterialShader void initialize(); void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial); - virtual void update(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial); + + virtual void update(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) + { + Q_UNUSED(state) + Q_UNUSED(newMaterial) + Q_UNUSED(oldMaterial) + } private: int _matrixId; From 3de432a9ce91fe53f2ab67af4ea9de23cb7b3a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 8 Jan 2020 11:02:32 -0300 Subject: [PATCH 20/20] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/qquickcomponents/qquickopengl.cpp | 17 ++++------------- src/qquickcomponents/qquickopengl.h | 9 --------- 2 files changed, 4 insertions(+), 22 deletions(-) diff --git a/src/qquickcomponents/qquickopengl.cpp b/src/qquickcomponents/qquickopengl.cpp index ad8d6e27f..5512b415d 100644 --- a/src/qquickcomponents/qquickopengl.cpp +++ b/src/qquickcomponents/qquickopengl.cpp @@ -86,24 +86,11 @@ void Shader::updateState(const RenderState &state, QSGMaterial *newMaterial, QSG QQuickOpenGL::QQuickOpenGL(QQuickItem *parent) : QQuickItem(parent) , _shaderProgram(nullptr) - , _shift(0) , _window(nullptr) { // We are going to fill our own content with OpenGL QQuickItem::setFlag(QQuickItem::ItemHasContents); - connect(this, &QQuickItem::windowChanged, this, &QQuickOpenGL::handleWindowChanged); - /* - connect(this, &QQuickOpenGL::shiftChanged, this, [this]{ - if (_window) { - _window->update(); - } - }); - - auto timer = new QTimer(); - connect(timer, &QTimer::timeout, this, &QQuickOpenGL::shiftChanged); - timer->start(1000/60); - */ } void QQuickOpenGL::handleWindowChanged(QQuickWindow* window) @@ -121,18 +108,22 @@ QSGNode *QQuickOpenGL::updatePaintNode(QSGNode *node, UpdatePaintNodeData *) { QSGSimpleRectNode *simpleRectNode = static_cast(node); if (!simpleRectNode) { + // We are going to deal with the material and the geometry simpleRectNode = new QSGSimpleRectNode(); simpleRectNode->setMaterial(new Material()); simpleRectNode->material()->setFlag(QSGMaterial::Blending); simpleRectNode->setFlag(QSGNode::OwnsMaterial, true); + QSGGeometry *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4); QSGGeometry::updateTexturedRectGeometry(geometry, boundingRect(), QRectF(0, 0, 1, 1)); simpleRectNode->setGeometry(geometry); simpleRectNode->setFlag(QSGNode::OwnsGeometry, true); } else { + // Update the geometry to make sure that we have the correct size QSGGeometry::updateTexturedRectGeometry(simpleRectNode->geometry(), boundingRect(), QRectF(0, 0, 1, 1)); simpleRectNode->markDirty(QSGNode::DirtyGeometry); } + simpleRectNode->setRect(boundingRect()); return simpleRectNode; } diff --git a/src/qquickcomponents/qquickopengl.h b/src/qquickcomponents/qquickopengl.h index 3cd281e72..950545388 100644 --- a/src/qquickcomponents/qquickopengl.h +++ b/src/qquickcomponents/qquickopengl.h @@ -46,7 +46,6 @@ class Shader : public QSGMaterialShader class QQuickOpenGL : public QQuickItem { Q_OBJECT - Q_PROPERTY(int shift READ shift WRITE setShift NOTIFY shiftChanged) public: QQuickOpenGL(QQuickItem *parent = nullptr); @@ -54,14 +53,6 @@ class QQuickOpenGL : public QQuickItem void handleWindowChanged(QQuickWindow *window); - int shift() { return _shift; } - void setShift(int shift) { - if(_shift != shift) { - _shift = shift; - emit shiftChanged(); - } - } - QSGNode* updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override; signals: