/// <summary> /// Creates a new orthopgrhic camera for a 3-dimensional scene, looking at a particular point. /// </summary> /// <param name="position">Position of the camera in 3-dimensional space.</param> /// <param name="lookAt">The point the camera should look at.</param> /// <param name="width">The width of the projection plane.</param> /// <param name="height">The height of the projection plane.</param> public OrthographicCamera(Point position, Point lookAt, float width, float height) : base(position, width, height) { var normal = (lookAt - position).Normalise(); var yAxis = new Vector(0.0f, 1.0f, 0.0f); Right = (yAxis % normal).Normalise(); Up = (normal % Right).Normalise(); }
/// <summary> /// Creates a new orthographic camera for a 3-dimensional scene. /// </summary> /// <param name="position">Position of the camera in the 3-dimensional scene.</param> /// <param name="up">The up direction of the viewing plane.</param> /// <param name="right">The right direction of the viewing plane.</param> /// <param name="width">The width of the projection plane.</param> /// <param name="height">The height of the projection plane.</param> public OrthographicCamera(Point position, Vector up, Vector right, float width, float height) : base(position, width, height) { Up = up.Normalise(); Right = right.Normalise(); if (Right * Up != 0.0f) { var normal = (Right % Up).Normalise(); Right = (Up % normal).Normalise(); Up = (normal % Right).Normalise(); } }
/// <summary> /// Constructs a ray in 3-dimensional space. /// </summary> /// <param name="start">The starting point of the ray.</param> /// <param name="direction">The direction of the ray.</param> public Ray(Point start, Vector direction) { if (start == null) { throw new ArgumentNullException("start"); } if (direction == null) { throw new ArgumentNullException("direction"); } if (direction.Norm() == 0.0f) { throw new ArgumentOutOfRangeException("direction"); } Start = start; Direction = direction; }
/// <summary> /// Creates a triangle in 3-dimensional space. /// </summary> /// <remarks>We assume points are specified in clockwise fashion to form the top of the triangle.</remarks> /// <param name="p1">The first point of the triangle.</param> /// <param name="p2">The second point of the triangle.</param> /// <param name="p3">The third point of the triangle.</param> public Triangle(Point p1, Point p2, Point p3) { if (p1 == null) { throw new ArgumentNullException("p1"); } if (p2 == null) { throw new ArgumentNullException("p2"); } if (p3 == null) { throw new ArgumentNullException("p3"); } P1 = p1; P2 = p2; P3 = p3; // Pre-calculate. normal = ((P2 - P1) % (P3 - P1)).Normalise(); if (normal == new Vector(0.0f, 0.0f, 0.0f)) { // Points form a line. throw new ArgumentOutOfRangeException("p3"); } d = normal * new Vector(P1.X, p1.Y, p1.Z); }
/// <summary> /// Calculates the distance along the specified ray the first intersection with this object occurs. /// </summary> /// <param name="ray">The ray to test.</param> /// <returns>The distance along the ray the first intersection with this object occurs. If no intersection, returns a negative value.</returns> public override float IntersectDistance(Ray ray) { if (ray == null) { throw new ArgumentNullException("ray"); } var nd = normal * ray.Direction; if (nd == 0) { return -1.0f; } var start = new Vector(ray.Start.X, ray.Start.Y, ray.Start.Z); var t = (d - normal * start) / (nd); var q = ray.Start + t * ray.Direction; if (((P2 - P1) % (q - P1)) * normal < 0.0f) { return -1.0f; } if (((P3 - P2) % (q - P2)) * normal < 0.0f) { return -1.0f; } if (((P1 - P3) % (q - P3)) * normal < 0.0f) { return -1.0f; } return t; }