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