Ejemplo n.º 1
0
        public static Matrix4f createProjectionMatrix(float FovY, float aspectRatio, float zNear, float zFar)
        {
            Matrix4f projectionMatrix = new Matrix4f(1);

            projectionMatrix = perspective(Trigonometric.toRadians(FovY), aspectRatio, zNear, zFar);
            return(projectionMatrix);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public static Matrix4f createModelMatrix(Vector3f translation, Vector3f rotate, Vector3f scale)
        {
            Matrix4f modelMatrix = Matrix4f.identity();

            modelMatrix = translate(modelMatrix, translation);
            modelMatrix = MatrixMath.rotate(modelMatrix, Trigonometric.toRadians(rotate.x), new Vector3f(1, 0, 0));
            modelMatrix = MatrixMath.rotate(modelMatrix, Trigonometric.toRadians(rotate.y), new Vector3f(0, 1, 0));
            modelMatrix = MatrixMath.rotate(modelMatrix, Trigonometric.toRadians(rotate.z), new Vector3f(0, 0, 1));
            modelMatrix = MatrixMath.scale(modelMatrix, scale);
            return(modelMatrix);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a matrix for a symmetric perspective-view frustum with far plane
        /// at infinite for graphics hardware that doesn't support depth clamping.
        /// </summary>
        /// <param name="fovy">The fovy.</param>
        /// <param name="aspect">The aspect.</param>
        /// <param name="zNear">The z near.</param>
        /// <returns></returns>
        public static Matrix4f tweakedInfinitePerspective(float fovy, float aspect, float zNear)
        {
            float range  = Trigonometric.Tan(fovy / (2)) * zNear;
            float left   = -range * aspect;
            float right  = range * aspect;
            float bottom = -range;
            float top    = range;

            Matrix4f Result = new Matrix4f((0f));

            Result[0, 0] = ((2) * zNear) / (right - left);
            Result[1, 1] = ((2) * zNear) / (top - bottom);
            Result[2, 2] = (0.0001f) - (1f);
            Result[2, 3] = (-1);
            Result[3, 2] = -((0.0001f) - (2)) * zNear;
            return(Result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a matrix for a symmetric perspective-view frustum with far plane at infinite.
        /// </summary>
        /// <param name="fovy">The fovy.</param>
        /// <param name="aspect">The aspect.</param>
        /// <param name="zNear">The z near.</param>
        /// <returns></returns>
        public static Matrix4f infinitePerspective(float fovy, float aspect, float zNear)
        {
            float range = Trigonometric.Tan(fovy / (2f)) * zNear;

            float left   = -range * aspect;
            float right  = range * aspect;
            float bottom = -range;
            float top    = range;

            var result = new Matrix4f(0);

            result[0, 0] = ((2f) * zNear) / (right - left);
            result[1, 1] = ((2f) * zNear) / (top - bottom);
            result[2, 2] = -(1f);
            result[2, 3] = -(1f);
            result[3, 2] = -(2f) * zNear;
            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Builds a perspective projection matrix based on a field of view.
        /// </summary>
        /// <param name="fov">The fov (in radians).</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="zNear">The z near.</param>
        /// <param name="zFar">The z far.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        public static Matrix4f perspectiveFov(float fov, float width, float height, float zNear, float zFar)
        {
            if (width <= 0 || height <= 0 || fov <= 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            var rad = fov;

            var h = Trigonometric.Cos((0.5f) * rad) / Trigonometric.Sin((0.5f) * rad);
            var w = h * height / width;

            var result = new Matrix4f(0);

            result[0, 0] = w;
            result[1, 1] = h;
            result[2, 2] = -(zFar + zNear) / (zFar - zNear);
            result[2, 3] = -(1f);
            result[3, 2] = -((2f) * zFar * zNear) / (zFar - zNear);
            return(result);
        }