Example #1
0
        /**
         * @see <a href="gluUnProject.html">gluUnProject</a>
         */
        public double []  un_project(double window_x, double window_y,
                                     double window_z, double [] modelview, double [] projection,
                                     int [] viewport)
        {
            Matrix4d product = new Matrix4d();

            Matrix4d.multiply(modelview, projection, product.m);
            if (product.invert() == null)
            {
                return(null);
            }

            Vector4d A = new Vector4d(window_x, window_y, window_z, 1.0);

            // map x and y from window coordinates
            A.v [0] = (A.v [0] - viewport [0]) / viewport [2];
            A.v [1] = (A.v [1] - viewport [1]) / viewport [3];

            // map to range -1 to 1, A = 2*A - I
            A.scalar_multiply(2).scalar_minus(1);

            A.multiply_right(product);
            double d = A.v [3];

            if (d == 0)
            {
                return(null);
            }

            double [] result = new double [3]; // vs. 4
            result [0] = A.v [0] / d;
            result [1] = A.v [1] / d;
            result [2] = A.v [2] / d;
            return(result);
        }