Как исправить странные артефакты при применении размытия по Гауссу в OpenGL ES 2.0 - PullRequest
0 голосов
/ 22 июня 2019

Я получаю странные артефакты при применении размытия по Гауссу для эффекта цветения в OpenGL ES 2.0 (с QT), как вы можете видеть ниже. Слева с включенным эффектом цветения, а справа с выключенным эффектом:

enter image description here

Странно, но это артефакт происходит только тогда, когда я рендеринг определенных линий, как вы можете видеть на рисунке ниже. Что бы это ни стоило, эти строки на самом деле являются квадраторами, которые отображаются на экране с постоянной шириной пикселя. Никакие другие объекты не имеют видимых артефактов, только ожидаемое размытие по Гауссу и, что еще более странно, большинство других линий, которые я отображал, выглядят просто отлично. На рисунке слева видно, что голубая линия слева отображается без каких-либо странных артефактов.

Вот шейдеры, которые я собрал:

Вершинный шейдер:

#ifdef GL_ES
precision mediump int;
precision mediump float;
#endif

attribute vec3 aPos;
attribute vec2 aTexCoords;

varying vec2 blurTextureCoords[11];
varying vec2 texCoords;

uniform vec2 screenRes; // image width
uniform int effectType;

void main(void){

    // Set GL position
    gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0); 
    texCoords = aTexCoords;


    if(effectType == 2){
        // Horizontal blur
        vec2 centerTexCoords = vec2(aPos.x, aPos.y)*0.5 + 0.5; // Coordinates of center of texture
        float pixelSize = 1.0 / screenRes.x;

        // Fill out texture coord array
        for(int i=-5; i<=5; i++){
            float i_float = float(i);
            blurTextureCoords[i+5] = centerTexCoords + vec2(pixelSize * i_float, 0.0);
        }
    }
    else if(effectType == 3){
        // Vertical blur
        vec2 centerTexCoords = vec2(aPos.x, aPos.y)*0.5 + 0.5; // Coordinates of center of texture
        float pixelSize = 1.0 / screenRes.y;

        // Fill out texture coord array
        for(int i=-5; i<=5; i++){
            float i_float = float(i);
            blurTextureCoords[i+5] = centerTexCoords + vec2(0.0, pixelSize * i_float);
        }
    }
}

Фрагмент шейдера

#ifdef GL_ES
precision mediump int;
precision mediump float;
#endif

varying vec2 blurTextureCoords[11];
varying vec2 texCoords;

uniform sampler2D screenTexture;
uniform sampler2D sceneTexture;
uniform int effectType;

void main(void){
    vec4 color = texture2D(screenTexture, texCoords);

    // Different effects
    if(effectType == 2 || effectType == 3){
        // Blur
        gl_FragColor = vec4(0.0);
        gl_FragColor += texture2D(screenTexture, blurTextureCoords[0]) * 0.0093;
        gl_FragColor += texture2D(screenTexture, blurTextureCoords[1]) * 0.028002;
        gl_FragColor += texture2D(screenTexture, blurTextureCoords[2]) * 0.065984;
        gl_FragColor += texture2D(screenTexture, blurTextureCoords[3]) * 0.121703;
        gl_FragColor += texture2D(screenTexture, blurTextureCoords[4]) * 0.175713;
        gl_FragColor += texture2D(screenTexture, blurTextureCoords[5]) * 0.198596;
        gl_FragColor += texture2D(screenTexture, blurTextureCoords[6]) * 0.175713;
        gl_FragColor += texture2D(screenTexture, blurTextureCoords[7]) * 0.121703;
        gl_FragColor += texture2D(screenTexture, blurTextureCoords[8]) * 0.065984;
        gl_FragColor += texture2D(screenTexture, blurTextureCoords[9]) * 0.028002;
        gl_FragColor += texture2D(screenTexture, blurTextureCoords[10]) * 0.0093;
    }
    else{
        gl_FragColor = color;
    }
}

Мне не удалось найти ничего странного в других частях моего кода, и это действительно странно, что только эти конкретные строки перепутаны. Есть идеи?

1 Ответ

1 голос
/ 22 июня 2019

Решил это! Мне нужно было сжать значения цвета моих выборочных координат текстуры между 0 и 1, так как я использую HDR, и эти значения насыщают эффект размытия по Гауссу.

...