# Generating a custom mesh based on two existing meshes

Hi guys,

A quick disclaimer: I’m relatively new to jME/game programming and only recently have begun to dig into the nitty-gritty of meshes. So apologies if I’m trying to do things in a dumb way or asking somewhat dumb questions; hopefully the answers will help me ask less dumb ones in the future!

I’m interested in making fairly simple meshes by combining multiple geometrical primitive meshes and have been looking at the custom mesh tutorial at https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:custom_meshes. For starters, I thought I’d try to combine two cylinders into an “X” shape. So I figured I’d see if something like this would work (this is somewhat verbose but hopefully it’s easy to follow what I’m trying):

Mesh makeCustomXMesh(float size) {

Mesh halfOfTheX = new Cylinder(20, 20, size/5, size*2, true);

Quaternion rotate45DegAroundY = new Quaternion();

rotate45DegAroundY.fromAngleAxis((float)Math.PI/4, new Vector3f(0f,1f,0f));

Quaternion rotateMinus45DegAroundY = new Quaternion();

rotateMinus45DegAroundY.fromAngleAxis(-(float)Math.PI/4, new Vector3f(0f,1f,0f));

List vertexList = new ArrayList();

getVerticesFromMeshAndAddToListWithRotation(vertexList, halfOfTheX, rotate45DegAroundY);

getVerticesFromMeshAndAddToListWithRotation(vertexList, halfOfTheX, rotateMinus45DegAroundY);

Mesh newMesh = new Mesh();

Vector3f[] vertexArray = vertexList.toArray(new Vector3f[2]);

newMesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertexArray));

newMesh.updateBound();

return newMesh;

}

void getVerticesFromMeshAndAddToListWithRotation(List vertexList, Mesh originalMesh, Quaternion rotationQuat) {

int numberOfLegTriangles = originalMesh.getTriangleCount();

Vector3f v1 = new Vector3f(), v2 = new Vector3f(), v3 = new Vector3f();

for (int i=0; i < numberOfLegTriangles; i++) {

originalMesh.getTriangle(i, v1, v2, v3);

v1 = rotationQuat.mult(v1);

v2 = rotationQuat.mult(v2);

v3 = rotationQuat.mult(v3);

vertexList.add(v1);

vertexList.add(v2);

vertexList.add(v3);

}

}

As you may have noticed, I’m not worrying about the index buffer just yet here – I’m not quite sure how best to handle that yet and figured I’d try and see how far this got me.

When I display this mesh (with a simple unshaded material), it appears as only a single cylinder. I can use a different mesh, say a torus, in one of the getVerticesFromMeshAndAddToListWithRotation calls, which demonstrates that the new mesh does combine the original ones in a sense. However, the rotations I’m trying to introduce appear to be ignored, so I’m unable to make the “X” shape I’m looking for.

Obviously I’m looking at things in somewhat of the wrong way. Suggestions?

Thanks!

What are you trying to achieve by merging the meshes?

You should just have to append the buffers (position,index,UV,normal) of the two meshes, but taking note to increment all of the indices of the second mesh so they all start after the 1st mesh’s indices.

If you are trying to improve performance by reducing the object count you can use BatchNode or GeometryBatchFactory to group the meshes. This does the grouping for you to produce a single mesh, but will keep your sanity as well.

Hi Sploreg,

Thanks for the input.

What are you trying to achieve by merging the meshes?

The meshes are passed to a superclass which generates one geometry per provided mesh, then stores the geometries so that a single “thing” represented by the superclass can have multiple geometries selectable by index. The merged mesh strategy was one thought about a way to pass more complex shapes to the existing superclass without having to have it display multiple geometries at once. Not that it’s necessarily the best way!

You should just have to append the buffers (position,index,UV,normal) of the two meshes, but taking note to increment all of the indices of the second mesh so they all start after the 1st mesh’s indices.

Makes sense, I’ll have to give that a try…

If you are trying to improve performance by reducing the object count you can use BatchNode or GeometryBatchFactory to group the meshes. This does the grouping for you to produce a single mesh, but will keep your sanity as well.

Thanks for the tip. Updating the superclass to use GeometryBatchFactory might make more sense even though on first blush it seemed cleaner to have the mesh combination occur in the subclasses instead.

Updating the superclass to use GeometryBatchFactory might make more sense even though on first blush it seemed cleaner to have the mesh combination occur in the subclasses instead.

The GeometryBatchFactory is indeed the magic I was looking for. Of course, I can use it in my subclass as well by generating some temporary geometries from which to produce the merged mesh, though I may factor it into the superclass. Thanks again for the pointers!