Mekanism-tilera-Edition/common/mekanism/client/render/tileentity/RenderMechanicalPipe.java

259 lines
7 KiB
Java

package mekanism.client.render.tileentity;
import java.util.HashMap;
import mekanism.client.model.ModelTransmitter;
import mekanism.client.model.ModelTransmitter.Size;
import mekanism.client.render.MekanismRenderer;
import mekanism.client.render.MekanismRenderer.DisplayInteger;
import mekanism.client.render.MekanismRenderer.Model3D;
import mekanism.common.PipeUtils;
import mekanism.common.tileentity.TileEntityMechanicalPipe;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.MekanismUtils.ResourceType;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import org.lwjgl.opengl.GL11;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class RenderMechanicalPipe extends TileEntitySpecialRenderer
{
private ModelTransmitter model = new ModelTransmitter(Size.LARGE);
private HashMap<ForgeDirection, HashMap<Fluid, DisplayInteger[]>> cachedLiquids = new HashMap<ForgeDirection, HashMap<Fluid, DisplayInteger[]>>();
private static final int stages = 100;
private static final double height = 0.45;
private static final double offset = 0.015;
@Override
public void renderTileEntityAt(TileEntity tileEntity, double x, double y, double z, float partialTick)
{
renderAModelAt((TileEntityMechanicalPipe)tileEntity, x, y, z, partialTick);
}
public void renderAModelAt(TileEntityMechanicalPipe tileEntity, double x, double y, double z, float partialTick)
{
bindTexture(MekanismUtils.getResource(ResourceType.RENDER, "MechanicalPipe" + (tileEntity.isActive ? "Active" : "") + ".png"));
GL11.glPushMatrix();
GL11.glTranslatef((float)x + 0.5F, (float)y + 1.5F, (float)z + 0.5F);
GL11.glScalef(1.0F, -1F, -1F);
GL11.glDisable(GL11.GL_CULL_FACE);
boolean[] connectable = PipeUtils.getConnections(tileEntity);
model.renderCenter(connectable);
for(int i = 0; i < 6; i++)
{
model.renderSide(ForgeDirection.getOrientation(i), connectable[i]);
}
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glPopMatrix();
Fluid fluid = tileEntity.getTransmitterNetwork().refFluid;
float scale = tileEntity.getTransmitterNetwork().fluidScale;
if(scale > 0 && fluid != null)
{
push();
MekanismRenderer.glowOn(fluid.getLuminosity());
bindTexture(MekanismRenderer.getBlocksTexture());
GL11.glTranslatef((float)x, (float)y, (float)z);
boolean gas = fluid.isGaseous();
for(int i = 0; i < 6; i++)
{
if(connectable[i])
{
DisplayInteger[] displayLists = getListAndRender(ForgeDirection.getOrientation(i), fluid);
if(displayLists != null)
{
if(!gas)
{
displayLists[Math.max(0, (int)((float)scale*(stages-1)))].render();
}
else {
GL11.glColor4f(1F, 1F, 1F, scale);
displayLists[stages-1].render();
}
}
}
}
DisplayInteger[] displayLists = getListAndRender(ForgeDirection.UNKNOWN, fluid);
if(displayLists != null)
{
if(!gas)
{
displayLists[Math.max(3, (int)((float)scale*(stages-1)))].render();
}
else {
GL11.glColor4f(1F, 1F, 1F, scale);
displayLists[stages-1].render();
}
}
MekanismRenderer.glowOff();
pop();
}
}
private void pop()
{
GL11.glPopAttrib();
GL11.glPopMatrix();
}
private void push()
{
GL11.glPushMatrix();
GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glEnable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
}
private DisplayInteger[] getListAndRender(ForgeDirection side, Fluid fluid)
{
if(side == null || fluid == null || fluid.getIcon() == null)
{
return null;
}
if(cachedLiquids.containsKey(side) && cachedLiquids.get(side).containsKey(fluid))
{
return cachedLiquids.get(side).get(fluid);
}
Model3D toReturn = new Model3D();
toReturn.baseBlock = Block.waterStill;
toReturn.setTexture(fluid.getIcon());
toReturn.setSideRender(side, false);
toReturn.setSideRender(side.getOpposite(), false);
DisplayInteger[] displays = new DisplayInteger[stages];
if(cachedLiquids.containsKey(side))
{
cachedLiquids.get(side).put(fluid, displays);
}
else {
HashMap<Fluid, DisplayInteger[]> map = new HashMap<Fluid, DisplayInteger[]>();
map.put(fluid, displays);
cachedLiquids.put(side, map);
}
MekanismRenderer.colorFluid(fluid);
for(int i = 0; i < stages; i++)
{
displays[i] = DisplayInteger.createAndStart();
switch(side)
{
case UNKNOWN:
{
toReturn.minX = 0.25 + offset;
toReturn.minY = 0.25 + offset;
toReturn.minZ = 0.25 + offset;
toReturn.maxX = 0.75 - offset;
toReturn.maxY = 0.25 + offset + ((float)i / (float)stages)*height;
toReturn.maxZ = 0.75 - offset;
break;
}
case DOWN:
{
toReturn.minX = 0.5 - (((float)i / (float)stages)*height)/2;
toReturn.minY = 0.0;
toReturn.minZ = 0.5 - (((float)i / (float)stages)*height)/2;
toReturn.maxX = 0.5 + (((float)i / (float)stages)*height)/2;
toReturn.maxY = 0.25 + offset;
toReturn.maxZ = 0.5 + (((float)i / (float)stages)*height)/2;
break;
}
case UP:
{
toReturn.minX = 0.5 - (((float)i / (float)stages)*height)/2;
toReturn.minY = 0.25 - offset + ((float)i / (float)stages)*height;
toReturn.minZ = 0.5 - (((float)i / (float)stages)*height)/2;
toReturn.maxX = 0.5 + (((float)i / (float)stages)*height)/2;
toReturn.maxY = 1.0;
toReturn.maxZ = 0.5 + (((float)i / (float)stages)*height)/2;
break;
}
case NORTH:
{
toReturn.minX = 0.25 + offset;
toReturn.minY = 0.25 + offset;
toReturn.minZ = 0.0;
toReturn.maxX = 0.75 - offset;
toReturn.maxY = 0.25 + offset + ((float)i / (float)stages)*height;
toReturn.maxZ = 0.25 + offset;
break;
}
case SOUTH:
{
toReturn.minX = 0.25 + offset;
toReturn.minY = 0.25 + offset;
toReturn.minZ = 0.75 - offset;
toReturn.maxX = 0.75 - offset;
toReturn.maxY = 0.25 + offset + ((float)i / (float)stages)*height;
toReturn.maxZ = 1.0;
break;
}
case WEST:
{
toReturn.minX = 0.0;
toReturn.minY = 0.25 + offset;
toReturn.minZ = 0.25 + offset;
toReturn.maxX = 0.25 + offset;
toReturn.maxY = 0.25 + offset + ((float)i / (float)stages)*height;
toReturn.maxZ = 0.75 - offset;
break;
}
case EAST:
{
toReturn.minX = 0.75 - offset;
toReturn.minY = 0.25 + offset;
toReturn.minZ = 0.25 + offset;
toReturn.maxX = 1.0;
toReturn.maxY = 0.25 + offset + ((float)i / (float)stages)*height;
toReturn.maxZ = 0.75 - offset;
break;
}
}
MekanismRenderer.renderObject(toReturn);
DisplayInteger.endList();
}
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
return displays;
}
}