6

I'm using Libgdx (Java) and trying to build a 3D model. Currently, I can create a model using already-defined methods, such as modelBuilder.createBox() or modelBuilder.createSphere(). However, I want to build my own sphere using a custom mesh (with vertices calculated based on some basic maths) and then subdivide to increase the number of faces. Eg, start with an icosahdron and then subdivide further to increase the number of faces. I have to do this manually as there doesn't seem to be any simpler way to do it. I want to do this because later want to target these faces individually to apply a unique colour/texture to said face.

enter image description here

I think if someone helps me just make a custom/manual cube, I can probably work out the rest.

My environment is already setup and I currently have this to build a sphere using pre-defined methods:

public class ManagerSphere {

    //Settings
    public String ASSET_TEXTURE_BOX = "data/libgdx.png";
    public float SPHERE_RADIUS = 2f;

    //init
    private GameScreen _gameRef;
    private ModelBuilder modelBuilder = new ModelBuilder();
    private Texture tex;
    private Material mat;
    private ModelInstance boxInstance;
    public AssetManager assets;

    public ManagerSphere(GameScreen gameRef) {
        this._gameRef = gameRef;
        createSphere();
    }


    public void createSphere() {
        this.tex = new Texture("spheretexture02.jpg"); 
        this.mat = new Material(TextureAttribute.createDiffuse(this.tex));

        Model tempSphere;
        tempSphere = modelBuilder.createSphere(SPHERE_RADIUS*2, SPHERE_RADIUS*2, SPHERE_RADIUS*2,
                50,50,
                new Material(),
                VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates
        );

        boxInstance = new ModelInstance(tempSphere,0,0,0); //x,y,z (+ve z = away, -ve z = behind)
        boxInstance.nodes.get(0).parts.get(0).material.set(this.mat);

    }

    public void render(float delta) {

        this._gameRef.modelBatch.render(boxInstance, this._gameRef.environment);

    }

}

In Summary:

  • I need a basic methodology to manually build an object in 3d space. Ideally as icosahedron, but will be happy with a cube to get me started
  • I need to be able to edit this mesh and subdivide to create more faces / triangles.
  • I'd like to be able to target each of these faces and change their colour/texture, most likely using 'meshpart' and its own mesh ID.

Many thanks in advance. J

EDIT: I've created the icosahedron object now using meshbuilder. I used a bit of basic maths to calculate the initial position of the 12 vertices, put that into an array, then created an array of 20 faces, (where each face contains reference to three of the vertices). Here is my new code:

public void createSphere() {

    createFaces();

    int attr = VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates;
    Texture myTexture = new Texture("badlogic.jpg");

    ModelBuilder modelBuilder = new ModelBuilder();
    modelBuilder.begin();
    modelBuilder.manage(myTexture); //make modelbuilder responsible for disposing the texture resource

    Random r = new Random();
    for(Face face : faces) {

        modelBuilder.part("face"+face.id, GL20.GL_TRIANGLES, attr,
                //new Material(TextureAttribute.createDiffuse(myTexture)))
                new Material(ColorAttribute.createDiffuse(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1)))
                .triangle(face.p1.toVector3(), face.p2.toVector3(), face.p3.toVector3());

    }


    boxInstance = new ModelInstance(modelBuilder.end(), 0, 0, 0);

}

However, because I use modelBuilder.part(...).triangle() instead of modelBuilder.part(...).box() I cannot seem to apply a texture to this triangular face, I can only assign a random diffuse colour (see image)

enter image description here

So I still need to

  • Work out how to subdivide each face and change the shape of the icosahedron to be more circular with that subdivision.
  • Apply a texture (from an atlas at some point) to individual triangular faces. Perhaps all faces could have the same texture, but also have a variable colour diffused with it.

EDIT 2:

A bit of maths later, and I've worked out how to edit the mesh and apply N number of subdivisions per face, plus extruding that point outwards from the origin to keep the spherical shape. See image below with 0, 1, and 2, iterations of my subdivision method.

enter image description here

All I need now, is to be able to apply a texture to each individual face, instead of a random colour. Any help? Thanks.

TheChubbyPanda
  • 1,721
  • 2
  • 16
  • 37
Jammo
  • 1,838
  • 4
  • 25
  • 39
  • Why not use the `#sphere` method? It allows you to specify the number of divisions. – Xoppa Jan 03 '16 at 18:29
  • 1
    Sphere will render the mesh with square faces in a grid layout, whereas I want/need the triangular face mesh style. Using square faces make it easier to apply the texture as a Quad (two triangles) but I can't seem to get it working texturing just a trianglular face – Jammo Jan 04 '16 at 09:48
  • The UV coords calculation would be the same. Perhaps look at the source for inspiration (https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g3d/utils/MeshBuilder.java#L1212-L1230). Use the `VertexInfo` argument if you want to specify UV coords for each triangle corner. Note that you can also create the vertices and indices entirely in your own code if you prefer that (`#addMesh(float[], short[])`). – Xoppa Jan 04 '16 at 10:11
  • @Xoppa, I see you're referencing `MeshPartBuilder` and not `ModelBuilder`. Would you recommend using that to build my model instead? – Jammo Jan 04 '16 at 10:50
  • It's the same, see: https://github.com/libgdx/libgdx/wiki/ModelBuilder%2C-MeshBuilder-and-MeshPartBuilder. Never ever use the `createXXX` methods though for anything other than quick testing. – Xoppa Jan 04 '16 at 11:11
  • 1
    Have you got your code for how you implemented it? I would really appreciate it to learn from. – TheChubbyPanda Feb 05 '19 at 20:34

0 Answers0