Skip to content

Commit

Permalink
Make X rendering more generic to reduce repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
bruberu authored and serenibyss committed Dec 16, 2021
1 parent 8a4f8bc commit 622f217
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 212 deletions.
9 changes: 9 additions & 0 deletions src/main/java/gregtech/api/metatileentity/MetaTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import gregtech.api.cover.ICoverable;
import gregtech.api.gui.ModularUI;
import gregtech.api.metatileentity.multiblock.MultiblockControllerBase;
import gregtech.api.pipenet.tile.TileEntityPipeBase;
import gregtech.api.recipes.FluidKey;
import gregtech.client.renderer.texture.Textures;
import gregtech.api.util.*;
Expand Down Expand Up @@ -1360,4 +1361,12 @@ public boolean isMuffled() {
public boolean canRenderFrontFaceX() {
return false;
}

public boolean isSideUsed(EnumFacing face) {
if (getCoverAtSide(face) != null) return true;
if (face == this.getFrontFacing() && this.canRenderFrontFaceX()) return true;
TileEntity tileEntity = this.getWorld().getTileEntity(this.getPos().add(face.getOpposite().getDirectionVec()));
if (!(tileEntity instanceof TileEntityPipeBase)) return false;
return ((TileEntityPipeBase) tileEntity).isConnectionOpenAny(face);
}
}
263 changes: 51 additions & 212 deletions src/main/java/gregtech/client/renderer/handler/ToolOverlayRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import gregtech.api.metatileentity.MetaTileEntityHolder;
import gregtech.api.pipenet.tile.TileEntityPipeBase;
import gregtech.api.util.GTUtility;
import gregtech.api.util.function.BooleanConsumer;
import gregtech.common.metatileentities.multi.electric.centralmonitor.MetaTileEntityMonitorScreen;
import gregtech.common.pipelike.cable.Insulation;
import gregtech.common.pipelike.fluidpipe.FluidPipeType;
Expand All @@ -36,6 +37,8 @@
import org.lwjgl.opengl.GL11;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

@SideOnly(Side.CLIENT)
public class ToolOverlayRenderer {
Expand Down Expand Up @@ -100,9 +103,13 @@ public static void onDrawBlockHighlight(DrawBlockHighlightEvent event) {
AxisAlignedBB box = blockState.getSelectedBoundingBox(world, pos).grow(0.002D).offset(-d3, -d4, -d5);
RenderGlobal.drawSelectionBoundingBox(box, 1, 1, 1, 0.4F);
rColor = gColor = bColor = 0.2f + (float) Math.sin((float) (System.currentTimeMillis() % (Math.PI * 800)) / 800) / 2;
drawOverlayLines(facing, box);

if (tileEntity instanceof TileEntityPipeBase)
drawPipeOverlayLines(facing, box, (TileEntityPipeBase) tileEntity);
drawOverlayLines(facing, box, ((TileEntityPipeBase) tileEntity)::isConnectionOpenAny);
else if (tileEntity instanceof MetaTileEntityHolder)
drawOverlayLines(facing, box, face -> ((MetaTileEntityHolder) tileEntity).getMetaTileEntity().isSideUsed(face));
else
drawOverlayLines(facing, box, ignored -> false);
}

GlStateManager.depthMask(true);
Expand Down Expand Up @@ -217,7 +224,7 @@ private static void drawBlockDamageTexture(DrawBlockHighlightEvent event, List<B
postRenderDamagedBlocks();
}

private static void drawOverlayLines(EnumFacing facing, AxisAlignedBB box) {
private static void drawOverlayLines(EnumFacing facing, AxisAlignedBB box, Function<EnumFacing, Boolean> test) {
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder buffer = tessellator.getBuffer();
buffer.begin(3, DefaultVertexFormats.POSITION_COLOR);
Expand All @@ -230,13 +237,19 @@ private static void drawOverlayLines(EnumFacing facing, AxisAlignedBB box) {
Vector3 shiftVert = new Vector3(0, 0.25, 0);

Vector3 cubeCenter = new Vector3(box.getCenter());



topRight.subtract(cubeCenter);
bottomRight.subtract(cubeCenter);
bottomLeft.subtract(cubeCenter);
topLeft.subtract(cubeCenter);

boolean leftBlocked;
boolean topBlocked;
boolean rightBlocked;
boolean bottomBlocked;
boolean frontBlocked = test.apply(facing);
boolean backBlocked = test.apply(facing.getOpposite());

switch (facing) {
case WEST: {
topRight.rotate(Math.PI / 2, Vector3.down);
Expand All @@ -245,6 +258,11 @@ private static void drawOverlayLines(EnumFacing facing, AxisAlignedBB box) {
topLeft.rotate(Math.PI / 2, Vector3.down);
shift.rotate(Math.PI / 2, Vector3.down);
shiftVert.rotate(Math.PI / 2, Vector3.down);

leftBlocked = test.apply(EnumFacing.NORTH);
topBlocked = test.apply(EnumFacing.UP);
rightBlocked = test.apply(EnumFacing.SOUTH);
bottomBlocked = test.apply(EnumFacing.DOWN);
break;
}
case EAST: {
Expand All @@ -254,6 +272,11 @@ private static void drawOverlayLines(EnumFacing facing, AxisAlignedBB box) {
topLeft.rotate(-Math.PI / 2, Vector3.down);
shift.rotate(-Math.PI / 2, Vector3.down);
shiftVert.rotate(-Math.PI / 2, Vector3.down);

leftBlocked = test.apply(EnumFacing.SOUTH);
topBlocked = test.apply(EnumFacing.UP);
rightBlocked = test.apply(EnumFacing.NORTH);
bottomBlocked = test.apply(EnumFacing.DOWN);
break;
}
case NORTH: {
Expand All @@ -263,6 +286,11 @@ private static void drawOverlayLines(EnumFacing facing, AxisAlignedBB box) {
topLeft.rotate(Math.PI, Vector3.down);
shift.rotate(Math.PI, Vector3.down);
shiftVert.rotate(Math.PI, Vector3.down);

leftBlocked = test.apply(EnumFacing.EAST);
topBlocked = test.apply(EnumFacing.UP);
rightBlocked = test.apply(EnumFacing.WEST);
bottomBlocked = test.apply(EnumFacing.DOWN);
break;
}
case UP: {
Expand All @@ -273,6 +301,11 @@ private static void drawOverlayLines(EnumFacing facing, AxisAlignedBB box) {
topLeft.rotate(-Math.PI / 2, side);
shift.rotate(-Math.PI / 2, side);
shiftVert.rotate(-Math.PI / 2, side);

leftBlocked = test.apply(EnumFacing.WEST);
topBlocked = test.apply(EnumFacing.NORTH);
rightBlocked = test.apply(EnumFacing.EAST);
bottomBlocked = test.apply(EnumFacing.SOUTH);
break;
}
case DOWN: {
Expand All @@ -283,8 +316,19 @@ private static void drawOverlayLines(EnumFacing facing, AxisAlignedBB box) {
topLeft.rotate(Math.PI / 2, side);
shift.rotate(Math.PI / 2, side);
shiftVert.rotate(Math.PI / 2, side);

leftBlocked = test.apply(EnumFacing.WEST);
topBlocked = test.apply(EnumFacing.SOUTH);
rightBlocked = test.apply(EnumFacing.EAST);
bottomBlocked = test.apply(EnumFacing.NORTH);
break;
}
default: {
leftBlocked = test.apply(EnumFacing.WEST);
topBlocked = test.apply(EnumFacing.UP);
rightBlocked = test.apply(EnumFacing.EAST);
bottomBlocked = test.apply(EnumFacing.DOWN);
}
}

topRight.add(cubeCenter);
Expand All @@ -306,211 +350,6 @@ private static void drawOverlayLines(EnumFacing facing, AxisAlignedBB box) {
startLine(buffer, bottomLeft.copy().add(shiftVert));
endLine(buffer, bottomRight.copy().add(shiftVert));

tessellator.draw();
}

private static void drawCrossedOverlayLines(EnumFacing facing, AxisAlignedBB box, MetaTileEntityHolder tileEntity) {
if(tileEntity.getMetaTileEntity().canRenderFrontFaceX())
return;
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder buffer = tessellator.getBuffer();
buffer.begin(3, DefaultVertexFormats.POSITION_COLOR);

Vector3 topRight = new Vector3(box.maxX, box.maxY, box.maxZ);
Vector3 bottomRight = new Vector3(box.maxX, box.minY, box.maxZ);
Vector3 bottomLeft = new Vector3(box.minX, box.minY, box.maxZ);
Vector3 topLeft = new Vector3(box.minX, box.maxY, box.maxZ);
Vector3 shift = new Vector3(0.25, 0, 0);
Vector3 shiftVert = new Vector3(0, 0.25, 0);

Vector3 cubeCenter = new Vector3(box.getCenter());


topRight.subtract(cubeCenter);
bottomRight.subtract(cubeCenter);
bottomLeft.subtract(cubeCenter);
topLeft.subtract(cubeCenter);

switch (facing) {
case WEST: {
topRight.rotate(Math.PI / 2, Vector3.down);
bottomRight.rotate(Math.PI / 2, Vector3.down);
bottomLeft.rotate(Math.PI / 2, Vector3.down);
topLeft.rotate(Math.PI / 2, Vector3.down);
shift.rotate(Math.PI / 2, Vector3.down);
shiftVert.rotate(Math.PI / 2, Vector3.down);
break;
}
case EAST: {
topRight.rotate(-Math.PI / 2, Vector3.down);
bottomRight.rotate(-Math.PI / 2, Vector3.down);
bottomLeft.rotate(-Math.PI / 2, Vector3.down);
topLeft.rotate(-Math.PI / 2, Vector3.down);
shift.rotate(-Math.PI / 2, Vector3.down);
shiftVert.rotate(-Math.PI / 2, Vector3.down);
break;
}
case NORTH: {
topRight.rotate(Math.PI, Vector3.down);
bottomRight.rotate(Math.PI, Vector3.down);
bottomLeft.rotate(Math.PI, Vector3.down);
topLeft.rotate(Math.PI, Vector3.down);
shift.rotate(Math.PI, Vector3.down);
shiftVert.rotate(Math.PI, Vector3.down);
break;
}
case UP: {
Vector3 side = new Vector3(1, 0, 0);
topRight.rotate(-Math.PI / 2, side);
bottomRight.rotate(-Math.PI / 2, side);
bottomLeft.rotate(-Math.PI / 2, side);
topLeft.rotate(-Math.PI / 2, side);
shift.rotate(-Math.PI / 2, side);
shiftVert.rotate(-Math.PI / 2, side);
break;
}
case DOWN: {
Vector3 side = new Vector3(1, 0, 0);
topRight.rotate(Math.PI / 2, side);
bottomRight.rotate(Math.PI / 2, side);
bottomLeft.rotate(Math.PI / 2, side);
topLeft.rotate(Math.PI / 2, side);
shift.rotate(Math.PI / 2, side);
shiftVert.rotate(Math.PI / 2, side);
break;
}
}

topRight.add(cubeCenter);
bottomRight.add(cubeCenter);
bottomLeft.add(cubeCenter);
topLeft.add(cubeCenter);

// internal X
startLine(buffer, topLeft.copy().add(shift).add(shiftVert.copy().negate()));
endLine(buffer, bottomRight.copy().add(shift.copy().negate()).add(shiftVert));

startLine(buffer, topRight.copy().add(shift.copy().negate()).add(shiftVert.copy().negate()));
endLine(buffer, bottomLeft.copy().add(shift).add(shiftVert));

tessellator.draw();
}

private static void drawPipeOverlayLines(EnumFacing facing, AxisAlignedBB box, TileEntityPipeBase pipe) {
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder buffer = tessellator.getBuffer();
buffer.begin(3, DefaultVertexFormats.POSITION_COLOR);

Vector3 topRight = new Vector3(box.maxX, box.maxY, box.maxZ);
Vector3 bottomRight = new Vector3(box.maxX, box.minY, box.maxZ);
Vector3 bottomLeft = new Vector3(box.minX, box.minY, box.maxZ);
Vector3 topLeft = new Vector3(box.minX, box.maxY, box.maxZ);
Vector3 shift = new Vector3(0.25, 0, 0);
Vector3 shiftVert = new Vector3(0, 0.25, 0);

Vector3 cubeCenter = new Vector3(box.getCenter());

boolean leftBlocked;
boolean topBlocked;
boolean rightBlocked;
boolean bottomBlocked;
boolean frontBlocked;
boolean backBlocked;

topRight.subtract(cubeCenter);
bottomRight.subtract(cubeCenter);
bottomLeft.subtract(cubeCenter);
topLeft.subtract(cubeCenter);

frontBlocked = pipe.isConnectionOpenAny(facing);
backBlocked = pipe.isConnectionOpenAny(facing.getOpposite());

switch (facing) {
case WEST: {
topRight.rotate(Math.PI / 2, Vector3.down);
bottomRight.rotate(Math.PI / 2, Vector3.down);
bottomLeft.rotate(Math.PI / 2, Vector3.down);
topLeft.rotate(Math.PI / 2, Vector3.down);
shift.rotate(Math.PI / 2, Vector3.down);
shiftVert.rotate(Math.PI / 2, Vector3.down);

leftBlocked = pipe.isConnectionOpenAny(EnumFacing.NORTH);
topBlocked = pipe.isConnectionOpenAny(EnumFacing.UP);
rightBlocked = pipe.isConnectionOpenAny(EnumFacing.SOUTH);
bottomBlocked = pipe.isConnectionOpenAny(EnumFacing.DOWN);
break;
}
case EAST: {
topRight.rotate(-Math.PI / 2, Vector3.down);
bottomRight.rotate(-Math.PI / 2, Vector3.down);
bottomLeft.rotate(-Math.PI / 2, Vector3.down);
topLeft.rotate(-Math.PI / 2, Vector3.down);
shift.rotate(-Math.PI / 2, Vector3.down);
shiftVert.rotate(-Math.PI / 2, Vector3.down);

leftBlocked = pipe.isConnectionOpenAny(EnumFacing.SOUTH);
topBlocked = pipe.isConnectionOpenAny(EnumFacing.UP);
rightBlocked = pipe.isConnectionOpenAny(EnumFacing.NORTH);
bottomBlocked = pipe.isConnectionOpenAny(EnumFacing.DOWN);
break;
}
case NORTH: {
topRight.rotate(Math.PI, Vector3.down);
bottomRight.rotate(Math.PI, Vector3.down);
bottomLeft.rotate(Math.PI, Vector3.down);
topLeft.rotate(Math.PI, Vector3.down);
shift.rotate(Math.PI, Vector3.down);
shiftVert.rotate(Math.PI, Vector3.down);

leftBlocked = pipe.isConnectionOpenAny(EnumFacing.EAST);
topBlocked = pipe.isConnectionOpenAny(EnumFacing.UP);
rightBlocked = pipe.isConnectionOpenAny(EnumFacing.WEST);
bottomBlocked = pipe.isConnectionOpenAny(EnumFacing.DOWN);
break;
}
case UP: {
Vector3 side = new Vector3(1, 0, 0);
topRight.rotate(-Math.PI / 2, side);
bottomRight.rotate(-Math.PI / 2, side);
bottomLeft.rotate(-Math.PI / 2, side);
topLeft.rotate(-Math.PI / 2, side);
shift.rotate(-Math.PI / 2, side);
shiftVert.rotate(-Math.PI / 2, side);

leftBlocked = pipe.isConnectionOpenAny(EnumFacing.WEST);
topBlocked = pipe.isConnectionOpenAny(EnumFacing.NORTH);
rightBlocked = pipe.isConnectionOpenAny(EnumFacing.EAST);
bottomBlocked = pipe.isConnectionOpenAny(EnumFacing.SOUTH);
break;
}
case DOWN: {
Vector3 side = new Vector3(1, 0, 0);
topRight.rotate(Math.PI / 2, side);
bottomRight.rotate(Math.PI / 2, side);
bottomLeft.rotate(Math.PI / 2, side);
topLeft.rotate(Math.PI / 2, side);
shift.rotate(Math.PI / 2, side);
shiftVert.rotate(Math.PI / 2, side);

leftBlocked = pipe.isConnectionOpenAny(EnumFacing.WEST);
topBlocked = pipe.isConnectionOpenAny(EnumFacing.SOUTH);
rightBlocked = pipe.isConnectionOpenAny(EnumFacing.EAST);
bottomBlocked = pipe.isConnectionOpenAny(EnumFacing.NORTH);
break;
}
default: {
leftBlocked = pipe.isConnectionOpenAny(EnumFacing.WEST);
topBlocked = pipe.isConnectionOpenAny(EnumFacing.UP);
rightBlocked = pipe.isConnectionOpenAny(EnumFacing.EAST);
bottomBlocked = pipe.isConnectionOpenAny(EnumFacing.DOWN);
}
}

topRight.add(cubeCenter);
bottomRight.add(cubeCenter);
bottomLeft.add(cubeCenter);
topLeft.add(cubeCenter);

if (leftBlocked) {
startLine(buffer, topLeft.copy().add(shiftVert.copy().negate()));
endLine(buffer, bottomLeft.copy().add(shiftVert.copy()).add(shift));
Expand Down Expand Up @@ -548,9 +387,9 @@ private static void drawPipeOverlayLines(EnumFacing facing, AxisAlignedBB box, T
}
if (backBlocked) {
Vector3 localXShift = new Vector3(0, 0, 0); // Set up translations for the current X.
for(int i = 0; i < 2; i++) {
for (int i = 0; i < 2; i++) {
Vector3 localXShiftVert = new Vector3(0, 0, 0);
for(int j = 0; j < 2; j++) {
for (int j = 0; j < 2; j++) {
startLine(buffer, topLeft.copy().add(localXShift).add(localXShiftVert));
endLine(buffer, topLeft.copy().add(localXShift).add(localXShiftVert).add(shift).subtract(shiftVert));

Expand Down

0 comments on commit 622f217

Please sign in to comment.