Skip to content

Commit

Permalink
Merge pull request #10 from S4mpsa/master
Browse files Browse the repository at this point in the history
Rewritten information panel rendering code to fix chunk boundary bug properly
  • Loading branch information
Connor-Colenso authored Aug 15, 2022
2 parents 86929c1 + 1daed2a commit eebc0c9
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ScreenManager() {
unusedPanels = new HashMap<Integer, List<TileEntityInfoPanel>>();
}

private int getWorldKey(World world) {
public int getWorldKey(World world) {
if (world == null)
return -10;
if (world.getWorldInfo() == null)
Expand All @@ -40,6 +40,10 @@ private int getWorldKey(World world) {
}
}

public Map<Integer, List<Screen>> getScreens() {
return screens;
}

private boolean isValidExtender(World world, int x, int y, int z,
int facing, boolean advanced) {
if (world.getBlock(x, y, z) != IC2NuclearControl.blockNuclearControlMain)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import shedar.mods.ic2.nuclearcontrol.panel.Screen;
import shedar.mods.ic2.nuclearcontrol.panel.ScreenManager;

@SideOnly(Side.CLIENT)
public class MainBlockRenderer implements ISimpleBlockRenderingHandler {
Expand Down Expand Up @@ -114,11 +116,22 @@ public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z,
y, z, renderer);
else
renderer.renderStandardBlock(block, x, y, z);

} else if (tileEntity instanceof TileEntityAdvancedInfoPanelExtender) {
TileEntityAdvancedInfoPanelExtender advancedExtender = (TileEntityAdvancedInfoPanelExtender) tileEntity;
if (advancedExtender.getNBTLoaded() && !advancedExtender.getPartOfScreen()) {
boolean wasRendered = false;

for (Screen screen : IC2NuclearControl.instance.screenManager.getScreens().get(
IC2NuclearControl.instance.screenManager.getWorldKey(advancedExtender.getWorldObj()))) {
if (screen.isBlockPartOf(advancedExtender)) {
wasRendered = true;
}
}

if (!wasRendered) {
renderer.renderStandardBlock(block, x, y, z);
}

} else {
renderer.renderStandardBlock(block, x, y, z);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ public void renderTileEntityAt(TileEntity tileEntity, double x, double y,
float scaleY = displayHeight / requiredHeight;
float scale = Math.min(scaleX, scaleY);
GL11.glScalef(scale, -scale, scale);
GL11.glDepthMask(false);

int offsetX;
int offsetY;
Expand Down Expand Up @@ -314,7 +313,6 @@ public void renderTileEntityAt(TileEntity tileEntity, double x, double y,

GL11.glEnable(GL11.GL_LIGHTING);

GL11.glDepthMask(true);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

GL11.glDisable(GL11.GL_POLYGON_OFFSET_FILL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Facing;

import org.lwjgl.opengl.GL11;

Expand All @@ -20,6 +21,22 @@ public class ModelInfoPanel {
private static final ResourceLocation TEXTURE_LOCATION = new ResourceLocation(TEXTURE_FILE);

private double[] coordinates = new double[24];
private static final byte[][] pointMap = {
{0, 3, 2, 1},
{4, 5, 6, 7},
{0, 4, 7, 3},
{6, 5, 1, 2},
{5, 4, 0, 1},
{2, 3, 7, 6}
};
private static final byte[][] normalMap = {
{0, -1, 0},
{0, 1, 0},
{0, 0, -1},
{0, 0, 1},
{-1, 0, 0},
{1, 0, 0}
};

private void assignWithRotation(int rotation, int offset, int sign, int tl,
int tr, int br, int bl, double dtl, double dtr, double dbr,
Expand Down Expand Up @@ -152,11 +169,13 @@ private void addSlopes(TileEntityAdvancedInfoPanel panel, Screen screen,

private void initCoordinates(Block block, Screen screen) {

// 5o ----- o6
// 4o ----- o7
// / | /
// / 1o / o2
// 0o ----- o3
// 5 -------6
// /| /|
// 4 -------7 |
// | | | |
// | 1 -----|-2
// |/ |/
// 0 ------ 3
double blockMinX = block.getBlockBoundsMinX();
double blockMinY = block.getBlockBoundsMinY();
double blockMinZ = block.getBlockBoundsMinZ();
Expand Down Expand Up @@ -204,175 +223,67 @@ private void addPoint(int point, double u, double v) {
coordinates[point * 3 + 1], coordinates[point * 3 + 2], u, v);
}

private void drawFacing(int facing, int rotation, Screen screen, TileEntityAdvancedInfoPanel panel, Block block, Tessellator tess) {
// TODO: refactor here
int point = 0;
int pointR = 0;
int pointB = 0;
int pointRB = 0;
private void addPoints(byte[] points, byte[] n, double u1, double u2, double v1, double v2) {
Tessellator.instance.setNormal(n[0], n[1], n[2]);
addPoint(points[0], u1, v1);
addPoint(points[1], u1, v2);
addPoint(points[2], u2, v2);
addPoint(points[3], u2, v1);
}

int offsetH = 0;
int offsetV = 0;
int offsetD = 0;
private double[] normalize(double[] vec) {
double len = Math.sqrt((vec[0]*vec[0]) + (vec[1]*vec[1]) + (vec[2]*vec[2]));
return new double[] {vec[0]/len, vec[1]/len, vec[2]/len};
}

boolean ccw = false;
switch (facing) {
case 0:
tess.setNormal(0, -1, 0);
point = 3;
pointR = 0;
pointB = 2;
pointRB = 1;

offsetH = 0;
offsetV = 2;
offsetD = 1;
ccw = true;
break;
case 1:
tess.setNormal(0, 1, 0);
point = 4;
pointR = 7;
pointB = 5;
pointRB = 6;

offsetH = 0;
offsetV = 2;
offsetD = 1;
break;
case 2:
tess.setNormal(0, 0, -1);
point = 7;
pointR = 4;
pointB = 3;
pointRB = 0;

offsetH = 0;
offsetV = 1;
offsetD = 2;
ccw = rotation == 1 || rotation == 2;
break;
case 3:
tess.setNormal(0, 0, 1);
point = 5;
pointR = 6;
pointB = 1;
pointRB = 2;

offsetH = 0;
offsetV = 1;
offsetD = 2;
break;
case 4:
tess.setNormal(-1, 0, 0);
point = 4;
pointR = 5;
pointB = 0;
pointRB = 1;

offsetH = 2;
offsetV = 1;
offsetD = 0;
break;
case 5:
tess.setNormal(1, 0, 0);
point = 6;
pointR = 7;
pointB = 2;
pointRB = 3;

offsetH = 2;
offsetV = 1;
offsetD = 0;
ccw = rotation == 1 || rotation == 2;
break;
private double[] scale(double[] vec, double scale) {
return new double[] {vec[0]*scale, vec[1]*scale, vec[2]*scale};
}

private double[] vectorBetweenPoints(double[] vec1, double[] vec2) {
return new double[] {vec1[0] - vec2[0], vec1[1] - vec2[1], vec1[2] - vec2[2]};
}
private void drawScreenWithBorder(byte[] points, byte[] n, double u1, double u2, double v1, double v2, double border, int facing) {
Tessellator.instance.setNormal(n[0], n[1], n[2]);
double[][] UVMap = {{u1, v1},{u1, v2},{u2, v2},{u2, v1}};
byte[] edges = {points[3], points[0], points[1], points[2], points[3], points[0]};

for (int i = 1; i < 5; i++) {
double[] edge1 = scale(normalize(vectorBetweenPoints(
new double[] {coordinates[edges[i]*3], coordinates[edges[i]*3+1], coordinates[edges[i]*3+2]},
new double[] {coordinates[edges[i+1]*3], coordinates[edges[i+1]*3+1], coordinates[edges[i+1]*3+2]})), border);
double[] edge2 = scale(normalize(vectorBetweenPoints(
new double[] {coordinates[edges[i]*3], coordinates[edges[i]*3+1], coordinates[edges[i]*3+2]},
new double[] {coordinates[edges[i-1]*3], coordinates[edges[i-1]*3+1], coordinates[edges[i-1]*3+2]})), border);

Tessellator.instance.addVertexWithUV(
coordinates[edges[i]*3] - edge1[0] - edge2[0] + 0.001 * Facing.offsetsXForSide[facing],
coordinates[edges[i]*3 + 1] - edge1[1] - edge2[1] + 0.001 * Facing.offsetsYForSide[facing],
coordinates[edges[i]*3 + 2] - edge1[2] - edge2[2] + 0.001 * Facing.offsetsZForSide[facing], UVMap[i-1][0], UVMap[i-1][1]);
}
switch (rotation) {
case 1:
int tmp = offsetH;
offsetH = offsetV;
offsetV = tmp;

pointB = point;
point = pointR;
pointR = pointRB;
break;
case 2:
tmp = offsetH;
offsetH = offsetV;
offsetV = tmp;
}

pointR = point;
point = pointB;
pointB = pointRB;
break;
case 3:
point = pointRB;
tmp = pointR;
pointR = pointB;
pointB = tmp;
break;
}
private void drawFacing(int facing, int rotation, Screen screen, TileEntityAdvancedInfoPanel panel, Block block, Tessellator tess) {

IIcon texture = block.getIcon(panel.getWorldObj(), panel.xCoord, panel.yCoord, panel.zCoord, 0);

double u1 = texture.getMinU();
double u2 = texture.getMaxU();
double v1 = texture.getMinV();
double v2 = texture.getMaxV();
GL11.glDepthMask(false);
addPoints(pointMap[facing], normalMap[facing], u1, u2, v1, v2);
GL11.glDepthMask(true);
texture = block.getIcon(panel.getWorldObj(), panel.xCoord, panel.yCoord, panel.zCoord, facing);

u1 = texture.getMinU();
u2 = texture.getMaxU();
v1 = texture.getMinV();
v2 = texture.getMaxV();

drawScreenWithBorder(pointMap[facing], normalMap[facing], u1, u2, v1, v2, 0.05, facing);

int stepsHor = screen.getWidth(panel);
int stepsVert = screen.getHeight(panel);
int sh = 0;
double dh = (coordinates[pointR * 3 + offsetH] - coordinates[point * 3
+ offsetH])
/ stepsHor;
double dv = (coordinates[pointB * 3 + offsetV] - coordinates[point * 3
+ offsetV])
/ stepsVert;
double ddh = (coordinates[pointR * 3 + offsetD] - coordinates[point * 3
+ offsetD])
/ stepsHor;
double ddv = (coordinates[pointB * 3 + offsetD] - coordinates[point * 3
+ offsetD])
/ stepsVert;
double[] base = new double[] { coordinates[point * 3],
coordinates[point * 3 + 1], coordinates[point * 3 + 2] };
double[] midpoint = new double[3];
while (sh < stepsHor) {
int sv = 0;
while (sv < stepsVert) {
double[] p = base.clone();
p[offsetH] += dh * sh;
p[offsetV] += dv * sv;
p[offsetD] += ddh * sh + ddv * sv;

midpoint[offsetH] = p[offsetH] + dh / 2;
midpoint[offsetV] = p[offsetV] + dv / 2;
midpoint[offsetD] = p[offsetD] + (ddh + ddv) / 2;

IIcon texture = block.getIcon(panel.getWorldObj(),
(int) Math.floor(midpoint[0]),
(int) Math.floor(midpoint[1]),
(int) Math.floor(midpoint[2]), facing);

double u1 = texture.getMinU();
double u2 = texture.getMaxU();
double v1 = texture.getMinV();
double v2 = texture.getMaxV();
if (ccw) {
double tu = u1;
u1 = u2;
u2 = tu;
}
tess.addVertexWithUV(p[0], p[1], p[2], u1, v1);
p[offsetV] += dv;
p[offsetD] += ddv;
tess.addVertexWithUV(p[0], p[1], p[2], u1, v2);
p[offsetH] += dh;
p[offsetD] += ddh;
tess.addVertexWithUV(p[0], p[1], p[2], u2, v2);
p[offsetV] -= dv;
p[offsetD] -= ddv;
tess.addVertexWithUV(p[0], p[1], p[2], u2, v1);

sv++;
}
sh++;
}
}

public void renderScreen(Block block, TileEntityAdvancedInfoPanel panel, double x, double y, double z, RenderBlocks renderer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ protected void saveDisplaySettings(NBTTagCompound nbttagcompound) {
nbttagcompound.setByte("thickness", thickness);
nbttagcompound.setByte("powerMode", powerMode);
nbttagcompound.setByte("transparencyMode", transparencyMode);
nbttagcompound.setByte("textRotation", transparencyMode);
nbttagcompound.setByte("textRotation", textRotation);
}

@Override
Expand Down
Loading

0 comments on commit eebc0c9

Please sign in to comment.