public IntersectionDto(Model.Ray ray, IBasicShape shape, float distanceT) { Ray = ray; Shape = shape; DistanceT = distanceT; TangentialIntersection = false; RayOrigin = RaysOrigin.Normal; }
/// <summary> /// Return only the first hit after calculating all /// possible intersections for all shapes in the scene. /// </summary> /// <remarks> /// This method offers an efficiency compared to <see cref="CalculateIntersections"/>. /// However if you need the intersections as well, then use <see cref="CalculateIntersections"/> /// instead and call the extension method <see cref="IntersectionExtensionMethods.GetHit"/>. /// </remarks> public IntersectionDto CalculateHit(Model.Ray ray) { return(( from s in _shapes from i in s.GetIntersections(ray) where i.DistanceT > 0F orderby i.DistanceT select i ).FirstOrDefault()); }
private static bool IsPointInShadow(World scene, Vector4 point) { // Vector from the point back to the light source. var v = scene.LightSource.Position - point; var distance = v.Length(); var direction = Vector4.Normalize(v); // Check for a "hit", within the world. var r = new Model.Ray(point, direction); var h = scene.CalculateHit(r); // If got a hit, check where (at what distance), // to determine whether point is in shadow. return(h.HasValue && h.DistanceT < distance); }
// NOTE: Notice below reference to the "acne" effect. This is a real rudimentary bootstrap // of a "special effect". May consider abstracting an "effects engine" at some stage, // But for now, just allowing for the effect, which is in essence a rounding error! public static LightingDto CalculateColorWithPhongReflection(World scene, Model.Ray ray, bool useAcneEffect = false) { var hit = scene.CalculateHit(ray); bool isInShadow = hit.HasValue && IsPointInShadow(scene, useAcneEffect ? hit.Position : hit.OverPosition); var color = CalculateColorWithPhongReflection( hit, scene.LightSource, isInShadow); return(new LightingDto { Color = color, Hit = hit, IsInShadow = isInShadow }); }
/// <summary> /// Calculate intersections with all shapes in the scene. Result includes all intersections /// for all shapes, with reference to the shape, ray and distanceT for each. /// </summary> public IEnumerable <IntersectionDto> CalculateIntersections(Model.Ray ray) { return(from shape in _shapes from intersection in shape.GetIntersections(ray) select intersection); }
public void CalculateRay_AtPixelLocation(int pixelX, int pixelY) { _rayInstance = _cameraInstance.GetRay(pixelX, pixelY); }