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