Пример #1
0
        public static double[] rayPlaneIntersect(Vector3 vRayPos, Vector3 vRayDir, double[] plane)
        {
            Vector3 planeNormal = new Vector3 (plane[0], plane[1], plane[2]);

            double denom = vRayDir.DotProduct (planeNormal);
            // Calculate the denominator first
            //if( Math.Abs(denom) < 0.0001f ) return null;                      // Make sure we won't create a divide by zero

            double numer = vRayPos.DotProduct (planeNormal) + plane[3];
            // Calculate the numerator next
            System.Console.WriteLine (denom.ToString () + "  " + numer.ToString ());

            double time = -numer / denom;
            // Get the time of intersection
            Vector3 result = new Vector3 ((time * vRayDir) + vRayPos);
            // Return the point of intersection
            return result.Position ();
        }
        //--------------------------------------------------------------
        public void modify() {
            if (mInputTriangleBuffer == null)
               OGRE_EXCEPT("Exception::ERR_INVALID_STATE", "Input triangle buffer must be set", "__FUNCTION__");
            ;

            Vector3[] directions = new Vector3[6] { Vector3.UNIT_X, Vector3.UNIT_Y, Vector3.UNIT_Z, Vector3.NEGATIVE_UNIT_X, Vector3.NEGATIVE_UNIT_Y, Vector3.NEGATIVE_UNIT_Z };

            //for (List<TriangleBuffer.Vertex>.Enumerator it = mInputTriangleBuffer.getVertices().begin(); it != mInputTriangleBuffer.getVertices().end(); ++it)
            foreach (var it in mInputTriangleBuffer.getVertices()) {
                Vector3 v = it.mPosition - mBoxCenter;
                if (v.IsZeroLength)
                    continue;
                //v.normalise();
                v.x /= mBoxSize.x;
                v.y /= mBoxSize.y;
                v.z /= mBoxSize.z;
                //C++ TO C# CONVERTER WARNING: The following line was determined to be a copy constructor call - this should be verified and a copy constructor should be created if it does not yet exist:
                //ORIGINAL LINE: Vector3 n = it->mNormal;
                Vector3 n = (it.mNormal);
                float maxAxis = 0;
                int principalAxis = 0;
                for (byte i = 0; i < 6; i++) {
                    if (directions[i].DotProduct(n) > maxAxis) {
                        maxAxis = directions[i].DotProduct(n);
                        principalAxis = i;
                    }
                }

                Vector3 vX = new Vector3();
                Vector3 vY = new Vector3();
                if (principalAxis % 3 == 1)
                    vY = Vector3.UNIT_X;
                else
                    vY = Vector3.UNIT_Y;
                vX = directions[principalAxis].CrossProduct(vY);

                Vector2 uv = new Vector2(0.5f - vX.DotProduct(v), 0.5f - vY.DotProduct(v));
                if (mMappingType == MappingType.MT_FULL)
                    it.mUV = uv;
                else if (mMappingType == MappingType.MT_CROSS) {
                }
                else if (mMappingType == MappingType.MT_PACKED)
                    it.mUV = new Vector2((uv.x + principalAxis % 3) / 3, (uv.y + principalAxis / 3) / 2);
            }
        }
Пример #3
0
        public static Radian angleBetween(Vector3 v1, Vector3 dest) {
            float lenProduct = v1.Length * dest.Length;

            // Divide by zero check
            if (lenProduct < 1e-6f)
                lenProduct = 1e-6f;

            float f = v1.DotProduct(dest) / lenProduct;

            f = Clamp(f, (float)-1.0f, 1.0f);
            return Math.ACos(f);
        }
        /// <summary>
        /// Calculates absolute UV values for all points of a face.
        /// </summary>
        /// <param name="faceVertsIn">Source array of native point values.</param>
        /// <param name="faceVertsOut">Destination array for calculated UV values.</param>
        /// <returns>True if successful, otherwise false.</returns>
        public bool ComputeFaceUVCoordinates(ref DFPurePoint[] faceVertsIn, ref DFPurePoint[] faceVertsOut)
        {
            // Get first three vertices of ngon (these three vertices are always non-collinear)
            Vector3 P0 = new Vector3(faceVertsIn[0].x, faceVertsIn[0].y, faceVertsIn[0].z);
            Vector3 P1 = new Vector3(faceVertsIn[1].x, faceVertsIn[1].y, faceVertsIn[1].z);
            Vector3 P2 = new Vector3(faceVertsIn[2].x, faceVertsIn[2].y, faceVertsIn[2].z);

            // Create coplanar vectors from p1->p0 and p2->p0
            Vector3 V0 = P1 - P0;
            Vector3 V1 = P2 - P0;

            // Orthogonalize V1
            V1 = V1 - (V0 * (V1.DotProduct(V0) / (V0.DotProduct(V0))));

            // Normalize both vectors
            V0.Normalize();
            V1.Normalize();

            // Compute first three vertices in 2D space
            DF2DPoint p0, p1, p2;
            p0.x = (Int32)P0.DotProduct(V0);
            p0.y = (Int32)P0.DotProduct(V1);
            p1.x = (Int32)P1.DotProduct(V0);
            p1.y = (Int32)P1.DotProduct(V1);
            p2.x = (Int32)P2.DotProduct(V0);
            p2.y = (Int32)P2.DotProduct(V1);

            // Initialise the params struct
            df3duvparams_lt Params = new df3duvparams_lt();
            Params.X = new float[4];
            Params.Y = new float[4];
            Params.Z = new float[4];
            Params.U = new float[4];
            Params.V = new float[4];

            // Initialise the conversion matrix
            df3duvmatrix_t Matrix = new df3duvmatrix_t();
            Matrix.UA = 1.0f;
            Matrix.UB = 0.0f;
            Matrix.UC = 0.0f;
            Matrix.UD = 0.0f;
            Matrix.VA = 0.0f;
            Matrix.VB = 1.0f;
            Matrix.VC = 0.0f;
            Matrix.UD = 0.0f;

            // Store the first 3 points of texture coordinates
            Params.U[0] = faceVertsIn[0].u;
            Params.U[1] = faceVertsIn[1].u + Params.U[0];
            Params.U[2] = faceVertsIn[2].u + Params.U[1];
            Params.V[0] = faceVertsIn[0].v;
            Params.V[1] = faceVertsIn[1].v + Params.V[0];
            Params.V[2] = faceVertsIn[2].v + Params.V[1];

            // Get and store the 1st point coordinates in face
            Params.X[0] = p0.x;
            Params.Y[0] = p0.y;
            Params.Z[0] = 0;

            // Get and store the 2nd point coordinates in face
            Params.X[1] = p1.x;
            Params.Y[1] = p1.y;
            Params.Z[1] = 0;

            // Get and store the 3rd point coordinates in face
            Params.X[2] = p2.x;
            Params.Y[2] = p2.y;
            Params.Z[2] = 0;

            // Compute the solution using an XY linear equation
            if (!l_ComputeDFUVMatrixXY(ref Matrix, ref Params))
                return false;

            // Assign matrix to all points if successful
            Int32 u = 0, v = 0;
            for (int point = 0; point < faceVertsIn.Length; point++)
            {
                if (point > 2)
                {
                    // Use generated matrix to calculate UV value from 2D point
                    DF2DPoint pn;
                    Vector3 PN = new Vector3(faceVertsIn[point].x, faceVertsIn[point].y, faceVertsIn[point].z);
                    pn.x = (Int32)PN.DotProduct(V0);
                    pn.y = (Int32)PN.DotProduct(V1);
                    u = (Int32)((pn.x * Matrix.UA) + (pn.y * Matrix.UB) + Matrix.UD);
                    v = (Int32)((pn.x * Matrix.VA) + (pn.y * Matrix.VB) + Matrix.VD);
                }
                else if (point == 0)
                {
                    // UV[0] is absolute
                    u = faceVertsIn[0].u;
                    v = faceVertsIn[0].v;
                }
                else if (point == 1)
                {
                    // UV[1] is a delta from UV[0]
                    u = faceVertsIn[0].u + faceVertsIn[1].u;
                    v = faceVertsIn[0].v + faceVertsIn[1].v;
                }
                else if (point == 2)
                {
                    // UV[2] is a delta from UV[1] + UV[0]
                    u = faceVertsIn[0].u + faceVertsIn[1].u + faceVertsIn[2].u;
                    v = faceVertsIn[0].v + faceVertsIn[1].v + faceVertsIn[2].v;
                }

                // Write outgoing point
                faceVertsOut[point].x = faceVertsIn[point].x;
                faceVertsOut[point].y = faceVertsIn[point].y;
                faceVertsOut[point].z = faceVertsIn[point].z;
                faceVertsOut[point].nx = faceVertsIn[point].nx;
                faceVertsOut[point].ny = faceVertsIn[point].ny;
                faceVertsOut[point].nz = faceVertsIn[point].nz;
                faceVertsOut[point].u = u;
                faceVertsOut[point].v = v;
            }

            return true;
        }