How to do it...

In the fragment shader, we apply the Blinn-Phong reflection model in the first pass. In the second pass, we compute the vertical sum. In the third, we compute the horizontal sum:

in vec3 Position; // Vertex position 
in vec3 Normal;  // Vertex normal 

uniform int Pass; // Pass number
layout(binding=0) uniform sampler2D Texture0; // Light/material uniforms ....
layout( location = 0 ) out vec4 FragColor; uniform int PixOffset[5] = int[](0,1,2,3,4); uniform float Weight[5]; vec3 blinnPhong( vec3 pos, vec3 norm ) { // ... } vec4 pass1() { return vec4(blinnPhong( Position, normalize(Normal) ),1.0); } vec4 pass2() { ivec2 pix = ivec2(gl_FragCoord.xy); vec4 sum = texelFetch(Texture0, pix, 0) * Weight[0]; for( int i = 1; i < 5; i++ ) { sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,PixOffset[i])) * Weight[i]; sum += texelFetchOffset( Texture0, pix, 0, ivec2(0,-PixOffset[i])) * Weight[i]; } return sum; } vec4 pass3() { ivec2 pix = ivec2(gl_FragCoord.xy); vec4 sum = texelFetch(Texture0, pix, 0) * Weight[0]; for( int i = 1; i < 5; i++ ) { sum += texelFetchOffset( Texture0, pix, 0, ivec2(PixOffset[i],0)) * Weight[i]; sum += texelFetchOffset( Texture0, pix, 0, ivec2(-PixOffset[i],0)) * Weight[i]; } return sum; } void main() { if( Pass == 1 ) FragColor = pass1();
else if( Pass == 2 ) FragColor = pass2();
else if( Pass == 3 ) FragColor = pass3(); }

In the OpenGL application, compute the Gaussian weights for the offsets found in the uniform variable PixOffset, and store the results in the array Weight. You could use the following code to do so:

char uniName[20]; 
float weights[5], sum, sigma2 = 4.0f; 
 
// Compute and sum the weights 
weights[0] = gauss(0,sigma2); // The 1-D Gaussian function 
sum = weights[0]; 
for( int i = 1; i < 5; i++ ) { 
 weights[i] = gauss(i, sigma2); 
 sum += 2 * weights[i]; 
} 
 
// Normalize the weights and set the uniform 
for( int i = 0; i < 5; i++ ) { 
 snprintf(uniName, 20, "Weight[%d]", i); 
 prog.setUniform(uniName, weights[i] / sum); 
} 

In the main render function, implement the following steps for pass #1:

  1. Select the render framebuffer, enable the depth test, and clear the color/depth buffers
  2. Set Pass to 1
  3. Draw the scene

Use the following steps for pass #2:

  1. Select the intermediate framebuffer, disable the depth test, and clear the color buffer
  2. Set Pass to 2
  3. Set the view, projection, and model matrices to the identity matrix
  4. Bind the texture from pass #1 to texture unit zero
  5. Draw a fullscreen quad

Use the following steps for pass #3:

  1. Deselect the framebuffer (revert to the default), and clear the color buffer
  2. Set Pass to 3
  3. Bind the texture from pass #2 to texture unit zero
  4. Draw a fullscreen quad

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.13.201