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    @SideOnly(Side.CLIENT)
011    public class ModelRenderer
012    {
013        /** The size of the texture file's width in pixels. */
014        public float textureWidth;
015    
016        /** The size of the texture file's height in pixels. */
017        public float textureHeight;
018    
019        /** The X offset into the texture used for displaying this model */
020        private int textureOffsetX;
021    
022        /** The Y offset into the texture used for displaying this model */
023        private int textureOffsetY;
024        public float rotationPointX;
025        public float rotationPointY;
026        public float rotationPointZ;
027        public float rotateAngleX;
028        public float rotateAngleY;
029        public float rotateAngleZ;
030        private boolean compiled;
031    
032        /** The GL display list rendered by the Tessellator for this model */
033        private int displayList;
034        public boolean mirror;
035        public boolean showModel;
036    
037        /** Hides the model. */
038        public boolean isHidden;
039        public List cubeList;
040        public List childModels;
041        public final String boxName;
042        private ModelBase baseModel;
043    
044        public ModelRenderer(ModelBase par1ModelBase, String par2Str)
045        {
046            this.textureWidth = 64.0F;
047            this.textureHeight = 32.0F;
048            this.compiled = false;
049            this.displayList = 0;
050            this.mirror = false;
051            this.showModel = true;
052            this.isHidden = false;
053            this.cubeList = new ArrayList();
054            this.baseModel = par1ModelBase;
055            par1ModelBase.boxList.add(this);
056            this.boxName = par2Str;
057            this.setTextureSize(par1ModelBase.textureWidth, par1ModelBase.textureHeight);
058        }
059    
060        public ModelRenderer(ModelBase par1ModelBase)
061        {
062            this(par1ModelBase, (String)null);
063        }
064    
065        public ModelRenderer(ModelBase par1ModelBase, int par2, int par3)
066        {
067            this(par1ModelBase);
068            this.setTextureOffset(par2, par3);
069        }
070    
071        /**
072         * Sets the current box's rotation points and rotation angles to another box.
073         */
074        public void addChild(ModelRenderer par1ModelRenderer)
075        {
076            if (this.childModels == null)
077            {
078                this.childModels = new ArrayList();
079            }
080    
081            this.childModels.add(par1ModelRenderer);
082        }
083    
084        public ModelRenderer setTextureOffset(int par1, int par2)
085        {
086            this.textureOffsetX = par1;
087            this.textureOffsetY = par2;
088            return this;
089        }
090    
091        public ModelRenderer addBox(String par1Str, float par2, float par3, float par4, int par5, int par6, int par7)
092        {
093            par1Str = this.boxName + "." + par1Str;
094            TextureOffset var8 = this.baseModel.getTextureOffset(par1Str);
095            this.setTextureOffset(var8.textureOffsetX, var8.textureOffsetY);
096            this.cubeList.add((new ModelBox(this, this.textureOffsetX, this.textureOffsetY, par2, par3, par4, par5, par6, par7, 0.0F)).func_78244_a(par1Str));
097            return this;
098        }
099    
100        public ModelRenderer addBox(float par1, float par2, float par3, int par4, int par5, int par6)
101        {
102            this.cubeList.add(new ModelBox(this, this.textureOffsetX, this.textureOffsetY, par1, par2, par3, par4, par5, par6, 0.0F));
103            return this;
104        }
105    
106        /**
107         * Creates a textured box. Args: originX, originY, originZ, width, height, depth, scaleFactor.
108         */
109        public void addBox(float par1, float par2, float par3, int par4, int par5, int par6, float par7)
110        {
111            this.cubeList.add(new ModelBox(this, this.textureOffsetX, this.textureOffsetY, par1, par2, par3, par4, par5, par6, par7));
112        }
113    
114        public void setRotationPoint(float par1, float par2, float par3)
115        {
116            this.rotationPointX = par1;
117            this.rotationPointY = par2;
118            this.rotationPointZ = par3;
119        }
120    
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        public void renderWithRotation(float par1)
211        {
212            if (!this.isHidden)
213            {
214                if (this.showModel)
215                {
216                    if (!this.compiled)
217                    {
218                        this.compileDisplayList(par1);
219                    }
220    
221                    GL11.glPushMatrix();
222                    GL11.glTranslatef(this.rotationPointX * par1, this.rotationPointY * par1, this.rotationPointZ * par1);
223    
224                    if (this.rotateAngleY != 0.0F)
225                    {
226                        GL11.glRotatef(this.rotateAngleY * (180F / (float)Math.PI), 0.0F, 1.0F, 0.0F);
227                    }
228    
229                    if (this.rotateAngleX != 0.0F)
230                    {
231                        GL11.glRotatef(this.rotateAngleX * (180F / (float)Math.PI), 1.0F, 0.0F, 0.0F);
232                    }
233    
234                    if (this.rotateAngleZ != 0.0F)
235                    {
236                        GL11.glRotatef(this.rotateAngleZ * (180F / (float)Math.PI), 0.0F, 0.0F, 1.0F);
237                    }
238    
239                    GL11.glCallList(this.displayList);
240                    GL11.glPopMatrix();
241                }
242            }
243        }
244    
245        /**
246         * Allows the changing of Angles after a box has been rendered
247         */
248        public void postRender(float par1)
249        {
250            if (!this.isHidden)
251            {
252                if (this.showModel)
253                {
254                    if (!this.compiled)
255                    {
256                        this.compileDisplayList(par1);
257                    }
258    
259                    if (this.rotateAngleX == 0.0F && this.rotateAngleY == 0.0F && this.rotateAngleZ == 0.0F)
260                    {
261                        if (this.rotationPointX != 0.0F || this.rotationPointY != 0.0F || this.rotationPointZ != 0.0F)
262                        {
263                            GL11.glTranslatef(this.rotationPointX * par1, this.rotationPointY * par1, this.rotationPointZ * par1);
264                        }
265                    }
266                    else
267                    {
268                        GL11.glTranslatef(this.rotationPointX * par1, this.rotationPointY * par1, this.rotationPointZ * par1);
269    
270                        if (this.rotateAngleZ != 0.0F)
271                        {
272                            GL11.glRotatef(this.rotateAngleZ * (180F / (float)Math.PI), 0.0F, 0.0F, 1.0F);
273                        }
274    
275                        if (this.rotateAngleY != 0.0F)
276                        {
277                            GL11.glRotatef(this.rotateAngleY * (180F / (float)Math.PI), 0.0F, 1.0F, 0.0F);
278                        }
279    
280                        if (this.rotateAngleX != 0.0F)
281                        {
282                            GL11.glRotatef(this.rotateAngleX * (180F / (float)Math.PI), 1.0F, 0.0F, 0.0F);
283                        }
284                    }
285                }
286            }
287        }
288    
289        /**
290         * Compiles a GL display list for this model
291         */
292        private void compileDisplayList(float par1)
293        {
294            this.displayList = GLAllocation.generateDisplayLists(1);
295            GL11.glNewList(this.displayList, GL11.GL_COMPILE);
296            Tessellator var2 = Tessellator.instance;
297            Iterator var3 = this.cubeList.iterator();
298    
299            while (var3.hasNext())
300            {
301                ModelBox var4 = (ModelBox)var3.next();
302                var4.render(var2, par1);
303            }
304    
305            GL11.glEndList();
306            this.compiled = true;
307        }
308    
309        /**
310         * Returns the model renderer with the new texture parameters.
311         */
312        public ModelRenderer setTextureSize(int par1, int par2)
313        {
314            this.textureWidth = (float)par1;
315            this.textureHeight = (float)par2;
316            return this;
317        }
318    }