片段着色器中的纹理定义丢失

时间:2019-03-01 05:09:17

标签: c++ opengl graphics glsl fragment-shader

我试图使用片段着色器绘制参考网格,并且发现缩小时纹理失去其定义,如您在此处看到的。有人知道为什么吗?放大时看起来不错。

外出时四边形上的网格纹理

没有缩放的网格纹理

这是代码:

#version 330 core
uniform int multiplicationFactor;
uniform lowp float threshold;
uniform vec4 gridColor;

in vec2 vUV;

void main() {
    // multiplicationFactor scales the number of stripes
    vec2 t = vUV * multiplicationFactor;

    // the threshold constant defines the with of the lines
    if (abs(t.x - round(t.x)) <= threshold  || abs(t.y - round(t.y)) < threshold )
        gl_FragColor = gridColor;    
    else
        discard;
}

1 个答案:

答案 0 :(得分:1)

您必须根据视口的大小计算到线的距离。

添加类型为vec2 resolution的统一变量,该变量包含视口的宽度和高度,变量threshold保留以像素为单位的线条粗细(例如1.0):

#version 330 core

uniform int        multiplicationFactor;
uniform lowp float threshold;   // line width in pixel
uniform vec4       gridColor;
uniform vec2       resolution;  // width and height of the viewport

in vec2 vUV;

void main()
{
    float m   = float(multiplicationFactor);
    vec2 t    = vUV * m;
    vec2 dist = abs(t - round(t)) / m; 
    vec2 th   = threshold / resolution;

    if (dist.x > th.x  && dist.y > th.y)
        discard;

    gl_FragColor = gridColor;    
}