Fetch buffer data from existing mesh and create a new mesh with it

Hey,



I’m aiming to give different parts of a sphere different materials. First I create a sphere to get the buffer data. I wrote a method to get parts of the buffer.



The one for index buffer data:



[java]

private int[] getIntBufferPart(VertexBuffer buf, int indexStart, int len)

{

int[] bufferArray = new int[len * buf.getNumComponents()];



int i = indexStart;

int k = 0;



while (i < indexStart + len && i < buf.getNumElements())

{

for (int s = 0; s < 3; s++)

{

bufferArray[k] = (Short) buf.getElementComponent(i, s);

k++;

}



i++;

}



return bufferArray;

}

[/java]





To test the method I did the following:



[java]

int[] intBufferPart = getIntBufferPart(sphereMesh.getBuffer(Type.Index), 0, sphereMesh.getBuffer(Type.Index).getNumElements());

mesh.setBuffer(Type.Index, 3, BufferUtils.createIntBuffer(intBufferPart));

[/java]





After that I update the bound (I acted in accordance with Custom Mesh Shapes | jMonkeyEngine.org).



This is what happens:

Picture of half sphere



When I do this though:



[java]

mesh.setBuffer(sphereMesh.getBuffer(Type.Index));

[/java]



It works just fine.



So I assumed that the problem has to be in my method. But I can’t find any mistake. Could you girls and boys please help me?



Greetings!

Well the index buffer defines wich vertexes are drawn, if you change it, of course it changes the apperance.



What is exactly your problem? For me this loks perfectly like a part of a sphere.

Yes, that’s true, but I actually want the complete sphere, to test the proper working of the method.



The third parameter length is the number of buffer elements.

[java]int[] intBufferPart = getIntBufferPart(sphereMesh.getBuffer(Type.Index), 0, sphereMesh.getBuffer(Type.Index).getNumElements());

[/java]

So I expect the hole sphere to be drawn. What happens is that the right side (in the picture above) is closed. So the polygons have the wrong position and the thick line in the middle are also wrong positioned polygons.

When you’re casting a short to an int, its going to copy the sign bit over to where its located in the int. You want to keep that bit where it is so you do a mask with 0xFFFF before casting.

1 Like
@Momoko_Fan said:
When you're casting a short to an int, its going to copy the sign bit over to where its located in the int. You want to keep that bit where it is so you do a mask with 0xFFFF before casting.


After casting, technically. If in Java you could someShort = someShort & 0xffff it would result in the same value... but you can't because the bit operators all return int.

short s1 = -123;
short s2 = s1 & 0xffff;

...will fail because s1 is up-case to int to be a part of the &.

int i = s1 & 0xffff;

...is therefore trimming off the extended sign bit after s1 is cast to int.

I know I'm kind of splitting hairs but I felt the distinction was important.

Thank you very much! It worked. By the way: is it possible to create a sphere with more than 256 zSamples and radialSamples?