To create a shader program that generates a night-vision effect, perform the following steps:
- Set up your vertex shader to pass along the position, normal, and texture coordinates via the Position, Normal, and TexCoord variables, respectively.
- 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(); }
- In the render function of your OpenGL program, perform the following steps:
- Bind to the FBO that you set up for rendering the scene to a texture.
- Select the pass1 subroutine function in the fragment shader via RenderPass.
- Render the scene.
- Bind to the default FBO.
- Select the pass2 subroutine function in the fragment shader via RenderPass.
- Draw a single quad that fills the viewport using texture coordinates that range from 0 to 1 in each direction.