CgExamples.Gl_14_bulge.buildLookAtMatrix C# (CSharp) Method

buildLookAtMatrix() static private method

Build a row-major (C-style) 4x4 matrix transform based on the parameters for gluLookAt.
static private buildLookAtMatrix ( double eyex, double eyey, double eyez, double centerx, double centery, double centerz, double upx, double upy, double upz, float &m ) : void
eyex double
eyey double
eyez double
centerx double
centery double
centerz double
upx double
upy double
upz double
m float
return void
        static void buildLookAtMatrix(double eyex, double eyey, double eyez,
                              double centerx, double centery, double centerz,
                              double upx, double upy, double upz,
                              ref float[] m)
        {
            double[] x = new double[3], y = new double[3], z = new double[3];
            double mag;

            /* Difference eye and center vectors to make Z vector. */
            z[0] = eyex - centerx;
            z[1] = eyey - centery;
            z[2] = eyez - centerz;
            /* Normalize Z. */
            mag = Math.Sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
            if (mag != 0)
            {
                z[0] /= mag;
                z[1] /= mag;
                z[2] /= mag;
            }

            /* Up vector makes Y vector. */
            y[0] = upx;
            y[1] = upy;
            y[2] = upz;

            /* X vector = Y cross Z. */
            x[0] = y[1] * z[2] - y[2] * z[1];
            x[1] = -y[0] * z[2] + y[2] * z[0];
            x[2] = y[0] * z[1] - y[1] * z[0];

            /* Recompute Y = Z cross X. */
            y[0] = z[1] * x[2] - z[2] * x[1];
            y[1] = -z[0] * x[2] + z[2] * x[0];
            y[2] = z[0] * x[1] - z[1] * x[0];

            /* Normalize X. */
            mag = Math.Sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
            if (mag != 0)
            {
                x[0] /= mag;
                x[1] /= mag;
                x[2] /= mag;
            }

            /* Normalize Y. */
            mag = Math.Sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
            if (mag != 0)
            {
                y[0] /= mag;
                y[1] /= mag;
                y[2] /= mag;
            }

            /* Build resulting view matrix. */
            m[0 * 4 + 0] = (float)x[0]; m[0 * 4 + 1] = (float)x[1];
            m[0 * 4 + 2] = (float)x[2]; m[0 * 4 + 3] = (float)(-x[0] * eyex + -x[1] * eyey + -x[2] * eyez);

            m[1 * 4 + 0] = (float)y[0]; m[1 * 4 + 1] = (float)y[1];
            m[1 * 4 + 2] = (float)y[2]; m[1 * 4 + 3] = (float)(-y[0] * eyex + -y[1] * eyey + -y[2] * eyez);

            m[2 * 4 + 0] = (float)z[0]; m[2 * 4 + 1] = (float)z[1];
            m[2 * 4 + 2] = (float)z[2]; m[2 * 4 + 3] = (float)(-z[0] * eyex + -z[1] * eyey + -z[2] * eyez);

            m[3 * 4 + 0] = (float)0.0; m[3 * 4 + 1] = (float)0.0; m[3 * 4 + 2] = 0.0f; m[3 * 4 + 3] = 1.0f;
        }