Jump to content

Recommended Posts

Posted (edited)

Please help me, I have been trying to do this for a long time, but I can't do it. How can draw a line between two coordinates in the world, for example between two blocks?

I try, but it doesn't work: (calling drawLine in Event RenderWorldLastEvent: drawLine(0,0,0,10,10,10);)

private static void drawLine(double x1, double y1, double z1, double x2, double y2, double z2) 
{
        Tessellator tessellator = Tessellator.getInstance();
        BufferBuilder bufferbuilder = tessellator.getBuilder();
        bufferbuilder.begin(3, DefaultVertexFormats.POSITION_COLOR);
        bufferbuilder.vertex(x1, y1, z1).color(255,0,0, 255).endVertex();
        bufferbuilder.vertex(x2, y2, z2).color(255,0,0, 255).endVertex();
        tessellator.end();
}

 

Edited by Block
Posted (edited)

Hi, help me please. I've been trying to do this for a long time, I've reviewed a bunch of sources, but it doesn't work. I want to draw a line between two coordinates (for example 0, 0, 0 and 10, 10, 10)
I understand that the line is drawn relative to 0 coordinates, and they need to be changed relative to the player (otherwise the line will move with the player)
1. But here's the trouble, the line is not displayed for me, what could be the problem?
2. By the way, the pushMatrix and translated methods of the GlStateManager class are deprecated. What is the actual replacement for this?

My code:

Spoiler





private static void drawLine(float x1, float y1, float z1, float x2, float y2, float z2, float partialTicks) 
{
    Entity entity = Minecraft.getInstance().getRenderViewEntity();
  
    double x_fix = entity.lastTickPosX + (entity.getPosX() - entity.lastTickPosX) * partialTicks;
    double y_fix = entity.lastTickPosY + (entity.getPosY() - entity.lastTickPosY) * partialTicks;
    double z_fix = entity.lastTickPosZ + (entity.getPosZ() - entity.lastTickPosZ) * partialTicks;
    GlStateManager.pushMatrix();
    GlStateManager.translated(-x_fix, -y_fix, -z_fix);

    Tessellator tessellator = Tessellator.getInstance();
    BufferBuilder bufferbuilder = tessellator.getBuffer();

    GlStateManager.lineWidth(5);

    bufferbuilder.begin(GL11.GL_LINE, DefaultVertexFormats.POSITION_COLOR);
    bufferbuilder.pos(x1, y1 , z1).color(255,0,0, 255).endVertex();
    bufferbuilder.pos(x2, y2 , z2).color(255,0,0, 255).endVertex();
    tessellator.draw();

    GlStateManager.lineWidth(1);
    GlStateManager.popMatrix();
}

 

Spoiler





@SubscribeEvent
public static void renderWorld(RenderWorldLastEvent event)
{
  drawLine(0,0,0,50,50,50, event.getPartialTicks());
}

 

P.S. Method drawLine() works because if I display a message to the player, then everything is displayed, but the line is not.

Edited by Block
issue resolved
Posted

IIRC the LINE drawing mode of OpenGL doesn't actually draw anything. (I think that's what the 3 specifies in your begin() call).

It's been a long time since I messed with this, but the drawing mode I used was QUADS (you should use the constants around...somewhere, but numerically it's probably 7) and give your line some amount of thickness.

 

This is some really old code, but has the relevant math.

https://github.com/Draco18s/HarderStuff/blob/master/src/main/java/com/draco18s/hazards/client/HazardsClientEventHandler.java#L422-L451

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

UPDATE:

Hooray!!!! 

I did as I wanted, but there is a problem:

When I turn on 3-person view, my line starts to move by an angle of rotation head, down or up or left or right.

So how do I get the angle of rotation of the camera from the 3rd person?

entity.getPitchYaw () doesn't work because this is the angle of the turn of the head.

 

I get the coordinate shift like so:

Spoiler

Entity entity = Minecraft.getInstance().getRenderViewEntity();

double x_fix = entity.lastTickPosX + (entity.getPosX() - entity.lastTickPosX) * event.getPartialTicks();
double y_fix = entity.lastTickPosY + (entity.getPosY() - entity.lastTickPosY) * event.getPartialTicks();
double z_fix = entity.lastTickPosZ + (entity.getPosZ() - entity.lastTickPosZ) * event.getPartialTicks();

matrixStack.push();
matrixStack.translate(-x_fix, -y_fix, -z_fix);

 

 

  • Block changed the title to [1.16.4] How get angle rotate camera? [Render line in world]
Posted

UPDATE №2:

Two days of searching later, Hooray!!!!

I managed to implement a method that draws a line from block 1 to block 2.

If anyone needs, here is my method:

Spoiler

public static void renderLine(RenderWorldLastEvent event, Vec3d vecStart, Vec3d vecEnd, Color4f color, int lineWidth)
{
    Tessellator tessellator = Tessellator.getInstance();
    BufferBuilder bufferbuilder = tessellator.getBuffer();
    MatrixStack matrixStack = event.getMatrixStack();
    Vector3d projectedView = Minecraft.getInstance().gameRenderer.getActiveRenderInfo().getProjectedView();

    matrixStack.push();
    matrixStack.translate(-projectedView.x, -projectedView.y, -projectedView.z);
    RenderSystem.lineWidth(lineWidth);

    Matrix4f matrix = matrixStack.getLast().getMatrix();

    bufferbuilder.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);
    drawLine(matrix, bufferbuilder, vecStart, vecEnd, color);
    tessellator.draw();

    GlStateManager.lineWidth(1);
    matrixStack.pop();
}

private static void drawLine(Matrix4f matrix, BufferBuilder buffer, Vec3d p1, Vec3d p2, Color4f color) 
{
    buffer.pos(matrix, (float)p1.x + 0.5f, (float)p1.y + 1.0f, (float)p1.z + 0.5f)
      .color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha())
      .endVertex();
    buffer.pos(matrix, (float)p2.x + 0.5f, (float)p2.y, (float)p2.z + 0.5f)
      .color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha())
      .endVertex();
}

 

Call example:

Spoiler

// Draws a line from 0, 0, 0 to 10, 10, 10 (x y z)
Color4f color = new Color4f(0, 1, 0, 1);
renderLine(event, new Vec3d(0, 0, 0), new Vec3d(10, 10, 10), color, 4);

 

 

  • Block changed the title to [resolved] [1.16.4] Render line in world
Posted
2 hours ago, Draco18s said:

IIRC the LINE drawing mode of OpenGL doesn't actually draw anything. (I think that's what the 3 specifies in your begin() call).

It's been a long time since I messed with this, but the drawing mode I used was QUADS (you should use the constants around...somewhere, but numerically it's probably 7) and give your line some amount of thickness.

 

This is some really old code, but has the relevant math.

https://github.com/Draco18s/HarderStuff/blob/master/src/main/java/com/draco18s/hazards/client/HazardsClientEventHandler.java#L422-L451

I decided to roll back the version just below, and I ended up drawing a line: Forum thread

  • Block changed the title to [Solved] [1.16.5] Render line between two blocks

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.

Announcements



×
×
  • Create New...

Important Information

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