private Mogre.Vector3 getOrbitalPosition(Mogre.Vector3 focalPoint, Quaternion direction, float distanceToFocalPoint) { Mogre.Vector3 eyePos, xAxis, yAxis, zAxis; Quaternion m_Orientation = direction; Mogre.Matrix3 m_ViewMatrix; // Get the rotation matrix from the quaternion m_ViewMatrix = m_Orientation.ToRotationMatrix(); // Get the axis we need them for the camera position calculation (the axes span a new coord frame) xAxis = new Mogre.Vector3(m_ViewMatrix[0, 0], m_ViewMatrix[0, 1], m_ViewMatrix[0, 2]); yAxis = new Mogre.Vector3(m_ViewMatrix[1, 0], m_ViewMatrix[1, 1], m_ViewMatrix[1, 2]); zAxis = new Mogre.Vector3(m_ViewMatrix[2, 0], m_ViewMatrix[2, 1], m_ViewMatrix[2, 2]); eyePos = focalPoint + zAxis * distanceToFocalPoint; // Transform our vector in the orbital space. Here we rotate and flip it. m_ViewMatrix[0, 3] = -xAxis.DotProduct(eyePos); m_ViewMatrix[1, 3] = -yAxis.DotProduct(eyePos); m_ViewMatrix[2, 3] = -zAxis.DotProduct(eyePos); Mogre.Vector3 m_CamPos = new Mogre.Vector3(); // So extract the cam position for the sake of completeness m_CamPos.x = m_ViewMatrix[0, 3]; m_CamPos.y = m_ViewMatrix[1, 3]; m_CamPos.z = m_ViewMatrix[2, 3]; return(m_CamPos); }
public static Tuple <bool, float> Intersects(Ray ray, Sphere sphere, bool discardInside) { Vector3 raydir = ray.Direction; // Adjust ray origin relative to sphere center Vector3 rayorig = ray.Origin - sphere.Center; float radius = sphere.Radius; // Check origin inside first if (rayorig.SquaredLength <= radius * radius && discardInside) { return(Tuple.Create(true, 0.0f)); } // Mmm, quadratics // Build coeffs which can be used with std quadratic solver // ie t = (-b +/- sqrt(b*b + 4ac)) / 2a float a = raydir.DotProduct(raydir); float b = 2 * rayorig.DotProduct(raydir); float c = rayorig.DotProduct(rayorig) - radius * radius; // Calc determinant float d = (b * b) - (4 * a * c); if (d < 0) { // No intersection return(Tuple.Create(false, 0.0f)); } // BTW, if d=0 there is one intersection, if d > 0 there are 2 // But we only want the closest one, so that's ok, just use the // '-' version of the solver float t = (-b - Math.Sqrt(d)) / (2 * a); if (t < 0) { t = (-b + Math.Sqrt(d)) / (2 * a); } return(Tuple.Create(true, t)); }
public static Vector3 CalculateTangentSpaceVector( Vector3 position1, Vector3 position2, Vector3 position3, float u1, float v1, float u2, float v2, float u3, float v3) { //side0 is the vector along one side of the triangle of vertices passed in, //and side1 is the vector along another side. Taking the cross product of these returns the normal. Vector3 side0 = position1 - position2; Vector3 side1 = position3 - position1; //Calculate face normal Vector3 normal = side1.CrossProduct(side0); normal.Normalise(); //Now we use a formula to calculate the tangent. float deltaV0 = v1 - v2; float deltaV1 = v3 - v1; Vector3 tangent = deltaV1 * side0 - deltaV0 * side1; tangent.Normalise(); //Calculate binormal float deltaU0 = u1 - u2; float deltaU1 = u3 - u1; Vector3 binormal = deltaU1 * side0 - deltaU0 * side1; binormal.Normalise(); //Now, we take the cross product of the tangents to get a vector which //should point in the same direction as our normal calculated above. //If it points in the opposite direction (the dot product between the normals is less than zero), //then we need to reverse the s and t tangents. //This is because the triangle has been mirrored when going from tangent space to object space. //reverse tangents if necessary Vector3 tangentCross = tangent.CrossProduct(binormal); if (tangentCross.DotProduct(normal) < 0.0f) { tangent = -tangent; binormal = -binormal; } return(tangent); }
private Mogre.Vector3 getOrbitalPosition(Mogre.Vector3 focalPoint, Quaternion direction, float distanceToFocalPoint) { Mogre.Vector3 eyePos, xAxis, yAxis, zAxis; Quaternion m_Orientation = direction; Mogre.Matrix3 m_ViewMatrix; // Get the rotation matrix from the quaternion m_ViewMatrix = m_Orientation.ToRotationMatrix(); // Get the axis we need them for the camera position calculation (the axes span a new coord frame) xAxis = new Mogre.Vector3(m_ViewMatrix[0, 0], m_ViewMatrix[0, 1], m_ViewMatrix[0, 2]); yAxis = new Mogre.Vector3(m_ViewMatrix[1, 0], m_ViewMatrix[1, 1], m_ViewMatrix[1, 2]); zAxis = new Mogre.Vector3(m_ViewMatrix[2, 0], m_ViewMatrix[2, 1], m_ViewMatrix[2, 2]); eyePos = focalPoint + zAxis * distanceToFocalPoint; // Transform our vector in the orbital space. Here we rotate and flip it. m_ViewMatrix[0, 3] = -xAxis.DotProduct(eyePos); m_ViewMatrix[1, 3] = -yAxis.DotProduct(eyePos); m_ViewMatrix[2, 3] = -zAxis.DotProduct(eyePos); Mogre.Vector3 m_CamPos = new Mogre.Vector3(); // So extract the cam position for the sake of completeness m_CamPos.x = m_ViewMatrix[0, 3]; m_CamPos.y = m_ViewMatrix[1, 3]; m_CamPos.z = m_ViewMatrix[2, 3]; return m_CamPos; }
public static Tuple <bool, float> Intersects(Ray ray, Vector3 a, Vector3 b, Vector3 c, Vector3 normal, bool positiveSide, bool negativeSide) { // // Calculate intersection with plane. // float t; { float denom = normal.DotProduct(ray.Direction); // Check intersect side if (denom > +float.Epsilon) { if (!negativeSide) { return(Tuple.Create(false, 0.0f)); } } else if (denom < -float.Epsilon) { if (!positiveSide) { return(Tuple.Create(false, 0.0f)); } } else { // Parallel or triangle area is close to zero when // the plane normal not normalised. return(Tuple.Create(false, 0.0f)); } t = normal.DotProduct(a - ray.Origin) / denom; if (t < 0) { // Intersection is behind origin return(Tuple.Create(false, 0.0f)); } } // // Calculate the largest area projection plane in X, Y or Z. // int i0, i1; { float n0 = Abs(normal[0]); float n1 = Abs(normal[1]); float n2 = Abs(normal[2]); i0 = 1; i1 = 2; if (n1 > n2) { if (n1 > n0) { i0 = 0; } } else { if (n2 > n0) { i1 = 0; } } } // // Check the intersection point is inside the triangle. // { float u1 = b[i0] - a[i0]; float v1 = b[i1] - a[i1]; float u2 = c[i0] - a[i0]; float v2 = c[i1] - a[i1]; float u0 = t * ray.Direction[i0] + ray.Origin[i0] - a[i0]; float v0 = t * ray.Direction[i1] + ray.Origin[i1] - a[i1]; float alpha = u0 * v2 - u2 * v0; float beta = u1 * v0 - u0 * v1; float area = u1 * v2 - u2 * v1; // epsilon to avoid float precision error const float EPSILON = 1e-3f; float tolerance = -EPSILON * area; if (area > 0) { if (alpha < tolerance || beta < tolerance || alpha + beta > area - tolerance) { return(Tuple.Create(false, 0.0f)); } } else { if (alpha > tolerance || beta > tolerance || alpha + beta < area - tolerance) { return(Tuple.Create(false, 0.0f)); } } } return(Tuple.Create(true, t)); }
/// <summary>Calculate a face normal without normalize, including the w component which is the offset from the origin. </summary> public static Vector4 CalculateFaceNormalWithoutNormalize(Vector3 v1, Vector3 v2, Vector3 v3) { Vector3 vector = Math.CalculateBasicFaceNormalWithoutNormalize(v1, v2, v3); return(new Vector4(vector.x, vector.y, vector.z, -vector.DotProduct(v1))); }