2
$\begingroup$

I've been following a guide to learn OpenGL, and I'm now learning how to do post-processing. In particular, I'm trying to apply a blur to my rendering through the following kernel:

$\frac{\begin{bmatrix}1&2&1\\2&4&2\\1&2&1\end{bmatrix}}{16}$

The problem I'm encountering is that whenever I try to access a float array's member through the [] operator giving it an int variable as parameter, it returns 0.0. Here's my fragment shader:

#version 410 core
in vec2 txcoords;  // Texture coordinates
out vec4 color;
uniform sampler2D tx;

const float offset = 1.0 / 300.0;

void main() {
    vec2 offsets[9] = vec2[](
        vec2(-offset,    offset),
        vec2( 0.0,       offset),
        vec2( offset,    offset),
        vec2(-offset,    0.0),
        vec2( 0.0,       0.0),
        vec2( offset,    0.0),
        vec2(-offset,   -offset),
        vec2( 0.0,      -offset),
        vec2( offset,   -offset)
    );

    const float kernel[9] = float[9](
        1.0 / 16, 2.0 / 16, 1.0 / 16,
        2.0 / 16, 4.0 / 16, 2.0 / 16,
        1.0 / 16, 2.0 / 16, 1.0 / 16
    );

    vec3 sampleTex[9];
    vec3 final = vec3(0.0);
    for (int i = 0; i < 9; i++) {
        sampleTex[i] = vec3(texture(tx, txcoords.st + offsets[i]));
    }

    for (int i = 0; i < 9; i++) {
        final += sampleTex[i] * kernel[i];
    }
    color = vec4(final, 1.0);
}

This code doesn't work(whole screen renders black), but if I change it to

final += sampleTex[i] * kernel[0];

then my scene is rendered correctly, but, of course, without any blur, just a bit darker than normal. It also doesn't just work with kernel[0], but with any number ([1], [2], etc.)

If instead I change it to

final += sampleTex[i] * (kernel[i] + 0.05);

then it renders exactly the same as with

final += sampleTex[i] * 0.05;

And that's why I ended up suspecting that it's the [i] part that just won't work, for whatever reason.

Does anyone have any idea as to why this is happening? I also tried with different shader versions (330, 400, etc.) but they all give the same result.

Thanks for your time!

$\endgroup$
  • $\begingroup$ What OpenGL version are you using, and is it WebGL or a real program? $\endgroup$ – Dan Hulme Jul 21 '17 at 14:13
  • 1
    $\begingroup$ I'm using SDL2 to create a context for OpenGL 4.1. It's the regular version of OpenGL, not ES or WebGL $\endgroup$ – Daus Jul 21 '17 at 15:00
  • 2
    $\begingroup$ In your kernel array you divide float by int. I suspect that for some reason your compiler cast this to intinstead of float so information is lost and then to float. $\endgroup$ – narthex Jul 21 '17 at 23:21
  • $\begingroup$ Why don't you use the numerators from the kernel and divide the whole thing by 16.0 at the end just to see if it works? $\endgroup$ – user1118321 Jul 22 '17 at 1:56
  • $\begingroup$ I've tried both adding .0 to the divisions by 16 and removing the division altogether, and even changing the kernel to another post-proc effect, but it still gives me the same results. For now, my solution is to just unravel the loop and write all the steps manually. Would that be a good approach or a bad practice in a professional shader? $\endgroup$ – Daus Jul 22 '17 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.