GLSL Shader Demo

Created a simple demo application that loads video using OpenCV, converts to an OpenGL texture, and renders it in an OpenGL window with a Shader running on top of it. All colorization and animation beyond the original (nearly monochrome) video is in the shader, which was inspired by a Shadertoy Example by Justicle.

Box-Muller Transform

The Box-Muller Transform is method for generating two normally distributed random numbers from two uniformly distributed numbers. It is widely used in C++11 implementations, cuRand, and elsewhere.

The algorithm is simple:

  1. Compute uniform random variables \(U_0, U_1 \in N(0,1)\) using rand()
  2. Compute two Gaussian random variables as:
    1. \(Z_0 = \sqrt{-2 ln(U_0)} cos(2 \pi U_1) \)
    2. \(Z_1 = \sqrt{-2 ln(U_0)} sin(2 \pi U_1) \)

Wikipedia’s article at the time of this writing contains a concise bit of c++ to return one random number at a time:

#define TWO_PI 6.2831853071795864769252866
 
double generateGaussianNoise(const double &variance)
{
	static bool haveSpare = false;
	static double rand1, rand2;
 
	if(haveSpare)
	{
		haveSpare = false;
		return sqrt(variance * rand1) * sin(rand2);
	}
 
	haveSpare = true;
 
	rand1 = rand() / ((double) RAND_MAX);
	if(rand1 < 1e-100) rand1 = 1e-100;
	rand1 = -2 * log(rand1);
	rand2 = (rand() / ((double) RAND_MAX)) * TWO_PI;
 
	return sqrt(variance * rand1) * cos(rand2);
}

 

 

* Image Courtesy Cmglee CC BY-SA