         // Detect overstepping & retreat.
         step = max(step,minstep);
         step = min(step,maxincrease*step0);
+        if (a1 <= a0) expected = 0.0;
+        else expected = a1 + step*(a1-a0)/(k1-k0);
         k0 = k1; a0 = a1;
       }
     }
   }
   vec3 eye = vec3(a,b,c);
   vec3 light = normalize(vec3(0.0,1.0,0.5));
-  if (!bracketed /*|| x*x+y*y+z*z > radius*radius*/) {
+  if (!found /*|| x*x+y*y+z*z > radius*radius*/) {
     //gl_FragColor = vec4(textureCube(uCubeMap,eye).rgb,1.0);
     discard;
   }

}

// Solution parameters.
// For better quality, use eg:
// maxstep = 0.5
// iterations = 200
// maxincrease = 1.1
const float maxstep = 2.0;     // The largest step that can be taken.
const float minstep = 0.0001;  // The smallest step
const int iterations = 100;    // Maximum number of iterations
const float maxincrease = 1.1; // Largest allowed step increase.

const float radius = 6.0; // Restrict view to this (unused)

void solve(float x0, float y0, float z0,
           float a, float b, float c) {
  float k0 = 0.0, k1;
  float a0 = Fun(x0,y0,z0,1.0), a1;
  bool bracketed = false;
  float step = 1.0;
  float x, y, z, w;
  for (int i = 0; i < iterations; i++) {
    if (bracketed) {
      // Once we are bracketed, just use bisection
      if (k1-k0 < minstep) break;
      float k2 = (k0 + k1)/2.0;
      x = x0+k2*a, y = y0+k2*b, z = z0+k2*c, w = 1.0;
      float a2 = Fun(x,y,z,w);

uniform int uFlags;
uniform samplerCube uCubeMap;
varying vec2 vTextureCoord;

// Things that should be uniforms
const vec3 defaultColor = vec3(1.0,0.5,0.0);

const float phi = 1.618033;
const float phi2 = phi*phi;
