Updating a mesh's position float buffer with a new one cause a black screen

First off, here’s my setup :

private Geometry meshInstance;
private Geometry referenceMeshInstance;

public static void main(String[] args) {
    FlatShadingMain app = new FlatShadingMain();
    app.start();
}

private Mesh createMesh() {
    return new Box(1, 1, 1);
}

@Override
public void simpleInitApp() {
    flyCam.setMoveSpeed(10f);

    Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    material.setColor("Color", ColorRGBA.Blue);
    material.getAdditionalRenderState().setWireframe(true);
    material.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);

    Mesh mesh = createMesh();
    mesh.clearBuffer(Type.TexCoord);
    mesh.clearBuffer(Type.Color);
    FlatShadedNormalCalculator.computeNormals(mesh, true);

    meshInstance = new Geometry("meshInstance", mesh);
    meshInstance.setMaterial(material);

    Mesh referenceMesh = createMesh();
    referenceMesh.clearBuffer(Type.TexCoord);
    referenceMesh.clearBuffer(Type.Normal);
    referenceMesh.clearBuffer(Type.Index);
    referenceMesh.clearBuffer(Type.Color);
    referenceMeshInstance = new Geometry("referenceMeshInstance", referenceMesh);
    referenceMeshInstance.setLocalTranslation(-3, 0, 0);
    referenceMeshInstance.setMaterial(material);
    
    rootNode.attachChild(meshInstance);
    rootNode.attachChild(referenceMeshInstance);
}

@Override
public void simpleUpdate(float tpf) {
    meshInstance.rotate(0, FastMath.PI * tpf, 0);
}

Notice that I have a reference mesh and that both share a material that disables face culling.

If I try to update a vertex buffer of type position with a new float buffer, the mesh (actually the created geometry) doesn’t appear on the screen, even if the float buffer is a “fake” deep clone of the previous one. Please keep in mind that I also cleared the normals.

FloatBuffer positionFloatBuffer = (FloatBuffer) positionVertexBuffer.getData();        
FloatBuffer positionNewFloatBuffer = FloatBuffer.allocate(positionFloatBuffer.limit());
positionFloatBuffer.rewind();                                                          
                                                                                       
for (int i = 0; i < positionFloatBuffer.limit(); ++i)                                  
    positionNewFloatBuffer.put(positionFloatBuffer.get());                             

mesh.clearBuffer(Type.Index);                           
positionVertexBuffer.updateData(positionNewFloatBuffer);
// positionVertexBuffer.updateData(BufferUtils.clone(positionFloatBuffer)); This works
mesh.updateCounts();
mesh.updateBound(); 

What could I be doing wrong?

Thanks in advance

tried geometry.updateModelBound()?

I just tried it but in vain. Actually, it’s normal that it doesn’t work because I’m already calling updateBound() on the mesh and because the mesh is created and updated before the geometry is instantiated.

[EDIT] I made it work. However, I’m not sure why.

// FloatBuffer positionNewFloatBuffer = FloatBuffer.allocate(targetVertexCount * positionVertexBuffer.getNumComponents());                             
// FloatBuffer normalNewFloatBuffer = FloatBuffer.allocate(targetVertexCount * normalVertexBuffer.getNumComponents());                                 
                                                                                                                                                       
FloatBuffer positionNewFloatBuffer = (FloatBuffer) VertexBuffer.createBuffer(Format.Float, positionVertexBuffer.getNumComponents(), targetVertexCount);
FloatBuffer normalNewFloatBuffer = (FloatBuffer) VertexBuffer.createBuffer(Format.Float, normalVertexBuffer.getNumComponents(), targetVertexCount);    

Behind the scenes, the method VertexBuffer.createBuffer always create a ByteBuffer and then converts it into whatever type requested. Here’s what the two type of buffers look like while inspecting them in Eclipse :

Instantiated using the VertexBuffer.createBuffer method
positionNewFloatBuffer	DirectFloatBufferU  (id=68)	
	address	693817568	
	att	DirectByteBuffer  (id=75)	
		address	693817568	
		att	null	
		bigEndian	false	
		capacity	432	
		cleaner	Cleaner  (id=107)	
		fd	null	
		hb	null	
		isReadOnly	false	
		limit	432	
		mark	-1	
		nativeByteOrder	true	
		offset	0	
		position	0	
	capacity	108	
	hb	null	
	isReadOnly	false	
	limit	108	
	mark	-1	
	offset	0	
	position	0	


Instantiated using the FloatBuffer.allocate	method
positionNewFloatBuffer	HeapFloatBuffer  (id=96)	
	address	0	
	capacity	108	
	hb	(id=104)	
		[0...99]	
		[100...107]	
	isReadOnly	false	
	limit	108	
	mark	-1	
	offset	0	
	position	0	

Notice that the buffer created using the VertexBuffer.createBuffer method contains an att variable which points to a DirectByteBuffer, whereas the other buffer actually contains the data itself.

Anyway, my only question left is why should I flip the buffer before updating the vertex buffer?

Thanks