⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 camera.java

📁 NeHe用java与OpenGL结合教程源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

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

        /* Normalize the result */
        t = (float) (Math.sqrt(m_Frustum[2][0] * m_Frustum[2][0] + m_Frustum[2][1] * m_Frustum[2][1] + m_Frustum[2][2] * m_Frustum[2][2]));
        m_Frustum[2][0] /= t;
        m_Frustum[2][1] /= t;
        m_Frustum[2][2] /= t;
        m_Frustum[2][3] /= t;

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

        /* Normalize the result */
        t = (float) (Math.sqrt(m_Frustum[3][0] * m_Frustum[3][0] + m_Frustum[3][1] * m_Frustum[3][1] + m_Frustum[3][2] * m_Frustum[3][2]));
        m_Frustum[3][0] /= t;
        m_Frustum[3][1] /= t;
        m_Frustum[3][2] /= t;
        m_Frustum[3][3] /= t;

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

        /* Normalize the result */
        t = (float) (Math.sqrt(m_Frustum[4][0] * m_Frustum[4][0] + m_Frustum[4][1] * m_Frustum[4][1] + m_Frustum[4][2] * m_Frustum[4][2]));
        m_Frustum[4][0] /= t;
        m_Frustum[4][1] /= t;
        m_Frustum[4][2] /= t;
        m_Frustum[4][3] /= t;

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

        /* Normalize the result */
        t = (float) (Math.sqrt(m_Frustum[5][0] * m_Frustum[5][0] + m_Frustum[5][1] * m_Frustum[5][1] + m_Frustum[5][2] * m_Frustum[5][2]));
        m_Frustum[5][0] /= t;
        m_Frustum[5][1] /= t;
        m_Frustum[5][2] /= t;
        m_Frustum[5][3] /= t;
    }

    // This is the much faster version of the above member
    // function, however the speed increase is not gained
    // without a cost. If you rotate or translate the projection
    // matrix then this member will not work correctly. That is acceptable
    // in my book considering I very rarely do such a thing.
    // This function has far fewer operations in it and I
    // shaved off 2 square root functions by passing in the
    // near and far values. This member has:
    // 38 muliplications, 28 additions, 24 divisions, and
    // 12 subtractions for a total of 102 operations. Still hurts
    // but at least it is decent now. In practice this will
    // run about 2 times faster than the above function.
    public void updateFrustumFaster(GL gl) {

        float clip[] = new float[16],
                proj[] = new float[16],
                modl[] = new float[16],
                t;

        /* Get the current PROJECTION matrix from OpenGL */
        gl.glGetFloatv(GL.GL_PROJECTION_MATRIX, proj, 0);

        /* Get the current MODELVIEW matrix from OpenGL */
        gl.glGetFloatv(GL.GL_MODELVIEW_MATRIX, modl, 0);

        /* Combine the two matrices (multiply projection by modelview)
           but keep in mind this function will only work if you do NOT
          rotate or translate your projection matrix                  */
        clip[0] = modl[0] * proj[0];
        clip[1] = modl[1] * proj[5];
        clip[2] = modl[2] * proj[10] + modl[3] * proj[14];
        clip[3] = modl[2] * proj[11];

        clip[4] = modl[4] * proj[0];
        clip[5] = modl[5] * proj[5];
        clip[6] = modl[6] * proj[10] + modl[7] * proj[14];
        clip[7] = modl[6] * proj[11];

        clip[8] = modl[8] * proj[0];
        clip[9] = modl[9] * proj[5];
        clip[10] = modl[10] * proj[10] + modl[11] * proj[14];
        clip[11] = modl[10] * proj[11];

        clip[12] = modl[12] * proj[0];
        clip[13] = modl[13] * proj[5];
        clip[14] = modl[14] * proj[10] + modl[15] * proj[14];
        clip[15] = modl[14] * proj[11];

        /* Extract the numbers for the RIGHT plane */
        m_Frustum[0][0] = clip[3] - clip[0];
        m_Frustum[0][1] = clip[7] - clip[4];
        m_Frustum[0][2] = clip[11] - clip[8];
        m_Frustum[0][3] = clip[15] - clip[12];

        /* Normalize the result */
        t = (float) (Math.sqrt(m_Frustum[0][0] * m_Frustum[0][0] + m_Frustum[0][1] * m_Frustum[0][1] + m_Frustum[0][2] * m_Frustum[0][2]));
        m_Frustum[0][0] /= t;
        m_Frustum[0][1] /= t;
        m_Frustum[0][2] /= t;
        m_Frustum[0][3] /= t;

        /* Extract the numbers for the LEFT plane */
        m_Frustum[1][0] = clip[3] + clip[0];
        m_Frustum[1][1] = clip[7] + clip[4];
        m_Frustum[1][2] = clip[11] + clip[8];
        m_Frustum[1][3] = clip[15] + clip[12];

        /* Normalize the result */
        t = (float) (Math.sqrt(m_Frustum[1][0] * m_Frustum[1][0] + m_Frustum[1][1] * m_Frustum[1][1] + m_Frustum[1][2] * m_Frustum[1][2]));
        m_Frustum[1][0] /= t;
        m_Frustum[1][1] /= t;
        m_Frustum[1][2] /= t;
        m_Frustum[1][3] /= t;

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

        /* Normalize the result */
        t = (float) (Math.sqrt(m_Frustum[2][0] * m_Frustum[2][0] + m_Frustum[2][1] * m_Frustum[2][1] + m_Frustum[2][2] * m_Frustum[2][2]));
        m_Frustum[2][0] /= t;
        m_Frustum[2][1] /= t;
        m_Frustum[2][2] /= t;
        m_Frustum[2][3] /= t;

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

        /* Normalize the result */
        t = (float) (Math.sqrt(m_Frustum[3][0] * m_Frustum[3][0] + m_Frustum[3][1] * m_Frustum[3][1] + m_Frustum[3][2] * m_Frustum[3][2]));
        m_Frustum[3][0] /= t;
        m_Frustum[3][1] /= t;
        m_Frustum[3][2] /= t;
        m_Frustum[3][3] /= t;

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

        /* Normalize the result */
        t = (float) (Math.sqrt(m_Frustum[4][0] * m_Frustum[4][0] + m_Frustum[4][1] * m_Frustum[4][1] + m_Frustum[4][2] * m_Frustum[4][2]));
        m_Frustum[4][0] /= t;
        m_Frustum[4][1] /= t;
        m_Frustum[4][2] /= t;
        m_Frustum[4][3] /= t;

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

        /* Normalize the result */
        t = (float) (Math.sqrt(m_Frustum[5][0] * m_Frustum[5][0] + m_Frustum[5][1] * m_Frustum[5][1] + m_Frustum[5][2] * m_Frustum[5][2]));
        m_Frustum[5][0] /= t;
        m_Frustum[5][1] /= t;
        m_Frustum[5][2] /= t;
        m_Frustum[5][3] /= t;
    }

    // This member function checks to see if a sphere is in
    // the viewing volume.
    private boolean sphereInFrustum(Tuple3f p, float Radius) {

        int i;
        // The idea here is the same as the PointInFrustum function.

        for (i = 0; i < 6; i++) {
            // If the point is outside of the plane then its not in the viewing volume.
            if (m_Frustum[i][0] * p.x + m_Frustum[i][1] * p.y + m_Frustum[i][2] * p.z + m_Frustum[i][3] <= -Radius) {
                return false;
            }
        }
        return true;
    }

    // This member fuction checks to see if a point is in
    // the viewing volume.
    public boolean pointInFrustum(Tuple3f p) {

        int i;

        // The idea behind this algorithum is that if the point
        // is inside all 6 clipping planes then it is inside our
        // viewing volume so we can return true.

        for (i = 0; i < 6; i++) {
            if (m_Frustum[i][0] * p.x + m_Frustum[i][1] * p.y + m_Frustum[i][2] * p.z + m_Frustum[i][3] <= 0) {
                return false;
            }
        }
        return true;
    }

    // This member function checks to see if a sphere is in
    // the viewing volume.
    private boolean sphereInFrustum(float x, float y, float z, float Radius) {

        int i;
        // The idea here is the same as the PointInFrustum function.

        for (i = 0; i < 6; i++) {
            // If the point is outside of the plane then its not in the viewing volume.
            if (m_Frustum[i][0] * x + m_Frustum[i][1] * y + m_Frustum[i][2] * z + m_Frustum[i][3] <= -Radius) {
                return false;
            }
        }
        return true;
    }

    // This member fuction checks to see if a point is in
    // the viewing volume.

    public boolean pointInFrustum(float x, float y, float z) {

        int i;
        // The idea behind this algorithum is that if the point
        // is inside all 6 clipping planes then it is inside our
        // viewing volume so we can return true.

        for (i = 0; i < 6; i++) {	// Loop through all our clipping planes
            // If the point is outside of the plane then its not in the viewing volume.
            if (m_Frustum[i][0] * x + m_Frustum[i][1] * y + m_Frustum[i][2] * z + m_Frustum[i][3] <= 0) {
                return false;
            }
        }
        return true;
    }

    public void renderLensFlare(GL gl) {

        float Length = 0.0f;

        // Draw the flare only If the light source is in our line of sight
        if (sphereInFrustum(m_LightSourcePos, 1.0f)) {

            vLightSourceToCamera.sub(m_Position, m_LightSourcePos);     // Lets compute the vector that points to the camera from
            // the light source.
            Length = vLightSourceToCamera.length();                     // Save the length we will need it in a minute
            ptIntersect.scale(Length, m_DirectionVector);                // Now lets find an point along the cameras direction
            // vector that we can use as an intersection point.
            // Lets translate down this vector the same distance
            // that the camera is away from the light source.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -