diff options
Diffstat (limited to 'examples/quick/scenegraph/shared')
-rw-r--r-- | examples/quick/scenegraph/shared/logorenderer.cpp | 219 | ||||
-rw-r--r-- | examples/quick/scenegraph/shared/logorenderer.h | 41 | ||||
-rw-r--r-- | examples/quick/scenegraph/shared/squircle_rhi.frag | 14 | ||||
-rw-r--r-- | examples/quick/scenegraph/shared/squircle_rhi.vert | 7 |
4 files changed, 14 insertions, 267 deletions
diff --git a/examples/quick/scenegraph/shared/logorenderer.cpp b/examples/quick/scenegraph/shared/logorenderer.cpp deleted file mode 100644 index b95ae0d21d..0000000000 --- a/examples/quick/scenegraph/shared/logorenderer.cpp +++ /dev/null @@ -1,219 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "logorenderer.h" -#include <QPainter> -#include <QPaintEngine> -#include <qmath.h> - -LogoRenderer::LogoRenderer() -{ -} - -LogoRenderer::~LogoRenderer() -{ -} - - -void LogoRenderer::paintQtLogo() -{ - program1.enableAttributeArray(normalAttr1); - program1.enableAttributeArray(vertexAttr1); - program1.setAttributeArray(vertexAttr1, vertices.constData()); - program1.setAttributeArray(normalAttr1, normals.constData()); - glDrawArrays(GL_TRIANGLES, 0, vertices.size()); - program1.disableAttributeArray(normalAttr1); - program1.disableAttributeArray(vertexAttr1); -} - - -void LogoRenderer::initialize() -{ - initializeOpenGLFunctions(); - - glClearColor(0.1f, 0.1f, 0.2f, 1.0f); - - const char *vsrc1 = - "attribute highp vec4 vertex;\n" - "attribute mediump vec3 normal;\n" - "uniform mediump mat4 matrix;\n" - "varying mediump vec4 color;\n" - "void main(void)\n" - "{\n" - " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n" - " float angle = max(dot(normal, toLight), 0.0);\n" - " vec3 col = vec3(0.40, 1.0, 0.0);\n" - " color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);\n" - " color = clamp(color, 0.0, 1.0);\n" - " gl_Position = matrix * vertex;\n" - "}\n"; - - const char *fsrc1 = - "varying mediump vec4 color;\n" - "void main(void)\n" - "{\n" - " gl_FragColor = color;\n" - "}\n"; - - program1.addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, vsrc1); - program1.addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, fsrc1); - program1.link(); - - vertexAttr1 = program1.attributeLocation("vertex"); - normalAttr1 = program1.attributeLocation("normal"); - matrixUniform1 = program1.uniformLocation("matrix"); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - - m_fAngle = 0; - m_fScale = 1; - createGeometry(); -} - -void LogoRenderer::render() -{ - glDepthMask(true); - - glClearColor(0.5f, 0.5f, 0.7f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - - glFrontFace(GL_CW); - glCullFace(GL_FRONT); - glEnable(GL_CULL_FACE); - glEnable(GL_DEPTH_TEST); - - QMatrix4x4 modelview; - modelview.rotate(m_fAngle, 0.0f, 1.0f, 0.0f); - modelview.rotate(m_fAngle, 1.0f, 0.0f, 0.0f); - modelview.rotate(m_fAngle, 0.0f, 0.0f, 1.0f); - modelview.scale(m_fScale); - modelview.translate(0.0f, -0.2f, 0.0f); - - program1.bind(); - program1.setUniformValue(matrixUniform1, modelview); - paintQtLogo(); - program1.release(); - - glDisable(GL_DEPTH_TEST); - glDisable(GL_CULL_FACE); - - m_fAngle += 1.0f; -} - -void LogoRenderer::createGeometry() -{ - vertices.clear(); - normals.clear(); - - qreal x1 = +0.06f; - qreal y1 = -0.14f; - qreal x2 = +0.14f; - qreal y2 = -0.06f; - qreal x3 = +0.08f; - qreal y3 = +0.00f; - qreal x4 = +0.30f; - qreal y4 = +0.22f; - - quad(x1, y1, x2, y2, y2, x2, y1, x1); - quad(x3, y3, x4, y4, y4, x4, y3, x3); - - extrude(x1, y1, x2, y2); - extrude(x2, y2, y2, x2); - extrude(y2, x2, y1, x1); - extrude(y1, x1, x1, y1); - extrude(x3, y3, x4, y4); - extrude(x4, y4, y4, x4); - extrude(y4, x4, y3, x3); - - const qreal Pi = M_PI; - const int NumSectors = 100; - - for (int i = 0; i < NumSectors; ++i) { - qreal angle1 = (i * 2 * Pi) / NumSectors; - qreal x5 = 0.30 * sin(angle1); - qreal y5 = 0.30 * cos(angle1); - qreal x6 = 0.20 * sin(angle1); - qreal y6 = 0.20 * cos(angle1); - - qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors; - qreal x7 = 0.20 * sin(angle2); - qreal y7 = 0.20 * cos(angle2); - qreal x8 = 0.30 * sin(angle2); - qreal y8 = 0.30 * cos(angle2); - - quad(x5, y5, x6, y6, x7, y7, x8, y8); - - extrude(x6, y6, x7, y7); - extrude(x8, y8, x5, y5); - } - - for (int i = 0;i < vertices.size();i++) - vertices[i] *= 2.0f; -} - -void LogoRenderer::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4) -{ - vertices << QVector3D(x1, y1, -0.05f); - vertices << QVector3D(x2, y2, -0.05f); - vertices << QVector3D(x4, y4, -0.05f); - - vertices << QVector3D(x3, y3, -0.05f); - vertices << QVector3D(x4, y4, -0.05f); - vertices << QVector3D(x2, y2, -0.05f); - - QVector3D n = QVector3D::normal - (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(x4 - x1, y4 - y1, 0.0f)); - - normals << n; - normals << n; - normals << n; - - normals << n; - normals << n; - normals << n; - - vertices << QVector3D(x4, y4, 0.05f); - vertices << QVector3D(x2, y2, 0.05f); - vertices << QVector3D(x1, y1, 0.05f); - - vertices << QVector3D(x2, y2, 0.05f); - vertices << QVector3D(x4, y4, 0.05f); - vertices << QVector3D(x3, y3, 0.05f); - - n = QVector3D::normal - (QVector3D(x2 - x4, y2 - y4, 0.0f), QVector3D(x1 - x4, y1 - y4, 0.0f)); - - normals << n; - normals << n; - normals << n; - - normals << n; - normals << n; - normals << n; -} - -void LogoRenderer::extrude(qreal x1, qreal y1, qreal x2, qreal y2) -{ - vertices << QVector3D(x1, y1, +0.05f); - vertices << QVector3D(x2, y2, +0.05f); - vertices << QVector3D(x1, y1, -0.05f); - - vertices << QVector3D(x2, y2, -0.05f); - vertices << QVector3D(x1, y1, -0.05f); - vertices << QVector3D(x2, y2, +0.05f); - - QVector3D n = QVector3D::normal - (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(0.0f, 0.0f, -0.1f)); - - normals << n; - normals << n; - normals << n; - - normals << n; - normals << n; - normals << n; -} diff --git a/examples/quick/scenegraph/shared/logorenderer.h b/examples/quick/scenegraph/shared/logorenderer.h deleted file mode 100644 index 06c49baab1..0000000000 --- a/examples/quick/scenegraph/shared/logorenderer.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef LOGORENDERER_H -#define LOGORENDERER_H - -#include <QtGui/qvector3d.h> -#include <QtGui/qmatrix4x4.h> -#include <qopenglshaderprogram.h> -#include <qopenglfunctions.h> - -#include <QTime> -#include <QList> - -class LogoRenderer : protected QOpenGLFunctions -{ -public: - LogoRenderer(); - ~LogoRenderer(); - - void render(); - void initialize(); - -private: - - qreal m_fAngle; - qreal m_fScale; - - void paintQtLogo(); - void createGeometry(); - void quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4); - void extrude(qreal x1, qreal y1, qreal x2, qreal y2); - - QList<QVector3D> vertices; - QList<QVector3D> normals; - QOpenGLShaderProgram program1; - int vertexAttr1; - int normalAttr1; - int matrixUniform1; -}; -#endif diff --git a/examples/quick/scenegraph/shared/squircle_rhi.frag b/examples/quick/scenegraph/shared/squircle_rhi.frag index 8da62b93e6..83669bdfaa 100644 --- a/examples/quick/scenegraph/shared/squircle_rhi.frag +++ b/examples/quick/scenegraph/shared/squircle_rhi.frag @@ -1,3 +1,6 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + #version 440 layout(location = 0) in vec2 coords; @@ -5,12 +8,13 @@ layout(location = 0) out vec4 fragColor; layout(std140, binding = 0) uniform buf { float t; -} ubuf; + float y_dir; +}; void main() { - float i = 1. - (pow(abs(coords.x), 4.) + pow(abs(coords.y), 4.)); - i = smoothstep(ubuf.t - 0.8, ubuf.t + 0.8, i); - i = floor(i * 20.) / 20.; - fragColor = vec4(coords * .5 + .5, i, i); + float i = 1.0 - (pow(abs(coords.x), 4.0) + pow(abs(coords.y), 4.0)); + i = smoothstep(t - 0.8, t + 0.8, i); + i = floor(i * 20.0) / 20.0; + fragColor = vec4(coords * 0.5 + 0.5, i, i); } diff --git a/examples/quick/scenegraph/shared/squircle_rhi.vert b/examples/quick/scenegraph/shared/squircle_rhi.vert index b57dfdfe10..bf079ab786 100644 --- a/examples/quick/scenegraph/shared/squircle_rhi.vert +++ b/examples/quick/scenegraph/shared/squircle_rhi.vert @@ -1,13 +1,16 @@ #version 440 layout(location = 0) in vec4 vertices; - layout(location = 0) out vec2 coords; -out gl_PerVertex { vec4 gl_Position; }; +layout(std140, binding = 0) uniform buf { + float t; + float y_dir; +}; void main() { gl_Position = vertices; coords = vertices.xy; + coords.y *= y_dir; } |