Search code examples
c#openglglslopentk

Showing particular region of texture on GLControl - openTK


I have to show a selected region of image on my Glcontrol. For that I have two variables LeftSideVal and RightSideVal. LeftSideVal is in the range of 0 to 0.5 and RightSideVal is in the range of 0.5 to 1.0.

If I set the LeftSideVal =0.3 and RightSideVal=0.8 then I have to show the region between 0.3 and 0.8 on GLControl with full of it's size (viewport).

I have tried like below. But it is not working properly. Shows some blurred area when changing the values.

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

===Shader Code===

  if( vTexCoord.x >=LeftSideVal && vTexCoord.x <=RightSideVal){      
  vec4 color=texture2D (sTexture, vec2(vTexCoord.x+LeftSideVal,vTexCoord.y)); 
  gl_FragColor=color;
  }

Solution

  • The range [left side val, right side val] is the range of texture coordinates to show over the whole screen. So you need to map that.

    In your shader usw

    float s = LeftSideVal + vTexCoord.x*(RightSideVal-LeftSideVal);
    texture(sTexture, vec2(s, vTexCoord.y));
    

    Strictly speaking that texture coordinate transformation can be represented by another matrix multiplication. The generalized version would be

    uniform mat3 T;
    
    /* ... */
    
    texture(sTexture, (T*vec2(vTexCoord,1).xy)
    

    where T is set to the appropriate transformation matrix that represents this transformation in 2D.