    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.util.ArrayList;
    006    import java.util.Iterator;
    007    import java.util.List;
    008    import org.lwjgl.opengl.GL11;
    009
    010    public class ModelRenderer
    011    {
    012        /** The size of the texture file's width in pixels. */
    013        public float textureWidth;
    014
    015        /** The size of the texture file's height in pixels. */
    016        public float textureHeight;
    017
    018        /** The X offset into the texture used for displaying this model */
    019        private int textureOffsetX;
    020
    021        /** The Y offset into the texture used for displaying this model */
    022        private int textureOffsetY;
    023        public float rotationPointX;
    024        public float rotationPointY;
    025        public float rotationPointZ;
    026        public float rotateAngleX;
    027        public float rotateAngleY;
    028        public float rotateAngleZ;
    029        private boolean compiled;
    030
    031        /** The GL display list rendered by the Tessellator for this model */
    032        private int displayList;
    033        public boolean mirror;
    034        public boolean showModel;
    035
    036        /** Hides the model. */
    037        public boolean isHidden;
    038        public List cubeList;
    039        public List childModels;
    040        public final String boxName;
    041        private ModelBase baseModel;
    042
    043        public ModelRenderer(ModelBase par1ModelBase, String par2Str)
    044        {
    045            this.textureWidth = 64.0F;
    046            this.textureHeight = 32.0F;
    047            this.compiled = false;
    048            this.displayList = 0;
    049            this.mirror = false;
    050            this.showModel = true;
    051            this.isHidden = false;
    052            this.cubeList = new ArrayList();
    053            this.baseModel = par1ModelBase;
    054            par1ModelBase.boxList.add(this);
    055            this.boxName = par2Str;
    056            this.setTextureSize(par1ModelBase.textureWidth, par1ModelBase.textureHeight);
    057        }
    058
    059        public ModelRenderer(ModelBase par1ModelBase)
    060        {
    061            this(par1ModelBase, (String)null);
    062        }
    063
    064        public ModelRenderer(ModelBase par1ModelBase, int par2, int par3)
    065        {
    066            this(par1ModelBase);
    067            this.setTextureOffset(par2, par3);
    068        }
    069
    070        /**
    071         * Sets the current box's rotation points and rotation angles to another box.
    072         */
    073        public void addChild(ModelRenderer par1ModelRenderer)
    074        {
    075            if (this.childModels == null)
    076            {
    077                this.childModels = new ArrayList();
    078            }
    079
    080            this.childModels.add(par1ModelRenderer);
    081        }
    082
    083        public ModelRenderer setTextureOffset(int par1, int par2)
    084        {
    085            this.textureOffsetX = par1;
    086            this.textureOffsetY = par2;
    087            return this;
    088        }
    089
    090        public ModelRenderer addBox(String par1Str, float par2, float par3, float par4, int par5, int par6, int par7)
    091        {
    092            par1Str = this.boxName + "." + par1Str;
    093            TextureOffset var8 = this.baseModel.getTextureOffset(par1Str);
    094            this.setTextureOffset(var8.textureOffsetX, var8.textureOffsetY);
    095            this.cubeList.add((new ModelBox(this, this.textureOffsetX, this.textureOffsetY, par2, par3, par4, par5, par6, par7, 0.0F)).func_78244_a(par1Str));
    096            return this;
    097        }
    098
    099        public ModelRenderer addBox(float par1, float par2, float par3, int par4, int par5, int par6)
    100        {
    101            this.cubeList.add(new ModelBox(this, this.textureOffsetX, this.textureOffsetY, par1, par2, par3, par4, par5, par6, 0.0F));
    102            return this;
    103        }
    104
    105        /**
    106         * Creates a textured box. Args: originX, originY, originZ, width, height, depth, scaleFactor.
    107         */
    108        public void addBox(float par1, float par2, float par3, int par4, int par5, int par6, float par7)
    109        {
    110            this.cubeList.add(new ModelBox(this, this.textureOffsetX, this.textureOffsetY, par1, par2, par3, par4, par5, par6, par7));
    111        }
    112
    113        public void setRotationPoint(float par1, float par2, float par3)
    114        {
    115            this.rotationPointX = par1;
    116            this.rotationPointY = par2;
    117            this.rotationPointZ = par3;
    118        }
    119
    120        @SideOnly(Side.CLIENT)
    121        public void render(float par1)
    122        {
    123            if (!this.isHidden)
    124            {
    125                if (this.showModel)
    126                {
    127                    if (!this.compiled)
    128                    {
    129                        this.compileDisplayList(par1);
    130                    }
    131
    132                    Iterator var2;
    133                    ModelRenderer var3;
    134
    135                    if (this.rotateAngleX == 0.0F && this.rotateAngleY == 0.0F && this.rotateAngleZ == 0.0F)
    136                    {
    137                        if (this.rotationPointX == 0.0F && this.rotationPointY == 0.0F && this.rotationPointZ == 0.0F)
    138                        {
    139                            GL11.glCallList(this.displayList);
    140
    141                            if (this.childModels != null)
    142                            {
    143                                var2 = this.childModels.iterator();
    144
    145                                while (var2.hasNext())
    146                                {
    147                                    var3 = (ModelRenderer)var2.next();
    148                                    var3.render(par1);
    149                                }
    150                            }
    151                        }
    152                        else
    153                        {
    154                            GL11.glTranslatef(this.rotationPointX * par1, this.rotationPointY * par1, this.rotationPointZ * par1);
    155                            GL11.glCallList(this.displayList);
    156
    157                            if (this.childModels != null)
    158                            {
    159                                var2 = this.childModels.iterator();
    160
    161                                while (var2.hasNext())
    162                                {
    163                                    var3 = (ModelRenderer)var2.next();
    164                                    var3.render(par1);
    165                                }
    166                            }
    167
    168                            GL11.glTranslatef(-this.rotationPointX * par1, -this.rotationPointY * par1, -this.rotationPointZ * par1);
    169                        }
    170                    }
    171                    else
    172                    {
    173                        GL11.glPushMatrix();
    174                        GL11.glTranslatef(this.rotationPointX * par1, this.rotationPointY * par1, this.rotationPointZ * par1);
    175
    176                        if (this.rotateAngleZ != 0.0F)
    177                        {
    178                            GL11.glRotatef(this.rotateAngleZ * (180F / (float)Math.PI), 0.0F, 0.0F, 1.0F);
    179                        }
    180
    181                        if (this.rotateAngleY != 0.0F)
    182                        {
    183                            GL11.glRotatef(this.rotateAngleY * (180F / (float)Math.PI), 0.0F, 1.0F, 0.0F);
    184                        }
    185
    186                        if (this.rotateAngleX != 0.0F)
    187                        {
    188                            GL11.glRotatef(this.rotateAngleX * (180F / (float)Math.PI), 1.0F, 0.0F, 0.0F);
    189                        }
    190
    191                        GL11.glCallList(this.displayList);
    192
    193                        if (this.childModels != null)
    194                        {
    195                            var2 = this.childModels.iterator();
    196
    197                            while (var2.hasNext())
    198                            {
    199                                var3 = (ModelRenderer)var2.next();
    200                                var3.render(par1);
    201                            }
    202                        }
    203
    204                        GL11.glPopMatrix();
    205                    }
    206                }
    207            }
    208        }
    209
    210        @SideOnly(Side.CLIENT)
    211        public void renderWithRotation(float par1)
    212        {
    213            if (!this.isHidden)
    214            {
    215                if (this.showModel)
    216                {
    217                    if (!this.compiled)
    218                    {
    219                        this.compileDisplayList(par1);
    220                    }
    221
    222                    GL11.glPushMatrix();
    223                    GL11.glTranslatef(this.rotationPointX * par1, this.rotationPointY * par1, this.rotationPointZ * par1);
    224
    225                    if (this.rotateAngleY != 0.0F)
    226                    {
    227                        GL11.glRotatef(this.rotateAngleY * (180F / (float)Math.PI), 0.0F, 1.0F, 0.0F);
    228                    }
    229
    230                    if (this.rotateAngleX != 0.0F)
    231                    {
    232                        GL11.glRotatef(this.rotateAngleX * (180F / (float)Math.PI), 1.0F, 0.0F, 0.0F);
    233                    }
    234
    235                    if (this.rotateAngleZ != 0.0F)
    236                    {
    237                        GL11.glRotatef(this.rotateAngleZ * (180F / (float)Math.PI), 0.0F, 0.0F, 1.0F);
    238                    }
    239
    240                    GL11.glCallList(this.displayList);
    241                    GL11.glPopMatrix();
    242                }
    243            }
    244        }
    245
    246        /**
    247         * Allows the changing of Angles after a box has been rendered
    248         */
    249        @SideOnly(Side.CLIENT)
    250        public void postRender(float par1)
    251        {
    252            if (!this.isHidden)
    253            {
    254                if (this.showModel)
    255                {
    256                    if (!this.compiled)
    257                    {
    258                        this.compileDisplayList(par1);
    259                    }
    260
    261                    if (this.rotateAngleX == 0.0F && this.rotateAngleY == 0.0F && this.rotateAngleZ == 0.0F)
    262                    {
    263                        if (this.rotationPointX != 0.0F || this.rotationPointY != 0.0F || this.rotationPointZ != 0.0F)
    264                        {
    265                            GL11.glTranslatef(this.rotationPointX * par1, this.rotationPointY * par1, this.rotationPointZ * par1);
    266                        }
    267                    }
    268                    else
    269                    {
    270                        GL11.glTranslatef(this.rotationPointX * par1, this.rotationPointY * par1, this.rotationPointZ * par1);
    271
    272                        if (this.rotateAngleZ != 0.0F)
    273                        {
    274                            GL11.glRotatef(this.rotateAngleZ * (180F / (float)Math.PI), 0.0F, 0.0F, 1.0F);
    275                        }
    276
    277                        if (this.rotateAngleY != 0.0F)
    278                        {
    279                            GL11.glRotatef(this.rotateAngleY * (180F / (float)Math.PI), 0.0F, 1.0F, 0.0F);
    280                        }
    281
    282                        if (this.rotateAngleX != 0.0F)
    283                        {
    284                            GL11.glRotatef(this.rotateAngleX * (180F / (float)Math.PI), 1.0F, 0.0F, 0.0F);
    285                        }
    286                    }
    287                }
    288            }
    289        }
    290
    291        /**
    292         * Compiles a GL display list for this model
    293         */
    294        @SideOnly(Side.CLIENT)
    295        private void compileDisplayList(float par1)
    296        {
    297            this.displayList = GLAllocation.generateDisplayLists(1);
    298            GL11.glNewList(this.displayList, GL11.GL_COMPILE);
    299            Tessellator var2 = Tessellator.instance;
    300            Iterator var3 = this.cubeList.iterator();
    301
    302            while (var3.hasNext())
    303            {
    304                ModelBox var4 = (ModelBox)var3.next();
    305                var4.render(var2, par1);
    306            }
    307
    308            GL11.glEndList();
    309            this.compiled = true;
    310        }
    311
    312        /**
    313         * Returns the model renderer with the new texture parameters.
    314         */
    315        public ModelRenderer setTextureSize(int par1, int par2)
    316        {
    317            this.textureWidth = (float)par1;
    318            this.textureHeight = (float)par2;
    319            return this;
    320        }
    321    }