/// <summary> /// Constructs an intersection /// </summary> /// <param name="point">The point at which the intersection occurred</param> /// <param name="normal">The normal direction at which the intersection occurred</param> /// <param name="impactDirection">The direction the ray was traveling on impact</param> /// <param name="obj">The object that was intersected</param> /// <param name="color">The object's raw color at the intersection point</param> /// <param name="distance">The distance from the ray's origin that the intersection occurred</param> public Intersection(Vector3 point, Vector3 normal, Vector3 impactDirection, DrawableSceneObject obj, Color color, float distance) { this.Point = point; this.Normal = normal; this.ImpactDirection = impactDirection; this.ObjectHit = obj; this.Color = color; this.Distance = distance; }
/// <summary> /// Determines whether a given ray intersects with any scene objects (other than excludedObject) /// </summary> /// <param name="ray">The ray to test</param> /// <param name="scene">The scene to test</param> /// <param name="excludedObject">An object that is not tested for intersections</param> /// <param name="intersection">If the intersection test succeeds, contains the closest intersection</param> /// <returns>A value indicating whether or not any scene object intersected with the ray</returns> private bool TryCalculateIntersection(Ray ray, Scene scene, DrawableSceneObject excludedObject, out Intersection intersection) { var closestDistance = float.PositiveInfinity; var closestIntersection = new Intersection(); foreach (var sceneObject in scene.DrawableObjects) { Intersection i; if (sceneObject != excludedObject && sceneObject.TryCalculateIntersection(ray, out i)) { if (i.Distance < closestDistance) { closestDistance = i.Distance; closestIntersection = i; } } } if (closestDistance == float.PositiveInfinity) { intersection = new Intersection(); return false; } else { intersection = closestIntersection; return true; } }