Search code examples
c#glslshaderfragment-shaderopentk

Found particular intensity points of texture - OpenTK


With the help of this link I  can change the intensity of entire texture. Now I want to detect all points on my texture which have intensity greater than 0.7. How can  I achieve this?

private void CreateShaders()
{
    /***********Vert Shader********************/
    vertShader = GL.CreateShader(ShaderType.VertexShader);
    GL.ShaderSource(vertShader, @"
        attribute vec3 a_position;
        varying vec2 vTexCoord;
        void main() {
            vTexCoord = (a_position.xy + 1) / 2;
            gl_Position = vec4(a_position,1);
        }");
    GL.CompileShader(vertShader);

    /***********Frag Shader ****************/
    fragShader = GL.CreateShader(ShaderType.FragmentShader);
    GL.ShaderSource(fragShader, @"
        precision highp float;
        uniform sampler2D sTexture;
        varying vec2 vTexCoord;
        const float Epsilon = 1e-10;

        //RGBtoHSV
        //HSVtoRGB

        void main ()
        {
            vec4  color = texture2D (sTexture, vTexCoord);
            float u_saturate=0.7;
            vec3 col_hsv = RGBtoHSV(color.rgb);
            col_hsv.z *= (u_saturate * 2.0);
            vec3 col_rgb = HSVtoRGB(col_hsv.rgb);
            gl_FragColor = vec4(col_rgb.rgb, color.a);
        }");
    GL.CompileShader(fragShader);
}

Solution

  • Compare the intensity to a threshold and use a different color dependent on the intensity:

    gl_FragColor = intensity > 0.7 ? colorH : vec4(col_rgb.rgb, color.a);  
    

    The fragment shader main may look like this:

    void main ()
    {
        vec4  color = texture2D (sTexture, vTexCoord);
        float u_saturate=0.7;
    
        vec3  col_hsv = RGBtoHSV(color.rgb);
        float intensity = col_hsv.z;
    
        col_hsv.z *= (u_saturate * 2.0);
        vec3 col_rgb = HSVtoRGB(col_hsv.rgb);   
    
        vec4 colorH = vec4(0.0, 0.0, 0.0, 1.0);
        gl_FragColor = intensity > 0.7 ? colorH : vec4(col_rgb.rgb, color.a);
    }