@pspeed said:
I'm saying that if you make it only about position then you're probably going to have a bad time.

thanks alot pspeed, i’ve made a verrrryy simple custom mesh can I have any critics on it?
also i’d really love if someone could share this code, or add this code if its good to the custom meshes tutorials, so people can understand how to handle multiple shapes.
For the moment you just have to call addSquare on the mesh, at certain positions, then call generateBuffers once after adding squares, its really simple to use, and it only works on x,y plane… but this is a start, and i’d like to share my code if this can help anyone understand cutom meshes i’d be really glad.
i’ll later update the generated buffers when adding a new square to the VerticesHandler.
usage :
[java] MyTerrain2 terrain = new MyTerrain2();
terrain.addSquare(new Vector3f(0,0,0));
terrain.addSquare(new Vector3f(1,0,0));
terrain.addSquare(new Vector3f(2,0,0));
terrain.addSquare(new Vector3f(4,0,0));
terrain.addSquare(new Vector3f(0,1,0));
terrain.addSquare(new Vector3f(0,2,0));
/*terrain.addSquare(new Vector3f(0,0,1));
terrain.addSquare(new Vector3f(0,0,2));*/
terrain.generateMesh();
Geometry geo = new Geometry("OurMesh", terrain); // using our custom mesh object
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
mat.getAdditionalRenderState().setWireframe(true);
geo.setMaterial(mat);
geo.setLocalTranslation(0f, 0f, 0f);
rootNode.attachChild(geo);[/java]
[java]
public class MyTerrain2 extends Mesh{
private MyVerticeHandler verticeHandler = new MyVerticeHandler();
public void addSquare(Vector3f position){
verticeHandler.addSquare(position);
}
public void generateMesh() {
verticeHandler.generateBuffers();
this.setBuffer(Type.Position, 3, verticeHandler.getPositions());
this.setBuffer(Type.TexCoord, 2, verticeHandler.getTexCoords());
this.setBuffer(Type.Index, 3, verticeHandler.getIndexes());
this.updateBound();
}
}[/java]
[java]
public class MyVerticeHandler {
private int maxVertices = 256;
private Map<Integer, MyVertice> vertices = new LinkedHashMap<Integer, MyVertice>(maxVertices);
private int[] verticesIndexes = new int[maxVertices];
int verticeIndex = 0;
int nbIndex = 0;
private float[] positions;
private float[] texCoords;
private int[] indexes;
public void addSquare(Vector3f position) {
Vector3f roundedPosition = MyMath.round(position);
MyVertice vertice1 = null;
MyVertice vertice2 = null;
MyVertice vertice3 = null;
MyVertice vertice4 = null;
//cube position at position
Vector3f position1 = new Vector3f(roundedPosition.x, roundedPosition.y, 0);
Vector3f position2 = new Vector3f(1 + roundedPosition.x, roundedPosition.y, 0);
Vector3f position3 = new Vector3f(roundedPosition.x, 1 + roundedPosition.y, 0);
Vector3f position4 = new Vector3f(1 + roundedPosition.x, 1 + roundedPosition.y, 0);
vertice1 = getVertice(position1);
vertice2 = getVertice(position2);
vertice3 = getVertice(position3);
vertice4 = getVertice(position4);
if (vertice1 == null) {
vertice1 = new MyVertice();
vertice1.setPosition(position1);
vertice1.setTexCoord(new Vector2f(0, 0));
vertice1.setIndex(verticeIndex);
vertices.put(vertice1.hashCode(), vertice1);
verticeIndex++;
}
if (vertice2 == null) {
vertice2 = new MyVertice();
vertice2.setPosition(position2);
vertice2.setTexCoord(new Vector2f(1, 0));
vertice2.setIndex(verticeIndex);
vertices.put(vertice2.hashCode(), vertice2);
verticeIndex++;
}
if (vertice3 == null) {
vertice3 = new MyVertice();
vertice3.setPosition(position3);
vertice3.setTexCoord(new Vector2f(0, 1));
vertice3.setIndex(verticeIndex);
vertices.put(vertice3.hashCode(), vertice3);
verticeIndex++;
}
if (vertice4 == null) {
vertice4 = new MyVertice();
vertice4.setPosition(position4);
vertice4.setTexCoord(new Vector2f(1, 1));
vertice4.setIndex(verticeIndex);
vertices.put(vertice4.hashCode(), vertice4);
verticeIndex++;
}
// 3,1,2, 2,4,3
verticesIndexes[nbIndex] = vertice3.getIndex();
nbIndex++;
verticesIndexes[nbIndex] = vertice1.getIndex();
nbIndex++;
verticesIndexes[nbIndex] = vertice2.getIndex();
nbIndex++;
verticesIndexes[nbIndex] = vertice2.getIndex();
nbIndex++;
verticesIndexes[nbIndex] = vertice4.getIndex();
nbIndex++;
verticesIndexes[nbIndex] = vertice3.getIndex();
nbIndex++;
}
private MyVertice getVertice(Vector3f position) {
return vertices.get(position.hashCode());
}
public Map<Integer, MyVertice> getVertices() {
return vertices;
}
public void generateBuffers() {
if (positions == null) {
positions = new float[nbIndex*3];
}
if (texCoords == null) {
texCoords = new float[nbIndex * 2];
}
if (indexes == null) {
indexes = new int[nbIndex];
}
int i = 0;
for (MyVertice vertice : vertices.values()) {
positions[vertice.getIndex()*3] = vertice.getPosition().x;
positions[(vertice.getIndex()*3) + 1] = vertice.getPosition().y;
positions[(vertice.getIndex()*3) + 2] = vertice.getPosition().z;
texCoords[vertice.getIndex()*2] = vertice.getPosition().x;
texCoords[(vertice.getIndex()*2)+1] = vertice.getPosition().y;
indexes[i] = verticesIndexes[vertice.getIndex()];
i++;
}
}
public int[] getIndexes() {
return verticesIndexes;
}
public void setIndexes(int[] indexes) {
this.verticesIndexes = indexes;
}
public float[] getPositions() {
return positions;
}
public void setPositions(float[] positions) {
this.positions = positions;
}
public float[] getTexCoords() {
return texCoords;
}
public void setTexCoords(float[] texCoords) {
this.texCoords = texCoords;
}
}
[/java]