Skip to content

Commit

Permalink
Work around vanilla font renderer pollution gl state
Browse files Browse the repository at this point in the history
* re #3869
  • Loading branch information
HenryLoenwind committed Dec 1, 2016
1 parent 040dae0 commit a12af1f
Showing 1 changed file with 93 additions and 97 deletions.
190 changes: 93 additions & 97 deletions src/main/java/crazypants/enderio/machine/capbank/render/IoDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public class IoDisplay implements IInfoRenderer {

@Override
public void render(TileCapBank cb, EnumFacing dir, float partialTick) {
if(dir.getFrontOffsetY() != 0) {
if (dir.getFrontOffsetY() != 0) {
return;
}

CapBankClientNetwork nw = (CapBankClientNetwork) cb.getNetwork();
if(nw == null) {
if (nw == null) {
return;
}

CapBankClientNetwork.IOInfo info = nw.getIODisplayInfo(cb.getPos(), dir);
if(info.isInside()) {
if (info.isInside()) {
return;
}

Expand All @@ -54,77 +54,77 @@ public void render(TileCapBank cb, EnumFacing dir, float partialTick) {
GlStateManager.doPolygonOffset(-1.0f, -1.0f);

boolean drawBackground = true;
if(drawBackground) {
if (drawBackground) {
RenderUtil.bindBlockTexture();

float scale = 0.85f;
float offset = (1-scale)/2;
float offset = (1 - scale) / 2;
TextureAtlasSprite icon = ((BlockCapBank) blockCapBank.getBlock()).getInfoPanelIcon();
float minU = icon.getMinU();
float maxU = icon.getMaxU();
float minV = icon.getMinV();
float maxV = icon.getMaxV();

List<Vertex> vertices = new ArrayList<Vertex>();

switch (dir) {
case NORTH: {
float y0 = offset - (info.height-1);
float y1 = 1 - offset;
float x0 = offset;
float x1 = info.width - offset;
float z0 = 0;
vertices.add(new Vertex(x1, y0, z0, minU, minV, 0f, 0f, -1f));
vertices.add(new Vertex(x0, y0, z0, maxU, minV, 0f, 0f, -1f));
vertices.add(new Vertex(x0, y1, z0, maxU, maxV, 0f, 0f, -1f));
vertices.add(new Vertex(x1, y1, z0, minU, maxV, 0f, 0f, -1f));
break;
}

case SOUTH: {
float y0 = offset - (info.height-1);
float y1 = 1 - offset;
float x0 = offset - (info.width-1);
float x1 = 1 - offset;
float z1 = 1;
vertices.add(new Vertex(x0, y0, z1, maxU, minV, 0f, 0f, 1f));
vertices.add(new Vertex(x1, y0, z1, minU, minV, 0f, 0f, 1f));
vertices.add(new Vertex(x1, y1, z1, minU, maxV, 0f, 0f, 1f));
vertices.add(new Vertex(x0, y1, z1, maxU, maxV, 0f, 0f, 1f));
break;
}

case EAST: {
float y0 = offset - (info.height-1);
float y1 = 1 - offset;
float z0 = offset;
float z1 = info.width - offset;
float x1 = 1;
vertices.add(new Vertex(x1, y1, z0, maxU, maxV, 1f, 0f, 0f));
vertices.add(new Vertex(x1, y1, z1, minU, maxV, 1f, 0f, 0f));
vertices.add(new Vertex(x1, y0, z1, minU, minV, 1f, 0f, 0f));
vertices.add(new Vertex(x1, y0, z0, maxU, minV, 1f, 0f, 0f));
break;
}

case WEST: {
float y0 = offset - (info.height-1);
float y1 = 1 - offset;
float z0 = offset - (info.width-1);
float z1 = 1 - offset;
float x0 = 0;
vertices.add(new Vertex(x0, y0, z0, maxU, minV, -1f, 0f, 0f));
vertices.add(new Vertex(x0, y0, z1, minU, minV, -1f, 0f, 0f));
vertices.add(new Vertex(x0, y1, z1, minU, maxV, -1f, 0f, 0f));
vertices.add(new Vertex(x0, y1, z0, maxU, maxV, -1f, 0f, 0f));
break;
}

default:
throw new AssertionError();
case NORTH: {
float y0 = offset - (info.height - 1);
float y1 = 1 - offset;
float x0 = offset;
float x1 = info.width - offset;
float z0 = 0;
vertices.add(new Vertex(x1, y0, z0, minU, minV, 0f, 0f, -1f));
vertices.add(new Vertex(x0, y0, z0, maxU, minV, 0f, 0f, -1f));
vertices.add(new Vertex(x0, y1, z0, maxU, maxV, 0f, 0f, -1f));
vertices.add(new Vertex(x1, y1, z0, minU, maxV, 0f, 0f, -1f));
break;
}

case SOUTH: {
float y0 = offset - (info.height - 1);
float y1 = 1 - offset;
float x0 = offset - (info.width - 1);
float x1 = 1 - offset;
float z1 = 1;
vertices.add(new Vertex(x0, y0, z1, maxU, minV, 0f, 0f, 1f));
vertices.add(new Vertex(x1, y0, z1, minU, minV, 0f, 0f, 1f));
vertices.add(new Vertex(x1, y1, z1, minU, maxV, 0f, 0f, 1f));
vertices.add(new Vertex(x0, y1, z1, maxU, maxV, 0f, 0f, 1f));

break;
}

case EAST: {
float y0 = offset - (info.height - 1);
float y1 = 1 - offset;
float z0 = offset;
float z1 = info.width - offset;
float x1 = 1;
vertices.add(new Vertex(x1, y1, z0, maxU, maxV, 1f, 0f, 0f));
vertices.add(new Vertex(x1, y1, z1, minU, maxV, 1f, 0f, 0f));
vertices.add(new Vertex(x1, y0, z1, minU, minV, 1f, 0f, 0f));
vertices.add(new Vertex(x1, y0, z0, maxU, minV, 1f, 0f, 0f));

break;
}

case WEST: {
float y0 = offset - (info.height - 1);
float y1 = 1 - offset;
float z0 = offset - (info.width - 1);
float z1 = 1 - offset;
float x0 = 0;
vertices.add(new Vertex(x0, y0, z0, maxU, minV, -1f, 0f, 0f));
vertices.add(new Vertex(x0, y0, z1, minU, minV, -1f, 0f, 0f));
vertices.add(new Vertex(x0, y1, z1, minU, maxV, -1f, 0f, 0f));
vertices.add(new Vertex(x0, y1, z0, maxU, maxV, -1f, 0f, 0f));

break;
}

default:
throw new AssertionError();
}

GlStateManager.color(1, 1, 1);
Expand All @@ -144,17 +144,17 @@ public void render(TileCapBank cb, EnumFacing dir, float partialTick) {
float scale = size / fr.FONT_HEIGHT;
float offset;

if(info.height*3 >= info.width*4) {
if (info.height * 3 >= info.width * 4) {
heading1 = HeadingText.INPUT;
heading2 = HeadingText.OUTPUT;
text1 = getChangeText(nw.getAverageInputPerTick(), fr);
text2 = getChangeText(nw.getAverageOutputPerTick(), fr);
offset = -size*2.5f;
offset = -size * 2.5f;
} else {
int change = Math.round(nw.getAverageChangePerTick());
if(change > 0) {
if (change > 0) {
heading1 = HeadingText.GAIN;
} else if(change < 0) {
} else if (change < 0) {
heading1 = HeadingText.LOSS;
}
text1 = getChangeText(change, fr);
Expand All @@ -163,50 +163,46 @@ public void render(TileCapBank cb, EnumFacing dir, float partialTick) {

EnumFacing right = dir.rotateAround(EnumFacing.Axis.Y);

GL11.glPushMatrix();
GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
GL11.glTranslatef(
(dir.getFrontOffsetX() * 1.02f)/2 + 0.5f + right.getFrontOffsetX()*(info.width-1)*0.5f,
1 + size*0.5f - info.height*0.5f,
(dir.getFrontOffsetZ() * 1.02f)/2 + 0.5f + right.getFrontOffsetZ()*(info.width-1)*0.5f);
GL11.glRotatef(-180, 1, 0, 0);
if(dir == EnumFacing.NORTH) {
GL11.glRotatef(-180, 0, 1, 0);
} else if(dir == EnumFacing.EAST) {
GL11.glRotatef(-90, 0, 1, 0);
} else if(dir == EnumFacing.WEST) {
GL11.glRotatef(90, 0, 1, 0);
GlStateManager.pushMatrix();
GlStateManager.translate((dir.getFrontOffsetX() * 1.02f) / 2 + 0.5f + right.getFrontOffsetX() * (info.width - 1) * 0.5f,
1 + size * 0.5f - info.height * 0.5f, (dir.getFrontOffsetZ() * 1.02f) / 2 + 0.5f + right.getFrontOffsetZ() * (info.width - 1) * 0.5f);
GlStateManager.rotate(-180, 1, 0, 0);
if (dir == EnumFacing.NORTH) {
GlStateManager.rotate(-180, 0, 1, 0);
} else if (dir == EnumFacing.EAST) {
GlStateManager.rotate(-90, 0, 1, 0);
} else if (dir == EnumFacing.WEST) {
GlStateManager.rotate(90, 0, 1, 0);
}

GL11.glEnable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_LIGHTING);
OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
GlStateManager.enableLighting();
GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);

offset = drawText(heading1, text1, offset, scale, size, fr);
if(heading2 != null) {
if (heading2 != null) {
drawText(heading2, text2, offset, scale, size, fr);
}
RenderUtil.bindBlockTexture();

GL11.glEnable(GL11.GL_LIGHTING);
GL11.glDisable(GL11.GL_BLEND);
GlStateManager.disableBlend();
GlStateManager.disablePolygonOffset();
GL11.glPopAttrib();
GL11.glPopMatrix();
GlStateManager.popMatrix();
}

private float drawText(HeadingText heading, String text, float offset, float scale, float size, FontRenderer fr) {
GL11.glPushMatrix();
GL11.glTranslatef(0, offset, 0);
GL11.glScalef(scale, scale, scale);
GlStateManager.pushMatrix();
GlStateManager.translate(0, offset, 0);
GlStateManager.scale(scale, scale, scale);
fr.drawString(heading.text, -fr.getStringWidth(heading.text) / 2, 0, 0);
GL11.glPopMatrix();
GlStateManager.popMatrix();
offset += size * 1.5f;

GL11.glPushMatrix();
GL11.glTranslatef(0, offset, 0);
GL11.glScalef(scale, scale, scale);
GlStateManager.pushMatrix();
GlStateManager.translate(0, offset, 0);
GlStateManager.scale(scale, scale, scale);
fr.drawString(text, -fr.getStringWidth(text) / 2, 0, heading.color);
GL11.glPopMatrix();
GlStateManager.popMatrix();
offset += size * 1.5f;

return offset;
Expand All @@ -216,7 +212,7 @@ protected String getChangeText(float average, FontRenderer fr) {
int change = Math.round(Math.abs(average));
String txt = PowerDisplayUtil.formatInteger(change);
int width = fr.getStringWidth(txt);
if(width > 38 && change > 1000) {
if (width > 38 && change > 1000) {
change = change / 1000;
txt = PowerDisplayUtil.formatInteger(change) + "K";
}
Expand Down

0 comments on commit a12af1f

Please sign in to comment.