1
\$\begingroup\$

I've based my shader on this single pass shadertoy here: https://www.shadertoy.com/view/XdfGDH

I applied the same shader and it looks like this: enter image description here

Scaled up so you can see the faint tears in the texture. They look grid-like and not smooth at all.

When it should be looking like this: enter image description here

Source image: enter image description here

Here's my shader. Translated the shadertoy to GLSL 110

#version 110
uniform sampler2D texture;

float normpdf(float x, float sigma)
{
  return 0.39894*exp(-0.5*x*x / (sigma*sigma)) / sigma;
}


void main()
{
  vec3 c = texture2D(texture, gl_TexCoord[0].xy).rgb;

  const int mSize = 11;
  const int kSize = const int((float(mSize) - 1.0) / 2.0);
  float kernel[mSize];
  vec3 final_color = vec3(0.0);

  // Create the kernel
  float sigma = 7.0;
  float Z = 0.0;
  for (int j = 0; j <= kSize; ++j)
  {
    kernel[kSize + j] = kernel[kSize - j] = normpdf(float(j), sigma);
  }

  //get the normalization factor (as the gaussian has been clamped)
  for (int j = 0; j < mSize; ++j)
  {
    Z += kernel[j];
  }

  //read out the texels
  for (int i = -kSize; i <= kSize; ++i)
  {
    for (int j = -kSize; j <= kSize; ++j)
    {
      final_color += kernel[kSize + j] * kernel[kSize + i] * texture2D(texture, (gl_TexCoord[0].xy + vec2(float(i), float(j)))).rgb;
    }
  }

  gl_FragColor = vec4(final_color / (Z*Z), 1.0);
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Your sampling points are most likely messed up because you are not taking into account that texture texel size is in range [0; 1]. You need to divide offset coordinates by texture resolution. We can get texture resolution using ivec2 textureSize(gsampler2D sampler, int lod) function.

So changing this

final_color += kernel[kSize + j] * kernel[kSize + i] * texture2D(texture, (gl_TexCoord[0].xy + vec2(float(i), float(j)))).rgb;

to this

final_color += kernel[kSize + j] * kernel[kSize + i] * texture2D(texture, (gl_TexCoord[0].xy + vec2(float(i), float(j))/textureSize(texture, 0).xy)).rgb;

will hopefully fix your shader.

\$\endgroup\$
7
  • \$\begingroup\$ but doesnt texCoord[0].xy always returns texel values from 0.0 to 1.0? \$\endgroup\$
    – Dan
    Dec 15, 2018 at 0:29
  • 1
    \$\begingroup\$ @Maverick please notice that I divide not texCoord[0].xy but offset coordinates. \$\endgroup\$
    – Ocelot
    Dec 15, 2018 at 0:37
  • \$\begingroup\$ I'm assuming that fixed the grid stretch marks. its possible now that the shader is blurring it and why I can't see those lines anymore. The shader overall is still not working as seen here: streamable.com/8b5lt \$\endgroup\$
    – Dan
    Dec 15, 2018 at 0:56
  • 1
    \$\begingroup\$ @Maverick sure, pastebin.com/4j7BwAXG But I had to port it to the modern OpenGL version. Changes in the code are minimal, but it could be that something is wrong with your pipeline. \$\endgroup\$
    – Ocelot
    Dec 15, 2018 at 1:14
  • 1
    \$\begingroup\$ Aha I placed the texture size division in the right spot. Needed to divide vec2(i, j) and then add it to the tex coord. I was dividing the sum of all three. thanks! \$\endgroup\$
    – Dan
    Dec 15, 2018 at 1:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .