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.
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 startedI 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)
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.
All I need now, is to be able to apply a texture to each individual face, instead of a random colour. Any help? Thanks.