首页 > 其他分享 >保存一段qml里使用opengl shader的代码,由于多平台原因暂时用不了

保存一段qml里使用opengl shader的代码,由于多平台原因暂时用不了

时间:2022-08-28 15:56:44浏览次数:59  
标签:Qt parent opengl point ctx shader height colors qml

                            RowLayout {
                                Layout.fillWidth: true
                                height: 60
                                Rectangle {
                                    Layout.margins: 5
                                    radius: 5
                                    width: 40
                                    height: width
                                    Layout.alignment: Qt.AlignVCenter | Qt.AlignLeft
                                    clip: true
                                    layer.enabled: true
                                    layer.effect: ShaderEffect {
                                        readonly property var dataTex: dataCanvas
                                        readonly property real count: dataCanvas.colors.length
                                        fragmentShader: "
varying highp vec2 qt_TexCoord0;
uniform lowp float qt_Opacity;
uniform sampler2D dataTex;
const uniform lowp int count;
void main() {
vec3 col = 0.0;
for (int i = 0; i < count; i++) {
vec4 color = texture2D(dataTex, vec2(i / (float)count + 0.01, 0.0));
vec2 point = texture2D(dataTex, vec2(i / (float)count + 0.01, 1.0)).rg;
float dist = distance(qt_TexCoord0, point);
col += color * (1.0 - dist);
}
gl_FragColor = vec4(col, 1.0);
}
"
                                    }
                                    // Because ShaderEffect doesn't allow passing arrays to shader, we will
                                    // convert arrays to graphical representation and pass them as texture.
                                    Canvas {
                                        id: dataCanvas
                                        readonly property var colors: ["green", "yellow", "cyan", "red"]
                                        readonly property var positions: [Qt.point(
                                                0, 0), Qt.point(parent.width,
                                                                0), Qt.point(
                                                0, parent.height), Qt.point(
                                                parent.width, parent.height)]

                                        height: 2
                                        width: colors.length
                                        antialiasing: false
                                        visible: false

                                        onPaint: {
                                            if (colors.length !== positions.length) {
                                                console.error(
                                                            "Array size of 'colors' doesn't equal array size of 'positions'")
                                                return
                                            }

                                            var ctx = getContext("2d")
                                            ctx.reset()
                                            ctx.lineWidth = 1

                                            for (var i = 0; i < colors.length; i++) {
                                                ctx.beginPath()
                                                ctx.strokeStyle = colors[i]
                                                ctx.moveTo(i, 0)
                                                ctx.lineTo(i + 1, 0)
                                                ctx.stroke()
                                                ctx.closePath()

                                                ctx.beginPath()
                                                ctx.strokeStyle = Qt.rgba(
                                                            positions[i].x / parent.width,
                                                            positions[i].y / parent.height,
                                                            0, 1)
                                                ctx.moveTo(i, 1)
                                                ctx.lineTo(i + 1, 1)
                                                ctx.stroke()
                                                ctx.closePath()
                                            }
                                        }
                                    }
                                }

标签:Qt,parent,opengl,point,ctx,shader,height,colors,qml
From: https://www.cnblogs.com/nocanstillbb/p/16632908.html

相关文章

  • Windows OpenGL ES 图像亮度调节
    目录一.OpenGLES图像亮度调节1.原始图片2.效果演示二.OpenGLES图像亮度调节源码下载三.猜你喜欢零基础OpenGLES学习路线推荐:OpenGLES学习目录>>......
  • Windows OpenGL 图像亮度调节
    目录一.OpenGL图像亮度调节1.原始图片2.效果演示二.OpenGL图像亮度调节源码下载三.猜你喜欢零基础OpenGLES学习路线推荐:OpenGLES学习目录>>OpenGL......
  • qtav shader处理
    链接shader,标准openglshader处理过程boolVideoShader::build(QOpenGLShaderProgram*shaderProgram){    if(shaderProgram->isLinked()){        qWar......
  • 阅读《计算机图形学编程(使用OpenGL和C++)》8 - 纹理贴图
    纹理贴图就是将图片贴到模型上,让模型看起来更真实。纹理贴图非常重要,因此硬件也为它提供了支持,使得它具备了实现实时的照片级真实感的超高性能。纹理单元是专为纹理设计的......
  • OpenGL 波浪特效
    目录一.OpenGL波浪特效效果演示1.IOS演示效果2.WindowsOpenGLES演示效果3.WindowsOpenGL演示效果二.OpenGL波浪特效源码下载1.IOSObject-C版本2.Win......
  • IOS OpenGL ES 波浪特效
    目录一.OpenGLES波浪特效效果演示1.原始图片2.效果演示二.OpenGLES波浪特效源码下载三.猜你喜欢零基础OpenGLES学习路线推荐:OpenGLES学习目录>>......
  • 阅读《计算机图形学编程(使用OpenGL和C++)》6
    同一个场景渲染不同的对象,一种简单的方法是为每个模型使用单独的缓冲区。每个模型都需要自己的模型矩阵,这样我们就需要为我们渲染的每个模型生成一个新的模型-视图矩阵。还......
  • 基于C++的OpenGL 13 之Mesh
    1.引言本文基于C++语言,描述OpenGL的Mesh前置知识可参考:基于C++的OpenGL12之多光源-当时明月在曾照彩云归-博客园(cnblogs.com)笔者这里不过多描述每个名词、......
  • 基于C++的OpenGL 14 之模型加载
    1.引言本文基于C++语言,描述OpenGL的模型加载前置知识可参考:基于C++的OpenGL13之Mesh-当时明月在曾照彩云归-博客园(cnblogs.com)笔者这里不过多描述每个名词......
  • 设计ShaderGraph
    清理过程MaterialCustomPara首先是材质的参数,MaterialCustomPara,作为基类,它只有一个着色器参数名字。CustomFloatValue然后是CustomFloatValue,作为MaterialCustomPar......