Mekanism-tilera-Edition/common/mekanism/client/render/RenderPartTransmitter.java

406 lines
11 KiB
Java
Raw Normal View History

2013-10-03 01:10:20 +02:00
package mekanism.client.render;
import java.util.HashMap;
import java.util.Map;
import mekanism.client.render.MekanismRenderer.DisplayInteger;
import mekanism.client.render.MekanismRenderer.Model3D;
import mekanism.common.EnergyNetwork;
import mekanism.common.multipart.PartLogisticalTransporter;
import mekanism.common.multipart.PartMechanicalPipe;
import mekanism.common.multipart.PartPressurizedTube;
import mekanism.common.multipart.PartSidedPipe;
import mekanism.common.multipart.PartTransmitter;
import mekanism.common.multipart.PartUniversalCable;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.MekanismUtils.ResourceType;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.util.Icon;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
2013-10-03 01:10:20 +02:00
import org.lwjgl.opengl.GL11;
import codechicken.lib.colour.Colour;
import codechicken.lib.colour.ColourRGBA;
import codechicken.lib.lighting.LightModel;
import codechicken.lib.render.CCModel;
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.ColourMultiplier;
import codechicken.lib.render.IconTransformation;
import codechicken.lib.render.TextureUtils;
import codechicken.lib.render.TextureUtils.IIconRegister;
import codechicken.lib.vec.Translation;
import codechicken.lib.vec.Vector3;
public class RenderPartTransmitter implements IIconRegister
{
public static RenderPartTransmitter INSTANCE;
2013-12-03 14:42:13 +01:00
public static Map<String, CCModel> small_models;
public static Map<String, CCModel> large_models;
public static Map<String, CCModel> contents_models;
2013-10-03 01:10:20 +02:00
private static final int stages = 100;
2013-12-03 16:54:50 +01:00
private static final double height = 0.45;
2013-10-03 01:10:20 +02:00
private static final double offset = 0.015;
2013-12-03 17:24:23 +01:00
2013-10-03 01:10:20 +02:00
private HashMap<ForgeDirection, HashMap<Fluid, DisplayInteger[]>> cachedLiquids = new HashMap<ForgeDirection, HashMap<Fluid, DisplayInteger[]>>();
public static RenderPartTransmitter getInstance()
{
return INSTANCE;
}
2013-10-03 01:10:20 +02:00
public static void init()
2013-10-03 01:10:20 +02:00
{
INSTANCE = new RenderPartTransmitter();
TextureUtils.addIconRegistrar(INSTANCE);
small_models = CCModel.parseObjModels(MekanismUtils.getResource(ResourceType.MODEL, "transmitter_small.obj"), 7, null);
for(CCModel c : small_models.values())
2013-12-03 16:54:50 +01:00
{
2013-10-03 01:10:20 +02:00
c.apply(new Translation(.5, .5, .5));
c.computeLighting(LightModel.standardLightModel);
c.shrinkUVs(0.0005);
}
large_models = CCModel.parseObjModels(MekanismUtils.getResource(ResourceType.MODEL, "transmitter_large.obj"), 7, null);
for(CCModel c : large_models.values())
{
c.apply(new Translation(.5, .5, .5));
c.computeLighting(LightModel.standardLightModel);
c.shrinkUVs(0.0005);
}
contents_models = CCModel.parseObjModels(MekanismUtils.getResource(ResourceType.MODEL, "transmitter_contents.obj"), 7, null);
2013-12-03 16:54:50 +01:00
for(CCModel c : contents_models.values())
2013-12-03 16:54:50 +01:00
{
2013-10-03 01:10:20 +02:00
c.apply(new Translation(.5, .5, .5));
c.computeLighting(LightModel.standardLightModel);
c.shrinkUVs(0.0005);
}
}
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 void pop()
{
GL11.glPopAttrib();
GL11.glPopMatrix();
}
public void renderContents(PartUniversalCable cable, Vector3 pos)
{
2013-12-13 04:31:27 +01:00
if(cable.currentPower == 0)
2013-12-03 16:54:50 +01:00
{
2013-10-03 01:10:20 +02:00
return;
2013-12-03 16:54:50 +01:00
}
2013-10-03 01:10:20 +02:00
GL11.glPushMatrix();
CCRenderState.reset();
CCRenderState.useNormals(true);
CCRenderState.useModelColours(true);
CCRenderState.startDrawing(7);
GL11.glTranslated(pos.x, pos.y, pos.z);
2013-12-03 16:54:50 +01:00
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
2013-10-03 01:10:20 +02:00
{
renderEnergySide(side, cable);
}
2013-12-03 16:54:50 +01:00
MekanismRenderer.glowOn();
2013-10-03 01:10:20 +02:00
CCRenderState.draw();
2013-12-03 16:54:50 +01:00
MekanismRenderer.glowOff();
2013-10-03 01:10:20 +02:00
GL11.glPopMatrix();
}
public void renderContents(PartMechanicalPipe pipe, Vector3 pos)
{
float targetScale = pipe.getTransmitterNetwork().fluidScale;
if(Math.abs(pipe.currentScale - targetScale) > 0.01)
{
2013-12-18 18:39:37 +01:00
pipe.currentScale = (12 * pipe.currentScale + targetScale) / 13;
}
2013-12-18 17:19:30 +01:00
else {
pipe.currentScale = targetScale;
}
2013-12-03 17:24:23 +01:00
Fluid fluid = pipe.getTransmitterNetwork().refFluid;
2013-12-17 21:36:25 +01:00
float scale = pipe.currentScale;
2013-12-03 17:24:23 +01:00
2013-12-18 00:20:18 +01:00
if(scale > 0.01 && fluid != null)
2013-12-03 17:24:23 +01:00
{
2013-10-03 01:10:20 +02:00
push();
2013-12-03 16:54:50 +01:00
MekanismRenderer.glowOn(fluid.getLuminosity());
2013-10-03 01:10:20 +02:00
CCRenderState.changeTexture((MekanismRenderer.getBlocksTexture()));
2013-10-03 01:10:20 +02:00
GL11.glTranslated(pos.x, pos.y, pos.z);
2013-12-03 17:24:23 +01:00
boolean gas = fluid.isGaseous();
2013-12-03 16:54:50 +01:00
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
2013-10-03 01:10:20 +02:00
{
if(PartTransmitter.connectionMapContainsSide(pipe.getAllCurrentConnections(), side))
{
2013-12-03 16:54:50 +01:00
DisplayInteger[] displayLists = getListAndRender(side, fluid);
2013-10-03 01:10:20 +02:00
if(displayLists != null)
{
2013-12-03 17:24:23 +01:00
if(!gas)
{
displayLists[Math.max(3, (int)((float)scale*(stages-1)))].render();
}
else {
GL11.glColor4f(1F, 1F, 1F, scale);
displayLists[stages-1].render();
}
2013-10-03 01:10:20 +02:00
}
}
}
2013-12-03 16:54:50 +01:00
DisplayInteger[] displayLists = getListAndRender(ForgeDirection.UNKNOWN, fluid);
2013-10-03 01:10:20 +02:00
if(displayLists != null)
{
2013-12-03 17:24:23 +01:00
if(!gas)
{
displayLists[Math.max(3, (int)((float)scale*(stages-1)))].render();
}
else {
GL11.glColor4f(1F, 1F, 1F, scale);
displayLists[stages-1].render();
}
2013-10-03 01:10:20 +02:00
}
2013-12-03 16:54:50 +01:00
MekanismRenderer.glowOff();
2013-10-03 01:10:20 +02:00
pop();
}
}
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:
{
2013-12-03 16:54:50 +01:00
toReturn.minX = 0.25 + offset;
toReturn.minY = 0.25 + offset;
toReturn.minZ = 0.25 + offset;
2013-10-03 01:10:20 +02:00
2013-12-03 16:54:50 +01:00
toReturn.maxX = 0.75 - offset;
toReturn.maxY = 0.25 + offset + ((float)i / (float)stages)*height;
toReturn.maxZ = 0.75 - offset;
2013-10-03 01:10:20 +02:00
break;
}
case DOWN:
{
2013-12-03 16:54:50 +01:00
toReturn.minX = 0.5 - (((float)i / (float)stages)*height)/2;
2013-10-03 01:10:20 +02:00
toReturn.minY = 0.0;
2013-12-03 16:54:50 +01:00
toReturn.minZ = 0.5 - (((float)i / (float)stages)*height)/2;
2013-10-03 01:10:20 +02:00
2013-12-03 16:54:50 +01:00
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;
2013-10-03 01:10:20 +02:00
break;
}
case UP:
{
2013-12-03 16:54:50 +01:00
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;
2013-10-03 01:10:20 +02:00
2013-12-03 16:54:50 +01:00
toReturn.maxX = 0.5 + (((float)i / (float)stages)*height)/2;
2013-10-03 01:10:20 +02:00
toReturn.maxY = 1.0;
2013-12-03 16:54:50 +01:00
toReturn.maxZ = 0.5 + (((float)i / (float)stages)*height)/2;
2013-10-03 01:10:20 +02:00
break;
}
case NORTH:
{
2013-12-03 16:54:50 +01:00
toReturn.minX = 0.25 + offset;
toReturn.minY = 0.25 + offset;
2013-10-03 01:10:20 +02:00
toReturn.minZ = 0.0;
2013-12-03 16:54:50 +01:00
toReturn.maxX = 0.75 - offset;
toReturn.maxY = 0.25 + offset + ((float)i / (float)stages)*height;
toReturn.maxZ = 0.25 + offset;
2013-10-03 01:10:20 +02:00
break;
}
case SOUTH:
{
2013-12-03 16:54:50 +01:00
toReturn.minX = 0.25 + offset;
toReturn.minY = 0.25 + offset;
toReturn.minZ = 0.75 - offset;
2013-10-03 01:10:20 +02:00
2013-12-03 16:54:50 +01:00
toReturn.maxX = 0.75 - offset;
toReturn.maxY = 0.25 + offset + ((float)i / (float)stages)*height;
2013-10-03 01:10:20 +02:00
toReturn.maxZ = 1.0;
break;
}
case WEST:
{
toReturn.minX = 0.0;
2013-12-03 16:54:50 +01:00
toReturn.minY = 0.25 + offset;
toReturn.minZ = 0.25 + offset;
2013-10-03 01:10:20 +02:00
2013-12-03 16:54:50 +01:00
toReturn.maxX = 0.25 + offset;
toReturn.maxY = 0.25 + offset + ((float)i / (float)stages)*height;
toReturn.maxZ = 0.75 - offset;
2013-10-03 01:10:20 +02:00
break;
}
case EAST:
{
2013-12-03 16:54:50 +01:00
toReturn.minX = 0.75 - offset;
toReturn.minY = 0.25 + offset;
toReturn.minZ = 0.25 + offset;
2013-10-03 01:10:20 +02:00
toReturn.maxX = 1.0;
2013-12-03 16:54:50 +01:00
toReturn.maxY = 0.25 + offset + ((float)i / (float)stages)*height;
toReturn.maxZ = 0.75 - offset;
2013-10-03 01:10:20 +02:00
break;
}
}
MekanismRenderer.renderObject(toReturn);
displays[i].endList();
2013-10-03 01:10:20 +02:00
}
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
return displays;
}
2013-12-03 22:23:08 +01:00
public void renderContents(PartPressurizedTube tube, Vector3 pos)
2013-10-03 01:10:20 +02:00
{
if(tube.getTransmitterNetwork().refGas == null || tube.getTransmitterNetwork().gasScale == 0)
2013-12-03 22:23:08 +01:00
{
return;
}
GL11.glPushMatrix();
CCRenderState.reset();
CCRenderState.useNormals(true);
CCRenderState.useModelColours(true);
CCRenderState.startDrawing(7);
GL11.glTranslated(pos.x, pos.y, pos.z);
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
{
renderGasSide(side, tube);
2013-12-03 22:23:08 +01:00
}
CCRenderState.draw();
GL11.glPopMatrix();
2013-10-03 01:10:20 +02:00
}
public void renderStatic(PartSidedPipe transmitter)
2013-10-03 01:10:20 +02:00
{
TextureUtils.bindAtlas(0);
CCRenderState.reset();
CCRenderState.useModelColours(true);
CCRenderState.setBrightness(transmitter.world(), transmitter.x(), transmitter.y(), transmitter.z());
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
2013-10-03 01:10:20 +02:00
{
2013-12-17 20:39:30 +01:00
renderSide(side, transmitter);
2013-10-03 01:10:20 +02:00
}
}
public void renderSide(ForgeDirection side, PartSidedPipe transmitter)
2013-10-03 01:10:20 +02:00
{
boolean connected = PartTransmitter.connectionMapContainsSide(transmitter.getAllCurrentConnections(), side);
Icon renderIcon = transmitter.getIconForSide(side);
renderPart(renderIcon, transmitter.getModelForSide(side, false), transmitter.x(), transmitter.y(), transmitter.z());
2013-10-03 01:10:20 +02:00
}
public void renderEnergySide(ForgeDirection side, PartUniversalCable cable)
{
boolean connected = PartTransmitter.connectionMapContainsSide(cable.getAllCurrentConnections(), side);
renderTransparency(MekanismRenderer.energyIcon, cable.getModelForSide(side, true), new ColourRGBA(1.0, 1.0, 1.0, cable.currentPower));
2013-10-03 01:10:20 +02:00
}
2013-12-03 22:23:08 +01:00
public void renderGasSide(ForgeDirection side, PartPressurizedTube tube)
{
boolean connected = PartTransmitter.connectionMapContainsSide(tube.getAllCurrentConnections(), side);
renderTransparency(tube.getTransmitterNetwork().refGas.getIcon(), tube.getModelForSide(side, true), new ColourRGBA(1.0, 1.0, 1.0, tube.currentScale));
2013-12-03 22:23:08 +01:00
}
2013-10-03 01:10:20 +02:00
2013-12-03 22:23:08 +01:00
public void renderPart(Icon icon, CCModel cc, double x, double y, double z)
{
cc.render(0, cc.verts.length, new Translation(x, y, z), new IconTransformation(icon), null);
2013-10-03 01:10:20 +02:00
}
2013-12-03 22:23:08 +01:00
public void renderTransparency(Icon icon, CCModel cc, Colour colour)
{
cc.render(0, cc.verts.length, new Translation(0, 0, 0), new IconTransformation(icon), new ColourMultiplier(colour));
2013-10-03 01:10:20 +02:00
}
@Override
public void registerIcons(IconRegister register)
{
PartUniversalCable.registerIcons(register);
PartMechanicalPipe.registerIcons(register);
PartPressurizedTube.registerIcons(register);
PartLogisticalTransporter.registerIcons(register);
2013-10-03 01:10:20 +02:00
}
@Override
public int atlasIndex()
{
return 0;
}
}