Index: /trunk/Mars/msimreflector/MFresnelLens.cc
===================================================================
--- /trunk/Mars/msimreflector/MFresnelLens.cc	(revision 19602)
+++ /trunk/Mars/msimreflector/MFresnelLens.cc	(revision 19603)
@@ -168,5 +168,5 @@
 }
 
-double CalcRefractiveIndex(const double &lambda)
+double CalcRefractiveIndex(double lambda)
 {
     // https://refractiveindex.info/?shelf=organic&book=poly(methyl_methacrylate)&page=Szczurowski
@@ -176,18 +176,28 @@
     const double l2 = lambda*lambda;
 
-    const double c0 = 0.99654/(1-0.00787/l2);
-    const double c1 = 0.18964/(1-0.02191/l2);
-    const double c2 = 0.00411/(1-3.85727/l2);
+    const double c0 = 0.99654/(1-0.00787e6/l2);
+    const double c1 = 0.18964/(1-0.02191e6/l2);
+    const double c2 = 0.00411/(1-3.85727e6/l2);
 
     return sqrt(1+c0+c1+c2);
 }
 
-void ApplyRefraction(MQuaternion &u, const TVector3 &n, const double &n1, const double &n2)
-{
-    // u: incoming direction
+bool ApplyRefraction(MQuaternion &u, const TVector3 &n, const double &n1, const double &n2)
+{
+    // From: https://stackoverflow.com/questions/29758545/how-to-find-refraction-vector-from-incoming-vector-and-surface-normal
+    // Note that as a default u is supposed to be normalized such that z=1!
+
+    // u: incoming direction (normalized!)
     // n: normal vector of surface
     // n2: refractive index of new(?) medium
     // n1: refractive index of old(?) medium
 
+    // V_refraction = r*V_incedence + (rc - sqrt(1- r^2 (1-c^2)))n
+    // r = n1/n2
+    // c = -n dot V_incedence.
+
+    // The vector should be normalized already
+    // u.NormalizeVector();
+
     const double r = n2/n1;
     const double c = n*u.fVectorPart;
@@ -195,9 +205,15 @@
     const double rc = r*c;
 
-    const double R = 1 - r*r + rc*rc; // = 1 - r*r *(1-c*c);
-
-    const double v = rc + sqrt(R<0 ? 0 : R);
-
-    u = r*u - n*v;
+    const double R = 1 - r*r + rc*rc; // = 1 - (r*r*(1-c*c));
+
+    // What is this? Total reflection?
+    if (R<0)
+        return false;
+
+    const double v = rc + sqrt(R);
+
+    u.fVectorPart = r*u.fVectorPart - v*n;
+
+    return true;
 }
 
@@ -274,5 +290,6 @@
     // Apply refraction at lens entrance (change directional vector)
     // FIXME: Surace roughness
-    ApplyRefraction(u, norm, 1, n);
+    if (!ApplyRefraction(u, norm, 1, n))
+        return -1;
 
     // Propagate ray until bottom of lens
@@ -285,5 +302,6 @@
     // Normal surface (bottom of lens)
     // FIXME: Surace roughness
-    ApplyRefraction(u, TVector3(0, 0, 1), n, 1);
+    if (!ApplyRefraction(u, TVector3(0, 0, 1), n, 1))
+        return -1;
 
     // To make this consistent with a mirror system,
