1
\$\begingroup\$

In LibGDX how can I construct a 3D terrain model that has

  • Position
  • Normal
  • Color

The model does not require a texture, it is colored on a per vertex basis. I've looked into LibGDX's ModelBuilder and MeshPartBuilder, but I can't seem to get it to work. I suspect this is a problem with indexing since I'm inexperienced on the subject.

So the question boils down to; In LibGDX, given positions (x, y, z), normals (nx, ny, nz), color (r, g, b, a) how can I construct a mesh / model with these vertex attributes and properly index them?

What I've tried so far:

final int attributes = Usage.Position | Usage.Normal | Usage.ColorPacked;
partBuilder = modelBuilder.part("Terrain", GL20.GL_TRIANGLES, attributes, material);

/*Generate vertices*/
for (int i = 0; i < VERTEX_COUNT; i++) {
    for (int j = 0; j < VERTEX_COUNT; j++) {
        final float x = j / ((float) VERTEX_COUNT - 1) * SIZE;
        final float z = i / ((float) VERTEX_COUNT - 1) * SIZE;
        final float y = MathUtils.random() * 10; // Testing different heights
        /*Position at (x,y,z), normal generated automatically*/
        partBuilder.vertex(new Vector3(x, y, z), null, Color.WHITE, null);
    }
}

/*Generate indices*/
for (short gz = 0; gz < VERTEX_COUNT - 1; gz++) {
    for (short gx = 0; gx < VERTEX_COUNT - 1; gx++) {
        final short topLeft = (short) ((gz * VERTEX_COUNT) + gx);
        final short topRight = (short) (topLeft + 1);
        final short bottomLeft = (short) (((gz + 1) * VERTEX_COUNT) + gx);
        final short bottomRight = (short) (bottomLeft + 1);
        partBuilder.index(topLeft, bottomLeft, topRight, topRight, bottomLeft, bottomRight);
    }
}

This doesn't work as I expected and generates the following result: Image

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.