问题
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));
}
回答1:
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 );
}
// [...]
}
来源:https://stackoverflow.com/questions/57033603/apply-projection-on-half-of-texture