Search code examples
java3dlibgdx

How to draw a line between two points in LibGDX, in 3D


I'm trying to learn the 3D side of LibGDX, and I've came to a problem. I want to draw a line from 0, 0, -5 to 0, 0, 5. I've tried a few things to make this work.
First I looked to see if I could create a line as a Model. As far as I can see, I can't do this.
What I then realised is that theoretically I can draw a line using a ShapeRenderer. Here's my code to try to do this.

public class Main implements ApplicationListener {
    ...
    public ShapeRenderer srend;
    ...
    @Override
    public void create() {
        ...
        srend = new ShapeRenderer();
        srend.setColor(Color.RED);
        ...
    }

    @Override
    public void render() {
        ...
        srend.begin(ShapeType.Line);
        srend.line(0, 0, -5, 0, 0, 5);
        srend.end();
        ...
    }
    ...
}

But for some reason this doesn't appear to work. I use ShapeRenderers a lot, but it's possible that I'm making a mistake initializing or using it, I don't think that's the problem though.
I've only just started using the 3D part of LibGDX, so I assume the problem is around where I'm drawing the actual line.


Solution

  • Model Builder works well for me.

    ModelBuilder modelBuilder = new ModelBuilder();
    modelBuilder.begin();
    MeshPartBuilder builder = modelBuilder.part("line", 1, 3, new Material());
    builder.setColor(Color.RED);
    builder.line(0.0f, 0.0f, -5.0f, 0.0f, 0.0f, 5.0f);
    lineModel = modelBuilder.end();
    lineInstance = new ModelInstance(lineModel);