【问题标题】:Crop the top and bottom part of texture using opentk使用 opentk 裁剪纹理的顶部和底部
【发布时间】:2021-02-06 11:21:06
【问题描述】:

我有一个滑块,其值范围从零到一。现在使用此滑块的值,我想将图像从图像的底部裁剪到图像的一半以及从图像的顶部到图像的一半。我通过调整 GLControl 的高度完成了第一个(底部裁剪)。不确定这是实现这一目标的正确方法。但它工作正常。我不知道第二个选项(从图像的顶部裁剪到一半)。请帮忙拿下。 附件是我在执行底部裁剪时得到的输出,分别为 0,0.4,1.0。

 int FramereHeight = (glControl2.Height / 2) / 10; // crop the middle camera upto half of it's height                     
 if (NumericUpdownMiddleBottomCropVal != 0.0)//value ranges from zero to one
                        {
 glControl2.Height = glControl2.Height - Convert.ToInt32(NumericUpdownMiddleBottomCropVal * 10 * FramereHeight);
                        }
      public void CreateShaders()
    {
        /***********Vert Shader********************/
        vertShader = GL.CreateShader(ShaderType.VertexShader);
        GL.ShaderSource(vertShader, @"attribute vec3 a_position;
                                varying vec2 vTexCoord;
                                void main() {
                                vTexCoord = (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;varying vec2 vTexCoord; 
  uniform float sSelectedCropVal;
  uniform float sSelectedTopCropVal;uniform float sSelectedBottomCropVal;
          void main ()
         {
    if (abs(vTexCoord.y-0.5) * 2.0 > 1.0 - 0.5*sSelectedCropVal)
    discard;
     vec4 color = texture2D (sTexture_2, vec2(vTexCoord.x, vTexCoord.y));
     gl_FragColor =color;
   }"); GL.CompileShader(fragShader);
    }

【问题讨论】:

    标签: c# opengl glsl fragment-shader opentk


    【解决方案1】:

    我假设sSelectedCropVal 在 [0.0, 1.0] 范围内。

    你可以discard根据v corodiante的片段:

    if ((0.5 - vTexCoord.y) * 2.0 > 1.0-sSelectedCropVal)
        discard;
    if ((vTexCoord.y - 0.5) * 2.0 > 1.0-sSelectedBottomCropVal)
        discard;
    

    完整的着色器:

    precision highp float;
    uniform sampler2D sTexture_2;varying vec2 vTexCoord; 
    uniform float sSelectedCropVal;
    uniform float sSelectedTopCropVal;uniform float sSelectedBottomCropVal;
    
    void main ()
    {
        if ((0.5 - vTexCoord.y) * 2.0 > 1.0-sSelectedCropVal)
            discard;
        if ((vTexCoord.y - 0.5) * 2.0 > 1.0-sSelectedBottomCropVal)
            discard;
    
         vec4 color = texture2D(sTexture_2, vTexCoord.xy);
         gl_FragColor = color;
    }
    

    【讨论】:

    • 请在设置 sSelectedCropVal=1.0 时查看我的输出。现在从图像的底部和顶部同时裁剪到图像的四分之一。为了更方便,我可以单独做吗?当 sSelectedBottomCropVal=1.0 时,用户只能看到图像的前半部分。而当 sSelectedTopCropVal=1.0 时,用户只能看到图像的后半部分。并且当两者都=1.0时,整个图像将被丢弃。
    • @user2431727 我已经改变了答案。
    • 感谢您的快速回复。我已经改变如下,以获得我在问题中的目标。 if ((0.5 - vTexCoord.y) * 2.0 > (1.0-sSelectedBottomCropVal)) 丢弃; if ((vTexCoord.y - 0.5) * 2.0 > (1.0-sSelectedTopCropVal)) 丢弃;
    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多