diff --git a/src/main/java/com/hbm/blocks/test/TestCT.java b/src/main/java/com/hbm/blocks/test/TestCT.java index 13e04ca30..f7436c909 100644 --- a/src/main/java/com/hbm/blocks/test/TestCT.java +++ b/src/main/java/com/hbm/blocks/test/TestCT.java @@ -1,6 +1,8 @@ package com.hbm.blocks.test; +import com.hbm.render.block.ct.IBlockCT; import com.hbm.render.block.ct.IconCT; +import com.hbm.render.block.ct.IconGeneric; import cpw.mods.fml.client.registry.RenderingRegistry; import cpw.mods.fml.relauncher.Side; @@ -12,7 +14,7 @@ import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.util.IIcon; -public class TestCT extends Block { +public class TestCT extends Block implements IBlockCT { public TestCT(Material p_i45394_1_) { super(p_i45394_1_); @@ -30,20 +32,11 @@ public class TestCT extends Block { @SideOnly(Side.CLIENT) public void registerBlockIcons(IIconRegister reg) { this.blockIcon = reg.registerIcon(this.getTextureName()); - - IIcon ct = new SevenUp(this.getTextureName() + "_ct"); - reg.registerIcon(this.getTextureName() + "_ct"); - - for(int i = 0; i < frags.length; i++) { - frags[i] = new IconCT(i < 4 ? this.blockIcon : ct, i); - } + this.frags = IBlockCT.registerIcons(reg, this.getTextureName(), this.blockIcon); } - @SideOnly(Side.CLIENT) - public static class SevenUp extends TextureAtlasSprite { - - protected SevenUp(String tex) { - super(tex); - } + @Override + public IIcon[] getFragments() { + return frags; } } diff --git a/src/main/java/com/hbm/render/block/RenderBlockCT.java b/src/main/java/com/hbm/render/block/RenderBlockCT.java new file mode 100644 index 000000000..8b34ec364 --- /dev/null +++ b/src/main/java/com/hbm/render/block/RenderBlockCT.java @@ -0,0 +1,42 @@ +package com.hbm.render.block; + +import com.hbm.blocks.test.TestCT; +import com.hbm.render.block.ct.CTContext; +import com.hbm.render.block.ct.RenderBlocksCT; + +import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler; +import net.minecraft.block.Block; +import net.minecraft.client.renderer.RenderBlocks; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; + +public class RenderBlockCT implements ISimpleBlockRenderingHandler { + + @Override + public void renderInventoryBlock(Block block, int metadata, int modelId, RenderBlocks renderer) { + renderer.renderBlockAsItem(block, 1, 1.0F); + } + + @Override + public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer) { + + RenderBlocksCT rbct = RenderBlocksCT.instance; + + CTContext.loadContext((World)world, x, y, z, block); + rbct.renderStandardBlock(block, x, y, z); + CTContext.dropContext(); + + return true; + } + + @Override + public boolean shouldRender3DInInventory(int modelId) { + return true; + } + + @Override + public int getRenderId() { + return TestCT.renderID; + } + +} diff --git a/src/main/java/com/hbm/render/block/ct/CT.java b/src/main/java/com/hbm/render/block/ct/CT.java new file mode 100644 index 000000000..b0cb5da87 --- /dev/null +++ b/src/main/java/com/hbm/render/block/ct/CT.java @@ -0,0 +1,35 @@ +package com.hbm.render.block.ct; + +public class CT { + + public static final int l = 0; //left + public static final int r = 1; //right + public static final int t = 0; //top + public static final int b = 2; //bottom + public static final int f = 0; //full/unconnected + public static final int c = 4; //connected + public static final int j = 8; //junction + public static final int h = 12; //horizontal + public static final int v = 16; //vertical + + public static final int ftl = 0; + public static final int ftr = 1; + public static final int fbl = 2; + public static final int fbr = 3; + public static final int ctl = 4; + public static final int ctr = 5; + public static final int cbl = 6; + public static final int cbr = 7; + public static final int jtl = 8; + public static final int jtr = 9; + public static final int jbl = 10; + public static final int jbr = 11; + public static final int htl = 12; + public static final int htr = 13; + public static final int hbl = 14; + public static final int hbr = 15; + public static final int vtl = 16; + public static final int vtr = 17; + public static final int vbl = 18; + public static final int vbr = 19; +} diff --git a/src/main/java/com/hbm/render/block/ct/CTContext.java b/src/main/java/com/hbm/render/block/ct/CTContext.java new file mode 100644 index 000000000..3a1cbed81 --- /dev/null +++ b/src/main/java/com/hbm/render/block/ct/CTContext.java @@ -0,0 +1,131 @@ +package com.hbm.render.block.ct; + +import net.minecraft.block.Block; +import net.minecraft.util.IIcon; +import net.minecraft.world.World; + +import static com.hbm.render.block.ct.CT.*; + +public class CTContext { + + public static CTFace[] faces; + + //dim 1: faces (forgeDir) + //dim 2: neighbors (TL, TC, TR, CL, CR, BL, BC, BR) + //dim 3: coord (x/y/z, [-1;1]) + public static int[][][] access = new int[][][] { + { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }, + { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }, + { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }, + { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }, + { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }, + { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0} } + //TODO + }; + + /** + * Generates the six CTFaces and fills the faces array, the faces contain the information on what icon types should be used + * @param world + * @param x + * @param y + * @param z + * @param block + */ + public static void loadContext(World world, int x, int y, int z, Block block) { + + faces = new CTFace[6]; + + for(int i = 0; i < 6; i++) { + + boolean[] cons = new boolean[8]; + int[][] dirs = access[i]; + + for(int j = 0; j < 8; j++) { + + int[] coord = dirs[j]; + Block neighbor = world.getBlock(x + coord[0], y + coord[1], z + coord[2]); + + if(neighbor instanceof IBlockCT && ((IBlockCT) neighbor).canConnect(world, x + coord[0], y + coord[1], z + coord[2], (IBlockCT)block)) { + cons[j] = true; + } + } + + /* + * 1 2 3 + * 4 5 + * 6 7 8 + */ + + int itl = t | l | cornerType(cons[4], cons[1], cons[2]); + int itr = t | r | cornerType(cons[5], cons[3], cons[2]); + int ibl = b | l | cornerType(cons[4], cons[6], cons[7]); + int ibr = b | r | cornerType(cons[5], cons[8], cons[7]); + + faces[i] = new CTFace((IBlockCT)block, itl, itr, ibl, ibr); + } + } + + /** + * Returns the overarching texture type based on whether the horizontal, vertical and corner are connected + * @param vert + * @param hor + * @param corner + * @return the bitmask for the full texture type that should be used (f/c/j/h/v) + */ + public static int cornerType(boolean vert, boolean hor, boolean corner) { + + if(vert && hor && corner) + return c; + else if(vert && hor) + return j; + else if(vert) + return v; + else if(hor) + return h; + else + return f; + } + + /** + * @return whether there's currently a context available + */ + public static boolean isContextLoaded() { + return faces != null; + } + + /** + * Gets rid of the current context + */ + public static void dropContext() { + faces = null; + } + + /** + * The buffered context information for a single side. Also stores the block itself for easier access. + * @author hbm + * + */ + public static class CTFace { + + IBlockCT ct; + int index_tl; + int index_tr; + int index_bl; + int index_br; + + public CTFace() { } + + public CTFace(IBlockCT block, int i, int j, int k, int l) { + this.ct = block; + this.index_tl = i; + this.index_tr = j; + this.index_bl = k; + this.index_br = l; + } + + public IIcon getTopLeft() { return ct.getFragments()[index_tl]; } + public IIcon getTopRight() { return ct.getFragments()[index_tr]; } + public IIcon getBottomLeft() { return ct.getFragments()[index_bl]; } + public IIcon getBottomRight() { return ct.getFragments()[index_br]; } + } +} diff --git a/src/main/java/com/hbm/render/block/ct/IBlockCT.java b/src/main/java/com/hbm/render/block/ct/IBlockCT.java new file mode 100644 index 000000000..860a87526 --- /dev/null +++ b/src/main/java/com/hbm/render/block/ct/IBlockCT.java @@ -0,0 +1,27 @@ +package com.hbm.render.block.ct; + +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.util.IIcon; +import net.minecraft.world.World; + +public interface IBlockCT { + + public IIcon[] getFragments(); + + public default boolean canConnect(World world, int x, int y, int z, IBlockCT block) { + return this == block; + } + + public static IIcon[] registerIcons(IIconRegister reg, String textureName, IIcon blockIcon) { + IIcon[] frags = new IIcon[20]; + + IIcon ct = new IconGeneric(textureName + "_ct"); + reg.registerIcon(textureName + "_ct"); + + for(int i = 0; i < frags.length; i++) { + frags[i] = new IconCT(i < 4 ? blockIcon : ct, i); + } + + return frags; + } +} diff --git a/src/main/java/com/hbm/render/block/ct/IconCT.java b/src/main/java/com/hbm/render/block/ct/IconCT.java index 5a2988cfd..2e2adecf3 100644 --- a/src/main/java/com/hbm/render/block/ct/IconCT.java +++ b/src/main/java/com/hbm/render/block/ct/IconCT.java @@ -1,39 +1,9 @@ package com.hbm.render.block.ct; +import static com.hbm.render.block.ct.CT.*; import net.minecraft.util.IIcon; public class IconCT implements IIcon { - - public static final int l = 0; //left - public static final int r = 1; //right - public static final int t = 0; //top - public static final int b = 2; //bottom - public static final int f = 0; //full - public static final int c = 4; //connected - public static final int j = 8; //junction - public static final int h = 12; //horizontal - public static final int v = 16; //vertical - - public static final int ftl = 0; - public static final int ftr = 1; - public static final int fbl = 2; - public static final int fbr = 3; - public static final int ctl = 4; - public static final int ctr = 5; - public static final int cbl = 6; - public static final int cbr = 7; - public static final int jtl = 8; - public static final int jtr = 9; - public static final int jbl = 10; - public static final int jbr = 11; - public static final int htl = 12; - public static final int htr = 13; - public static final int hbl = 14; - public static final int hbr = 15; - public static final int vtl = 16; - public static final int vtr = 17; - public static final int vbl = 18; - public static final int vbr = 19; private IIcon parent; private int type; @@ -42,38 +12,37 @@ public class IconCT implements IIcon { private float minV; private float maxV; - /// none of this is going to work because that's just not how icon UV works! /// public IconCT(IIcon parent, int type) { this.parent = parent; this.type = type; int sub = ((type & f) != 0) ? 2 : 4; - float len = 1F / sub; + float lenU = (parent.getMaxU() - parent.getMinU()) / sub; + float lenV = (parent.getMaxV() - parent.getMinV()) / sub; - float du = 0F; - float dv = 0F; + float du = parent.getMinU(); + float dv = parent.getMinV(); //set pos to full block (coarse positioning) if((type & v) > 0 || (type & j) > 0) { - du += len * 2; + du += lenU * 2; } if((type & h) > 0 || (type & j) > 0) { - dv += len * 2; + dv += lenV * 2; } //set pos to sub-block (fine positioning) if((type & r) > 0) { - du += len; + du += lenU; } if((type & b) > 0) { - dv += len; + dv += lenV; } minU = du; - maxU = du + len; + maxU = du + lenU; minV = dv; - maxV = dv + len; - //what moron wrote this + maxV = dv + lenV; } @Override diff --git a/src/main/java/com/hbm/render/block/ct/IconGeneric.java b/src/main/java/com/hbm/render/block/ct/IconGeneric.java new file mode 100644 index 000000000..166505d7e --- /dev/null +++ b/src/main/java/com/hbm/render/block/ct/IconGeneric.java @@ -0,0 +1,13 @@ +package com.hbm.render.block.ct; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; + +@SideOnly(Side.CLIENT) +public class IconGeneric extends TextureAtlasSprite { + + public IconGeneric(String name) { + super(name); + } +} diff --git a/src/main/java/com/hbm/render/block/ct/RenderBlocksCT.java b/src/main/java/com/hbm/render/block/ct/RenderBlocksCT.java index 1973e04ce..fbe9e2c30 100644 --- a/src/main/java/com/hbm/render/block/ct/RenderBlocksCT.java +++ b/src/main/java/com/hbm/render/block/ct/RenderBlocksCT.java @@ -1,11 +1,15 @@ package com.hbm.render.block.ct; +import org.lwjgl.opengl.GL11; + import net.minecraft.block.Block; import net.minecraft.client.renderer.RenderBlocks; import net.minecraft.client.renderer.Tessellator; import net.minecraft.util.IIcon; public class RenderBlocksCT extends RenderBlocks { + + public static RenderBlocksCT instance = new RenderBlocksCT(); VertInfo tl; VertInfo tc; @@ -42,6 +46,11 @@ public class RenderBlocksCT extends RenderBlocks { this.cc = VertInfo.avg(tl, tr, bl, br); } + /*@Override + public boolean renderStandardBlock(Block block, int x, int y, int z) { + return super.renderStandardBlock(block, x, y, z); + }*/ + @Override public void renderFaceXPos(Block block, double x, double y, double z, IIcon icon) { super.renderFaceXPos(block, x, y, z, icon); @@ -149,4 +158,42 @@ public class RenderBlocksCT extends RenderBlocks { return new VertInfo(r, g, b, l); } } + + @Override + public void renderBlockAsItem(Block block, int meta, float mult) { + + block.setBlockBoundsForItemRender(); + this.setRenderBoundsFromBlock(block); + GL11.glRotatef(90.0F, 0.0F, 1.0F, 0.0F); + GL11.glTranslatef(-0.5F, -0.5F, -0.5F); + + tess.startDrawingQuads(); + tess.setNormal(0.0F, -1.0F, 0.0F); + this.renderFaceYNeg(block, 0.0D, 0.0D, 0.0D, this.getBlockIconFromSideAndMetadata(block, 0, meta)); + tess.draw(); + tess.startDrawingQuads(); + tess.setNormal(0.0F, 1.0F, 0.0F); + this.renderFaceYPos(block, 0.0D, 0.0D, 0.0D, this.getBlockIconFromSideAndMetadata(block, 1, meta)); + tess.draw(); + + tess.startDrawingQuads(); + tess.setNormal(0.0F, 0.0F, -1.0F); + this.renderFaceZNeg(block, 0.0D, 0.0D, 0.0D, this.getBlockIconFromSideAndMetadata(block, 2, meta)); + tess.draw(); + tess.startDrawingQuads(); + tess.setNormal(0.0F, 0.0F, 1.0F); + this.renderFaceZPos(block, 0.0D, 0.0D, 0.0D, this.getBlockIconFromSideAndMetadata(block, 3, meta)); + tess.draw(); + + tess.startDrawingQuads(); + tess.setNormal(-1.0F, 0.0F, 0.0F); + this.renderFaceXNeg(block, 0.0D, 0.0D, 0.0D, this.getBlockIconFromSideAndMetadata(block, 4, meta)); + tess.draw(); + tess.startDrawingQuads(); + tess.setNormal(1.0F, 0.0F, 0.0F); + this.renderFaceXPos(block, 0.0D, 0.0D, 0.0D, this.getBlockIconFromSideAndMetadata(block, 5, meta)); + tess.draw(); + + GL11.glTranslatef(0.5F, 0.5F, 0.5F); + } }