[Solved] Simple Dynamic Mesh Example with JME3

Thanks to everyone on #jme on IRC. Here’s the solve:



[java]

import java.nio.ShortBuffer;

import java.util.Random;



import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.scene.Geometry;

import com.jme3.scene.Mesh;

import com.jme3.scene.VertexBuffer;

import com.jme3.scene.VertexBuffer.Type;



public class DynamicMesh extends SimpleApplication {

VertexBuffer indexBuffer;

ShortBuffer indices;



@Override

public void simpleInitApp() {

short[] indexbuffer = new short[]{ 1, 2 };

float[] vertexbuffer = new float[]{ 0, 0, 0, 0, 3, 0, 3, 0, 0, -3, 0, 0};

Mesh lineMesh = new Mesh();

lineMesh.setMode(Mesh.Mode.Lines);

lineMesh.setDynamic();



lineMesh.setBuffer(VertexBuffer.Type.Position, 3, vertexbuffer);

lineMesh.setBuffer(VertexBuffer.Type.Index, 1, indexbuffer);



Geometry lineGeometry = new Geometry(“line”, lineMesh);



Material mat = new Material(assetManager,

“Common/MatDefs/Misc/Unshaded.j3md”); // create a simple material

mat.setColor(“Color”, ColorRGBA.Red); // set color of material to blue



lineGeometry.setMaterial(mat);

rootNode.attachChild(lineGeometry);



rootNode.updateGeometricState();



//THESE are the buffers I need.

indexBuffer = (VertexBuffer)lineGeometry.getMesh().getBuffer(Type.Index);

indices = (ShortBuffer)indexBuffer.getData();

}



public void simpleUpdate(float time) {

Random r = new Random();

indices.put(0, (short)r.nextInt(4));

indices.put(1, (short)r.nextInt(4));

indexBuffer.setUpdateNeeded();

}



public static void main(String[] args){

DynamicMesh app = new DynamicMesh();

app.start();

}

}

[/java]



The float/short buffers on the java side aren’t native format so once you set it, they aren’t being watched. You have to get the native buffers from the mesh itself (the vertexbuffer) which isn’t really a vertex - it holds arbitrary data. You can then cast the generic “buffer” to a shortbuffer and use it as such. I then modify these native buffers with the .put method and then tell jme to update their structures. At least this is my interpretation of the solve.



Thanks again all!

1 Like