I can not make removal / addition of cubes created through a mesh

Cards based on the this example: http://hub.jmonkeyengine.org/groups/free-announcements/forum/topic/simple-voxel-engine-starter-kit/

I created a class getCell:

[java]public void getCell(Vector3f point, BulletAppState bulletAppState, boolean lookDown) {
int x, y, z;
//calculates the coordinates of the points that are within the mesh
x = (int) FastMath.floor(Math.abs(point.getX() / 2));
if (lookDown) {
y = (int) FastMath.ceil(Math.abs(point.getY() / 2));
} else {
y = (int) FastMath.floor(Math.abs(point.getY() / 2));
}
z = (int) FastMath.floor(Math.abs(point.getZ() / 2));
//Stored in mapCells 0 and 1, if 1 means there is a cube
mapCells[x][y][z] = 0;
//this.levelGeom.updateModelBound();
mapmesh.clearBuffer(Type.Position);
mapmesh.clearBuffer(Type.Normal);
mapmesh.clearBuffer(Type.TexCoord);
mapmesh.clearBuffer(Type.Index);
mapmesh.clearBuffer(Type.Color);
mapmesh.updateBound();

this.levelGeom.updateModelBound();
this.levelGeom.updateGeometricState();

build(bulletAppState);
}[/java]

I tried to make reconstruction of the whole mesh, but the old mesh is not deleted and the new is drawn over the old = (

PS: Sorry for bad english

Here trying to write, but did not work … Mesh was not removed. Help me please

[java]public void getCell(Vector3f point, BulletAppState bulletAppState, Vector3f walkDirection) {

int x = 0, y = 0, z = 0;

if (walkDirection.getX() > point.getX()) {

if (point.getX() == FastMath.floor(point.getX())) {

x = (int) FastMath.floor(Math.abs((point.getX()) / 2)) - 1;

} else {

x = (int) FastMath.floor(Math.abs(point.getX() / 2));

}

} else {

x = (int) FastMath.floor(Math.abs(point.getX() / 2));

}

if (walkDirection.getY() > point.getY()) {

if (point.getY() == FastMath.floor(point.getY())) {

y = (int) FastMath.floor(Math.abs((point.getY()) / 2)) - 1;

} else {

y = (int) FastMath.floor(Math.abs((point.getY()) / 2));

}

} else {

y = (int) FastMath.floor(Math.abs(point.getY() / 2));

}

if (walkDirection.getZ() > point.getZ()) {

if (point.getZ() == FastMath.floor(point.getZ())) {

z = (int) FastMath.floor(Math.abs((point.getZ()) / 2)) - 1;

} else {

z = (int) FastMath.floor(Math.abs((point.getZ()) / 2));

}

} else {

z = (int) FastMath.floor(Math.abs(point.getZ() / 2));

}

if (mapCells[x][y][z] == 1) {

System.out.println("Abstract points: ( " + x + "; " + y + "; " + z + ")");

} else {

System.out.println("Normal points: ( " + point.getX() + "; " + point.getY() + "; " + point.getZ() + ")");

}



//Обновление мэша



this.detachChild(levelGeom);

bulletAppState.getPhysicsSpace().removeAll(this);

mapCells[0][4][0] = 0;

for (int coord_z = 0; coord_z < lengthZ; coord_z++) {

for (int coord_x = 0; coord_x < lengthX; coord_x++) {

for (int coord_y = 0; coord_y < lengthY; coord_y++) {

if (mapCells[coord_x][coord_y][coord_z] == 1) {

createWalls(checkSix(coord_x, coord_y, coord_z), 1.0f + (coord_x), 0.5f + (coord_y), 0.5f + (coord_z));

}

}

}

}



Vector4f[] c4 = verticesColor.toArray(new Vector4f[verticesColor.size()]);



int colorIndex = 0;

float[] colorArray = new float[verticesColor.size() * 4];

//Set custom RGBA value for each Vertex. Values range from 0.0f to 1.0f

for (int i = 0; i < verticesColor.size(); i++) {

// Red value (is increased by .2 on each next vertex here)

colorArray[colorIndex++] = c4.x; //0.1f+(.2fi);

// Green value (is reduced by .2 on each next vertex)

colorArray[colorIndex++] = c4.y; //0.9f-(0.2f
i);

// Blue value (remains the same in our case)

colorArray[colorIndex++] = c4.z;

// Alpha value (no transparency set here)

colorArray[colorIndex++] = c4.w;

}





Vector3f[] v3 = vertices.toArray(new Vector3f[vertices.size()]);

// Vector3f[] n3 = normals.toArray(new Vector3f[normals.size()]);

Vector2f[] v2 = texCoord.toArray(new Vector2f[texCoord.size()]);

int indx[] = convertIntegers(indexes);

mapmesh.getBuffer(Type.Position).clearUpdateNeeded();

mapmesh.getBuffer(Type.TexCoord).clearUpdateNeeded();

mapmesh.getBuffer(Type.Index).clearUpdateNeeded();

mapmesh.getBuffer(Type.Color).clearUpdateNeeded();

mapmesh.getBuffer(Type.Position).updateData(BufferUtils.createFloatBuffer(v3));

mapmesh.getBuffer(Type.TexCoord).updateData(BufferUtils.createFloatBuffer(v2));

mapmesh.getBuffer(Type.Index).updateData(BufferUtils.createIntBuffer(indx));

mapmesh.getBuffer(Type.Color).updateData(BufferUtils.createFloatBuffer(colorArray));



mapmesh.updateBound();



levelGeom = new Geometry("OurMesh", mapmesh);

Material mat1 = new Material(assetManager, matDefName);

mat1.setTexture("ColorMap", assetManager.loadTexture("Textures/cube.bmp"));

mat1.setBoolean("VertexColor", true);

levelGeom.setMaterial(mat1);

this.attachChild(levelGeom);



landscape = new RigidBodyControl(CollisionShapeFactory.createSingleMeshShape(levelGeom), 0);

levelGeom.addControl(landscape);

bulletAppState.getPhysicsSpace().add(landscape);

}[/java]

hihi,



why do you “recycle” the old mesh? just create a new one :slight_smile:



greetings

andreas

Recycling is actually not a bad idea as the data doesn’t have to be pushed to OpenGL again (which costs bandwidth).

Ok. So is it a good idea to reuse already created (and root node attached) meshes/geomtries?

In our game bloxel I create each “update time” (after chunk changed etc.) a brand new mesh + a geometry. Should I recycle these objects too? What do you think?

My understanding is that if you change the mesh data then it will still need to be resent to the card. What you save is the direct memory churn which affects RAM usage. Direct memory buffers do not influence the garbage collector like regular heap memory does but they still only get garbage collected when the heap is GC’ed.



Therefore, it can be beneficial to reuse them… but I don’t think it affects rendering performance greatly.

ahoehma said:
Ok. So is it a good idea to reuse already created (and root node attached) meshes/geomtries?
In our game bloxel I create each "update time" (after chunk changed etc.) a brand new mesh + a geometry. Should I recycle these objects too? What do you think?

I trying to create a new mesh every update time, but the old mesh is not removed.. I don't understand how to remove the old mesh... detachAllChildren from box node don't remove mesh.. :(
  1. Mesh m
  2. Geometry g1 = new Geometry(m)
  3. node.attach(g1) → scene will have 1 object
  4. node.detachAllChildren() → scene will have 0 object
  5. Mesh m2 (or reuse m)
  6. Geometry g2 = new Geometry(m2||m)
  7. node.attach(g2) → scene will have 1 object
  8. node.detachAllChildren() → scene will have 0 object

    … and so on



    please check this against your code

Ok! your post give me an idea :slight_smile: i know that, but now i could do it :slight_smile:

but this is a new problem: mesh re-created pretty slow. May be this is because i have mesh 32x32 cubes, but i don’t think so… 16x16 chunk rebuild is noticeably too.