1

I'm using three glcontrols say GlControl1, GlControl2, GlControl3. And I have two textures stexture1 and stexture2. Where stexture1 is displaying in glcontrol2. And right half portion of stexture2 is displaying on glcontrol1 and left half is dispalying on glcontrol3. Now I want to apply projection on these three glcontrols. Using this link I can apply it successfully on glcontrol2 since it is displaying the texture fully.

But when applying on glcontrol1 and glcontrol3 it is not working well.

Please see the shader code I'm trying with.

   GL.ShaderSource(fragShader, @"precision highp float;
    uniform sampler2D sTexture_1;
    uniform sampler2D sTexture_2;
    uniform float sSelectedRangeLeft;   
    uniform float sSelectedRangeRight;
    uniform float sSelectedRangeLeftEnd;   
    uniform float sSelectedRangeRightEnd;     
    uniform int sCurrentGLControl;
    varying vec2 vTexCoordIn;
    void main ()
    {
    vec2 vTexCoord=vec2(vTexCoordIn.x,vTexCoordIn.y);

   float rightsliderStartval=sSelectedRangeRight;//0.5(value between 0.5 and 1.0)
   float rightsliderEndval=sSelectedRangeRightEnd;//1.0(value between 0.5 and 1.0)
   float rightsliderDelta=rightsliderEndval-rightsliderStartval;

   float leftsliderStartval=sSelectedRangeLeftEnd;//0.0(value between 0 and 0.5)
   float leftsliderEndval=sSelectedRangeLeft;//0.5(value between 0 and 0.5)
   float leftsliderDelta=leftsliderEndval-leftsliderStartval;

 if(sCurrentGLControl==1)
 { 
 if ( vTexCoord.x <=1.0 &&  vTexCoord.x > 1.0 -rightsliderDelta)
 {
 vec4 colorLeft=texture2D (sTexture_2, vec2(vTexCoord.x -(1.0-rightsliderEndval), vTexCoord.y));
//i want to show this result in a projected view like glcontrol2
 gl_FragColor  = colorLeft;
 }
 }
 else if(sCurrentGLControl==3) 
 { 
  if ( vTexCoord.x <=leftsliderDelta )
 {
  vec4 colorRight=texture2D (sTexture_2, vec2((vTexCoord.x)+leftsliderStartval, vTexCoord.y+sSelectedRightEndVerticalShift));
   //i want to show this result in a projected view like glcontrol2
    gl_FragColor  = colorRight;  
   }
  }

else if(sCurrentGLControl==2) 
 {  //Projection works fine 
vec2  pos     = vTexCoord.xy * 2.0 - 1.0;
float b       = 0.3;
float v_scale = (1.0 + b) / (1.0 + b * sqrt(1.0 - pos.x*pos.x));
float u = asin( pos.x ) / 3.1415 + 0.5;
float v = (pos.y * v_scale) * 0.5 + 0.5;
if ( v < 0.0 || v > 1.0 )
  discard;
vec3 texColor = texture2D( sTexture_1, vec2(u, v) ).rgb;
gl_FragColor  = vec4( texColor.rgb, 1.0 );   
  }
    }");

  ==================================================================
if (glControl.Name == "glControl1")
        {
            GL.Viewport(new Rectangle(-glControl.Width, 0, glControl.Width*2.0, glControl.Height));

        }
        else if (glControl.Name == "glControl2")
        {
            GL.Viewport(new Rectangle(0, 0, glControl.Width , glControl.Height));

        }
        else
        {
            GL.Viewport(new Rectangle(0, 0, glControl.Width*2.0, glControl.Height));

        }

enter image description here enter image description here enter image description here enter image description here enter image description here

user2431727
  • 877
  • 2
  • 15
  • 46

1 Answers1

2

If you don't want to distort the "discarded" area, the just keep the original texture coordinate:

precision highp float;

uniform sampler2D sTexture;
varying vec2 vTexCoordIn;

void main ()
{
    vec2 uv = vTexCoordIn.xy

    if (vTexCoordIn.x >= 0.7 && vTexCoordIn.x <= 0.9)
    {
        vec2  pos     = vTexCoordIn.xy * 2.0 - 1.0;
        float b       = 0.3;
        float v_scale = (1.0 + b) / (1.0 + b * sqrt(1.0 - pos.x*pos.x));

        float u = asin( pos.x ) / 3.1415 + 0.5;
        float v = (pos.y * v_scale) * 0.5 + 0.5;
        if (v >= 0.0 && v <= 1.0)
          uv = vec2(u, v);
    }

    vec3 texColor = texture2D(sTexture, uv).rgb;
    gl_FragColor  = vec4(texColor.rgb, 1.0);
}

Edit according to the changes of the question:

If you just want to show a part of the texture, then you've to map the texture coordinate from the range [0, 1] to the range [left, right], when the texture is looked up:

u = u * rightsliderDelta + rightsliderStartval;  
vec3 texColor = texture2D( sTexture_2, vec2(u, v) ).rgb;

Apply this to your code as follows:

e.g.:

void main ()
{
    vec2 vTexCoord=vec2(vTexCoordIn.x,vTexCoordIn.y);

    float rightsliderStartval=sSelectedRangeRight;//0.5(value between 0.5 and 1.0)
    float rightsliderEndval=sSelectedRangeRightEnd;//1.0(value between 0.5 and 1.0)
    float rightsliderDelta=rightsliderEndval-rightsliderStartval;

    // [...]

    if(sCurrentGLControl==1) {

        vec2  pos     =  vTexCoord.xy * 2.0 - 1.0;
        float b       = 0.3;
        float v_scale = (1.0 + b) / (1.0 + b * sqrt(1.0 - pos.x*pos.x));
        float u = asin( pos.x ) / 3.1415 + 0.5;
        float v = (pos.y * v_scale) * 0.5 + 0.5;
        if ( v < 0.0 || v > 1.0 )
            discard;

        u = u * rightsliderDelta + rightsliderStartval;   
        vec3 colorLeft = texture2D( sTexture_2, vec2(u, v) ).rgb;
        gl_FragColor   = vec4( colorLeft .rgb, 1.0 );
    }

    // [...]

}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I want to show full projection on visible area of attached image. – user2431727 Jul 15 '19 at 05:57
  • Means, i want to apply projection on area other than discarded area. now the screen shot looks like the image is shown in right half portion of cylinder. Left half portion of cylinder is black. I want to show that image part fully cylindrical. – user2431727 Jul 15 '19 at 06:20
  • In short, i need projection between rightsliderStartval and rightsliderEndval – user2431727 Jul 15 '19 at 06:41
  • Now i'm getting blurred area on some points of glcontrol1. In my code except projection all other part is working fine(since this rightsliderStartval and rightsliderEndval will change with a slider). so can i correct with existing code? for example in the case of glcontrol1, projection should be applied to " if ( vTexCoord.x <=1.0 && vTexCoord.x > 1.0 -rightsliderDelta){ vec4 colorLeft=texture2D (sTexture_2, vec2(vTexCoord.x -(1.0-rightsliderEndval), vTexCoord.y));}" – user2431727 Sep 03 '19 at 03:49
  • vTexCoord.x <=1.0 && vTexCoord.x > 1.0 -rightsliderDelta will give values in the range of 0.5 to 1. because in this loop, i need right half portion of texture .maximum value of rightsliderDelta is 0.5. So coordinate will never greater then 1.0. – user2431727 Sep 03 '19 at 09:22
  • when rightsliderStartval=0.6 and rightsliderEndval=1.0 some start portion of resulted image seems blurred – user2431727 Sep 03 '19 at 09:31
  • it is working fine with the loop i used in the question . only i want to apply projection properly on vec4 colorLeft. if(sCurrentGLControl==1){ if ( vTexCoord.x <=1.0 && vTexCoord.x > 1.0 -rightsliderDelta){ vec4 colorLeft=texture2D (sTexture_2, vec2(vTexCoord.x -(1.0-rightsliderEndval), vTexCoord.y)); gl_FragColor =colorLeft;}} – user2431727 Sep 03 '19 at 10:06
  • tried it attached the output with values rightsliderDelta=1.0-0.5=0.5 – user2431727 Sep 04 '19 at 05:26
  • 1
    @user2431727 I think I know whats going on. Try the new code in the answer. (I've edited it right now. – Rabbid76 Sep 04 '19 at 05:32
  • output is attached. data loss is there. – user2431727 Sep 04 '19 at 05:46
  • @user2431727 What are you doing!? Are your texture coordinates in range [0.0, 1.0] for `sCurrentGLControl==1`? Or are they in a different range? e.g. [0.5, 1.0]. I don't want to help you any more. You do not give me the all the information, which I need to help you. That's boring. – Rabbid76 Sep 04 '19 at 05:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198914/discussion-between-user2431727-and-rabbid76). – user2431727 Sep 04 '19 at 05:57