Search code examples
c#openglglslfragment-shaderopentk

Display three textures equally on openGL control - OpenTK C#


I have three textures which should display on an opengl control in a way that those three should equally be in it. Means texture1 should be in 0 to 0.33 of glcontrol. And texture2 should be in 0.33 to 0.66 And texture3 in remain place. I have done like below. But right portion of middle image get a blurred area. Please help to correct

   private 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, @"
    uniform sampler2D sTexture1;
    uniform sampler2D sTexture2;
    uniform sampler2D sTexture3;     
    varying vec2 vTexCoordIn;
    void main ()
    {                                               
    vec2 vTexCoord=vec2(vTexCoordIn.x,vTexCoordIn.y);
    if ( vTexCoord.x<0.3 )
    gl_FragColor = texture2D (sTexture1, vec2(vTexCoord.x*2.0, vTexCoord.y));
    else if ( vTexCoord.x>=0.3 && vTexCoord.x<=0.6 )
    gl_FragColor = texture2D (sTexture2, vec2(vTexCoord.x*2.0, vTexCoord.y));
    else
    gl_FragColor = texture2D (sTexture3, vec2(vTexCoord.x*2.0, vTexCoord.y));
 }");
        GL.CompileShader(fragShader);
    }

Solution

  • Means texture1 should be in 0 to 0.33 of glcontrol. And texture2 should be in 0.33 to 0.66 And texture3 in remain place.

    If the texture coordinates are in range [0, 0.33] then sTexture1 has to be drawn and the texture coordinates have to be mapped from [0, 0.33] to [0, 1]:

    if ( vTexCoord.x < 1.0/3.0 )
        gl_FragColor = texture2D(sTexture1, vec2(vTexCoord.x * 3.0, vTexCoord.y));
    

    If the texture coordinates are in range [0.33, 0.66] then sTexture2 has to be drawn and the texture coordinates have to be mapped from [0.33, 0.66] to [0, 1]:

    else if ( vTexCoord.x >= 1.0/3.0 && vTexCoord.x < 2.0/3.0 )
        gl_FragColor = texture2D(sTexture2, vec2(vTexCoord.x * 3.0 - 1.0, vTexCoord.y));
    

    f the texture coordinates are in range [0.66, 1] then sTexture3 has to be drawn and the texture coordinates have to be mapped from [0.66, 1] to [0, 1]:

    else if ( vTexCoord.x >= 2.0/3.0 )
        gl_FragColor = texture2D(sTexture2, vec2(vTexCoord.x * 3.0 - 2.0, vTexCoord.y));