public float Intersect(Ray E) { P3 P; P3 EO = center.Sub(E.start); float d; float v = EO.Dot(E.direction); float disc = (r * r) - ((EO.Dot(EO) - (v * v))); if (disc < 0) { return(-1); } else { d = (float)Math.Sqrt(disc); P = E.start.Add(E.direction.Scale(v - d)); } if (v - d < 0 && v + d < 0) { return(-1); } else if (v - d > 0) { return(v - d); } else { return(v + d); } }
public void nonThreadedTrace() { float distance; P3 d; Ray r; SceneObject s; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { d = new P3(i / Form1.width * 2 - 1, 0, j / Form1.height * 2 - 1); r = new Ray(Form1.cameraPos, d.Sub(Form1.cameraPos)); s = Form1.shootRay(r, out distance); if (s != null) { try { Form1.cs[i, j] = s.ColorAt(r.Travel(distance), r); } catch (Exception e) { //Set pixel to black if there are any errors Form1.cs[i, j] = new RColor(0, 0, 0); } } } } }
public static void Trace(float minRange, float maxRange) { float distance; P3 d; Ray r; SceneObject s; for (int i = 0; i < Form1.width; i++) { for (int j = (int)minRange; j < (int)maxRange; j++) { d = new P3(i / Form1.width * 2 - 1, 0, j / Form1.height * 2 - 1); r = new Ray(Form1.cameraPos, d.Sub(Form1.cameraPos)); s = Form1.shootRay(r, out distance); if (s != null) { try { Form1.cs[i, j] = s.ColorAt(r.Travel(distance), r); } catch (FormatException e) { //Set pixel to black if there are any errors Console.WriteLine(e); Form1.cs[i, j] = new RColor(0, 0, 0); } } } } }
public P2 ToP2(P3 p) { P3 vp = p.Sub(center).Normalize(); //Console.WriteLine(-vn.Dot(vp)); float vndot = -vn.Dot(vp); if (vndot < -1) { vndot = -1; } else if (vndot > 1) { vndot = 1; } float phi = (float)Math.Acos(vndot); float v = phi / (float)Math.PI; float u; float vpdotsin = vp.Dot(ve) / (float)Math.Sin(phi); if (vpdotsin < -1) { vpdotsin = -1; } else if (vpdotsin > 1) { vpdotsin = 1; } //theta = ( arccos( dot_product( Vp, Ve ) / sin( phi )) ) / ( 2 * PI) float theta = ((float)Math.Acos(vpdotsin) / (2 * (float)Math.PI)); if (vp.Dot(vn.Cross(ve)) > 0) { u = theta; } else { u = 1 - theta; } //Console.WriteLine(u + " " + v); return(new P2(u, v)); }
public P2 ToP2(P3 p) { P3 vp = p.Sub(center).Normalize(); //Console.WriteLine(-vn.Dot(vp)); float vndot = -vn.Dot(vp); if (vndot < -1) vndot = -1; else if (vndot > 1) vndot = 1; float phi = (float)Math.Acos(vndot); float v = phi / (float)Math.PI; float u; float vpdotsin = vp.Dot(ve) / (float)Math.Sin(phi); if (vpdotsin < -1) vpdotsin = -1; else if (vpdotsin > 1) vpdotsin = 1; //theta = ( arccos( dot_product( Vp, Ve ) / sin( phi )) ) / ( 2 * PI) float theta = ((float)Math.Acos(vpdotsin) / (2 * (float)Math.PI)); if (vp.Dot(vn.Cross(ve)) > 0) { u = theta; } else { u = 1 - theta; } //Console.WriteLine(u + " " + v); return new P2(u, v); }
public P3 Normal(P3 p, P3 x) { return(x.Sub(p).Scale((1 / (x.Magnitude() - p.Magnitude())))); }
public P3 Normal(P3 p, P3 x) { return x.Sub(p).Scale((1 / (x.Magnitude() - p.Magnitude()))); }
public P3 Normal(P3 x) { return(x.Sub(this.center).Scale(1 / r)); }
public P3 Normal(P3 x) { return x.Sub(this.center).Scale(1 / r); }