基于Qt的OpenGL可编程管线学习(18)- 平滑、锐化、边缘检测

发布时间:2020-07-03 14:50:52 作者:Douzhq
来源:网络 阅读:1693

1、平滑

shader

// 平滑
uniform sampler2D U_MainTexture;
uniform sampler2D U_SubTexture;

varying vec2 M_coord;

void main()
{
        vec4 color = vec4(0.0);
        int coreSize = 3;
        float texelOffset = 1 / 300.0;
        float kernel[9];

        kernel[6] = 1; kernel[7] = 1; kernel[8] = 1;
        kernel[3] = 1; kernel[4] = 1; kernel[5] = 1;
        kernel[0] = 1; kernel[1] = 1; kernel[2] = 1;

        int index = 0;
        for(int y = 0; y<coreSize; y++)
        {
                for(int x=0; x<coreSize; x++)
                {
                        vec4 currentColor=texture2D(U_MainTexture, M_coord+vec2((-1+x)*texelOffset,(-1+y)*texelOffset));
                        color += currentColor * kernel[index++];
                }
        }
        color /= 9.0;

        gl_FragColor = color;
}

效果图

基于Qt的OpenGL可编程管线学习(18)- 平滑、锐化、边缘检测


2、锐化

shader

// 锐化
uniform sampler2D U_MainTexture;
uniform sampler2D U_SubTexture;

varying vec2 M_coord;

void main()
{
        vec4 color = vec4(0.0);
        int coreSize = 3;
        float texelOffset = 1 / 300.0;
        float kernel[9];

        kernel[6] = 0; kernel[7] = -1; kernel[8] = 0;
        kernel[3] = -1; kernel[4] = 4; kernel[5] = -1;
        kernel[0] = 0; kernel[1] = -1; kernel[2] = 0;

        int index = 0;
        for(int y = 0; y<coreSize; y++)
        {
                for(int x=0; x<coreSize; x++)
                {
                        vec4 currentColor=texture2D(U_MainTexture, M_coord+vec2((-1+x)*texelOffset,(-1+y)*texelOffset));
                        color += currentColor * kernel[index++];
                }
        }
        //color /= 9.0;

        gl_FragColor = 4.0 * color + texture2D(U_MainTexture, M_coord);
}

效果图

基于Qt的OpenGL可编程管线学习(18)- 平滑、锐化、边缘检测


3、边缘检测

shader

//  边缘检测
uniform sampler2D U_MainTexture;
uniform sampler2D U_SubTexture;

varying vec2 M_coord;

void main()
{
        vec4 color = vec4(0.0);
        int coreSize = 3;
        float texelOffset = 1 / 300.0;
        float kernel[9];

        kernel[6] = 0; kernel[7] = 1; kernel[8] = 0;
        kernel[3] = 1; kernel[4] = -4; kernel[5] = 1;
        kernel[0] = 0; kernel[1] = 1; kernel[2] = 0;

        int index = 0;
        for(int y = 0; y<coreSize; y++)
        {
                for(int x=0; x<coreSize; x++)
                {
                        vec4 currentColor=texture2D(U_MainTexture, M_coord+vec2((-1+x)*texelOffset,(-1+y)*texelOffset));
                        color += currentColor * kernel[index++];
                }
        }
        //color /= 9.0;

        gl_FragColor = 4.0 * color + texture2D(U_MainTexture, M_coord);
}

效果图

基于Qt的OpenGL可编程管线学习(18)- 平滑、锐化、边缘检测

推荐阅读:
  1. Qt下的OpenGL 编程纹理和贴图
  2. GPUImage的简单使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

qt opengl shader

上一篇:nginx隐藏".php"后缀的方法

下一篇:python代码有下划波浪线的原因是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》