public Sphere(P3 p, float r, P3 vn = null, P3 ve = null) { this.center = p; this.r = r; this.vn = vn == null ? new P3(0, 0, 1) : vn; this.ve = ve == null ? new P3(-1, 0, 0) : ve; }
public static RColor getLight(P3 p, P3 normal) { RColor c = new RColor(0, 0, 0); foreach (Light l in lights) { c = c.Add(l.illumination(p, normal)); } return c; }
public static RColor getLight(P3 p, P3 normal) { RColor c = new RColor(0, 0, 0); foreach (Light l in lights) { c = c.Add(l.illumination(p, normal)); } return(c); }
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 RColor illumination(P3 point, P3 normal) { float inten = this.p.Dot(normal); RColor color = new RColor(0, 0, 0); if (inten > 0) { color = c.Scale(inten); float d; SceneObject s = Form1.shootRay(new Ray(point, this.p), out d); if (s != null) { return new RColor(0, 0, 0); } } return color; }
public RColor illumination(P3 point, P3 normal) { float inten = this.p.Dot(normal); RColor color = new RColor(0, 0, 0); if (inten > 0) { color = c.Scale(inten); float d; SceneObject s = Form1.shootRay(new Ray(point, this.p), out d); if (s != null) { return(new RColor(0, 0, 0)); } } return(color); }
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 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 RColor illumination(P3 p, P3 normal) { return(l); }
public P3 Normal(P3 p) { return n; }
public Ray(P3 start, P3 direction) { this.start = start; this.direction = direction.Normalize(); color = new RColor(0, 0, 0); }
public P3 Normal(P3 p) { return(n); }
public RColor ColorAt(P3 p, Ray r) { return(Skin.Color(p, r, this)); }
public P3 Normal(P3 p, P3 x) { return x.Sub(p).Scale((1 / (x.Magnitude() - p.Magnitude()))); }
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 Theta(P3 p) { return((float)Math.Acos(Dot(p) / p.Magnitude() * this.Magnitude())); }
public float Dot(P3 p) { return((this.x * p.x) + (this.y * p.y) + (this.z * p.z)); }
public P3 Add(P3 p) { return(new P3(this.x + p.x, this.y + p.y, this.z + p.z)); }
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); }
public RColor illumination(P3 p, P3 normal) { return l; }
public P2 ToP2(P3 p) { return(new P2(0, 0)); }
public P3 Sub(P3 p) { return(new P3(this.x - p.x, this.y - p.y, this.z - p.z)); }
public DirectionalLight(RColor c, P3 p) { this.c = c; this.p = p; }
public P3 Cross(P3 p) { return(new P3((this.y * p.z) - (this.z * p.y), (this.z * p.x) - (this.x * p.z), (this.x * p.y) - (this.y * p.x))); }
public DirectionalLight(RColor c, float theta, float phi) { this.c = c; this.p = P3.fromSphere(theta, phi); }
public RColor ColorAt(P3 p, Ray r) { return Skin.Color(p, r, this); }
public P3 Normal(P3 p, P3 x) { return(x.Sub(p).Scale((1 / (x.Magnitude() - p.Magnitude())))); }
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 P3 Cross(P3 p) { return new P3((this.y * p.z) - (this.z * p.y), (this.z * p.x) - (this.x * p.z), (this.x * p.y) - (this.y * p.x)); }
public P2 ToP2(P3 p) { return new P2(0, 0); }
public P3 Sub(P3 p) { return new P3(this.x - p.x, this.y - p.y, this.z - p.z); }
public Plane(P3 p, P3 n) { this.p = p; this.n = n; d = -p.Dot(n); }
public P3 Add(P3 p) { return new P3(this.x + p.x, this.y + p.y, this.z + p.z); }
public P3 Travel(float distance) { this.start = this.start.Add(direction.Scale(distance)); return start; }
public float Dot(P3 p) { return ((this.x * p.x) + (this.y * p.y) + (this.z * p.z)); }
public float Theta(P3 p) { return (float)Math.Acos(Dot(p) / p.Magnitude() * this.Magnitude()); }
public P3 Travel(float distance) { this.start = this.start.Add(direction.Scale(distance)); return(start); }