protected (bool, float, float, float) TestLocalIntersection(Ray localRay) { Tuple dirCrossE2 = localRay.Direction.Cross(E2); float det = E1.Dot(dirCrossE2); if (Abs(det) < Constants.floatEps) { return(false, 0, 0, 0); } float invDet = 1.0f / det; Tuple p1ToOrigin = localRay.Origin - P1; float u = invDet * p1ToOrigin.Dot(dirCrossE2); if (u < 0 || u > 1) { return(false, 0, 0, 0); } Tuple originCrossE1 = p1ToOrigin.Cross(E1); float v = invDet * localRay.Direction.Dot(originCrossE1); if (v < 0 || (u + v) > 1) { return(false, 0, 0, 0); } float t = invDet * E2.Dot(originCrossE1); return(true, t, u, v); }
public override Tuple Lighting(Material m, Shape shape, Light l, Tuple position, Tuple toEye, Tuple normal, bool inShadow) { Tuple color = (m.Pattern == null)? m.Color : m.Pattern.ColorAtShape(shape, position); Tuple effectiveColor = color * l.Intensity; Tuple toLight = (l.Position - position).Normalize(); Tuple ambient = effectiveColor * m.Ambient; float ndotl = normal.Dot(toLight); Tuple diffuse = Tuple.Color(0, 0, 0); Tuple specular = Tuple.Color(0, 0, 0); if ((inShadow == false) && (ndotl >= 0)) { diffuse = effectiveColor * m.Diffuse * ndotl; Tuple reflect = (-toLight).Reflect(normal); float rdote = reflect.Dot(toEye); if (rdote > 0) { specular = l.Intensity * m.Specular * Pow(rdote, m.Shininess); } } return(ambient + diffuse + specular); }
protected override List <Intersection> LocalIntersect(Ray localRay) { Tuple sphereToRay = localRay.Origin - Tuple.Point(0, 0, 0); float a = localRay.Direction.Dot(localRay.Direction); float b = localRay.Direction.Dot(sphereToRay); float c = sphereToRay.Dot(sphereToRay) - 1; float discriminant = b * b - a * c; if (discriminant >= 0) { float sqrtDisc = System.MathF.Sqrt(discriminant); return(Intersection.Aggregate( new Intersection((-b - sqrtDisc) / a, this), new Intersection((-b + sqrtDisc) / a, this))); } return(Intersection.Aggregate()); }