Opengl FBO高斯模糊混乱

时间:2015-04-11 23:18:54

标签: opengl blur

我对使用2遍高斯模糊模糊阴影贴感兴趣,但我对第二次和第三次传递感到有点困惑(片段着色器在下面是完整性)。我理解两个FBO之间的ping ponging,我可以很好地模糊我的纹理。但是渲染调用是让我感觉到的。我感兴趣的只是模糊我的纹理,但我必须使用至少一些模型调用我的渲染方法。目前在我的第二和第三遍我只是渲染一个四边形,这是必要的吗?我宁愿避免任何额外的操作。我应该用Java模糊吗?

public static void render(Entity entity) {


    GL20.glEnableVertexAttribArray(0);
    GL20.glEnableVertexAttribArray(1);
    GL20.glEnableVertexAttribArray(2);
    Matrix4f transformationMatrix = Maths.createTransformationMatrix(
            entity.getPosition(), entity.getRotX(), entity.getRotY(),
            entity.getRotZ(), entity.getScale());
    loadTransformationMatrix(transformationMatrix);
    createProjectionMatrix();



    GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(),
            GL11.GL_UNSIGNED_INT, 0);

    GL20.glDisableVertexAttribArray(0);
    GL20.glDisableVertexAttribArray(1);
    GL20.glDisableVertexAttribArray(2);
    // unbindVAO();

}

这是模糊片段着色器。

#version 430

in vec3 Position;
in vec3 Normal;

layout(binding=0) uniform sampler2D Texture0;

in vec2 TexCoord;

uniform int Width;
uniform int Height;

subroutine vec4 RenderPassType();
subroutine uniform RenderPassType RenderPass;

struct LightInfo {
  vec4 Position;  // Light position in eye coords.
  vec3 Intensity; // A,D,S intensity
};
uniform LightInfo Light;

struct MaterialInfo {
  vec3 Ka;            // Ambient reflectivity
  vec3 Kd;            // Diffuse reflectivity
  vec3 Ks;            // Specular reflectivity
  float Shininess;    // Specular shininess factor
};
uniform MaterialInfo Material;

layout( location = 0 ) out vec4 FragColor;


uniform float Weight[5] = float[5]( 0.12270270270,0.01945945946,          
 0.1216216216, 0.1540540541, 0.00162162162  );

vec3 phongModel( vec3 pos, vec3 norm )
{
vec3 s = normalize(vec3(Light.Position) - pos);
vec3 v = normalize(-pos.xyz);
vec3 r = reflect( -s, norm );
vec3 ambient = Light.Intensity * Material.Ka;
float sDotN = max( dot(s,norm), 0.0 );
vec3 diffuse = Light.Intensity * Material.Kd * sDotN;
vec3 spec = vec3(0.0);
if( sDotN > 0.0 )
    spec = Light.Intensity * Material.Ks *
           pow( max( dot(r,v), 0.0 ), Material.Shininess );

return ambient + diffuse + spec;
}

subroutine (RenderPassType)
vec4 pass1()
{
return vec4(phongModel( Position, Normal ),1.0);
}

subroutine( RenderPassType )
vec4 pass2()
{
ivec2 pix = ivec2( gl_FragCoord.xy );
vec4 sum = texelFetch(Texture0, pix, 0) * Weight[0];

sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,0) ) * Weight[0];
sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,-0) ) * Weight[0];

sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,1) ) * Weight[1];
sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,-1) ) * Weight[1];

sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,2) ) * Weight[2];
sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,-2) ) * Weight[2];

sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,3) ) * Weight[3];
sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,-3) ) * Weight[3];

sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,4) ) * Weight[4];
sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,-4) ) * Weight[4];

return sum;
}

subroutine( RenderPassType )
vec4 pass3()
{
ivec2 pix = ivec2( gl_FragCoord.xy );
vec4 sum = texelFetch(Texture0, pix, 0) * Weight[0];

sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,0) ) * Weight[0];
sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,-0) ) * Weight[0];

sum += texelFetchOffset( Texture0, pix, 0, ivec2(1,0) ) * Weight[1];
sum += texelFetchOffset( Texture0, pix, 0, ivec2(-1,0) ) * Weight[1];

sum += texelFetchOffset( Texture0, pix, 0, ivec2(2,0) ) * Weight[2];
sum += texelFetchOffset( Texture0, pix, 0, ivec2(-2,0) ) * Weight[2];

sum += texelFetchOffset( Texture0, pix, 0, ivec2(3,0) ) * Weight[3];
sum += texelFetchOffset( Texture0, pix, 0, ivec2(-3,0) ) * Weight[3];

sum += texelFetchOffset( Texture0, pix, 0, ivec2(4,0) ) * Weight[4];
sum += texelFetchOffset( Texture0, pix, 0, ivec2(-4,0) ) * Weight[4];
return sum;
}

void main()
{
vec4 texColor = texture( Texture0, TexCoord );
// This will call either pass1(), pass2(), or pass3()
FragColor = RenderPass()*2;
}

0 个答案:

没有答案