Show half portion of image side by side on two sep

2019-08-21 13:45发布

问题:

I have a single texture and two glControls. I need to show the first half portion of texture on glControl2 and second half portion of texture on glControl1. And also I have put two numericupdown controls on left end of texture (range from 0 to 0.5) and another two numericupdown controls on right end of texture (range from 0.5 to 1.0). When I select 0. 4 and 0. 5 on left end of texture, I have to show the area between 0. 4 and 0. 5 on glControl2. And If I select 0. 8 and 1 on right end of texture, I have to show the area between 0. 8 and 1. 0 on glControl1. I have tried like by referring this link . But not getting correctly.

       public void CreateShaders()
    {
        /***********Vert Shader********************/
        vertShader = GL.CreateShader(ShaderType.VertexShader);
        GL.ShaderSource(vertShader, @"attribute vec3 a_position;
                                    varying vec2 vTexCoordIn; 

                                    void main() {
                           vTexCoordIn=( 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_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 to 1.0
float rightsliderEndval=sSelectedRangeRightEnd;//1.0 to 0.5
float rightsliderDelta=rightsliderEndval-rightsliderStartval;

float leftsliderStartval=sSelectedRangeLeftEnd;//0.0 to 0.5
float leftsliderEndval=sSelectedRangeLeft;//0.5 to 0.0
float leftsliderDelta=leftsliderEndval-leftsliderStartval;

 if(sCurrentGLControl==1)//GLControl1
 {
 vec4 colorLeft= texture2D(sTexture_2, vec2((0.5+vTexCoord.x)-(0.5-rightsliderStartval)-(1.0-rightsliderEndval), vTexCoord.y));
 gl_FragColor = colorLeft;

}
 else if(sCurrentGLControl==2) //GLControl2
 {  
 vec4 colorRight= texture2D(sTexture_2, vec2(((vTexCoord.x-0.75)*2.0) +(leftsliderStartval), vTexCoord.y));
 gl_FragColor = colorRight;  
 }
 }");
        GL.CompileShader(fragShader);
    }

回答1:

Instead of a conditional logic in the shader it seams much simpler to create 2 controls and re-position the geometry (or change a view-port / camera) according to the constrains.

Update I propose following approach to accomplish your task:

  • Create 2 rectangles representing a full view area each
  • In Paint handler of each glControl
    • Position/scale rectangle or adjust a view port (camera) according to values in relevant updown controls
    • Apply shader
    • Draw rectangle