Search code examples
c#openglglslshaderopentk

How to hide/remove particular area from top and bottom of image - GLControl


I can display below image using opengl in my glControl. Is there any way to cut or hide particular area (say 50px from top and bottom) equally from both bottom and top of glControl? Below is the code I have used to calculate the size of glControl. Can I achieve it by changing values on view port?

private void OpenGL_Size(GLControl glControl, VideoCaptureDevice videoSource)//always in portrait  mode

{
    decimal RateOfResolution = (decimal)videoSource.VideoResolution.FrameSize.Width / (decimal)videoSource.VideoResolution.FrameSize.Height;

    decimal screenHeightbyTwo = this._Screenheight / 2;             
    RateOfResolution = 1 / RateOfResolution;// portrait 
    openGLheight = Convert.ToInt32(screenHeightbyTwo);   // height is fixed; calculate the width
    openGLwidth = (Convert.ToInt32(RateOfResolution * screenHeightbyTwo));

    glControl.Width = openGLwidth;
    glControl.Height = openGLheight;
}

GL.Viewport(new Rectangle(0, 0, glControl.Width, glControl.Height));

Shader code

precision highp float;
uniform sampler2D sTexture;
varying vec2 vTexCoordIn;
void main ()
{ 
    vec4 color = texture2D(sTexture,vTexCoordIn);
    gl_FragColor = color;
}

Solution

  • If you want to skip a some parts of the texture, then you can use the discard keyword. This command causes the fragment's output values to be discarded and the fragments are not drawn at all.

    precision highp float;
    
    uniform sampler2D sTexture;
    varying vec2 vTexCoordIn;
    
    void main ()
    { 
        if (vTexCoordIn.y < 0.1 || vTexCoordIn.y > 0.9)
            discard;
    
        vec4 color = texture2D(sTexture, vTexCoordIn);
        gl_FragColor = color;
    }
    

    If the height of the image and the height of the area which has to be discarded is given, then the condition is:

    float img_h_px  = 432.0; // height of the image in pixel
    float area_h_px = 50.0;  // area height in pixel
    
    float w = area_h_px/img_h_px;
    if (vTexCoordIn.y < w || vTexCoordIn.y > (1.0-w))
        discard;