uniform sampler1D ColorIndex;
uniform float CETX;
uniform float CETY;
const int maxiterations = 16;
const float scaling = 160.0;
float xpos;
float ypos;
float xquad;
float yquad;
int color;
int loopcount;

void Iterate()
{
  ypos = 2.0 * xpos * ypos + CETY;
  xpos = xquad - yquad + CETX;
  xquad = pow( xpos, 2.0 );
  yquad = pow( ypos, 2.0 );
  color++;
  float tempstorage = xquad + yquad;
  if ( tempstorage > 4.0 ) loopcount = maxiterations;
  loopcount++;
}


void main(void)
{
  xpos = gl_FragCoord.x / scaling - 1.6;
  ypos = gl_FragCoord.y / scaling - 1.6;
  xquad = pow( xpos, 2.0 );
  yquad = pow( ypos, 2.0 );
  color = -1;
  loopcount = 0;
  if ( loopcount <= maxiterations ) Iterate(); // color=0
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); 
  if ( loopcount <= maxiterations ) Iterate(); // color=16
  float finalcolor = float( color ) / float( maxiterations );
  gl_FragColor = texture1D( ColorIndex, finalcolor );
  // for Greyscale use: gl_FragColor = vec4(finalcolor, finalcolor, finalcolor,0.0);
}