Example #1
0
        public void ExtractFrustum()
        {
            float[]  proj = new float[16];                                       // For Grabbing The PROJECTION Matrix
            float[]  modl = new float[16];                                       // For Grabbing The MODELVIEW Matrix
            double[] clip = new double[16];                                      // Result Of Concatenating PROJECTION and MODELVIEW
            double   t;                                                          // Temporary Work Variable

            OpenGLControl.glGetFloatv(OpenGLControl.GL_PROJECTION_MATRIX, proj); // Grab The Current PROJECTION Matrix
            OpenGLControl.glGetFloatv(OpenGLControl.GL_MODELVIEW_MATRIX, modl);  // Grab The Current MODELVIEW Matrix

            // Concatenate (Multiply) The Two Matricies
            clip[0] = modl[0] * proj[0] + modl[1] * proj[4] + modl[2] * proj[8] + modl[3] * proj[12];
            clip[1] = modl[0] * proj[1] + modl[1] * proj[5] + modl[2] * proj[9] + modl[3] * proj[13];
            clip[2] = modl[0] * proj[2] + modl[1] * proj[6] + modl[2] * proj[10] + modl[3] * proj[14];
            clip[3] = modl[0] * proj[3] + modl[1] * proj[7] + modl[2] * proj[11] + modl[3] * proj[15];

            clip[4] = modl[4] * proj[0] + modl[5] * proj[4] + modl[6] * proj[8] + modl[7] * proj[12];
            clip[5] = modl[4] * proj[1] + modl[5] * proj[5] + modl[6] * proj[9] + modl[7] * proj[13];
            clip[6] = modl[4] * proj[2] + modl[5] * proj[6] + modl[6] * proj[10] + modl[7] * proj[14];
            clip[7] = modl[4] * proj[3] + modl[5] * proj[7] + modl[6] * proj[11] + modl[7] * proj[15];

            clip[8]  = modl[8] * proj[0] + modl[9] * proj[4] + modl[10] * proj[8] + modl[11] * proj[12];
            clip[9]  = modl[8] * proj[1] + modl[9] * proj[5] + modl[10] * proj[9] + modl[11] * proj[13];
            clip[10] = modl[8] * proj[2] + modl[9] * proj[6] + modl[10] * proj[10] + modl[11] * proj[14];
            clip[11] = modl[8] * proj[3] + modl[9] * proj[7] + modl[10] * proj[11] + modl[11] * proj[15];

            clip[12] = modl[12] * proj[0] + modl[13] * proj[4] + modl[14] * proj[8] + modl[15] * proj[12];
            clip[13] = modl[12] * proj[1] + modl[13] * proj[5] + modl[14] * proj[9] + modl[15] * proj[13];
            clip[14] = modl[12] * proj[2] + modl[13] * proj[6] + modl[14] * proj[10] + modl[15] * proj[14];
            clip[15] = modl[12] * proj[3] + modl[13] * proj[7] + modl[14] * proj[11] + modl[15] * proj[15];


            // Extract the RIGHT clipping plane
            m_fFrustum[0][0] = clip[3] - clip[0];
            m_fFrustum[0][1] = clip[7] - clip[4];
            m_fFrustum[0][2] = clip[11] - clip[8];
            m_fFrustum[0][3] = clip[15] - clip[12];

            // Normalize it
            t = (double)Math.Sqrt(m_fFrustum[0][0] * m_fFrustum[0][0] + m_fFrustum[0][1] * m_fFrustum[0][1] + m_fFrustum[0][2] * m_fFrustum[0][2]);
            m_fFrustum[0][0] /= t;
            m_fFrustum[0][1] /= t;
            m_fFrustum[0][2] /= t;
            m_fFrustum[0][3] /= t;

            // Extract the LEFT clipping plane
            m_fFrustum[1][0] = clip[3] + clip[0];
            m_fFrustum[1][1] = clip[7] + clip[4];
            m_fFrustum[1][2] = clip[11] + clip[8];
            m_fFrustum[1][3] = clip[15] + clip[12];

            // Normalize it
            t = (double)Math.Sqrt(m_fFrustum[1][0] * m_fFrustum[1][0] + m_fFrustum[1][1] * m_fFrustum[1][1] + m_fFrustum[1][2] * m_fFrustum[1][2]);
            m_fFrustum[1][0] /= t;
            m_fFrustum[1][1] /= t;
            m_fFrustum[1][2] /= t;
            m_fFrustum[1][3] /= t;


            // Extract the BOTTOM clipping plane
            m_fFrustum[2][0] = clip[3] + clip[1];
            m_fFrustum[2][1] = clip[7] + clip[5];
            m_fFrustum[2][2] = clip[11] + clip[9];
            m_fFrustum[2][3] = clip[15] + clip[13];

            // Normalize it
            t = (double)Math.Sqrt(m_fFrustum[2][0] * m_fFrustum[2][0] + m_fFrustum[2][1] * m_fFrustum[2][1] + m_fFrustum[2][2] * m_fFrustum[2][2]);
            m_fFrustum[2][0] /= t;
            m_fFrustum[2][1] /= t;
            m_fFrustum[2][2] /= t;
            m_fFrustum[2][3] /= t;


            // Extract the TOP clipping plane
            m_fFrustum[3][0] = clip[3] - clip[1];
            m_fFrustum[3][1] = clip[7] - clip[5];
            m_fFrustum[3][2] = clip[11] - clip[9];
            m_fFrustum[3][3] = clip[15] - clip[13];

            // Normalize it
            t = Math.Sqrt(m_fFrustum[3][0] * m_fFrustum[3][0] + m_fFrustum[3][1] * m_fFrustum[3][1] + m_fFrustum[3][2] * m_fFrustum[3][2]);
            m_fFrustum[3][0] /= t;
            m_fFrustum[3][1] /= t;
            m_fFrustum[3][2] /= t;
            m_fFrustum[3][3] /= t;


            // Extract the FAR clipping plane
            m_fFrustum[4][0] = clip[3] - clip[2];
            m_fFrustum[4][1] = clip[7] - clip[6];
            m_fFrustum[4][2] = clip[11] - clip[10];
            m_fFrustum[4][3] = clip[15] - clip[14];

            // Normalize it
            t = Math.Sqrt(m_fFrustum[4][0] * m_fFrustum[4][0] + m_fFrustum[4][1] * m_fFrustum[4][1] + m_fFrustum[4][2] * m_fFrustum[4][2]);
            m_fFrustum[4][0] /= t;
            m_fFrustum[4][1] /= t;
            m_fFrustum[4][2] /= t;
            m_fFrustum[4][3] /= t;


            // Extract the NEAR clipping plane.
            m_fFrustum[5][0] = clip[3] + clip[2];
            m_fFrustum[5][1] = clip[7] + clip[6];
            m_fFrustum[5][2] = clip[11] + clip[10];
            m_fFrustum[5][3] = clip[15] + clip[14];

            // Normalize it
            t = (double)Math.Sqrt(m_fFrustum[5][0] * m_fFrustum[5][0] + m_fFrustum[5][1] * m_fFrustum[5][1] + m_fFrustum[5][2] * m_fFrustum[5][2]);
            m_fFrustum[5][0] /= t;
            m_fFrustum[5][1] /= t;
            m_fFrustum[5][2] /= t;
            m_fFrustum[5][3] /= t;
        }