    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.nio.FloatBuffer;
    006    import org.lwjgl.opengl.GL11;
    007
    008    @SideOnly(Side.CLIENT)
    009    public class ClippingHelperImpl extends ClippingHelper
    010    {
    011        private static ClippingHelperImpl instance = new ClippingHelperImpl();
    012        private FloatBuffer projectionMatrixBuffer = GLAllocation.createDirectFloatBuffer(16);
    013        private FloatBuffer modelviewMatrixBuffer = GLAllocation.createDirectFloatBuffer(16);
    014        private FloatBuffer field_78564_h = GLAllocation.createDirectFloatBuffer(16);
    015
    016        /**
    017         * Initialises the ClippingHelper object then returns an instance of it.
    018         */
    019        public static ClippingHelper getInstance()
    020        {
    021            instance.init();
    022            return instance;
    023        }
    024
    025        /**
    026         * Normalize the frustum.
    027         */
    028        private void normalize(float[][] par1ArrayOfFloat, int par2)
    029        {
    030            float var3 = MathHelper.sqrt_float(par1ArrayOfFloat[par2][0] * par1ArrayOfFloat[par2][0] + par1ArrayOfFloat[par2][1] * par1ArrayOfFloat[par2][1] + par1ArrayOfFloat[par2][2] * par1ArrayOfFloat[par2][2]);
    031            par1ArrayOfFloat[par2][0] /= var3;
    032            par1ArrayOfFloat[par2][1] /= var3;
    033            par1ArrayOfFloat[par2][2] /= var3;
    034            par1ArrayOfFloat[par2][3] /= var3;
    035        }
    036
    037        private void init()
    038        {
    039            this.projectionMatrixBuffer.clear();
    040            this.modelviewMatrixBuffer.clear();
    041            this.field_78564_h.clear();
    042            GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, this.projectionMatrixBuffer);
    043            GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, this.modelviewMatrixBuffer);
    044            this.projectionMatrixBuffer.flip().limit(16);
    045            this.projectionMatrixBuffer.get(this.projectionMatrix);
    046            this.modelviewMatrixBuffer.flip().limit(16);
    047            this.modelviewMatrixBuffer.get(this.modelviewMatrix);
    048            this.clippingMatrix[0] = this.modelviewMatrix[0] * this.projectionMatrix[0] + this.modelviewMatrix[1] * this.projectionMatrix[4] + this.modelviewMatrix[2] * this.projectionMatrix[8] + this.modelviewMatrix[3] * this.projectionMatrix[12];
    049            this.clippingMatrix[1] = this.modelviewMatrix[0] * this.projectionMatrix[1] + this.modelviewMatrix[1] * this.projectionMatrix[5] + this.modelviewMatrix[2] * this.projectionMatrix[9] + this.modelviewMatrix[3] * this.projectionMatrix[13];
    050            this.clippingMatrix[2] = this.modelviewMatrix[0] * this.projectionMatrix[2] + this.modelviewMatrix[1] * this.projectionMatrix[6] + this.modelviewMatrix[2] * this.projectionMatrix[10] + this.modelviewMatrix[3] * this.projectionMatrix[14];
    051            this.clippingMatrix[3] = this.modelviewMatrix[0] * this.projectionMatrix[3] + this.modelviewMatrix[1] * this.projectionMatrix[7] + this.modelviewMatrix[2] * this.projectionMatrix[11] + this.modelviewMatrix[3] * this.projectionMatrix[15];
    052            this.clippingMatrix[4] = this.modelviewMatrix[4] * this.projectionMatrix[0] + this.modelviewMatrix[5] * this.projectionMatrix[4] + this.modelviewMatrix[6] * this.projectionMatrix[8] + this.modelviewMatrix[7] * this.projectionMatrix[12];
    053            this.clippingMatrix[5] = this.modelviewMatrix[4] * this.projectionMatrix[1] + this.modelviewMatrix[5] * this.projectionMatrix[5] + this.modelviewMatrix[6] * this.projectionMatrix[9] + this.modelviewMatrix[7] * this.projectionMatrix[13];
    054            this.clippingMatrix[6] = this.modelviewMatrix[4] * this.projectionMatrix[2] + this.modelviewMatrix[5] * this.projectionMatrix[6] + this.modelviewMatrix[6] * this.projectionMatrix[10] + this.modelviewMatrix[7] * this.projectionMatrix[14];
    055            this.clippingMatrix[7] = this.modelviewMatrix[4] * this.projectionMatrix[3] + this.modelviewMatrix[5] * this.projectionMatrix[7] + this.modelviewMatrix[6] * this.projectionMatrix[11] + this.modelviewMatrix[7] * this.projectionMatrix[15];
    056            this.clippingMatrix[8] = this.modelviewMatrix[8] * this.projectionMatrix[0] + this.modelviewMatrix[9] * this.projectionMatrix[4] + this.modelviewMatrix[10] * this.projectionMatrix[8] + this.modelviewMatrix[11] * this.projectionMatrix[12];
    057            this.clippingMatrix[9] = this.modelviewMatrix[8] * this.projectionMatrix[1] + this.modelviewMatrix[9] * this.projectionMatrix[5] + this.modelviewMatrix[10] * this.projectionMatrix[9] + this.modelviewMatrix[11] * this.projectionMatrix[13];
    058            this.clippingMatrix[10] = this.modelviewMatrix[8] * this.projectionMatrix[2] + this.modelviewMatrix[9] * this.projectionMatrix[6] + this.modelviewMatrix[10] * this.projectionMatrix[10] + this.modelviewMatrix[11] * this.projectionMatrix[14];
    059            this.clippingMatrix[11] = this.modelviewMatrix[8] * this.projectionMatrix[3] + this.modelviewMatrix[9] * this.projectionMatrix[7] + this.modelviewMatrix[10] * this.projectionMatrix[11] + this.modelviewMatrix[11] * this.projectionMatrix[15];
    060            this.clippingMatrix[12] = this.modelviewMatrix[12] * this.projectionMatrix[0] + this.modelviewMatrix[13] * this.projectionMatrix[4] + this.modelviewMatrix[14] * this.projectionMatrix[8] + this.modelviewMatrix[15] * this.projectionMatrix[12];
    061            this.clippingMatrix[13] = this.modelviewMatrix[12] * this.projectionMatrix[1] + this.modelviewMatrix[13] * this.projectionMatrix[5] + this.modelviewMatrix[14] * this.projectionMatrix[9] + this.modelviewMatrix[15] * this.projectionMatrix[13];
    062            this.clippingMatrix[14] = this.modelviewMatrix[12] * this.projectionMatrix[2] + this.modelviewMatrix[13] * this.projectionMatrix[6] + this.modelviewMatrix[14] * this.projectionMatrix[10] + this.modelviewMatrix[15] * this.projectionMatrix[14];
    063            this.clippingMatrix[15] = this.modelviewMatrix[12] * this.projectionMatrix[3] + this.modelviewMatrix[13] * this.projectionMatrix[7] + this.modelviewMatrix[14] * this.projectionMatrix[11] + this.modelviewMatrix[15] * this.projectionMatrix[15];
    064            this.frustum[0][0] = this.clippingMatrix[3] - this.clippingMatrix[0];
    065            this.frustum[0][1] = this.clippingMatrix[7] - this.clippingMatrix[4];
    066            this.frustum[0][2] = this.clippingMatrix[11] - this.clippingMatrix[8];
    067            this.frustum[0][3] = this.clippingMatrix[15] - this.clippingMatrix[12];
    068            this.normalize(this.frustum, 0);
    069            this.frustum[1][0] = this.clippingMatrix[3] + this.clippingMatrix[0];
    070            this.frustum[1][1] = this.clippingMatrix[7] + this.clippingMatrix[4];
    071            this.frustum[1][2] = this.clippingMatrix[11] + this.clippingMatrix[8];
    072            this.frustum[1][3] = this.clippingMatrix[15] + this.clippingMatrix[12];
    073            this.normalize(this.frustum, 1);
    074            this.frustum[2][0] = this.clippingMatrix[3] + this.clippingMatrix[1];
    075            this.frustum[2][1] = this.clippingMatrix[7] + this.clippingMatrix[5];
    076            this.frustum[2][2] = this.clippingMatrix[11] + this.clippingMatrix[9];
    077            this.frustum[2][3] = this.clippingMatrix[15] + this.clippingMatrix[13];
    078            this.normalize(this.frustum, 2);
    079            this.frustum[3][0] = this.clippingMatrix[3] - this.clippingMatrix[1];
    080            this.frustum[3][1] = this.clippingMatrix[7] - this.clippingMatrix[5];
    081            this.frustum[3][2] = this.clippingMatrix[11] - this.clippingMatrix[9];
    082            this.frustum[3][3] = this.clippingMatrix[15] - this.clippingMatrix[13];
    083            this.normalize(this.frustum, 3);
    084            this.frustum[4][0] = this.clippingMatrix[3] - this.clippingMatrix[2];
    085            this.frustum[4][1] = this.clippingMatrix[7] - this.clippingMatrix[6];
    086            this.frustum[4][2] = this.clippingMatrix[11] - this.clippingMatrix[10];
    087            this.frustum[4][3] = this.clippingMatrix[15] - this.clippingMatrix[14];
    088            this.normalize(this.frustum, 4);
    089            this.frustum[5][0] = this.clippingMatrix[3] + this.clippingMatrix[2];
    090            this.frustum[5][1] = this.clippingMatrix[7] + this.clippingMatrix[6];
    091            this.frustum[5][2] = this.clippingMatrix[11] + this.clippingMatrix[10];
    092            this.frustum[5][3] = this.clippingMatrix[15] + this.clippingMatrix[14];
    093            this.normalize(this.frustum, 5);
    094        }
    095    }