public RColor Color(P3 p, Ray r, SceneObject s) { P3 N = s.Geo.Normal(p); float c1 = -N.Dot(r.direction); P3 R1 = r.direction.Add(N.Scale(2).Scale(c1)); float d; Ray r1 = new Ray(p, R1); SceneObject so = Form1.shootRay(r1, out d); if (so == null) { return new RColor(0, 0, 0); } else return so.ColorAt(r1.Travel(d), r1); }
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 static SceneObject shootRay(Ray r, out float d) { d = -1; SceneObject close = null; foreach (SceneObject s in objects) { float ds = s.Distance(r); if (ds != -1 && ds > 0.01) { if (d != -1) { if (ds < d) { d = ds; close = s; } } else { d = ds; close = s; } } } return close; }
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 float Distance(Ray r) { return Geo.Intersect(r); }
public RColor ColorAt(P3 p, Ray r) { return Skin.Color(p, r, this); }
public RColor Color(P3 p, Ray r, SceneObject s) { RColor light = Form1.getLight(p, s.Geo.Normal(p)); return light.Mul(s.Texture.Color(s.Geo.ToP2(p))); }
public float Intersect(Ray r) { float t = -(d + n.Dot(r.start)) / n.Dot(r.direction); return t; }