[SOLVED] How to remove an vertice

I even feel stupid to ask this, but I try to find any usefull information on wiki and in the forum and I just got this :


In the forum some people talking about its required to build an new buffer and replace the old one with the new, but no tips on how to do it.

So how can I do it ? Using VertexBuffer ? How I remove or add stuff on it ? There is no add or remove, so the javadoc was not usefull as well…

I have an mesh with only Type.Position type on screen, it is composed by points and I need to remove those points one by one, and maybe add some new, so any good example / tip ?

Create new buffer.
Copy data from old buffer to new buffer, skipping data you don’t want.

Probably easier to move everything into an array list if you will be editing it a lot. Else, essentially you are directly editing an array of bytes/floats/whatever and the process will be the same as for that.

I guess I should use the copyElements then ?
So if I want to remove an vertice at index position :

    VertexBuffer buf = ((Geometry)spatial).getMesh().getBuffer(Type.Position);
    VertexBuffer newbuf = new VertexBuffer(Type.Position);

    newbuf.copyElement(0, buf, index);
    newbuf.copyElement(index+1, buf, buf.getNumElements());

I just tried and got the “java.lang.IllegalArgumentException: Buffer format mismatch. Cannot copy” error…
What am I doing wrong ?

Well, your new buffer has no underlying data buffer.

Given your confusion here, it might be best to pull the data out as an array and work with the raw array… then create the new buffer from the new array when done.

What do you mean ?
Create an array of this vertices and then loop into all the vertices in order to create the new buffer adding one by one ?
I think it will have terrible performance dont you think ?

Well, you have two choices… continue like you are seemingly without a basic understanding of how nio buffers work… or get a copy of the array and edit that and have working code. Then see how bad it is.

You don’t need to create an array of the vertices… just grab the buffer and it will already give you a copy of the float[] array.

You haven’t said how often you want to do this or even why you want to do it. So from my perspective the performance problems are irrelevant… especially if it’s not even working.

I have an mesh with something about 10000 points (vertices) on screen representing pickabes the player get on each free space on an maze, and will remove it when player pick it, also, it will run on android, so I just cant run an loop of 10000 for each second or less (average time based on the player movement) just to remove an vertice.
I know how buffer works, I just dont know how is the correct way to do this on jm3, there is an lack of information about this on the docs, wiki, forum etc, I was hoping someone could help me out on this.
As you can see in the code, I have the mesh, I have the position of the vertice I want to delete on the buffer, I just dont know how to do it with out getting too much overhead (looping), so there is two things here, or its impossible to do this in jm3 what is very very bad, or I just dont know how to do it…
Of course, I could create 10000 different meshes with only one vertice and just remove the entire mesh when the player colide with it fixing my problem, but it sounds bad, I really hope there’s a way to do this with jm3 since I already did this in other engines and it really should be something very very easy to do and well documented…

Look at the nio buffer API and see if you can figure out how to delete an element. (Hint: you can’t) This is not a JME issue.

Is your mesh generated? If so, then modify the original data structure and regenerate it.

Else, you will be COPYING the mesh data and leaving out the one part you want to skip. COPYING implies that you have a COMPLETELY NEW DESTINATION FULLY SETUP to copy it into. ie: a new buffer that is originalBufferSize - 1.

THERE IS NO OTHER WAY… unless you can find a removeElement() in nio.Buffer.

It was done exactly the way I’ve described since it’s physically impossible to do it any other way.

I mean, you can reuse the same buffer, too, and then it will just be a bit bigger… but the process is identical if you must use this worst-approach way of modifying the mesh directly.

My mesh is generated yes, I understand that the buffer is made of FloatBuffer, I have no problem with that, I can for example just hide the vertice in this case, I just dont get yet how its organized on it.
I am using vector3f, so I would gess the size of my floatbuffer would be 3x the size of my vector3f array, but there is 2 extra byes on this, what would it be ?


Never mind ! I think I just got an bad day today…
Its working now !

Just for the record, it would be nice to have some more info in the wiki, something like :

If you want to change your mesh, store your FloatBuffer and then change it like :

verticebuffer.put(position, value);  
mesh.setBuffer(Type.Position, 3, verticebuffer);

Well, thank you again !

Why not just hide and reuse vertices instead of removing and adding them? Needs custom shader but should have better performance if you plan to change it often.

Hi prog, its exactly what I am doing.
To complete remove I would have to recreate the buffer, I found out its faster to just hide it.