Saturday, June 20, 2015

Circular Fragment Shader with Qt 5

Circular guage in qt can be attained by GLSL sharers. Include this piece of code for your Image source.

ShaderEffect{
                property variant  source: ..
                property real value: ..
                property real thicknessvalue: ..
                property real radiusvalue: ..
                ...

                fragmentShader: "
                                uniform sampler2D source;
                                varying highp vec2 qt_TexCoord0;
                                uniform lowp float qt_Opacity;
                                uniform highp float value;
                                uniform highp float radiusvalue;
                                uniform highp float thicknessvalue;
                                const float kInvPi = 1.0 / 3.141592;
                                void main()
                                {
                                    float bluriness = 1.0;
                                    float radius = radiusvalue; //0.01 to 2.00 (0.90)
                                    float thickness = thicknessvalue; // 0.001 to 1.00
                                    float len = value;  // 0.001 to 0.760;
                                    float offset =  0.37; // Start of Arc
                                    vec2 uv = vec2(qt_TexCoord0.y,  qt_TexCoord0.x);
                                    uv = 2.0 * uv - 1.0;
                                    uv.x *= 1.0;
                                    float d = length( uv );
                                    float angle = atan( uv.x, uv. y ) * kInvPi * 0.5;
                                    angle = fract( angle - offset );
                                    float w = bluriness * fwidth( d );
                                    float circle = smoothstep( radius + w, radius - w, d );
                                    float inner = radius - thickness;
                                    circle -= smoothstep( inner + w, inner - w, d );
                                    float segment = smoothstep( len + 0.002, len, angle );
                                    segment *= smoothstep( 0.0, 0.002, angle );
                                    circle *= mix( segment, 1.0, step( 1.0, len ) );
                                    gl_FragColor = qt_Opacity * texture2D(source, uv) * circle;
                                }"
            }




No comments:

Post a Comment