1. 程式人生 > >基於Qt的OpenGL可編程管線學習(10)- 膨脹與腐蝕

基於Qt的OpenGL可編程管線學習(10)- 膨脹與腐蝕

qt opengl shader 腐蝕 膨脹

膨脹:取一個像素周圍的點,取最亮的點為當前的點顏色,為膨脹效果

腐蝕:取一個像素周圍的點,取最暗的點為當前的點顏色,為腐蝕效果


膨脹Fragment Shader

varying vec2 M_coord;
varying vec3 M_normal;
varying vec3 M_WordPos;

uniform sampler2D U_MainTexture;
uniform sampler2D U_SubTexture;

void main()
{
    vec4 maxValue=vec4(0.0);
    int coreSize=3;
    int halfCoreSize=coreSize/2;
    float texelOffset=1/200.0;
    for(int y=0;y<coreSize;y++)
    {
        for(int x=0;x<coreSize;x++)
        {
            vec4 color=texture2D(U_MainTexture,
                                 M_coord+vec2((-halfCoreSize+x)*texelOffset,
                                             (-halfCoreSize+y)*texelOffset));
                        
            maxValue=max(maxValue,color);
         }
    }
    gl_FragColor=maxValue;
}


腐蝕Fragment Shader

varying vec2 M_coord;
varying vec3 M_normal;
varying vec3 M_WordPos;

uniform sampler2D U_MainTexture;
uniform sampler2D U_SubTexture;

void main()
{
    vec4 minValue=vec4(1.0);
    int coreSize=3;
    int halfCoreSize=coreSize/2;
    float texelOffset=1/100.0;
    for(int y=0;y<coreSize;y++)
    {
        for(int x=0;x<coreSize;x++)
        {
            vec4 color=texture2D(U_MainTexture,
                                 M_coord+vec2((-halfCoreSize+x)*texelOffset,
                                              (-halfCoreSize+y)*texelOffset));
            minValue=min(minValue,color);
        }
    }
    gl_FragColor=minValue;
}


本文出自 “不會飛的紙飛機” 博客,請務必保留此出處http://douzhq.blog.51cto.com/12552184/1931072

基於Qt的OpenGL可編程管線學習(10)- 膨脹與腐蝕