Exemple #1
0
        public float[] CalculateQuaternion(int x, int y, bool camera_z)
        {
            //  Map the current vector.
            MapToSphere((float)x, (float)y, out currentVector, camera_z);

            //  Compute the cross product of the begin and end vectors.
            Vertex cross = startVector.VectorProduct(currentVector);

            //  Is the perpendicular length essentially non-zero?
            if (cross.Magnitude() > 1.0e-5)
            {
                //  The quaternion is the transform.
                //return new float[] { cross.X, cross.Y, cross.Z, startVector.ScalarProduct(currentVector) };
                return(new float[] { cross.X, 0, cross.Z, startVector.ScalarProduct(currentVector) });
            }
            else
            {
                //  Begin and end coincide, return identity.
                return(new float[] { 0, 0, 0, 0 });
            }
        }
Exemple #2
0
        /// <summary>
        /// This function tests to see if a ray interesects the polygon.
        /// </summary>
        /// <param name="ray">The ray you want to test.</param>
        /// <returns>
        /// The distance from the origin of the ray to the intersection, or -1 if there
        /// is no intersection.
        /// </returns>
        private Intersection TestIntersection(Ray ray)
        {
            Intersection intersect = new Intersection();

            //	This code came from jgt intersect_triangle code (search dogpile for it).
            foreach (Face face in faces)
            {
                //	Assert that it's a triangle.
                if (face.Count != 3)
                {
                    continue;
                }

                //	Find the point of intersection upon the plane, as a point 't' along
                //	the ray.
                Vertex point1OnPlane = vertices[face.Indices[0].Vertex];
                Vertex point2OnPlane = vertices[face.Indices[1].Vertex];
                Vertex point3OnPlane = vertices[face.Indices[2].Vertex];
                Vertex midpointOpp1  = (point2OnPlane + point3OnPlane) / 2;
                Vertex midpointOpp2  = (point1OnPlane + point3OnPlane) / 2;
                Vertex midpointOpp3  = (point1OnPlane + point2OnPlane) / 2;

                Vertex planeNormal = face.GetSurfaceNormal(this);


                Vertex diff = point1OnPlane - ray.origin;
                float  s1   = diff.ScalarProduct(planeNormal);
                float  s2   = ray.direction.ScalarProduct(planeNormal);

                if (s2 == 0)
                {
                    continue;
                }
                float t = s1 / s2;
                if (t < 0)
                {
                    continue;
                }

                float denomintor = planeNormal.ScalarProduct(ray.direction);
                if (denomintor < 0.00001f && denomintor > -0.00001f)
                {
                    continue;                           //	doesn't intersect the plane.
                }
                //	Vertex v = point1OnPlane - ray.origin;
                //	float t = (v.ScalarProduct(planeNormal)) / denomintor;

                //	Now we can get the point of intersection.
                Vertex vIntersect = ray.origin + (ray.direction * t);

                //	Do my cool test.
                Vertex vectorTo1    = vIntersect - point1OnPlane;
                Vertex vectorTo2    = vIntersect - point2OnPlane;
                Vertex vectorTo3    = vIntersect - point3OnPlane;
                Vertex vectorMidTo1 = midpointOpp1 - point1OnPlane;
                Vertex vectorMidTo2 = midpointOpp2 - point2OnPlane;
                Vertex vectorMidTo3 = midpointOpp3 - point3OnPlane;

                if (vectorTo1.Magnitude() > vectorMidTo1.Magnitude())
                {
                    continue;
                }
                if (vectorTo2.Magnitude() > vectorMidTo2.Magnitude())
                {
                    continue;
                }
                if (vectorTo3.Magnitude() > vectorMidTo3.Magnitude())
                {
                    continue;
                }

                if (intersect.closeness == -1 || t < intersect.closeness)
                {
                    //	It's f*****g intersection city man
                    intersect.point       = vIntersect;
                    intersect.intersected = true;
                    intersect.normal      = planeNormal;
                    intersect.closeness   = t;
                }
            }

            return(intersect);
        }