1

現在、C++ を使用して Opengl でガウスぼかしシェーダーを実装しています。ガウスぼかしが機能します。ただし、結果は元の画像と比べて少しずれているようです。結果のぼかし画像は、いくらか高く配置され、いくらか左に配置されているように見えます。そして、オリジナルと同じ場所に集中していません。

私が使用しているガウスぼかしは、2 パス計算です。ウェブで見つけたデモコードに基づいてガウス計算を行いました。

ガウス カーネルの計算は次のようになります。

    for(i = 0; i <= center; ++i)
    {
        result            = exp(-(i*i)/(double)(2*_sigma*_sigma));
        _kernel[center+i]  = _kernel[center-i] = (float)result;
        sum              += (float)result;
        if(i != 0) sum   += (float)result;

    }

    // normalize kernel
    for(i = 0; i <= center; ++i)
    {
        _kernel[center+i] = _kernel[center-i] /= sum;
    }

オフセットの計算は次のようになります。

_offsets        = new float [_kernelSize * 2 + 1];
_offsetSize     = _kernelSize * 2 + 1;

float xInc      = 0;

if(_shaderType == S2DGaussianComponentBlurShaderHorizontal)
{
    xInc        = 1.0f / (float)_width;
}
else
{
    xInc        = 1.0f / (float)_height;
}

int   index     = 0;
for (int i = -_kernelSize; i < _kernelSize + 1; ++i)
{
    index            = i + _kernelSize;
    _offsets [index] = i * xInc;
}

フラグ シェーダは次のようになります。

#version 120   \n                               \
uniform sampler2D texture;                      \
varying vec4 texcoord;                          \
                                                \
                                                \
uniform int kernelSize;                         \
uniform float weight[50];                       \
uniform float offsets[50];                      \
void main(void)                                 \
{                                               \
vec2 uv  = texcoord.xy;                         \
vec4 sum = vec4(0.0);                           \
for (int i = 0; i< kernelSize; i++)             \
{                                               \
vec4 tmp       = texture2D( texture,  uv + vec2(0.0, offsets[i]) ); \
sum           += tmp * weight[i];               \
}                                               \
                                                \
gl_FragColor = sum;                             \
}"

テクスチャ入力はこれらを使用しています:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);

何が間違っているのでしょうか?

4

1 に答える 1

1

I figured it out.

It seems that the '_offsets' must be the same size as the kernel. The main reason for this is that the center position of the offsets must be at the same position as the center position of the kernel. If you don't do this, then the gaussian will not be aligned properly with the texture.

One way of doing this is to make the '_offsets' the same size as the kernel.

I have been looking for this solution for days -_-'

于 2013-01-31T20:08:50.227 に答える