Created
June 25, 2014 17:18
-
-
Save marchermans/ec3e5fede170c94fa2e4 to your computer and use it in GitHub Desktop.
Item Renderer 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.Orion.OrionsBelt.Client.CustomResource; | |
import com.Orion.OrionsBelt.Common.Util.Armor.MultiLayeredArmor; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.client.renderer.ItemRenderer; | |
import net.minecraft.client.renderer.Tessellator; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.util.IIcon; | |
import net.minecraft.util.ResourceLocation; | |
import net.minecraftforge.client.IItemRenderer; | |
import org.lwjgl.opengl.GL11; | |
import org.lwjgl.opengl.GL12; | |
public class RendererItemMultiLayeredArmor implements IItemRenderer { | |
@Override | |
public boolean handleRenderType(ItemStack pItemStack, ItemRenderType pRenderType) { | |
switch (pRenderType) | |
{ | |
case INVENTORY: | |
return true; | |
case ENTITY: | |
return true; | |
case EQUIPPED: | |
return true; | |
case EQUIPPED_FIRST_PERSON: | |
return true; | |
default: | |
return false; | |
} | |
} | |
@Override | |
public boolean shouldUseRenderHelper(ItemRenderType pRenderType, ItemStack pItemStack, ItemRendererHelper pRenderHelper) { | |
return false; | |
} | |
@Override | |
public void renderItem(ItemRenderType pRenderType, ItemStack pItemStack, Object... pRenderData) { | |
switch (pRenderType) | |
{ | |
case INVENTORY: | |
this.renderItemInInventory(pItemStack, pRenderData); | |
break; | |
case ENTITY: | |
this.renderItemAsEntity(pItemStack, pRenderData); | |
break; | |
case EQUIPPED: | |
this.renderItemAsEquipped(pItemStack, pRenderData); | |
break; | |
case EQUIPPED_FIRST_PERSON: | |
this.renderItemInInventory(pItemStack, pRenderData); | |
break; | |
default: | |
return; | |
} | |
} | |
private void renderItemInInventory(ItemStack pItemStack, Object... pRenderData) | |
{ | |
Tessellator tTess = Tessellator.instance; | |
MultiLayeredArmor tArmor = (MultiLayeredArmor) pItemStack.getItem(); | |
int tTotalRenderPasses = tArmor.getRenderPasses(pItemStack); | |
GL11.glPushMatrix(); | |
GL11.glEnable(GL11.GL_BLEND); | |
GL11.glScalef(16F, 16F, 16F); | |
for (int lRenderPass = 0; lRenderPass <= tTotalRenderPasses; lRenderPass++) { | |
tTess.startDrawingQuads(); | |
CustomResource tResource = tArmor.getResource(pItemStack, lRenderPass); | |
//Split the string so that the Icon Location can be modified for the RenderEngine. | |
String tIconLocation = tResource.getIconLocation(); | |
String[] tIconLocations = tIconLocation.split(":"); | |
Minecraft.getMinecraft().renderEngine.bindTexture(new ResourceLocation(tIconLocations[0] + ":" + "textures/items/" + tIconLocations[1] + ".png")); | |
float tRed = tResource.getColor(0) / 255.0F; | |
float tGreen = tResource.getColor(1) / 255.0F; | |
float tBlue = tResource.getColor(2) / 255.0F; | |
tTess.setColorRGBA_F(tRed, tGreen, tBlue, 1.0F); | |
tTess.addVertexWithUV(0, 0, 0, 0, 0);//bottom left texture | |
tTess.addVertexWithUV(0, 1, 0, 0, 1);//top left | |
tTess.addVertexWithUV(1, 1, 0, 1, 1);//top right | |
tTess.addVertexWithUV(1, 0, 0, 1, 0);//bottom right | |
tTess.setColorRGBA_F(1.0F, 1.0F, 1.0F, 1.0F); | |
tTess.draw(); | |
} | |
GL11.glPopMatrix(); | |
} | |
private void renderItemAsEntity(ItemStack pItemStack, Object... pRenderData) | |
{ | |
GL11.glPopMatrix(); // prevents Forge from pre-translating the item | |
this.doRender(pItemStack); | |
GL11.glPushMatrix(); // prevents GL Underflow errors | |
} | |
private void renderItemAsEquipped(ItemStack pItemStack, Object... pRenderData) | |
{ | |
GL11.glPopMatrix(); // prevents Forge from pre-translating the item | |
GL11.glPushMatrix(); | |
float f2 = 3F - (1F/3F); | |
GL11.glRotatef(-20.0F, 0.0F, 0.0F, 1.0F); | |
GL11.glRotatef(90.0F, 1.0F, 0.0F, 0.0F); | |
GL11.glRotatef(-60.0F, 0.0F, 0.0F, 1.0F); | |
GL11.glScalef(f2, f2, f2); | |
GL11.glTranslatef(-0.25F, -0.1875F, 0.1875F); | |
float f3 = 0.625F; | |
GL11.glTranslatef(0.0F, 0.125F, 0.3125F); | |
GL11.glRotatef(-20.0F, 0.0F, 1.0F, 0.0F); | |
GL11.glScalef(f3, -f3, f3); | |
GL11.glRotatef(-100.0F, 1.0F, 0.0F, 0.0F); | |
GL11.glRotatef(45.0F, 0.0F, 1.0F, 0.0F); | |
this.doRender(pItemStack); | |
GL11.glPopMatrix(); | |
GL11.glPushMatrix(); // prevents GL Underflow error | |
} | |
private void doRender(ItemStack pItemStack) | |
{ | |
int tRenderPasses = ((MultiLayeredArmor) pItemStack.getItem()).getRenderPasses(pItemStack); | |
GL11.glEnable(GL11.GL_BLEND); | |
for(int tIter = 0; tIter <= tRenderPasses; tIter++) { | |
IIcon icon = ((MultiLayeredArmor) pItemStack.getItem()).getIcon(pItemStack, tIter); | |
if (icon == null) { | |
GL11.glPopMatrix(); | |
return; | |
} | |
CustomResource tResource = ((MultiLayeredArmor) pItemStack.getItem()).getResource(pItemStack, tIter); | |
Minecraft.getMinecraft().getTextureManager().bindTexture(new ResourceLocation(tResource.getIconLocation())); | |
Tessellator tessellator = Tessellator.instance; | |
float f = icon.getMinU(); | |
float f1 = icon.getMaxU(); | |
float f2 = icon.getMinV(); | |
float f3 = icon.getMaxV(); | |
float f4 = 0.0F; | |
float f5 = 0.3F; | |
GL11.glEnable(GL12.GL_RESCALE_NORMAL); | |
GL11.glTranslatef(-f4, -f5, 0.0F); | |
float f6 = 1.5F; | |
GL11.glScalef(f6, f6, f6); | |
GL11.glRotatef(50.0F, 0.0F, 1.0F, 0.0F); | |
GL11.glRotatef(335.0F, 0.0F, 0.0F, 1.0F); | |
GL11.glTranslatef(-0.9375F, -0.0625F, 0.0F); | |
this.renderItemIn2D(tessellator, f1, f2, f, f3, icon.getIconWidth(), icon.getIconHeight(), 0.0625F, tResource.getColor(0), tResource.getColor(1), tResource.getColor(2)); | |
GL11.glDisable(GL12.GL_RESCALE_NORMAL); | |
} | |
GL11.glDisable(GL11.GL_BLEND); | |
} | |
//Custom modified version of the ItemRenderer.renderItemIn2D. | |
//Used to allow drawing of the colors EQUIPPED mode | |
private static void renderItemIn2D(Tessellator pTessellator, float pIconMinU, float pIconMaxU, float pIconMinV, float pIconMaxV, int pIconWidth, int pIconHeight, float par7, int pColorRed, int pColorGreen, int pColorBlue) | |
{ | |
pTessellator.startDrawingQuads(); | |
pTessellator.setColorRGBA(pColorRed, pColorGreen, pColorBlue, 255); | |
pTessellator.setNormal(0.0F, 0.0F, 1.0F); | |
pTessellator.addVertexWithUV(0.0D, 0.0D, 0.0D, (double)pIconMinU, (double)pIconMaxV); | |
pTessellator.addVertexWithUV(1.0D, 0.0D, 0.0D, (double)pIconMinV, (double)pIconMaxV); | |
pTessellator.addVertexWithUV(1.0D, 1.0D, 0.0D, (double)pIconMinV, (double)pIconMaxU); | |
pTessellator.addVertexWithUV(0.0D, 1.0D, 0.0D, (double) pIconMinU, (double) pIconMaxU); | |
pTessellator.draw(); | |
pTessellator.startDrawingQuads(); | |
pTessellator.setColorRGBA(pColorRed, pColorGreen, pColorBlue, 255); | |
pTessellator.setNormal(0.0F, 0.0F, -1.0F); | |
pTessellator.addVertexWithUV(0.0D, 1.0D, (double) (0.0F - par7), (double) pIconMinU, (double) pIconMaxU); | |
pTessellator.addVertexWithUV(1.0D, 1.0D, (double) (0.0F - par7), (double) pIconMinV, (double) pIconMaxU); | |
pTessellator.addVertexWithUV(1.0D, 0.0D, (double)(0.0F - par7), (double)pIconMinV, (double)pIconMaxV); | |
pTessellator.addVertexWithUV(0.0D, 0.0D, (double)(0.0F - par7), (double)pIconMinU, (double)pIconMaxV); | |
pTessellator.draw(); | |
float f5 = 0.5F * (pIconMinU - pIconMinV) / (float)pIconWidth; | |
float f6 = 0.5F * (pIconMaxV - pIconMaxU) / (float)pIconHeight; | |
pTessellator.startDrawingQuads(); | |
pTessellator.setColorRGBA(pColorRed, pColorGreen, pColorBlue, 255); | |
pTessellator.setNormal(-1.0F, 0.0F, 0.0F); | |
int k; | |
float f7; | |
float f8; | |
for (k = 0; k < pIconWidth; ++k) | |
{ | |
f7 = (float)k / (float)pIconWidth; | |
f8 = pIconMinU + (pIconMinV - pIconMinU) * f7 - f5; | |
pTessellator.addVertexWithUV((double)f7, 0.0D, (double)(0.0F - par7), (double)f8, (double)pIconMaxV); | |
pTessellator.addVertexWithUV((double)f7, 0.0D, 0.0D, (double)f8, (double)pIconMaxV); | |
pTessellator.addVertexWithUV((double)f7, 1.0D, 0.0D, (double)f8, (double)pIconMaxU); | |
pTessellator.addVertexWithUV((double)f7, 1.0D, (double)(0.0F - par7), (double)f8, (double)pIconMaxU); | |
} | |
pTessellator.draw(); | |
pTessellator.startDrawingQuads(); | |
pTessellator.setColorRGBA(pColorRed, pColorGreen, pColorBlue, 255); | |
pTessellator.setNormal(1.0F, 0.0F, 0.0F); | |
float f9; | |
for (k = 0; k < pIconWidth; ++k) | |
{ | |
f7 = (float)k / (float)pIconWidth; | |
f8 = pIconMinU + (pIconMinV - pIconMinU) * f7 - f5; | |
f9 = f7 + 1.0F / (float)pIconWidth; | |
pTessellator.addVertexWithUV((double)f9, 1.0D, (double)(0.0F - par7), (double)f8, (double)pIconMaxU); | |
pTessellator.addVertexWithUV((double)f9, 1.0D, 0.0D, (double)f8, (double)pIconMaxU); | |
pTessellator.addVertexWithUV((double)f9, 0.0D, 0.0D, (double)f8, (double)pIconMaxV); | |
pTessellator.addVertexWithUV((double)f9, 0.0D, (double)(0.0F - par7), (double)f8, (double)pIconMaxV); | |
} | |
pTessellator.draw(); | |
pTessellator.startDrawingQuads(); | |
pTessellator.setColorRGBA(pColorRed, pColorGreen, pColorBlue, 255); | |
pTessellator.setNormal(0.0F, 1.0F, 0.0F); | |
for (k = 0; k < pIconHeight; ++k) | |
{ | |
f7 = (float)k / (float)pIconHeight; | |
f8 = pIconMaxV + (pIconMaxU - pIconMaxV) * f7 - f6; | |
f9 = f7 + 1.0F / (float)pIconHeight; | |
pTessellator.addVertexWithUV(0.0D, (double)f9, 0.0D, (double)pIconMinU, (double)f8); | |
pTessellator.addVertexWithUV(1.0D, (double)f9, 0.0D, (double)pIconMinV, (double)f8); | |
pTessellator.addVertexWithUV(1.0D, (double)f9, (double)(0.0F - par7), (double)pIconMinV, (double)f8); | |
pTessellator.addVertexWithUV(0.0D, (double)f9, (double)(0.0F - par7), (double)pIconMinU, (double)f8); | |
} | |
pTessellator.draw(); | |
pTessellator.startDrawingQuads(); | |
pTessellator.setColorRGBA(pColorRed, pColorGreen, pColorBlue, 255); | |
pTessellator.setNormal(0.0F, -1.0F, 0.0F); | |
for (k = 0; k < pIconHeight; ++k) | |
{ | |
f7 = (float)k / (float)pIconHeight; | |
f8 = pIconMaxV + (pIconMaxU - pIconMaxV) * f7 - f6; | |
pTessellator.addVertexWithUV(1.0D, (double)f7, 0.0D, (double)pIconMinV, (double)f8); | |
pTessellator.addVertexWithUV(0.0D, (double)f7, 0.0D, (double)pIconMinU, (double)f8); | |
pTessellator.addVertexWithUV(0.0D, (double)f7, (double)(0.0F - par7), (double)pIconMinU, (double)f8); | |
pTessellator.addVertexWithUV(1.0D, (double)f7, (double)(0.0F - par7), (double)pIconMinV, (double)f8); | |
} | |
pTessellator.draw(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment