【问题标题】:Projection is not working when cutting particular area from top and bottom of openGL control从openGL控件的顶部和底部切割特定区域时,投影不起作用
【发布时间】:2019-07-03 09:10:43
【问题描述】:

this 链接的帮助下,我可以在我的纹理上应用投影。 现在我想从我的 glcontrol 的顶部和底部剪切/删除相等的区域,然后需要在剩余区域上应用相同的投影。我已经尝试过如下。但如图所示,投影时缺少顶部和底部曲线。 我怎样才能把它带回保留区?

precision highp float;
uniform sampler2D sTexture;
varying vec2 vTexCoord;
void main()
{
    float img_h_px  = 432.0; // height of the image in pixel
    float area_h_px = 39.0;  // area height in pixel

    float w = area_h_px/img_h_px;
    if (vTexCoord.y < w || vTexCoord.y > (1.0-w)){
        gl_FragColor= vec4(1.0,0.0,1.0,1.0);
    }
    else
    {
        vec2  pos     = vTexCoord.xy * 2.0 - 1.0;
        float b       = 0.5;
        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( u_texture, vec2(u, v) ).rgb;
        gl_FragColor  = vec4( texColor.rgb, 1.0 );
    }    
}

【问题讨论】:

    标签: c# opengl glsl opentk coordinate-transformation


    【解决方案1】:

    底部和顶部区域的大小(底部和顶部区域的总和),相对于控件的大小为2.0*area_h_px/img_h_px = 2.0*w。 控件大小与“可见”区域的比例(h_ratio)为:

    float w = area_h_px/img_h_px;
    float h_ratio = 1.0 - 2.0*w;
    

    您必须通过“可见”区域和控件大小的比例来缩放纹理查找的 y 坐标,这是 h_ratio (1.0/h_ratio) 的倒数:

    float v = (pos.y * v_scale / h_ratio) * 0.5 + 0.5;
    

    最终着色器:

    precision highp float;
    uniform sampler2D sTexture;
    varying vec2 vTexCoord;
    
    void main()
    {
        float img_h_px  = 432.0; // height of the image in pixel
        float area_h_px = 39.0;  // area height in pixel
        float w = area_h_px/img_h_px;
        float h_ratio = 1.0 - 2.0*w;
    
        vec2  pos     = vTexCoord.xy * 2.0 - 1.0;
        float b       = 0.5;
        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 / h_ratio) * 0.5 + 0.5;
    
        vec3 texColor = texture2D(sTexture, vec2(u, v)).rgb;
    
        vec4 color = vec4(texColor.rgb, 1.0);
        if (vTexCoord.y < w || vTexCoord.y > (1.0-w))
            color = vec4(1.0, 0.0, 1.0, 1.0);
        else if (v < 0.0 || v > 1.0)
            discard;
    
        gl_FragColor = color; 
    }
    

    如果你想把整个区域染成紫色,那么你必须设置color,而不是discarding片段:

    if (v < 0.0 || v > 1.0)
        color = vec4(1.0, 0.0, 1.0, 1.0);
    

    【讨论】:

    • 投影工作正常。但是当尝试使用下面的代码切割曲线时,并不完全从顶部和底部曲线切割。 //****cRoP 投影**** if(IsCutProjection==1) {if (abs(pos.y) * (1.0 + b) > 1.0){ discard;} }
    • @user2431727 您必须按1.0/h_ratio - if (abs(pos.y)*(1.0+b)/h_ratio &gt; 1.0) { discard; } 进行扩展
    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多