/// standard object services --------------------------------------------------- public Camera(StreamReader infile) { // read and condition view definition ViewPosition = new Vector(infile); viewDirection = new Vector(infile); viewAngle = infile.ReadFloat(); viewDirection = viewDirection.Unitize(); if (viewDirection.IsZero()) viewDirection = new Vector(0.0f, 0.0f, 1.0f); if (viewAngle < 10) viewAngle = 10; if (viewAngle > 160) viewAngle = 160; viewAngle *= (float)(Math.PI / 180); // make other directions of frame up = new Vector(0.0f, 1.0f, 0.0f); right = up.Cross(viewDirection).Unitize(); if (!right.IsZero()) up = viewDirection.Cross(right).Unitize(); else { up = new Vector(0.0f, 0.0f, viewDirection[1] < 0.0f ? 1.0f : -1.0f); right = up.Cross(viewDirection).Unitize(); } } // Camera
/* * @implementation * Adapted from: * <cite>'Fast, Minimum Storage Ray-Triangle Intersection' * Moller, Trumbore; * Journal Of Graphics Tools, v2n1p21, 1997. * http://www.acm.org/jgt/papers/MollerTrumbore97/</cite> */ public bool GetIntersection(Vector rayOrigin, Vector rayDirection, ref float hitDistance) { // begin calculating determinant - also used to calculate U parameter Vector pvec = rayDirection.Cross(edge2); // if determinant is near zero, ray lies in plane of triangle float det = edge1.Dot(pvec); const float EPSILON = 0.000001f; if ((det > -EPSILON) && (det < EPSILON)) return false; float inv_det = 1.0f / det; // calculate distance from vertex 0 to ray origin Vector tvec = rayOrigin - verts[0]; // calculate U parameter and test bounds float u = tvec.Dot(pvec) * inv_det; if ((u < 0.0f) || (u > 1.0f)) return false; // prepare to test V parameter Vector qvec = tvec.Cross(edge1); // calculate V parameter and test bounds float v = rayDirection.Dot(qvec) * inv_det; if ((v < 0.0f) || (u + v > 1.0f)) return false; // calculate t, ray intersects triangle hitDistance = edge2.Dot(qvec) * inv_det; // only allow intersections in the forward ray direction return hitDistance >= 0.0f; }