Exemplo n.º 1
0
        /// <summary>
        /// Builds a rotation 4 * 4 matrix created from an axis vector and an angle.
        /// </summary>
        /// <param name="m">The m.</param>
        /// <param name="angle">The angle.</param>
        /// <param name="v">The v.</param>
        /// <returns></returns>
        public static Matrix4f rotate(Matrix4f m, float angle, Vector3f v)
        {
            float c = Trigonometric.Cos(angle);
            float s = Trigonometric.Sin(angle);

            Vector3f axis = Geometric.Normalize(v);
            Vector3f temp = (1.0f - c) * axis;

            Matrix4f rotate = Matrix4f.identity();

            rotate[0, 0] = c + temp[0] * axis[0];
            rotate[0, 1] = 0 + temp[0] * axis[1] + s * axis[2];
            rotate[0, 2] = 0 + temp[0] * axis[2] - s * axis[1];

            rotate[1, 0] = 0 + temp[1] * axis[0] - s * axis[2];
            rotate[1, 1] = c + temp[1] * axis[1];
            rotate[1, 2] = 0 + temp[1] * axis[2] + s * axis[0];

            rotate[2, 0] = 0 + temp[2] * axis[0] + s * axis[1];
            rotate[2, 1] = 0 + temp[2] * axis[1] - s * axis[0];
            rotate[2, 2] = c + temp[2] * axis[2];

            Matrix4f result = Matrix4f.identity();

            result[0] = m[0] * rotate[0][0] + m[1] * rotate[0][1] + m[2] * rotate[0][2];
            result[1] = m[0] * rotate[1][0] + m[1] * rotate[1][1] + m[2] * rotate[1][2];
            result[2] = m[0] * rotate[2][0] + m[1] * rotate[2][1] + m[2] * rotate[2][2];
            result[3] = m[3];
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Build a look at view matrix.
        /// </summary>
        /// <param name="eye">The eye.</param>
        /// <param name="center">The center.</param>
        /// <param name="up">Up.</param>
        /// <returns></returns>
        public static Matrix4f lookAt(Vector3f eye, Vector3f center, Vector3f up)
        {
            Vector3f f = new Vector3f(Geometric.Normalize(center - eye));
            Vector3f s = new Vector3f(Geometric.Normalize(Geometric.cross(f, up)));
            Vector3f u = new Vector3f(Geometric.cross(s, f));

            Matrix4f Result = new Matrix4f(1);

            Result[0, 0] = s.x;
            Result[1, 0] = s.y;
            Result[2, 0] = s.z;
            Result[0, 1] = u.x;
            Result[1, 1] = u.y;
            Result[2, 1] = u.z;
            Result[0, 2] = -f.x;
            Result[1, 2] = -f.y;
            Result[2, 2] = -f.z;
            Result[3, 0] = -Geometric.Dot(s, eye);
            Result[3, 1] = -Geometric.Dot(u, eye);
            Result[3, 2] = Geometric.Dot(f, eye);
            return(Result);
        }