How to do it...

To create a shader program that generates a night-vision effect, perform the following steps:

  1. Set up your vertex shader to pass along the position, normal, and texture coordinates via the Position, Normal, and TexCoord variables, respectively.
  2. Use the following code for the fragment shader:
in vec3 Position; 
in vec3 Normal; 
in vec2 TexCoord; 
 
uniform int Width; 
uniform int Height; 
uniform float Radius; 
layout(binding=0) uniform sampler2D RenderTex; 
layout(binding=1) uniform sampler2D NoiseTex; 
 
subroutine vec4 RenderPassType(); 
subroutine uniform RenderPassType RenderPass; 
 
// Define any uniforms needed for the shading model. 
 
layout( location = 0 ) out vec4 FragColor; 
 
vec3 phongModel( vec3 pos, vec3 norm ) {
  // Compute the Phong shading model 
} 
 
// Returns the relative luminance of the color value 
float luminance( vec3 color ) { 
  return dot( color.rgb, vec3(0.2126, 0.7152, 0.0722) ); 
} 
 
subroutine (RenderPassType) 
vec4 pass1() {
  return vec4(phongModel( Position, Normal ),1.0); 
} 
 
subroutine( RenderPassType ) 
vec4 pass2() {
  vec4 noise = texture(NoiseTex, TexCoord); 
  vec4 color = texture(RenderTex, TexCoord); 
  float green = luminance( color.rgb ); 
 
  float dist1 = length(gl_FragCoord.xy - 
vec2(Width*0.25, Height*0.5));
float dist2 = length(gl_FragCoord.xy -
vec2(3.0*Width*0.25, Height*0.5)); if( dist1 > Radius && dist2 > Radius ) green = 0.0; return vec4(0.0, green * clamp(noise.a + 0.25, 0.0, 1.0),
0.0 ,1.0); } void main() { // This will call either pass1() or pass2() FragColor = RenderPass(); }
  1. In the render function of your OpenGL program, perform the following steps:
    1. Bind to the FBO that you set up for rendering the scene to a texture.
    2. Select the pass1 subroutine function in the fragment shader via RenderPass.
    3. Render the scene.
    4. Bind to the default FBO.
    5. Select the pass2 subroutine function in the fragment shader via RenderPass.
    6. Draw a single quad that fills the viewport using texture coordinates that range from 0 to 1 in each direction.
..................Content has been hidden....................

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