在openGL控件上同样显示三个纹理 - OpenTK C#

问题描述 投票:1回答:2

我有三个纹理应该在opengl控件上显示,这三个纹理应该同样在其中。表示texture1应该在glcontrol的0到0.33之间。而texture2应该在0.33到0.66并且texture3保留在原地。我在下面做了。但中间图像的右侧部分会出现模糊区域。请帮忙纠正

   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);
    }
c# opengl glsl fragment-shader opentk
2个回答
0
投票

表示texture1应该在glcontrol的0到0.33之间。而texture2应该在0.33到0.66并且texture3保留在原地。

如果纹理坐标在[0,0.33]范围内,则必须绘制sTexture1,并且纹理坐标必须从[0,0.33]映射到[0,1]:

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

如果纹理坐标在[0.33,0.66]范围内,那么必须绘制sTexture2并且纹理坐标必须从[0.33,0.66]映射到[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));

如果纹理坐标在[0.66,1]范围内,那么必须绘制sTexture3并且纹理坐标必须从[0.66,1]映射到[0,1]:

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

0
投票
gl_FragColor = texture2D (sTexture3, vec2(vTexCoord.x*2.0, vTexCoord.y));
                                                     ^^^^

如果x> = 0.5,则将x坐标乘以2.0会导致值超过1.0。如果您的采样器设置为CLAMP_TO_EDGE(这似乎是这种情况),这会导致在纹理边缘重复采样相同的纹素(如上所述,它将显示为模糊/模糊)。

© www.soinside.com 2019 - 2024. All rights reserved.