Jump to content

[1.10.2] [Solved] Lines render black in the world


Earthcomputer

Recommended Posts

This code is supposed to render lines above a redstone component when it is hovered over showing the inputs and outputs of the component (it should be rendered on top of all the blocks, even if it is behind them). It works sort of, but inconsistently - all the lines turn black or are darker than they should be, and sometimes the lines disappear completely.

 

Picture of the black lines: https://goo.gl/uh7ert

 

Relevant code:

 

 

Post-world-render event listener:

package net.earthcomputer.redbuilder.logic;

import org.lwjgl.opengl.GL11;

import net.earthcomputer.redbuilder.RedBuilderSettings;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class RedstoneLogicDisplayListener {

@SubscribeEvent
public void onRenderBlockOverlay(RenderWorldLastEvent e) {
	if (!RedBuilderSettings.enableRedstonePowerInfo) {
		return;
	}

	Minecraft mc = Minecraft.getMinecraft();
	World world = mc.theWorld;
	EntityPlayer player = mc.thePlayer;
	RayTraceResult target = mc.objectMouseOver;
	if (world == null || player == null || target == null) {
		return;
	}

	if (target.typeOfHit != RayTraceResult.Type.BLOCK) {
		return;
	}
	BlockPos pos = target.getBlockPos();
	RedstonePowerInfo powerInfo = RedstoneComponentRegistry.getPowerInfo(world, pos);

	GlStateManager.pushMatrix();
	GlStateManager.translate(0.5 - player.posX, -player.posY, 0.5 - player.posZ);

	GlStateManager.clear(GL11.GL_DEPTH_BUFFER_BIT);
	for (PowerPath path : powerInfo.genPowerPaths(world, pos, world.getBlockState(pos))) {
		path.draw();
	}

	GlStateManager.popMatrix();
}

}

 

And here is the PowerPath class where the lines are actually rendered:

package net.earthcomputer.redbuilder.logic;

import java.util.Iterator;
import java.util.List;

import org.lwjgl.opengl.GL11;

import com.google.common.collect.Lists;

import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.VertexBuffer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.math.BlockPos;

public class PowerPath {

protected List<BlockPos> points = Lists.newArrayList();
protected List<Integer> colors = Lists.newArrayList();

protected PowerPath(BlockPos startingPoint) {
	points.add(startingPoint);
}

public static PowerPath startPoint(BlockPos startingPoint) {
	return new PowerPath(startingPoint);
}

public PowerPath add(BlockPos point, int color) {
	points.add(point);
	colors.add(color);
	return this;
}

public void draw() {
	if (colors.isEmpty()) {
		return;
	}
	if (points.size() != colors.size() + 1) {
		throw new IllegalStateException("points.size() != colors.size() + 1");
	}

	Tessellator tessellator = Tessellator.getInstance();
	VertexBuffer vertexBuffer = tessellator.getBuffer();

	vertexBuffer.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);

	Iterator<BlockPos> pointIterator = points.iterator();
	Iterator<Integer> colorIterator = colors.iterator();
	BlockPos point = pointIterator.next();
	int color;

	while (pointIterator.hasNext()) {
		color = colorIterator.next();
		drawPoint(vertexBuffer, point, color);
		point = pointIterator.next();
		drawPoint(vertexBuffer, point, color);
	}

	tessellator.draw();
}

private void drawPoint(VertexBuffer buffer, BlockPos point, int color) {
	buffer.pos(point.getX(), point.getY() + 0.5, point.getZ());
	buffer.color((color & 0x00ff0000) >> 16, (color & 0x0000ff00) >> 8, color & 0x000000ff,
			(color & 0xff000000) >>> 24);
	buffer.endVertex();
}

@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((colors == null) ? 0 : colors.hashCode());
	result = prime * result + ((points == null) ? 0 : points.hashCode());
	return result;
}

@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (!(obj instanceof PowerPath))
		return false;
	PowerPath other = (PowerPath) obj;
	if (colors == null) {
		if (other.colors != null)
			return false;
	} else if (!colors.equals(other.colors))
		return false;
	if (points == null) {
		if (other.points != null)
			return false;
	} else if (!points.equals(other.points))
		return false;
	return true;
}

}

 

 

 

An interesting effect is that if you open the chat GUI while looking at a redstone component, the lines blink on and off (very strange). It's almost as if OpenGL is being distracted by the chat cursor so doesn't draw the lines.

 

If you want to reproduce this yourself, all the code is also on github:

https://github.com/Earthcomputer/RedBuilder

If you want to reproduce the behaviour, remember to turn the mod setting called 'Enable Redstone Power Info' to true, or the lines won't ever appear. The lines still may not appear (this is the bug), though it helps to reload the chunks if you want to see them.

 

Thanks in advance for help, if any can be given!

catch(Exception e)

{

 

}

Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).

Link to comment
Share on other sites

Switching to DrawBlockHighlightEvent fixed the lines randomly disappearing (out of interest, why?).

- I have also changed GlStateManager.clear(GL11.GL_DEPTH_BUFFER_BIT) to GlStateManager.disableDepth(), because it was causing other issues.

 

However there is still the problem that the lines are appearing black.

- I have tried GlStateManager.color(1f, 1f, 1f), that didn't fix it.

- I also discovered that when looking at a redstone torch with the debug screen open, the lines turn blue and partially transparent (doesn't work with redstone dust or when the debug screen is closed).

I can't figure out what's causing this problem, more help would be appreciated :)

catch(Exception e)

{

 

}

Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).

Link to comment
Share on other sites

After more code digging, I noticed that when the debug screen is opened, GlStateManager.enableTexture2D() is called, and GlStateManager.disableTexture2D() is not called, leading to the inconsistent behaviour with my lines.

 

So, I fixed all my problems with GlStateManager.disableTexture2D() and GlStateManager.disableDepth().

And we all lived happily ever after.

catch(Exception e)

{

 

}

Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.