How to do it...

To build a shader program that uses a noise texture to create a cloud-like effect, perform the following steps:

  1. Set up your vertex shader to pass the texture coordinates to the fragment shader via the TexCoord variable.
  2. Use the following code for the fragment shader:
#define PI 3.14159265 
 
layout( binding=0 ) uniform sampler2D NoiseTex; 
 
uniform vec4 SkyColor = vec4( 0.3, 0.3, 0.9, 1.0 ); 
uniform vec4 CloudColor = vec4( 1.0, 1.0, 1.0, 1.0 ); 
 
in vec2 TexCoord; 
 
layout ( location = 0 ) out vec4 FragColor; 
 
void main() {
  vec4 noise = texture(NoiseTex, TexCoord); 
  float t = (cos( noise.g * PI ) + 1.0) / 2.0; 
  vec4 color = mix( SkyColor, CloudColor, t ); 
  FragColor = vec4( color.rgb , 1.0 ); 
} 

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.