Files
TinkersConstruct/src/main/java/tconstruct/client/ToolCoreRenderer.java
T

318 lines
11 KiB
Java
Raw Normal View History

2013-08-11 12:23:49 -07:00
package tconstruct.client;
import net.minecraft.client.Minecraft;
2014-09-26 17:15:33 +02:00
import net.minecraft.client.renderer.ItemRenderer;
2013-08-11 12:23:49 -07:00
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.renderer.texture.TextureUtil;
2013-08-11 12:23:49 -07:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
2013-12-31 11:26:08 -05:00
import net.minecraft.util.IIcon;
2013-08-11 12:23:49 -07:00
import net.minecraftforge.client.IItemRenderer;
2014-09-09 18:30:15 +10:00
import org.lwjgl.opengl.*;
import tconstruct.TConstruct;
2013-08-11 12:23:49 -07:00
import tconstruct.library.tools.ToolCore;
public class ToolCoreRenderer implements IItemRenderer
{
2014-08-28 12:49:57 +02:00
private final boolean isEntity;
private final boolean noEntityTranslation;
2014-09-09 18:30:15 +10:00
public ToolCoreRenderer()
{
this(false, false);
}
2014-08-28 12:49:57 +02:00
2014-09-09 18:30:15 +10:00
public ToolCoreRenderer(boolean isEntity)
{
this(isEntity, false);
}
2014-09-09 18:30:15 +10:00
public ToolCoreRenderer(boolean isEntity, boolean noEntityTranslation)
{
2014-08-28 12:49:57 +02:00
this.isEntity = isEntity;
this.noEntityTranslation = noEntityTranslation;
2014-08-28 12:49:57 +02:00
}
2013-08-11 12:23:49 -07:00
@Override
public boolean handleRenderType (ItemStack item, ItemRenderType type)
{
2014-09-09 18:30:15 +10:00
if (!item.hasTagCompound())
return false;
2013-08-11 12:23:49 -07:00
switch (type)
{
case ENTITY:
2013-08-21 15:46:14 -07:00
return true;
2013-08-11 12:23:49 -07:00
case EQUIPPED:
2013-08-31 05:13:31 -07:00
GL11.glTranslatef(0.03f, 0F, -0.09375F);
2013-08-11 12:23:49 -07:00
case EQUIPPED_FIRST_PERSON:
2014-08-28 12:49:57 +02:00
return !isEntity;
2013-08-11 12:23:49 -07:00
case INVENTORY:
return true;
default:
2014-01-08 00:45:09 -05:00
TConstruct.logger.warn("[TCon] Unhandled render case!");
2013-08-11 12:23:49 -07:00
case FIRST_PERSON_MAP:
return false;
}
}
@Override
public boolean shouldUseRenderHelper (ItemRenderType type, ItemStack item, ItemRendererHelper helper)
{
return handleRenderType(item, type) & helper.ordinal() < ItemRendererHelper.EQUIPPED_BLOCK.ordinal();
}
private static final int toolIcons = 10;
@Override
public void renderItem (ItemRenderType type, ItemStack item, Object... data)
{
ToolCore tool = (ToolCore) item.getItem();
boolean isInventory = type == ItemRenderType.INVENTORY;
Entity ent = null;
if (data.length > 1)
ent = (Entity) data[1];
2014-08-28 14:30:49 +02:00
int iconParts = toolIcons;//tool.getRenderPasses(item.getItemDamage());
// TODO: have the tools define how many render passes they have
// (requires more logic rewrite than it sounds like)
2013-08-11 12:23:49 -07:00
2013-12-31 11:26:08 -05:00
IIcon[] tempParts = new IIcon[iconParts];
2013-08-11 12:23:49 -07:00
label:
{
if (!isInventory && ent instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) ent;
ItemStack itemInUse = player.getItemInUse();
if (itemInUse != null)
{
int useCount = player.getItemInUseCount();
for (int i = iconParts; i-- > 0;)
tempParts[i] = tool.getIcon(item, i, player, itemInUse, useCount);
break label;
}
}
for (int i = iconParts; i-- > 0;)
2014-01-08 00:45:09 -05:00
tempParts[i] = tool.getIcon(item, i);
2013-08-11 12:23:49 -07:00
}
int count = 0;
2013-12-31 11:26:08 -05:00
IIcon[] parts = new IIcon[iconParts];
2013-08-11 12:23:49 -07:00
for (int i = 0; i < iconParts; ++i)
{
2013-12-31 11:26:08 -05:00
IIcon part = tempParts[i];
2014-08-28 12:49:57 +02:00
if (part == null || part == ToolCore.blankSprite || part == ToolCore.emptyIcon)
2013-08-11 12:23:49 -07:00
++count;
else
parts[i - count] = part;
}
iconParts -= count;
if (iconParts <= 0)
{
iconParts = 1;
// TODO: assign default sprite
// parts = new Icon[]{ defaultSprite };
}
Tessellator tess = Tessellator.instance;
float[] xMax = new float[iconParts];
float[] yMin = new float[iconParts];
float[] xMin = new float[iconParts];
float[] yMax = new float[iconParts];
float depth = 1f / 16f;
float[] width = new float[iconParts];
float[] height = new float[iconParts];
float[] xDiff = new float[iconParts];
float[] yDiff = new float[iconParts];
2013-08-13 13:41:46 -04:00
float[] xSub = new float[iconParts];
float[] ySub = new float[iconParts];
2013-08-11 12:23:49 -07:00
for (int i = 0; i < iconParts; ++i)
{
2013-12-31 11:26:08 -05:00
IIcon icon = parts[i];
2013-08-11 12:23:49 -07:00
xMin[i] = icon.getMinU();
xMax[i] = icon.getMaxU();
yMin[i] = icon.getMinV();
yMax[i] = icon.getMaxV();
2014-08-28 14:30:49 +02:00
width[i] = icon.getIconWidth();
height[i] = icon.getIconHeight();
2013-08-11 12:23:49 -07:00
xDiff[i] = xMin[i] - xMax[i];
yDiff[i] = yMin[i] - yMax[i];
2014-08-28 14:30:49 +02:00
xSub[i] = 0.5f * (xMax[i] - xMin[i]) / width[i];
ySub[i] = 0.5f * (yMax[i] - yMin[i]) / height[i];
2013-08-11 12:23:49 -07:00
}
GL11.glPushMatrix();
// color
2014-09-26 17:15:33 +02:00
int[] color = new int[iconParts];
for(int i = 0; i < iconParts; i++)
color[i] = item.getItem().getColorFromItemStack(item, i);
GL11.glEnable(GL12.GL_RESCALE_NORMAL);
2013-08-11 12:23:49 -07:00
if (type == ItemRenderType.INVENTORY)
{
2014-09-26 17:15:33 +02:00
TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager();
texturemanager.getResourceLocation(item.getItemSpriteNumber());
TextureUtil.func_152777_a(false, false, 1.0F);
2013-08-11 12:23:49 -07:00
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_ALPHA_TEST);
GL11.glAlphaFunc(GL11.GL_GREATER, 0.5F);
GL11.glDisable(GL11.GL_BLEND);
2014-09-26 17:15:33 +02:00
2013-08-11 12:23:49 -07:00
tess.startDrawingQuads();
for (int i = 0; i < iconParts; ++i)
{
2014-09-26 17:15:33 +02:00
tess.setColorOpaque_I(color[i]);
2013-08-11 12:23:49 -07:00
tess.addVertexWithUV(0, 16, 0, xMin[i], yMax[i]);
tess.addVertexWithUV(16, 16, 0, xMax[i], yMax[i]);
tess.addVertexWithUV(16, 0, 0, xMax[i], yMin[i]);
tess.addVertexWithUV(0, 0, 0, xMin[i], yMin[i]);
}
tess.draw();
GL11.glEnable(GL11.GL_LIGHTING);
2014-09-26 17:15:33 +02:00
GL11.glDisable(GL11.GL_ALPHA_TEST);
2014-09-26 17:15:33 +02:00
//GL11.glDisable(GL12.GL_RESCALE_NORMAL);
texturemanager.bindTexture(texturemanager.getResourceLocation(item.getItemSpriteNumber()));
TextureUtil.func_147945_b();
2013-08-11 12:23:49 -07:00
}
else
{
switch (type)
{
case EQUIPPED_FIRST_PERSON:
break;
case EQUIPPED:
GL11.glTranslatef(0, -4 / 16f, 0);
break;
case ENTITY:
2014-09-09 18:30:15 +10:00
if (!noEntityTranslation)
GL11.glTranslatef(-0.5f, 0f, depth); // correction of the rotation point when items lie on the ground
2013-08-11 12:23:49 -07:00
break;
default:
}
// one side
2013-08-11 12:23:49 -07:00
tess.startDrawingQuads();
tess.setNormal(0, 0, 1);
for (int i = 0; i < iconParts; ++i)
{
2014-09-26 17:15:33 +02:00
tess.setColorOpaque_I(color[i]);
2013-08-11 12:23:49 -07:00
tess.addVertexWithUV(0, 0, 0, xMax[i], yMax[i]);
tess.addVertexWithUV(1, 0, 0, xMin[i], yMax[i]);
tess.addVertexWithUV(1, 1, 0, xMin[i], yMin[i]);
tess.addVertexWithUV(0, 1, 0, xMax[i], yMin[i]);
}
tess.draw();
// other side
2013-08-11 12:23:49 -07:00
tess.startDrawingQuads();
tess.setNormal(0, 0, -1);
for (int i = 0; i < iconParts; ++i)
{
2014-09-26 17:15:33 +02:00
tess.setColorOpaque_I(color[i]);
2013-08-11 12:23:49 -07:00
tess.addVertexWithUV(0, 1, -depth, xMax[i], yMin[i]);
tess.addVertexWithUV(1, 1, -depth, xMin[i], yMin[i]);
tess.addVertexWithUV(1, 0, -depth, xMin[i], yMax[i]);
tess.addVertexWithUV(0, 0, -depth, xMax[i], yMax[i]);
}
tess.draw();
// make it have "depth"
2013-08-11 12:23:49 -07:00
tess.startDrawingQuads();
tess.setNormal(-1, 0, 0);
float pos;
float iconPos;
for (int i = 0; i < iconParts; ++i)
{
2014-09-26 17:15:33 +02:00
tess.setColorOpaque_I(color[i]);
2014-09-09 18:30:15 +10:00
float w = width[i], m = xMax[i], d = xDiff[i], s = xSub[i];
for (int k = 0, e = (int) w; k < e; ++k)
2013-08-11 12:23:49 -07:00
{
2014-08-28 14:30:49 +02:00
pos = k / w;
iconPos = m + d * pos - s;
2013-08-11 12:23:49 -07:00
tess.addVertexWithUV(pos, 0, -depth, iconPos, yMax[i]);
2014-09-09 18:30:15 +10:00
tess.addVertexWithUV(pos, 0, 0, iconPos, yMax[i]);
tess.addVertexWithUV(pos, 1, 0, iconPos, yMin[i]);
2013-08-11 12:23:49 -07:00
tess.addVertexWithUV(pos, 1, -depth, iconPos, yMin[i]);
}
}
tess.draw();
tess.startDrawingQuads();
tess.setNormal(1, 0, 0);
float posEnd;
for (int i = 0; i < iconParts; ++i)
{
2014-09-26 17:15:33 +02:00
tess.setColorOpaque_I(color[i]);
2014-09-09 18:30:15 +10:00
float w = width[i], m = xMax[i], d = xDiff[i], s = xSub[i];
float d2 = 1f / w;
for (int k = 0, e = (int) w; k < e; ++k)
2013-08-11 12:23:49 -07:00
{
2014-08-28 14:30:49 +02:00
pos = k / w;
iconPos = m + d * pos - s;
posEnd = pos + d2;
2013-08-11 12:23:49 -07:00
tess.addVertexWithUV(posEnd, 1, -depth, iconPos, yMin[i]);
2014-09-09 18:30:15 +10:00
tess.addVertexWithUV(posEnd, 1, 0, iconPos, yMin[i]);
tess.addVertexWithUV(posEnd, 0, 0, iconPos, yMax[i]);
2013-08-11 12:23:49 -07:00
tess.addVertexWithUV(posEnd, 0, -depth, iconPos, yMax[i]);
}
}
tess.draw();
tess.startDrawingQuads();
tess.setNormal(0, 1, 0);
for (int i = 0; i < iconParts; ++i)
{
2014-09-26 17:15:33 +02:00
tess.setColorOpaque_I(color[i]);
2014-09-09 18:30:15 +10:00
float h = height[i], m = yMax[i], d = yDiff[i], s = ySub[i];
float d2 = 1f / h;
for (int k = 0, e = (int) h; k < e; ++k)
2013-08-11 12:23:49 -07:00
{
2014-08-28 14:30:49 +02:00
pos = k / h;
iconPos = m + d * pos - s;
posEnd = pos + d2;
2014-09-09 18:30:15 +10:00
tess.addVertexWithUV(0, posEnd, 0, xMax[i], iconPos);
tess.addVertexWithUV(1, posEnd, 0, xMin[i], iconPos);
2013-08-11 12:23:49 -07:00
tess.addVertexWithUV(1, posEnd, -depth, xMin[i], iconPos);
tess.addVertexWithUV(0, posEnd, -depth, xMax[i], iconPos);
}
}
tess.draw();
tess.startDrawingQuads();
tess.setNormal(0, -1, 0);
for (int i = 0; i < iconParts; ++i)
{
2014-09-26 17:15:33 +02:00
tess.setColorOpaque_I(color[i]);
2014-09-09 18:30:15 +10:00
float h = height[i], m = yMax[i], d = yDiff[i], s = ySub[i];
for (int k = 0, e = (int) h; k < e; ++k)
2013-08-11 12:23:49 -07:00
{
2014-08-28 14:30:49 +02:00
pos = k / h;
iconPos = m + d * pos - s;
2014-09-09 18:30:15 +10:00
tess.addVertexWithUV(1, pos, 0, xMin[i], iconPos);
tess.addVertexWithUV(0, pos, 0, xMax[i], iconPos);
2013-08-11 12:23:49 -07:00
tess.addVertexWithUV(0, pos, -depth, xMax[i], iconPos);
tess.addVertexWithUV(1, pos, -depth, xMin[i], iconPos);
}
}
tess.draw();
GL11.glDisable(GL12.GL_RESCALE_NORMAL);
}
GL11.glPopMatrix();
}
}