OpenGL屏幕空间运动模糊

时间:2013-07-22 11:47:12

标签: opengl glsl

我正在尝试实现基于后处理运动矢量的运动模糊。 它有点“有效”......

我遇到了两个主要问题:

  1. 如果对象和相机都没有移动,则图形会产生空白 屏幕。
  2. 屏幕边框上有物体扭曲。
  3. 这是2遍方法:传递#1 - 将对象渲染到FBO texture.pass#2 - 渲染到全屏四边形并将MB应用于上一个传递输出。

    第二遍的顶点着色器:

    void main(void){
        // transform previous and current position to eye space
        vec4 P =     MODEL_VIEW_MATRIX * position;
        vec4 Pprev = prevModelView * position;
        vec3 motionVector = P.xyz - Pprev.xyz;
    
        // calculate window space motion vector
        P =           PROJ  * MODEL_VIEW_MATRIX * position;
        Pprev =       PROJ * prevModelView * position;
        Pprev = mix(P, Pprev, blurScale);
    
        // choose previous or current position based on dot product between motion vector 
        // and normal
        vec3 N = mat3(MODEL_VIEW_MATRIX) *normal;
        bool flag = dot(motionVector, N) > 0;
        vec4 Pstretch = flag ? P : Pprev;
        gl_Position =  position;
    
        // do divide by W -> NDC coordinates
        P.xyz = P.xyz / P.w;
        Pprev.xyz = Pprev.xyz / Pprev.w;
    
        // calculate window space velocity
        vec3 dP = (P.xyz - Pprev.xyz) * halfWindowSize.xyz;
        float len = length (dP)/(halfWindowSize[0]*2);
        len = clamp(len, 0., 1.);
        dP =  normalize(dP);
        dP.z = len; 
        out_velocity = dP;
    }
    

    第二遍的片段着色器:

    void main(void){
        float w = 1.0 / samples;        
        vec4 a=vec4(0.);
        vec2 velocity = out_velocity.xy * blurScale * .05 * out_velocity.z;
    
        ivec2 tsize = textureSize(COLOR_MAP_0, 0);
        vec2 screntc = gl_FragCoord.xy * (1.0 / vec2(tsize));
    
        // sample into scene texture along motion vector 
        for(float i=0; i<samples; i+=1) 
        {
            float t = i / (samples-1);
            a = a + texture(COLOR_MAP_0, vec2(screntc)  + velocity * t ) * w;               
        }
    
        colorOut = a;
     }
    

    这些着色器有什么问题?

0 个答案:

没有答案