private Ray GetReflectedRay(Ray r, SceneObject hitObject) { Vector3D n = hitObject.Geometry.GetNormalAtPoint(hitObject.Material.Hitpoint); float k = (-1f) * r.Direction * n; return new Ray(hitObject.Material.Hitpoint, r.Direction + (2 * k * n), hitObject); }
public SceneObject GetIntersectedObject(Ray r) { var hitObjects = from so in sceneObjects where so.Hit(r).Item1 orderby so.Hit(r).Item2 select so; return hitObjects.FirstOrDefault(); }
public Tuple<bool, float> Hit(Ray ray) { var ht = geometry.Hit(ray); if (ht.Item1) material.Hitpoint = ray.Origin + ray.Direction * ht.Item2; return ht; }
public override Tuple<bool, float> Hit(Ray r) { Tuple<bool, float> hit = plane.Hit(r); Vector3D hitpoint = r.Origin + hit.Item2 * r.Direction; Vector3D v = hitpoint - A; float dot = plane.GetNormalAtPoint(hitpoint) * v; if (v.Length <= radius && dot < Geometry.Epsilon) return hit; return Geometry.NoIntersection; }
public override Tuple<bool, float> Hit(Ray r) { Vector3D v = p - r.Origin; float a = n * v; float b = n * r.Direction; if (b < Geometry.Epsilon) return Geometry.NoIntersection; return new Tuple<bool, float>(true, a / b); }
public Color Shade(Ray ray) { Color L = Color.Black; Vector3D normal = parentObject.Geometry.GetNormalAtPoint(hitpoint); foreach (var light in world.Lights) { Vector3D toLight = (light.Position - hitpoint).Unit; float s = toLight * normal; if (s < 0f) s = 0f; L += props.Color * (props.Ambient + props.Diffuse * s); } return L; }
public override Tuple<bool, float> Hit(Ray r) { Vector3D v = r.Origin - center; float a = r.Direction * r.Direction; float b = 2f * v * r.Direction; float c = v * v - radius * radius; float delta = b * b - 4f * a * c; if (delta < 0f) return Geometry.NoIntersection; if (delta > 0f) { float t = (-b - (float)Math.Sqrt(delta)) / (2f * a); if (t > Geometry.Epsilon) return new Tuple<bool, float>(true, t); } return Geometry.NoIntersection; }
private Ray GetRefractedRay(Ray r, SceneObject hitObject) { Vector3D n = hitObject.Geometry.GetNormalAtPoint(hitObject.Material.Hitpoint); float n1 = r.ObjectAtEmission.Material.RefractiveIndex; float n2 = hitObject.Material.RefractiveIndex; float ri = n1 / n2; float c1 = -(n * r.Direction); float c2 = (float)Math.Sqrt(1f - ri * ri * (1f - c1 * c1)); Vector3D dir = (ri * r.Direction) + (ri * c1 - c2) * n; return new Ray(hitObject.Material.Hitpoint, dir, hitObject); }
private Color TraceRay(Ray r, int d) { Color L = Color.Black; if (d > maxDepth) return L; var hitObject = GetIntersectedObject(r); if (hitObject == null) return L; L = hitObject.Material.Shade(r); if (hitObject.Material.IsReflexive) L += TraceRay(GetReflectedRay(r, hitObject), d + 1); if (hitObject.Material.IsRefractive) L += TraceRay(GetRefractedRay(r, hitObject), d + 1); return L; }
public virtual Tuple<bool, float> Hit(Ray r) { throw new NotImplementedException(); }
public override Tuple<bool, float> Hit(Ray r) { return bt; }